16
16
package devfile
17
17
18
18
import (
19
+ "context"
19
20
"net"
20
21
"net/http"
21
22
"net/http/httptest"
23
+ "path/filepath"
22
24
"reflect"
23
25
"strings"
24
26
"testing"
25
27
26
28
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
27
29
30
+ schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
28
31
"github.com/devfile/api/v2/pkg/validation/variables"
29
32
"github.com/devfile/library/v2/pkg/devfile/parser"
30
33
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
34
+ "github.com/devfile/library/v2/pkg/testingutil"
35
+
36
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
+ "sigs.k8s.io/controller-runtime/pkg/client"
31
38
)
32
39
33
40
func TestParseDevfileAndValidate (t * testing.T ) {
34
41
falseValue := false
35
42
trueValue := true
36
43
convertUriToInline := false
37
44
K8sLikeComponentOriginalURIKey := "api.devfile.io/k8sLikeComponent-originalURI"
45
+
46
+ devfileStruct := schema.DevWorkspaceTemplate {
47
+ TypeMeta : metav1.TypeMeta {
48
+ APIVersion : "2.1.0" ,
49
+ },
50
+ Spec : schema.DevWorkspaceTemplateSpec {
51
+ DevWorkspaceTemplateSpecContent : schema.DevWorkspaceTemplateSpecContent {
52
+ Commands : []schema.Command {
53
+ {
54
+ Id : "run" ,
55
+ CommandUnion : schema.CommandUnion {
56
+ Exec : & schema.ExecCommand {
57
+ CommandLine : "./main {{ PARAMS }}" ,
58
+ Component : "runtime" ,
59
+ WorkingDir : "${PROJECT_SOURCE}" ,
60
+ LabeledCommand : schema.LabeledCommand {
61
+ BaseCommand : schema.BaseCommand {
62
+ Group : & schema.CommandGroup {
63
+ Kind : "run" ,
64
+ IsDefault : & trueValue ,
65
+ },
66
+ },
67
+ },
68
+ },
69
+ },
70
+ },
71
+ },
72
+ Components : []schema.Component {
73
+ {
74
+ Name : "runtime" ,
75
+ ComponentUnion : schema.ComponentUnion {
76
+ Container : & schema.ContainerComponent {
77
+ Endpoints : []schema.Endpoint {
78
+ {
79
+ Name : "http" ,
80
+ TargetPort : 8080 ,
81
+ },
82
+ },
83
+ Container : schema.Container {
84
+ Image : "golang:latest" ,
85
+ MemoryLimit : "1024Mi" ,
86
+ MountSources : & trueValue ,
87
+ },
88
+ },
89
+ },
90
+ },
91
+ {
92
+ Name : "outerloop-deploy" ,
93
+ ComponentUnion : schema.ComponentUnion {
94
+ Kubernetes : & schema.KubernetesComponent {
95
+ K8sLikeComponent : schema.K8sLikeComponent {
96
+ K8sLikeComponentLocation : schema.K8sLikeComponentLocation {
97
+ Uri : "http://127.0.0.1:8080/outerloop-deploy.yaml" ,
98
+ },
99
+ },
100
+ },
101
+ },
102
+ },
103
+ {
104
+ Name : "outerloop-deploy2" ,
105
+ ComponentUnion : schema.ComponentUnion {
106
+ Openshift : & schema.OpenshiftComponent {
107
+ K8sLikeComponent : schema.K8sLikeComponent {
108
+ K8sLikeComponentLocation : schema.K8sLikeComponentLocation {
109
+ Uri : "http://127.0.0.1:8080/outerloop-service.yaml" ,
110
+ },
111
+ },
112
+ },
113
+ },
114
+ },
115
+ },
116
+ },
117
+ },
118
+ }
119
+
120
+ devfileContent := `commands:
121
+ - exec:
122
+ commandLine: ./main {{ PARAMS }}
123
+ component: runtime
124
+ group:
125
+ isDefault: true
126
+ kind: run
127
+ workingDir: ${PROJECT_SOURCE}
128
+ id: run
129
+ components:
130
+ - container:
131
+ endpoints:
132
+ - name: http
133
+ targetPort: 8080
134
+ image: golang:latest
135
+ memoryLimit: 1024Mi
136
+ mountSources: true
137
+ name: runtime
138
+ - kubernetes:
139
+ uri: http://127.0.0.1:8080/outerloop-deploy.yaml
140
+ name: outerloop-deploy
141
+ - openshift:
142
+ uri: http://127.0.0.1:8080/outerloop-service.yaml
143
+ name: outerloop-deploy2
144
+ metadata:
145
+ description: Stack with the latest Go version
146
+ displayName: Go Runtime
147
+ icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg
148
+ language: go
149
+ name: my-go-app
150
+ projectType: go
151
+ tags:
152
+ - Go
153
+ version: 1.0.0
154
+ schemaVersion: 2.2.0
155
+ `
156
+
157
+ devfileContentWithVariable := devfileContent + `variables:
158
+ PARAMS: foo`
159
+ devfileContentWithParent := `schemaVersion: 2.2.0
160
+ parent:
161
+ id: devfile1
162
+ registryUrl: http://127.0.0.1:8080/registry
163
+ `
164
+ devfileContentWithParentNoRegistry := `schemaVersion: 2.2.0
165
+ parent:
166
+ id: devfile1
167
+ `
168
+ devfileContentWithCRDParent := `schemaVersion: 2.2.0
169
+ parent:
170
+ kubernetes:
171
+ name: devfile1
172
+ registryUrl: http://127.0.0.1:8080/registry
173
+ `
174
+
175
+ registryIndex := `[{
176
+ "name": "devfile1",
177
+ "version": "1.0.0",
178
+ "type": "stack",
179
+ "links": {
180
+ "self": "devfile-catalog/devfile1:1.0.0"
181
+ },
182
+ "resources": [
183
+ "devfile.yaml"
184
+ ]
185
+ }]`
38
186
outerloopDeployContent := `
39
187
kind: Deployment
40
188
apiVersion: apps/v1
@@ -88,6 +236,12 @@ spec:
88
236
_ , err = w .Write ([]byte (outerloopDeployContent ))
89
237
} else if strings .Contains (r .URL .Path , "/outerloop-service.yaml" ) {
90
238
_ , err = w .Write ([]byte (outerloopServiceContent ))
239
+ } else if strings .Contains (r .URL .Path , "/devfile1.yaml" ) {
240
+ _ , err = w .Write ([]byte (devfileContent ))
241
+ } else if r .URL .Path == "/registry/devfiles/devfile1/" {
242
+ _ , err = w .Write ([]byte (devfileContent ))
243
+ } else if r .URL .Path == "/index" {
244
+ _ , err = w .Write ([]byte (registryIndex ))
91
245
}
92
246
if err != nil {
93
247
t .Errorf ("unexpected error while writing yaml: %v" , err )
@@ -108,45 +262,6 @@ spec:
108
262
testServer .Start ()
109
263
defer testServer .Close ()
110
264
111
- devfileContent := `commands:
112
- - exec:
113
- commandLine: ./main {{ PARAMS }}
114
- component: runtime
115
- group:
116
- isDefault: true
117
- kind: run
118
- workingDir: ${PROJECT_SOURCE}
119
- id: run
120
- components:
121
- - container:
122
- endpoints:
123
- - name: http
124
- targetPort: 8080
125
- image: golang:latest
126
- memoryLimit: 1024Mi
127
- mountSources: true
128
- name: runtime
129
- - kubernetes:
130
- uri: http://127.0.0.1:8080/outerloop-deploy.yaml
131
- name: outerloop-deploy
132
- - openshift:
133
- uri: http://127.0.0.1:8080/outerloop-service.yaml
134
- name: outerloop-deploy2
135
- metadata:
136
- description: Stack with the latest Go version
137
- displayName: Go Runtime
138
- icon: https://raw.githubusercontent.com/devfile-samples/devfile-stack-icons/main/golang.svg
139
- language: go
140
- name: my-go-app
141
- projectType: go
142
- tags:
143
- - Go
144
- version: 1.0.0
145
- schemaVersion: 2.2.0
146
- `
147
-
148
- devfileContentWithVariable := devfileContent + `variables:
149
- PARAMS: foo`
150
265
type args struct {
151
266
args parser.ParserArgs
152
267
}
@@ -293,6 +408,125 @@ schemaVersion: 2.2.0
293
408
StarterProjects : map [string ][]string {},
294
409
},
295
410
},
411
+ {
412
+ name : "get content from path" ,
413
+ args : args {
414
+ args : parser.ParserArgs {
415
+ ExternalVariables : map [string ]string {
416
+ "PARAMS" : "baz" ,
417
+ },
418
+ Path : "./testdata/devfile1.yaml" ,
419
+ },
420
+ },
421
+ wantCommandLine : "./main baz" ,
422
+ wantVariables : map [string ]string {
423
+ "PARAMS" : "baz" ,
424
+ },
425
+ wantVarWarning : variables.VariableWarning {
426
+ Commands : map [string ][]string {},
427
+ Components : map [string ][]string {},
428
+ Projects : map [string ][]string {},
429
+ StarterProjects : map [string ][]string {},
430
+ },
431
+ },
432
+ {
433
+ name : "get content from url" ,
434
+ args : args {
435
+ args : parser.ParserArgs {
436
+ ExternalVariables : map [string ]string {
437
+ "PARAMS" : "baz" ,
438
+ },
439
+ URL : "http://" + filepath .Join (uri , "devfile1.yaml" ),
440
+ },
441
+ },
442
+ wantCommandLine : "./main baz" ,
443
+ wantVariables : map [string ]string {
444
+ "PARAMS" : "baz" ,
445
+ },
446
+ wantVarWarning : variables.VariableWarning {
447
+ Commands : map [string ][]string {},
448
+ Components : map [string ][]string {},
449
+ Projects : map [string ][]string {},
450
+ StarterProjects : map [string ][]string {},
451
+ },
452
+ },
453
+ {
454
+ name : "with parent and registry url in devfile" ,
455
+ args : args {
456
+ args : parser.ParserArgs {
457
+ ExternalVariables : map [string ]string {
458
+ "PARAMS" : "baz" ,
459
+ },
460
+ Data : []byte (devfileContentWithParent ),
461
+ },
462
+ },
463
+ wantCommandLine : "./main baz" ,
464
+ wantVariables : map [string ]string {
465
+ "PARAMS" : "baz" ,
466
+ },
467
+ wantVarWarning : variables.VariableWarning {
468
+ Commands : map [string ][]string {},
469
+ Components : map [string ][]string {},
470
+ Projects : map [string ][]string {},
471
+ StarterProjects : map [string ][]string {},
472
+ },
473
+ },
474
+ {
475
+ name : "with parent and no registry url in devfile" ,
476
+ args : args {
477
+ args : parser.ParserArgs {
478
+ ExternalVariables : map [string ]string {
479
+ "PARAMS" : "baz" ,
480
+ },
481
+ Data : []byte (devfileContentWithParentNoRegistry ),
482
+ RegistryURLs : []string {
483
+ "http://127.0.0.1:8080/registry" ,
484
+ },
485
+ },
486
+ },
487
+ wantCommandLine : "./main baz" ,
488
+ wantVariables : map [string ]string {
489
+ "PARAMS" : "baz" ,
490
+ },
491
+ wantVarWarning : variables.VariableWarning {
492
+ Commands : map [string ][]string {},
493
+ Components : map [string ][]string {},
494
+ Projects : map [string ][]string {},
495
+ StarterProjects : map [string ][]string {},
496
+ },
497
+ },
498
+ {
499
+ name : "getting from cluster and setting default namespace" ,
500
+ args : args {
501
+ args : parser.ParserArgs {
502
+ ExternalVariables : map [string ]string {
503
+ "PARAMS" : "baz" ,
504
+ },
505
+ Data : []byte (devfileContentWithCRDParent ),
506
+ K8sClient : func () client.Client {
507
+ testK8sClient := & testingutil.FakeK8sClient {
508
+ ExpectedNamespace : "my-namespace" ,
509
+ DevWorkspaceResources : map [string ]schema.DevWorkspaceTemplate {
510
+ "devfile1" : devfileStruct ,
511
+ },
512
+ }
513
+ return testK8sClient
514
+ }(),
515
+ Context : context .Background (),
516
+ DefaultNamespace : "my-namespace" ,
517
+ },
518
+ },
519
+ wantCommandLine : "./main baz" ,
520
+ wantVariables : map [string ]string {
521
+ "PARAMS" : "baz" ,
522
+ },
523
+ wantVarWarning : variables.VariableWarning {
524
+ Commands : map [string ][]string {},
525
+ Components : map [string ][]string {},
526
+ Projects : map [string ][]string {},
527
+ StarterProjects : map [string ][]string {},
528
+ },
529
+ },
296
530
}
297
531
for _ , tt := range tests {
298
532
t .Run (tt .name , func (t * testing.T ) {
0 commit comments