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"
@@ -11,9 +10,9 @@ import (
11
10
"github.com/devfile/api/v2/pkg/attributes"
12
11
"github.com/devfile/library/pkg/devfile/parser"
13
12
"github.com/devfile/library/pkg/devfile/parser/data"
14
- v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
15
13
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
16
14
"github.com/devfile/library/pkg/testingutil"
15
+ "github.com/devfile/library/pkg/util"
17
16
"github.com/golang/mock/gomock"
18
17
19
18
corev1 "k8s.io/api/core/v1"
@@ -354,33 +353,32 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
354
353
wantErr : false ,
355
354
},
356
355
{
357
- name : "Invalid case" ,
358
- components : []v1.Component {
359
- {
360
- Name : "container1" ,
361
- Attributes : attributes.Attributes {}.FromStringMap (map [string ]string {
362
- "firstString" : "firstStringValue" ,
363
- }),
364
- ComponentUnion : v1.ComponentUnion {},
365
- },
366
- },
367
- wantErr : true ,
356
+ name : "Invalid case simulating no container components" ,
357
+ components : nil ,
358
+ wantErr : true ,
368
359
},
369
360
}
370
361
371
362
for _ , tt := range tests {
372
363
t .Run (tt .name , func (t * testing.T ) {
373
364
365
+ ctrl := gomock .NewController (t )
366
+ defer ctrl .Finish ()
367
+ mockDevfileData := data .NewMockDevfileData (ctrl )
368
+
369
+ // set up the mock data
370
+ if tt .wantErr {
371
+ // if we have an error, just call the mock GetDevfileContainerComponents once for GetContainers() and a different
372
+ // one for GetVolumesAndVolumeMounts() below to simulate err from that function
373
+ mockDevfileData .EXPECT ().GetDevfileContainerComponents (common.DevfileOptions {}).Return (tt .components , nil ).Times (1 )
374
+ } else {
375
+ // no error, use this below mock for all the future GetDevfileContainerComponents in the test
376
+ mockDevfileData .EXPECT ().GetDevfileContainerComponents (common.DevfileOptions {}).Return (tt .components , nil ).AnyTimes ()
377
+ }
378
+ mockDevfileData .EXPECT ().GetProjects (common.DevfileOptions {}).Return (nil , nil ).AnyTimes ()
379
+
374
380
devObj := parser.DevfileObj {
375
- Data : & v2.DevfileV2 {
376
- Devfile : v1.Devfile {
377
- DevWorkspaceTemplateSpec : v1.DevWorkspaceTemplateSpec {
378
- DevWorkspaceTemplateSpecContent : v1.DevWorkspaceTemplateSpecContent {
379
- Components : tt .components ,
380
- },
381
- },
382
- },
383
- },
381
+ Data : mockDevfileData ,
384
382
}
385
383
386
384
containers , err := GetContainers (devObj , common.DevfileOptions {})
@@ -389,21 +387,18 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
389
387
return
390
388
}
391
389
392
- var options common.DevfileOptions
393
390
if tt .wantErr {
394
- options = common.DevfileOptions {
395
- Filter : map [string ]interface {}{
396
- "firstString" : "firstStringValue" ,
397
- },
398
- }
391
+ // simulate error condition
392
+ mockDevfileData .EXPECT ().GetDevfileContainerComponents (common.DevfileOptions {}).Return (nil , fmt .Errorf ("mock error" )).Times (1 )
393
+
399
394
}
400
395
401
396
volumeParams := VolumeParams {
402
397
Containers : containers ,
403
398
VolumeNameToVolumeInfo : tt .volumeNameToVolInfo ,
404
399
}
405
400
406
- pvcVols , err := GetVolumesAndVolumeMounts (devObj , volumeParams , options )
401
+ pvcVols , err := GetVolumesAndVolumeMounts (devObj , volumeParams , common. DevfileOptions {} )
407
402
if tt .wantErr == (err == nil ) {
408
403
t .Errorf ("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v" , err , tt .wantErr )
409
404
} else if err == nil {
@@ -508,7 +503,7 @@ func TestGetInitContainers(t *testing.T) {
508
503
},
509
504
}
510
505
511
- execCommands := []v1.Command {
506
+ applyCommands := []v1.Command {
512
507
{
513
508
Id : "apply1" ,
514
509
CommandUnion : v1.CommandUnion {
@@ -578,6 +573,15 @@ func TestGetInitContainers(t *testing.T) {
578
573
},
579
574
},
580
575
},
576
+ {
577
+ name : "Simulate error condition" ,
578
+ eventCommands : []string {
579
+ "apply1" ,
580
+ "apply3" ,
581
+ "apply2" ,
582
+ },
583
+ wantErr : true ,
584
+ },
581
585
{
582
586
name : "Long Container Name" ,
583
587
eventCommands : []string {
@@ -594,44 +598,41 @@ func TestGetInitContainers(t *testing.T) {
594
598
for _ , tt := range tests {
595
599
t .Run (tt .name , func (t * testing.T ) {
596
600
601
+ preStartEvents := v1.Events {
602
+ DevWorkspaceEvents : v1.DevWorkspaceEvents {
603
+ PreStart : tt .eventCommands ,
604
+ },
605
+ }
606
+
597
607
if tt .longName {
598
608
containers [0 ].Name = longContainerName
599
- execCommands [1 ].Apply .Component = longContainerName
609
+ applyCommands [1 ].Apply .Component = longContainerName
610
+ }
611
+
612
+ ctrl := gomock .NewController (t )
613
+ defer ctrl .Finish ()
614
+ mockDevfileData := data .NewMockDevfileData (ctrl )
615
+
616
+ // set up the mock data
617
+ mockDevfileData .EXPECT ().GetDevfileContainerComponents (common.DevfileOptions {}).Return (containers , nil ).AnyTimes ()
618
+ mockDevfileData .EXPECT ().GetProjects (common.DevfileOptions {}).Return (nil , nil ).AnyTimes ()
619
+ mockDevfileData .EXPECT ().GetEvents ().Return (preStartEvents ).AnyTimes ()
620
+
621
+ if tt .wantErr {
622
+ mockDevfileData .EXPECT ().GetCommands (common.DevfileOptions {}).Return (nil , fmt .Errorf ("mock error" )).AnyTimes ()
623
+ } else {
624
+ mockDevfileData .EXPECT ().GetCommands (common.DevfileOptions {}).Return (append (applyCommands , compCommands ... ), nil ).AnyTimes ()
600
625
}
601
626
602
627
devObj := parser.DevfileObj {
603
- Data : func () data.DevfileData {
604
- devfileData , err := data .NewDevfileData (string (data .APISchemaVersion210 ))
605
- if err != nil {
606
- t .Error (err )
607
- }
608
- err = devfileData .AddComponents (containers )
609
- if err != nil {
610
- t .Error (err )
611
- }
612
- err = devfileData .AddCommands (execCommands )
613
- if err != nil {
614
- t .Error (err )
615
- }
616
- err = devfileData .AddCommands (compCommands )
617
- if err != nil {
618
- t .Error (err )
619
- }
620
- err = devfileData .AddEvents (v1.Events {
621
- DevWorkspaceEvents : v1.DevWorkspaceEvents {
622
- PreStart : tt .eventCommands ,
623
- },
624
- })
625
- if err != nil {
626
- t .Error (err )
627
- }
628
- return devfileData
629
- }(),
628
+ Data : mockDevfileData ,
630
629
}
631
630
632
631
initContainers , err := GetInitContainers (devObj )
633
632
if (err != nil ) != tt .wantErr {
634
633
t .Errorf ("TestGetInitContainers() error = %v, wantErr %v" , err , tt .wantErr )
634
+ } else if err != nil {
635
+ return
635
636
}
636
637
637
638
if len (tt .wantInitContainer ) != len (initContainers ) {
0 commit comments