示例#1
1
func TestAPIGroupMissing(t *testing.T) {
	indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
	handler := &apiGroupHandler{
		lister:    listers.NewAPIServiceLister(indexer),
		groupName: "foo",
	}

	server := httptest.NewServer(handler)
	defer server.Close()

	resp, err := http.Get(server.URL + "/apis/groupName/foo")
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != http.StatusNotFound {
		t.Fatalf("expected %v, got %v", resp.StatusCode, http.StatusNotFound)
	}

	// foo still has no api services for it (like it was deleted), it should 404
	resp, err = http.Get(server.URL + "/apis/groupName/")
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != http.StatusNotFound {
		t.Fatalf("expected %v, got %v", resp.StatusCode, http.StatusNotFound)
	}
}
示例#2
0
func TestAPIsDelegation(t *testing.T) {
	indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
	delegate := &delegationHTTPHandler{}
	handler := &apisHandler{
		lister:   listers.NewAPIServiceLister(indexer),
		delegate: delegate,
	}

	server := httptest.NewServer(handler)
	defer server.Close()

	pathToDelegation := map[string]bool{
		"/":      true,
		"/apis":  false,
		"/apis/": false,
		"/apis/" + apiregistration.GroupName:                     true,
		"/apis/" + apiregistration.GroupName + "/":               true,
		"/apis/" + apiregistration.GroupName + "/anything":       true,
		"/apis/" + apiregistration.GroupName + "/anything/again": true,
		"/apis/something":                                        true,
		"/apis/something/nested":                                 true,
		"/apis/something/nested/deeper":                          true,
		"/api":     true,
		"/api/v1":  true,
		"/version": true,
	}

	for path, expectedDelegation := range pathToDelegation {
		delegate.called = false

		resp, err := http.Get(server.URL + path)
		if err != nil {
			t.Errorf("%s: %v", path, err)
			continue
		}
		if resp.StatusCode != http.StatusOK {
			httputil.DumpResponse(resp, true)
			t.Errorf("%s: %v", path, err)
			continue
		}
		if e, a := expectedDelegation, delegate.called; e != a {
			t.Errorf("%s: expected %v, got %v", path, e, a)
			continue
		}
	}
}
示例#3
0
func TestAPIs(t *testing.T) {
	tests := []struct {
		name        string
		apiservices []*apiregistration.APIService
		expected    *metav1.APIGroupList
	}{
		{
			name:        "empty",
			apiservices: []*apiregistration.APIService{},
			expected: &metav1.APIGroupList{
				TypeMeta: metav1.TypeMeta{Kind: "APIGroupList", APIVersion: "v1"},
				Groups: []metav1.APIGroup{
					discoveryGroup,
				},
			},
		},
		{
			name: "simple add",
			apiservices: []*apiregistration.APIService{
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.foo"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "foo",
						Version:  "v1",
						Priority: 10,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.bar"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "bar",
						Version:  "v1",
						Priority: 11,
					},
				},
			},
			expected: &metav1.APIGroupList{
				TypeMeta: metav1.TypeMeta{Kind: "APIGroupList", APIVersion: "v1"},
				Groups: []metav1.APIGroup{
					discoveryGroup,
					{
						Name: "foo",
						Versions: []metav1.GroupVersionForDiscovery{
							{
								GroupVersion: "foo/v1",
								Version:      "v1",
							},
						},
						PreferredVersion: metav1.GroupVersionForDiscovery{
							GroupVersion: "foo/v1",
							Version:      "v1",
						},
					},
					{
						Name: "bar",
						Versions: []metav1.GroupVersionForDiscovery{
							{
								GroupVersion: "bar/v1",
								Version:      "v1",
							},
						},
						PreferredVersion: metav1.GroupVersionForDiscovery{
							GroupVersion: "bar/v1",
							Version:      "v1",
						},
					},
				},
			},
		},
		{
			name: "sorting",
			apiservices: []*apiregistration.APIService{
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.foo"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "foo",
						Version:  "v1",
						Priority: 20,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v2.bar"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "bar",
						Version:  "v2",
						Priority: 11,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v2.foo"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "foo",
						Version:  "v2",
						Priority: 1,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.bar"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "bar",
						Version:  "v1",
						Priority: 11,
					},
				},
			},
			expected: &metav1.APIGroupList{
				TypeMeta: metav1.TypeMeta{Kind: "APIGroupList", APIVersion: "v1"},
				Groups: []metav1.APIGroup{
					discoveryGroup,
					{
						Name: "foo",
						Versions: []metav1.GroupVersionForDiscovery{
							{
								GroupVersion: "foo/v2",
								Version:      "v2",
							},
							{
								GroupVersion: "foo/v1",
								Version:      "v1",
							},
						},
						PreferredVersion: metav1.GroupVersionForDiscovery{
							GroupVersion: "foo/v2",
							Version:      "v2",
						},
					},
					{
						Name: "bar",
						Versions: []metav1.GroupVersionForDiscovery{
							{
								GroupVersion: "bar/v1",
								Version:      "v1",
							},
							{
								GroupVersion: "bar/v2",
								Version:      "v2",
							},
						},
						PreferredVersion: metav1.GroupVersionForDiscovery{
							GroupVersion: "bar/v1",
							Version:      "v1",
						},
					},
				},
			},
		},
	}

	for _, tc := range tests {
		indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		delegate := &delegationHTTPHandler{}
		handler := &apisHandler{
			lister:   listers.NewAPIServiceLister(indexer),
			delegate: delegate,
		}
		for _, o := range tc.apiservices {
			indexer.Add(o)
		}

		server := httptest.NewServer(handler)
		defer server.Close()

		resp, err := http.Get(server.URL + "/apis")
		if err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}
		bytes, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}

		actual := &metav1.APIGroupList{}
		if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), bytes, actual); err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}
		if !api.Semantic.DeepEqual(tc.expected, actual) {
			t.Errorf("%s: %v", tc.name, diff.ObjectDiff(tc.expected, actual))
			continue
		}
	}
}
示例#4
0
func TestAPIGroup(t *testing.T) {
	tests := []struct {
		name        string
		group       string
		apiservices []*apiregistration.APIService
		expected    *metav1.APIGroup
	}{
		{
			name:  "sorting",
			group: "foo",
			apiservices: []*apiregistration.APIService{
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.foo"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "foo",
						Version:  "v1",
						Priority: 20,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v2.bar"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "bar",
						Version:  "v2",
						Priority: 11,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v2.foo"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "foo",
						Version:  "v2",
						Priority: 1,
					},
				},
				{
					ObjectMeta: api.ObjectMeta{Name: "v1.bar"},
					Spec: apiregistration.APIServiceSpec{
						Group:    "bar",
						Version:  "v1",
						Priority: 11,
					},
				},
			},
			expected: &metav1.APIGroup{
				TypeMeta: metav1.TypeMeta{Kind: "APIGroup", APIVersion: "v1"},
				Name:     "foo",
				Versions: []metav1.GroupVersionForDiscovery{
					{
						GroupVersion: "foo/v2",
						Version:      "v2",
					},
					{
						GroupVersion: "foo/v1",
						Version:      "v1",
					},
				},
				PreferredVersion: metav1.GroupVersionForDiscovery{
					GroupVersion: "foo/v2",
					Version:      "v2",
				},
			},
		},
	}

	for _, tc := range tests {
		indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		handler := &apiGroupHandler{
			lister:    listers.NewAPIServiceLister(indexer),
			groupName: "foo",
		}
		for _, o := range tc.apiservices {
			indexer.Add(o)
		}

		server := httptest.NewServer(handler)
		defer server.Close()

		resp, err := http.Get(server.URL + "/apis/" + tc.group)
		if err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}
		if resp.StatusCode != http.StatusOK {
			httputil.DumpResponse(resp, true)
			t.Errorf("%s", tc.name)
			continue
		}
		bytes, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}

		actual := &metav1.APIGroup{}
		if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), bytes, actual); err != nil {
			t.Errorf("%s: %v", tc.name, err)
			continue
		}
		if !api.Semantic.DeepEqual(tc.expected, actual) {
			t.Errorf("%s: %v", tc.name, diff.ObjectDiff(tc.expected, actual))
			continue
		}
	}
}