Skip to content

Commit 5a2ea56

Browse files
committed
Update mock calls for the other func
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 85db67f commit 5a2ea56

File tree

3 files changed

+137
-79
lines changed

3 files changed

+137
-79
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +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 // indirect
10+
github.com/golang/mock v1.5.0
1111
github.com/google/go-cmp v0.5.2
1212
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
1313
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348

pkg/devfile/generator/generators_test.go

+60-59
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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"
@@ -11,9 +10,9 @@ import (
1110
"github.com/devfile/api/v2/pkg/attributes"
1211
"github.com/devfile/library/pkg/devfile/parser"
1312
"github.com/devfile/library/pkg/devfile/parser/data"
14-
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
1513
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1614
"github.com/devfile/library/pkg/testingutil"
15+
"github.com/devfile/library/pkg/util"
1716
"github.com/golang/mock/gomock"
1817

1918
corev1 "k8s.io/api/core/v1"
@@ -354,33 +353,32 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
354353
wantErr: false,
355354
},
356355
{
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,
368359
},
369360
}
370361

371362
for _, tt := range tests {
372363
t.Run(tt.name, func(t *testing.T) {
373364

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+
374380
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,
384382
}
385383

386384
containers, err := GetContainers(devObj, common.DevfileOptions{})
@@ -389,21 +387,18 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
389387
return
390388
}
391389

392-
var options common.DevfileOptions
393390
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+
399394
}
400395

401396
volumeParams := VolumeParams{
402397
Containers: containers,
403398
VolumeNameToVolumeInfo: tt.volumeNameToVolInfo,
404399
}
405400

406-
pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, options)
401+
pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, common.DevfileOptions{})
407402
if tt.wantErr == (err == nil) {
408403
t.Errorf("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v", err, tt.wantErr)
409404
} else if err == nil {
@@ -508,7 +503,7 @@ func TestGetInitContainers(t *testing.T) {
508503
},
509504
}
510505

511-
execCommands := []v1.Command{
506+
applyCommands := []v1.Command{
512507
{
513508
Id: "apply1",
514509
CommandUnion: v1.CommandUnion{
@@ -578,6 +573,15 @@ func TestGetInitContainers(t *testing.T) {
578573
},
579574
},
580575
},
576+
{
577+
name: "Simulate error condition",
578+
eventCommands: []string{
579+
"apply1",
580+
"apply3",
581+
"apply2",
582+
},
583+
wantErr: true,
584+
},
581585
{
582586
name: "Long Container Name",
583587
eventCommands: []string{
@@ -594,44 +598,41 @@ func TestGetInitContainers(t *testing.T) {
594598
for _, tt := range tests {
595599
t.Run(tt.name, func(t *testing.T) {
596600

601+
preStartEvents := v1.Events{
602+
DevWorkspaceEvents: v1.DevWorkspaceEvents{
603+
PreStart: tt.eventCommands,
604+
},
605+
}
606+
597607
if tt.longName {
598608
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()
600625
}
601626

602627
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,
630629
}
631630

632631
initContainers, err := GetInitContainers(devObj)
633632
if (err != nil) != tt.wantErr {
634633
t.Errorf("TestGetInitContainers() error = %v, wantErr %v", err, tt.wantErr)
634+
} else if err != nil {
635+
return
635636
}
636637

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

pkg/devfile/generator/utils_test.go

+76-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,20 @@ 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+
// set up the mock data
766+
if len(tt.filterOptions.Filter) == 0 {
767+
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
768+
} else {
769+
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes()
770+
}
771+
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()
772+
740773
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-
},
774+
Data: mockDevfileData,
750775
}
751776

752777
serviceSpec, err := getServiceSpec(devObj, tt.labels, tt.filterOptions)
@@ -781,6 +806,7 @@ func TestGetPortExposure(t *testing.T) {
781806
tests := []struct {
782807
name string
783808
containerComponents []v1.Component
809+
filteredComponents []v1.Component
784810
filterOptions common.DevfileOptions
785811
wantMap map[int]v1.EndpointExposure
786812
wantErr bool
@@ -1020,6 +1046,33 @@ func TestGetPortExposure(t *testing.T) {
10201046
},
10211047
},
10221048
},
1049+
filteredComponents: []v1.Component{
1050+
{
1051+
Name: "testcontainer1",
1052+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
1053+
"firstString": "firstStringValue",
1054+
"thirdString": "thirdStringValue",
1055+
}),
1056+
ComponentUnion: v1.ComponentUnion{
1057+
Container: &v1.ContainerComponent{
1058+
Container: v1.Container{
1059+
Image: "image",
1060+
},
1061+
Endpoints: []v1.Endpoint{
1062+
{
1063+
Name: urlName,
1064+
TargetPort: 8080,
1065+
},
1066+
{
1067+
Name: urlName,
1068+
TargetPort: 3000,
1069+
Exposure: v1.NoneEndpointExposure,
1070+
},
1071+
},
1072+
},
1073+
},
1074+
},
1075+
},
10231076
filterOptions: common.DevfileOptions{
10241077
Filter: map[string]interface{}{
10251078
"firstString": "firstStringValue",
@@ -1056,6 +1109,7 @@ func TestGetPortExposure(t *testing.T) {
10561109
},
10571110
},
10581111
},
1112+
filteredComponents: nil,
10591113
filterOptions: common.DevfileOptions{
10601114
Filter: map[string]interface{}{
10611115
"firstStringWrong": "firstStringValue",
@@ -1066,16 +1120,19 @@ func TestGetPortExposure(t *testing.T) {
10661120
}
10671121
for _, tt := range tests {
10681122
t.Run(tt.name, func(t *testing.T) {
1123+
1124+
ctrl := gomock.NewController(t)
1125+
defer ctrl.Finish()
1126+
mockDevfileData := data.NewMockDevfileData(ctrl)
1127+
1128+
// set up the mock data
1129+
if len(tt.filterOptions.Filter) == 0 {
1130+
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
1131+
} else {
1132+
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes()
1133+
}
10691134
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-
},
1135+
Data: mockDevfileData,
10791136
}
10801137

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

0 commit comments

Comments
 (0)