Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock func for DevfileData interface #93

Merged
merged 4 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
github.com/golang/mock v1.5.0
github.com/google/go-cmp v0.5.2
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4er
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
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
191 changes: 112 additions & 79 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package generator

import (
"github.com/devfile/library/pkg/devfile/parser/data"
"github.com/devfile/library/pkg/util"
"fmt"
"reflect"
"strings"
"testing"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
"github.com/devfile/library/pkg/devfile/parser"
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
"github.com/devfile/library/pkg/devfile/parser/data"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/pkg/testingutil"
"github.com/devfile/library/pkg/util"
"github.com/golang/mock/gomock"

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

project := v1.Project{
ClonePath: "test-project/",
Name: "project0",
ProjectSource: v1.ProjectSource{
Git: &v1.GitProjectSource{
GitLikeProjectSource: v1.GitLikeProjectSource{
Remotes: map[string]string{
"origin": "repo",
projects := []v1.Project{
{
ClonePath: "test-project/",
Name: "project0",
ProjectSource: v1.ProjectSource{
Git: &v1.GitProjectSource{
GitLikeProjectSource: v1.GitLikeProjectSource{
Remotes: map[string]string{
"origin": "repo",
},
},
},
},
Expand All @@ -47,6 +50,7 @@ func TestGetContainers(t *testing.T) {
tests := []struct {
name string
containerComponents []v1.Component
filteredComponents []v1.Component
filterOptions common.DevfileOptions
wantContainerName string
wantContainerImage string
Expand Down Expand Up @@ -175,6 +179,23 @@ func TestGetContainers(t *testing.T) {
},
wantContainerName: containerNames[1],
wantContainerImage: containerImages[0],
filteredComponents: []v1.Component{
{
Name: containerNames[1],
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
"firstString": "firstStringValue",
"thirdString": "thirdStringValue",
}),
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: containerImages[0],
MountSources: &falseMountSources,
},
},
},
},
},
filterOptions: common.DevfileOptions{
Filter: map[string]interface{}{
"firstString": "firstStringValue",
Expand All @@ -185,19 +206,25 @@ func TestGetContainers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

ctrl := gomock.NewController(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 {
mockGetComponents.Return(tt.containerComponents, nil).AnyTimes()
} else {
mockGetComponents.Return(tt.filteredComponents, nil).AnyTimes()
}
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(projects, nil).AnyTimes()

devObj := parser.DevfileObj{
Data: &v2.DevfileV2{
Devfile: v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Components: tt.containerComponents,
Projects: []v1.Project{
project,
},
},
},
},
},
Data: mockDevfileData,
}

containers, err := GetContainers(devObj, tt.filterOptions)
Expand Down Expand Up @@ -364,33 +391,31 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) {
wantErr: false,
},
{
name: "Invalid case",
components: []v1.Component{
{
Name: "container1",
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
"firstString": "firstStringValue",
}),
ComponentUnion: v1.ComponentUnion{},
},
},
wantErr: true,
name: "Invalid case simulating no container components",
components: nil,
wantErr: true,
},
}

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

devObj := parser.DevfileObj{
Data: &v2.DevfileV2{
Devfile: v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Components: tt.components,
},
},
},
ctrl := gomock.NewController(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
mockGetComponents.Return(tt.components, nil).AnyTimes()
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes()

devObj := parser.DevfileObj{
Data: mockDevfileData,
}

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

var options common.DevfileOptions
if tt.wantErr {
options = common.DevfileOptions{
Filter: map[string]interface{}{
"firstString": "firstStringValue",
},
}
// simulate error condition
mockGetComponents.Return(nil, fmt.Errorf("mock error"))

}

volumeParams := VolumeParams{
Containers: containers,
VolumeNameToVolumeInfo: tt.volumeNameToVolInfo,
}

pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, options)
pvcVols, err := GetVolumesAndVolumeMounts(devObj, volumeParams, common.DevfileOptions{})
if tt.wantErr == (err == nil) {
t.Errorf("TestGetVolumesAndVolumeMounts() error = %v, wantErr %v", err, tt.wantErr)
} else if err == nil {
Expand Down Expand Up @@ -518,7 +540,7 @@ func TestGetInitContainers(t *testing.T) {
},
}

execCommands := []v1.Command{
applyCommands := []v1.Command{
{
Id: "apply1",
CommandUnion: v1.CommandUnion{
Expand Down Expand Up @@ -588,6 +610,15 @@ func TestGetInitContainers(t *testing.T) {
},
},
},
{
name: "Simulate error condition",
eventCommands: []string{
"apply1",
"apply3",
"apply2",
},
wantErr: true,
},
{
name: "Long Container Name",
eventCommands: []string{
Expand All @@ -604,44 +635,46 @@ func TestGetInitContainers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

preStartEvents := v1.Events{
DevWorkspaceEvents: v1.DevWorkspaceEvents{
PreStart: tt.eventCommands,
},
}

if tt.longName {
containers[0].Name = longContainerName
execCommands[1].Apply.Component = longContainerName
applyCommands[1].Apply.Component = longContainerName
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockDevfileData := data.NewMockDevfileData(ctrl)

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

// set up the mock data
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 {
mockGetCommands.Return(nil, fmt.Errorf("mock error")).AnyTimes()
}

devObj := parser.DevfileObj{
Data: func() data.DevfileData {
devfileData, err := data.NewDevfileData(string(data.APISchemaVersion210))
if err != nil {
t.Error(err)
}
err = devfileData.AddComponents(containers)
if err != nil {
t.Error(err)
}
err = devfileData.AddCommands(execCommands)
if err != nil {
t.Error(err)
}
err = devfileData.AddCommands(compCommands)
if err != nil {
t.Error(err)
}
err = devfileData.AddEvents(v1.Events{
DevWorkspaceEvents: v1.DevWorkspaceEvents{
PreStart: tt.eventCommands,
},
})
if err != nil {
t.Error(err)
}
return devfileData
}(),
Data: mockDevfileData,
}

initContainers, err := GetInitContainers(devObj)
if (err != nil) != tt.wantErr {
t.Errorf("TestGetInitContainers() error = %v, wantErr %v", err, tt.wantErr)
} else if err != nil {
return
}

if len(tt.wantInitContainer) != len(initContainers) {
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
Loading