1
1
package generator
2
2
3
3
import (
4
- "github.com/devfile/library/pkg/devfile/parser/data"
5
- "github.com/devfile/library/pkg/util"
4
+ "fmt"
6
5
"reflect"
7
6
"strings"
8
7
"testing"
9
8
10
9
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
11
10
"github.com/devfile/api/v2/pkg/attributes"
12
11
"github.com/devfile/library/pkg/devfile/parser"
13
- v2 "github.com/devfile/library/pkg/devfile/parser/data/v2 "
12
+ "github.com/devfile/library/pkg/devfile/parser/data"
14
13
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
15
14
"github.com/devfile/library/pkg/testingutil"
15
+ "github.com/devfile/library/pkg/util"
16
+ "github.com/golang/mock/gomock"
16
17
17
18
corev1 "k8s.io/api/core/v1"
18
19
)
@@ -30,14 +31,16 @@ func TestGetContainers(t *testing.T) {
30
31
trueMountSources := true
31
32
falseMountSources := false
32
33
33
- project := v1.Project {
34
- ClonePath : "test-project/" ,
35
- Name : "project0" ,
36
- ProjectSource : v1.ProjectSource {
37
- Git : & v1.GitProjectSource {
38
- GitLikeProjectSource : v1.GitLikeProjectSource {
39
- Remotes : map [string ]string {
40
- "origin" : "repo" ,
34
+ projects := []v1.Project {
35
+ {
36
+ ClonePath : "test-project/" ,
37
+ Name : "project0" ,
38
+ ProjectSource : v1.ProjectSource {
39
+ Git : & v1.GitProjectSource {
40
+ GitLikeProjectSource : v1.GitLikeProjectSource {
41
+ Remotes : map [string ]string {
42
+ "origin" : "repo" ,
43
+ },
41
44
},
42
45
},
43
46
},
@@ -47,6 +50,7 @@ func TestGetContainers(t *testing.T) {
47
50
tests := []struct {
48
51
name string
49
52
containerComponents []v1.Component
53
+ filteredComponents []v1.Component
50
54
filterOptions common.DevfileOptions
51
55
wantContainerName string
52
56
wantContainerImage string
@@ -175,6 +179,23 @@ func TestGetContainers(t *testing.T) {
175
179
},
176
180
wantContainerName : containerNames [1 ],
177
181
wantContainerImage : containerImages [0 ],
182
+ filteredComponents : []v1.Component {
183
+ {
184
+ Name : containerNames [1 ],
185
+ Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
186
+ "firstString" : "firstStringValue" ,
187
+ "thirdString" : "thirdStringValue" ,
188
+ }),
189
+ ComponentUnion : v1.ComponentUnion {
190
+ Container : & v1.ContainerComponent {
191
+ Container : v1.Container {
192
+ Image : containerImages [0 ],
193
+ MountSources : & falseMountSources ,
194
+ },
195
+ },
196
+ },
197
+ },
198
+ },
178
199
filterOptions : common.DevfileOptions {
179
200
Filter : map [string ]interface {}{
180
201
"firstString" : "firstStringValue" ,
@@ -185,19 +206,25 @@ func TestGetContainers(t *testing.T) {
185
206
for _ , tt := range tests {
186
207
t .Run (tt .name , func (t * testing.T ) {
187
208
209
+ ctrl := gomock .NewController (t )
210
+ defer ctrl .Finish ()
211
+ mockDevfileData := data .NewMockDevfileData (ctrl )
212
+
213
+ tt .filterOptions .ComponentOptions = common.ComponentOptions {
214
+ ComponentType : v1 .ContainerComponentType ,
215
+ }
216
+ mockGetComponents := mockDevfileData .EXPECT ().GetComponents (tt .filterOptions )
217
+
218
+ // set up the mock data
219
+ if len (tt .filterOptions .Filter ) == 0 {
220
+ mockGetComponents .Return (tt .containerComponents , nil ).AnyTimes ()
221
+ } else {
222
+ mockGetComponents .Return (tt .filteredComponents , nil ).AnyTimes ()
223
+ }
224
+ mockDevfileData .EXPECT ().GetProjects (common.DevfileOptions {}).Return (projects , nil ).AnyTimes ()
225
+
188
226
devObj := parser.DevfileObj {
189
- Data : & v2.DevfileV2 {
190
- Devfile : v1.Devfile {
191
- DevWorkspaceTemplateSpec : v1.DevWorkspaceTemplateSpec {
192
- DevWorkspaceTemplateSpecContent : v1.DevWorkspaceTemplateSpecContent {
193
- Components : tt .containerComponents ,
194
- Projects : []v1.Project {
195
- project ,
196
- },
197
- },
198
- },
199
- },
200
- },
227
+ Data : mockDevfileData ,
201
228
}
202
229
203
230
containers , err := GetContainers (devObj , tt .filterOptions )
@@ -364,33 +391,31 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
364
391
wantErr : false ,
365
392
},
366
393
{
367
- name : "Invalid case" ,
368
- components : []v1.Component {
369
- {
370
- Name : "container1" ,
371
- Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
372
- "firstString" : "firstStringValue" ,
373
- }),
374
- ComponentUnion : v1.ComponentUnion {},
375
- },
376
- },
377
- wantErr : true ,
394
+ name : "Invalid case simulating no container components" ,
395
+ components : nil ,
396
+ wantErr : true ,
378
397
},
379
398
}
380
399
381
400
for _ , tt := range tests {
382
401
t .Run (tt .name , func (t * testing.T ) {
383
402
384
- devObj := parser.DevfileObj {
385
- Data : & v2.DevfileV2 {
386
- Devfile : v1.Devfile {
387
- DevWorkspaceTemplateSpec : v1.DevWorkspaceTemplateSpec {
388
- DevWorkspaceTemplateSpecContent : v1.DevWorkspaceTemplateSpecContent {
389
- Components : tt .components ,
390
- },
391
- },
392
- },
403
+ ctrl := gomock .NewController (t )
404
+ defer ctrl .Finish ()
405
+ mockDevfileData := data .NewMockDevfileData (ctrl )
406
+
407
+ mockGetComponents := mockDevfileData .EXPECT ().GetComponents (common.DevfileOptions {
408
+ ComponentOptions : common.ComponentOptions {
409
+ ComponentType : v1 .ContainerComponentType ,
393
410
},
411
+ })
412
+
413
+ // set up the mock data
414
+ mockGetComponents .Return (tt .components , nil ).AnyTimes ()
415
+ mockDevfileData .EXPECT ().GetProjects (common.DevfileOptions {}).Return (nil , nil ).AnyTimes ()
416
+
417
+ devObj := parser.DevfileObj {
418
+ Data : mockDevfileData ,
394
419
}
395
420
396
421
containers , err := GetContainers (devObj , common.DevfileOptions {})
@@ -399,21 +424,18 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
399
424
return
400
425
}
401
426
402
- var options common.DevfileOptions
403
427
if tt .wantErr {
404
- options = common.DevfileOptions {
405
- Filter : map [string ]interface {}{
406
- "firstString" : "firstStringValue" ,
407
- },
408
- }
428
+ // simulate error condition
429
+ mockGetComponents .Return (nil , fmt .Errorf ("mock error" ))
430
+
409
431
}
410
432
411
433
volumeParams := VolumeParams {
412
434
Containers : containers ,
413
435
VolumeNameToVolumeInfo : tt .volumeNameToVolInfo ,
414
436
}
415
437
416
- pvcVols , err := GetVolumesAndVolumeMounts (devObj , volumeParams , options )
438
+ pvcVols , err := GetVolumesAndVolumeMounts (devObj , volumeParams , common. DevfileOptions {} )
417
439
if tt .wantErr == (err == nil ) {
418
440
t .Errorf ("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v" , err , tt .wantErr )
419
441
} else if err == nil {
@@ -518,7 +540,7 @@ func TestGetInitContainers(t *testing.T) {
518
540
},
519
541
}
520
542
521
- execCommands := []v1.Command {
543
+ applyCommands := []v1.Command {
522
544
{
523
545
Id : "apply1" ,
524
546
CommandUnion : v1.CommandUnion {
@@ -588,6 +610,15 @@ func TestGetInitContainers(t *testing.T) {
588
610
},
589
611
},
590
612
},
613
+ {
614
+ name : "Simulate error condition" ,
615
+ eventCommands : []string {
616
+ "apply1" ,
617
+ "apply3" ,
618
+ "apply2" ,
619
+ },
620
+ wantErr : true ,
621
+ },
591
622
{
592
623
name : "Long Container Name" ,
593
624
eventCommands : []string {
@@ -604,44 +635,46 @@ func TestGetInitContainers(t *testing.T) {
604
635
for _ , tt := range tests {
605
636
t .Run (tt .name , func (t * testing.T ) {
606
637
638
+ preStartEvents := v1.Events {
639
+ DevWorkspaceEvents : v1.DevWorkspaceEvents {
640
+ PreStart : tt .eventCommands ,
641
+ },
642
+ }
643
+
607
644
if tt .longName {
608
645
containers [0 ].Name = longContainerName
609
- execCommands [1 ].Apply .Component = longContainerName
646
+ applyCommands [1 ].Apply .Component = longContainerName
647
+ }
648
+
649
+ ctrl := gomock .NewController (t )
650
+ defer ctrl .Finish ()
651
+ mockDevfileData := data .NewMockDevfileData (ctrl )
652
+
653
+ mockGetCommands := mockDevfileData .EXPECT ().GetCommands (common.DevfileOptions {})
654
+
655
+ // set up the mock data
656
+ mockDevfileData .EXPECT ().GetComponents (common.DevfileOptions {
657
+ ComponentOptions : common.ComponentOptions {
658
+ ComponentType : v1 .ContainerComponentType ,
659
+ },
660
+ }).Return (containers , nil ).AnyTimes ()
661
+ mockDevfileData .EXPECT ().GetProjects (common.DevfileOptions {}).Return (nil , nil ).AnyTimes ()
662
+ mockDevfileData .EXPECT ().GetEvents ().Return (preStartEvents ).AnyTimes ()
663
+ mockGetCommands .Return (append (applyCommands , compCommands ... ), nil ).AnyTimes ()
664
+
665
+ if tt .wantErr {
666
+ mockGetCommands .Return (nil , fmt .Errorf ("mock error" )).AnyTimes ()
610
667
}
611
668
612
669
devObj := parser.DevfileObj {
613
- Data : func () data.DevfileData {
614
- devfileData , err := data .NewDevfileData (string (data .APISchemaVersion210 ))
615
- if err != nil {
616
- t .Error (err )
617
- }
618
- err = devfileData .AddComponents (containers )
619
- if err != nil {
620
- t .Error (err )
621
- }
622
- err = devfileData .AddCommands (execCommands )
623
- if err != nil {
624
- t .Error (err )
625
- }
626
- err = devfileData .AddCommands (compCommands )
627
- if err != nil {
628
- t .Error (err )
629
- }
630
- err = devfileData .AddEvents (v1.Events {
631
- DevWorkspaceEvents : v1.DevWorkspaceEvents {
632
- PreStart : tt .eventCommands ,
633
- },
634
- })
635
- if err != nil {
636
- t .Error (err )
637
- }
638
- return devfileData
639
- }(),
670
+ Data : mockDevfileData ,
640
671
}
641
672
642
673
initContainers , err := GetInitContainers (devObj )
643
674
if (err != nil ) != tt .wantErr {
644
675
t .Errorf ("TestGetInitContainers() error = %v, wantErr %v" , err , tt .wantErr )
676
+ } else if err != nil {
677
+ return
645
678
}
646
679
647
680
if len (tt .wantInitContainer ) != len (initContainers ) {
0 commit comments