@@ -24,52 +24,62 @@ func init() {
24
24
k8s .Register ("" , "v1" , "configmaps" , true , & configMapJSON {})
25
25
}
26
26
27
- func testWatch (t * testing.T , client * k8s.Client , namespace string , newCM func () k8s.Resource , update func (cm k8s.Resource )) {
28
- w , err := client .Watch (context .TODO (), namespace , newCM ())
27
+ func wantEvent (t * testing.T , w * k8s.Watcher , eventType string , got , want k8s.Resource ) {
28
+ t .Helper ()
29
+ eT , err := w .Next (got )
29
30
if err != nil {
30
- t .Errorf ("watch configmaps: %v" , err )
31
+ t .Errorf ("decode watch event: %v" , err )
32
+ return
31
33
}
32
- defer w .Close ()
34
+ if eT != eventType {
35
+ t .Errorf ("expected event type %q got %q" , eventType , eT )
36
+ }
37
+ want .GetMetadata ().ResourceVersion = k8s .String ("" )
38
+ got .GetMetadata ().ResourceVersion = k8s .String ("" )
39
+ if ! reflect .DeepEqual (got , want ) {
40
+ t .Errorf ("configmaps didn't match" )
41
+ t .Errorf ("want: %#v" , want )
42
+ t .Errorf (" got: %#v" , got )
43
+ }
44
+ }
33
45
46
+ func testWatch (t * testing.T , client * k8s.Client , namespace string , r k8s.Resource , newCM func () k8s.Resource , update func (cm k8s.Resource )) {
34
47
cm := newCM ()
35
- want := func ( eventType string ) {
36
- got := newCM ()
37
- eT , err := w . Next ( got )
38
- if err != nil {
39
- t .Errorf ("decode watch event : %v" , err )
48
+
49
+ if r . GetMetadata () != nil {
50
+ // Individual watch must created beforehand
51
+ if err := client . Create ( context . TODO (), cm ); err != nil {
52
+ t .Errorf ("create configmap : %v" , err )
40
53
return
41
54
}
42
- if eT != eventType {
43
- t .Errorf ("expected event type %q got %q" , eventType , eT )
44
- }
45
- cm .GetMetadata ().ResourceVersion = k8s .String ("" )
46
- got .GetMetadata ().ResourceVersion = k8s .String ("" )
47
- if ! reflect .DeepEqual (got , cm ) {
48
- t .Errorf ("configmaps didn't match" )
49
- t .Errorf ("want: %#v" , cm )
50
- t .Errorf (" got: %#v" , got )
51
- }
52
55
}
56
+ w , err := client .Watch (context .TODO (), namespace , r )
57
+ if err != nil {
58
+ t .Fatalf ("watch configmaps: %v" , err )
59
+ }
60
+ defer w .Close ()
53
61
54
- if err := client .Create (context .TODO (), cm ); err != nil {
55
- t .Errorf ("create configmap: %v" , err )
56
- return
62
+ if r .GetMetadata () == nil {
63
+ if err := client .Create (context .TODO (), cm ); err != nil {
64
+ t .Errorf ("create configmap: %v" , err )
65
+ return
66
+ }
67
+ wantEvent (t , w , k8s .EventAdded , newCM (), cm )
57
68
}
58
- want (k8s .EventAdded )
59
69
60
70
update (cm )
61
71
62
72
if err := client .Update (context .TODO (), cm ); err != nil {
63
73
t .Errorf ("update configmap: %v" , err )
64
74
return
65
75
}
66
- want ( k8s .EventModified )
76
+ wantEvent ( t , w , k8s .EventModified , newCM (), cm )
67
77
68
78
if err := client .Delete (context .TODO (), cm ); err != nil {
69
79
t .Errorf ("Delete configmap: %v" , err )
70
80
return
71
81
}
72
- want ( k8s .EventDeleted )
82
+ wantEvent ( t , w , k8s .EventDeleted , newCM (), cm )
73
83
}
74
84
75
85
func TestWatchConfigMapJSON (t * testing.T ) {
@@ -86,7 +96,7 @@ func TestWatchConfigMapJSON(t *testing.T) {
86
96
updateCM := func (cm k8s.Resource ) {
87
97
(cm .(* configMapJSON )).Data = map [string ]string {"hello" : "world" }
88
98
}
89
- testWatch (t , client , namespace , newCM , updateCM )
99
+ testWatch (t , client , namespace , & configMapJSON {}, newCM , updateCM )
90
100
})
91
101
}
92
102
@@ -104,6 +114,24 @@ func TestWatchConfigMapProto(t *testing.T) {
104
114
updateCM := func (cm k8s.Resource ) {
105
115
(cm .(* corev1.ConfigMap )).Data = map [string ]string {"hello" : "world" }
106
116
}
107
- testWatch (t , client , namespace , newCM , updateCM )
117
+ testWatch (t , client , namespace , & corev1.ConfigMap {}, newCM , updateCM )
118
+ })
119
+ }
120
+
121
+ func TestWatchIndividualConfigMap (t * testing.T ) {
122
+ withNamespace (t , func (client * k8s.Client , namespace string ) {
123
+ newCM := func () k8s.Resource {
124
+ return & corev1.ConfigMap {
125
+ Metadata : & metav1.ObjectMeta {
126
+ Name : k8s .String ("my-configmap" ),
127
+ Namespace : & namespace ,
128
+ },
129
+ }
130
+ }
131
+
132
+ updateCM := func (cm k8s.Resource ) {
133
+ (cm .(* corev1.ConfigMap )).Data = map [string ]string {"hello" : "world" }
134
+ }
135
+ testWatch (t , client , namespace , newCM (), newCM , updateCM )
108
136
})
109
137
}
0 commit comments