Skip to content

Commit

Permalink
Add mock func for DevfileData
Browse files Browse the repository at this point in the history
Signed-off-by: Maysun J Faisal <maysunaneek@gmail.com>
  • Loading branch information
maysunfaisal committed Jun 4, 2021
1 parent b420dad commit 75bb643
Show file tree
Hide file tree
Showing 5 changed files with 579 additions and 31 deletions.
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 // 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
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
52 changes: 21 additions & 31 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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"
)
Expand All @@ -27,14 +29,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 Down Expand Up @@ -143,17 +147,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{
Expand Down Expand Up @@ -182,19 +175,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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/devfile/parser/data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading

0 comments on commit 75bb643

Please sign in to comment.