@@ -3,7 +3,6 @@ package controllers
33import (
44 "context"
55 "errors"
6- "fmt"
76 "testing"
87
98 "github.com/milvus-io/milvus-operator/apis/milvus.io/v1beta1"
@@ -12,18 +11,75 @@ import (
1211 corev1 "k8s.io/api/core/v1"
1312 k8sErrors "k8s.io/apimachinery/pkg/api/errors"
1413 "k8s.io/apimachinery/pkg/runtime/schema"
14+ "k8s.io/apimachinery/pkg/types"
1515 "sigs.k8s.io/controller-runtime/pkg/client"
16- "sigs.k8s.io/yaml"
1716)
1817
19- func TestReconcileConfigMaps_CreateIfNotfound (t * testing.T ) {
18+ func TestReconcileConfigMaps (t * testing.T ) {
2019 env := newTestEnv (t )
2120 defer env .checkMocks ()
22- r := env .Reconciler
2321 mockClient := env .MockClient
22+ r := env .Reconciler
2423 ctx := env .ctx
2524 mc := env .Inst
2625
26+ // mock reconcileOneConfigMap
27+ bak := reconcileOneConfigMap
28+ defer func () {
29+ reconcileOneConfigMap = bak
30+ }()
31+ var reconcileCMCount int
32+ reconcileOneConfigMap = func (r * MilvusReconciler , ctx context.Context , mc v1beta1.Milvus , namespacedName types.NamespacedName ) error {
33+ reconcileCMCount ++
34+ return nil
35+ }
36+ t .Run ("reconcile only active cm when manual mode enabled" , func (t * testing.T ) {
37+ reconcileCMCount = 0
38+ mc .Spec .Com .EnableManualMode = true
39+ err := r .ReconcileConfigMaps (ctx , mc )
40+ assert .NoError (t , err )
41+ assert .Equal (t , 1 , reconcileCMCount )
42+ })
43+
44+ t .Run ("create if none" , func (t * testing.T ) {
45+ reconcileCMCount = 0
46+ mc .Spec .Com .EnableManualMode = false
47+ mockClient .EXPECT ().
48+ List (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil )
49+ err := r .ReconcileConfigMaps (ctx , mc )
50+ assert .NoError (t , err )
51+ assert .Equal (t , 1 , reconcileCMCount )
52+ })
53+
54+ t .Run ("reconcile all cm when manual mode disabled" , func (t * testing.T ) {
55+ reconcileCMCount = 0
56+ mc .Spec .Com .EnableManualMode = false
57+ mockClient .EXPECT ().
58+ List (gomock .Any (), gomock .Any (), gomock .Any ()).
59+ DoAndReturn (func (ctx , obj any , opts ... any ) error {
60+ list := obj .(* corev1.ConfigMapList )
61+ list .Items = []corev1.ConfigMap {
62+ {}, {}, {},
63+ }
64+ return nil
65+ })
66+ err := r .ReconcileConfigMaps (ctx , mc )
67+ assert .NoError (t , err )
68+ assert .Equal (t , 3 , reconcileCMCount )
69+ })
70+ }
71+
72+ func TestReconcileOneConfigMap_CreateIfNotfound (t * testing.T ) {
73+ env := newTestEnv (t )
74+ defer env .checkMocks ()
75+ r := env .Reconciler
76+ mockClient := env .MockClient
77+ ctx := env .ctx
78+ mc := env .Inst
79+ nn := types.NamespacedName {
80+ Namespace : mc .Namespace ,
81+ Name : mc .GetActiveConfigMap (),
82+ }
2783 // all ok
2884 gomock .InOrder (
2985 mockClient .EXPECT ().
@@ -36,32 +92,35 @@ func TestReconcileConfigMaps_CreateIfNotfound(t *testing.T) {
3692 mockClient .EXPECT ().
3793 Create (gomock .Any (), gomock .Any ()).Return (nil ),
3894 )
39- err := r . ReconcileConfigMaps ( ctx , mc )
95+ err := reconcileOneConfigMap ( r , ctx , mc , nn )
4096 assert .NoError (t , err )
4197
4298 // get failed
4399 mockClient .EXPECT ().
44100 Get (gomock .Any (), gomock .Any (), gomock .AssignableToTypeOf (& corev1.ConfigMap {})).
45101 Return (errors .New ("some network issue" ))
46- err = r . ReconcileConfigMaps ( ctx , mc )
102+ err = reconcileOneConfigMap ( r , ctx , mc , nn )
47103 assert .Error (t , err )
48104
49105 // get failed
50106 mockClient .EXPECT ().
51107 Get (gomock .Any (), gomock .Any (), gomock .AssignableToTypeOf (& corev1.ConfigMap {})).
52108 Return (errors .New ("some network issue" ))
53- err = r . ReconcileConfigMaps ( ctx , mc )
109+ err = reconcileOneConfigMap ( r , ctx , mc , nn )
54110 assert .Error (t , err )
55111}
56112
57- func TestReconcileConfigMaps_Existed (t * testing.T ) {
113+ func TestReconcileOneConfigMap_Existed (t * testing.T ) {
58114 env := newTestEnv (t )
59115 defer env .checkMocks ()
60116 r := env .Reconciler
61117 mockClient := env .MockClient
62118 ctx := env .ctx
63119 mc := env .Inst
64-
120+ nn := types.NamespacedName {
121+ Namespace : mc .Namespace ,
122+ Name : mc .GetActiveConfigMap (),
123+ }
65124 t .Run ("call client.Update if changed configmap" , func (t * testing.T ) {
66125 mc .Spec .HookConf .Data = map [string ]interface {}{
67126 "x" : "y" ,
@@ -83,35 +142,8 @@ func TestReconcileConfigMaps_Existed(t *testing.T) {
83142 Update (gomock .Any (), gomock .Any ()).Return (nil ),
84143 )
85144
86- err := r . ReconcileConfigMaps ( ctx , mc )
145+ err := reconcileOneConfigMap ( r , ctx , mc , nn )
87146 assert .NoError (t , err )
88- mockClient .EXPECT ().
89- Get (gomock .Any (), gomock .Any (), gomock .AssignableToTypeOf (& corev1.ConfigMap {})).
90- DoAndReturn (func (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... any ) error {
91- cm := obj .(* corev1.ConfigMap )
92- cm .Name = "cm1"
93- cm .Namespace = "ns"
94- err := mockClient .Get (ctx , client .ObjectKeyFromObject (cm ), cm )
95- if err != nil {
96- return err
97- }
98- if len (cm .Data ) == 0 {
99- return errors .New ("expect data in configmap" )
100- }
101- if _ , ok := cm .Data ["hook.yaml" ]; ! ok {
102- return errors .New ("expect hook.yaml as key in data" )
103- }
104- expected , err := yaml .Marshal (map [string ]string {
105- "x" : "y" ,
106- })
107- if err != nil {
108- return err
109- }
110- if cm .Data ["hook.yaml" ] != string (expected ) {
111- return fmt .Errorf ("content not match, expected: %s, got: %s" , cm .Data ["hook.yaml" ], string (expected ))
112- }
113- return nil
114- })
115147 })
116148
117149 t .Run ("not call client.Update if configmap not changed" , func (t * testing.T ) {
@@ -130,7 +162,7 @@ func TestReconcileConfigMaps_Existed(t *testing.T) {
130162 Get (gomock .Any (), gomock .Any (), gomock .AssignableToTypeOf (& corev1.Secret {})).
131163 Return (k8sErrors .NewNotFound (schema.GroupResource {}, "mockErr" )).Times (2 ),
132164 )
133- err := r . ReconcileConfigMaps ( ctx , mc )
165+ err := reconcileOneConfigMap ( r , ctx , mc , nn )
134166 assert .NoError (t , err )
135167 })
136168
@@ -150,7 +182,7 @@ func TestReconcileConfigMaps_Existed(t *testing.T) {
150182 Get (gomock .Any (), gomock .Any (), gomock .AssignableToTypeOf (& corev1.Secret {})).
151183 Return (k8sErrors .NewNotFound (schema.GroupResource {}, "mockErr" )).Times (2 ),
152184 )
153- err := r . ReconcileConfigMaps ( ctx , mc )
185+ err := reconcileOneConfigMap ( r , ctx , mc , nn )
154186 assert .NoError (t , err )
155187 })
156188
0 commit comments