2015-01-09 23:53:06 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-09 23:53:06 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
2015-03-01 02:40:57 +00:00
|
|
|
"errors"
|
2017-11-20 17:44:07 +00:00
|
|
|
"fmt"
|
2015-01-09 23:53:06 +00:00
|
|
|
"testing"
|
2017-11-20 17:44:07 +00:00
|
|
|
"time"
|
2015-01-09 23:53:06 +00:00
|
|
|
|
2017-01-13 17:48:50 +00:00
|
|
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-20 17:44:07 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/client-go/scale"
|
2017-01-25 20:07:10 +00:00
|
|
|
testcore "k8s.io/client-go/testing"
|
2016-04-18 15:44:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/batch"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
2016-09-08 15:50:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2016-10-21 22:24:05 +00:00
|
|
|
batchclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/batch/internalversion"
|
2015-01-09 23:53:06 +00:00
|
|
|
)
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScaleRetry(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClientExpectedAction := []string{"get", "update", "get"}
|
|
|
|
scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
2015-03-01 02:40:57 +00:00
|
|
|
count := uint(3)
|
2016-09-08 15:50:53 +00:00
|
|
|
name := "foo-v1"
|
2017-01-22 03:36:02 +00:00
|
|
|
namespace := metav1.NamespaceDefault
|
2015-03-01 02:40:57 +00:00
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-05-21 21:10:25 +00:00
|
|
|
pass, err := scaleFunc()
|
2015-09-15 00:04:13 +00:00
|
|
|
if pass {
|
2015-03-01 02:40:57 +00:00
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-08 15:07:01 +00:00
|
|
|
t.Errorf("Did not expect an error on update conflict failure, got %v", err)
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions = ScalePrecondition{3, ""}
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc = ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-05-21 21:10:25 +00:00
|
|
|
pass, err = scaleFunc()
|
2015-03-01 02:40:57 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error on precondition failure")
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 00:04:13 +00:00
|
|
|
func TestReplicationControllerScaleInvalid(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 1, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
|
2015-09-15 00:04:13 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
2016-09-08 15:50:53 +00:00
|
|
|
name := "foo-v1"
|
2015-09-15 00:04:13 +00:00
|
|
|
namespace := "default"
|
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-09-15 00:04:13 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
2015-11-13 12:44:03 +00:00
|
|
|
e, ok := err.(ScaleError)
|
2016-06-08 15:07:01 +00:00
|
|
|
if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
|
2015-09-15 00:04:13 +00:00
|
|
|
t.Errorf("Expected error on invalid update failure, got %v", err)
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 00:04:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScale(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("replicationcontrollers", "foo-v1", 2, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
2015-01-09 23:53:06 +00:00
|
|
|
count := uint(3)
|
2016-09-08 15:50:53 +00:00
|
|
|
name := "foo-v1"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
2015-01-09 23:53:06 +00:00
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error occurred = %v while scaling the resource", err)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get"}
|
|
|
|
scaleClient := createFakeScaleClient("replicationcontrollers", "foo", 10, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "", Resource: "replicationcontrollers"})
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions := ScalePrecondition{2, ""}
|
2015-01-09 23:53:06 +00:00
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected to get an error but none was returned")
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
type errorJobs struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
batchclient.JobInterface
|
2016-06-08 15:07:01 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
2015-11-13 12:44:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
func (c *errorJobs) Update(job *batch.Job) (*batch.Job, error) {
|
2016-06-08 15:07:01 +00:00
|
|
|
switch {
|
|
|
|
case c.invalid:
|
|
|
|
return nil, kerrors.NewInvalid(api.Kind(job.Kind), job.Name, nil)
|
|
|
|
case c.conflict:
|
|
|
|
return nil, kerrors.NewConflict(api.Resource(job.Kind), job.Name, nil)
|
2015-11-13 12:44:03 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("Job update failure")
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
func (c *errorJobs) Get(name string, options metav1.GetOptions) (*batch.Job, error) {
|
2016-04-27 04:35:14 +00:00
|
|
|
zero := int32(0)
|
2016-04-18 15:44:19 +00:00
|
|
|
return &batch.Job{
|
|
|
|
Spec: batch.JobSpec{
|
2015-11-13 12:44:03 +00:00
|
|
|
Parallelism: &zero,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
type errorJobClient struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
batchclient.JobsGetter
|
2016-06-08 15:07:01 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
2015-11-13 12:44:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
func (c *errorJobClient) Jobs(namespace string) batchclient.JobInterface {
|
|
|
|
return &errorJobs{
|
2016-09-08 15:50:53 +00:00
|
|
|
JobInterface: c.JobsGetter.Jobs(namespace),
|
|
|
|
conflict: c.conflict,
|
|
|
|
invalid: c.invalid,
|
2016-06-08 15:07:01 +00:00
|
|
|
}
|
2015-11-13 12:44:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
func TestJobScaleRetry(t *testing.T) {
|
2018-01-29 08:28:02 +00:00
|
|
|
fake := &errorJobClient{JobsGetter: fake.NewSimpleClientset().Batch(), conflict: true}
|
|
|
|
scaler := ScalerFor(schema.GroupKind{Group: batch.GroupName, Kind: "Job"}, fake, nil, schema.GroupResource{})
|
2015-09-16 15:32:59 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-09-16 15:32:59 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass != false {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error on update failure, got %v", err)
|
|
|
|
}
|
|
|
|
preconditions = ScalePrecondition{3, ""}
|
2018-01-29 08:28:02 +00:00
|
|
|
scaleFunc = ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-09-16 15:32:59 +00:00
|
|
|
pass, err = scaleFunc()
|
|
|
|
if err == nil {
|
2017-11-11 07:00:43 +00:00
|
|
|
t.Error("Expected error on precondition failure")
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
func job() *batch.Job {
|
|
|
|
return &batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-09-08 15:50:53 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
func TestJobScale(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fakeClientset := fake.NewSimpleClientset(job())
|
2018-01-29 08:28:02 +00:00
|
|
|
scaler := ScalerFor(schema.GroupKind{Group: batch.GroupName, Kind: "Job"}, fakeClientset.Batch(), nil, schema.GroupResource{})
|
2015-09-16 15:32:59 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
actions := fakeClientset.Actions()
|
2015-09-16 15:32:59 +00:00
|
|
|
if len(actions) != 2 {
|
|
|
|
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
|
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != batch.Resource("jobs") || action.GetName() != name {
|
2017-11-11 07:00:43 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-job %s", actions[0], name)
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[1].(testcore.UpdateAction); !ok || action.GetResource().GroupResource() != batch.Resource("jobs") || *action.GetObject().(*batch.Job).Spec.Parallelism != int32(count) {
|
2015-09-16 15:32:59 +00:00
|
|
|
t.Errorf("unexpected action %v, expected update-job with parallelism = %d", actions[1], count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobScaleInvalid(t *testing.T) {
|
2018-01-29 08:28:02 +00:00
|
|
|
fake := &errorJobClient{JobsGetter: fake.NewSimpleClientset().Batch(), invalid: true}
|
|
|
|
scaler := ScalerFor(schema.GroupKind{Group: batch.GroupName, Kind: "Job"}, fake, nil, schema.GroupResource{})
|
2015-09-16 15:32:59 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2018-01-29 08:28:02 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2015-09-16 15:32:59 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
2015-11-13 12:44:03 +00:00
|
|
|
e, ok := err.(ScaleError)
|
2016-06-08 15:07:01 +00:00
|
|
|
if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
|
2015-09-16 15:32:59 +00:00
|
|
|
t.Errorf("Expected error on invalid update failure, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobScaleFailsPreconditions(t *testing.T) {
|
2016-04-27 04:35:14 +00:00
|
|
|
ten := int32(10)
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(&batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-01-22 03:36:02 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2016-09-08 15:50:53 +00:00
|
|
|
Name: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &ten,
|
|
|
|
},
|
|
|
|
})
|
2018-01-29 08:28:02 +00:00
|
|
|
scaler := ScalerFor(schema.GroupKind{Group: batch.GroupName, Kind: "Job"}, fake.Batch(), nil, schema.GroupResource{})
|
2015-09-16 15:32:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != batch.Resource("jobs") || action.GetName() != name {
|
2015-09-16 15:32:59 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-job %s", actions[0], name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateJob(t *testing.T) {
|
2016-04-27 04:35:14 +00:00
|
|
|
zero, ten, twenty := int32(0), int32(10), int32(20)
|
2015-09-16 15:32:59 +00:00
|
|
|
tests := []struct {
|
|
|
|
preconditions ScalePrecondition
|
2016-04-18 15:44:19 +00:00
|
|
|
job batch.Job
|
2015-09-16 15:32:59 +00:00
|
|
|
expectError bool
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults 2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{0, ""},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "size matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "resource version matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "both match",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "size different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "parallelism nil",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "version different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-04-18 15:44:19 +00:00
|
|
|
job: batch.Job{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-09-16 15:32:59 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
2016-04-18 15:44:19 +00:00
|
|
|
Spec: batch.JobSpec{
|
2015-09-16 15:32:59 +00:00
|
|
|
Parallelism: &twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "both different",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
err := test.preconditions.ValidateJob(&test.job)
|
2015-01-09 23:53:06 +00:00
|
|
|
if err != nil && !test.expectError {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, test.test)
|
|
|
|
}
|
|
|
|
if err == nil && test.expectError {
|
2017-08-09 02:37:03 +00:00
|
|
|
t.Errorf("expected an error: %v (%s)", err, test.test)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 12:44:03 +00:00
|
|
|
|
2016-02-11 10:39:44 +00:00
|
|
|
func TestDeploymentScaleRetry(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClientExpectedAction := []string{"get", "update", "get"}
|
|
|
|
scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "deployments"})
|
2016-02-11 10:39:44 +00:00
|
|
|
preconditions := &ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2016-08-20 04:08:23 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
2016-02-11 10:39:44 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass != false {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error on update failure, got %v", err)
|
|
|
|
}
|
|
|
|
preconditions = &ScalePrecondition{3, ""}
|
2016-08-20 04:08:23 +00:00
|
|
|
scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
2016-02-11 10:39:44 +00:00
|
|
|
pass, err = scaleFunc()
|
|
|
|
if err == nil {
|
2017-11-11 07:00:43 +00:00
|
|
|
t.Error("Expected error on precondition failure")
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 10:39:44 +00:00
|
|
|
func TestDeploymentScale(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("deployments", "foo", 2, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "deployments"})
|
2016-02-11 10:39:44 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentScaleInvalid(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClient := createFakeScaleClient("deployments", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "deployments"})
|
2016-02-11 10:39:44 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2016-02-11 10:39:44 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
e, ok := err.(ScaleError)
|
2016-06-08 15:07:01 +00:00
|
|
|
if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
|
2016-02-11 10:39:44 +00:00
|
|
|
t.Errorf("Expected error on invalid update failure, got %v", err)
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentScaleFailsPreconditions(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get"}
|
|
|
|
scaleClient := createFakeScaleClient("deployments", "foo", 10, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "deployments"})
|
2016-02-11 10:39:44 +00:00
|
|
|
preconditions := ScalePrecondition{2, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("exptected to get an error but none was returned")
|
|
|
|
}
|
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 10:39:44 +00:00
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
func TestStatefulSetScale(t *testing.T) {
|
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("statefulsets", "foo", 2, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "statefullset"})
|
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleRetry(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update", "get"}
|
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
|
2017-11-09 05:43:32 +00:00
|
|
|
preconditions := &ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
|
|
|
scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass != false {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error on update failure, got %v", err)
|
|
|
|
}
|
|
|
|
preconditions = &ScalePrecondition{3, ""}
|
|
|
|
scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
|
|
|
pass, err = scaleFunc()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error on precondition failure")
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleInvalid(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClient := createFakeScaleClient("statefulsets", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
|
2017-11-09 05:43:32 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2017-11-09 05:43:32 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
e, ok := err.(ScaleError)
|
|
|
|
if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
|
|
|
|
t.Errorf("Expected error on invalid update failure, got %v", err)
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleFailsPreconditions(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get"}
|
|
|
|
scaleClient := createFakeScaleClient("statefulsets", "foo", 10, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "apps", Resource: "statefulsets"})
|
2017-11-09 05:43:32 +00:00
|
|
|
preconditions := ScalePrecondition{2, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected to get an error but none was returned")
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
2017-11-09 05:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 06:51:41 +00:00
|
|
|
|
|
|
|
func TestReplicaSetScale(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
|
2017-11-11 06:51:41 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplicaSetScaleRetry(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewConflict(api.Resource("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClientExpectedAction := []string{"get", "update", "get"}
|
|
|
|
scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
|
2017-11-11 06:51:41 +00:00
|
|
|
preconditions := &ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
|
|
|
scaleFunc := ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass != false {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error on update failure, got %v", err)
|
|
|
|
}
|
|
|
|
preconditions = &ScalePrecondition{3, ""}
|
|
|
|
scaleFunc = ScaleCondition(scaler, preconditions, namespace, name, count, nil)
|
|
|
|
pass, err = scaleFunc()
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error on precondition failure")
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplicaSetScaleInvalid(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
verbsOnError := map[string]*kerrors.StatusError{
|
|
|
|
"update": kerrors.NewInvalid(api.Kind("Status"), "foo", nil),
|
|
|
|
}
|
|
|
|
scaleClientExpectedAction := []string{"get", "update"}
|
|
|
|
scaleClient := createFakeScaleClient("replicasets", "foo", 2, verbsOnError)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
|
2017-11-11 06:51:41 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleFunc := ScaleCondition(scaler, &preconditions, namespace, name, count, nil)
|
2017-11-11 06:51:41 +00:00
|
|
|
pass, err := scaleFunc()
|
|
|
|
if pass {
|
|
|
|
t.Errorf("Expected an update failure to return pass = false, got pass = %v", pass)
|
|
|
|
}
|
|
|
|
e, ok := err.(ScaleError)
|
|
|
|
if err == nil || !ok || e.FailureType != ScaleUpdateFailure {
|
|
|
|
t.Errorf("Expected error on invalid update failure, got %v", err)
|
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
|
|
|
}
|
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplicaSetsGetterFailsPreconditions(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClientExpectedAction := []string{"get"}
|
|
|
|
scaleClient := createFakeScaleClient("replicasets", "foo", 10, nil)
|
|
|
|
scaler := NewScaler(scaleClient, schema.GroupResource{Group: "extensions", Resource: "replicasets"})
|
2017-11-11 06:51:41 +00:00
|
|
|
preconditions := ScalePrecondition{2, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2018-02-26 20:23:33 +00:00
|
|
|
err := scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected to get an error but non was returned")
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
actions := scaleClient.Actions()
|
|
|
|
if len(actions) != len(scaleClientExpectedAction) {
|
|
|
|
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(scaleClientExpectedAction), len(actions))
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
2018-02-26 20:23:33 +00:00
|
|
|
for i, verb := range scaleClientExpectedAction {
|
|
|
|
if actions[i].GetVerb() != verb {
|
|
|
|
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
|
2017-11-11 06:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 17:44:07 +00:00
|
|
|
|
|
|
|
// TestGenericScaleSimple exercises GenericScaler.ScaleSimple method
|
|
|
|
func TestGenericScaleSimple(t *testing.T) {
|
|
|
|
// test data
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClient := createFakeScaleClient("deployments", "abc", 10, nil)
|
2017-11-20 17:44:07 +00:00
|
|
|
|
|
|
|
// test scenarios
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
precondition ScalePrecondition
|
|
|
|
newSize int
|
|
|
|
targetGR schema.GroupResource
|
|
|
|
resName string
|
|
|
|
scaleGetter scale.ScalesGetter
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
// scenario 1: scale up the "abc" deployment
|
|
|
|
{
|
|
|
|
name: "scale up the \"abc\" deployment",
|
|
|
|
precondition: ScalePrecondition{10, ""},
|
|
|
|
newSize: 20,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
},
|
|
|
|
// scenario 2: scale down the "abc" deployment
|
|
|
|
{
|
2018-02-26 20:23:33 +00:00
|
|
|
name: "scale down the \"abs\" deployment",
|
2017-11-20 17:44:07 +00:00
|
|
|
precondition: ScalePrecondition{20, ""},
|
|
|
|
newSize: 5,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
},
|
|
|
|
// scenario 3: precondition error, expected size is 1,
|
|
|
|
// note that the previous scenario (2) set the size to 5
|
|
|
|
{
|
|
|
|
name: "precondition error, expected size is 1",
|
|
|
|
precondition: ScalePrecondition{1, ""},
|
|
|
|
newSize: 5,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
// scenario 4: precondition is not validated when the precondition size is set to -1
|
|
|
|
{
|
|
|
|
name: "precondition is not validated when the size is set to -1",
|
|
|
|
precondition: ScalePrecondition{-1, ""},
|
|
|
|
newSize: 5,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
},
|
|
|
|
// scenario 5: precondition error, resource version mismatch
|
|
|
|
{
|
|
|
|
name: "precondition error, resource version mismatch",
|
|
|
|
precondition: ScalePrecondition{5, "v1"},
|
|
|
|
newSize: 5,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// act
|
|
|
|
for index, scenario := range scenarios {
|
|
|
|
t.Run(fmt.Sprintf("running scenario %d: %s", index+1, scenario.name), func(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
target := NewScaler(scenario.scaleGetter, scenario.targetGR)
|
2017-11-20 17:44:07 +00:00
|
|
|
|
|
|
|
resVersion, err := target.ScaleSimple("default", scenario.resName, &scenario.precondition, uint(scenario.newSize))
|
|
|
|
|
|
|
|
if scenario.expectError && err == nil {
|
2018-02-09 06:53:53 +00:00
|
|
|
t.Fatal("expected an error but was not returned")
|
2017-11-20 17:44:07 +00:00
|
|
|
}
|
|
|
|
if !scenario.expectError && err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if resVersion != "" {
|
|
|
|
t.Fatalf("unexpected resource version returned = %s, wanted = %s", resVersion, "")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGenericScale exercises GenericScaler.Scale method
|
|
|
|
func TestGenericScale(t *testing.T) {
|
|
|
|
// test data
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleClient := createFakeScaleClient("deployments", "abc", 10, nil)
|
2017-11-20 17:44:07 +00:00
|
|
|
|
|
|
|
// test scenarios
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
precondition ScalePrecondition
|
|
|
|
newSize int
|
|
|
|
targetGR schema.GroupResource
|
|
|
|
resName string
|
|
|
|
scaleGetter scale.ScalesGetter
|
|
|
|
waitForReplicas *RetryParams
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
// scenario 1: scale up the "abc" deployment
|
|
|
|
{
|
|
|
|
name: "scale up the \"abc\" deployment",
|
|
|
|
precondition: ScalePrecondition{10, ""},
|
|
|
|
newSize: 20,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
},
|
|
|
|
// scenario 2: a resource name cannot be empty
|
|
|
|
{
|
|
|
|
name: "a resource name cannot be empty",
|
|
|
|
precondition: ScalePrecondition{10, ""},
|
|
|
|
newSize: 20,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
// scenario 3: wait for replicas error due to status.Replicas != spec.Replicas
|
|
|
|
{
|
|
|
|
name: "wait for replicas error due to status.Replicas != spec.Replicas",
|
|
|
|
precondition: ScalePrecondition{10, ""},
|
|
|
|
newSize: 20,
|
2018-02-26 20:23:33 +00:00
|
|
|
targetGR: schema.GroupResource{Group: "apps", Resource: "deployments"},
|
2017-11-20 17:44:07 +00:00
|
|
|
resName: "abc",
|
|
|
|
scaleGetter: scaleClient,
|
|
|
|
waitForReplicas: &RetryParams{time.Duration(5 * time.Second), time.Duration(5 * time.Second)},
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// act
|
|
|
|
for index, scenario := range scenarios {
|
|
|
|
t.Run(fmt.Sprintf("running scenario %d: %s", index+1, scenario.name), func(t *testing.T) {
|
2018-02-26 20:23:33 +00:00
|
|
|
target := NewScaler(scenario.scaleGetter, scenario.targetGR)
|
2017-11-20 17:44:07 +00:00
|
|
|
|
|
|
|
err := target.Scale("default", scenario.resName, uint(scenario.newSize), &scenario.precondition, nil, scenario.waitForReplicas)
|
|
|
|
|
|
|
|
if scenario.expectError && err == nil {
|
2018-02-09 06:53:53 +00:00
|
|
|
t.Fatal("expected an error but was not returned")
|
2017-11-20 17:44:07 +00:00
|
|
|
}
|
|
|
|
if !scenario.expectError && err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|