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)
	}
}
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
		}
	}
}
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: metav1.ObjectMeta{Name: "v1.foo"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						Group:    "foo",
						Version:  "v1",
						Priority: 10,
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "v1.bar"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						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: metav1.ObjectMeta{Name: "v1.foo"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						Group:    "foo",
						Version:  "v1",
						Priority: 20,
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "v2.bar"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						Group:    "bar",
						Version:  "v2",
						Priority: 11,
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "v2.foo"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						Group:    "foo",
						Version:  "v2",
						Priority: 1,
					},
				},
				{
					ObjectMeta: metav1.ObjectMeta{Name: "v1.bar"},
					Spec: apiregistration.APIServiceSpec{
						Service: apiregistration.ServiceReference{
							Namespace: "ns",
							Name:      "api",
						},
						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})
		serviceIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		endpointsIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
		delegate := &delegationHTTPHandler{}
		handler := &apisHandler{
			serviceLister:   v1listers.NewServiceLister(serviceIndexer),
			endpointsLister: v1listers.NewEndpointsLister(endpointsIndexer),
			lister:          listers.NewAPIServiceLister(indexer),
			delegate:        delegate,
		}
		for _, o := range tc.apiservices {
			indexer.Add(o)
		}
		serviceIndexer.Add(&corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "api"}})
		endpointsIndexer.Add(&corev1.Endpoints{
			ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "api"},
			Subsets: []corev1.EndpointSubset{
				{Addresses: []corev1.EndpointAddress{{}}},
			},
		},
		)

		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
		}
	}
}
Beispiel #4
0
func (f *aPIServiceInformer) Lister() internalversion.APIServiceLister {
	return internalversion.NewAPIServiceLister(f.Informer().GetIndexer())
}