@@ -29,35 +29,44 @@ metadata:
2929func TestModifyConfigMapWithBuilder (t * testing.T ) {
3030
3131 tests := []struct {
32- name string
33- configMap * corev1.ConfigMap
34- apiServer * configv1.APIServer
35- expectYAML string // Expected YAML content after modification
32+ name string
33+ configMap * corev1.ConfigMap
34+ apiServer * configv1.APIServer
35+ expectYAML string // Expected YAML content after modification
36+ expectError bool // Whether an error is expected from Do()
3637 }{
3738 {
3839 name : "ConfigMap with GenericOperatorConfig - TLS injection" ,
3940 configMap : makeConfigMap (true , map [string ]string {
40- "config.yaml" : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
41+ genericOperatorConfigCMKey : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
4142 }),
4243 apiServer : makeAPIServerConfig (withCustomTLSProfile (testOpenSSLCipherSuites2 , configv1 .VersionTLS13 )),
4344 expectYAML : makeGenericOperatorConfigYAML (testCipherSuites2 , tlsVersion13 ),
4445 },
4546 {
4647 name : "ConfigMap without annotation - no modification" ,
4748 configMap : makeConfigMap (false , map [string ]string {
48- "config.yaml" : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
49+ genericOperatorConfigCMKey : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
4950 }),
5051 apiServer : makeAPIServerConfig (withCustomTLSProfile (testOpenSSLCipherSuites2 , configv1 .VersionTLS13 )),
5152 expectYAML : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion12 ),
5253 },
5354 {
5455 name : "ConfigMap with non-GenericOperatorConfig - no modification" ,
5556 configMap : makeConfigMap (true , map [string ]string {
56- "config.yaml" : noGenericOperatorConfigYAML ,
57+ genericOperatorConfigCMKey : noGenericOperatorConfigYAML ,
5758 }),
5859 apiServer : makeAPIServerConfig (withCustomTLSProfile (testOpenSSLCipherSuites , configv1 .VersionTLS13 )),
5960 expectYAML : noGenericOperatorConfigYAML ,
6061 },
62+ {
63+ name : "ConfigMap with annotation - APIServer not found - error expected" ,
64+ configMap : makeConfigMap (true , map [string ]string {
65+ genericOperatorConfigCMKey : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion13 ),
66+ }),
67+ expectYAML : makeGenericOperatorConfigYAML (testCipherSuites , tlsVersion13 ),
68+ expectError : true ,
69+ },
6170 }
6271
6372 for _ , tt := range tests {
@@ -73,6 +82,13 @@ func TestModifyConfigMapWithBuilder(t *testing.T) {
7382 // Create fake kubernetes client
7483 fakeKubeClient := fakekubernetes .NewClientset ()
7584
85+ // Create the ConfigMap in the fake client before running the builder
86+ ctx := context .Background ()
87+ _ , err := fakeKubeClient .CoreV1 ().ConfigMaps (tt .configMap .Namespace ).Create (ctx , tt .configMap , metav1.CreateOptions {})
88+ if err != nil {
89+ t .Fatalf ("Failed to create ConfigMap: %v" , err )
90+ }
91+
7692 // Marshal ConfigMap to YAML bytes
7793 configMapYAML , err := yaml .Marshal (tt .configMap )
7894 if err != nil {
@@ -87,20 +103,23 @@ func TestModifyConfigMapWithBuilder(t *testing.T) {
87103 }
88104
89105 // Call Do()
90- ctx := context .Background ()
91106 err = b .Do (ctx )
92- if err != nil {
93- t .Fatalf ("Do() unexpected error: %v" , err )
107+
108+ // Check error expectation
109+ if (err != nil ) != tt .expectError {
110+ t .Fatalf ("Do() error = %v, expectError %v" , err , tt .expectError )
94111 }
95112
96- // Retrieve the applied ConfigMap from the fake client
113+ // ConfigMap should always be retrievable, regardless of error
97114 appliedConfigMap , err := fakeKubeClient .CoreV1 ().ConfigMaps (tt .configMap .Namespace ).Get (ctx , tt .configMap .Name , metav1.GetOptions {})
98115 if err != nil {
99116 t .Fatalf ("Failed to get applied ConfigMap: %v" , err )
100117 }
101118
102119 // Verify the result matches expected YAML
103- modifiedYAML := appliedConfigMap .Data ["config.yaml" ]
120+ // When Do() errors, the ConfigMap should remain unchanged (expectYAML has the original values)
121+ // When Do() succeeds, the ConfigMap should have modified values (expectYAML has the modified values)
122+ modifiedYAML := appliedConfigMap .Data [genericOperatorConfigCMKey ]
104123 if diff := cmp .Diff (tt .expectYAML , modifiedYAML ); diff != "" {
105124 t .Errorf ("ConfigMap YAML mismatch (-want +got):\n %s" , diff )
106125 }
0 commit comments