1 package backend
2
3 import (
4 "context"
5 "testing"
6
7 "github.com/golang/mock/gomock"
8 "github.com/google/go-cmp/cmp"
9 "github.com/google/go-cmp/cmp/cmpopts"
10 "k8s.io/utils/pointer"
11
12 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
13 "github.com/devfile/library/v2/pkg/devfile/parser"
14 parsercontext "github.com/devfile/library/v2/pkg/devfile/parser/context"
15 "github.com/devfile/library/v2/pkg/devfile/parser/data"
16 dffilesystem "github.com/devfile/library/v2/pkg/testingutil/filesystem"
17
18 "github.com/redhat-developer/odo/pkg/api"
19 "github.com/redhat-developer/odo/pkg/registry"
20 "github.com/redhat-developer/odo/pkg/testingutil"
21 "github.com/redhat-developer/odo/pkg/testingutil/filesystem"
22 )
23
24 func TestFlagsBackend_SelectDevfile(t *testing.T) {
25 type fields struct {
26 flags map[string]string
27 }
28 tests := []struct {
29 name string
30 fields fields
31 want *api.DetectionResult
32 wantErr bool
33 }{
34 {
35 name: "all fields defined",
36 fields: fields{
37 flags: map[string]string{
38 FLAG_DEVFILE: "adevfile",
39 FLAG_DEVFILE_PATH: "apath",
40 FLAG_DEVFILE_REGISTRY: "aregistry",
41 },
42 },
43 wantErr: false,
44 want: &api.DetectionResult{
45 Devfile: "adevfile",
46 DevfilePath: "apath",
47 DevfileRegistry: "aregistry",
48 Architectures: []string{},
49 },
50 },
51 }
52 for _, tt := range tests {
53 t.Run(tt.name, func(t *testing.T) {
54 o := &FlagsBackend{}
55 ctx := context.Background()
56 got, err := o.SelectDevfile(ctx, tt.fields.flags, nil, "")
57 if (err != nil) != tt.wantErr {
58 t.Errorf("FlagsBackend.SelectDevfile() error = %v, wantErr %v", err, tt.wantErr)
59 return
60 }
61 if diff := cmp.Diff(tt.want, got); diff != "" {
62 t.Errorf("FlagsBackend.SelectDevfile() mismatch (-want +got):\n%s", diff)
63 }
64 })
65 }
66 }
67
68 func TestFlagsBackend_Validate(t *testing.T) {
69 type fields struct {
70 }
71 type args struct {
72 flags map[string]string
73 fsys func() filesystem.Filesystem
74 dir string
75 }
76 tests := []struct {
77 name string
78 fields fields
79 args args
80 registryList []api.Registry
81 wantErr bool
82 }{
83 {
84 name: "no name passed",
85 args: args{
86 flags: map[string]string{
87 "name": "",
88 },
89 fsys: func() filesystem.Filesystem {
90 fs := filesystem.NewFakeFs()
91 _ = fs.MkdirAll("/tmp", 0644)
92 return fs
93 },
94 dir: "/tmp",
95 },
96 wantErr: true,
97 },
98 {
99 name: "no devfile info passed",
100 args: args{
101 flags: map[string]string{
102 "name": "aname",
103 },
104 fsys: func() filesystem.Filesystem {
105 fs := filesystem.NewFakeFs()
106 _ = fs.MkdirAll("/tmp", 0644)
107 return fs
108 },
109 dir: "/tmp",
110 },
111 wantErr: true,
112 },
113 {
114 name: "devfile passed with a single registry",
115 args: args{
116 flags: map[string]string{
117 "name": "aname",
118 "devfile": "adevfile",
119 },
120 fsys: func() filesystem.Filesystem {
121 fs := filesystem.NewFakeFs()
122 _ = fs.MkdirAll("/tmp", 0644)
123 return fs
124 },
125 dir: "/tmp",
126 },
127 registryList: []api.Registry{
128 {
129 Name: "aregistry",
130 },
131 },
132 wantErr: false,
133 },
134 {
135 name: "devfile and devfile-path passed",
136 args: args{
137 flags: map[string]string{
138 "name": "aname",
139 "devfile": "adevfile",
140 "devfile-path": "apath",
141 },
142 fsys: func() filesystem.Filesystem {
143 fs := filesystem.NewFakeFs()
144 _ = fs.MkdirAll("/tmp", 0644)
145 return fs
146 },
147 dir: "/tmp",
148 },
149 wantErr: true,
150 },
151 {
152 name: "devfile and devfile-registry passed",
153 args: args{
154 flags: map[string]string{
155 "name": "aname",
156 "devfile": "adevfile",
157 "devfile-registry": "aregistry",
158 },
159 fsys: func() filesystem.Filesystem {
160 fs := filesystem.NewFakeFs()
161 _ = fs.MkdirAll("/tmp", 0644)
162 return fs
163 },
164 dir: "/tmp",
165 },
166 registryList: []api.Registry{
167 {
168 Name: "aregistry",
169 },
170 },
171 wantErr: false,
172 },
173 {
174 name: "devfile and devfile-registry passed with non existing registry",
175 args: args{
176 flags: map[string]string{
177 "name": "aname",
178 "devfile": "adevfile",
179 "devfile-registry": "aregistry",
180 },
181 fsys: func() filesystem.Filesystem {
182 fs := filesystem.NewFakeFs()
183 _ = fs.MkdirAll("/tmp", 0644)
184 return fs
185 },
186 dir: "/tmp",
187 },
188 wantErr: true,
189 },
190 {
191 name: "devfile-path and devfile-registry passed",
192 args: args{
193 flags: map[string]string{
194 "name": "aname",
195 "devfile-path": "apath",
196 "devfile-registry": "aregistry",
197 },
198 fsys: func() filesystem.Filesystem {
199 fs := filesystem.NewFakeFs()
200 _ = fs.MkdirAll("/tmp", 0644)
201 return fs
202 },
203 dir: "/tmp",
204 },
205 registryList: []api.Registry{
206 {
207 Name: "aregistry",
208 },
209 },
210 wantErr: true,
211 },
212 {
213 name: "numeric name",
214 args: args{
215 flags: map[string]string{
216 "name": "1234",
217 "devfile": "adevfile",
218 },
219 fsys: func() filesystem.Filesystem {
220 fs := filesystem.NewFakeFs()
221 _ = fs.MkdirAll("/tmp", 0644)
222 return fs
223 },
224 dir: "/tmp",
225 },
226 wantErr: true,
227 },
228 {
229 name: "non DNS name",
230 args: args{
231 flags: map[string]string{
232 "name": "WrongName",
233 "devfile": "adevfile",
234 },
235 fsys: func() filesystem.Filesystem {
236 fs := filesystem.NewFakeFs()
237 _ = fs.MkdirAll("/tmp", 0644)
238 return fs
239 },
240 dir: "/tmp",
241 },
242 wantErr: true,
243 },
244 {
245 name: "starter flag with an empty directory",
246 args: args{
247 flags: map[string]string{
248 "name": "aname",
249 "devfile": "adevfile",
250 "starter": "astarter",
251 },
252 fsys: func() filesystem.Filesystem {
253 fs := filesystem.NewFakeFs()
254 _ = fs.MkdirAll("/tmp", 0644)
255 return fs
256 },
257 dir: "/tmp",
258 },
259 wantErr: false,
260 },
261 {
262 name: "starter flag with a non empty directory",
263 args: args{
264 flags: map[string]string{
265 "name": "aname",
266 "devfile": "adevfile",
267 "starter": "astarter",
268 },
269 fsys: func() filesystem.Filesystem {
270 fs := filesystem.NewFakeFs()
271 _ = fs.MkdirAll("/tmp", 0644)
272 _ = fs.WriteFile("/tmp/main.go", []byte("package main"), 0644)
273 return fs
274 },
275 dir: "/tmp",
276 },
277 wantErr: true,
278 },
279
280 }
281 for _, tt := range tests {
282 t.Run(tt.name, func(t *testing.T) {
283 ctrl := gomock.NewController(t)
284
285 registryClient := registry.NewMockClient(ctrl)
286 registryClient.EXPECT().GetDevfileRegistries(gomock.Eq(tt.args.flags[FLAG_DEVFILE_REGISTRY])).Return(tt.registryList, nil).AnyTimes()
287
288 o := &FlagsBackend{
289 registryClient: registryClient,
290 }
291 if err := o.Validate(tt.args.flags, tt.args.fsys(), tt.args.dir); (err != nil) != tt.wantErr {
292 t.Errorf("FlagsBackend.Validate() error = %v, wantErr %v", err, tt.wantErr)
293 }
294 })
295 }
296 }
297
298 func TestFlagsBackend_SelectStarterProject(t *testing.T) {
299 type fields struct {
300 registryClient registry.Client
301 }
302 type args struct {
303 devfile func() parser.DevfileObj
304 flags map[string]string
305 }
306 tests := []struct {
307 name string
308 fields fields
309 args args
310 want *v1alpha2.StarterProject
311 wantErr bool
312 }{
313 {
314 name: "some flags, but not starter",
315 args: args{
316 devfile: func() parser.DevfileObj {
317 return parser.DevfileObj{}
318 },
319 flags: map[string]string{
320 "devfile": "adevfile",
321 },
322 },
323 want: nil,
324 wantErr: false,
325 },
326 {
327 name: "starter flag defined and starter exists",
328 args: args{
329 devfile: func() parser.DevfileObj {
330 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
331 _ = devfileData.AddStarterProjects([]v1alpha2.StarterProject{
332 {
333 Name: "starter1",
334 },
335 {
336 Name: "starter2",
337 },
338 {
339 Name: "starter3",
340 },
341 })
342 return parser.DevfileObj{
343 Data: devfileData,
344 }
345 },
346 flags: map[string]string{
347 "devfile": "adevfile",
348 "starter": "starter2",
349 },
350 },
351 want: &v1alpha2.StarterProject{
352 Name: "starter2",
353 },
354 wantErr: false,
355 },
356 {
357 name: "starter flag defined and starter does not exist",
358 args: args{
359 devfile: func() parser.DevfileObj {
360 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
361 _ = devfileData.AddStarterProjects([]v1alpha2.StarterProject{
362 {
363 Name: "starter1",
364 },
365 {
366 Name: "starter3",
367 },
368 })
369 return parser.DevfileObj{
370 Data: devfileData,
371 }
372 },
373 flags: map[string]string{
374 "devfile": "adevfile",
375 "starter": "starter4",
376 },
377 },
378 want: nil,
379 wantErr: true,
380 },
381
382 }
383 for _, tt := range tests {
384 t.Run(tt.name, func(t *testing.T) {
385 o := &FlagsBackend{
386 registryClient: tt.fields.registryClient,
387 }
388 got1, err := o.SelectStarterProject(tt.args.devfile(), tt.args.flags)
389 if (err != nil) != tt.wantErr {
390 t.Errorf("FlagsBackend.SelectStarterProject() error = %v, wantErr %v", err, tt.wantErr)
391 return
392 }
393 if diff := cmp.Diff(tt.want, got1); diff != "" {
394 t.Errorf("FlagsBackend.SelectStarterProject() mismatch (-want +got):\n%s", diff)
395 }
396 })
397 }
398 }
399
400 func TestFlagsBackend_PersonalizeName(t *testing.T) {
401 type fields struct {
402 registryClient registry.Client
403 }
404 type args struct {
405 devfile func(fs dffilesystem.Filesystem) parser.DevfileObj
406 flags map[string]string
407 }
408 tests := []struct {
409 name string
410 fields fields
411 args args
412 wantErr bool
413 checkResult func(newName string, args args) bool
414 }{
415 {
416 name: "name flag",
417 args: args{
418 devfile: func(fs dffilesystem.Filesystem) parser.DevfileObj {
419 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
420 obj := parser.DevfileObj{
421 Ctx: parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
422 Data: devfileData,
423 }
424 return obj
425 },
426 flags: map[string]string{
427 "devfile": "adevfile",
428 "name": "a-name",
429 },
430 },
431 wantErr: false,
432 checkResult: func(newName string, args args) bool {
433 return newName == args.flags["name"]
434 },
435 },
436 {
437 name: "invalid name flag",
438 args: args{
439 devfile: func(fs dffilesystem.Filesystem) parser.DevfileObj {
440 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion200))
441 obj := parser.DevfileObj{
442 Ctx: parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
443 Data: devfileData,
444 }
445 return obj
446 },
447 flags: map[string]string{
448 "devfile": "adevfile",
449 "name": "1234",
450 },
451 },
452 wantErr: true,
453 checkResult: func(newName string, args args) bool {
454 return newName == ""
455 },
456 },
457 }
458 for _, tt := range tests {
459 t.Run(tt.name, func(t *testing.T) {
460 o := &FlagsBackend{
461 registryClient: tt.fields.registryClient,
462 }
463 fs := dffilesystem.NewFakeFs()
464 newName, err := o.PersonalizeName(tt.args.devfile(fs), tt.args.flags)
465 if (err != nil) != tt.wantErr {
466 t.Errorf("FlagsBackend.PersonalizeName() error = %v, wantErr %v", err, tt.wantErr)
467 return
468 }
469 if tt.checkResult != nil && !tt.checkResult(newName, tt.args) {
470 t.Errorf("FlagsBackend.PersonalizeName(), checking result failed")
471 }
472 })
473 }
474 }
475
476 func TestFlagsBackend_HandleApplicationPorts(t *testing.T) {
477 type devfileProvider func(fs dffilesystem.Filesystem) (parser.DevfileObj, error)
478
479 zeroDevfileProvider := func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
480 return parser.DevfileObj{}, nil
481 }
482 fakeDevfileProvider := func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
483 devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion220))
484 obj := parser.DevfileObj{
485 Ctx: parsercontext.FakeContext(fs, "/tmp/devfile.yaml"),
486 Data: devfileData,
487 }
488 return obj, nil
489 }
490 type fields struct {
491 registryClient registry.Client
492 }
493 type args struct {
494 devfileObjProvider devfileProvider
495 flags map[string]string
496 }
497 tests := []struct {
498 name string
499 fields fields
500 args args
501 wantProvider devfileProvider
502 wantErr bool
503 }{
504 {
505 name: "no run-port flag",
506 args: args{
507 devfileObjProvider: fakeDevfileProvider,
508 flags: map[string]string{
509 "opt1": "val1",
510 FLAG_NAME: "my-name",
511 },
512 },
513 wantProvider: fakeDevfileProvider,
514 },
515 {
516 name: "flag string value not enclosed within []",
517 args: args{
518 devfileObjProvider: fakeDevfileProvider,
519 flags: map[string]string{
520 FLAG_NAME: "my-name",
521 FLAG_RUN_PORT: "aaa,bbb",
522 },
523 },
524 wantProvider: fakeDevfileProvider,
525 },
526 {
527 name: "invalid port type",
528 args: args{
529 devfileObjProvider: fakeDevfileProvider,
530 flags: map[string]string{
531 FLAG_NAME: "my-name",
532 FLAG_RUN_PORT: "[8080,abcde]",
533 },
534 },
535 wantErr: true,
536 wantProvider: zeroDevfileProvider,
537 },
538 {
539 name: "devfile with no command, but --run-port set",
540 args: args{
541 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
542 devfileObj, err := fakeDevfileProvider(fs)
543 if err != nil {
544 return parser.DevfileObj{}, err
545 }
546 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
547 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
548 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
549 })
550 if err != nil {
551 return parser.DevfileObj{}, err
552 }
553 return devfileObj, nil
554 },
555 flags: map[string]string{
556 FLAG_NAME: "my-name",
557 FLAG_RUN_PORT: "[8080,8081]",
558 },
559 },
560 wantProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
561 devfileObj, err := fakeDevfileProvider(fs)
562 if err != nil {
563 return parser.DevfileObj{}, err
564 }
565 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
566 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
567 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
568 })
569 if err != nil {
570 return parser.DevfileObj{}, err
571 }
572 return devfileObj, nil
573 },
574 },
575 {
576 name: "devfile with more than one default run commands, --run-port set",
577 args: args{
578 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
579 devfileObj, err := fakeDevfileProvider(fs)
580 if err != nil {
581 return parser.DevfileObj{}, err
582 }
583 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
584 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
585 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
586 })
587 if err != nil {
588 return parser.DevfileObj{}, err
589 }
590 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
591 {
592 Id: "devrun1",
593 CommandUnion: v1alpha2.CommandUnion{
594 Exec: &v1alpha2.ExecCommand{
595 LabeledCommand: v1alpha2.LabeledCommand{
596 BaseCommand: v1alpha2.BaseCommand{
597 Group: &v1alpha2.CommandGroup{
598 Kind: v1alpha2.RunCommandGroupKind,
599 IsDefault: pointer.Bool(true),
600 },
601 },
602 },
603 },
604 },
605 },
606 {
607 Id: "devrun2",
608 CommandUnion: v1alpha2.CommandUnion{
609 Exec: &v1alpha2.ExecCommand{
610 LabeledCommand: v1alpha2.LabeledCommand{
611 BaseCommand: v1alpha2.BaseCommand{
612 Group: &v1alpha2.CommandGroup{
613 Kind: v1alpha2.RunCommandGroupKind,
614 IsDefault: pointer.Bool(true),
615 },
616 },
617 },
618 },
619 },
620 },
621 })
622 if err != nil {
623 return parser.DevfileObj{}, err
624 }
625 return devfileObj, nil
626 },
627 flags: map[string]string{
628 FLAG_NAME: "my-name",
629 FLAG_RUN_PORT: "[8080,8081]",
630 },
631 },
632 wantErr: true,
633 wantProvider: zeroDevfileProvider,
634 },
635 {
636 name: "devfile with more than one non-default run commands, --run-port set",
637 args: args{
638 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
639 devfileObj, err := fakeDevfileProvider(fs)
640 if err != nil {
641 return parser.DevfileObj{}, err
642 }
643 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
644 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
645 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
646 })
647 if err != nil {
648 return parser.DevfileObj{}, err
649 }
650 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
651 {
652 Id: "devrun1",
653 CommandUnion: v1alpha2.CommandUnion{
654 Exec: &v1alpha2.ExecCommand{
655 LabeledCommand: v1alpha2.LabeledCommand{
656 BaseCommand: v1alpha2.BaseCommand{
657 Group: &v1alpha2.CommandGroup{
658 Kind: v1alpha2.RunCommandGroupKind,
659 IsDefault: pointer.Bool(false),
660 },
661 },
662 },
663 },
664 },
665 },
666 {
667 Id: "devrun2",
668 CommandUnion: v1alpha2.CommandUnion{
669 Exec: &v1alpha2.ExecCommand{
670 LabeledCommand: v1alpha2.LabeledCommand{
671 BaseCommand: v1alpha2.BaseCommand{
672 Group: &v1alpha2.CommandGroup{
673 Kind: v1alpha2.RunCommandGroupKind,
674 IsDefault: pointer.Bool(false),
675 },
676 },
677 },
678 },
679 },
680 },
681 })
682 if err != nil {
683 return parser.DevfileObj{}, err
684 }
685 return devfileObj, nil
686 },
687 flags: map[string]string{
688 FLAG_NAME: "my-name",
689 FLAG_RUN_PORT: "[8080,8081]",
690 },
691 },
692 wantProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
693 devfileObj, err := fakeDevfileProvider(fs)
694 if err != nil {
695 return parser.DevfileObj{}, err
696 }
697 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
698 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
699 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
700 })
701 if err != nil {
702 return parser.DevfileObj{}, err
703 }
704 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
705 {
706 Id: "devrun1",
707 CommandUnion: v1alpha2.CommandUnion{
708 Exec: &v1alpha2.ExecCommand{
709 LabeledCommand: v1alpha2.LabeledCommand{
710 BaseCommand: v1alpha2.BaseCommand{
711 Group: &v1alpha2.CommandGroup{
712 Kind: v1alpha2.RunCommandGroupKind,
713 IsDefault: pointer.Bool(false),
714 },
715 },
716 },
717 },
718 },
719 },
720 {
721 Id: "devrun2",
722 CommandUnion: v1alpha2.CommandUnion{
723 Exec: &v1alpha2.ExecCommand{
724 LabeledCommand: v1alpha2.LabeledCommand{
725 BaseCommand: v1alpha2.BaseCommand{
726 Group: &v1alpha2.CommandGroup{
727 Kind: v1alpha2.RunCommandGroupKind,
728 IsDefault: pointer.Bool(false),
729 },
730 },
731 },
732 },
733 },
734 },
735 })
736 if err != nil {
737 return parser.DevfileObj{}, err
738 }
739 return devfileObj, nil
740 },
741 },
742 {
743 name: "devfile with no run command, --run-port set",
744 args: args{
745 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
746 devfileObj, err := fakeDevfileProvider(fs)
747 if err != nil {
748 return parser.DevfileObj{}, err
749 }
750 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
751 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
752 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
753 })
754 if err != nil {
755 return parser.DevfileObj{}, err
756 }
757 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
758 {
759 Id: "devdebug",
760 CommandUnion: v1alpha2.CommandUnion{
761 Exec: &v1alpha2.ExecCommand{
762 LabeledCommand: v1alpha2.LabeledCommand{
763 BaseCommand: v1alpha2.BaseCommand{
764 Group: &v1alpha2.CommandGroup{Kind: v1alpha2.DebugCommandGroupKind},
765 },
766 },
767 },
768 },
769 },
770 })
771 if err != nil {
772 return parser.DevfileObj{}, err
773 }
774 return devfileObj, nil
775 },
776 flags: map[string]string{
777 FLAG_NAME: "my-name",
778 FLAG_RUN_PORT: "[8080,8081]",
779 },
780 },
781 wantProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
782 devfileObj, err := fakeDevfileProvider(fs)
783 if err != nil {
784 return parser.DevfileObj{}, err
785 }
786 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
787 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
788 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
789 })
790 if err != nil {
791 return parser.DevfileObj{}, err
792 }
793 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
794 {
795 Id: "devdebug",
796 CommandUnion: v1alpha2.CommandUnion{
797 Exec: &v1alpha2.ExecCommand{
798 LabeledCommand: v1alpha2.LabeledCommand{
799 BaseCommand: v1alpha2.BaseCommand{
800 Group: &v1alpha2.CommandGroup{Kind: v1alpha2.DebugCommandGroupKind},
801 },
802 },
803 },
804 },
805 },
806 })
807 if err != nil {
808 return parser.DevfileObj{}, err
809 }
810 return devfileObj, nil
811 },
812 },
813 {
814 name: "devfile with a default non-exec (apply) run command, --run-port set",
815 args: args{
816 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
817 devfileObj, err := fakeDevfileProvider(fs)
818 if err != nil {
819 return parser.DevfileObj{}, err
820 }
821 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
822 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
823 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
824 })
825 if err != nil {
826 return parser.DevfileObj{}, err
827 }
828 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
829 {
830 Id: "devrun1",
831 CommandUnion: v1alpha2.CommandUnion{
832 Apply: &v1alpha2.ApplyCommand{
833 LabeledCommand: v1alpha2.LabeledCommand{
834 BaseCommand: v1alpha2.BaseCommand{
835 Group: &v1alpha2.CommandGroup{
836 Kind: v1alpha2.RunCommandGroupKind,
837 IsDefault: pointer.Bool(true),
838 },
839 },
840 },
841 },
842 },
843 },
844 })
845 if err != nil {
846 return parser.DevfileObj{}, err
847 }
848 return devfileObj, nil
849 },
850 flags: map[string]string{
851 FLAG_NAME: "my-name",
852 FLAG_RUN_PORT: "[8080,8081]",
853 },
854 },
855 wantErr: true,
856 wantProvider: zeroDevfileProvider,
857 },
858 {
859 name: "devfile with a default non-exec (composite) run command, --run-port set",
860 args: args{
861 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
862 devfileObj, err := fakeDevfileProvider(fs)
863 if err != nil {
864 return parser.DevfileObj{}, err
865 }
866 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
867 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
868 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
869 })
870 if err != nil {
871 return parser.DevfileObj{}, err
872 }
873 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
874 {
875 Id: "devrun1",
876 CommandUnion: v1alpha2.CommandUnion{
877 Composite: &v1alpha2.CompositeCommand{
878 LabeledCommand: v1alpha2.LabeledCommand{
879 BaseCommand: v1alpha2.BaseCommand{
880 Group: &v1alpha2.CommandGroup{
881 Kind: v1alpha2.RunCommandGroupKind,
882 IsDefault: pointer.Bool(true),
883 },
884 },
885 },
886 },
887 },
888 },
889 })
890 if err != nil {
891 return parser.DevfileObj{}, err
892 }
893 return devfileObj, nil
894 },
895 flags: map[string]string{
896 FLAG_NAME: "my-name",
897 FLAG_RUN_PORT: "[8080,8081]",
898 },
899 },
900 wantErr: true,
901 wantProvider: zeroDevfileProvider,
902 },
903
904 {
905 name: "devfile with an exec run command with non-container component, --run-port set",
906 args: args{
907 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
908 devfileObj, err := fakeDevfileProvider(fs)
909 if err != nil {
910 return parser.DevfileObj{}, err
911 }
912 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
913 {
914 Id: "devrun1",
915 CommandUnion: v1alpha2.CommandUnion{
916 Exec: &v1alpha2.ExecCommand{
917 LabeledCommand: v1alpha2.LabeledCommand{
918 BaseCommand: v1alpha2.BaseCommand{
919 Group: &v1alpha2.CommandGroup{
920 Kind: v1alpha2.RunCommandGroupKind,
921 IsDefault: pointer.Bool(true),
922 },
923 },
924 },
925 Component: "some-random-name",
926 },
927 },
928 },
929 })
930 if err != nil {
931 return parser.DevfileObj{}, err
932 }
933 return devfileObj, nil
934 },
935 flags: map[string]string{
936 FLAG_NAME: "my-name",
937 FLAG_RUN_PORT: "[8080,8081]",
938 },
939 },
940 wantErr: true,
941 wantProvider: zeroDevfileProvider,
942 },
943 {
944 name: "devfile with an exec run command with non-container component, --run-port set",
945 args: args{
946 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
947 devfileObj, err := fakeDevfileProvider(fs)
948 if err != nil {
949 return parser.DevfileObj{}, err
950 }
951 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
952 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
953 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
954 {
955 Name: "k8s-comp1",
956 ComponentUnion: v1alpha2.ComponentUnion{
957 Kubernetes: &v1alpha2.KubernetesComponent{
958 K8sLikeComponent: v1alpha2.K8sLikeComponent{
959 K8sLikeComponentLocation: v1alpha2.K8sLikeComponentLocation{
960 Inlined: "some-k8s-def",
961 },
962 },
963 },
964 },
965 },
966 })
967 if err != nil {
968 return parser.DevfileObj{}, err
969 }
970 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
971 {
972 Id: "devrun1",
973 CommandUnion: v1alpha2.CommandUnion{
974 Exec: &v1alpha2.ExecCommand{
975 LabeledCommand: v1alpha2.LabeledCommand{
976 BaseCommand: v1alpha2.BaseCommand{
977 Group: &v1alpha2.CommandGroup{
978 Kind: v1alpha2.RunCommandGroupKind,
979 IsDefault: pointer.Bool(true),
980 },
981 },
982 },
983 Component: "k8s-comp1",
984 },
985 },
986 },
987 })
988 if err != nil {
989 return parser.DevfileObj{}, err
990 }
991 return devfileObj, nil
992 },
993 flags: map[string]string{
994 FLAG_NAME: "my-name",
995 FLAG_RUN_PORT: "[8080,8081]",
996 },
997 },
998 wantErr: true,
999 wantProvider: zeroDevfileProvider,
1000 },
1001 {
1002 name: "devfile with default exec run command with container component, --run-port set",
1003 args: args{
1004 devfileObjProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
1005 devfileObj, err := fakeDevfileProvider(fs)
1006 if err != nil {
1007 return parser.DevfileObj{}, err
1008 }
1009 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
1010 testingutil.GetFakeContainerComponent("my-cont1", 1234, 2345),
1011 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
1012 })
1013 if err != nil {
1014 return parser.DevfileObj{}, err
1015 }
1016 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
1017 {
1018 Id: "devrun1",
1019 CommandUnion: v1alpha2.CommandUnion{
1020 Exec: &v1alpha2.ExecCommand{
1021 LabeledCommand: v1alpha2.LabeledCommand{
1022 BaseCommand: v1alpha2.BaseCommand{
1023 Group: &v1alpha2.CommandGroup{
1024 Kind: v1alpha2.RunCommandGroupKind,
1025 IsDefault: pointer.Bool(true),
1026 },
1027 },
1028 },
1029 Component: "my-cont1",
1030 },
1031 },
1032 },
1033 {
1034 Id: "devdebug1",
1035 CommandUnion: v1alpha2.CommandUnion{
1036 Exec: &v1alpha2.ExecCommand{
1037 LabeledCommand: v1alpha2.LabeledCommand{
1038 BaseCommand: v1alpha2.BaseCommand{
1039 Group: &v1alpha2.CommandGroup{
1040 Kind: v1alpha2.DebugCommandGroupKind,
1041 IsDefault: pointer.Bool(true),
1042 },
1043 },
1044 },
1045 Component: "my-cont2",
1046 },
1047 },
1048 },
1049 })
1050 if err != nil {
1051 return parser.DevfileObj{}, err
1052 }
1053 return devfileObj, nil
1054 },
1055 flags: map[string]string{
1056 FLAG_NAME: "my-name",
1057 FLAG_RUN_PORT: "[8080,8081]",
1058 },
1059 },
1060 wantErr: false,
1061 wantProvider: func(fs dffilesystem.Filesystem) (parser.DevfileObj, error) {
1062 devfileObj, err := fakeDevfileProvider(fs)
1063 if err != nil {
1064 return parser.DevfileObj{}, err
1065 }
1066
1067 cont1 := testingutil.GetFakeContainerComponent("my-cont1")
1068 cont1.Container.Endpoints = append(cont1.Container.Endpoints,
1069 v1alpha2.Endpoint{
1070 Name: "port-8080-tcp",
1071 TargetPort: 8080,
1072 Protocol: "tcp",
1073 },
1074 v1alpha2.Endpoint{
1075 Name: "port-8081-tcp",
1076 TargetPort: 8081,
1077 Protocol: "tcp",
1078 },
1079 )
1080 err = devfileObj.Data.AddComponents([]v1alpha2.Component{
1081 cont1,
1082 testingutil.GetFakeContainerComponent("my-cont2", 4321, 5432),
1083 })
1084 if err != nil {
1085 return parser.DevfileObj{}, err
1086 }
1087 err = devfileObj.Data.AddCommands([]v1alpha2.Command{
1088 {
1089 Id: "devrun1",
1090 CommandUnion: v1alpha2.CommandUnion{
1091 Exec: &v1alpha2.ExecCommand{
1092 LabeledCommand: v1alpha2.LabeledCommand{
1093 BaseCommand: v1alpha2.BaseCommand{
1094 Group: &v1alpha2.CommandGroup{
1095 Kind: v1alpha2.RunCommandGroupKind,
1096 IsDefault: pointer.Bool(true),
1097 },
1098 },
1099 },
1100 Component: "my-cont1",
1101 },
1102 },
1103 },
1104 {
1105 Id: "devdebug1",
1106 CommandUnion: v1alpha2.CommandUnion{
1107 Exec: &v1alpha2.ExecCommand{
1108 LabeledCommand: v1alpha2.LabeledCommand{
1109 BaseCommand: v1alpha2.BaseCommand{
1110 Group: &v1alpha2.CommandGroup{
1111 Kind: v1alpha2.DebugCommandGroupKind,
1112 IsDefault: pointer.Bool(true),
1113 },
1114 },
1115 },
1116 Component: "my-cont2",
1117 },
1118 },
1119 },
1120 })
1121 if err != nil {
1122 return parser.DevfileObj{}, err
1123 }
1124 return devfileObj, nil
1125 },
1126 },
1127 }
1128 for _, tt := range tests {
1129 t.Run(tt.name, func(t *testing.T) {
1130 o := &FlagsBackend{
1131 registryClient: tt.fields.registryClient,
1132 }
1133 fs := dffilesystem.NewFakeFs()
1134 devfileObj, err := tt.args.devfileObjProvider(fs)
1135 if err != nil {
1136 t.Errorf("error building input DevfileObj: %v", err)
1137 return
1138 }
1139 want, err := tt.wantProvider(fs)
1140 if err != nil {
1141 t.Errorf("error building expected DevfileObj: %v", err)
1142 return
1143 }
1144
1145 got, err := o.HandleApplicationPorts(devfileObj, nil, tt.args.flags)
1146 if (err != nil) != tt.wantErr {
1147 t.Errorf("HandleApplicationPorts() error = %v, wantErr %v", err, tt.wantErr)
1148 return
1149 }
1150
1151 if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(parsercontext.DevfileCtx{})); diff != "" {
1152 t.Errorf("HandleApplicationPorts() mismatch (-want +got):\n%s", diff)
1153 }
1154 })
1155 }
1156 }
1157
View as plain text