func TestJobScaleFailsPreconditions(t *testing.T) { ten := int32(10) fake := fake.NewSimpleClientset(&batch.Job{ ObjectMeta: metav1.ObjectMeta{ Namespace: api.NamespaceDefault, Name: "foo", }, Spec: batch.JobSpec{ Parallelism: &ten, }, }) scaler := JobScaler{fake.Batch()} preconditions := ScalePrecondition{2, ""} count := uint(3) name := "foo" scaler.Scale("default", name, count, &preconditions, nil, nil) actions := fake.Actions() if len(actions) != 1 { t.Errorf("unexpected actions: %v, expected 1 actions (get)", actions) } if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != batch.Resource("jobs") || action.GetName() != name { t.Errorf("unexpected action: %v, expected get-job %s", actions[0], name) } }
func TestJobStop(t *testing.T) { name := "foo" ns := "default" zero := int32(0) tests := []struct { Name string Objs []runtime.Object StopError error ExpectedActions []string }{ { Name: "OnlyOneJob", Objs: []runtime.Object{ &batch.JobList{ // LIST Items: []batch.Job{ { ObjectMeta: api.ObjectMeta{ Name: name, Namespace: ns, }, Spec: batch.JobSpec{ Parallelism: &zero, Selector: &unversioned.LabelSelector{ MatchLabels: map[string]string{"k1": "v1"}, }, }, }, }, }, }, StopError: nil, ExpectedActions: []string{"get:jobs", "get:jobs", "update:jobs", "get:jobs", "get:jobs", "list:pods", "delete:jobs"}, }, { Name: "JobWithDeadPods", Objs: []runtime.Object{ &batch.JobList{ // LIST Items: []batch.Job{ { ObjectMeta: api.ObjectMeta{ Name: name, Namespace: ns, }, Spec: batch.JobSpec{ Parallelism: &zero, Selector: &unversioned.LabelSelector{ MatchLabels: map[string]string{"k1": "v1"}, }, }, }, }, }, &api.PodList{ // LIST Items: []api.Pod{ { ObjectMeta: api.ObjectMeta{ Name: "pod1", Namespace: ns, Labels: map[string]string{"k1": "v1"}, }, }, }, }, }, StopError: nil, ExpectedActions: []string{"get:jobs", "get:jobs", "update:jobs", "get:jobs", "get:jobs", "list:pods", "delete:pods", "delete:jobs"}, }, } for _, test := range tests { fake := fake.NewSimpleClientset(test.Objs...) reaper := JobReaper{fake.Batch(), fake.Core(), time.Millisecond, time.Millisecond} err := reaper.Stop(ns, name, 0, nil) if !reflect.DeepEqual(err, test.StopError) { t.Errorf("%s unexpected error: %v", test.Name, err) continue } actions := fake.Actions() if len(actions) != len(test.ExpectedActions) { t.Errorf("%s unexpected actions: %v, expected %d actions got %d", test.Name, actions, len(test.ExpectedActions), len(actions)) continue } for i, expAction := range test.ExpectedActions { action := strings.Split(expAction, ":") if actions[i].GetVerb() != action[0] { t.Errorf("%s unexpected verb: %+v, expected %s", test.Name, actions[i], expAction) } if actions[i].GetResource().Resource != action[1] { t.Errorf("%s unexpected resource: %+v, expected %s", test.Name, actions[i], expAction) } } } }