1 package devstate
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 . "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
8 openapi "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
9 )
10
11 func TestDevfileState_AddContainer(t *testing.T) {
12 type args struct {
13 name string
14 image string
15 command []string
16 args []string
17 envs []Env
18 memRequest string
19 memLimit string
20 cpuRequest string
21 cpuLimit string
22 volumeMounts []openapi.VolumeMount
23 configureSources bool
24 mountSources bool
25 sourceMapping string
26 annotation Annotation
27 endpoints []Endpoint
28 }
29 tests := []struct {
30 name string
31 state func() DevfileState
32 args args
33 want DevfileContent
34 wantErr bool
35 }{
36 {
37 name: "Add a container, with sources configured",
38 state: func() DevfileState {
39 return NewDevfileState()
40 },
41 args: args{
42 name: "a-name",
43 image: "an-image",
44 command: []string{"run", "command"},
45 args: []string{"arg1", "arg2"},
46 memRequest: "1Gi",
47 memLimit: "2Gi",
48 cpuRequest: "100m",
49 cpuLimit: "200m",
50 volumeMounts: []openapi.VolumeMount{
51 {
52 Name: "vol1",
53 Path: "/mnt/volume1",
54 },
55 },
56 configureSources: true,
57 mountSources: false,
58 },
59 want: DevfileContent{
60 Content: `components:
61 - container:
62 args:
63 - arg1
64 - arg2
65 command:
66 - run
67 - command
68 cpuLimit: 200m
69 cpuRequest: 100m
70 image: an-image
71 memoryLimit: 2Gi
72 memoryRequest: 1Gi
73 mountSources: false
74 volumeMounts:
75 - name: vol1
76 path: /mnt/volume1
77 name: a-name
78 metadata: {}
79 schemaVersion: 2.2.0
80 `,
81 Version: "2.2.0",
82 Commands: []Command{},
83 Containers: []Container{
84 {
85 Name: "a-name",
86 Image: "an-image",
87 Command: []string{"run", "command"},
88 Args: []string{"arg1", "arg2"},
89 MemoryRequest: "1Gi",
90 MemoryLimit: "2Gi",
91 CpuRequest: "100m",
92 CpuLimit: "200m",
93 VolumeMounts: []openapi.VolumeMount{
94 {
95 Name: "vol1",
96 Path: "/mnt/volume1",
97 },
98 },
99 Endpoints: []openapi.Endpoint{},
100 Env: []openapi.Env{},
101 ConfigureSources: true,
102 MountSources: false,
103 },
104 },
105 Images: []Image{},
106 Resources: []Resource{},
107 Volumes: []Volume{},
108 Events: Events{},
109 },
110 },
111 {
112 name: "Add a container, without sources configured",
113 state: func() DevfileState {
114 return NewDevfileState()
115 },
116 args: args{
117 name: "a-name",
118 image: "an-image",
119 command: []string{"run", "command"},
120 args: []string{"arg1", "arg2"},
121 memRequest: "1Gi",
122 memLimit: "2Gi",
123 cpuRequest: "100m",
124 cpuLimit: "200m",
125 volumeMounts: []openapi.VolumeMount{
126 {
127 Name: "vol1",
128 Path: "/mnt/volume1",
129 },
130 },
131 configureSources: false,
132 },
133 want: DevfileContent{
134 Content: `components:
135 - container:
136 args:
137 - arg1
138 - arg2
139 command:
140 - run
141 - command
142 cpuLimit: 200m
143 cpuRequest: 100m
144 image: an-image
145 memoryLimit: 2Gi
146 memoryRequest: 1Gi
147 volumeMounts:
148 - name: vol1
149 path: /mnt/volume1
150 name: a-name
151 metadata: {}
152 schemaVersion: 2.2.0
153 `,
154 Version: "2.2.0",
155 Commands: []Command{},
156 Containers: []Container{
157 {
158 Name: "a-name",
159 Image: "an-image",
160 Command: []string{"run", "command"},
161 Args: []string{"arg1", "arg2"},
162 MemoryRequest: "1Gi",
163 MemoryLimit: "2Gi",
164 CpuRequest: "100m",
165 CpuLimit: "200m",
166 VolumeMounts: []openapi.VolumeMount{
167 {
168 Name: "vol1",
169 Path: "/mnt/volume1",
170 },
171 },
172 Endpoints: []openapi.Endpoint{},
173 Env: []openapi.Env{},
174 ConfigureSources: false,
175 MountSources: true,
176 },
177 },
178 Images: []Image{},
179 Resources: []Resource{},
180 Volumes: []Volume{},
181 Events: Events{},
182 },
183 },
184
185 }
186 for _, tt := range tests {
187 t.Run(tt.name, func(t *testing.T) {
188 o := tt.state()
189 got, err := o.AddContainer(tt.args.name, tt.args.image, tt.args.command, tt.args.args, tt.args.envs, tt.args.memRequest, tt.args.memLimit, tt.args.cpuRequest, tt.args.cpuLimit, tt.args.volumeMounts, tt.args.configureSources, tt.args.mountSources, tt.args.sourceMapping, tt.args.annotation, tt.args.endpoints)
190 if (err != nil) != tt.wantErr {
191 t.Errorf("DevfileState.AddContainer() error = %v, wantErr %v", err, tt.wantErr)
192 return
193 }
194 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
195 t.Errorf("DevfileState.AddContainer() mismatch (-want +got):\n%s", diff)
196 }
197 if diff := cmp.Diff(tt.want, got); diff != "" {
198 t.Errorf("DevfileState.AddContainer() mismatch (-want +got):\n%s", diff)
199 }
200 })
201 }
202 }
203
204 func TestDevfileState_DeleteContainer(t *testing.T) {
205 type args struct {
206 name string
207 }
208 tests := []struct {
209 name string
210 state func(t *testing.T) DevfileState
211 args args
212 want DevfileContent
213 wantErr bool
214 }{
215 {
216 name: "Delete an existing container",
217 state: func(t *testing.T) DevfileState {
218 state := NewDevfileState()
219 _, err := state.AddContainer(
220 "a-name",
221 "an-image",
222 []string{"run", "command"},
223 []string{"arg1", "arg2"},
224 nil,
225 "1Gi",
226 "2Gi",
227 "100m",
228 "200m",
229 nil,
230 true,
231 false,
232 "",
233 Annotation{},
234 nil,
235 )
236 if err != nil {
237 t.Fatal(err)
238 }
239 return state
240 },
241 args: args{
242 name: "a-name",
243 },
244 want: DevfileContent{
245 Content: `metadata: {}
246 schemaVersion: 2.2.0
247 `,
248 Version: "2.2.0",
249 Commands: []Command{},
250 Containers: []Container{},
251 Images: []Image{},
252 Resources: []Resource{},
253 Volumes: []Volume{},
254 Events: Events{},
255 },
256 },
257 {
258 name: "Delete a non existing container",
259 state: func(t *testing.T) DevfileState {
260 state := NewDevfileState()
261 _, err := state.AddContainer(
262 "a-name",
263 "an-image",
264 []string{"run", "command"},
265 []string{"arg1", "arg2"},
266 nil,
267 "1Gi",
268 "2Gi",
269 "100m",
270 "200m",
271 nil,
272 true,
273 false,
274 "",
275 Annotation{},
276 nil,
277 )
278 if err != nil {
279 t.Fatal(err)
280 }
281 return state
282 },
283 args: args{
284 name: "another-name",
285 },
286 want: DevfileContent{},
287 wantErr: true,
288 },
289
290 }
291 for _, tt := range tests {
292 t.Run(tt.name, func(t *testing.T) {
293 o := tt.state(t)
294 got, err := o.DeleteContainer(tt.args.name)
295 if (err != nil) != tt.wantErr {
296 t.Errorf("DevfileState.DeleteContainer() error = %v, wantErr %v", err, tt.wantErr)
297 return
298 }
299 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
300 t.Errorf("DevfileState.DeleteContainer() mismatch (-want +got):\n%s", diff)
301 }
302 if diff := cmp.Diff(tt.want, got); diff != "" {
303 t.Errorf("DevfileState.DeleteContainer() mismatch (-want +got):\n%s", diff)
304 }
305 })
306 }
307 }
308
309 func TestDevfileState_AddImage(t *testing.T) {
310 type args struct {
311 name string
312 imageName string
313 args []string
314 buildContext string
315 rootRequired bool
316 uri string
317 autoBuild string
318 }
319 tests := []struct {
320 name string
321 state func() DevfileState
322 args args
323 want DevfileContent
324 wantErr bool
325 }{
326 {
327 name: "Add an image",
328 state: func() DevfileState {
329 return NewDevfileState()
330 },
331 args: args{
332 name: "a-name",
333 imageName: "an-image-name",
334 args: []string{"start", "command"},
335 buildContext: "path/to/context",
336 rootRequired: true,
337 uri: "an-uri",
338 autoBuild: "undefined",
339 },
340 want: DevfileContent{
341 Content: `components:
342 - image:
343 dockerfile:
344 args:
345 - start
346 - command
347 buildContext: path/to/context
348 rootRequired: true
349 uri: an-uri
350 imageName: an-image-name
351 name: a-name
352 metadata: {}
353 schemaVersion: 2.2.0
354 `,
355 Version: "2.2.0",
356 Commands: []Command{},
357 Containers: []Container{},
358 Images: []Image{
359 {
360 Name: "a-name",
361 ImageName: "an-image-name",
362 Args: []string{"start", "command"},
363 BuildContext: "path/to/context",
364 RootRequired: true,
365 Uri: "an-uri",
366 Orphan: true,
367 AutoBuild: "undefined",
368 },
369 },
370 Resources: []Resource{},
371 Volumes: []Volume{},
372 Events: Events{},
373 },
374 },
375
376 }
377 for _, tt := range tests {
378 t.Run(tt.name, func(t *testing.T) {
379 o := tt.state()
380 got, err := o.AddImage(tt.args.name, tt.args.imageName, tt.args.args, tt.args.buildContext, tt.args.rootRequired, tt.args.uri, tt.args.autoBuild)
381 if (err != nil) != tt.wantErr {
382 t.Errorf("DevfileState.AddImage() error = %v, wantErr %v", err, tt.wantErr)
383 return
384 }
385 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
386 t.Errorf("DevfileState.AddImage() mismatch (-want +got):\n%s", diff)
387 }
388 if diff := cmp.Diff(tt.want, got); diff != "" {
389 t.Errorf("DevfileState.AddImage() mismatch (-want +got):\n%s", diff)
390 }
391 })
392 }
393 }
394
395 func TestDevfileState_DeleteImage(t *testing.T) {
396 type args struct {
397 name string
398 }
399 tests := []struct {
400 name string
401 state func(t *testing.T) DevfileState
402 args args
403 want DevfileContent
404 wantErr bool
405 }{
406 {
407 name: "Delete an existing image",
408 state: func(t *testing.T) DevfileState {
409 state := NewDevfileState()
410 _, err := state.AddImage(
411 "a-name",
412 "an-image-name",
413 []string{"start", "command"},
414 "path/to/context",
415 true,
416 "an-uri",
417 "undefined",
418 )
419 if err != nil {
420 t.Fatal(err)
421 }
422 return state
423 },
424 args: args{
425 name: "a-name",
426 },
427 want: DevfileContent{
428 Content: `metadata: {}
429 schemaVersion: 2.2.0
430 `,
431 Version: "2.2.0",
432 Commands: []Command{},
433 Containers: []Container{},
434 Images: []Image{},
435 Resources: []Resource{},
436 Volumes: []Volume{},
437 Events: Events{},
438 },
439 },
440 {
441 name: "Delete a non existing image",
442 state: func(t *testing.T) DevfileState {
443 state := NewDevfileState()
444 _, err := state.AddImage(
445 "a-name",
446 "an-image-name",
447 []string{"start", "command"},
448 "path/to/context",
449 true,
450 "an-uri",
451 "undefined",
452 )
453 if err != nil {
454 t.Fatal(err)
455 }
456 return state
457 },
458 args: args{
459 name: "another-name",
460 },
461 want: DevfileContent{},
462 wantErr: true,
463 },
464
465 }
466 for _, tt := range tests {
467 t.Run(tt.name, func(t *testing.T) {
468 o := tt.state(t)
469 got, err := o.DeleteImage(tt.args.name)
470 if (err != nil) != tt.wantErr {
471 t.Errorf("DevfileState.DeleteImage() error = %v, wantErr %v", err, tt.wantErr)
472 return
473 }
474 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
475 t.Errorf("DevfileState.DeleteImage() mismatch (-want +got):\n%s", diff)
476 }
477 if diff := cmp.Diff(tt.want, got); diff != "" {
478 t.Errorf("DevfileState.DeleteImage() mismatch (-want +got):\n%s", diff)
479 }
480 })
481 }
482 }
483
484 func TestDevfileState_AddResource(t *testing.T) {
485 type args struct {
486 name string
487 inline string
488 uri string
489 deployByDefault string
490 }
491 tests := []struct {
492 name string
493 state func() DevfileState
494 args args
495 want DevfileContent
496 wantErr bool
497 }{
498 {
499 name: "Add a resource with uri",
500 state: func() DevfileState {
501 return NewDevfileState()
502 },
503 args: args{
504 name: "a-name",
505 uri: "an-uri",
506 },
507 want: DevfileContent{
508 Content: `components:
509 - kubernetes:
510 uri: an-uri
511 name: a-name
512 metadata: {}
513 schemaVersion: 2.2.0
514 `,
515 Version: "2.2.0",
516 Commands: []Command{},
517 Containers: []Container{},
518 Images: []Image{},
519 Resources: []Resource{
520 {
521 Name: "a-name",
522 Uri: "an-uri",
523 Orphan: true,
524 DeployByDefault: "undefined",
525 },
526 },
527 Volumes: []Volume{},
528 Events: Events{},
529 },
530 },
531 {
532 name: "Add an inline resource",
533 state: func() DevfileState {
534 return NewDevfileState()
535 },
536 args: args{
537 name: "a-name",
538 inline: "inline resource...",
539 },
540 want: DevfileContent{
541 Content: `components:
542 - kubernetes:
543 inlined: inline resource...
544 name: a-name
545 metadata: {}
546 schemaVersion: 2.2.0
547 `,
548 Version: "2.2.0",
549 Commands: []Command{},
550 Containers: []Container{},
551 Images: []Image{},
552 Resources: []Resource{
553 {
554 Name: "a-name",
555 Inlined: "inline resource...",
556 Orphan: true,
557 DeployByDefault: "undefined",
558 },
559 },
560 Volumes: []Volume{},
561 Events: Events{},
562 },
563 },
564
565 }
566 for _, tt := range tests {
567 t.Run(tt.name, func(t *testing.T) {
568 o := tt.state()
569 got, err := o.AddResource(tt.args.name, tt.args.inline, tt.args.uri, tt.args.deployByDefault)
570 if (err != nil) != tt.wantErr {
571 t.Errorf("DevfileState.AddResource() error = %v, wantErr %v", err, tt.wantErr)
572 return
573 }
574 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
575 t.Errorf("DevfileState.AddResource() mismatch (-want +got):\n%s", diff)
576 }
577 if diff := cmp.Diff(tt.want, got); diff != "" {
578 t.Errorf("DevfileState.AddResource() mismatch (-want +got):\n%s", diff)
579 }
580 })
581 }
582 }
583
584 func TestDevfileState_Deleteresource(t *testing.T) {
585 type args struct {
586 name string
587 }
588 tests := []struct {
589 name string
590 state func(t *testing.T) DevfileState
591 args args
592 want DevfileContent
593 wantErr bool
594 }{
595 {
596 name: "Delete an existing resource",
597 state: func(t *testing.T) DevfileState {
598 state := NewDevfileState()
599 _, err := state.AddResource(
600 "a-name",
601 "",
602 "an-uri",
603 "undefined",
604 )
605 if err != nil {
606 t.Fatal(err)
607 }
608 return state
609 },
610 args: args{
611 name: "a-name",
612 },
613 want: DevfileContent{
614 Content: `metadata: {}
615 schemaVersion: 2.2.0
616 `,
617 Version: "2.2.0",
618 Commands: []Command{},
619 Containers: []Container{},
620 Images: []Image{},
621 Resources: []Resource{},
622 Volumes: []Volume{},
623 Events: Events{},
624 },
625 },
626 {
627 name: "Delete a non existing resource",
628 state: func(t *testing.T) DevfileState {
629 state := NewDevfileState()
630 _, err := state.AddResource(
631 "a-name",
632 "",
633 "an-uri",
634 "undefined",
635 )
636 if err != nil {
637 t.Fatal(err)
638 }
639 return state
640 },
641 args: args{
642 name: "another-name",
643 },
644 want: DevfileContent{},
645 wantErr: true,
646 },
647
648 }
649 for _, tt := range tests {
650 t.Run(tt.name, func(t *testing.T) {
651 o := tt.state(t)
652 got, err := o.DeleteResource(tt.args.name)
653 if (err != nil) != tt.wantErr {
654 t.Errorf("DevfileState.DeleteResource() error = %v, wantErr %v", err, tt.wantErr)
655 return
656 }
657 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
658 t.Errorf("DevfileState.DeleteResource() mismatch (-want +got):\n%s", diff)
659 }
660 if diff := cmp.Diff(tt.want, got); diff != "" {
661 t.Errorf("DevfileState.DeleteResource() mismatch (-want +got):\n%s", diff)
662 }
663 })
664 }
665 }
666
667 func TestDevfileState_AddVolume(t *testing.T) {
668 type args struct {
669 name string
670 size string
671 ephemeral bool
672 }
673 tests := []struct {
674 name string
675 state func() DevfileState
676 args args
677 want DevfileContent
678 wantErr bool
679 }{
680 {
681 name: "Add a volume",
682 state: func() DevfileState {
683 return NewDevfileState()
684 },
685 args: args{
686 name: "a-name",
687 size: "1Gi",
688 ephemeral: true,
689 },
690 want: DevfileContent{
691 Content: `components:
692 - name: a-name
693 volume:
694 ephemeral: true
695 size: 1Gi
696 metadata: {}
697 schemaVersion: 2.2.0
698 `,
699 Version: "2.2.0",
700 Commands: []Command{},
701 Containers: []Container{},
702 Images: []Image{},
703 Resources: []Resource{},
704 Volumes: []Volume{
705 {
706 Name: "a-name",
707 Size: "1Gi",
708 Ephemeral: true,
709 },
710 },
711 Events: Events{},
712 },
713 },
714 }
715 for _, tt := range tests {
716 t.Run(tt.name, func(t *testing.T) {
717 o := tt.state()
718 got, err := o.AddVolume(tt.args.name, tt.args.ephemeral, tt.args.size)
719 if (err != nil) != tt.wantErr {
720 t.Errorf("DevfileState.AddVolume() error = %v, wantErr %v", err, tt.wantErr)
721 return
722 }
723 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
724 t.Errorf("DevfileState.AddVolume() mismatch (-want +got):\n%s", diff)
725 }
726 if diff := cmp.Diff(tt.want, got); diff != "" {
727 t.Errorf("DevfileState.AddVolume() mismatch (-want +got):\n%s", diff)
728 }
729 })
730 }
731 }
732
733 func TestDevfileState_DeleteVolume(t *testing.T) {
734 type args struct {
735 name string
736 }
737 tests := []struct {
738 name string
739 state func(t *testing.T) DevfileState
740 args args
741 want DevfileContent
742 wantErr bool
743 }{
744 {
745 name: "Delete an existing volume",
746 state: func(t *testing.T) DevfileState {
747 state := NewDevfileState()
748 _, err := state.AddVolume(
749 "a-name",
750 true,
751 "1Gi",
752 )
753 if err != nil {
754 t.Fatal(err)
755 }
756 return state
757 },
758 args: args{
759 name: "a-name",
760 },
761 want: DevfileContent{
762 Content: `metadata: {}
763 schemaVersion: 2.2.0
764 `,
765 Version: "2.2.0",
766 Commands: []Command{},
767 Containers: []Container{},
768 Images: []Image{},
769 Resources: []Resource{},
770 Volumes: []Volume{},
771 Events: Events{},
772 },
773 },
774 {
775 name: "Delete a non existing resource",
776 state: func(t *testing.T) DevfileState {
777 state := NewDevfileState()
778 _, err := state.AddVolume(
779 "a-name",
780 true,
781 "1Gi",
782 )
783 if err != nil {
784 t.Fatal(err)
785 }
786 return state
787 },
788 args: args{
789 name: "another-name",
790 },
791 want: DevfileContent{},
792 wantErr: true,
793 },
794
795 }
796 for _, tt := range tests {
797 t.Run(tt.name, func(t *testing.T) {
798 o := tt.state(t)
799 got, err := o.DeleteVolume(tt.args.name)
800 if (err != nil) != tt.wantErr {
801 t.Errorf("DevfileState.DeleteVolume() error = %v, wantErr %v", err, tt.wantErr)
802 return
803 }
804 if diff := cmp.Diff(tt.want.Content, got.Content); diff != "" {
805 t.Errorf("DevfileState.DeleteVolume() mismatch (-want +got):\n%s", diff)
806 }
807 if diff := cmp.Diff(tt.want, got); diff != "" {
808 t.Errorf("DevfileState.DeleteVolume() mismatch (-want +got):\n%s", diff)
809 }
810 })
811 }
812 }
813
View as plain text