MCPcopy
hub / github.com/kubernetes/client-go / TestWatch

Function TestWatch

rest/request_test.go:1769–1822  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1767}
1768
1769func TestWatch(t *testing.T) {
1770 var table = []struct {
1771 t watch.EventType
1772 obj runtime.Object
1773 }{
1774 {watch.Added, &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "first"}}},
1775 {watch.Modified, &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "second"}}},
1776 {watch.Deleted, &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "last"}}},
1777 }
1778
1779 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1780 flusher, ok := w.(http.Flusher)
1781 if !ok {
1782 panic("need flusher!")
1783 }
1784
1785 w.Header().Set("Transfer-Encoding", "chunked")
1786 w.WriteHeader(http.StatusOK)
1787 flusher.Flush()
1788
1789 encoder := restclientwatch.NewEncoder(streaming.NewEncoder(w, scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion)), scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion))
1790 for _, item := range table {
1791 if err := encoder.Encode(&watch.Event{Type: item.t, Object: item.obj}); err != nil {
1792 panic(err)
1793 }
1794 flusher.Flush()
1795 }
1796 }))
1797 defer testServer.Close()
1798
1799 s := testRESTClient(t, testServer)
1800 watching, err := s.Get().Prefix("path/to/watch/thing").Watch()
1801 if err != nil {
1802 t.Fatalf("Unexpected error")
1803 }
1804
1805 for _, item := range table {
1806 got, ok := <-watching.ResultChan()
1807 if !ok {
1808 t.Fatalf("Unexpected early close")
1809 }
1810 if e, a := item.t, got.Type; e != a {
1811 t.Errorf("Expected %v, got %v", e, a)
1812 }
1813 if e, a := item.obj, got.Object; !apiequality.Semantic.DeepDerivative(e, a) {
1814 t.Errorf("Expected %v, got %v", e, a)
1815 }
1816 }
1817
1818 _, ok := <-watching.ResultChan()
1819 if ok {
1820 t.Fatal("Unexpected non-close")
1821 }
1822}
1823
1824func TestStream(t *testing.T) {
1825 expectedBody := "expected body"

Callers

nothing calls this directly

Calls 9

EncodeMethod · 0.95
testRESTClientFunction · 0.85
PrefixMethod · 0.80
SetMethod · 0.65
CloseMethod · 0.65
WatchMethod · 0.65
GetMethod · 0.65
ErrorfMethod · 0.65
ResultChanMethod · 0.45

Tested by

no test coverage detected