deployments_test.go 2.0 KB
Newer Older
Z
zryfish 已提交
1 2 3 4
package deployment

import (
	"github.com/google/go-cmp/cmp"
H
hongming 已提交
5 6
	appsv1 "k8s.io/api/apps/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Z
zryfish 已提交
7 8 9 10
	"k8s.io/client-go/informers"
	"k8s.io/client-go/kubernetes/fake"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/apiserver/query"
H
hongming 已提交
11
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
Z
zryfish 已提交
12 13 14 15 16 17 18 19
	"testing"
)

func TestListDeployments(t *testing.T) {
	tests := []struct {
		description string
		namespace   string
		query       *query.Query
H
hongming 已提交
20
		expected    *api.ListResult
Z
zryfish 已提交
21 22 23 24 25 26 27
		expectedErr error
	}{
		{
			"test name filter",
			"bar",
			&query.Query{
				Pagination: &query.Pagination{
H
hongming 已提交
28
					Limit:  1,
H
hongming 已提交
29
					Offset: 0,
Z
zryfish 已提交
30 31 32 33 34 35
				},
				SortBy:    query.FieldName,
				Ascending: false,
				Filters: []query.Filter{
					{
						Field: query.FieldName,
H
hongming 已提交
36
						Value: query.Value("foo2"),
Z
zryfish 已提交
37 38 39
					},
				},
			},
H
hongming 已提交
40
			&api.ListResult{
Z
zryfish 已提交
41
				Items: []interface{}{
H
hongming 已提交
42
					foo2,
Z
zryfish 已提交
43
				},
H
hongming 已提交
44
				TotalItems: 1,
Z
zryfish 已提交
45 46 47 48 49
			},
			nil,
		},
	}

H
hongming 已提交
50 51
	getter := prepare()

Z
zryfish 已提交
52 53 54 55
	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {

			got, err := getter.List(test.namespace, test.query)
H
hongming 已提交
56

Z
zryfish 已提交
57 58 59 60 61 62
			if test.expectedErr != nil && err != test.expectedErr {
				t.Errorf("expected error, got nothing")
			} else if err != nil {
				t.Fatal(err)
			}

H
hongming 已提交
63
			if diff := cmp.Diff(got, test.expected); diff != "" {
Z
zryfish 已提交
64 65 66 67 68
				t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
			}
		})
	}
}
H
hongming 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

var (
	foo1 = &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "foo1",
			Namespace: "bar",
		},
	}

	foo2 = &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "foo2",
			Namespace: "bar",
		},
	}
	bar1 = &appsv1.Deployment{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "bar1",
			Namespace: "bar",
		},
	}

	deployments = []interface{}{foo1, foo2, bar1}
)

func prepare() v1alpha3.Interface {
	client := fake.NewSimpleClientset()
	informer := informers.NewSharedInformerFactory(client, 0)

	for _, deployment := range deployments {
		informer.Apps().V1().Deployments().Informer().GetIndexer().Add(deployment)
	}

	return New(informer)
}