@@ -17,9 +17,11 @@ limitations under the License.
17
17
package client
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
21
22
"encoding/json"
22
23
"fmt"
24
+ "io"
23
25
"net/http"
24
26
"reflect"
25
27
"testing"
@@ -32,6 +34,7 @@ import (
32
34
"k8s.io/apimachinery/pkg/fields"
33
35
"k8s.io/apimachinery/pkg/runtime"
34
36
"k8s.io/apimachinery/pkg/runtime/schema"
37
+ cbor "k8s.io/apimachinery/pkg/runtime/serializer/cbor/direct"
35
38
"k8s.io/apimachinery/pkg/types"
36
39
"k8s.io/apimachinery/pkg/util/wait"
37
40
"k8s.io/apimachinery/pkg/watch"
@@ -137,6 +140,59 @@ func TestDynamicClientWatch(t *testing.T) {
137
140
t .Fatalf ("unexpected error creating dynamic client: %v" , err )
138
141
}
139
142
143
+ testDynamicClientWatch (t , client , dynamicClient )
144
+ }
145
+
146
+ func TestDynamicClientWatchWithCBOR (t * testing.T ) {
147
+ framework .EnableCBORServingAndStorageForTest (t )
148
+ framework .SetTestOnlyCBORClientFeatureGatesForTest (t , true , true )
149
+
150
+ result := kubeapiservertesting .StartTestServerOrDie (t , nil , framework .DefaultTestServerFlags (), framework .SharedEtcd ())
151
+ defer result .TearDownFn ()
152
+
153
+ client := clientset .NewForConfigOrDie (result .ClientConfig )
154
+ dynamicClientConfig := rest .CopyConfig (result .ClientConfig )
155
+ dynamicClientConfig .Wrap (framework .AssertRequestResponseAsCBOR (t ))
156
+ dynamicClientConfig .Wrap (func (rt http.RoundTripper ) http.RoundTripper {
157
+ return roundTripperFunc (func (request * http.Request ) (* http.Response , error ) {
158
+ response , rterr := rt .RoundTrip (request )
159
+ if rterr != nil {
160
+ return response , rterr
161
+ }
162
+
163
+ // We can't synchronously inspect streaming responses, so tee to a buffer
164
+ // and inspect it at the end of the test.
165
+ var buf bytes.Buffer
166
+ response .Body = struct {
167
+ io.Reader
168
+ io.Closer
169
+ }{
170
+ Reader : io .TeeReader (response .Body , & buf ),
171
+ Closer : response .Body ,
172
+ }
173
+ t .Cleanup (func () {
174
+ var event metav1.WatchEvent
175
+ if err := cbor .Unmarshal (buf .Bytes (), & event ); err != nil {
176
+ t .Errorf ("non-cbor event: 0x%x" , buf .Bytes ())
177
+ return
178
+ }
179
+ if err := cbor .Unmarshal (event .Object .Raw , new (interface {})); err != nil {
180
+ t .Errorf ("non-cbor event object: 0x%x" , buf .Bytes ())
181
+ }
182
+ })
183
+
184
+ return response , rterr
185
+ })
186
+ })
187
+ dynamicClient , err := dynamic .NewForConfig (dynamicClientConfig )
188
+ if err != nil {
189
+ t .Fatalf ("unexpected error creating dynamic client: %v" , err )
190
+ }
191
+
192
+ testDynamicClientWatch (t , client , dynamicClient )
193
+ }
194
+
195
+ func testDynamicClientWatch (t * testing.T , client clientset.Interface , dynamicClient dynamic.Interface ) {
140
196
resource := corev1 .SchemeGroupVersion .WithResource ("events" )
141
197
142
198
mkEvent := func (i int ) * corev1.Event {
0 commit comments