k3s/pkg/kubectl/delete_test.go

838 lines
25 KiB
Go
Raw Normal View History

2015-01-22 17:46:38 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2015-01-22 17:46:38 +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 (
"fmt"
"reflect"
2016-01-22 22:52:38 +00:00
"strings"
2015-01-22 17:46:38 +00:00
"testing"
"time"
2018-02-26 20:23:33 +00:00
autoscalingv1 "k8s.io/api/autoscaling/v1"
2017-01-13 17:48:50 +00:00
"k8s.io/apimachinery/pkg/api/errors"
2018-02-26 20:23:33 +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"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/uuid"
2017-01-11 14:09:48 +00:00
"k8s.io/apimachinery/pkg/watch"
2018-02-26 20:23:33 +00:00
fakescale "k8s.io/client-go/scale/fake"
2017-01-25 20:07:10 +00:00
testcore "k8s.io/client-go/testing"
"k8s.io/kubernetes/pkg/apis/batch"
api "k8s.io/kubernetes/pkg/apis/core"
2015-10-09 22:04:41 +00:00
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
2015-01-22 17:46:38 +00:00
)
func TestReplicationControllerStop(t *testing.T) {
2016-01-22 22:52:38 +00:00
name := "foo"
ns := "default"
tests := []struct {
2018-02-26 20:23:33 +00:00
Name string
Objs []runtime.Object
ScaledDown bool
StopError error
ExpectedActions []string
ScaleClientExpectedAction []string
}{
{
Name: "OnlyOneRC",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: true,
StopError: nil,
ExpectedActions: []string{"get", "list", "delete"},
ScaleClientExpectedAction: []string{"get", "update", "get", "get"},
},
{
Name: "NoOverlapping",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "baz",
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k3": "v3"}},
},
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: true,
StopError: nil,
ExpectedActions: []string{"get", "list", "delete"},
ScaleClientExpectedAction: []string{"get", "update", "get", "get"},
},
{
Name: "OverlappingError",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "baz",
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1", "k2": "v2"}},
},
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: false, // scale resource was not scaled down due to overlapping controllers
2016-01-22 22:52:38 +00:00
StopError: fmt.Errorf("Detected overlapping controllers for rc foo: baz, please manage deletion individually with --cascade=false."),
ExpectedActions: []string{"get", "list"},
},
{
Name: "OverlappingButSafeDelete",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "baz",
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}},
},
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "zaz",
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1", "k2": "v2"}},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: false, // scale resource was not scaled down due to overlapping controllers
2016-01-22 22:52:38 +00:00
StopError: fmt.Errorf("Detected overlapping controllers for rc foo: baz,zaz, please manage deletion individually with --cascade=false."),
ExpectedActions: []string{"get", "list"},
},
2016-01-22 22:52:38 +00:00
{
Name: "TwoExactMatchRCs",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "zaz",
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: false, // scale resource was not scaled down because there is still an additional replica
2016-01-22 22:52:38 +00:00
StopError: nil,
ExpectedActions: []string{"get", "list", "delete"},
2015-01-22 17:46:38 +00:00
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
copiedForWatch := test.Objs[0].DeepCopyObject()
scaleClient := createFakeScaleClient("replicationcontrollers", "foo", 3, nil)
fake := fake.NewSimpleClientset(test.Objs...)
fakeWatch := watch.NewFake()
fake.PrependWatchReactor("replicationcontrollers", testcore.DefaultWatchReactor(fakeWatch, nil))
go func() {
fakeWatch.Add(copiedForWatch)
}()
reaper := ReplicationControllerReaper{fake.Core(), time.Millisecond, time.Millisecond, scaleClient}
err := reaper.Stop(ns, name, 0, nil)
if !reflect.DeepEqual(err, test.StopError) {
t.Fatalf("unexpected error: %v", err)
2018-02-26 20:23:33 +00:00
}
actions := fake.Actions()
if len(actions) != len(test.ExpectedActions) {
t.Fatalf("unexpected actions: %v, expected %d actions got %d", actions, len(test.ExpectedActions), len(actions))
2018-02-26 20:23:33 +00:00
}
for i, verb := range test.ExpectedActions {
if actions[i].GetResource().GroupResource() != api.Resource("replicationcontrollers") {
t.Errorf("unexpected action: %+v, expected %s-replicationController", actions[i], verb)
}
2018-02-26 20:23:33 +00:00
if actions[i].GetVerb() != verb {
t.Errorf("unexpected action: %+v, expected %s-replicationController", actions[i], verb)
2018-02-26 20:23:33 +00:00
}
}
if test.ScaledDown {
scale, err := scaleClient.Scales(ns).Get(schema.GroupResource{Group: "", Resource: "replicationcontrollers"}, name)
if err != nil {
t.Error(err)
}
if scale.Spec.Replicas != 0 {
t.Errorf("a scale subresource has unexpected number of replicas, got %d expected 0", scale.Spec.Replicas)
}
actions := scaleClient.Actions()
if len(actions) != len(test.ScaleClientExpectedAction) {
t.Errorf("unexpected actions: %v, expected %d actions got %d", actions, len(test.ScaleClientExpectedAction), len(actions))
}
for i, verb := range test.ScaleClientExpectedAction {
if actions[i].GetVerb() != verb {
t.Errorf("unexpected action: %+v, expected %s", actions[i].GetVerb(), verb)
}
}
}
})
2015-01-22 17:46:38 +00:00
}
}
func TestReplicaSetStop(t *testing.T) {
name := "foo"
ns := "default"
tests := []struct {
2018-02-26 20:23:33 +00:00
Name string
Objs []runtime.Object
DiscoveryResources []*metav1.APIResourceList
PathsResources map[string]runtime.Object
ScaledDown bool
StopError error
ExpectedActions []string
ScaleClientExpectedAction []string
}{
{
Name: "OnlyOneRS",
Objs: []runtime.Object{
&extensions.ReplicaSetList{ // LIST
2018-02-26 20:23:33 +00:00
TypeMeta: metav1.TypeMeta{
APIVersion: extensions.SchemeGroupVersion.String(),
},
Items: []extensions.ReplicaSet{
{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: true,
StopError: nil,
ExpectedActions: []string{"get", "delete"},
ScaleClientExpectedAction: []string{"get", "update", "get", "get"},
},
{
Name: "NoOverlapping",
Objs: []runtime.Object{
&extensions.ReplicaSetList{ // LIST
Items: []extensions.ReplicaSet{
{
ObjectMeta: metav1.ObjectMeta{
Name: "baz",
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k3": "v3"}},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: true,
StopError: nil,
ExpectedActions: []string{"get", "delete"},
ScaleClientExpectedAction: []string{"get", "update", "get", "get"},
},
// TODO: Implement tests for overlapping replica sets, similar to replication controllers,
// when the overlapping checks are implemented for replica sets.
}
for _, test := range tests {
fake := fake.NewSimpleClientset(test.Objs...)
2018-02-26 20:23:33 +00:00
scaleClient := createFakeScaleClient("replicasets", "foo", 3, nil)
reaper := ReplicaSetReaper{fake.Extensions(), time.Millisecond, time.Millisecond, scaleClient, schema.GroupResource{Group: "extensions", Resource: "replicasets"}}
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, verb := range test.ExpectedActions {
if actions[i].GetResource().GroupResource() != extensions.Resource("replicasets") {
t.Errorf("%s unexpected action: %+v, expected %s-replicaSet", test.Name, actions[i], verb)
}
if actions[i].GetVerb() != verb {
t.Errorf("%s unexpected action: %+v, expected %s-replicaSet", test.Name, actions[i], verb)
}
}
2018-02-26 20:23:33 +00:00
if test.ScaledDown {
scale, err := scaleClient.Scales(ns).Get(schema.GroupResource{Group: "extensions", Resource: "replicasets"}, name)
if err != nil {
t.Error(err)
}
if scale.Spec.Replicas != 0 {
t.Errorf("a scale subresource has unexpected number of replicas, got %d expected 0", scale.Spec.Replicas)
}
actions := scaleClient.Actions()
if len(actions) != len(test.ScaleClientExpectedAction) {
t.Errorf("%s unexpected actions: %v, expected %d actions got %d", test.Name, actions, len(test.ScaleClientExpectedAction), len(actions))
}
for i, verb := range test.ScaleClientExpectedAction {
if actions[i].GetVerb() != verb {
t.Errorf("%s unexpected action: %+v, expected %s", test.Name, actions[i].GetVerb(), verb)
}
}
}
}
}
2015-09-16 15:32:59 +00:00
func TestJobStop(t *testing.T) {
2016-01-22 22:52:38 +00:00
name := "foo"
ns := "default"
2016-04-27 04:35:14 +00:00
zero := int32(0)
2015-09-16 15:32:59 +00:00
tests := []struct {
Name string
2016-01-22 22:52:38 +00:00
Objs []runtime.Object
2015-09-16 15:32:59 +00:00
StopError error
2016-01-22 22:52:38 +00:00
ExpectedActions []string
2015-09-16 15:32:59 +00:00
}{
{
Name: "OnlyOneJob",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&batch.JobList{ // LIST
Items: []batch.Job{
2016-01-22 22:52:38 +00:00
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: batch.JobSpec{
2016-01-22 22:52:38 +00:00
Parallelism: &zero,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{
2016-01-22 22:52:38 +00:00
MatchLabels: map[string]string{"k1": "v1"},
},
},
},
},
2015-09-16 15:32:59 +00:00
},
},
StopError: nil,
2016-01-22 22:52:38 +00:00
ExpectedActions: []string{"get:jobs", "get:jobs", "update:jobs",
"get:jobs", "get:jobs", "list:pods", "delete:jobs"},
2015-10-29 10:07:00 +00:00
},
{
Name: "JobWithDeadPods",
2016-01-22 22:52:38 +00:00
Objs: []runtime.Object{
&batch.JobList{ // LIST
Items: []batch.Job{
2016-01-22 22:52:38 +00:00
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: name,
Namespace: ns,
},
Spec: batch.JobSpec{
2016-01-22 22:52:38 +00:00
Parallelism: &zero,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{
2016-01-22 22:52:38 +00:00
MatchLabels: map[string]string{"k1": "v1"},
2015-10-29 10:07:00 +00:00
},
},
},
2016-01-22 22:52:38 +00:00
},
},
&api.PodList{ // LIST
Items: []api.Pod{
{
ObjectMeta: metav1.ObjectMeta{
2016-01-22 22:52:38 +00:00
Name: "pod1",
Namespace: ns,
Labels: map[string]string{"k1": "v1"},
},
},
},
},
},
StopError: nil,
2016-01-22 22:52:38 +00:00
ExpectedActions: []string{"get:jobs", "get:jobs", "update:jobs",
"get:jobs", "get:jobs", "list:pods", "delete:pods", "delete:jobs"},
2015-09-16 15:32:59 +00:00
},
}
for _, test := range tests {
fake := fake.NewSimpleClientset(test.Objs...)
reaper := JobReaper{fake.Batch(), fake.Core(), time.Millisecond, time.Millisecond}
2016-01-22 22:52:38 +00:00
err := reaper.Stop(ns, name, 0, nil)
2015-09-16 15:32:59 +00:00
if !reflect.DeepEqual(err, test.StopError) {
t.Errorf("%s unexpected error: %v", test.Name, err)
continue
}
actions := fake.Actions()
2016-01-22 22:52:38 +00:00
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
2015-09-16 15:32:59 +00:00
}
2016-01-22 22:52:38 +00:00
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] {
2016-01-22 22:52:38 +00:00
t.Errorf("%s unexpected resource: %+v, expected %s", test.Name, actions[i], expAction)
2015-12-02 15:09:01 +00:00
}
}
}
}
func TestDeploymentStop(t *testing.T) {
name := "foo"
ns := "default"
deployment := extensions.Deployment{
ObjectMeta: metav1.ObjectMeta{
2015-12-02 15:09:01 +00:00
Name: name,
UID: uuid.NewUUID(),
2015-12-02 15:09:01 +00:00
Namespace: ns,
},
Spec: extensions.DeploymentSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
2015-12-02 15:09:01 +00:00
},
Status: extensions.DeploymentStatus{
Replicas: 0,
},
}
trueVar := true
2015-12-02 15:09:01 +00:00
tests := []struct {
2018-02-26 20:23:33 +00:00
Name string
Objs []runtime.Object
ScaledDown bool
StopError error
ExpectedActions []string
ScaleClientExpectedAction []string
2015-12-02 15:09:01 +00:00
}{
{
Name: "SimpleDeployment",
Objs: []runtime.Object{
&extensions.Deployment{ // GET
ObjectMeta: metav1.ObjectMeta{
2015-12-02 15:09:01 +00:00
Name: name,
Namespace: ns,
},
Spec: extensions.DeploymentSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
2015-12-02 15:09:01 +00:00
},
Status: extensions.DeploymentStatus{
Replicas: 0,
},
},
},
StopError: nil,
ExpectedActions: []string{"get:deployments", "update:deployments",
2016-02-24 21:00:45 +00:00
"get:deployments", "list:replicasets", "delete:deployments"},
2015-12-02 15:09:01 +00:00
},
{
Name: "Deployment with single replicaset",
Objs: []runtime.Object{
&deployment, // GET
&extensions.ReplicaSetList{ // LIST
Items: []extensions.ReplicaSet{
// ReplicaSet owned by this Deployment.
2015-12-02 15:09:01 +00:00
{
ObjectMeta: metav1.ObjectMeta{
2015-12-02 15:09:01 +00:00
Name: name,
Namespace: ns,
Labels: map[string]string{"k1": "v1"},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: extensions.SchemeGroupVersion.String(),
Kind: "Deployment",
Name: deployment.Name,
UID: deployment.UID,
Controller: &trueVar,
},
},
2015-12-02 15:09:01 +00:00
},
Spec: extensions.ReplicaSetSpec{},
2015-12-02 15:09:01 +00:00
},
// ReplicaSet owned by something else (should be ignored).
{
ObjectMeta: metav1.ObjectMeta{
Name: "rs2",
Namespace: ns,
Labels: map[string]string{"k1": "v1"},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: extensions.SchemeGroupVersion.String(),
Kind: "Deployment",
Name: "somethingelse",
UID: uuid.NewUUID(),
Controller: &trueVar,
},
},
},
Spec: extensions.ReplicaSetSpec{},
},
},
},
},
2018-02-26 20:23:33 +00:00
ScaledDown: true,
StopError: nil,
ExpectedActions: []string{"get:deployments", "update:deployments",
"get:deployments", "list:replicasets", "get:replicasets",
2018-02-26 20:23:33 +00:00
"delete:replicasets", "delete:deployments"},
ScaleClientExpectedAction: []string{"get", "update", "get", "get"},
2015-12-02 15:09:01 +00:00
},
}
for _, test := range tests {
2018-02-26 20:23:33 +00:00
scaleClient := createFakeScaleClient("deployments", "foo", 3, nil)
fake := fake.NewSimpleClientset(test.Objs...)
2018-02-26 20:23:33 +00:00
reaper := DeploymentReaper{fake.Extensions(), fake.Extensions(), time.Millisecond, time.Millisecond, scaleClient, schema.GroupResource{Group: "extensions", Resource: "deployments"}}
2015-12-02 15:09:01 +00:00
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] {
2015-12-02 15:09:01 +00:00
t.Errorf("%s unexpected resource: %+v, expected %s", test.Name, actions[i], expAction)
}
if len(action) == 3 && actions[i].GetSubresource() != action[2] {
t.Errorf("%s unexpected subresource: %+v, expected %s", test.Name, actions[i], expAction)
2015-09-16 15:32:59 +00:00
}
}
2018-02-26 20:23:33 +00:00
if test.ScaledDown {
scale, err := scaleClient.Scales(ns).Get(schema.GroupResource{Group: "extensions", Resource: "replicaset"}, name)
if err != nil {
t.Error(err)
}
if scale.Spec.Replicas != 0 {
t.Errorf("a scale subresource has unexpected number of replicas, got %d expected 0", scale.Spec.Replicas)
}
actions := scaleClient.Actions()
if len(actions) != len(test.ScaleClientExpectedAction) {
t.Errorf("%s unexpected actions: %v, expected %d actions got %d", test.Name, actions, len(test.ScaleClientExpectedAction), len(actions))
}
for i, verb := range test.ScaleClientExpectedAction {
if actions[i].GetVerb() != verb {
t.Errorf("%s unexpected action: %+v, expected %s", test.Name, actions[i].GetVerb(), verb)
}
}
}
2015-09-16 15:32:59 +00:00
}
}
2015-01-22 17:46:38 +00:00
type noSuchPod struct {
coreclient.PodInterface
2015-01-22 17:46:38 +00:00
}
2016-12-07 13:26:33 +00:00
func (c *noSuchPod) Get(name string, options metav1.GetOptions) (*api.Pod, error) {
2015-01-22 17:46:38 +00:00
return nil, fmt.Errorf("%s does not exist", name)
}
2017-06-01 20:19:50 +00:00
type noDeletePod struct {
coreclient.PodInterface
2015-01-22 17:46:38 +00:00
}
2017-06-01 20:19:50 +00:00
func (c *noDeletePod) Delete(name string, o *metav1.DeleteOptions) error {
2015-01-22 17:46:38 +00:00
return fmt.Errorf("I'm afraid I can't do that, Dave")
}
type reaperFake struct {
*fake.Clientset
2017-06-01 20:19:50 +00:00
noSuchPod, noDeletePod bool
}
func (c *reaperFake) Core() coreclient.CoreInterface {
2017-06-01 20:19:50 +00:00
return &reaperCoreFake{c.Clientset.Core(), c.noSuchPod, c.noDeletePod}
}
type reaperCoreFake struct {
coreclient.CoreInterface
2017-06-01 20:19:50 +00:00
noSuchPod, noDeletePod bool
2015-01-22 17:46:38 +00:00
}
func (c *reaperCoreFake) Pods(namespace string) coreclient.PodInterface {
pods := c.CoreInterface.Pods(namespace)
2015-01-22 17:46:38 +00:00
if c.noSuchPod {
return &noSuchPod{pods}
}
2017-06-01 20:19:50 +00:00
if c.noDeletePod {
return &noDeletePod{pods}
2015-01-22 17:46:38 +00:00
}
2017-06-01 20:19:50 +00:00
return pods
2015-01-22 17:46:38 +00:00
}
2018-01-30 14:12:39 +00:00
func newPod() *api.Pod {
return &api.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceDefault, Name: "foo"}}
}
2015-01-22 17:46:38 +00:00
func TestSimpleStop(t *testing.T) {
tests := []struct {
fake *reaperFake
2016-11-21 02:55:31 +00:00
kind schema.GroupKind
actions []testcore.Action
2015-01-22 17:46:38 +00:00
expectError bool
test string
}{
{
fake: &reaperFake{
2018-01-30 14:12:39 +00:00
Clientset: fake.NewSimpleClientset(newPod()),
2015-01-22 17:46:38 +00:00
},
kind: api.Kind("Pod"),
actions: []testcore.Action{
testcore.NewGetAction(api.Resource("pods").WithVersion(""), metav1.NamespaceDefault, "foo"),
testcore.NewDeleteAction(api.Resource("pods").WithVersion(""), metav1.NamespaceDefault, "foo"),
2015-08-03 13:21:11 +00:00
},
2015-01-22 17:46:38 +00:00
expectError: false,
test: "stop pod succeeds",
},
{
fake: &reaperFake{
Clientset: fake.NewSimpleClientset(),
2015-01-22 17:46:38 +00:00
noSuchPod: true,
},
kind: api.Kind("Pod"),
actions: []testcore.Action{},
2015-01-22 17:46:38 +00:00
expectError: true,
test: "stop pod fails, no pod",
},
{
fake: &reaperFake{
2018-01-30 14:12:39 +00:00
Clientset: fake.NewSimpleClientset(newPod()),
2017-06-01 20:19:50 +00:00
noDeletePod: true,
2015-01-22 17:46:38 +00:00
},
2017-06-01 20:19:50 +00:00
kind: api.Kind("Pod"),
actions: []testcore.Action{
2017-06-01 20:19:50 +00:00
testcore.NewGetAction(api.Resource("pods").WithVersion(""), metav1.NamespaceDefault, "foo"),
2015-08-03 13:21:11 +00:00
},
2015-01-22 17:46:38 +00:00
expectError: true,
2017-06-01 20:19:50 +00:00
test: "stop pod fails, can't delete",
2015-01-22 17:46:38 +00:00
},
}
for _, test := range tests {
fake := test.fake
2018-02-26 20:23:33 +00:00
reaper, err := ReaperFor(test.kind, fake, nil)
2015-01-22 17:46:38 +00:00
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, test.test)
}
err = reaper.Stop("default", "foo", 0, nil)
2015-01-22 17:46:38 +00:00
if err != nil && !test.expectError {
t.Errorf("unexpected error: %v (%s)", err, test.test)
}
if err == nil {
if test.expectError {
t.Errorf("unexpected non-error: %v (%s)", err, test.test)
}
}
actions := fake.Actions()
if len(test.actions) != len(actions) {
t.Errorf("unexpected actions: %v; expected %v (%s)", actions, test.actions, test.test)
2015-01-22 17:46:38 +00:00
}
for i, action := range actions {
2015-01-22 17:46:38 +00:00
testAction := test.actions[i]
2015-08-03 13:21:11 +00:00
if action != testAction {
t.Errorf("unexpected action: %#v; expected %v (%s)", action, testAction, test.test)
2015-01-22 17:46:38 +00:00
}
}
}
}
2016-08-24 20:33:32 +00:00
func TestDeploymentNotFoundError(t *testing.T) {
name := "foo"
ns := "default"
deployment := &extensions.Deployment{
ObjectMeta: metav1.ObjectMeta{
2016-08-24 20:33:32 +00:00
Name: name,
Namespace: ns,
},
Spec: extensions.DeploymentSpec{
Replicas: 0,
2016-12-03 18:57:26 +00:00
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
2016-08-24 20:33:32 +00:00
},
Status: extensions.DeploymentStatus{
Replicas: 0,
},
}
fake := fake.NewSimpleClientset(
deployment,
&extensions.ReplicaSetList{Items: []extensions.ReplicaSet{
2016-08-24 20:33:32 +00:00
{
ObjectMeta: metav1.ObjectMeta{
2016-08-24 20:33:32 +00:00
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{},
2016-08-24 20:33:32 +00:00
},
},
},
)
fake.AddReactor("get", "replicasets", func(action testcore.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, errors.NewNotFound(api.Resource("replicaset"), "doesn't-matter")
2016-08-24 20:33:32 +00:00
})
2018-02-26 20:23:33 +00:00
reaper := DeploymentReaper{fake.Extensions(), fake.Extensions(), time.Millisecond, time.Millisecond, nil, schema.GroupResource{}}
2016-08-24 20:33:32 +00:00
if err := reaper.Stop(ns, name, 0, nil); err != nil {
t.Fatalf("unexpected error: %#v", err)
}
}
2018-02-26 20:23:33 +00:00
func createFakeScaleClient(resource string, resourceName string, replicas int, errorsOnVerb map[string]*kerrors.StatusError) *fakescale.FakeScaleClient {
shouldReturnAnError := func(verb string) (*kerrors.StatusError, bool) {
if anError, anErrorExists := errorsOnVerb[verb]; anErrorExists {
return anError, true
}
return &kerrors.StatusError{}, false
}
newReplicas := int32(replicas)
scaleClient := &fakescale.FakeScaleClient{}
scaleClient.AddReactor("get", resource, func(rawAction testcore.Action) (handled bool, ret runtime.Object, err error) {
action := rawAction.(testcore.GetAction)
if action.GetName() != resourceName {
return true, nil, fmt.Errorf("expected = %s, got = %s", resourceName, action.GetName())
}
if anError, should := shouldReturnAnError("get"); should {
return true, nil, anError
}
obj := &autoscalingv1.Scale{
ObjectMeta: metav1.ObjectMeta{
Name: action.GetName(),
Namespace: action.GetNamespace(),
},
Spec: autoscalingv1.ScaleSpec{
Replicas: newReplicas,
},
}
return true, obj, nil
})
scaleClient.AddReactor("update", resource, func(rawAction testcore.Action) (handled bool, ret runtime.Object, err error) {
action := rawAction.(testcore.UpdateAction)
obj := action.GetObject().(*autoscalingv1.Scale)
if obj.Name != resourceName {
return true, nil, fmt.Errorf("expected = %s, got = %s", resourceName, obj.Name)
}
if anError, should := shouldReturnAnError("update"); should {
return true, nil, anError
}
newReplicas = obj.Spec.Replicas
return true, &autoscalingv1.Scale{
ObjectMeta: metav1.ObjectMeta{
Name: obj.Name,
Namespace: action.GetNamespace(),
},
Spec: autoscalingv1.ScaleSpec{
Replicas: newReplicas,
},
}, nil
})
return scaleClient
}