Exemplo n.º 1
0
func (c *Client) Setup(t *testing.T) *Client {
	c.handler = &utiltesting.FakeHandler{
		StatusCode: c.Response.StatusCode,
	}
	if responseBody := body(t, c.Response.Body, c.Response.RawBody); responseBody != nil {
		c.handler.ResponseBody = *responseBody
	}
	c.server = httptest.NewServer(c.handler)
	if c.Client == nil {
		c.Client = client.NewOrDie(&client.Config{
			Host:          c.server.URL,
			ContentConfig: client.ContentConfig{GroupVersion: testapi.Default.GroupVersion()},
		})

		// TODO: caesarxuchao: hacky way to specify version of Experimental client.
		// We will fix this by supporting multiple group versions in Config
		c.ExtensionsClient = client.NewExtensionsOrDie(&client.Config{
			Host:          c.server.URL,
			ContentConfig: client.ContentConfig{GroupVersion: testapi.Extensions.GroupVersion()},
		})

		c.Clientset = clientset.NewForConfigOrDie(&client.Config{Host: c.server.URL})
	}
	c.QueryValidator = map[string]func(string, string) bool{}
	return c
}
Exemplo n.º 2
0
func TestListWatchesCanList(t *testing.T) {
	fieldSelectorQueryParamName := unversioned.FieldSelectorQueryParam(testapi.Default.GroupVersion().String())
	table := []struct {
		location      string
		resource      string
		namespace     string
		fieldSelector fields.Selector
	}{
		// Node
		{
			location:      testapi.Default.ResourcePath("nodes", api.NamespaceAll, ""),
			resource:      "nodes",
			namespace:     api.NamespaceAll,
			fieldSelector: parseSelectorOrDie(""),
		},
		// pod with "assigned" field selector.
		{
			location: buildLocation(
				testapi.Default.ResourcePath("pods", api.NamespaceAll, ""),
				buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
			resource:      "pods",
			namespace:     api.NamespaceAll,
			fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
		},
		// pod in namespace "foo"
		{
			location: buildLocation(
				testapi.Default.ResourcePath("pods", "foo", ""),
				buildQueryValues(url.Values{fieldSelectorQueryParamName: []string{"spec.host="}})),
			resource:      "pods",
			namespace:     "foo",
			fieldSelector: fields.Set{"spec.host": ""}.AsSelector(),
		},
	}
	for _, item := range table {
		handler := utiltesting.FakeHandler{
			StatusCode:   500,
			ResponseBody: "",
			T:            t,
		}
		server := httptest.NewServer(&handler)
		// TODO: Uncomment when fix #19254
		// defer server.Close()
		client := client.NewOrDie(&client.Config{Host: server.URL, ContentConfig: client.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}})
		lw := NewListWatchFromClient(client, item.resource, item.namespace, item.fieldSelector)
		// This test merely tests that the correct request is made.
		lw.List(api.ListOptions{})
		handler.ValidateRequest(t, item.location, "GET", nil)
	}
}
Exemplo n.º 3
0
func TestNegotiateVersion(t *testing.T) {
	tests := []struct {
		name            string
		version         *uapi.GroupVersion
		expectedVersion *uapi.GroupVersion
		serverVersions  []string
		clientVersions  []uapi.GroupVersion
		config          *unversioned.Config
		expectErr       func(err error) bool
		sendErr         error
	}{
		{
			name:            "server supports client default",
			version:         &uapi.GroupVersion{Version: "version1"},
			config:          &unversioned.Config{},
			serverVersions:  []string{"version1", testapi.Default.GroupVersion().String()},
			clientVersions:  []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
			expectedVersion: &uapi.GroupVersion{Version: "version1"},
		},
		{
			name:            "server falls back to client supported",
			version:         testapi.Default.GroupVersion(),
			config:          &unversioned.Config{},
			serverVersions:  []string{"version1"},
			clientVersions:  []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
			expectedVersion: &uapi.GroupVersion{Version: "version1"},
		},
		{
			name:            "explicit version supported",
			config:          &unversioned.Config{ContentConfig: unversioned.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}},
			serverVersions:  []string{"/version1", testapi.Default.GroupVersion().String()},
			clientVersions:  []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
			expectedVersion: testapi.Default.GroupVersion(),
		},
		{
			name:           "explicit version not supported",
			config:         &unversioned.Config{ContentConfig: unversioned.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}},
			serverVersions: []string{"version1"},
			clientVersions: []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
			expectErr:      func(err error) bool { return strings.Contains(err.Error(), `server does not support API version "v1"`) },
		},
		{
			name:           "connection refused error",
			config:         &unversioned.Config{ContentConfig: unversioned.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}},
			serverVersions: []string{"version1"},
			clientVersions: []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
			sendErr:        errors.New("connection refused"),
			expectErr:      func(err error) bool { return strings.Contains(err.Error(), "connection refused") },
		},
	}
	codec := testapi.Default.Codec()

	for _, test := range tests {
		fakeClient := &fake.RESTClient{
			Codec: codec,
			Resp: &http.Response{
				StatusCode: 200,
				Body:       objBody(&unversionedapi.APIVersions{Versions: test.serverVersions}),
			},
			Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
				if test.sendErr != nil {
					return nil, test.sendErr
				}
				return &http.Response{StatusCode: 200, Body: objBody(&unversionedapi.APIVersions{Versions: test.serverVersions})}, nil
			}),
		}
		c := unversioned.NewOrDie(test.config)
		c.DiscoveryClient.Client = fakeClient.Client
		response, err := unversioned.NegotiateVersion(c, test.config, test.version, test.clientVersions)
		if err == nil && test.expectErr != nil {
			t.Errorf("expected error, got nil for [%s].", test.name)
		}
		if err != nil {
			if test.expectErr == nil || !test.expectErr(err) {
				t.Errorf("unexpected error for [%s]: %v.", test.name, err)
			}
			continue
		}
		if *response != *test.expectedVersion {
			t.Errorf("%s: expected version %s, got %s.", test.name, test.expectedVersion, response)
		}
	}
}