Skip to content

Commit 5fffb0f

Browse files
committed
Verify that mover can handle a nil mutator
1 parent 0908bfc commit 5fffb0f

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

cmd/clusterctl/client/cluster/mover_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
33+
"k8s.io/apimachinery/pkg/runtime"
3334
"k8s.io/apimachinery/pkg/types"
3435
"k8s.io/apimachinery/pkg/util/sets"
3536
"k8s.io/apimachinery/pkg/util/wait"
@@ -1876,7 +1877,6 @@ func Test_objectMoverService_ensureNamespaces(t *testing.T) {
18761877
expectedNamespaces: []string{"namespace-1", "namespace-2"},
18771878
},
18781879
{
1879-
18801880
name: "ensureNamespaces moves namespace-2 to target which already has namespace-1",
18811881
fields: fields{
18821882
objs: cluster2.Objs(),
@@ -2408,3 +2408,48 @@ func TestWaitReadyForMove(t *testing.T) {
24082408
})
24092409
}
24102410
}
2411+
2412+
func Test_applyMutators(t *testing.T) {
2413+
tests := []struct {
2414+
name string
2415+
object client.Object
2416+
mutators []ResourceMutatorFunc
2417+
want *unstructured.Unstructured
2418+
wantErr bool
2419+
}{
2420+
{
2421+
name: "do nothing if object is nil",
2422+
},
2423+
{
2424+
name: "do nothing if mutators is a nil slice",
2425+
object: test.NewFakeCluster("example", "example").Objs()[0],
2426+
want: func() *unstructured.Unstructured {
2427+
g := NewWithT(t)
2428+
obj := test.NewFakeCluster("example", "example").Objs()[0]
2429+
u := &unstructured.Unstructured{}
2430+
to, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
2431+
g.Expect(err).NotTo(HaveOccurred())
2432+
u.SetUnstructuredContent(to)
2433+
return u
2434+
}(),
2435+
},
2436+
{
2437+
name: "return error if any element in mutators slice is nil ",
2438+
mutators: []ResourceMutatorFunc{nil},
2439+
object: test.NewFakeCluster("example", "example").Objs()[0],
2440+
wantErr: true,
2441+
},
2442+
}
2443+
for _, tt := range tests {
2444+
t.Run(tt.name, func(t *testing.T) {
2445+
g := NewWithT(t)
2446+
got, err := applyMutators(tt.object, tt.mutators...)
2447+
g.Expect(got).To(Equal(tt.want))
2448+
if tt.wantErr {
2449+
g.Expect(err).To(HaveOccurred())
2450+
} else {
2451+
g.Expect(err).NotTo(HaveOccurred())
2452+
}
2453+
})
2454+
}
2455+
}

0 commit comments

Comments
 (0)