detail_test.go 2.9 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

M
Marcin Maciaszczyk 已提交
15
package job
16 17

import (
18
	"reflect"
19 20
	"testing"

21
	"github.com/kubernetes/dashboard/src/app/backend/api"
22
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
23

24
	"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
25
	batch "k8s.io/api/batch/v1"
S
Sebastian Florek 已提交
26 27
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
28 29
)

30
func createJob(name, namespace string, jobCompletions int32, labelSelector map[string]string) *batch.Job {
31
	var parallelism int32 = 0
32
	return &batch.Job{
S
Sebastian Florek 已提交
33
		ObjectMeta: metaV1.ObjectMeta{
34 35 36
			Name: name, Namespace: namespace, Labels: labelSelector,
		},
		Spec: batch.JobSpec{
S
Sebastian Florek 已提交
37
			Selector:    &metaV1.LabelSelector{MatchLabels: labelSelector},
38 39 40 41 42 43
			Completions: &jobCompletions,
			Parallelism: &parallelism,
		},
	}
}

44
func TestGetJobDetail(t *testing.T) {
45 46 47 48 49 50 51 52 53 54 55
	var jobCompletions int32
	var parallelism int32

	cases := []struct {
		namespace, name string
		expectedActions []string
		job             *batch.Job
		expected        *JobDetail
	}{
		{
			"ns-1", "job-1",
56
			[]string{"get", "list"},
57
			createJob("job-1", "ns-1", jobCompletions, map[string]string{"app": "test"}),
58
			&JobDetail{
59 60 61 62 63 64 65 66 67 68 69 70 71
				Job: Job{
					ObjectMeta: api.ObjectMeta{Name: "job-1", Namespace: "ns-1",
						Labels: map[string]string{"app": "test"}},
					TypeMeta: api.TypeMeta{Kind: api.ResourceKindJob},
					Pods: common.PodInfo{
						Warnings: []common.Event{},
						Desired:  &jobCompletions,
					},
					Parallelism: &jobCompletions,
					JobStatus: JobStatus{
						Status:  "Running",
						Message: "",
					},
72
				},
73
				Completions: &parallelism,
74
				Errors:      []error{},
75 76 77 78 79 80 81
			},
		},
	}

	for _, c := range cases {
		fakeClient := fake.NewSimpleClientset(c.job)
		dataselect.DefaultDataSelectWithMetrics.MetricQuery = dataselect.NoMetrics
82
		actual, _ := GetJobDetail(fakeClient, c.namespace, c.name)
83 84 85

		actions := fakeClient.Actions()
		if len(actions) != len(c.expectedActions) {
86
			t.Errorf("Unexpected actions: %v, expected %d actions got %d", actions, len(c.expectedActions), len(actions))
87 88 89 90 91 92 93 94 95
			continue
		}

		for i, verb := range c.expectedActions {
			if actions[i].GetVerb() != verb {
				t.Errorf("Unexpected action: %+v, expected %s",
					actions[i], verb)
			}
		}
96

97
		if !reflect.DeepEqual(actual, c.expected) {
98
			t.Errorf("TestGetJobDetail() == \ngot: %#v, \nexpected %#v", actual, c.expected)
99 100
		}
	}
101
}