Skip to content

Commit ed6a80a

Browse files
committed
Use constants in tests
1 parent b2d8597 commit ed6a80a

File tree

4 files changed

+85
-103
lines changed

4 files changed

+85
-103
lines changed

internal/controller/state/graph/graph_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ import (
3333

3434
func TestBuildGraph(t *testing.T) {
3535
const (
36-
gcName = "my-class"
37-
controllerName = "my.controller"
36+
gcName = "my-class"
37+
controllerName = "my.controller"
38+
experimentalFeaturesOff = false
3839
)
3940

4041
cm := &v1.ConfigMap{
@@ -1456,7 +1457,7 @@ func TestBuildGraph(t *testing.T) {
14561457
PolicyValidator: fakePolicyValidator,
14571458
},
14581459
logr.Discard(),
1459-
false,
1460+
experimentalFeaturesOff,
14601461
)
14611462

14621463
g.Expect(helpers.Diff(test.expected, result)).To(BeEmpty())

internal/controller/state/graph/multiple_gateways_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
)
2323

2424
const (
25-
controllerName = "nginx"
26-
gcName = "my-gateway-class"
25+
controllerName = "nginx"
26+
gcName = "my-gateway-class"
27+
experimentalFeaturesOff = false
2728
)
2829

2930
var (
@@ -406,7 +407,7 @@ func Test_MultipleGateways_WithNginxProxy(t *testing.T) {
406407
PolicyValidator: fakePolicyValidator,
407408
},
408409
logr.Discard(),
409-
false,
410+
experimentalFeaturesOff,
410411
)
411412

412413
g.Expect(helpers.Diff(test.expGraph, result)).To(BeEmpty())
@@ -896,7 +897,7 @@ func Test_MultipleGateways_WithListeners(t *testing.T) {
896897
PolicyValidator: fakePolicyValidator,
897898
},
898899
logr.Discard(),
899-
false,
900+
experimentalFeaturesOff,
900901
)
901902

902903
g.Expect(helpers.Diff(test.expGraph, result)).To(BeEmpty())

internal/controller/status/gatewayclass.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package status
33
import (
44
"sort"
55

6-
"k8s.io/apimachinery/pkg/util/sets"
76
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
87
"sigs.k8s.io/gateway-api/pkg/features"
98
)
@@ -12,55 +11,55 @@ import (
1211
// The list must be sorted in ascending alphabetical order.
1312
// If experimental is true, experimental features like TLSRoute will be included.
1413
func supportedFeatures(experimental bool) []gatewayv1.SupportedFeature {
15-
featSet := sets.New(
14+
featureNames := []features.FeatureName{
1615
// Core features
17-
features.GatewayFeature,
18-
features.GRPCRouteFeature,
19-
features.HTTPRouteFeature,
20-
features.ReferenceGrantFeature,
16+
features.SupportGateway,
17+
features.SupportGRPCRoute,
18+
features.SupportHTTPRoute,
19+
features.SupportReferenceGrant,
2120

2221
// BackendTLSPolicy
23-
features.BackendTLSPolicyFeature,
22+
features.SupportBackendTLSPolicy,
2423

2524
// Gateway extended
26-
features.GatewayEmptyAddressFeature,
27-
features.GatewayHTTPListenerIsolationFeature,
28-
features.GatewayInfrastructurePropagationFeature,
29-
features.GatewayPort8080Feature,
30-
features.GatewayStaticAddressesFeature,
25+
features.SupportGatewayAddressEmpty,
26+
features.SupportGatewayHTTPListenerIsolation,
27+
features.SupportGatewayInfrastructurePropagation,
28+
features.SupportGatewayPort8080,
29+
features.SupportGatewayStaticAddresses,
3130

3231
// HTTPRoute extended
33-
features.HTTPRouteBackendProtocolWebSocketFeature,
34-
features.HTTPRouteDestinationPortMatchingFeature,
35-
features.HTTPRouteHostRewriteFeature,
36-
features.HTTPRouteMethodMatchingFeature,
37-
features.HTTPRouteParentRefPortFeature,
38-
features.HTTPRoutePathRedirectFeature,
39-
features.HTTPRoutePathRewriteFeature,
40-
features.HTTPRoutePortRedirectFeature,
41-
features.HTTPRouteQueryParamMatchingFeature,
42-
features.HTTPRouteRequestMirrorFeature,
43-
features.HTTPRouteRequestMultipleMirrorsFeature,
44-
features.HTTPRouteRequestPercentageMirrorFeature,
45-
features.HTTPRouteResponseHeaderModificationFeature,
46-
features.HTTPRouteSchemeRedirectFeature,
47-
)
32+
features.SupportHTTPRouteBackendProtocolWebSocket,
33+
features.SupportHTTPRouteDestinationPortMatching,
34+
features.SupportHTTPRouteHostRewrite,
35+
features.SupportHTTPRouteMethodMatching,
36+
features.SupportHTTPRouteParentRefPort,
37+
features.SupportHTTPRoutePathRedirect,
38+
features.SupportHTTPRoutePathRewrite,
39+
features.SupportHTTPRoutePortRedirect,
40+
features.SupportHTTPRouteQueryParamMatching,
41+
features.SupportHTTPRouteRequestMirror,
42+
features.SupportHTTPRouteRequestMultipleMirrors,
43+
features.SupportHTTPRouteRequestPercentageMirror,
44+
features.SupportHTTPRouteResponseHeaderModification,
45+
features.SupportHTTPRouteSchemeRedirect,
46+
}
4847

4948
// Add experimental features if enabled
5049
if experimental {
51-
featSet.Insert(features.TLSRouteFeature)
52-
}
53-
54-
// Convert features to SupportedFeature slice
55-
result := make([]gatewayv1.SupportedFeature, 0, featSet.Len())
56-
for _, feat := range featSet.UnsortedList() {
57-
result = append(result, gatewayv1.SupportedFeature{Name: gatewayv1.FeatureName(feat.Name)})
50+
featureNames = append(featureNames, features.SupportTLSRoute)
5851
}
5952

6053
// Sort alphabetically by feature name
61-
sort.Slice(result, func(i, j int) bool {
62-
return string(result[i].Name) < string(result[j].Name)
54+
sort.Slice(featureNames, func(i, j int) bool {
55+
return string(featureNames[i]) < string(featureNames[j])
6356
})
6457

58+
// Convert to SupportedFeature slice
59+
result := make([]gatewayv1.SupportedFeature, 0, len(featureNames))
60+
for _, name := range featureNames {
61+
result = append(result, gatewayv1.SupportedFeature{Name: gatewayv1.FeatureName(name)})
62+
}
63+
6564
return result
6665
}

internal/controller/status/gatewayclass_test.go

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,61 @@ import (
66

77
. "github.com/onsi/gomega"
88
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
9+
"sigs.k8s.io/gateway-api/pkg/features"
910
)
1011

1112
func TestSupportedFeatures(t *testing.T) {
1213
t.Parallel()
1314

15+
standardFeatures := []gatewayv1.FeatureName{
16+
gatewayv1.FeatureName(features.SupportBackendTLSPolicy),
17+
gatewayv1.FeatureName(features.SupportGRPCRoute),
18+
gatewayv1.FeatureName(features.SupportGateway),
19+
gatewayv1.FeatureName(features.SupportGatewayAddressEmpty),
20+
gatewayv1.FeatureName(features.SupportGatewayHTTPListenerIsolation),
21+
gatewayv1.FeatureName(features.SupportGatewayInfrastructurePropagation),
22+
gatewayv1.FeatureName(features.SupportGatewayPort8080),
23+
gatewayv1.FeatureName(features.SupportGatewayStaticAddresses),
24+
gatewayv1.FeatureName(features.SupportHTTPRoute),
25+
gatewayv1.FeatureName(features.SupportHTTPRouteBackendProtocolWebSocket),
26+
gatewayv1.FeatureName(features.SupportHTTPRouteDestinationPortMatching),
27+
gatewayv1.FeatureName(features.SupportHTTPRouteHostRewrite),
28+
gatewayv1.FeatureName(features.SupportHTTPRouteMethodMatching),
29+
gatewayv1.FeatureName(features.SupportHTTPRouteParentRefPort),
30+
gatewayv1.FeatureName(features.SupportHTTPRoutePathRedirect),
31+
gatewayv1.FeatureName(features.SupportHTTPRoutePathRewrite),
32+
gatewayv1.FeatureName(features.SupportHTTPRoutePortRedirect),
33+
gatewayv1.FeatureName(features.SupportHTTPRouteQueryParamMatching),
34+
gatewayv1.FeatureName(features.SupportHTTPRouteRequestMirror),
35+
gatewayv1.FeatureName(features.SupportHTTPRouteRequestMultipleMirrors),
36+
gatewayv1.FeatureName(features.SupportHTTPRouteRequestPercentageMirror),
37+
gatewayv1.FeatureName(features.SupportHTTPRouteResponseHeaderModification),
38+
gatewayv1.FeatureName(features.SupportHTTPRouteSchemeRedirect),
39+
gatewayv1.FeatureName(features.SupportReferenceGrant),
40+
}
41+
42+
experimentalFeatures := []gatewayv1.FeatureName{
43+
gatewayv1.FeatureName(features.SupportTLSRoute),
44+
}
45+
46+
allFeatures := append(slices.Clone(standardFeatures), experimentalFeatures...)
47+
1448
tests := []struct {
1549
name string
1650
expectedFeatures []gatewayv1.FeatureName
1751
unexpectedFeatures []gatewayv1.FeatureName
1852
experimental bool
1953
}{
2054
{
21-
name: "standard features only",
22-
experimental: false,
23-
expectedFeatures: []gatewayv1.FeatureName{
24-
"BackendTLSPolicy",
25-
"GRPCRoute",
26-
"Gateway",
27-
"GatewayAddressEmpty",
28-
"GatewayHTTPListenerIsolation",
29-
"GatewayInfrastructurePropagation",
30-
"GatewayPort8080",
31-
"GatewayStaticAddresses",
32-
"HTTPRoute",
33-
"HTTPRouteBackendProtocolWebSocket",
34-
"HTTPRouteDestinationPortMatching",
35-
"HTTPRouteHostRewrite",
36-
"HTTPRouteMethodMatching",
37-
"HTTPRouteParentRefPort",
38-
"HTTPRoutePathRedirect",
39-
"HTTPRoutePathRewrite",
40-
"HTTPRoutePortRedirect",
41-
"HTTPRouteQueryParamMatching",
42-
"HTTPRouteRequestMirror",
43-
"HTTPRouteRequestMultipleMirrors",
44-
"HTTPRouteRequestPercentageMirror",
45-
"HTTPRouteResponseHeaderModification",
46-
"HTTPRouteSchemeRedirect",
47-
"ReferenceGrant",
48-
},
49-
unexpectedFeatures: []gatewayv1.FeatureName{
50-
"TLSRoute",
51-
},
55+
name: "standard features only",
56+
experimental: false,
57+
expectedFeatures: standardFeatures,
58+
unexpectedFeatures: experimentalFeatures,
5259
},
5360
{
54-
name: "standard and experimental features",
55-
experimental: true,
56-
expectedFeatures: []gatewayv1.FeatureName{
57-
"BackendTLSPolicy",
58-
"GRPCRoute",
59-
"Gateway",
60-
"GatewayAddressEmpty",
61-
"GatewayHTTPListenerIsolation",
62-
"GatewayInfrastructurePropagation",
63-
"GatewayPort8080",
64-
"GatewayStaticAddresses",
65-
"HTTPRoute",
66-
"HTTPRouteBackendProtocolWebSocket",
67-
"HTTPRouteDestinationPortMatching",
68-
"HTTPRouteHostRewrite",
69-
"HTTPRouteMethodMatching",
70-
"HTTPRouteParentRefPort",
71-
"HTTPRoutePathRedirect",
72-
"HTTPRoutePathRewrite",
73-
"HTTPRoutePortRedirect",
74-
"HTTPRouteQueryParamMatching",
75-
"HTTPRouteRequestMirror",
76-
"HTTPRouteRequestMultipleMirrors",
77-
"HTTPRouteRequestPercentageMirror",
78-
"HTTPRouteResponseHeaderModification",
79-
"HTTPRouteSchemeRedirect",
80-
"ReferenceGrant",
81-
"TLSRoute",
82-
},
61+
name: "standard and experimental features",
62+
experimental: true,
63+
expectedFeatures: allFeatures,
8364
unexpectedFeatures: []gatewayv1.FeatureName{},
8465
},
8566
}

0 commit comments

Comments
 (0)