leases: cleanup TestWithLabels
- don't define a type, but just an ad-hoc struct - use a single slice with test-cases; this allows IDE's to pick up the table as a test-table (which allows (re-)running individual tests) - make use of testify's assert.Equal to compare the results, instead of a DIY loop over the expected values. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
d015c99b2e
commit
1480e3bd4f
@ -24,60 +24,47 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestWithLabels(t *testing.T) {
|
func TestWithLabels(t *testing.T) {
|
||||||
type unitTest struct {
|
testcases := []struct {
|
||||||
name string
|
name string
|
||||||
uut *Lease
|
uut *Lease
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
expected map[string]string
|
expected map[string]string
|
||||||
}
|
}{
|
||||||
|
{
|
||||||
addLabelsToEmptyMap := &unitTest{
|
name: "AddLabelsToEmptyMap",
|
||||||
name: "AddLabelsToEmptyMap",
|
uut: &Lease{},
|
||||||
uut: &Lease{},
|
labels: map[string]string{
|
||||||
labels: map[string]string{
|
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
||||||
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
},
|
||||||
},
|
expected: map[string]string{
|
||||||
expected: map[string]string{
|
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
||||||
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
addLabelsToNonEmptyMap := &unitTest{
|
|
||||||
name: "AddLabelsToNonEmptyMap",
|
|
||||||
uut: &Lease{
|
|
||||||
Labels: map[string]string{
|
|
||||||
"containerd.io/gc.expire": "2015-12-05T00:00:00Z",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
labels: map[string]string{
|
{
|
||||||
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
name: "AddLabelsToNonEmptyMap",
|
||||||
"containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d",
|
uut: &Lease{
|
||||||
},
|
Labels: map[string]string{
|
||||||
expected: map[string]string{
|
"containerd.io/gc.expire": "2015-12-05T00:00:00Z",
|
||||||
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
},
|
||||||
"containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d",
|
},
|
||||||
"containerd.io/gc.expire": "2015-12-05T00:00:00Z",
|
labels: map[string]string{
|
||||||
|
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
||||||
|
"containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d",
|
||||||
|
},
|
||||||
|
expected: map[string]string{
|
||||||
|
"containerd.io/gc.root": "2015-12-04T00:00:00Z",
|
||||||
|
"containerd.io/gc.ref.snapshot.overlayfs": "sha256:87806a591ce894ff5c699c28fe02093d6cdadd6b1ad86819acea05ccb212ff3d",
|
||||||
|
"containerd.io/gc.expire": "2015-12-05T00:00:00Z",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
testcases := []*unitTest{
|
for _, tc := range testcases {
|
||||||
addLabelsToEmptyMap,
|
tc := tc
|
||||||
addLabelsToNonEmptyMap,
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
}
|
err := WithLabels(tc.labels)(tc.uut)
|
||||||
|
|
||||||
for _, testcase := range testcases {
|
|
||||||
testcase := testcase
|
|
||||||
|
|
||||||
t.Run(testcase.name, func(t *testing.T) {
|
|
||||||
f := WithLabels(testcase.labels)
|
|
||||||
|
|
||||||
err := f(testcase.uut)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, tc.uut.Labels, tc.expected)
|
||||||
for k, v := range testcase.expected {
|
|
||||||
assert.Contains(t, testcase.uut.Labels, k)
|
|
||||||
assert.Equal(t, v, testcase.uut.Labels[k])
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user