forked from kubernetes-sigs/ingress2gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingress2gateway_test.go
95 lines (87 loc) · 3.24 KB
/
ingress2gateway_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package i2gw
import (
"fmt"
"testing"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func Test_constructProviders(t *testing.T) {
supportProviders := []string{"ingress-nginx"}
for _, provider := range supportProviders {
ProviderConstructorByName[ProviderName(provider)] = func(conf *ProviderConf) Provider { return nil }
}
testCases := []struct {
name string
providers []string
expectedProviders []string
expectedError error
}{{
name: "Test construct providers with default providers",
providers: GetSupportedProviders(),
expectedProviders: supportProviders,
expectedError: nil,
}, {
name: "Test construct providers with provider that not supported",
providers: []string{"ingress-nginx", "fake-provider"},
expectedProviders: []string{},
expectedError: fmt.Errorf("%s is not a supported provider", "fake-provider"),
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cl := fake.NewClientBuilder().WithRuntimeObjects([]runtime.Object{}...).Build()
providerByName, err := constructProviders(&ProviderConf{
Client: cl,
}, tc.providers)
if tc.expectedError != nil {
if err == nil {
t.Errorf("Expected error but got none")
} else if tc.expectedError.Error() != err.Error() {
t.Errorf("The got error '%s' not equal to expected error '%s'", err.Error(), tc.expectedError.Error())
}
} else {
if err != nil {
t.Errorf("Expected no error but got %v", err)
}
if len(tc.expectedProviders) != len(providerByName) {
t.Errorf("Expected contructed providers num is %d but got %d", len(tc.providers), len(providerByName))
}
for _, provider := range tc.expectedProviders {
if _, ok := providerByName[ProviderName(provider)]; !ok {
t.Errorf("The expected provider %s was not constructed", provider)
}
}
}
})
}
}
func Test_GetSupportedProviders(t *testing.T) {
supportProviders := []string{"ingress-nginx"}
for _, provider := range supportProviders {
ProviderConstructorByName[ProviderName(provider)] = func(conf *ProviderConf) Provider { return nil }
}
t.Run("Test GetSupportedProviders", func(t *testing.T) {
allProviders := GetSupportedProviders()
if len(allProviders) != len(ProviderConstructorByName) {
t.Errorf("The acutal number of the providers we supported is %d but we got the number is: %d",
len(ProviderConstructorByName), len(allProviders))
}
for _, provider := range allProviders {
providerName := ProviderName(provider)
if _, ok := ProviderConstructorByName[providerName]; !ok {
t.Errorf("%s is not a supported provider", providerName)
}
}
})
}