|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package leases |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestWithLabels(t *testing.T) { |
| 27 | + type unitTest struct { |
| 28 | + name string |
| 29 | + uut *Lease |
| 30 | + labels map[string]string |
| 31 | + expected map[string]string |
| 32 | + } |
| 33 | + |
| 34 | + addLabelsToEmptyMap := &unitTest{ |
| 35 | + name: "AddLabelsToEmptyMap", |
| 36 | + uut: &Lease{}, |
| 37 | + labels: map[string]string{ |
| 38 | + "containerd.io/gc.root": "2015-12-04T00:00:00Z", |
| 39 | + }, |
| 40 | + expected: map[string]string{ |
| 41 | + "containerd.io/gc.root": "2015-12-04T00:00:00Z", |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + addLabelsToNonEmptyMap := &unitTest{ |
| 46 | + name: "AddLabelsToNonEmptyMap", |
| 47 | + uut: &Lease{ |
| 48 | + Labels: map[string]string{ |
| 49 | + "containerd.io/gc.expire": "2015-12-05T00:00:00Z", |
| 50 | + }, |
| 51 | + }, |
| 52 | + labels: map[string]string{ |
| 53 | + "containerd.io/gc.root": "2015-12-04T00:00:00Z", |
| 54 | + "containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d", |
| 55 | + }, |
| 56 | + expected: map[string]string{ |
| 57 | + "containerd.io/gc.root": "2015-12-04T00:00:00Z", |
| 58 | + "containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d", |
| 59 | + "containerd.io/gc.expire": "2015-12-05T00:00:00Z", |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + testcases := []*unitTest{ |
| 64 | + addLabelsToEmptyMap, |
| 65 | + addLabelsToNonEmptyMap, |
| 66 | + } |
| 67 | + |
| 68 | + for _, testcase := range testcases { |
| 69 | + testcase := testcase |
| 70 | + |
| 71 | + t.Run(testcase.name, func(t *testing.T) { |
| 72 | + f := WithLabels(testcase.labels) |
| 73 | + |
| 74 | + err := f(testcase.uut) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + for k, v := range testcase.expected { |
| 78 | + assert.Contains(t, testcase.uut.Labels, k) |
| 79 | + assert.Equal(t, v, testcase.uut.Labels[k]) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments