Skip to content

Commit 4fea03e

Browse files
prietyc123kadel
authored andcommitted
Writing unit tests using Gomock (redhat-developer#4110)
* Adding unit tests and mocks for interface and nested interface using gomock and testify * Add testify latest verion in go mod * Added vendor for dependencies * Added examples with both gomock and testify for interface and embeded interface * removed testify tests and mocks
1 parent 2aaf39d commit 4fea03e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+17472
-350
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1616
github.com/go-git/go-git/v5 v5.1.0
1717
github.com/gobwas/glob v0.2.4-0.20181002190808-e7a84e9525fe
18-
github.com/golang/mock v1.4.3
18+
github.com/golang/mock v1.4.4
1919
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
2020
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
2121
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
@@ -41,7 +41,7 @@ require (
4141
github.com/spf13/afero v1.2.2
4242
github.com/spf13/cobra v0.0.5
4343
github.com/spf13/pflag v1.0.5
44-
github.com/stretchr/testify v1.4.0
44+
github.com/stretchr/testify v1.6.1
4545
github.com/xeipuuv/gojsonschema v1.2.0
4646
github.com/zalando/go-keyring v0.1.0
4747
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9

go.sum

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ github.com/golang/mock v1.0.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
429429
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
430430
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
431431
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
432-
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
433-
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
432+
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
433+
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
434434
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
435435
github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
436436
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -954,6 +954,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
954954
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
955955
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
956956
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
957+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
958+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
957959
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
958960
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
959961
github.com/tchap/go-patricia v2.3.0+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
@@ -1170,7 +1172,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
11701172
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
11711173
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
11721174
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1173-
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
11741175
golang.org/x/text v0.0.0-20170915090833-1cbadb444a80/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
11751176
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
11761177
golang.org/x/text v0.3.1-0.20171227012246-e19ae1496984/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -1327,6 +1328,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
13271328
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
13281329
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
13291330
gopkg.in/yaml.v3 v3.0.0-20190905181640-827449938966/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1331+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1332+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13301333
gotest.tools v0.0.0-20190624233834-05ebafbffc79/go.mod h1:R//lfYlUuTOTfblYI3lGoAAAebUdzjvbmQsuB7Ykd90=
13311334
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
13321335
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
@@ -1407,8 +1410,6 @@ mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jC
14071410
mvdan.cc/unparam v0.0.0-20190209190245-fbb59629db34/go.mod h1:H6SUd1XjIs+qQCyskXg5OFSrilMRUkD8ePJpHKDPaeY=
14081411
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
14091412
rsc.io/letsencrypt v0.0.1/go.mod h1:buyQKZ6IXrRnB7TdkHP0RyEybLx18HHyOSoTyoOLqNY=
1410-
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
1411-
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
14121413
sigs.k8s.io/controller-runtime v0.1.10/go.mod h1:HFAYoOh6XMV+jKF1UjFwrknPbowfyHEHHRdJMf2jMX8=
14131414
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
14141415
sigs.k8s.io/controller-runtime v0.6.0 h1:Fzna3DY7c4BIP6KwfSlrfnj20DJ+SeMBK8HSFvOk9NM=

pkg/component/component_test.go

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ package component
33
import (
44
"fmt"
55
"io/ioutil"
6-
v1 "k8s.io/api/apps/v1"
76
"os"
87
"path/filepath"
98
"reflect"
109
"regexp"
1110
"sort"
1211
"testing"
1312

13+
v1 "k8s.io/api/apps/v1"
14+
1415
"github.com/openshift/odo/pkg/util"
1516

17+
"github.com/golang/mock/gomock"
1618
applabels "github.com/openshift/odo/pkg/application/labels"
1719
componentlabels "github.com/openshift/odo/pkg/component/labels"
1820
"github.com/openshift/odo/pkg/config"
21+
"github.com/openshift/odo/pkg/envinfo"
1922
"github.com/openshift/odo/pkg/occlient"
2023
"github.com/openshift/odo/pkg/testingutil"
2124

@@ -30,6 +33,118 @@ import (
3033
. "github.com/openshift/odo/pkg/config"
3134
)
3235

36+
func TestGetComponentFrom(t *testing.T) {
37+
type cmpSetting struct {
38+
componentName string
39+
project string
40+
applicationName string
41+
debugPort int
42+
}
43+
tests := []struct {
44+
name string
45+
isEnvInfo bool
46+
componentType string
47+
envURL []envinfo.EnvInfoURL
48+
cmpSetting cmpSetting
49+
want Component
50+
}{
51+
{
52+
name: "Case 1: Get component when env info file exists",
53+
isEnvInfo: true,
54+
componentType: "nodejs",
55+
envURL: []envinfo.EnvInfoURL{
56+
{
57+
Name: "url1",
58+
},
59+
},
60+
cmpSetting: cmpSetting{
61+
componentName: "frontend",
62+
project: "project1",
63+
applicationName: "testing",
64+
debugPort: 1234,
65+
},
66+
want: Component{
67+
TypeMeta: metav1.TypeMeta{
68+
Kind: "Component",
69+
APIVersion: "odo.dev/v1alpha1",
70+
},
71+
ObjectMeta: metav1.ObjectMeta{
72+
Name: "frontend",
73+
},
74+
Spec: ComponentSpec{
75+
Type: "nodejs",
76+
},
77+
Status: ComponentStatus{},
78+
},
79+
},
80+
81+
{
82+
name: "Case 2: Get component when env info file does not exists",
83+
isEnvInfo: false,
84+
componentType: "nodejs",
85+
envURL: []envinfo.EnvInfoURL{
86+
{
87+
Name: "url2",
88+
},
89+
},
90+
cmpSetting: cmpSetting{
91+
componentName: "backend",
92+
project: "project2",
93+
applicationName: "app1",
94+
debugPort: 5896,
95+
},
96+
want: Component{},
97+
},
98+
}
99+
100+
for _, tt := range tests {
101+
t.Run(tt.name, func(t *testing.T) {
102+
103+
ctrl := gomock.NewController(t)
104+
defer ctrl.Finish()
105+
106+
mockLocalConfigProvider := envinfo.NewMockLocalConfigProvider(ctrl)
107+
108+
mockLocalConfigProvider.EXPECT().Exists().Return(tt.isEnvInfo)
109+
110+
if tt.isEnvInfo {
111+
mockLocalConfigProvider.EXPECT().GetName().Return(tt.cmpSetting.componentName)
112+
113+
component := getMachineReadableFormat(tt.cmpSetting.componentName, tt.componentType)
114+
115+
mockLocalConfigProvider.EXPECT().GetNamespace().Return(tt.cmpSetting.project)
116+
117+
component.Namespace = tt.cmpSetting.project
118+
mockLocalConfigProvider.EXPECT().GetApplication().Return(tt.cmpSetting.applicationName)
119+
mockLocalConfigProvider.EXPECT().GetDebugPort().Return(tt.cmpSetting.debugPort)
120+
121+
component.Spec = ComponentSpec{
122+
App: tt.cmpSetting.applicationName,
123+
Type: tt.componentType,
124+
Ports: []string{fmt.Sprintf("%d", tt.cmpSetting.debugPort)},
125+
}
126+
127+
mockLocalConfigProvider.EXPECT().GetURL().Return(tt.envURL)
128+
129+
if len(tt.envURL) > 0 {
130+
for _, url := range tt.envURL {
131+
component.Spec.URL = append(component.Spec.URL, url.Name)
132+
}
133+
}
134+
135+
tt.want = component
136+
137+
}
138+
139+
got := getComponentFrom(mockLocalConfigProvider, tt.componentType)
140+
if !reflect.DeepEqual(got, tt.want) {
141+
t.Errorf("getComponentFrom() = %v, want %v", got, tt.want)
142+
}
143+
144+
})
145+
}
146+
}
147+
33148
func TestGetS2IPaths(t *testing.T) {
34149

35150
tests := []struct {

pkg/envinfo/LocalConfigProviderMocks.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/stretchr/objx/.codeclimate.yml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/stretchr/objx/.gitignore

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/stretchr/objx/.travis.yml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)