convert rest of kubectl to generated clients

This commit is contained in:
deads2k
2016-09-08 11:50:53 -04:00
parent f756e43e7f
commit 519c13745f
27 changed files with 500 additions and 606 deletions

View File

@@ -28,8 +28,9 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/batch"
"k8s.io/kubernetes/pkg/apis/extensions"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
testcore "k8s.io/kubernetes/pkg/client/testing/core"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
@@ -47,15 +48,6 @@ func TestReplicationControllerStop(t *testing.T) {
{
Name: "OnlyOneRC",
Objs: []runtime.Object{
&api.ReplicationController{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
@@ -76,15 +68,6 @@ func TestReplicationControllerStop(t *testing.T) {
{
Name: "NoOverlapping",
Objs: []runtime.Object{
&api.ReplicationController{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
@@ -115,15 +98,6 @@ func TestReplicationControllerStop(t *testing.T) {
Name: "OverlappingError",
Objs: []runtime.Object{
&api.ReplicationController{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
@@ -155,15 +129,6 @@ func TestReplicationControllerStop(t *testing.T) {
Name: "OverlappingButSafeDelete",
Objs: []runtime.Object{
&api.ReplicationController{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1", "k2": "v2"}},
},
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
@@ -205,15 +170,6 @@ func TestReplicationControllerStop(t *testing.T) {
Name: "TwoExactMatchRCs",
Objs: []runtime.Object{
&api.ReplicationController{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: api.ReplicationControllerSpec{
Replicas: 0,
Selector: map[string]string{"k1": "v1"}},
},
&api.ReplicationControllerList{ // LIST
Items: []api.ReplicationController{
{
@@ -248,15 +204,15 @@ func TestReplicationControllerStop(t *testing.T) {
if err != nil {
t.Fatalf("%s unexpected error: %v", test.Name, err)
}
fake := testclient.NewSimpleFake(test.Objs...)
fake := fake.NewSimpleClientset(test.Objs...)
fakeWatch := watch.NewFake()
fake.PrependWatchReactor("replicationcontrollers", testclient.DefaultWatchReactor(fakeWatch, nil))
fake.PrependWatchReactor("replicationcontrollers", testcore.DefaultWatchReactor(fakeWatch, nil))
go func() {
fakeWatch.Add(copiedForWatch)
}()
reaper := ReplicationControllerReaper{fake, time.Millisecond, time.Millisecond}
reaper := ReplicationControllerReaper{fake.Core(), time.Millisecond, time.Millisecond}
err = reaper.Stop(ns, name, 0, nil)
if !reflect.DeepEqual(err, test.StopError) {
t.Errorf("%s unexpected error: %v", test.Name, err)
@@ -269,7 +225,7 @@ func TestReplicationControllerStop(t *testing.T) {
continue
}
for i, verb := range test.ExpectedActions {
if actions[i].GetResource() != "replicationcontrollers" {
if actions[i].GetResource().GroupResource() != api.Resource("replicationcontrollers") {
t.Errorf("%s unexpected action: %+v, expected %s-replicationController", test.Name, actions[i], verb)
}
if actions[i].GetVerb() != verb {
@@ -291,16 +247,6 @@ func TestReplicaSetStop(t *testing.T) {
{
Name: "OnlyOneRS",
Objs: []runtime.Object{
&extensions.ReplicaSet{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 0,
Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
},
},
&extensions.ReplicaSetList{ // LIST
Items: []extensions.ReplicaSet{
{
@@ -322,16 +268,6 @@ func TestReplicaSetStop(t *testing.T) {
{
Name: "NoOverlapping",
Objs: []runtime.Object{
&extensions.ReplicaSet{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 0,
Selector: &unversioned.LabelSelector{MatchLabels: map[string]string{"k1": "v1"}},
},
},
&extensions.ReplicaSetList{ // LIST
Items: []extensions.ReplicaSet{
{
@@ -365,8 +301,8 @@ func TestReplicaSetStop(t *testing.T) {
}
for _, test := range tests {
fake := testclient.NewSimpleFake(test.Objs...)
reaper := ReplicaSetReaper{fake, time.Millisecond, time.Millisecond}
fake := fake.NewSimpleClientset(test.Objs...)
reaper := ReplicaSetReaper{fake.Extensions(), time.Millisecond, time.Millisecond}
err := reaper.Stop(ns, name, 0, nil)
if !reflect.DeepEqual(err, test.StopError) {
t.Errorf("%s unexpected error: %v", test.Name, err)
@@ -379,7 +315,7 @@ func TestReplicaSetStop(t *testing.T) {
continue
}
for i, verb := range test.ExpectedActions {
if actions[i].GetResource() != "replicasets" {
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 {
@@ -402,18 +338,6 @@ func TestJobStop(t *testing.T) {
{
Name: "OnlyOneJob",
Objs: []runtime.Object{
&batch.Job{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: batch.JobSpec{
Parallelism: &zero,
Selector: &unversioned.LabelSelector{
MatchLabels: map[string]string{"k1": "v1"},
},
},
},
&batch.JobList{ // LIST
Items: []batch.Job{
{
@@ -438,18 +362,6 @@ func TestJobStop(t *testing.T) {
{
Name: "JobWithDeadPods",
Objs: []runtime.Object{
&batch.Job{ // GET
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: batch.JobSpec{
Parallelism: &zero,
Selector: &unversioned.LabelSelector{
MatchLabels: map[string]string{"k1": "v1"},
},
},
},
&batch.JobList{ // LIST
Items: []batch.Job{
{
@@ -485,8 +397,8 @@ func TestJobStop(t *testing.T) {
}
for _, test := range tests {
fake := testclient.NewSimpleFake(test.Objs...)
reaper := JobReaper{fake, time.Millisecond, time.Millisecond}
fake := fake.NewSimpleClientset(test.Objs...)
reaper := JobReaper{fake.Batch(), fake.Core(), time.Millisecond, time.Millisecond}
err := reaper.Stop(ns, name, 0, nil)
if !reflect.DeepEqual(err, test.StopError) {
t.Errorf("%s unexpected error: %v", test.Name, err)
@@ -503,7 +415,7 @@ func TestJobStop(t *testing.T) {
if actions[i].GetVerb() != action[0] {
t.Errorf("%s unexpected verb: %+v, expected %s", test.Name, actions[i], expAction)
}
if actions[i].GetResource() != action[1] {
if actions[i].GetResource().Resource != action[1] {
t.Errorf("%s unexpected resource: %+v, expected %s", test.Name, actions[i], expAction)
}
}
@@ -564,6 +476,7 @@ func TestDeploymentStop(t *testing.T) {
ObjectMeta: api.ObjectMeta{
Name: name,
Namespace: ns,
Labels: map[string]string{"k1": "v1"},
},
Spec: extensions.ReplicaSetSpec{
Template: template,
@@ -581,8 +494,8 @@ func TestDeploymentStop(t *testing.T) {
}
for _, test := range tests {
fake := testclient.NewSimpleFake(test.Objs...)
reaper := DeploymentReaper{fake, time.Millisecond, time.Millisecond}
fake := fake.NewSimpleClientset(test.Objs...)
reaper := DeploymentReaper{fake.Extensions(), fake.Extensions(), time.Millisecond, time.Millisecond}
err := reaper.Stop(ns, name, 0, nil)
if !reflect.DeepEqual(err, test.StopError) {
t.Errorf("%s unexpected error: %v", test.Name, err)
@@ -599,7 +512,7 @@ func TestDeploymentStop(t *testing.T) {
if actions[i].GetVerb() != action[0] {
t.Errorf("%s unexpected verb: %+v, expected %s", test.Name, actions[i], expAction)
}
if actions[i].GetResource() != action[1] {
if actions[i].GetResource().Resource != action[1] {
t.Errorf("%s unexpected resource: %+v, expected %s", test.Name, actions[i], expAction)
}
if len(action) == 3 && actions[i].GetSubresource() != action[2] {
@@ -610,7 +523,7 @@ func TestDeploymentStop(t *testing.T) {
}
type noSuchPod struct {
*testclient.FakePods
coreclient.PodInterface
}
func (c *noSuchPod) Get(name string) (*api.Pod, error) {
@@ -618,84 +531,101 @@ func (c *noSuchPod) Get(name string) (*api.Pod, error) {
}
type noDeleteService struct {
*testclient.FakeServices
coreclient.ServiceInterface
}
func (c *noDeleteService) Delete(service string) error {
func (c *noDeleteService) Delete(service string, o *api.DeleteOptions) error {
return fmt.Errorf("I'm afraid I can't do that, Dave")
}
type reaperFake struct {
*testclient.Fake
*fake.Clientset
noSuchPod, noDeleteService bool
}
func (c *reaperFake) Pods(namespace string) client.PodInterface {
pods := &testclient.FakePods{Fake: c.Fake, Namespace: namespace}
func (c *reaperFake) Core() coreclient.CoreInterface {
return &reaperCoreFake{c.Clientset.Core(), c.noSuchPod, c.noDeleteService}
}
type reaperCoreFake struct {
coreclient.CoreInterface
noSuchPod, noDeleteService bool
}
func (c *reaperCoreFake) Pods(namespace string) coreclient.PodInterface {
pods := c.CoreInterface.Pods(namespace)
if c.noSuchPod {
return &noSuchPod{pods}
}
return pods
}
func (c *reaperFake) Services(namespace string) client.ServiceInterface {
services := &testclient.FakeServices{Fake: c.Fake, Namespace: namespace}
func (c *reaperCoreFake) Services(namespace string) coreclient.ServiceInterface {
services := c.CoreInterface.Services(namespace)
if c.noDeleteService {
return &noDeleteService{services}
}
return services
}
func pod() *api.Pod {
return &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}}
}
func service() *api.Service {
return &api.Service{ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}}
}
func TestSimpleStop(t *testing.T) {
tests := []struct {
fake *reaperFake
kind unversioned.GroupKind
actions []testclient.Action
actions []testcore.Action
expectError bool
test string
}{
{
fake: &reaperFake{
Fake: &testclient.Fake{},
Clientset: fake.NewSimpleClientset(pod()),
},
kind: api.Kind("Pod"),
actions: []testclient.Action{
testclient.NewGetAction("pods", api.NamespaceDefault, "foo"),
testclient.NewDeleteAction("pods", api.NamespaceDefault, "foo"),
actions: []testcore.Action{
testcore.NewGetAction(api.Resource("pods").WithVersion(""), api.NamespaceDefault, "foo"),
testcore.NewDeleteAction(api.Resource("pods").WithVersion(""), api.NamespaceDefault, "foo"),
},
expectError: false,
test: "stop pod succeeds",
},
{
fake: &reaperFake{
Fake: &testclient.Fake{},
Clientset: fake.NewSimpleClientset(service()),
},
kind: api.Kind("Service"),
actions: []testclient.Action{
testclient.NewGetAction("services", api.NamespaceDefault, "foo"),
testclient.NewDeleteAction("services", api.NamespaceDefault, "foo"),
actions: []testcore.Action{
testcore.NewGetAction(api.Resource("services").WithVersion(""), api.NamespaceDefault, "foo"),
testcore.NewDeleteAction(api.Resource("services").WithVersion(""), api.NamespaceDefault, "foo"),
},
expectError: false,
test: "stop service succeeds",
},
{
fake: &reaperFake{
Fake: &testclient.Fake{},
Clientset: fake.NewSimpleClientset(),
noSuchPod: true,
},
kind: api.Kind("Pod"),
actions: []testclient.Action{},
actions: []testcore.Action{},
expectError: true,
test: "stop pod fails, no pod",
},
{
fake: &reaperFake{
Fake: &testclient.Fake{},
Clientset: fake.NewSimpleClientset(service()),
noDeleteService: true,
},
kind: api.Kind("Service"),
actions: []testclient.Action{
testclient.NewGetAction("services", api.NamespaceDefault, "foo"),
actions: []testcore.Action{
testcore.NewGetAction(api.Resource("services").WithVersion(""), api.NamespaceDefault, "foo"),
},
expectError: true,
test: "stop service fails, can't delete",
@@ -747,12 +677,9 @@ func TestDeploymentNotFoundError(t *testing.T) {
}
template := deploymentutil.GetNewReplicaSetTemplate(deployment)
fake := &testclient.Fake{}
fake.AddReactor("get", "deployments", func(action testclient.Action) (handled bool, ret runtime.Object, err error) {
return true, deployment, nil
})
fake.AddReactor("list", "replicasets", func(action testclient.Action) (handled bool, ret runtime.Object, err error) {
list := &extensions.ReplicaSetList{Items: []extensions.ReplicaSet{
fake := fake.NewSimpleClientset(
deployment,
&extensions.ReplicaSetList{Items: []extensions.ReplicaSet{
{
ObjectMeta: api.ObjectMeta{
Name: name,
@@ -762,14 +689,14 @@ func TestDeploymentNotFoundError(t *testing.T) {
Template: template,
},
},
}}
return true, list, nil
})
fake.AddReactor("get", "replicasets", func(action testclient.Action) (handled bool, ret runtime.Object, err error) {
},
},
)
fake.AddReactor("get", "replicasets", func(action testcore.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, ScaleError{ActualError: errors.NewNotFound(api.Resource("replicaset"), "doesn't-matter")}
})
reaper := DeploymentReaper{fake, time.Millisecond, time.Millisecond}
reaper := DeploymentReaper{fake.Extensions(), fake.Extensions(), time.Millisecond, time.Millisecond}
if err := reaper.Stop(ns, name, 0, nil); err != nil {
t.Fatalf("unexpected error: %#v", err)
}