Skip to content

Commit

Permalink
PR feedback - mock and update dep func usage
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <[email protected]>
  • Loading branch information
maysunfaisal committed Jun 10, 2021
1 parent beb20b6 commit c0ce883
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
12 changes: 10 additions & 2 deletions pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"fmt"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
Expand Down Expand Up @@ -56,7 +57,11 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
// GetContainers iterates through the devfile components and returns a slice of the corresponding containers
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
var containers []corev1.Container
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)

options.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
containerComponents, err := devfileObj.Data.GetComponents(options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -357,7 +362,10 @@ type VolumeParams struct {
// GetVolumesAndVolumeMounts gets the PVC volumes and updates the containers with the volume mounts.
func GetVolumesAndVolumeMounts(devfileObj parser.DevfileObj, volumeParams VolumeParams, options common.DevfileOptions) ([]corev1.Volume, error) {

containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
options.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
containerComponents, err := devfileObj.Data.GetComponents(options)
if err != nil {
return nil, err
}
Expand Down
39 changes: 24 additions & 15 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@ func TestGetContainers(t *testing.T) {
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

tt.filterOptions.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
mockGetComponents := mockDevfileData.EXPECT().GetComponents(tt.filterOptions)

// set up the mock data
if len(tt.filterOptions.Filter) == 0 {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
} else {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes()
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
}
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(projects, nil).AnyTimes()

Expand Down Expand Up @@ -399,15 +404,14 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

mockGetComponents := mockDevfileData.EXPECT().GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
},
})

// set up the mock data
if tt.wantErr {
// if we have an error, just call the mock GetDevfileContainerComponents once for GetContainers() and a different
// one for GetVolumesAndVolumeMounts() below to simulate err from that function
mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(tt.components, nil).Times(1)
} else {
// no error, use this below mock for all the future GetDevfileContainerComponents in the test
mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(tt.components, nil).AnyTimes()
}
mockGetComponents.Return(tt.components, nil).AnyTimes()
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()

devObj := parser.DevfileObj{
Expand All @@ -422,7 +426,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {

if tt.wantErr {
// simulate error condition
mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(nil, fmt.Errorf("mock error")).Times(1)
mockGetComponents.Return(nil, fmt.Errorf("mock error"))

}

Expand Down Expand Up @@ -646,15 +650,20 @@ func TestGetInitContainers(t *testing.T) {
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

mockGetCommands := mockDevfileData.EXPECT().GetCommands(common.DevfileOptions{})

// set up the mock data
mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(containers, nil).AnyTimes()
mockDevfileData.EXPECT().GetComponents(common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
},
}).Return(containers, nil).AnyTimes()
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()
mockDevfileData.EXPECT().GetEvents().Return(preStartEvents).AnyTimes()
mockGetCommands.Return(append(applyCommands, compCommands...), nil).AnyTimes()

if tt.wantErr {
mockDevfileData.EXPECT().GetCommands(common.DevfileOptions{}).Return(nil, fmt.Errorf("mock error")).AnyTimes()
} else {
mockDevfileData.EXPECT().GetCommands(common.DevfileOptions{}).Return(append(applyCommands, compCommands...), nil).AnyTimes()
mockGetCommands.Return(nil, fmt.Errorf("mock error")).AnyTimes()
}

devObj := parser.DevfileObj{
Expand Down
5 changes: 4 additions & 1 deletion pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
// exposure level: public > internal > none
func getPortExposure(devfileObj parser.DevfileObj, options common.DevfileOptions) (map[int]v1.EndpointExposure, error) {
portExposureMap := make(map[int]v1.EndpointExposure)
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
options.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
containerComponents, err := devfileObj.Data.GetComponents(options)
if err != nil {
return portExposureMap, err
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/devfile/generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,16 @@ func TestGetServiceSpec(t *testing.T) {
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

tt.filterOptions.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
mockGetComponents := mockDevfileData.EXPECT().GetComponents(tt.filterOptions)

// set up the mock data
if len(tt.filterOptions.Filter) == 0 {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
} else {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes()
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
}
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()

Expand Down Expand Up @@ -1125,11 +1130,16 @@ func TestGetPortExposure(t *testing.T) {
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

tt.filterOptions.ComponentOptions = common.ComponentOptions{
ComponentType: v1.ContainerComponentType,
}
mockGetComponents := mockDevfileData.EXPECT().GetComponents(tt.filterOptions)

// set up the mock data
if len(tt.filterOptions.Filter) == 0 {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
} else {
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes()
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
}
devObj := parser.DevfileObj{
Data: mockDevfileData,
Expand Down

0 comments on commit c0ce883

Please sign in to comment.