From 19d6173f973cfd3c2ae58c0f8544886e59d739fd Mon Sep 17 00:00:00 2001 From: Maysun J Faisal Date: Fri, 4 Jun 2021 17:04:57 -0400 Subject: [PATCH 1/4] Add mock func for DevfileData Signed-off-by: Maysun J Faisal --- go.mod | 1 + go.sum | 2 + pkg/devfile/generator/generators_test.go | 52 +- pkg/devfile/parser/data/interface.go | 3 + pkg/devfile/parser/data/mock_interface.go | 552 ++++++++++++++++++++++ 5 files changed, 579 insertions(+), 31 deletions(-) create mode 100644 pkg/devfile/parser/data/mock_interface.go diff --git a/go.mod b/go.mod index e102cf8a..9686eba9 100644 --- a/go.mod +++ b/go.mod @@ -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 // indirect 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 diff --git a/go.sum b/go.sum index ba93c3a9..4a0ed46f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index 399aed38..c0dbd400 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -10,9 +10,11 @@ import ( v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" "github.com/devfile/library/pkg/devfile/parser" + "github.com/devfile/library/pkg/devfile/parser/data" v2 "github.com/devfile/library/pkg/devfile/parser/data/v2" "github.com/devfile/library/pkg/devfile/parser/data/v2/common" "github.com/devfile/library/pkg/testingutil" + "github.com/golang/mock/gomock" corev1 "k8s.io/api/core/v1" ) @@ -30,14 +32,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", + }, }, }, }, @@ -146,17 +150,6 @@ func TestGetContainers(t *testing.T) { { name: "Filter containers", containerComponents: []v1.Component{ - { - Name: containerNames[0], - ComponentUnion: v1.ComponentUnion{ - Container: &v1.ContainerComponent{ - Container: v1.Container{ - Image: containerImages[0], - MountSources: &falseMountSources, - }, - }, - }, - }, { Name: containerNames[1], Attributes: attributes.Attributes{}.FromStringMap(map[string]string{ @@ -185,19 +178,16 @@ 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) + + // set up the mock data + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, 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) diff --git a/pkg/devfile/parser/data/interface.go b/pkg/devfile/parser/data/interface.go index 11882d4e..5721ca1d 100644 --- a/pkg/devfile/parser/data/interface.go +++ b/pkg/devfile/parser/data/interface.go @@ -7,6 +7,9 @@ import ( "github.com/devfile/library/pkg/devfile/parser/data/v2/common" ) +// Generate mock interfaces for DevfileData by executing the following cmd in pkg/devfile/parser/data +// mockgen -package=data -source=interface.go DevfileData > /tmp/mock_interface.go ; cp /tmp/mock_interface.go ./mock_interface.go + // DevfileData is an interface that defines functions for Devfile data operations type DevfileData interface { diff --git a/pkg/devfile/parser/data/mock_interface.go b/pkg/devfile/parser/data/mock_interface.go new file mode 100644 index 00000000..89951d41 --- /dev/null +++ b/pkg/devfile/parser/data/mock_interface.go @@ -0,0 +1,552 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: interface.go + +// Package data is a generated GoMock package. +package data + +import ( + reflect "reflect" + + v1alpha2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" + attributes "github.com/devfile/api/v2/pkg/attributes" + devfile "github.com/devfile/api/v2/pkg/devfile" + common "github.com/devfile/library/pkg/devfile/parser/data/v2/common" + gomock "github.com/golang/mock/gomock" +) + +// MockDevfileData is a mock of DevfileData interface. +type MockDevfileData struct { + ctrl *gomock.Controller + recorder *MockDevfileDataMockRecorder +} + +// MockDevfileDataMockRecorder is the mock recorder for MockDevfileData. +type MockDevfileDataMockRecorder struct { + mock *MockDevfileData +} + +// NewMockDevfileData creates a new mock instance. +func NewMockDevfileData(ctrl *gomock.Controller) *MockDevfileData { + mock := &MockDevfileData{ctrl: ctrl} + mock.recorder = &MockDevfileDataMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockDevfileData) EXPECT() *MockDevfileDataMockRecorder { + return m.recorder +} + +// AddAttributes mocks base method. +func (m *MockDevfileData) AddAttributes(key string, value interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddAttributes", key, value) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddAttributes indicates an expected call of AddAttributes. +func (mr *MockDevfileDataMockRecorder) AddAttributes(key, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddAttributes", reflect.TypeOf((*MockDevfileData)(nil).AddAttributes), key, value) +} + +// AddCommands mocks base method. +func (m *MockDevfileData) AddCommands(commands []v1alpha2.Command) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddCommands", commands) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddCommands indicates an expected call of AddCommands. +func (mr *MockDevfileDataMockRecorder) AddCommands(commands interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddCommands", reflect.TypeOf((*MockDevfileData)(nil).AddCommands), commands) +} + +// AddComponents mocks base method. +func (m *MockDevfileData) AddComponents(components []v1alpha2.Component) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddComponents", components) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddComponents indicates an expected call of AddComponents. +func (mr *MockDevfileDataMockRecorder) AddComponents(components interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddComponents", reflect.TypeOf((*MockDevfileData)(nil).AddComponents), components) +} + +// AddEvents mocks base method. +func (m *MockDevfileData) AddEvents(events v1alpha2.Events) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddEvents", events) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddEvents indicates an expected call of AddEvents. +func (mr *MockDevfileDataMockRecorder) AddEvents(events interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddEvents", reflect.TypeOf((*MockDevfileData)(nil).AddEvents), events) +} + +// AddProjects mocks base method. +func (m *MockDevfileData) AddProjects(projects []v1alpha2.Project) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddProjects", projects) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddProjects indicates an expected call of AddProjects. +func (mr *MockDevfileDataMockRecorder) AddProjects(projects interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddProjects", reflect.TypeOf((*MockDevfileData)(nil).AddProjects), projects) +} + +// AddStarterProjects mocks base method. +func (m *MockDevfileData) AddStarterProjects(projects []v1alpha2.StarterProject) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddStarterProjects", projects) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddStarterProjects indicates an expected call of AddStarterProjects. +func (mr *MockDevfileDataMockRecorder) AddStarterProjects(projects interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddStarterProjects", reflect.TypeOf((*MockDevfileData)(nil).AddStarterProjects), projects) +} + +// AddVolumeMounts mocks base method. +func (m *MockDevfileData) AddVolumeMounts(containerName string, volumeMounts []v1alpha2.VolumeMount) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddVolumeMounts", containerName, volumeMounts) + ret0, _ := ret[0].(error) + return ret0 +} + +// AddVolumeMounts indicates an expected call of AddVolumeMounts. +func (mr *MockDevfileDataMockRecorder) AddVolumeMounts(containerName, volumeMounts interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddVolumeMounts", reflect.TypeOf((*MockDevfileData)(nil).AddVolumeMounts), containerName, volumeMounts) +} + +// DeleteCommand mocks base method. +func (m *MockDevfileData) DeleteCommand(id string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteCommand", id) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteCommand indicates an expected call of DeleteCommand. +func (mr *MockDevfileDataMockRecorder) DeleteCommand(id interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteCommand", reflect.TypeOf((*MockDevfileData)(nil).DeleteCommand), id) +} + +// DeleteComponent mocks base method. +func (m *MockDevfileData) DeleteComponent(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteComponent", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteComponent indicates an expected call of DeleteComponent. +func (mr *MockDevfileDataMockRecorder) DeleteComponent(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteComponent", reflect.TypeOf((*MockDevfileData)(nil).DeleteComponent), name) +} + +// DeleteProject mocks base method. +func (m *MockDevfileData) DeleteProject(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteProject", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteProject indicates an expected call of DeleteProject. +func (mr *MockDevfileDataMockRecorder) DeleteProject(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteProject", reflect.TypeOf((*MockDevfileData)(nil).DeleteProject), name) +} + +// DeleteStarterProject mocks base method. +func (m *MockDevfileData) DeleteStarterProject(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteStarterProject", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteStarterProject indicates an expected call of DeleteStarterProject. +func (mr *MockDevfileDataMockRecorder) DeleteStarterProject(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteStarterProject", reflect.TypeOf((*MockDevfileData)(nil).DeleteStarterProject), name) +} + +// DeleteVolumeMount mocks base method. +func (m *MockDevfileData) DeleteVolumeMount(name string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteVolumeMount", name) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteVolumeMount indicates an expected call of DeleteVolumeMount. +func (mr *MockDevfileDataMockRecorder) DeleteVolumeMount(name interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteVolumeMount", reflect.TypeOf((*MockDevfileData)(nil).DeleteVolumeMount), name) +} + +// GetAttributes mocks base method. +func (m *MockDevfileData) GetAttributes() (attributes.Attributes, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAttributes") + ret0, _ := ret[0].(attributes.Attributes) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetAttributes indicates an expected call of GetAttributes. +func (mr *MockDevfileDataMockRecorder) GetAttributes() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAttributes", reflect.TypeOf((*MockDevfileData)(nil).GetAttributes)) +} + +// GetCommands mocks base method. +func (m *MockDevfileData) GetCommands(arg0 common.DevfileOptions) ([]v1alpha2.Command, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetCommands", arg0) + ret0, _ := ret[0].([]v1alpha2.Command) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetCommands indicates an expected call of GetCommands. +func (mr *MockDevfileDataMockRecorder) GetCommands(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCommands", reflect.TypeOf((*MockDevfileData)(nil).GetCommands), arg0) +} + +// GetComponents mocks base method. +func (m *MockDevfileData) GetComponents(arg0 common.DevfileOptions) ([]v1alpha2.Component, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetComponents", arg0) + ret0, _ := ret[0].([]v1alpha2.Component) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetComponents indicates an expected call of GetComponents. +func (mr *MockDevfileDataMockRecorder) GetComponents(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetComponents", reflect.TypeOf((*MockDevfileData)(nil).GetComponents), arg0) +} + +// GetDevfileContainerComponents mocks base method. +func (m *MockDevfileData) GetDevfileContainerComponents(arg0 common.DevfileOptions) ([]v1alpha2.Component, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevfileContainerComponents", arg0) + ret0, _ := ret[0].([]v1alpha2.Component) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDevfileContainerComponents indicates an expected call of GetDevfileContainerComponents. +func (mr *MockDevfileDataMockRecorder) GetDevfileContainerComponents(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevfileContainerComponents", reflect.TypeOf((*MockDevfileData)(nil).GetDevfileContainerComponents), arg0) +} + +// GetDevfileVolumeComponents mocks base method. +func (m *MockDevfileData) GetDevfileVolumeComponents(arg0 common.DevfileOptions) ([]v1alpha2.Component, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevfileVolumeComponents", arg0) + ret0, _ := ret[0].([]v1alpha2.Component) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetDevfileVolumeComponents indicates an expected call of GetDevfileVolumeComponents. +func (mr *MockDevfileDataMockRecorder) GetDevfileVolumeComponents(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevfileVolumeComponents", reflect.TypeOf((*MockDevfileData)(nil).GetDevfileVolumeComponents), arg0) +} + +// GetDevfileWorkspaceSpec mocks base method. +func (m *MockDevfileData) GetDevfileWorkspaceSpec() *v1alpha2.DevWorkspaceTemplateSpec { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevfileWorkspaceSpec") + ret0, _ := ret[0].(*v1alpha2.DevWorkspaceTemplateSpec) + return ret0 +} + +// GetDevfileWorkspaceSpec indicates an expected call of GetDevfileWorkspaceSpec. +func (mr *MockDevfileDataMockRecorder) GetDevfileWorkspaceSpec() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevfileWorkspaceSpec", reflect.TypeOf((*MockDevfileData)(nil).GetDevfileWorkspaceSpec)) +} + +// GetDevfileWorkspaceSpecContent mocks base method. +func (m *MockDevfileData) GetDevfileWorkspaceSpecContent() *v1alpha2.DevWorkspaceTemplateSpecContent { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetDevfileWorkspaceSpecContent") + ret0, _ := ret[0].(*v1alpha2.DevWorkspaceTemplateSpecContent) + return ret0 +} + +// GetDevfileWorkspaceSpecContent indicates an expected call of GetDevfileWorkspaceSpecContent. +func (mr *MockDevfileDataMockRecorder) GetDevfileWorkspaceSpecContent() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDevfileWorkspaceSpecContent", reflect.TypeOf((*MockDevfileData)(nil).GetDevfileWorkspaceSpecContent)) +} + +// GetEvents mocks base method. +func (m *MockDevfileData) GetEvents() v1alpha2.Events { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetEvents") + ret0, _ := ret[0].(v1alpha2.Events) + return ret0 +} + +// GetEvents indicates an expected call of GetEvents. +func (mr *MockDevfileDataMockRecorder) GetEvents() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetEvents", reflect.TypeOf((*MockDevfileData)(nil).GetEvents)) +} + +// GetMetadata mocks base method. +func (m *MockDevfileData) GetMetadata() devfile.DevfileMetadata { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetMetadata") + ret0, _ := ret[0].(devfile.DevfileMetadata) + return ret0 +} + +// GetMetadata indicates an expected call of GetMetadata. +func (mr *MockDevfileDataMockRecorder) GetMetadata() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMetadata", reflect.TypeOf((*MockDevfileData)(nil).GetMetadata)) +} + +// GetParent mocks base method. +func (m *MockDevfileData) GetParent() *v1alpha2.Parent { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetParent") + ret0, _ := ret[0].(*v1alpha2.Parent) + return ret0 +} + +// GetParent indicates an expected call of GetParent. +func (mr *MockDevfileDataMockRecorder) GetParent() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetParent", reflect.TypeOf((*MockDevfileData)(nil).GetParent)) +} + +// GetProjects mocks base method. +func (m *MockDevfileData) GetProjects(arg0 common.DevfileOptions) ([]v1alpha2.Project, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProjects", arg0) + ret0, _ := ret[0].([]v1alpha2.Project) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProjects indicates an expected call of GetProjects. +func (mr *MockDevfileDataMockRecorder) GetProjects(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProjects", reflect.TypeOf((*MockDevfileData)(nil).GetProjects), arg0) +} + +// GetSchemaVersion mocks base method. +func (m *MockDevfileData) GetSchemaVersion() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetSchemaVersion") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetSchemaVersion indicates an expected call of GetSchemaVersion. +func (mr *MockDevfileDataMockRecorder) GetSchemaVersion() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSchemaVersion", reflect.TypeOf((*MockDevfileData)(nil).GetSchemaVersion)) +} + +// GetStarterProjects mocks base method. +func (m *MockDevfileData) GetStarterProjects(arg0 common.DevfileOptions) ([]v1alpha2.StarterProject, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetStarterProjects", arg0) + ret0, _ := ret[0].([]v1alpha2.StarterProject) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetStarterProjects indicates an expected call of GetStarterProjects. +func (mr *MockDevfileDataMockRecorder) GetStarterProjects(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetStarterProjects", reflect.TypeOf((*MockDevfileData)(nil).GetStarterProjects), arg0) +} + +// GetVolumeMountPaths mocks base method. +func (m *MockDevfileData) GetVolumeMountPaths(mountName, containerName string) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetVolumeMountPaths", mountName, containerName) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetVolumeMountPaths indicates an expected call of GetVolumeMountPaths. +func (mr *MockDevfileDataMockRecorder) GetVolumeMountPaths(mountName, containerName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVolumeMountPaths", reflect.TypeOf((*MockDevfileData)(nil).GetVolumeMountPaths), mountName, containerName) +} + +// SetDevfileWorkspaceSpec mocks base method. +func (m *MockDevfileData) SetDevfileWorkspaceSpec(spec v1alpha2.DevWorkspaceTemplateSpec) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetDevfileWorkspaceSpec", spec) +} + +// SetDevfileWorkspaceSpec indicates an expected call of SetDevfileWorkspaceSpec. +func (mr *MockDevfileDataMockRecorder) SetDevfileWorkspaceSpec(spec interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDevfileWorkspaceSpec", reflect.TypeOf((*MockDevfileData)(nil).SetDevfileWorkspaceSpec), spec) +} + +// SetDevfileWorkspaceSpecContent mocks base method. +func (m *MockDevfileData) SetDevfileWorkspaceSpecContent(content v1alpha2.DevWorkspaceTemplateSpecContent) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetDevfileWorkspaceSpecContent", content) +} + +// SetDevfileWorkspaceSpecContent indicates an expected call of SetDevfileWorkspaceSpecContent. +func (mr *MockDevfileDataMockRecorder) SetDevfileWorkspaceSpecContent(content interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDevfileWorkspaceSpecContent", reflect.TypeOf((*MockDevfileData)(nil).SetDevfileWorkspaceSpecContent), content) +} + +// SetMetadata mocks base method. +func (m *MockDevfileData) SetMetadata(metadata devfile.DevfileMetadata) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetMetadata", metadata) +} + +// SetMetadata indicates an expected call of SetMetadata. +func (mr *MockDevfileDataMockRecorder) SetMetadata(metadata interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetMetadata", reflect.TypeOf((*MockDevfileData)(nil).SetMetadata), metadata) +} + +// SetParent mocks base method. +func (m *MockDevfileData) SetParent(parent *v1alpha2.Parent) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetParent", parent) +} + +// SetParent indicates an expected call of SetParent. +func (mr *MockDevfileDataMockRecorder) SetParent(parent interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetParent", reflect.TypeOf((*MockDevfileData)(nil).SetParent), parent) +} + +// SetSchemaVersion mocks base method. +func (m *MockDevfileData) SetSchemaVersion(version string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetSchemaVersion", version) +} + +// SetSchemaVersion indicates an expected call of SetSchemaVersion. +func (mr *MockDevfileDataMockRecorder) SetSchemaVersion(version interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetSchemaVersion", reflect.TypeOf((*MockDevfileData)(nil).SetSchemaVersion), version) +} + +// UpdateAttributes mocks base method. +func (m *MockDevfileData) UpdateAttributes(key string, value interface{}) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateAttributes", key, value) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateAttributes indicates an expected call of UpdateAttributes. +func (mr *MockDevfileDataMockRecorder) UpdateAttributes(key, value interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateAttributes", reflect.TypeOf((*MockDevfileData)(nil).UpdateAttributes), key, value) +} + +// UpdateCommand mocks base method. +func (m *MockDevfileData) UpdateCommand(command v1alpha2.Command) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateCommand", command) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateCommand indicates an expected call of UpdateCommand. +func (mr *MockDevfileDataMockRecorder) UpdateCommand(command interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateCommand", reflect.TypeOf((*MockDevfileData)(nil).UpdateCommand), command) +} + +// UpdateComponent mocks base method. +func (m *MockDevfileData) UpdateComponent(component v1alpha2.Component) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateComponent", component) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateComponent indicates an expected call of UpdateComponent. +func (mr *MockDevfileDataMockRecorder) UpdateComponent(component interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateComponent", reflect.TypeOf((*MockDevfileData)(nil).UpdateComponent), component) +} + +// UpdateEvents mocks base method. +func (m *MockDevfileData) UpdateEvents(postStart, postStop, preStart, preStop []string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "UpdateEvents", postStart, postStop, preStart, preStop) +} + +// UpdateEvents indicates an expected call of UpdateEvents. +func (mr *MockDevfileDataMockRecorder) UpdateEvents(postStart, postStop, preStart, preStop interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateEvents", reflect.TypeOf((*MockDevfileData)(nil).UpdateEvents), postStart, postStop, preStart, preStop) +} + +// UpdateProject mocks base method. +func (m *MockDevfileData) UpdateProject(project v1alpha2.Project) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateProject", project) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateProject indicates an expected call of UpdateProject. +func (mr *MockDevfileDataMockRecorder) UpdateProject(project interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateProject", reflect.TypeOf((*MockDevfileData)(nil).UpdateProject), project) +} + +// UpdateStarterProject mocks base method. +func (m *MockDevfileData) UpdateStarterProject(project v1alpha2.StarterProject) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateStarterProject", project) + ret0, _ := ret[0].(error) + return ret0 +} + +// UpdateStarterProject indicates an expected call of UpdateStarterProject. +func (mr *MockDevfileDataMockRecorder) UpdateStarterProject(project interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateStarterProject", reflect.TypeOf((*MockDevfileData)(nil).UpdateStarterProject), project) +} From d34425b3a5bffc10c9e0daf0aa940b627f35e84d Mon Sep 17 00:00:00 2001 From: Maysun J Faisal Date: Mon, 7 Jun 2021 17:09:36 -0400 Subject: [PATCH 2/4] Update mock calls for the other func Signed-off-by: Maysun J Faisal --- go.mod | 2 +- pkg/devfile/generator/generators_test.go | 119 ++++++++++++----------- pkg/devfile/generator/utils_test.go | 95 ++++++++++++++---- 3 files changed, 137 insertions(+), 79 deletions(-) diff --git a/go.mod b/go.mod index 9686eba9..408dbabc 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +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 // indirect + 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 diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index c0dbd400..82df0ca3 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -1,8 +1,7 @@ package generator import ( - "github.com/devfile/library/pkg/devfile/parser/data" - "github.com/devfile/library/pkg/util" + "fmt" "reflect" "strings" "testing" @@ -11,9 +10,9 @@ import ( "github.com/devfile/api/v2/pkg/attributes" "github.com/devfile/library/pkg/devfile/parser" "github.com/devfile/library/pkg/devfile/parser/data" - v2 "github.com/devfile/library/pkg/devfile/parser/data/v2" "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" @@ -354,33 +353,32 @@ 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) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockDevfileData := data.NewMockDevfileData(ctrl) + + // 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() + } + mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes() + devObj := parser.DevfileObj{ - Data: &v2.DevfileV2{ - Devfile: v1.Devfile{ - DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ - DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ - Components: tt.components, - }, - }, - }, - }, + Data: mockDevfileData, } containers, err := GetContainers(devObj, common.DevfileOptions{}) @@ -389,13 +387,10 @@ 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 + mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(nil, fmt.Errorf("mock error")).Times(1) + } volumeParams := VolumeParams{ @@ -403,7 +398,7 @@ func TestGetVolumesAndVolumeMounts(t *testing.T) { 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 { @@ -508,7 +503,7 @@ func TestGetInitContainers(t *testing.T) { }, } - execCommands := []v1.Command{ + applyCommands := []v1.Command{ { Id: "apply1", CommandUnion: v1.CommandUnion{ @@ -578,6 +573,15 @@ func TestGetInitContainers(t *testing.T) { }, }, }, + { + name: "Simulate error condition", + eventCommands: []string{ + "apply1", + "apply3", + "apply2", + }, + wantErr: true, + }, { name: "Long Container Name", eventCommands: []string{ @@ -594,44 +598,41 @@ 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) + + // set up the mock data + mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(containers, nil).AnyTimes() + mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes() + mockDevfileData.EXPECT().GetEvents().Return(preStartEvents).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() } 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) { diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index f034d4cc..53894b52 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -8,9 +8,10 @@ import ( "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/golang/mock/gomock" buildv1 "github.com/openshift/api/build/v1" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" @@ -608,6 +609,7 @@ func TestGetServiceSpec(t *testing.T) { tests := []struct { name string containerComponents []v1.Component + filteredComponents []v1.Component labels map[string]string filterOptions common.DevfileOptions wantPorts []corev1.ServicePort @@ -727,6 +729,25 @@ func TestGetServiceSpec(t *testing.T) { }, }, wantErr: false, + filteredComponents: []v1.Component{ + { + Name: "testcontainer2", + Attributes: attributes.Attributes{}.FromStringMap(map[string]string{ + "firstString": "firstStringValue", + "thirdString": "thirdStringValue", + }), + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Endpoints: []v1.Endpoint{ + { + Name: endpointNames[2], + TargetPort: 9090, + }, + }, + }, + }, + }, + }, filterOptions: common.DevfileOptions{ Filter: map[string]interface{}{ "firstString": "firstStringValue", @@ -737,16 +758,20 @@ func TestGetServiceSpec(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) + + // set up the mock data + if len(tt.filterOptions.Filter) == 0 { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes() + } else { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes() + } + mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(nil, nil).AnyTimes() + devObj := parser.DevfileObj{ - Data: &v2.DevfileV2{ - Devfile: v1.Devfile{ - DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ - DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ - Components: tt.containerComponents, - }, - }, - }, - }, + Data: mockDevfileData, } serviceSpec, err := getServiceSpec(devObj, tt.labels, tt.filterOptions) @@ -781,6 +806,7 @@ func TestGetPortExposure(t *testing.T) { tests := []struct { name string containerComponents []v1.Component + filteredComponents []v1.Component filterOptions common.DevfileOptions wantMap map[int]v1.EndpointExposure wantErr bool @@ -1020,6 +1046,33 @@ func TestGetPortExposure(t *testing.T) { }, }, }, + filteredComponents: []v1.Component{ + { + Name: "testcontainer1", + Attributes: attributes.Attributes{}.FromStringMap(map[string]string{ + "firstString": "firstStringValue", + "thirdString": "thirdStringValue", + }), + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: "image", + }, + Endpoints: []v1.Endpoint{ + { + Name: urlName, + TargetPort: 8080, + }, + { + Name: urlName, + TargetPort: 3000, + Exposure: v1.NoneEndpointExposure, + }, + }, + }, + }, + }, + }, filterOptions: common.DevfileOptions{ Filter: map[string]interface{}{ "firstString": "firstStringValue", @@ -1056,6 +1109,7 @@ func TestGetPortExposure(t *testing.T) { }, }, }, + filteredComponents: nil, filterOptions: common.DevfileOptions{ Filter: map[string]interface{}{ "firstStringWrong": "firstStringValue", @@ -1066,16 +1120,19 @@ func TestGetPortExposure(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) + + // set up the mock data + if len(tt.filterOptions.Filter) == 0 { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes() + } else { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes() + } devObj := parser.DevfileObj{ - Data: &v2.DevfileV2{ - Devfile: v1.Devfile{ - DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{ - DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{ - Components: tt.containerComponents, - }, - }, - }, - }, + Data: mockDevfileData, } mapCreated, err := getPortExposure(devObj, tt.filterOptions) From beb20b64f9d1784a2907eb27c07192961d56583b Mon Sep 17 00:00:00 2001 From: Maysun J Faisal Date: Mon, 7 Jun 2021 17:27:12 -0400 Subject: [PATCH 3/4] Update filtered test Signed-off-by: Maysun J Faisal --- pkg/devfile/generator/generators_test.go | 35 +++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index 82df0ca3..68e78841 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -50,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 @@ -149,6 +150,17 @@ func TestGetContainers(t *testing.T) { { name: "Filter containers", containerComponents: []v1.Component{ + { + Name: containerNames[0], + ComponentUnion: v1.ComponentUnion{ + Container: &v1.ContainerComponent{ + Container: v1.Container{ + Image: containerImages[0], + MountSources: &falseMountSources, + }, + }, + }, + }, { Name: containerNames[1], Attributes: attributes.Attributes{}.FromStringMap(map[string]string{ @@ -167,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", @@ -182,7 +211,11 @@ func TestGetContainers(t *testing.T) { mockDevfileData := data.NewMockDevfileData(ctrl) // set up the mock data - mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes() + if len(tt.filterOptions.Filter) == 0 { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes() + } else { + mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.filteredComponents, nil).AnyTimes() + } mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(projects, nil).AnyTimes() devObj := parser.DevfileObj{ From c0ce8836802a3a4009c871956278364020fcd586 Mon Sep 17 00:00:00 2001 From: Maysun J Faisal Date: Thu, 10 Jun 2021 15:10:19 -0400 Subject: [PATCH 4/4] PR feedback - mock and update dep func usage Signed-off-by: Maysun J Faisal --- pkg/devfile/generator/generators.go | 12 ++++++-- pkg/devfile/generator/generators_test.go | 39 +++++++++++++++--------- pkg/devfile/generator/utils.go | 5 ++- pkg/devfile/generator/utils_test.go | 18 ++++++++--- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/pkg/devfile/generator/generators.go b/pkg/devfile/generator/generators.go index b0a16e36..63ebc0b2 100644 --- a/pkg/devfile/generator/generators.go +++ b/pkg/devfile/generator/generators.go @@ -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" @@ -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 } @@ -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 } diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index 68e78841..b390c5e4 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -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() @@ -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{ @@ -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")) } @@ -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{ diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index 4a7cb7f0..23ccdc68 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -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 } diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index 53894b52..c500dbb0 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -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() @@ -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,