func TestReflectorListAndWatch(t *testing.T) { createdFakes := make(chan *watch.FakeWatcher) // The ListFunc says that it's at revision 1. Therefore, we expect our WatchFunc // to get called at the beginning of the watch with 1, and again with 3 when we // inject an error. expectedRVs := []string{"1", "3"} lw := &testLW{ WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { rv := options.ResourceVersion fw := watch.NewFake() if e, a := expectedRVs[0], rv; e != a { t.Errorf("Expected rv %v, but got %v", e, a) } expectedRVs = expectedRVs[1:] // channel is not buffered because the for loop below needs to block. But // we don't want to block here, so report the new fake via a go routine. go func() { createdFakes <- fw }() return fw, nil }, ListFunc: func(options v1.ListOptions) (runtime.Object, error) { return &v1.PodList{ListMeta: metav1.ListMeta{ResourceVersion: "1"}}, nil }, } s := NewFIFO(MetaNamespaceKeyFunc) r := NewReflector(lw, &v1.Pod{}, s, 0) go r.ListAndWatch(wait.NeverStop) ids := []string{"foo", "bar", "baz", "qux", "zoo"} var fw *watch.FakeWatcher for i, id := range ids { if fw == nil { fw = <-createdFakes } sendingRV := strconv.FormatUint(uint64(i+2), 10) fw.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: id, ResourceVersion: sendingRV}}) if sendingRV == "3" { // Inject a failure. fw.Stop() fw = nil } } // Verify we received the right ids with the right resource versions. for i, id := range ids { pod := Pop(s).(*v1.Pod) if e, a := id, pod.Name; e != a { t.Errorf("%v: Expected %v, got %v", i, e, a) } if e, a := strconv.FormatUint(uint64(i+2), 10), pod.ResourceVersion; e != a { t.Errorf("%v: Expected %v, got %v", i, e, a) } } if len(expectedRVs) != 0 { t.Error("called watchStarter an unexpected number of times") } }
func TestWatchRead(t *testing.T) { simpleStorage := &SimpleRESTStorage{} _ = rest.Watcher(simpleStorage) // Give compile error if this doesn't work. handler := handle(map[string]rest.Storage{"simples": simpleStorage}) server := httptest.NewServer(handler) defer server.Close() dest, _ := url.Parse(server.URL) dest.Path = "/" + prefix + "/" + testGroupVersion.Group + "/" + testGroupVersion.Version + "/simples" dest.RawQuery = "watch=1" connectHTTP := func(accept string) (io.ReadCloser, string) { client := http.Client{} request, err := http.NewRequest("GET", dest.String(), nil) if err != nil { t.Fatalf("unexpected error: %v", err) } request.Header.Add("Accept", accept) response, err := client.Do(request) if err != nil { t.Fatalf("unexpected error: %v", err) } if response.StatusCode != http.StatusOK { b, _ := ioutil.ReadAll(response.Body) t.Fatalf("Unexpected response for accept: %q: %#v\n%s", accept, response, string(b)) } return response.Body, response.Header.Get("Content-Type") } connectWebSocket := func(accept string) (io.ReadCloser, string) { dest := *dest dest.Scheme = "ws" // Required by websocket, though the server never sees it. config, err := websocket.NewConfig(dest.String(), "http://localhost") if err != nil { t.Fatalf("unexpected error: %v", err) } config.Header.Add("Accept", accept) ws, err := websocket.DialConfig(config) if err != nil { t.Fatalf("unexpected error: %v", err) } return ws, "__default__" } testCases := []struct { Accept string ExpectedContentType string MediaType string }{ { Accept: "application/json", ExpectedContentType: "application/json", MediaType: "application/json", }, { Accept: "application/json;stream=watch", ExpectedContentType: "application/json", // legacy behavior MediaType: "application/json", }, // TODO: yaml stream serialization requires that RawExtension.MarshalJSON // be able to understand nested encoding (since yaml calls json.Marshal // rather than yaml.Marshal, which results in the raw bytes being in yaml). // Same problem as thirdparty object. /*{ Accept: "application/yaml", ExpectedContentType: "application/yaml;stream=watch", MediaType: "application/yaml", },*/ { Accept: "application/vnd.kubernetes.protobuf", ExpectedContentType: "application/vnd.kubernetes.protobuf;stream=watch", MediaType: "application/vnd.kubernetes.protobuf", }, { Accept: "application/vnd.kubernetes.protobuf;stream=watch", ExpectedContentType: "application/vnd.kubernetes.protobuf;stream=watch", MediaType: "application/vnd.kubernetes.protobuf", }, } protocols := []struct { name string selfFraming bool fn func(string) (io.ReadCloser, string) }{ {name: "http", fn: connectHTTP}, {name: "websocket", selfFraming: true, fn: connectWebSocket}, } for _, protocol := range protocols { for _, test := range testCases { info, ok := runtime.SerializerInfoForMediaType(api.Codecs.SupportedMediaTypes(), test.MediaType) if !ok || info.StreamSerializer == nil { t.Fatal(info) } streamSerializer := info.StreamSerializer r, contentType := protocol.fn(test.Accept) defer r.Close() if contentType != "__default__" && contentType != test.ExpectedContentType { t.Errorf("Unexpected content type: %#v", contentType) } objectCodec := api.Codecs.DecoderToVersion(info.Serializer, testInternalGroupVersion) var fr io.ReadCloser = r if !protocol.selfFraming { fr = streamSerializer.Framer.NewFrameReader(r) } d := streaming.NewDecoder(fr, streamSerializer.Serializer) var w *watch.FakeWatcher for w == nil { w = simpleStorage.Watcher() time.Sleep(time.Millisecond) } for i, item := range podWatchTestTable { action, object := item.t, item.obj name := fmt.Sprintf("%s-%s-%d", protocol.name, test.MediaType, i) // Send w.Action(action, object) // Test receive var got metav1.WatchEvent _, _, err := d.Decode(nil, &got) if err != nil { t.Fatalf("%s: Unexpected error: %v", name, err) } if got.Type != string(action) { t.Errorf("%s: Unexpected type: %v", name, got.Type) } gotObj, err := runtime.Decode(objectCodec, got.Object.Raw) if err != nil { t.Fatalf("%s: Decode error: %v", name, err) } if _, err := api.GetReference(gotObj); err != nil { t.Errorf("%s: Unable to construct reference: %v", name, err) } if e, a := object, gotObj; !api.Semantic.DeepEqual(e, a) { t.Errorf("%s: different: %s", name, diff.ObjectDiff(e, a)) } } w.Stop() var got metav1.WatchEvent _, _, err := d.Decode(nil, &got) if err == nil { t.Errorf("Unexpected non-error") } r.Close() } } }