Skip to content

Commit 2e1498d

Browse files
author
sami-wazery
committed
fix tests
1 parent 909a650 commit 2e1498d

File tree

8 files changed

+500
-615
lines changed

8 files changed

+500
-615
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/gobuffalo/flect v1.0.3
88
github.com/google/go-cmp v0.7.0
99
github.com/onsi/ginkgo v1.16.5
10+
github.com/onsi/ginkgo/v2 v2.23.4
1011
github.com/onsi/gomega v1.38.0
1112
github.com/spf13/cobra v1.9.1
1213
github.com/spf13/pflag v1.0.7
@@ -40,9 +41,11 @@ require (
4041
github.com/go-openapi/jsonpointer v0.21.0 // indirect
4142
github.com/go-openapi/jsonreference v0.20.2 // indirect
4243
github.com/go-openapi/swag v0.23.0 // indirect
44+
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
4345
github.com/gogo/protobuf v1.3.2 // indirect
4446
github.com/google/cel-go v0.26.0 // indirect
4547
github.com/google/gnostic-models v0.7.0 // indirect
48+
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 // indirect
4649
github.com/google/uuid v1.6.0 // indirect
4750
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 // indirect
4851
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -70,6 +73,7 @@ require (
7073
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
7174
go.opentelemetry.io/otel/trace v1.35.0 // indirect
7275
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
76+
go.uber.org/automaxprocs v1.6.0 // indirect
7377
go.yaml.in/yaml/v2 v2.4.2 // indirect
7478
go.yaml.in/yaml/v3 v3.0.4 // indirect
7579
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
4444
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
4545
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
4646
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
47-
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
4847
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
4948
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
5049
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
@@ -133,6 +132,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
133132
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
134133
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
135134
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
135+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
136+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
136137
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
137138
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
138139
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=

pkg/crd/markers/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ var ValidationIshMarkers = []*definitionWithHelp{
127127
WithHelp(XPreserveUnknownFields{}.Help()),
128128
must(markers.MakeDefinition("kubebuilder:pruning:PreserveUnknownFields", markers.DescribesType, XPreserveUnknownFields{})).
129129
WithHelp(XPreserveUnknownFields{}.Help()),
130-
130+
131131
must(markers.MakeDefinition("kubebuilder:feature-gate", markers.DescribesField, FeatureGate(""))).
132132
WithHelp(FeatureGate("").Help()),
133133
}

pkg/crd/testdata/featuregates/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
//go:generate ../../../../.run-controller-gen.sh crd:featureGates=alpha=true paths=. output:dir=./output_alpha
18-
//go:generate ../../../../.run-controller-gen.sh crd:featureGates=beta=true paths=. output:dir=./output_beta
17+
//go:generate ../../../../.run-controller-gen.sh crd:featureGates="alpha=true" paths=. output:dir=./output_alpha
18+
//go:generate ../../../../.run-controller-gen.sh crd:featureGates="beta=true" paths=. output:dir=./output_beta
19+
//go:generate ../../../../.run-controller-gen.sh crd:featureGates="alpha=true,beta=true" paths=. output:dir=./output_both
1920
//go:generate ../../../../.run-controller-gen.sh crd paths=. output:dir=./output_none
2021

2122
package featuregates
2223

2324
import (
24-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
)
2627

2728
// FeatureGateTestSpec defines the desired state with feature-gated fields
@@ -33,7 +34,7 @@ type FeatureGateTestSpec struct {
3334
// +kubebuilder:feature-gate=alpha
3435
AlphaFeature *string `json:"alphaFeature,omitempty"`
3536

36-
// Beta-gated field - only included when beta gate is enabled
37+
// Beta-gated field - only included when beta gate is enabled
3738
// +kubebuilder:feature-gate=beta
3839
BetaFeature *string `json:"betaFeature,omitempty"`
3940
}

pkg/featuregate/evaluator_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package featuregate_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
23+
"sigs.k8s.io/controller-tools/pkg/featuregate"
24+
)
25+
26+
var _ = Describe("FeatureGate Evaluator", func() {
27+
var evaluator *featuregate.FeatureGateEvaluator
28+
var gates featuregate.FeatureGateMap
29+
30+
BeforeEach(func() {
31+
gates = featuregate.FeatureGateMap{
32+
"alpha": true,
33+
"beta": false,
34+
"gamma": true,
35+
"delta": false,
36+
}
37+
evaluator = featuregate.NewFeatureGateEvaluator(gates)
38+
})
39+
40+
Describe("NewFeatureGateEvaluator", func() {
41+
It("should create evaluator with provided gates", func() {
42+
Expect(evaluator).NotTo(BeNil())
43+
})
44+
})
45+
46+
Describe("FeatureGateMap.IsEnabled", func() {
47+
It("should return true for enabled gates", func() {
48+
Expect(gates.IsEnabled("alpha")).To(BeTrue())
49+
Expect(gates.IsEnabled("gamma")).To(BeTrue())
50+
})
51+
52+
It("should return false for disabled gates", func() {
53+
Expect(gates.IsEnabled("beta")).To(BeFalse())
54+
Expect(gates.IsEnabled("delta")).To(BeFalse())
55+
})
56+
57+
It("should return false for unknown gates", func() {
58+
Expect(gates.IsEnabled("unknown")).To(BeFalse())
59+
})
60+
})
61+
62+
Describe("EvaluateExpression", func() {
63+
Context("with empty expressions", func() {
64+
It("should return true for empty string", func() {
65+
Expect(evaluator.EvaluateExpression("")).To(BeTrue())
66+
})
67+
})
68+
69+
Context("with single gate expressions", func() {
70+
It("should evaluate enabled gates correctly", func() {
71+
Expect(evaluator.EvaluateExpression("alpha")).To(BeTrue())
72+
Expect(evaluator.EvaluateExpression("gamma")).To(BeTrue())
73+
})
74+
75+
It("should evaluate disabled gates correctly", func() {
76+
Expect(evaluator.EvaluateExpression("beta")).To(BeFalse())
77+
Expect(evaluator.EvaluateExpression("delta")).To(BeFalse())
78+
})
79+
80+
It("should evaluate unknown gates as false", func() {
81+
Expect(evaluator.EvaluateExpression("unknown")).To(BeFalse())
82+
})
83+
})
84+
85+
Context("with OR expressions", func() {
86+
It("should return true when any gate is enabled", func() {
87+
Expect(evaluator.EvaluateExpression("alpha|beta")).To(BeTrue())
88+
Expect(evaluator.EvaluateExpression("beta|gamma")).To(BeTrue())
89+
Expect(evaluator.EvaluateExpression("alpha|gamma")).To(BeTrue())
90+
})
91+
92+
It("should return false when all gates are disabled", func() {
93+
Expect(evaluator.EvaluateExpression("beta|delta")).To(BeFalse())
94+
})
95+
96+
It("should handle multiple OR gates", func() {
97+
Expect(evaluator.EvaluateExpression("beta|delta|alpha")).To(BeTrue())
98+
Expect(evaluator.EvaluateExpression("beta|delta|unknown")).To(BeFalse())
99+
})
100+
})
101+
102+
Context("with AND expressions", func() {
103+
It("should return true when all gates are enabled", func() {
104+
Expect(evaluator.EvaluateExpression("alpha&gamma")).To(BeTrue())
105+
})
106+
107+
It("should return false when any gate is disabled", func() {
108+
Expect(evaluator.EvaluateExpression("alpha&beta")).To(BeFalse())
109+
Expect(evaluator.EvaluateExpression("beta&gamma")).To(BeFalse())
110+
Expect(evaluator.EvaluateExpression("beta&delta")).To(BeFalse())
111+
})
112+
113+
It("should handle multiple AND gates", func() {
114+
Expect(evaluator.EvaluateExpression("alpha&gamma&beta")).To(BeFalse())
115+
Expect(evaluator.EvaluateExpression("alpha&gamma&unknown")).To(BeFalse())
116+
})
117+
})
118+
119+
Context("with complex expressions", func() {
120+
It("should handle gates with special characters", func() {
121+
gatesWithSpecial := featuregate.FeatureGateMap{
122+
"my-feature": true,
123+
"under_score": false,
124+
"v1beta1": true,
125+
}
126+
specialEvaluator := featuregate.NewFeatureGateEvaluator(gatesWithSpecial)
127+
128+
Expect(specialEvaluator.EvaluateExpression("my-feature")).To(BeTrue())
129+
Expect(specialEvaluator.EvaluateExpression("under_score")).To(BeFalse())
130+
Expect(specialEvaluator.EvaluateExpression("v1beta1")).To(BeTrue())
131+
Expect(specialEvaluator.EvaluateExpression("my-feature|under_score")).To(BeTrue())
132+
Expect(specialEvaluator.EvaluateExpression("my-feature&v1beta1")).To(BeTrue())
133+
})
134+
})
135+
})
136+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package featuregate_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestFeatureGate(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "FeatureGate Suite")
29+
}

0 commit comments

Comments
 (0)