Skip to content

Commit 179c407

Browse files
authoredJun 10, 2021
Merge pull request #93 from maysunfaisal/383-1
Add mock func for DevfileData interface
2 parents 4b764c8 + c0ce883 commit 179c407

File tree

8 files changed

+770
-101
lines changed

8 files changed

+770
-101
lines changed
 

‎go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/fatih/color v1.7.0
88
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
99
github.com/gobwas/glob v0.2.3
10+
github.com/golang/mock v1.5.0
1011
github.com/google/go-cmp v0.5.2
1112
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
1213
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348

‎go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4er
177177
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
178178
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
179179
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
180+
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
181+
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
180182
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
181183
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
182184
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

‎pkg/devfile/generator/generators.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generator
22

33
import (
44
"fmt"
5+
56
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
67
"github.com/devfile/library/pkg/devfile/parser"
78
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
@@ -56,7 +57,11 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
5657
// GetContainers iterates through the devfile components and returns a slice of the corresponding containers
5758
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
5859
var containers []corev1.Container
59-
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
60+
61+
options.ComponentOptions = common.ComponentOptions{
62+
ComponentType: v1.ContainerComponentType,
63+
}
64+
containerComponents, err := devfileObj.Data.GetComponents(options)
6065
if err != nil {
6166
return nil, err
6267
}
@@ -357,7 +362,10 @@ type VolumeParams struct {
357362
// GetVolumesAndVolumeMounts gets the PVC volumes and updates the containers with the volume mounts.
358363
func GetVolumesAndVolumeMounts(devfileObj parser.DevfileObj, volumeParams VolumeParams, options common.DevfileOptions) ([]corev1.Volume, error) {
359364

360-
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
365+
options.ComponentOptions = common.ComponentOptions{
366+
ComponentType: v1.ContainerComponentType,
367+
}
368+
containerComponents, err := devfileObj.Data.GetComponents(options)
361369
if err != nil {
362370
return nil, err
363371
}

‎pkg/devfile/generator/generators_test.go

+112-79
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package generator
22

33
import (
4-
"github.com/devfile/library/pkg/devfile/parser/data"
5-
"github.com/devfile/library/pkg/util"
4+
"fmt"
65
"reflect"
76
"strings"
87
"testing"
98

109
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
1110
"github.com/devfile/api/v2/pkg/attributes"
1211
"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"
1413
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1514
"github.com/devfile/library/pkg/testingutil"
15+
"github.com/devfile/library/pkg/util"
16+
"github.com/golang/mock/gomock"
1617

1718
corev1 "k8s.io/api/core/v1"
1819
)
@@ -30,14 +31,16 @@ func TestGetContainers(t *testing.T) {
3031
trueMountSources := true
3132
falseMountSources := false
3233

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+
},
4144
},
4245
},
4346
},
@@ -47,6 +50,7 @@ func TestGetContainers(t *testing.T) {
4750
tests := []struct {
4851
name string
4952
containerComponents []v1.Component
53+
filteredComponents []v1.Component
5054
filterOptions common.DevfileOptions
5155
wantContainerName string
5256
wantContainerImage string
@@ -175,6 +179,23 @@ func TestGetContainers(t *testing.T) {
175179
},
176180
wantContainerName: containerNames[1],
177181
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+
},
178199
filterOptions: common.DevfileOptions{
179200
Filter: map[string]interface{}{
180201
"firstString": "firstStringValue",
@@ -185,19 +206,25 @@ func TestGetContainers(t *testing.T) {
185206
for _, tt := range tests {
186207
t.Run(tt.name, func(t *testing.T) {
187208

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+
188226
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,
201228
}
202229

203230
containers, err := GetContainers(devObj, tt.filterOptions)
@@ -364,33 +391,31 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
364391
wantErr: false,
365392
},
366393
{
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,
378397
},
379398
}
380399

381400
for _, tt := range tests {
382401
t.Run(tt.name, func(t *testing.T) {
383402

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,
393410
},
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,
394419
}
395420

396421
containers, err := GetContainers(devObj, common.DevfileOptions{})
@@ -399,21 +424,18 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
399424
return
400425
}
401426

402-
var options common.DevfileOptions
403427
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+
409431
}
410432

411433
volumeParams := VolumeParams{
412434
Containers: containers,
413435
VolumeNameToVolumeInfo: tt.volumeNameToVolInfo,
414436
}
415437

416-
pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, options)
438+
pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, common.DevfileOptions{})
417439
if tt.wantErr == (err == nil) {
418440
t.Errorf("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v", err, tt.wantErr)
419441
} else if err == nil {
@@ -518,7 +540,7 @@ func TestGetInitContainers(t *testing.T) {
518540
},
519541
}
520542

521-
execCommands := []v1.Command{
543+
applyCommands := []v1.Command{
522544
{
523545
Id: "apply1",
524546
CommandUnion: v1.CommandUnion{
@@ -588,6 +610,15 @@ func TestGetInitContainers(t *testing.T) {
588610
},
589611
},
590612
},
613+
{
614+
name: "Simulate error condition",
615+
eventCommands: []string{
616+
"apply1",
617+
"apply3",
618+
"apply2",
619+
},
620+
wantErr: true,
621+
},
591622
{
592623
name: "Long Container Name",
593624
eventCommands: []string{
@@ -604,44 +635,46 @@ func TestGetInitContainers(t *testing.T) {
604635
for _, tt := range tests {
605636
t.Run(tt.name, func(t *testing.T) {
606637

638+
preStartEvents := v1.Events{
639+
DevWorkspaceEvents: v1.DevWorkspaceEvents{
640+
PreStart: tt.eventCommands,
641+
},
642+
}
643+
607644
if tt.longName {
608645
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()
610667
}
611668

612669
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,
640671
}
641672

642673
initContainers, err := GetInitContainers(devObj)
643674
if (err != nil) != tt.wantErr {
644675
t.Errorf("TestGetInitContainers() error = %v, wantErr %v", err, tt.wantErr)
676+
} else if err != nil {
677+
return
645678
}
646679

647680
if len(tt.wantInitContainer) != len(initContainers) {

‎pkg/devfile/generator/utils.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,10 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
254254
// exposure level: public > internal > none
255255
func getPortExposure(devfileObj parser.DevfileObj, options common.DevfileOptions) (map[int]v1.EndpointExposure, error) {
256256
portExposureMap := make(map[int]v1.EndpointExposure)
257-
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
257+
options.ComponentOptions = common.ComponentOptions{
258+
ComponentType: v1.ContainerComponentType,
259+
}
260+
containerComponents, err := devfileObj.Data.GetComponents(options)
258261
if err != nil {
259262
return portExposureMap, err
260263
}

‎pkg/devfile/generator/utils_test.go

+86-19
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88

99
"github.com/devfile/api/v2/pkg/attributes"
1010
"github.com/devfile/library/pkg/devfile/parser"
11-
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
11+
"github.com/devfile/library/pkg/devfile/parser/data"
1212
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1313
"github.com/devfile/library/pkg/testingutil"
14+
"github.com/golang/mock/gomock"
1415
buildv1 "github.com/openshift/api/build/v1"
1516

1617
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
@@ -608,6 +609,7 @@ func TestGetServiceSpec(t *testing.T) {
608609
tests := []struct {
609610
name string
610611
containerComponents []v1.Component
612+
filteredComponents []v1.Component
611613
labels map[string]string
612614
filterOptions common.DevfileOptions
613615
wantPorts []corev1.ServicePort
@@ -727,6 +729,25 @@ func TestGetServiceSpec(t *testing.T) {
727729
},
728730
},
729731
wantErr: false,
732+
filteredComponents: []v1.Component{
733+
{
734+
Name: "testcontainer2",
735+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
736+
"firstString": "firstStringValue",
737+
"thirdString": "thirdStringValue",
738+
}),
739+
ComponentUnion: v1.ComponentUnion{
740+
Container: &v1.ContainerComponent{
741+
Endpoints: []v1.Endpoint{
742+
{
743+
Name: endpointNames[2],
744+
TargetPort: 9090,
745+
},
746+
},
747+
},
748+
},
749+
},
750+
},
730751
filterOptions: common.DevfileOptions{
731752
Filter: map[string]interface{}{
732753
"firstString": "firstStringValue",
@@ -737,16 +758,25 @@ func TestGetServiceSpec(t *testing.T) {
737758
for _, tt := range tests {
738759
t.Run(tt.name, func(t *testing.T) {
739760

761+
ctrl := gomock.NewController(t)
762+
defer ctrl.Finish()
763+
mockDevfileData := data.NewMockDevfileData(ctrl)
764+
765+
tt.filterOptions.ComponentOptions = common.ComponentOptions{
766+
ComponentType: v1.ContainerComponentType,
767+
}
768+
mockGetComponents := mockDevfileData.EXPECT().GetComponents(tt.filterOptions)
769+
770+
// set up the mock data
771+
if len(tt.filterOptions.Filter) == 0 {
772+
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
773+
} else {
774+
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
775+
}
776+
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()
777+
740778
devObj := parser.DevfileObj{
741-
Data: &v2.DevfileV2{
742-
Devfile: v1.Devfile{
743-
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
744-
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
745-
Components: tt.containerComponents,
746-
},
747-
},
748-
},
749-
},
779+
Data: mockDevfileData,
750780
}
751781

752782
serviceSpec, err := getServiceSpec(devObj, tt.labels, tt.filterOptions)
@@ -781,6 +811,7 @@ func TestGetPortExposure(t *testing.T) {
781811
tests := []struct {
782812
name string
783813
containerComponents []v1.Component
814+
filteredComponents []v1.Component
784815
filterOptions common.DevfileOptions
785816
wantMap map[int]v1.EndpointExposure
786817
wantErr bool
@@ -1020,6 +1051,33 @@ func TestGetPortExposure(t *testing.T) {
10201051
},
10211052
},
10221053
},
1054+
filteredComponents: []v1.Component{
1055+
{
1056+
Name: "testcontainer1",
1057+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
1058+
"firstString": "firstStringValue",
1059+
"thirdString": "thirdStringValue",
1060+
}),
1061+
ComponentUnion: v1.ComponentUnion{
1062+
Container: &v1.ContainerComponent{
1063+
Container: v1.Container{
1064+
Image: "image",
1065+
},
1066+
Endpoints: []v1.Endpoint{
1067+
{
1068+
Name: urlName,
1069+
TargetPort: 8080,
1070+
},
1071+
{
1072+
Name: urlName,
1073+
TargetPort: 3000,
1074+
Exposure: v1.NoneEndpointExposure,
1075+
},
1076+
},
1077+
},
1078+
},
1079+
},
1080+
},
10231081
filterOptions: common.DevfileOptions{
10241082
Filter: map[string]interface{}{
10251083
"firstString": "firstStringValue",
@@ -1056,6 +1114,7 @@ func TestGetPortExposure(t *testing.T) {
10561114
},
10571115
},
10581116
},
1117+
filteredComponents: nil,
10591118
filterOptions: common.DevfileOptions{
10601119
Filter: map[string]interface{}{
10611120
"firstStringWrong": "firstStringValue",
@@ -1066,16 +1125,24 @@ func TestGetPortExposure(t *testing.T) {
10661125
}
10671126
for _, tt := range tests {
10681127
t.Run(tt.name, func(t *testing.T) {
1128+
1129+
ctrl := gomock.NewController(t)
1130+
defer ctrl.Finish()
1131+
mockDevfileData := data.NewMockDevfileData(ctrl)
1132+
1133+
tt.filterOptions.ComponentOptions = common.ComponentOptions{
1134+
ComponentType: v1.ContainerComponentType,
1135+
}
1136+
mockGetComponents := mockDevfileData.EXPECT().GetComponents(tt.filterOptions)
1137+
1138+
// set up the mock data
1139+
if len(tt.filterOptions.Filter) == 0 {
1140+
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
1141+
} else {
1142+
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
1143+
}
10691144
devObj := parser.DevfileObj{
1070-
Data: &v2.DevfileV2{
1071-
Devfile: v1.Devfile{
1072-
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
1073-
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
1074-
Components: tt.containerComponents,
1075-
},
1076-
},
1077-
},
1078-
},
1145+
Data: mockDevfileData,
10791146
}
10801147

10811148
mapCreated, err := getPortExposure(devObj, tt.filterOptions)

‎pkg/devfile/parser/data/interface.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
88
)
99

10+
// Generate mock interfaces for DevfileData by executing the following cmd in pkg/devfile/parser/data
11+
// mockgen -package=data -source=interface.go DevfileData > /tmp/mock_interface.go ; cp /tmp/mock_interface.go ./mock_interface.go
12+
1013
// DevfileData is an interface that defines functions for Devfile data operations
1114
type DevfileData interface {
1215

‎pkg/devfile/parser/data/mock_interface.go

+552
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.