@@ -2188,11 +2188,12 @@ func Test_parseParentAndPluginFromURI(t *testing.T) {
2188
2188
}
2189
2189
}
2190
2190
2191
- func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI (t * testing.T ) {
2191
+ func Test_parseParentAndPlugin_RecursivelyReference (t * testing.T ) {
2192
2192
const uri1 = "127.0.0.1:8080"
2193
- const uri2 = "127.0.0.1:9090"
2194
- const uri3 = "127.0.0.1:8090"
2193
+ const uri2 = "127.0.0.1:8090"
2195
2194
const httpPrefix = "http://"
2195
+ const name = "testcrd"
2196
+ const namespace = "defaultnamespace"
2196
2197
2197
2198
devFileObj := DevfileObj {
2198
2199
Ctx : devfileCtx .NewDevfileCtx (OutputDevfileYamlPath ),
@@ -2236,7 +2237,10 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
2236
2237
Parent : & v1.Parent {
2237
2238
ImportReference : v1.ImportReference {
2238
2239
ImportReferenceUnion : v1.ImportReferenceUnion {
2239
- Uri : httpPrefix + uri2 ,
2240
+ Kubernetes : & v1.KubernetesCustomResourceImportReference {
2241
+ Name : name ,
2242
+ Namespace : namespace ,
2243
+ },
2240
2244
},
2241
2245
},
2242
2246
},
@@ -2258,35 +2262,8 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
2258
2262
},
2259
2263
},
2260
2264
}
2265
+
2261
2266
parentDevfile2 := DevfileObj {
2262
- Ctx : devfileCtx .NewDevfileCtx (OutputDevfileYamlPath ),
2263
- Data : & v2.DevfileV2 {
2264
- Devfile : v1.Devfile {
2265
- DevfileHeader : devfilepkg.DevfileHeader {
2266
- SchemaVersion : schemaV200 ,
2267
- },
2268
- DevWorkspaceTemplateSpec : v1.DevWorkspaceTemplateSpec {
2269
- DevWorkspaceTemplateSpecContent : v1.DevWorkspaceTemplateSpecContent {
2270
- Components : []v1.Component {
2271
- {
2272
- Name : "plugin" ,
2273
- ComponentUnion : v1.ComponentUnion {
2274
- Plugin : & v1.PluginComponent {
2275
- ImportReference : v1.ImportReference {
2276
- ImportReferenceUnion : v1.ImportReferenceUnion {
2277
- Uri : httpPrefix + uri3 ,
2278
- },
2279
- },
2280
- },
2281
- },
2282
- },
2283
- },
2284
- },
2285
- },
2286
- },
2287
- },
2288
- }
2289
- parentDevfile3 := DevfileObj {
2290
2267
Ctx : devfileCtx .NewDevfileCtx (OutputDevfileYamlPath ),
2291
2268
Data : & v2.DevfileV2 {
2292
2269
Devfile : v1.Devfile {
@@ -2345,7 +2322,13 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
2345
2322
defer testServer1 .Close ()
2346
2323
2347
2324
testServer2 := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
2348
- data , err := yaml .Marshal (parentDevfile2 .Data )
2325
+ var data []byte
2326
+ if strings .Contains (r .URL .Path , "/devfiles/nodejs" ) {
2327
+ data , err = yaml .Marshal (parentDevfile2 .Data )
2328
+ } else {
2329
+ w .WriteHeader (http .StatusNotFound )
2330
+ return
2331
+ }
2349
2332
if err != nil {
2350
2333
t .Errorf ("unexpected error: %v" , err )
2351
2334
}
@@ -2355,50 +2338,70 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
2355
2338
}
2356
2339
}))
2357
2340
// create a listener with the desired port.
2358
- l2 , err := net .Listen ("tcp" , uri2 )
2341
+ l3 , err := net .Listen ("tcp" , uri2 )
2359
2342
if err != nil {
2360
2343
t .Errorf ("unexpected error: %v" , err )
2361
2344
}
2362
2345
2363
2346
// NewUnstartedServer creates a listener. Close that listener and replace
2364
2347
// with the one we created.
2365
2348
testServer2 .Listener .Close ()
2366
- testServer2 .Listener = l2
2349
+ testServer2 .Listener = l3
2367
2350
2368
2351
testServer2 .Start ()
2369
2352
defer testServer2 .Close ()
2370
2353
2371
- testServer3 := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
2372
- data , err := yaml .Marshal (parentDevfile3 .Data )
2373
- if err != nil {
2374
- t .Errorf ("unexpected error: %v" , err )
2375
- }
2376
- _ , err = w .Write (data )
2377
- if err != nil {
2378
- t .Errorf ("unexpected error: %v" , err )
2379
- }
2380
- }))
2381
- // create a listener with the desired port.
2382
- l3 , err := net .Listen ("tcp" , uri3 )
2383
- if err != nil {
2384
- t .Errorf ("unexpected error: %v" , err )
2354
+ parentSpec := v1.DevWorkspaceTemplateSpec {
2355
+ Parent : & v1.Parent {
2356
+ ImportReference : v1.ImportReference {
2357
+ ImportReferenceUnion : v1.ImportReferenceUnion {
2358
+ Id : "nodejs" ,
2359
+ },
2360
+ RegistryUrl : httpPrefix + uri2 ,
2361
+ },
2362
+ },
2363
+ DevWorkspaceTemplateSpecContent : v1.DevWorkspaceTemplateSpecContent {
2364
+ Components : []v1.Component {
2365
+ {
2366
+ Name : "crdcomponent" ,
2367
+ ComponentUnion : v1.ComponentUnion {
2368
+ Volume : & v1.VolumeComponent {
2369
+ Volume : v1.Volume {
2370
+ Size : "500Mi" ,
2371
+ },
2372
+ },
2373
+ },
2374
+ },
2375
+ },
2376
+ },
2377
+ }
2378
+ devWorkspaceResources := map [string ]v1.DevWorkspaceTemplate {
2379
+ name : {
2380
+ TypeMeta : kubev1.TypeMeta {
2381
+ Kind : "DevWorkspaceTemplate" ,
2382
+ APIVersion : "testgroup/v1alpha2" ,
2383
+ },
2384
+ Spec : parentSpec ,
2385
+ },
2385
2386
}
2386
2387
2387
- // NewUnstartedServer creates a listener. Close that listener and replace
2388
- // with the one we created.
2389
- testServer3 .Listener .Close ()
2390
- testServer3 .Listener = l3
2388
+ t .Run ("it should error out if import reference has a cycle" , func (t * testing.T ) {
2389
+ testK8sClient := & testingutil.FakeK8sClient {
2390
+ DevWorkspaceResources : devWorkspaceResources ,
2391
+ }
2392
+ tool := resolverTools {
2393
+ k8sClient : testK8sClient ,
2394
+ context : context .Background (),
2395
+ }
2391
2396
2392
- testServer3 .Start ()
2393
- defer testServer3 .Close ()
2394
- t .Run ("it should error out if URI is recursively referenced with multiple references" , func (t * testing.T ) {
2395
- err := parseParentAndPlugin (devFileObj , & resolutionContextTree {}, resolverTools {})
2396
- // devfile has an cycle in references: main devfile -> uri: http://127.0.0.1:8080 -> uri: http://127.0.0.1:9090 -> uri: http://127.0.0.1:8090 -> uri: http://127.0.0.1:8080
2397
- expectedErr := fmt .Sprintf ("devfile has an cycle in references: main devfile -> uri: %s%s -> uri: %s%s -> uri: %s%s -> uri: %s%s" , httpPrefix , uri1 ,
2398
- httpPrefix , uri2 , httpPrefix , uri3 , httpPrefix , uri1 )
2397
+ err := parseParentAndPlugin (devFileObj , & resolutionContextTree {}, tool )
2398
+ // devfile has a cycle in references: main devfile -> uri: http://127.0.0.1:8080 -> uri: http://127.0.0.1:9090 -> id: nodejs, registryURL: http://127.0.0.1:8090 -> uri: http://127.0.0.1:8080
2399
+ expectedErr := fmt .Sprintf ("devfile has an cycle in references: main devfile -> uri: %s%s -> name: %s, namespace: %s -> id: nodejs, registryURL: %s%s -> uri: %s%s" , httpPrefix , uri1 , name , namespace ,
2400
+ httpPrefix , uri2 , httpPrefix , uri1 )
2399
2401
// Unexpected error
2400
2402
if err == nil || ! reflect .DeepEqual (expectedErr , err .Error ()) {
2401
- t .Errorf ("Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI() unexpected error = %v" , err )
2403
+ t .Errorf ("Test_parseParentAndPlugin_RecursivelyReference unexpected error = %v" , err )
2404
+
2402
2405
return
2403
2406
}
2404
2407
0 commit comments