@@ -4,16 +4,15 @@ import (
4
4
"net"
5
5
"net/http"
6
6
"net/http/httptest"
7
+ "reflect"
7
8
"testing"
8
9
9
10
"github.com/devfile/library/pkg/testingutil/filesystem"
10
11
"github.com/devfile/library/pkg/util"
11
12
"github.com/stretchr/testify/assert"
12
- corev1 "k8s.io/api/core/v1"
13
- k8yaml "sigs.k8s.io/yaml"
14
13
)
15
14
16
- func TestReadKubernetesYaml (t * testing.T ) {
15
+ func TestReadAndParseKubernetesYaml (t * testing.T ) {
17
16
const serverIP = "127.0.0.1:9080"
18
17
var data []byte
19
18
@@ -62,6 +61,7 @@ func TestReadKubernetesYaml(t *testing.T) {
62
61
wantDeploymentNames []string
63
62
wantServiceNames []string
64
63
wantRouteNames []string
64
+ wantIngressNames []string
65
65
wantOtherNames []string
66
66
}{
67
67
{
@@ -70,32 +70,35 @@ func TestReadKubernetesYaml(t *testing.T) {
70
70
URL : "http://" + serverIP ,
71
71
},
72
72
fs : filesystem.DefaultFs {},
73
- wantDeploymentNames : []string {"deploy-sample" },
74
- wantServiceNames : []string {"service-sample" },
75
- wantRouteNames : []string {"route-sample" },
76
- wantOtherNames : []string {"pvc-sample" },
73
+ wantDeploymentNames : []string {"deploy-sample" , "deploy-sample-2" },
74
+ wantServiceNames : []string {"service-sample" , "service-sample-2" },
75
+ wantRouteNames : []string {"route-sample" , "route-sample-2" },
76
+ wantIngressNames : []string {"ingress-sample" , "ingress-sample-2" },
77
+ wantOtherNames : []string {"pvc-sample" , "pvc-sample-2" },
77
78
},
78
79
{
79
80
name : "Read the YAML from the Path" ,
80
81
src : YamlSrc {
81
82
Path : "../../../tests/yamls/resources.yaml" ,
82
83
},
83
84
fs : filesystem.DefaultFs {},
84
- wantDeploymentNames : []string {"deploy-sample" },
85
- wantServiceNames : []string {"service-sample" },
86
- wantRouteNames : []string {"route-sample" },
87
- wantOtherNames : []string {"pvc-sample" },
85
+ wantDeploymentNames : []string {"deploy-sample" , "deploy-sample-2" },
86
+ wantServiceNames : []string {"service-sample" , "service-sample-2" },
87
+ wantRouteNames : []string {"route-sample" , "route-sample-2" },
88
+ wantIngressNames : []string {"ingress-sample" , "ingress-sample-2" },
89
+ wantOtherNames : []string {"pvc-sample" , "pvc-sample-2" },
88
90
},
89
91
{
90
92
name : "Read the YAML from the Data" ,
91
93
src : YamlSrc {
92
94
Data : data ,
93
95
},
94
96
fs : filesystem.DefaultFs {},
95
- wantDeploymentNames : []string {"deploy-sample" },
96
- wantServiceNames : []string {"service-sample" },
97
- wantRouteNames : []string {"route-sample" },
98
- wantOtherNames : []string {"pvc-sample" },
97
+ wantDeploymentNames : []string {"deploy-sample" , "deploy-sample-2" },
98
+ wantServiceNames : []string {"service-sample" , "service-sample-2" },
99
+ wantRouteNames : []string {"route-sample" , "route-sample-2" },
100
+ wantIngressNames : []string {"ingress-sample" , "ingress-sample-2" },
101
+ wantOtherNames : []string {"pvc-sample" , "pvc-sample-2" },
99
102
},
100
103
{
101
104
name : "Bad URL" ,
@@ -125,27 +128,61 @@ func TestReadKubernetesYaml(t *testing.T) {
125
128
126
129
for _ , tt := range tests {
127
130
t .Run (tt .name , func (t * testing.T ) {
128
- deployments , services , routes , others , err := ReadKubernetesYaml (tt .src , tt .fs )
131
+ values , err := ReadKubernetesYaml (tt .src , tt .fs )
129
132
if (err != nil ) != tt .wantErr {
130
133
t .Errorf ("unexpected error: %v" , err )
131
134
return
132
135
}
133
- for _ , deploy := range deployments {
134
- assert .Contains (t , tt .wantDeploymentNames , deploy .Name )
135
- }
136
- for _ , svc := range services {
137
- assert .Contains (t , tt .wantServiceNames , svc .Name )
138
- }
139
- for _ , route := range routes {
140
- assert .Contains (t , tt .wantRouteNames , route .Name )
136
+
137
+ for _ , value := range values {
138
+ kubernetesMap := value .(map [string ]interface {})
139
+
140
+ kind := kubernetesMap ["kind" ]
141
+ metadataMap := kubernetesMap ["metadata" ].(map [string ]interface {})
142
+ name := metadataMap ["name" ]
143
+
144
+ switch kind {
145
+ case "Deployment" :
146
+ assert .Contains (t , tt .wantDeploymentNames , name )
147
+ case "Service" :
148
+ assert .Contains (t , tt .wantServiceNames , name )
149
+ case "Route" :
150
+ assert .Contains (t , tt .wantRouteNames , name )
151
+ case "Ingress" :
152
+ assert .Contains (t , tt .wantIngressNames , name )
153
+ default :
154
+ assert .Contains (t , tt .wantOtherNames , name )
155
+ }
141
156
}
142
- for _ , other := range others {
143
- pvc := corev1. PersistentVolumeClaim {}
144
- err = k8yaml . Unmarshal ( other , & pvc )
157
+
158
+ if len ( values ) > 0 {
159
+ resources , err := ParseKubernetesYaml ( values )
145
160
if err != nil {
146
161
t .Error (err )
162
+ return
163
+ }
164
+
165
+ if reflect .DeepEqual (resources , KubernetesResources {}) {
166
+ t .Error ("Kubernetes resources is empty, expected to contain some resources" )
167
+ } else {
168
+ deployments := resources .Deployments
169
+ services := resources .Services
170
+ routes := resources .Routes
171
+ ingresses := resources .Ingresses
172
+
173
+ for _ , deploy := range deployments {
174
+ assert .Contains (t , tt .wantDeploymentNames , deploy .Name )
175
+ }
176
+ for _ , svc := range services {
177
+ assert .Contains (t , tt .wantServiceNames , svc .Name )
178
+ }
179
+ for _ , route := range routes {
180
+ assert .Contains (t , tt .wantRouteNames , route .Name )
181
+ }
182
+ for _ , ingress := range ingresses {
183
+ assert .Contains (t , tt .wantIngressNames , ingress .Name )
184
+ }
147
185
}
148
- assert .Contains (t , tt .wantOtherNames , pvc .Name )
149
186
}
150
187
})
151
188
}
0 commit comments