Skip to content

Commit 8ef2a53

Browse files
committed
Adding unit tests and mocks for interface and nested interface using gomock and testify
1 parent 49853dc commit 8ef2a53

File tree

7 files changed

+754
-43
lines changed

7 files changed

+754
-43
lines changed

pkg/component/component_test.go

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
applabels "github.com/openshift/odo/pkg/application/labels"
1616
componentlabels "github.com/openshift/odo/pkg/component/labels"
1717
"github.com/openshift/odo/pkg/config"
18+
"github.com/openshift/odo/pkg/envinfo"
19+
"github.com/openshift/odo/pkg/envinfo/mocks"
1820
"github.com/openshift/odo/pkg/occlient"
1921
"github.com/openshift/odo/pkg/testingutil"
2022

@@ -29,6 +31,281 @@ import (
2931
. "github.com/openshift/odo/pkg/config"
3032
)
3133

34+
// func TestGomockGetComponentFrom(t *testing.T) {
35+
// tests := []struct {
36+
// name string
37+
// isEnvInfo bool
38+
// componentName string
39+
// componentType string
40+
// env []envinfo.EnvInfoURL
41+
// cmpSetting envinfo.ComponentSettings
42+
// defaultPort int
43+
// want Component
44+
// }{
45+
// {
46+
// name: "Case 1: Get component when env info file exists with nil debug port",
47+
// isEnvInfo: true,
48+
// componentType: "nodejs",
49+
// env: []envinfo.EnvInfoURL{
50+
// {
51+
// Name: "abcde",
52+
// Port: 5858,
53+
// },
54+
// },
55+
// cmpSetting: envinfo.ComponentSettings{
56+
// Name: "frontend",
57+
// Project: "test",
58+
// URL: &[]envinfo.EnvInfoURL{},
59+
// AppName: "testing",
60+
// DebugPort: nil,
61+
// },
62+
// defaultPort: 5859,
63+
// want: Component{
64+
// TypeMeta: metav1.TypeMeta{
65+
// Kind: "Component",
66+
// APIVersion: "odo.dev/v1alpha1",
67+
// },
68+
// ObjectMeta: metav1.ObjectMeta{
69+
// Name: "frontend",
70+
// },
71+
// Spec: ComponentSpec{
72+
// Type: "nodejs",
73+
// },
74+
// Status: ComponentStatus{},
75+
// },
76+
// },
77+
// }
78+
79+
// for _, tt := range tests {
80+
// t.Run(tt.name, func(t *testing.T) {
81+
82+
// ctrl := gomock.NewController(t)
83+
// defer ctrl.Finish()
84+
85+
// envInfoSpc := envinfo.EnvSpecificInfo{
86+
// EnvinfoFileExists: tt.isEnvInfo,
87+
// }
88+
89+
// mockLocalConfigProvider := envinfo.NewMockLocalConfigProvider(ctrl)
90+
91+
// envFileExists := (reflect.ValueOf(mockLocalConfigProvider.EXPECT().Exists().Return(envInfoSpc.EnvinfoFileExists))).Interface().(bool)
92+
// // envFileExists := (reflect.ValueOf(mockLocalConfigProvider.EXPECT().Exists().DoAndReturn(
93+
// // func() bool {
94+
// // return envInfoSpc.EnvinfoFileExists
95+
// // })
96+
// // )).Interface().(bool)
97+
98+
// if envFileExists {
99+
// var debugPort int
100+
// var urls []envinfo.EnvInfoURL
101+
102+
// envInfo := envinfo.EnvInfo{
103+
// ComponentSettings: tt.cmpSetting,
104+
// }
105+
106+
// mockLocalConfigProvider.EXPECT().GetName().Return(envInfo.ComponentSettings.Name)
107+
// cmpN := (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetName().Return(envInfo.ComponentSettings.Name))).Interface().(string)
108+
// comp := getMachineReadableFormat(cmpN, tt.componentType)
109+
110+
// comp.Namespace = (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetNamespace().Return(envInfo.ComponentSettings.Project))).Interface().(string)
111+
// app := (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetApplication().Return(envInfo.ComponentSettings.AppName))).Interface().(string)
112+
113+
// if envInfo.ComponentSettings.DebugPort == nil {
114+
// debugPort = (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetDebugPort().Return(tt.defaultPort))).Interface().(int)
115+
116+
// } else {
117+
// debugPort = (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetDebugPort().Return(*envInfo.ComponentSettings.DebugPort))).Interface().(int)
118+
// }
119+
120+
// comp.Spec = ComponentSpec{
121+
// App: app,
122+
// Type: tt.componentType,
123+
// Ports: []string{fmt.Sprintf("%d", debugPort)},
124+
// }
125+
126+
// if envInfo.ComponentSettings.URL == nil {
127+
// urls = (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetURL().Return(tt.env))).Interface().([]envinfo.EnvInfoURL)
128+
// } else {
129+
// urls = (reflect.ValueOf(mockLocalConfigProvider.EXPECT().GetURL().Return(*envInfo.ComponentSettings.URL))).Interface().([]envinfo.EnvInfoURL)
130+
// }
131+
132+
// if len(urls) > 0 {
133+
// for _, url := range urls {
134+
// comp.Spec.URL = append(comp.Spec.URL, url.Name)
135+
// }
136+
// }
137+
138+
// fmt.Println("hi final component is", comp)
139+
// tt.want = comp
140+
141+
// }
142+
143+
// got := getComponentFrom(mockLocalConfigProvider, tt.componentType)
144+
// if !reflect.DeepEqual(got, tt.want) {
145+
// t.Errorf("getComponentFrom() = %v, want %v", got, tt.want)
146+
// }
147+
148+
// })
149+
// }
150+
// }
151+
152+
func TestTestifyGetComponentFrom(t *testing.T) {
153+
debport := 2439
154+
tests := []struct {
155+
name string
156+
isEnvInfo bool
157+
componentName string
158+
componentType string
159+
env []envinfo.EnvInfoURL
160+
cmpSetting envinfo.ComponentSettings
161+
defaultPort int
162+
want Component
163+
}{
164+
{
165+
name: "Case 1: Get component when env info file exists with nil debug port",
166+
isEnvInfo: true,
167+
componentType: "nodejs",
168+
env: []envinfo.EnvInfoURL{
169+
{
170+
Name: "abcde",
171+
Port: 5858,
172+
},
173+
},
174+
cmpSetting: envinfo.ComponentSettings{
175+
Name: "frontend",
176+
Project: "test",
177+
URL: &[]envinfo.EnvInfoURL{},
178+
AppName: "testing",
179+
DebugPort: nil,
180+
},
181+
defaultPort: 5859,
182+
want: Component{
183+
TypeMeta: metav1.TypeMeta{
184+
Kind: "Component",
185+
APIVersion: "odo.dev/v1alpha1",
186+
},
187+
ObjectMeta: metav1.ObjectMeta{
188+
Name: "frontend",
189+
},
190+
Spec: ComponentSpec{
191+
Type: "nodejs",
192+
},
193+
Status: ComponentStatus{},
194+
},
195+
},
196+
197+
{
198+
name: "Case 2: Get component when env info file exists with debug port and url value",
199+
isEnvInfo: true,
200+
componentType: "nodejs",
201+
env: []envinfo.EnvInfoURL{
202+
{
203+
Name: "abcde",
204+
Port: 5858,
205+
},
206+
},
207+
cmpSetting: envinfo.ComponentSettings{
208+
Name: "frontend",
209+
Project: "test",
210+
URL: &[]envinfo.EnvInfoURL{
211+
{
212+
Name: "abc",
213+
Port: 5856,
214+
},
215+
},
216+
AppName: "testing",
217+
DebugPort: &debport,
218+
},
219+
defaultPort: 5859,
220+
want: Component{
221+
TypeMeta: metav1.TypeMeta{
222+
Kind: "Component",
223+
APIVersion: "odo.dev/v1alpha1",
224+
},
225+
ObjectMeta: metav1.ObjectMeta{
226+
Name: "frontend",
227+
},
228+
Spec: ComponentSpec{
229+
Type: "nodejs",
230+
},
231+
Status: ComponentStatus{},
232+
},
233+
},
234+
235+
{
236+
name: "Case 3: Get component when env info file does not exists",
237+
isEnvInfo: false,
238+
componentType: "nodejs",
239+
want: Component{},
240+
},
241+
}
242+
243+
for _, tt := range tests {
244+
t.Run(tt.name, func(t *testing.T) {
245+
246+
envInfoSpc := envinfo.EnvSpecificInfo{
247+
EnvinfoFileExists: tt.isEnvInfo,
248+
}
249+
250+
mockLocalConfig := &mocks.LocalConfigProvider{}
251+
252+
envFileExists := (reflect.ValueOf((mockLocalConfig.On("Exists").Return(envInfoSpc.EnvinfoFileExists)).ReturnArguments[0])).Interface().(bool)
253+
254+
if envFileExists {
255+
var debugPort int
256+
var urls []envinfo.EnvInfoURL
257+
258+
envInfo := envinfo.EnvInfo{
259+
ComponentSettings: tt.cmpSetting,
260+
}
261+
262+
cmpN := (reflect.ValueOf((mockLocalConfig.On("GetName").Return(envInfo.ComponentSettings.Name)).ReturnArguments[0])).Interface().(string)
263+
comp := getMachineReadableFormat(cmpN, tt.componentType)
264+
265+
comp.Namespace = (reflect.ValueOf((mockLocalConfig.On("GetNamespace").Return(envInfo.ComponentSettings.Project)).ReturnArguments[0])).Interface().(string)
266+
app := (reflect.ValueOf((mockLocalConfig.On("GetApplication").Return(envInfo.ComponentSettings.AppName)).ReturnArguments[0])).Interface().(string)
267+
268+
if envInfo.ComponentSettings.DebugPort == nil {
269+
debugPort = (reflect.ValueOf((mockLocalConfig.On("GetDebugPort").Return(tt.defaultPort)).ReturnArguments[0])).Interface().(int)
270+
271+
} else {
272+
debugPort = (reflect.ValueOf((mockLocalConfig.On("GetDebugPort").Return(*envInfo.ComponentSettings.DebugPort)).ReturnArguments[0])).Interface().(int)
273+
}
274+
275+
comp.Spec = ComponentSpec{
276+
App: app,
277+
Type: tt.componentType,
278+
Ports: []string{fmt.Sprintf("%d", debugPort)},
279+
}
280+
281+
if envInfo.ComponentSettings.URL == nil {
282+
urls = (reflect.ValueOf((mockLocalConfig.On("GetURL").Return(tt.env)).ReturnArguments[0])).Interface().([]envinfo.EnvInfoURL)
283+
} else {
284+
urls = (reflect.ValueOf((mockLocalConfig.On("GetURL").Return(*envInfo.ComponentSettings.URL)).ReturnArguments[0])).Interface().([]envinfo.EnvInfoURL)
285+
}
286+
287+
if len(urls) > 0 {
288+
for _, url := range urls {
289+
comp.Spec.URL = append(comp.Spec.URL, url.Name)
290+
}
291+
}
292+
293+
fmt.Println("hi final component is", comp)
294+
tt.want = comp
295+
296+
}
297+
298+
got := getComponentFrom(mockLocalConfig, tt.componentType)
299+
if !reflect.DeepEqual(got, tt.want) {
300+
t.Errorf("getComponentFrom() = %v, want %v", got, tt.want)
301+
}
302+
303+
mockLocalConfig.AssertExpectations(t)
304+
305+
})
306+
}
307+
}
308+
32309
func TestGetS2IPaths(t *testing.T) {
33310

34311
tests := []struct {

0 commit comments

Comments
 (0)