Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 75bb643

Browse files
committedJun 4, 2021
Add mock func for DevfileData
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent b420dad commit 75bb643

File tree

5 files changed

+579
-31
lines changed

5 files changed

+579
-31
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 // indirect
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_test.go

+21-31
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
88
"github.com/devfile/api/v2/pkg/attributes"
99
"github.com/devfile/library/pkg/devfile/parser"
10+
"github.com/devfile/library/pkg/devfile/parser/data"
1011
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
1112
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1213
"github.com/devfile/library/pkg/testingutil"
14+
"github.com/golang/mock/gomock"
1315

1416
corev1 "k8s.io/api/core/v1"
1517
)
@@ -27,14 +29,16 @@ func TestGetContainers(t *testing.T) {
2729
trueMountSources := true
2830
falseMountSources := false
2931

30-
project := v1.Project{
31-
ClonePath: "test-project/",
32-
Name: "project0",
33-
ProjectSource: v1.ProjectSource{
34-
Git: &v1.GitProjectSource{
35-
GitLikeProjectSource: v1.GitLikeProjectSource{
36-
Remotes: map[string]string{
37-
"origin": "repo",
32+
projects := []v1.Project{
33+
{
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",
41+
},
3842
},
3943
},
4044
},
@@ -143,17 +147,6 @@ func TestGetContainers(t *testing.T) {
143147
{
144148
name: "Filter containers",
145149
containerComponents: []v1.Component{
146-
{
147-
Name: containerNames[0],
148-
ComponentUnion: v1.ComponentUnion{
149-
Container: &v1.ContainerComponent{
150-
Container: v1.Container{
151-
Image: containerImages[0],
152-
MountSources: &falseMountSources,
153-
},
154-
},
155-
},
156-
},
157150
{
158151
Name: containerNames[1],
159152
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
@@ -182,19 +175,16 @@ func TestGetContainers(t *testing.T) {
182175
for _, tt := range tests {
183176
t.Run(tt.name, func(t *testing.T) {
184177

178+
ctrl := gomock.NewController(t)
179+
defer ctrl.Finish()
180+
mockDevfileData := data.NewMockDevfileData(ctrl)
181+
182+
// set up the mock data
183+
mockDevfileData.EXPECT().GetDevfileContainerComponents(tt.filterOptions).Return(tt.containerComponents, nil).AnyTimes()
184+
mockDevfileData.EXPECT().GetProjects(common.DevfileOptions{}).Return(projects, nil).AnyTimes()
185+
185186
devObj := parser.DevfileObj{
186-
Data: &v2.DevfileV2{
187-
Devfile: v1.Devfile{
188-
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
189-
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
190-
Components: tt.containerComponents,
191-
Projects: []v1.Project{
192-
project,
193-
},
194-
},
195-
},
196-
},
197-
},
187+
Data: mockDevfileData,
198188
}
199189

200190
containers, err := GetContainers(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.