Skip to content

Commit 1c3b45d

Browse files
committed
modify existing test, check multiple types of reference in import cycle
Signed-off-by: Stephanie <yangcao@redhat.com>
1 parent aa85a4a commit 1c3b45d

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

pkg/devfile/parser/parse.go

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool
203203
}
204204
case parent.Kubernetes != nil:
205205
parentDevfileObj, err = parseFromKubeCRD(parent.ImportReference, resolveCtx, tool)
206+
if err != nil {
207+
return err
208+
}
206209
default:
207210
return fmt.Errorf("devfile parent does not define any resources")
208211
}

pkg/devfile/parser/parse_test.go

+64-61
Original file line numberDiff line numberDiff line change
@@ -2188,11 +2188,12 @@ func Test_parseParentAndPluginFromURI(t *testing.T) {
21882188
}
21892189
}
21902190

2191-
func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T) {
2191+
func Test_parseParentAndPlugin_RecursivelyReference(t *testing.T) {
21922192
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"
21952194
const httpPrefix = "http://"
2195+
const name = "testcrd"
2196+
const namespace = "defaultnamespace"
21962197

21972198
devFileObj := DevfileObj{
21982199
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
@@ -2236,7 +2237,10 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
22362237
Parent: &v1.Parent{
22372238
ImportReference: v1.ImportReference{
22382239
ImportReferenceUnion: v1.ImportReferenceUnion{
2239-
Uri: httpPrefix + uri2,
2240+
Kubernetes: &v1.KubernetesCustomResourceImportReference{
2241+
Name: name,
2242+
Namespace: namespace,
2243+
},
22402244
},
22412245
},
22422246
},
@@ -2258,35 +2262,8 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
22582262
},
22592263
},
22602264
}
2265+
22612266
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{
22902267
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
22912268
Data: &v2.DevfileV2{
22922269
Devfile: v1.Devfile{
@@ -2345,7 +2322,13 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
23452322
defer testServer1.Close()
23462323

23472324
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+
}
23492332
if err != nil {
23502333
t.Errorf("unexpected error: %v", err)
23512334
}
@@ -2355,50 +2338,70 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
23552338
}
23562339
}))
23572340
// create a listener with the desired port.
2358-
l2, err := net.Listen("tcp", uri2)
2341+
l3, err := net.Listen("tcp", uri2)
23592342
if err != nil {
23602343
t.Errorf("unexpected error: %v", err)
23612344
}
23622345

23632346
// NewUnstartedServer creates a listener. Close that listener and replace
23642347
// with the one we created.
23652348
testServer2.Listener.Close()
2366-
testServer2.Listener = l2
2349+
testServer2.Listener = l3
23672350

23682351
testServer2.Start()
23692352
defer testServer2.Close()
23702353

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+
},
23852386
}
23862387

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+
}
23912396

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)
23992401
// Unexpected error
24002402
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+
24022405
return
24032406
}
24042407

0 commit comments

Comments
 (0)