Skip to content

Commit 1071d14

Browse files
add unit tests for client
Signed-off-by: Xudong Liu <[email protected]>
1 parent 7cc41d5 commit 1071d14

File tree

2 files changed

+650
-0
lines changed

2 files changed

+650
-0
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
vmopv1alpha1 "github.com/vmware-tanzu/vm-operator-api/api/v1alpha1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/runtime"
12+
clientgotesting "k8s.io/client-go/testing"
13+
14+
dynamicfake "k8s.io/client-go/dynamic/fake"
15+
)
16+
17+
func initVMTest() (*virtualMachines, *dynamicfake.FakeDynamicClient) {
18+
scheme := runtime.NewScheme()
19+
_ = vmopv1alpha1.AddToScheme(scheme)
20+
fc := dynamicfake.NewSimpleDynamicClient(scheme)
21+
vms := newVirtualMachines(NewFakeClient(fc), "test-ns")
22+
return vms, fc
23+
}
24+
25+
func TestVMCreate(t *testing.T) {
26+
vms, fc := initVMTest()
27+
testCases := []struct {
28+
name string
29+
virtualMachine *vmopv1alpha1.VirtualMachine
30+
createFunc func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
31+
expectedVM *vmopv1alpha1.VirtualMachine
32+
expectedErr bool
33+
}{
34+
{
35+
name: "Create: when everything is ok",
36+
virtualMachine: &vmopv1alpha1.VirtualMachine{
37+
Spec: vmopv1alpha1.VirtualMachineSpec{
38+
ImageName: "test-image",
39+
},
40+
},
41+
expectedVM: &vmopv1alpha1.VirtualMachine{
42+
Spec: vmopv1alpha1.VirtualMachineSpec{
43+
ImageName: "test-image",
44+
},
45+
},
46+
},
47+
{
48+
name: "Create: when create error",
49+
virtualMachine: &vmopv1alpha1.VirtualMachine{
50+
Spec: vmopv1alpha1.VirtualMachineSpec{
51+
ImageName: "test-image",
52+
},
53+
},
54+
createFunc: func(action clientgotesting.Action) (bool, runtime.Object, error) {
55+
return true, nil, fmt.Errorf("test error")
56+
},
57+
expectedVM: nil,
58+
expectedErr: true,
59+
},
60+
}
61+
62+
for _, testCase := range testCases {
63+
t.Run(testCase.name, func(t *testing.T) {
64+
if testCase.createFunc != nil {
65+
fc.PrependReactor("create", "*", testCase.createFunc)
66+
}
67+
actualVM, err := vms.Create(context.Background(), testCase.virtualMachine, metav1.CreateOptions{})
68+
if testCase.expectedErr {
69+
assert.Error(t, err)
70+
} else {
71+
assert.NoError(t, err)
72+
assert.Equal(t, testCase.expectedVM.Spec, actualVM.Spec)
73+
}
74+
})
75+
}
76+
}
77+
78+
func TestVMUpdate(t *testing.T) {
79+
vms, fc := initVMTest()
80+
testCases := []struct {
81+
name string
82+
oldVM *vmopv1alpha1.VirtualMachine
83+
newVM *vmopv1alpha1.VirtualMachine
84+
updateFunc func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
85+
expectedVM *vmopv1alpha1.VirtualMachine
86+
expectedErr bool
87+
}{
88+
{
89+
name: "Update: when everything is ok",
90+
oldVM: &vmopv1alpha1.VirtualMachine{
91+
ObjectMeta: metav1.ObjectMeta{
92+
Name: "test-vm",
93+
},
94+
Spec: vmopv1alpha1.VirtualMachineSpec{
95+
ImageName: "test-image",
96+
},
97+
},
98+
newVM: &vmopv1alpha1.VirtualMachine{
99+
ObjectMeta: metav1.ObjectMeta{
100+
Name: "test-vm",
101+
},
102+
Spec: vmopv1alpha1.VirtualMachineSpec{
103+
ImageName: "test-image-2",
104+
},
105+
},
106+
expectedVM: &vmopv1alpha1.VirtualMachine{
107+
ObjectMeta: metav1.ObjectMeta{
108+
Name: "test-vm",
109+
},
110+
Spec: vmopv1alpha1.VirtualMachineSpec{
111+
ImageName: "test-image-2",
112+
},
113+
},
114+
},
115+
{
116+
name: "Update: when update error",
117+
oldVM: &vmopv1alpha1.VirtualMachine{
118+
Spec: vmopv1alpha1.VirtualMachineSpec{
119+
ImageName: "test-image",
120+
},
121+
},
122+
newVM: &vmopv1alpha1.VirtualMachine{
123+
Spec: vmopv1alpha1.VirtualMachineSpec{
124+
ImageName: "test-image",
125+
},
126+
},
127+
updateFunc: func(action clientgotesting.Action) (bool, runtime.Object, error) {
128+
return true, nil, fmt.Errorf("test error")
129+
},
130+
expectedVM: nil,
131+
expectedErr: true,
132+
},
133+
}
134+
135+
for _, testCase := range testCases {
136+
t.Run(testCase.name, func(t *testing.T) {
137+
_, err := vms.Create(context.Background(), testCase.oldVM, metav1.CreateOptions{})
138+
assert.NoError(t, err)
139+
if testCase.updateFunc != nil {
140+
fc.PrependReactor("update", "*", testCase.updateFunc)
141+
}
142+
updatedVM, err := vms.Update(context.Background(), testCase.newVM, metav1.UpdateOptions{})
143+
if testCase.expectedErr {
144+
assert.Error(t, err)
145+
} else {
146+
assert.NoError(t, err)
147+
assert.Equal(t, testCase.expectedVM.Spec, updatedVM.Spec)
148+
}
149+
})
150+
}
151+
}
152+
153+
func TestVMDelete(t *testing.T) {
154+
vms, fc := initVMTest()
155+
testCases := []struct {
156+
name string
157+
virtualMachine *vmopv1alpha1.VirtualMachine
158+
deleteFunc func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
159+
expectedErr bool
160+
}{
161+
{
162+
name: "Delete: when everything is ok",
163+
virtualMachine: &vmopv1alpha1.VirtualMachine{
164+
ObjectMeta: metav1.ObjectMeta{
165+
Name: "test-vm",
166+
},
167+
Spec: vmopv1alpha1.VirtualMachineSpec{
168+
ImageName: "test-image",
169+
},
170+
},
171+
},
172+
{
173+
name: "Create: when delete error",
174+
virtualMachine: &vmopv1alpha1.VirtualMachine{
175+
ObjectMeta: metav1.ObjectMeta{
176+
Name: "test-vm",
177+
},
178+
Spec: vmopv1alpha1.VirtualMachineSpec{
179+
ImageName: "test-image",
180+
},
181+
},
182+
deleteFunc: func(action clientgotesting.Action) (bool, runtime.Object, error) {
183+
return true, nil, fmt.Errorf("test error")
184+
},
185+
expectedErr: true,
186+
},
187+
}
188+
189+
for _, testCase := range testCases {
190+
t.Run(testCase.name, func(t *testing.T) {
191+
_, err := vms.Create(context.Background(), testCase.virtualMachine, metav1.CreateOptions{})
192+
assert.NoError(t, err)
193+
if testCase.deleteFunc != nil {
194+
fc.PrependReactor("delete", "*", testCase.deleteFunc)
195+
}
196+
err = vms.Delete(context.Background(), testCase.virtualMachine.Name, metav1.DeleteOptions{})
197+
if testCase.expectedErr {
198+
assert.Error(t, err)
199+
} else {
200+
assert.NoError(t, err)
201+
}
202+
})
203+
}
204+
}
205+
206+
func TestVMGet(t *testing.T) {
207+
vms, fc := initVMTest()
208+
testCases := []struct {
209+
name string
210+
virtualMachine *vmopv1alpha1.VirtualMachine
211+
getFunc func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
212+
expectedVM *vmopv1alpha1.VirtualMachine
213+
expectedErr bool
214+
}{
215+
{
216+
name: "Get: when everything is ok",
217+
virtualMachine: &vmopv1alpha1.VirtualMachine{
218+
ObjectMeta: metav1.ObjectMeta{
219+
Name: "test-vm",
220+
},
221+
Spec: vmopv1alpha1.VirtualMachineSpec{
222+
ImageName: "test-image",
223+
},
224+
},
225+
expectedVM: &vmopv1alpha1.VirtualMachine{
226+
ObjectMeta: metav1.ObjectMeta{
227+
Name: "test-vm",
228+
},
229+
Spec: vmopv1alpha1.VirtualMachineSpec{
230+
ImageName: "test-image",
231+
},
232+
},
233+
},
234+
{
235+
name: "Get: when get error",
236+
virtualMachine: &vmopv1alpha1.VirtualMachine{
237+
ObjectMeta: metav1.ObjectMeta{
238+
Name: "test-vm-error",
239+
},
240+
Spec: vmopv1alpha1.VirtualMachineSpec{
241+
ImageName: "test-image",
242+
},
243+
},
244+
getFunc: func(action clientgotesting.Action) (bool, runtime.Object, error) {
245+
return true, nil, fmt.Errorf("test error")
246+
},
247+
expectedVM: nil,
248+
expectedErr: true,
249+
},
250+
}
251+
252+
for _, testCase := range testCases {
253+
t.Run(testCase.name, func(t *testing.T) {
254+
_, err := vms.Create(context.Background(), testCase.virtualMachine, metav1.CreateOptions{})
255+
assert.NoError(t, err)
256+
if testCase.getFunc != nil {
257+
fc.PrependReactor("get", "*", testCase.getFunc)
258+
}
259+
actualVM, err := vms.Get(context.Background(), testCase.virtualMachine.Name, metav1.GetOptions{})
260+
if testCase.expectedErr {
261+
assert.Error(t, err)
262+
} else {
263+
assert.NoError(t, err)
264+
assert.Equal(t, testCase.expectedVM.Spec, actualVM.Spec)
265+
}
266+
})
267+
}
268+
}
269+
270+
func TestVMList(t *testing.T) {
271+
vms, fc := initVMTest()
272+
testCases := []struct {
273+
name string
274+
virtualMachine *vmopv1alpha1.VirtualMachine
275+
listFunc func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error)
276+
expectedVMNum int
277+
expectedErr bool
278+
}{
279+
{
280+
name: "List: when everything is ok",
281+
virtualMachine: &vmopv1alpha1.VirtualMachine{
282+
ObjectMeta: metav1.ObjectMeta{
283+
Name: "test-vm",
284+
},
285+
Spec: vmopv1alpha1.VirtualMachineSpec{
286+
ImageName: "test-image",
287+
},
288+
},
289+
expectedVMNum: 1,
290+
},
291+
{
292+
name: "List: when list error",
293+
virtualMachine: &vmopv1alpha1.VirtualMachine{
294+
ObjectMeta: metav1.ObjectMeta{
295+
Name: "test-vm-error",
296+
},
297+
Spec: vmopv1alpha1.VirtualMachineSpec{
298+
ImageName: "test-image",
299+
},
300+
},
301+
listFunc: func(action clientgotesting.Action) (bool, runtime.Object, error) {
302+
return true, nil, fmt.Errorf("test error")
303+
},
304+
expectedVMNum: 0,
305+
expectedErr: true,
306+
},
307+
}
308+
309+
for _, testCase := range testCases {
310+
t.Run(testCase.name, func(t *testing.T) {
311+
_, err := vms.Create(context.Background(), testCase.virtualMachine, metav1.CreateOptions{})
312+
assert.NoError(t, err)
313+
if testCase.listFunc != nil {
314+
fc.PrependReactor("list", "*", testCase.listFunc)
315+
}
316+
vmList, err := vms.List(context.Background(), metav1.ListOptions{})
317+
if testCase.expectedErr {
318+
assert.Error(t, err)
319+
} else {
320+
assert.NoError(t, err)
321+
assert.Equal(t, testCase.expectedVMNum, len(vmList.Items))
322+
}
323+
})
324+
}
325+
}

0 commit comments

Comments
 (0)