Skip to content

Commit 5932c5d

Browse files
DavidHurtaclaude
authored andcommitted
lib/resourcebuilder/core: Add tests for updateRNodeWithTLSSettings
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 4577fce commit 5932c5d

1 file changed

Lines changed: 230 additions & 2 deletions

File tree

lib/resourcebuilder/core_test.go

Lines changed: 230 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
"text/template"
1212

1313
"github.com/google/go-cmp/cmp"
14-
"sigs.k8s.io/yaml"
14+
"sigs.k8s.io/kustomize/kyaml/yaml"
15+
k8syaml "sigs.k8s.io/yaml"
1516

1617
corev1 "k8s.io/api/core/v1"
1718
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -213,7 +214,7 @@ func validateGenericOperatorConfigTLSInjected(modified *corev1.ConfigMap, fieldN
213214

214215
// Parse YAML into unstructured map
215216
var obj map[string]interface{}
216-
if err := yaml.Unmarshal([]byte(configYAML), &obj); err != nil {
217+
if err := k8syaml.Unmarshal([]byte(configYAML), &obj); err != nil {
217218
return fmt.Errorf("failed to unmarshal %s: %v", fieldName, err)
218219
}
219220

@@ -602,3 +603,230 @@ servingInfo:
602603
})
603604
}
604605
}
606+
607+
func TestUpdateRNodeWithTLSSettings(t *testing.T) {
608+
tests := []struct {
609+
name string
610+
inputYAML string
611+
tlsConf *tlsConfig
612+
expectedYAML string
613+
expectError bool
614+
}{
615+
{
616+
name: "Delete both cipherSuites and minTLSVersion when not found",
617+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
618+
kind: GenericOperatorConfig
619+
servingInfo:
620+
bindAddress: 0.0.0.0:8443
621+
certFile: /var/serving-cert/tls.crt
622+
keyFile: /var/serving-cert/tls.key
623+
cipherSuites:
624+
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
625+
minTLSVersion: VersionTLS12
626+
`,
627+
tlsConf: &tlsConfig{
628+
minTLSVersion: optional[string]{found: false},
629+
cipherSuites: optional[[]string]{found: false},
630+
},
631+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
632+
kind: GenericOperatorConfig
633+
servingInfo:
634+
bindAddress: 0.0.0.0:8443
635+
certFile: /var/serving-cert/tls.crt
636+
keyFile: /var/serving-cert/tls.key
637+
`,
638+
},
639+
{
640+
name: "Delete only cipherSuites when not found",
641+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
642+
kind: GenericOperatorConfig
643+
servingInfo:
644+
bindAddress: 0.0.0.0:8443
645+
cipherSuites:
646+
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
647+
minTLSVersion: VersionTLS12
648+
`,
649+
tlsConf: &tlsConfig{
650+
minTLSVersion: optional[string]{value: "VersionTLS13", found: true},
651+
cipherSuites: optional[[]string]{found: false},
652+
},
653+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
654+
kind: GenericOperatorConfig
655+
servingInfo:
656+
bindAddress: 0.0.0.0:8443
657+
minTLSVersion: VersionTLS13
658+
`,
659+
},
660+
{
661+
name: "Delete only minTLSVersion when not found",
662+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
663+
kind: GenericOperatorConfig
664+
servingInfo:
665+
bindAddress: 0.0.0.0:8443
666+
cipherSuites:
667+
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
668+
minTLSVersion: VersionTLS12
669+
`,
670+
tlsConf: &tlsConfig{
671+
minTLSVersion: optional[string]{found: false},
672+
cipherSuites: optional[[]string]{value: []string{"TLS_RSA_WITH_AES_128_GCM_SHA256"}, found: true},
673+
},
674+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
675+
kind: GenericOperatorConfig
676+
servingInfo:
677+
bindAddress: 0.0.0.0:8443
678+
cipherSuites:
679+
- TLS_RSA_WITH_AES_128_GCM_SHA256
680+
`,
681+
},
682+
{
683+
name: "Set both fields when found",
684+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
685+
kind: GenericOperatorConfig
686+
servingInfo:
687+
bindAddress: 0.0.0.0:8443
688+
`,
689+
tlsConf: &tlsConfig{
690+
minTLSVersion: optional[string]{value: "VersionTLS13", found: true},
691+
cipherSuites: optional[[]string]{value: []string{"TLS_RSA_WITH_AES_128_GCM_SHA256", "TLS_RSA_WITH_AES_256_GCM_SHA384"}, found: true},
692+
},
693+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
694+
kind: GenericOperatorConfig
695+
servingInfo:
696+
bindAddress: 0.0.0.0:8443
697+
cipherSuites:
698+
- TLS_RSA_WITH_AES_128_GCM_SHA256
699+
- TLS_RSA_WITH_AES_256_GCM_SHA384
700+
minTLSVersion: VersionTLS13
701+
`,
702+
},
703+
{
704+
name: "Set empty string for minTLSVersion when found but empty",
705+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
706+
kind: GenericOperatorConfig
707+
servingInfo:
708+
bindAddress: 0.0.0.0:8443
709+
minTLSVersion: VersionTLS12
710+
`,
711+
tlsConf: &tlsConfig{
712+
minTLSVersion: optional[string]{value: "", found: true},
713+
cipherSuites: optional[[]string]{value: []string{"TLS_RSA_WITH_AES_128_GCM_SHA256"}, found: true},
714+
},
715+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
716+
kind: GenericOperatorConfig
717+
servingInfo:
718+
bindAddress: 0.0.0.0:8443
719+
minTLSVersion: ""
720+
cipherSuites:
721+
- TLS_RSA_WITH_AES_128_GCM_SHA256
722+
`,
723+
},
724+
{
725+
name: "Set empty slice for cipherSuites when found but empty",
726+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
727+
kind: GenericOperatorConfig
728+
servingInfo:
729+
bindAddress: 0.0.0.0:8443
730+
cipherSuites:
731+
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
732+
`,
733+
tlsConf: &tlsConfig{
734+
minTLSVersion: optional[string]{value: "VersionTLS13", found: true},
735+
cipherSuites: optional[[]string]{value: []string{}, found: true},
736+
},
737+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
738+
kind: GenericOperatorConfig
739+
servingInfo:
740+
bindAddress: 0.0.0.0:8443
741+
cipherSuites: []
742+
minTLSVersion: VersionTLS13
743+
`,
744+
},
745+
{
746+
name: "Set both fields to empty values when found but empty",
747+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
748+
kind: GenericOperatorConfig
749+
servingInfo:
750+
bindAddress: 0.0.0.0:8443
751+
cipherSuites:
752+
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
753+
minTLSVersion: VersionTLS12
754+
`,
755+
tlsConf: &tlsConfig{
756+
minTLSVersion: optional[string]{value: "", found: true},
757+
cipherSuites: optional[[]string]{value: []string{}, found: true},
758+
},
759+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
760+
kind: GenericOperatorConfig
761+
servingInfo:
762+
bindAddress: 0.0.0.0:8443
763+
cipherSuites: []
764+
minTLSVersion: ""
765+
`,
766+
},
767+
{
768+
name: "Minimal config with only kind and apiVersion - nothing found - no fields added",
769+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
770+
kind: GenericOperatorConfig
771+
`,
772+
tlsConf: &tlsConfig{
773+
minTLSVersion: optional[string]{found: false},
774+
cipherSuites: optional[[]string]{found: false},
775+
},
776+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
777+
kind: GenericOperatorConfig
778+
servingInfo: {}
779+
`,
780+
},
781+
{
782+
name: "Minimal config with only kind and apiVersion - everything found - fields added",
783+
inputYAML: `apiVersion: operator.openshift.io/v1alpha1
784+
kind: GenericOperatorConfig
785+
`,
786+
tlsConf: &tlsConfig{
787+
minTLSVersion: optional[string]{value: "VersionTLS13", found: true},
788+
cipherSuites: optional[[]string]{value: []string{"TLS_RSA_WITH_AES_128_GCM_SHA256", "TLS_RSA_WITH_AES_256_GCM_SHA384"}, found: true},
789+
},
790+
expectedYAML: `apiVersion: operator.openshift.io/v1alpha1
791+
kind: GenericOperatorConfig
792+
servingInfo:
793+
cipherSuites:
794+
- TLS_RSA_WITH_AES_128_GCM_SHA256
795+
- TLS_RSA_WITH_AES_256_GCM_SHA384
796+
minTLSVersion: VersionTLS13
797+
`,
798+
},
799+
}
800+
801+
for _, tt := range tests {
802+
t.Run(tt.name, func(t *testing.T) {
803+
// Parse input YAML
804+
rnode, err := yaml.Parse(tt.inputYAML)
805+
if err != nil {
806+
t.Fatalf("failed to parse input YAML: %v", err)
807+
}
808+
809+
// Call updateRNodeWithTLSSettings
810+
err = updateRNodeWithTLSSettings(rnode, tt.tlsConf)
811+
812+
// Check error expectation
813+
if (err != nil) != tt.expectError {
814+
t.Errorf("updateRNodeWithTLSSettings() error = %v, expectError %v", err, tt.expectError)
815+
return
816+
}
817+
818+
if err == nil {
819+
// Convert back to YAML string
820+
resultYAML, err := rnode.String()
821+
if err != nil {
822+
t.Fatalf("failed to convert rnode to string: %v", err)
823+
}
824+
825+
// Compare YAML output
826+
if resultYAML != tt.expectedYAML {
827+
t.Errorf("YAML mismatch.\nExpected:\n%s\nGot:\n%s", tt.expectedYAML, resultYAML)
828+
}
829+
}
830+
})
831+
}
832+
}

0 commit comments

Comments
 (0)