@@ -18,9 +18,11 @@ package fake
18
18
19
19
import (
20
20
"context"
21
- "github.com/stretchr/testify/assert"
22
21
"testing"
23
22
23
+ "github.com/google/go-cmp/cmp"
24
+ "github.com/stretchr/testify/require"
25
+
24
26
v1 "k8s.io/api/core/v1"
25
27
policy "k8s.io/api/policy/v1"
26
28
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,35 +31,36 @@ import (
29
31
30
32
func TestNewSimpleClientset (t * testing.T ) {
31
33
client := NewSimpleClientset ()
32
- client .CoreV1 ().Pods ("default" ).Create (context .Background (), & v1.Pod {
34
+ expectedPods := []* v1.Pod {}
35
+
36
+ pod , err := client .CoreV1 ().Pods ("default" ).Create (context .Background (), & v1.Pod {
33
37
ObjectMeta : meta_v1.ObjectMeta {
34
38
Name : "pod-1" ,
35
39
Namespace : "default" ,
36
40
},
37
41
}, meta_v1.CreateOptions {})
38
- client .CoreV1 ().Pods ("default" ).Create (context .Background (), & v1.Pod {
42
+ require .NoError (t , err )
43
+ expectedPods = append (expectedPods , pod )
44
+
45
+ pod , err = client .CoreV1 ().Pods ("default" ).Create (context .Background (), & v1.Pod {
39
46
ObjectMeta : meta_v1.ObjectMeta {
40
47
Name : "pod-2" ,
41
48
Namespace : "default" ,
42
49
},
43
50
}, meta_v1.CreateOptions {})
44
- err := client .CoreV1 ().Pods ("default" ).EvictV1 (context .Background (), & policy.Eviction {
51
+ require .NoError (t , err )
52
+ expectedPods = append (expectedPods , pod )
53
+
54
+ err = client .CoreV1 ().Pods ("default" ).EvictV1 (context .Background (), & policy.Eviction {
45
55
ObjectMeta : meta_v1.ObjectMeta {
46
56
Name : "pod-2" ,
47
57
},
48
58
})
49
-
50
- if err != nil {
51
- t .Errorf ("TestNewSimpleClientset() res = %v" , err .Error ())
52
- }
59
+ require .NoError (t , err )
53
60
54
61
pods , err := client .CoreV1 ().Pods ("default" ).List (context .Background (), meta_v1.ListOptions {})
55
- // err: item[0]: can't assign or convert v1beta1.Eviction into v1.Pod
56
- if err != nil {
57
- t .Errorf ("TestNewSimpleClientset() res = %v" , err .Error ())
58
- } else {
59
- t .Logf ("TestNewSimpleClientset() res = %v" , pods )
60
- }
62
+ require .NoError (t , err )
63
+ cmp .Equal (expectedPods , pods .Items )
61
64
}
62
65
63
66
func TestManagedFieldClientset (t * testing.T ) {
@@ -69,58 +72,47 @@ func TestManagedFieldClientset(t *testing.T) {
69
72
ObjectMeta : meta_v1.ObjectMeta {Name : name , Namespace : namespace },
70
73
Data : map [string ]string {"k0" : "v0" },
71
74
}, meta_v1.CreateOptions {FieldManager : "test-manager-0" })
72
- if err != nil {
73
- t .Errorf ("Failed to create pod: %v" , err )
74
- }
75
- assert .Equal (t , map [string ]string {"k0" : "v0" }, cm .Data )
75
+ require .NoError (t , err )
76
+ require .Equal (t , map [string ]string {"k0" : "v0" }, cm .Data )
76
77
77
78
// Apply with test-manager-1
78
79
// Expect data to be shared with initial create
79
80
cm , err = client .CoreV1 ().ConfigMaps ("default" ).Apply (context .Background (),
80
81
v1ac .ConfigMap (name , namespace ).WithData (map [string ]string {"k1" : "v1" }),
81
82
meta_v1.ApplyOptions {FieldManager : "test-manager-1" })
82
- if err != nil {
83
- t .Errorf ("Failed to create pod: %v" , err )
84
- }
85
- assert .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v1" }, cm .Data )
83
+ require .NoError (t , err )
84
+ require .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v1" }, cm .Data )
86
85
87
86
// Apply conflicting with test-manager-2, expect apply to fail
88
87
_ , err = client .CoreV1 ().ConfigMaps ("default" ).Apply (context .Background (),
89
88
v1ac .ConfigMap (name , namespace ).WithData (map [string ]string {"k1" : "xyz" }),
90
89
meta_v1.ApplyOptions {FieldManager : "test-manager-2" })
91
- if assert .Error (t , err ) {
92
- assert .Equal (t , "Apply failed with 1 conflict: conflict with \" test-manager-1\" : .data.k1" , err .Error ())
93
- }
90
+ require .Error (t , err )
91
+ require .Equal (t , "Apply failed with 1 conflict: conflict with \" test-manager-1\" : .data.k1" , err .Error ())
94
92
95
93
// Apply with test-manager-2
96
94
// Expect data to be shared with initial create and test-manager-1
97
95
cm , err = client .CoreV1 ().ConfigMaps ("default" ).Apply (context .Background (),
98
96
v1ac .ConfigMap (name , namespace ).WithData (map [string ]string {"k2" : "v2" }),
99
97
meta_v1.ApplyOptions {FieldManager : "test-manager-2" })
100
- if err != nil {
101
- t .Errorf ("Failed to create pod: %v" , err )
102
- }
103
- assert .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v1" , "k2" : "v2" }, cm .Data )
98
+ require .NoError (t , err )
99
+ require .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v1" , "k2" : "v2" }, cm .Data )
104
100
105
101
// Apply with test-manager-1
106
102
// Expect owned data to be updated
107
103
cm , err = client .CoreV1 ().ConfigMaps ("default" ).Apply (context .Background (),
108
104
v1ac .ConfigMap (name , namespace ).WithData (map [string ]string {"k1" : "v101" }),
109
105
meta_v1.ApplyOptions {FieldManager : "test-manager-1" })
110
- if err != nil {
111
- t .Errorf ("Failed to create pod: %v" , err )
112
- }
113
- assert .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v101" , "k2" : "v2" }, cm .Data )
106
+ require .NoError (t , err )
107
+ require .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v101" , "k2" : "v2" }, cm .Data )
114
108
115
109
// Force apply with test-manager-2
116
110
// Expect data owned by test-manager-1 to be updated, expect data already owned but not in apply configuration to be removed
117
111
cm , err = client .CoreV1 ().ConfigMaps ("default" ).Apply (context .Background (),
118
112
v1ac .ConfigMap (name , namespace ).WithData (map [string ]string {"k1" : "v202" }),
119
113
meta_v1.ApplyOptions {FieldManager : "test-manager-2" , Force : true })
120
- if err != nil {
121
- t .Errorf ("Failed to create pod: %v" , err )
122
- }
123
- assert .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v202" }, cm .Data )
114
+ require .NoError (t , err )
115
+ require .Equal (t , map [string ]string {"k0" : "v0" , "k1" : "v202" }, cm .Data )
124
116
125
117
// Update with test-manager-1 to perform a force update of the entire resource
126
118
cm , err = client .CoreV1 ().ConfigMaps ("default" ).Update (context .Background (),
@@ -137,8 +129,6 @@ func TestManagedFieldClientset(t *testing.T) {
137
129
"k99" : "v99" ,
138
130
},
139
131
}, meta_v1.UpdateOptions {FieldManager : "test-manager-0" })
140
- if err != nil {
141
- t .Errorf ("Failed to update pod: %v" , err )
142
- }
143
- assert .Equal (t , map [string ]string {"k99" : "v99" }, cm .Data )
132
+ require .NoError (t , err )
133
+ require .Equal (t , map [string ]string {"k99" : "v99" }, cm .Data )
144
134
}
0 commit comments