@@ -5,14 +5,81 @@ import (
55
66 . "github.com/onsi/gomega"
77
8+ configv1 "github.com/openshift/api/config/v1"
9+
810 "github.com/openshift/library-go/pkg/operator/configobserver/featuregates"
911)
1012
13+ var mockEmptyFeatureGates = featuregates .NewFeatureGate ([]configv1.FeatureGateName {}, []configv1.FeatureGateName {})
14+ var mockEnabledFeatureGates = featuregates .NewFeatureGate ([]configv1.FeatureGateName {"AWSServiceLBNetworkSecurityGroup" }, []configv1.FeatureGateName {})
15+ var mockDisabledFeatureGates = featuregates .NewFeatureGate ([]configv1.FeatureGateName {}, []configv1.FeatureGateName {"AWSServiceLBNetworkSecurityGroup" })
16+
17+ func TestIsFeatureGateEnabled (t * testing.T ) {
18+ testCases := []struct {
19+ name string
20+ features featuregates.FeatureGate
21+ featureName string
22+ expected bool
23+ }{
24+ {
25+ name : "returns false when features is nil" ,
26+ features : nil ,
27+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
28+ expected : false ,
29+ },
30+ {
31+ name : "returns false when feature name is empty string" ,
32+ features : mockEnabledFeatureGates ,
33+ featureName : "" ,
34+ expected : false ,
35+ },
36+ {
37+ name : "returns false when feature is not registered (empty feature gate)" ,
38+ features : mockEmptyFeatureGates ,
39+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
40+ expected : false ,
41+ },
42+ {
43+ name : "returns true when feature is enabled" ,
44+ features : mockEnabledFeatureGates ,
45+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
46+ expected : true ,
47+ },
48+ {
49+ name : "returns false when feature is explicitly disabled" ,
50+ features : mockDisabledFeatureGates ,
51+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
52+ expected : false ,
53+ },
54+ {
55+ name : "returns false when feature is not in known features list" ,
56+ features : featuregates .NewFeatureGate ([]configv1.FeatureGateName {"SomeOtherFeature" }, []configv1.FeatureGateName {}),
57+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
58+ expected : false ,
59+ },
60+ {
61+ name : "returns false for unknown feature with disabled features registered" ,
62+ features : featuregates .NewFeatureGate ([]configv1.FeatureGateName {}, []configv1.FeatureGateName {"SomeOtherFeature" }),
63+ featureName : "AWSServiceLBNetworkSecurityGroup" ,
64+ expected : false ,
65+ },
66+ }
67+
68+ for _ , tc := range testCases {
69+ t .Run (tc .name , func (t * testing.T ) {
70+ g := NewWithT (t )
71+ result := isFeatureGateEnabled (tc .features , tc .featureName )
72+ g .Expect (result ).To (Equal (tc .expected ), "Expected isFeatureGateEnabled to return %v for feature '%s'" , tc .expected , tc .featureName )
73+ })
74+ }
75+ }
76+
1177func TestCloudConfigTransformer (t * testing.T ) {
1278 testCases := []struct {
1379 name string
1480 source string
1581 expected string
82+ features featuregates.FeatureGate
1683 }{
1784 {
1885 name : "default source" ,
@@ -23,6 +90,7 @@ DisableSecurityGroupIngress = false
2390ClusterServiceLoadBalancerHealthProbeMode = Shared
2491ClusterServiceSharedLoadBalancerHealthProbePort = 0
2592` ,
93+ features : mockEmptyFeatureGates ,
2694 },
2795 {
2896 name : "completely empty source" ,
@@ -32,6 +100,7 @@ DisableSecurityGroupIngress = false
32100ClusterServiceLoadBalancerHealthProbeMode = Shared
33101ClusterServiceSharedLoadBalancerHealthProbePort = 0
34102` ,
103+ features : mockEmptyFeatureGates ,
35104 },
36105 {
37106 name : "with existing configuration" ,
@@ -45,6 +114,7 @@ DisableSecurityGroupIngress = true
45114ClusterServiceLoadBalancerHealthProbeMode = Shared
46115ClusterServiceSharedLoadBalancerHealthProbePort = 0
47116` , // Ordered based on the order of fields in the AWS CloudConfig struct.
117+ features : mockEmptyFeatureGates ,
48118 },
49119 {
50120 name : "with existing configuration and overrides" ,
@@ -82,14 +152,44 @@ Region = us-west-1
82152URL = https://s3.foo.bar
83153SigningRegion = signing_region
84154` , // Ordered based on the order of fields in the AWS CloudConfig struct.
155+ features : mockEmptyFeatureGates ,
156+ },
157+ {
158+ name : "with AWSServiceLBNetworkSecurityGroup feature gate enabled" ,
159+ source : `[Global]
160+ DisableSecurityGroupIngress = true
161+ Zone = Foo
162+ ` ,
163+ expected : `[Global]
164+ Zone = Foo
165+ DisableSecurityGroupIngress = true
166+ ClusterServiceLoadBalancerHealthProbeMode = Shared
167+ ClusterServiceSharedLoadBalancerHealthProbePort = 0
168+ NLBSecurityGroupMode = Managed
169+ ` ,
170+ features : mockEnabledFeatureGates ,
171+ },
172+ {
173+ name : "with AWSServiceLBNetworkSecurityGroup feature gate disabled" ,
174+ source : `[Global]
175+ DisableSecurityGroupIngress = true
176+ Zone = Foo
177+ ` ,
178+ expected : `[Global]
179+ Zone = Foo
180+ DisableSecurityGroupIngress = true
181+ ClusterServiceLoadBalancerHealthProbeMode = Shared
182+ ClusterServiceSharedLoadBalancerHealthProbePort = 0
183+ ` ,
184+ features : mockDisabledFeatureGates ,
85185 },
86186 }
87187
88188 for _ , tc := range testCases {
89189 t .Run (tc .name , func (t * testing.T ) {
90190 g := NewWithT (t )
91191
92- gotConfig , err := CloudConfigTransformer (tc .source , nil , nil , featuregates . NewFeatureGate ( nil , nil ) ) // No Infra or Network are required for the current functionality.
192+ gotConfig , err := CloudConfigTransformer (tc .source , nil , nil , tc . features ) // No Infra or Network are required for the current functionality.
93193 g .Expect (err ).ToNot (HaveOccurred ())
94194
95195 g .Expect (gotConfig ).To (Equal (tc .expected ))
0 commit comments