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"
|
2015-01-09 23:53:06 +00:00
|
|
|
"testing"
|
|
|
|
|
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-01-25 20:07:10 +00:00
|
|
|
testcore "k8s.io/client-go/testing"
|
2017-11-09 05:43:32 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/apps"
|
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"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2016-09-08 15:50:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
2017-11-09 05:43:32 +00:00
|
|
|
appsclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/apps/internalversion"
|
2016-10-21 22:24:05 +00:00
|
|
|
batchclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/batch/internalversion"
|
|
|
|
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
|
|
|
|
extensionsclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/extensions/internalversion"
|
2015-01-09 23:53:06 +00:00
|
|
|
)
|
|
|
|
|
2016-09-10 16:35:07 +00:00
|
|
|
type ErrorReplicationControllers struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
coreclient.ReplicationControllerInterface
|
2016-06-08 15:07:01 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 16:35:07 +00:00
|
|
|
func (c *ErrorReplicationControllers) Update(controller *api.ReplicationController) (*api.ReplicationController, error) {
|
2016-06-08 15:07:01 +00:00
|
|
|
switch {
|
|
|
|
case c.invalid:
|
2016-09-10 16:35:07 +00:00
|
|
|
return nil, kerrors.NewInvalid(api.Kind(controller.Kind), controller.Name, nil)
|
2016-06-08 15:07:01 +00:00
|
|
|
case c.conflict:
|
2016-09-10 16:35:07 +00:00
|
|
|
return nil, kerrors.NewConflict(api.Resource(controller.Kind), controller.Name, nil)
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
2016-09-10 16:35:07 +00:00
|
|
|
return nil, errors.New("Replication controller update failure")
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 16:35:07 +00:00
|
|
|
type ErrorReplicationControllerClient struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
*fake.Clientset
|
2016-06-08 15:07:01 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
func (c *ErrorReplicationControllerClient) ReplicationControllers(namespace string) coreclient.ReplicationControllerInterface {
|
2016-09-10 16:35:07 +00:00
|
|
|
return &ErrorReplicationControllers{
|
2016-09-08 15:50:53 +00:00
|
|
|
ReplicationControllerInterface: c.Clientset.Core().ReplicationControllers(namespace),
|
|
|
|
conflict: c.conflict,
|
|
|
|
invalid: c.invalid,
|
2016-06-08 15:07:01 +00:00
|
|
|
}
|
2015-03-01 02:40:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScaleRetry(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorReplicationControllerClient{Clientset: fake.NewSimpleClientset(oldRc(0, 0)), conflict: true}
|
2015-09-16 15:32:59 +00:00
|
|
|
scaler := ReplicationControllerScaler{fake}
|
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
|
|
|
|
2016-08-20 04:08:23 +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, ""}
|
2016-08-20 04:08:23 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:04:13 +00:00
|
|
|
func TestReplicationControllerScaleInvalid(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorReplicationControllerClient{Clientset: fake.NewSimpleClientset(oldRc(0, 0)), invalid: true}
|
2015-09-16 15:32:59 +00:00
|
|
|
scaler := ReplicationControllerScaler{fake}
|
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"
|
|
|
|
|
2016-08-20 04:08:23 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScale(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(oldRc(0, 0))
|
|
|
|
scaler := ReplicationControllerScaler{fake.Core()}
|
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"
|
2015-05-21 21:10:25 +00:00
|
|
|
scaler.Scale("default", name, count, &preconditions, nil, nil)
|
2015-01-09 23:53:06 +00:00
|
|
|
|
2015-07-06 21:37:46 +00:00
|
|
|
actions := fake.Actions()
|
|
|
|
if len(actions) != 2 {
|
|
|
|
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != api.Resource("replicationcontrollers") || action.GetName() != name {
|
2015-07-06 21:37:46 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[1].(testcore.UpdateAction); !ok || action.GetResource().GroupResource() != api.Resource("replicationcontrollers") || action.GetObject().(*api.ReplicationController).Spec.Replicas != int32(count) {
|
2015-07-06 21:37:46 +00:00
|
|
|
t.Errorf("unexpected action %v, expected update-replicationController with replicas = %d", actions[1], count)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func TestReplicationControllerScaleFailsPreconditions(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(&api.ReplicationController{
|
2017-01-22 03:36:02 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-04-06 23:27:53 +00:00
|
|
|
Replicas: 10,
|
2015-01-09 23:53:06 +00:00
|
|
|
},
|
2015-04-06 23:27:53 +00:00
|
|
|
})
|
2016-09-08 15:50:53 +00:00
|
|
|
scaler := ReplicationControllerScaler{fake.Core()}
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions := ScalePrecondition{2, ""}
|
2015-01-09 23:53:06 +00:00
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
2015-05-21 21:10:25 +00:00
|
|
|
scaler.Scale("default", name, count, &preconditions, nil, nil)
|
2015-01-09 23:53:06 +00:00
|
|
|
|
2015-07-06 21:37:46 +00:00
|
|
|
actions := fake.Actions()
|
|
|
|
if len(actions) != 1 {
|
2015-09-16 15:32:59 +00:00
|
|
|
t.Errorf("unexpected actions: %v, expected 1 action (get)", actions)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != api.Resource("replicationcontrollers") || action.GetName() != name {
|
2015-07-06 21:37:46 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 16:35:07 +00:00
|
|
|
func TestValidateReplicationController(t *testing.T) {
|
2015-01-09 23:53:06 +00:00
|
|
|
tests := []struct {
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions ScalePrecondition
|
2016-09-10 16:35:07 +00:00
|
|
|
controller api.ReplicationController
|
2015-01-09 23:53:06 +00:00
|
|
|
expectError bool
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
2015-01-09 23:53:06 +00:00
|
|
|
expectError: false,
|
|
|
|
test: "defaults",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults 2",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{0, ""},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "size matches",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{-1, "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "resource version matches",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "both match",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "size different",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "version different",
|
|
|
|
},
|
|
|
|
{
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
2016-09-10 16:35:07 +00:00
|
|
|
controller: api.ReplicationController{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-01-09 23:53:06 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
2016-09-10 16:35:07 +00:00
|
|
|
Spec: api.ReplicationControllerSpec{
|
2015-01-09 23:53:06 +00:00
|
|
|
Replicas: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "both different",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
2016-09-10 16:35:07 +00:00
|
|
|
err := test.preconditions.ValidateReplicationController(&test.controller)
|
2015-09-16 15:32:59 +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-09-16 15:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 12:44:03 +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
|
|
|
}
|
|
|
|
|
2016-04-18 15:44:19 +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")
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:26:33 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
func (c *ErrorJobClient) Jobs(namespace string) batchclient.JobInterface {
|
2016-06-08 15:07:01 +00:00
|
|
|
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) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorJobClient{JobsGetter: fake.NewSimpleClientset().Batch(), conflict: true}
|
2015-09-16 15:32:59 +00:00
|
|
|
scaler := JobScaler{fake}
|
|
|
|
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)
|
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, ""}
|
2016-08-20 04:08:23 +00:00
|
|
|
scaleFunc = ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
|
2015-09-16 15:32:59 +00:00
|
|
|
pass, err = scaleFunc()
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error on precondition failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
scaler := JobScaler{fakeClientset.Batch()}
|
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 {
|
2015-09-16 15:32:59 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
|
|
|
|
}
|
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) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorJobClient{JobsGetter: fake.NewSimpleClientset().Batch(), invalid: true}
|
2015-09-16 15:32:59 +00:00
|
|
|
scaler := JobScaler{fake}
|
|
|
|
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)
|
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,
|
|
|
|
},
|
|
|
|
})
|
2016-09-08 15:50:53 +00:00
|
|
|
scaler := JobScaler{fake.Batch()}
|
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-09-10 16:35:07 +00:00
|
|
|
type ErrorDeployments struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
extensionsclient.DeploymentInterface
|
2016-09-10 16:35:07 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ErrorDeployments) Update(deployment *extensions.Deployment) (*extensions.Deployment, error) {
|
|
|
|
switch {
|
|
|
|
case c.invalid:
|
|
|
|
return nil, kerrors.NewInvalid(api.Kind(deployment.Kind), deployment.Name, nil)
|
|
|
|
case c.conflict:
|
|
|
|
return nil, kerrors.NewConflict(api.Resource(deployment.Kind), deployment.Name, nil)
|
|
|
|
}
|
|
|
|
return nil, errors.New("deployment update failure")
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:26:33 +00:00
|
|
|
func (c *ErrorDeployments) Get(name string, options metav1.GetOptions) (*extensions.Deployment, error) {
|
2016-09-10 16:35:07 +00:00
|
|
|
return &extensions.Deployment{
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: 0,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorDeploymentClient struct {
|
2016-09-08 15:50:53 +00:00
|
|
|
extensionsclient.DeploymentsGetter
|
2016-09-10 16:35:07 +00:00
|
|
|
conflict bool
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
func (c *ErrorDeploymentClient) Deployments(namespace string) extensionsclient.DeploymentInterface {
|
2016-09-10 16:35:07 +00:00
|
|
|
return &ErrorDeployments{
|
2016-09-08 15:50:53 +00:00
|
|
|
DeploymentInterface: c.DeploymentsGetter.Deployments(namespace),
|
|
|
|
invalid: c.invalid,
|
|
|
|
conflict: c.conflict,
|
2016-09-10 16:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 10:39:44 +00:00
|
|
|
func TestDeploymentScaleRetry(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorDeploymentClient{DeploymentsGetter: fake.NewSimpleClientset().Extensions(), conflict: true}
|
2016-02-11 10:39:44 +00:00
|
|
|
scaler := &DeploymentScaler{fake}
|
|
|
|
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 {
|
|
|
|
t.Errorf("Expected error on precondition failure")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:50:53 +00:00
|
|
|
func deployment() *extensions.Deployment {
|
|
|
|
return &extensions.Deployment{
|
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-02-11 10:39:44 +00:00
|
|
|
func TestDeploymentScale(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(deployment())
|
|
|
|
scaler := DeploymentScaler{fake.Extensions()}
|
2016-02-11 10:39:44 +00:00
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
|
|
|
|
actions := fake.Actions()
|
|
|
|
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() != extensions.Resource("deployments") || action.GetName() != name {
|
2016-09-10 16:35:07 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-replicationController %s", actions[0], name)
|
2016-02-11 10:39:44 +00:00
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
if action, ok := actions[1].(testcore.UpdateAction); !ok || action.GetResource().GroupResource() != extensions.Resource("deployments") || action.GetObject().(*extensions.Deployment).Spec.Replicas != int32(count) {
|
2016-02-11 10:39:44 +00:00
|
|
|
t.Errorf("unexpected action %v, expected update-deployment with replicas = %d", actions[1], count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentScaleInvalid(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := &ErrorDeploymentClient{DeploymentsGetter: fake.NewSimpleClientset().Extensions(), invalid: true}
|
2016-02-11 10:39:44 +00:00
|
|
|
scaler := DeploymentScaler{fake}
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeploymentScaleFailsPreconditions(t *testing.T) {
|
2016-09-08 15:50:53 +00:00
|
|
|
fake := fake.NewSimpleClientset(&extensions.Deployment{
|
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-09-10 16:35:07 +00:00
|
|
|
Spec: extensions.DeploymentSpec{
|
2016-02-11 10:39:44 +00:00
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
})
|
2016-09-08 15:50:53 +00:00
|
|
|
scaler := DeploymentScaler{fake.Extensions()}
|
2016-02-11 10:39:44 +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() != extensions.Resource("deployments") || action.GetName() != name {
|
2016-02-11 10:39:44 +00:00
|
|
|
t.Errorf("unexpected action: %v, expected get-deployment %s", actions[0], name)
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 16:35:07 +00:00
|
|
|
|
|
|
|
func TestValidateDeployment(t *testing.T) {
|
|
|
|
zero, ten, twenty := int32(0), int32(10), int32(20)
|
|
|
|
tests := []struct {
|
|
|
|
preconditions ScalePrecondition
|
|
|
|
deployment extensions.Deployment
|
|
|
|
expectError bool
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults 2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{0, ""},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "size matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "resource version matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "both match",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "size different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "no replicas",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "version different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
deployment: extensions.Deployment{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-09-10 16:35:07 +00:00
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
|
|
|
Spec: extensions.DeploymentSpec{
|
|
|
|
Replicas: twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "both different",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
err := test.preconditions.ValidateDeployment(&test.deployment)
|
|
|
|
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)
|
2016-09-10 16:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 05:43:32 +00:00
|
|
|
|
|
|
|
type ErrorStatefulSets struct {
|
|
|
|
appsclient.StatefulSetInterface
|
|
|
|
conflict bool
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ErrorStatefulSets) Update(statefulSet *apps.StatefulSet) (*apps.StatefulSet, error) {
|
|
|
|
switch {
|
|
|
|
case c.invalid:
|
|
|
|
return nil, kerrors.NewInvalid(api.Kind(statefulSet.Kind), statefulSet.Name, nil)
|
|
|
|
case c.conflict:
|
|
|
|
return nil, kerrors.NewConflict(api.Resource(statefulSet.Kind), statefulSet.Name, nil)
|
|
|
|
}
|
|
|
|
return nil, errors.New("statefulSet update failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ErrorStatefulSets) Get(name string, options metav1.GetOptions) (*apps.StatefulSet, error) {
|
|
|
|
return &apps.StatefulSet{
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: 0,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorStatefulSetClient struct {
|
|
|
|
appsclient.StatefulSetsGetter
|
|
|
|
conflict bool
|
|
|
|
invalid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ErrorStatefulSetClient) StatefulSets(namespace string) appsclient.StatefulSetInterface {
|
|
|
|
return &ErrorStatefulSets{
|
|
|
|
StatefulSetInterface: c.StatefulSetsGetter.StatefulSets(namespace),
|
|
|
|
invalid: c.invalid,
|
|
|
|
conflict: c.conflict,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func statefulSet() *apps.StatefulSet {
|
|
|
|
return &apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: metav1.NamespaceDefault,
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScale(t *testing.T) {
|
|
|
|
fake := fake.NewSimpleClientset(statefulSet())
|
|
|
|
scaler := StatefulSetScaler{fake.Apps()}
|
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
scaler.Scale("default", name, count, &preconditions, nil, nil)
|
|
|
|
|
|
|
|
actions := fake.Actions()
|
|
|
|
if len(actions) != 2 {
|
|
|
|
t.Errorf("unexpected actions: %v, expected 2 actions (get, update)", actions)
|
|
|
|
}
|
|
|
|
|
|
|
|
if action, ok := actions[0].(testcore.GetAction); !ok || action.GetResource().GroupResource() != apps.Resource("statefulsets") || action.GetName() != name {
|
|
|
|
t.Errorf("unexpected action: %v, expected get-statefulsets %s", actions[0], name)
|
|
|
|
}
|
|
|
|
if action, ok := actions[1].(testcore.UpdateAction); !ok || action.GetResource().GroupResource() != apps.Resource("statefulsets") || action.GetObject().(*apps.StatefulSet).Spec.Replicas != int32(count) {
|
|
|
|
t.Errorf("unexpected action %v, expected update-statefulset with replicas = %d", actions[1], count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleRetry(t *testing.T) {
|
|
|
|
fake := &ErrorStatefulSetClient{StatefulSetsGetter: fake.NewSimpleClientset().Apps(), conflict: true}
|
|
|
|
scaler := &StatefulSetScaler{fake}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleInvalid(t *testing.T) {
|
|
|
|
fake := &ErrorStatefulSetClient{StatefulSetsGetter: fake.NewSimpleClientset().Apps(), invalid: true}
|
|
|
|
scaler := StatefulSetScaler{fake}
|
|
|
|
preconditions := ScalePrecondition{-1, ""}
|
|
|
|
count := uint(3)
|
|
|
|
name := "foo"
|
|
|
|
namespace := "default"
|
|
|
|
|
|
|
|
scaleFunc := ScaleCondition(&scaler, &preconditions, namespace, name, count, nil)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatefulSetScaleFailsPreconditions(t *testing.T) {
|
|
|
|
fake := fake.NewSimpleClientset(&apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: metav1.NamespaceDefault,
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: 10,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
scaler := StatefulSetScaler{fake.Apps()}
|
|
|
|
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() != apps.Resource("statefulsets") || action.GetName() != name {
|
|
|
|
t.Errorf("unexpected action: %v, expected get-statefulset %s", actions[0], name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateStatefulSet(t *testing.T) {
|
|
|
|
zero, ten, twenty := int32(0), int32(10), int32(20)
|
|
|
|
tests := []struct {
|
|
|
|
preconditions ScalePrecondition
|
|
|
|
statefulset apps.StatefulSet
|
|
|
|
expectError bool
|
|
|
|
test string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, ""},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "defaults 2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{0, ""},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "size matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{-1, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "resource version matches",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: false,
|
|
|
|
test: "both match",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "size different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "no replicas",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: ten,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "version different",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preconditions: ScalePrecondition{10, "foo"},
|
|
|
|
statefulset: apps.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
ResourceVersion: "bar",
|
|
|
|
},
|
|
|
|
Spec: apps.StatefulSetSpec{
|
|
|
|
Replicas: twenty,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
test: "both different",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
err := test.preconditions.ValidateStatefulSet(&test.statefulset)
|
|
|
|
if err != nil && !test.expectError {
|
|
|
|
t.Errorf("unexpected error: %v (%s)", err, test.test)
|
|
|
|
}
|
|
|
|
if err == nil && test.expectError {
|
|
|
|
t.Errorf("expected an error: %v (%s)", err, test.test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|