@@ -17,140 +17,13 @@ limitations under the License.
17
17
package i2gw
18
18
19
19
import (
20
- "context"
21
20
"fmt"
22
21
"testing"
23
22
24
- "github.com/google/go-cmp/cmp"
25
- networkingv1 "k8s.io/api/networking/v1"
26
- apiequality "k8s.io/apimachinery/pkg/api/equality"
27
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
23
"k8s.io/apimachinery/pkg/runtime"
29
24
"sigs.k8s.io/controller-runtime/pkg/client/fake"
30
25
)
31
26
32
- func Test_constructIngressesFromFile (t * testing.T ) {
33
- ingress1 := ingress (443 , "ingress1" , "namespace1" )
34
- ingress2 := ingress (80 , "ingress2" , "namespace2" )
35
- ingressNoNamespace := ingress (80 , "ingress-no-namespace" , "" )
36
-
37
- testCases := []struct {
38
- name string
39
- filePath string
40
- namespace string
41
- wantIngressList []networkingv1.Ingress
42
- }{
43
- {
44
- name : "Test yaml input file with multiple resources with no namespace flag" ,
45
- filePath : "testdata/input-file.yaml" ,
46
- namespace : "" ,
47
- wantIngressList : []networkingv1.Ingress {ingress1 , ingress2 , ingressNoNamespace },
48
- }, {
49
- name : "Test json input file with multiple resources with no namespace flag" ,
50
- filePath : "testdata/input-file.json" ,
51
- namespace : "" ,
52
- wantIngressList : []networkingv1.Ingress {ingress1 , ingress2 , ingressNoNamespace },
53
- }, {
54
- name : "Test yaml input file with multiple resources with namespace1 flag" ,
55
- filePath : "testdata/input-file.yaml" ,
56
- namespace : "namespace1" ,
57
- wantIngressList : []networkingv1.Ingress {ingress1 },
58
- },
59
- }
60
-
61
- for _ , tc := range testCases {
62
- t .Run (tc .name , func (t * testing.T ) {
63
- gotIngressList := & networkingv1.IngressList {}
64
- err := ConstructIngressesFromFile (gotIngressList , tc .filePath , tc .namespace )
65
- if err != nil {
66
- t .Errorf ("Failed to open test file: %v" , err )
67
- }
68
- compareIngressLists (t , gotIngressList , tc .wantIngressList )
69
- })
70
- }
71
- }
72
-
73
- func ingress (port int32 , name , namespace string ) networkingv1.Ingress {
74
- iPrefix := networkingv1 .PathTypePrefix
75
- ingressClassName := fmt .Sprintf ("ingressClass-%s" , name )
76
- var objMeta metav1.ObjectMeta
77
- if namespace != "" {
78
- objMeta = metav1.ObjectMeta {Name : name , ResourceVersion : "999" , Namespace : namespace }
79
- } else {
80
- objMeta = metav1.ObjectMeta {Name : name , ResourceVersion : "999" }
81
- }
82
-
83
- ing := networkingv1.Ingress {
84
- TypeMeta : metav1.TypeMeta {
85
- Kind : "Ingress" ,
86
- APIVersion : "networking.k8s.io/v1" ,
87
- },
88
- ObjectMeta : objMeta ,
89
- Spec : networkingv1.IngressSpec {
90
- IngressClassName : & ingressClassName ,
91
- Rules : []networkingv1.IngressRule {{
92
- IngressRuleValue : networkingv1.IngressRuleValue {
93
- HTTP : & networkingv1.HTTPIngressRuleValue {
94
- Paths : []networkingv1.HTTPIngressPath {{
95
- Path : fmt .Sprintf ("/path-%s" , name ),
96
- PathType : & iPrefix ,
97
- Backend : networkingv1.IngressBackend {
98
- Service : & networkingv1.IngressServiceBackend {
99
- Name : fmt .Sprintf ("service-%s" , name ),
100
- Port : networkingv1.ServiceBackendPort {
101
- Number : port ,
102
- },
103
- },
104
- },
105
- }},
106
- },
107
- },
108
- }},
109
- },
110
- }
111
- return ing
112
- }
113
-
114
- func compareIngressLists (t * testing.T , gotIngressList * networkingv1.IngressList , wantIngressList []networkingv1.Ingress ) {
115
- for i , got := range gotIngressList .Items {
116
- want := wantIngressList [i ]
117
- if ! apiequality .Semantic .DeepEqual (got , want ) {
118
- t .Errorf ("Expected Ingress %d to be %+v\n Got: %+v\n Diff: %s" , i , want , got , cmp .Diff (want , got ))
119
- }
120
- }
121
- }
122
-
123
- func Test_constructIngressesFromCluster (t * testing.T ) {
124
- ingress1 := ingress (443 , "ingress1" , "namespace1" )
125
- ingress2 := ingress (80 , "ingress2" , "namespace2" )
126
- testCases := []struct {
127
- name string
128
- runtimeObjs []runtime.Object
129
- wantIngresses []networkingv1.Ingress
130
- }{{
131
- name : "Test cluster client with 2 resources" ,
132
- runtimeObjs : []runtime.Object {& ingress1 , & ingress2 },
133
- wantIngresses : []networkingv1.Ingress {ingress1 , ingress2 },
134
- }, {
135
- name : "Test cluster client without resources" ,
136
- runtimeObjs : []runtime.Object {},
137
- wantIngresses : []networkingv1.Ingress {},
138
- },
139
- }
140
-
141
- for _ , tc := range testCases {
142
- t .Run (tc .name , func (t * testing.T ) {
143
- gotIngresses := & networkingv1.IngressList {}
144
- cl := fake .NewClientBuilder ().WithRuntimeObjects (tc .runtimeObjs ... ).Build ()
145
- err := ConstructIngressesFromCluster (context .Background (), cl , gotIngresses )
146
- if err != nil {
147
- t .Errorf ("test failed unexpectedly: %v" , err )
148
- }
149
- compareIngressLists (t , gotIngresses , tc .wantIngresses )
150
- })
151
- }
152
- }
153
-
154
27
func Test_constructProviders (t * testing.T ) {
155
28
supportProviders := []string {"ingress-nginx" }
156
29
for _ , provider := range supportProviders {
0 commit comments