Skip to content

Commit c4dee23

Browse files
committed
Fix order of operations when setting lease labels
Fixes an edge case where `WithLabels` would overwrite `WithExpiration` on a lease when using client options. Signed-off-by: Austin Vazquez <[email protected]>
1 parent 722df11 commit c4dee23

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

leases/lease.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ func SynchronousDelete(ctx context.Context, o *DeleteOptions) error {
6565
return nil
6666
}
6767

68-
// WithLabels sets labels on a lease
68+
// WithLabels merges labels on a lease
6969
func WithLabels(labels map[string]string) Opt {
7070
return func(l *Lease) error {
71-
l.Labels = labels
71+
if l.Labels == nil {
72+
l.Labels = map[string]string{}
73+
}
74+
for k, v := range labels {
75+
l.Labels[k] = v
76+
}
7277
return nil
7378
}
7479
}

leases/lease_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)