From d8c87e93dc8e6cd6c83eeb01d4c4d88bb4b76f00 Mon Sep 17 00:00:00 2001 From: markturansky Date: Thu, 9 Apr 2015 12:02:06 -0400 Subject: [PATCH 1/2] Adding missing UpdateStatus methods to client --- pkg/client/persistentvolumeclaim.go | 7 +++++++ pkg/client/persistentvolumes.go | 7 +++++++ .../testclient/fake_persistent_volume_claims.go | 11 ++++++++--- pkg/client/testclient/fake_persistent_volumes.go | 11 ++++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/pkg/client/persistentvolumeclaim.go b/pkg/client/persistentvolumeclaim.go index 533710774e0..04fa5959d87 100644 --- a/pkg/client/persistentvolumeclaim.go +++ b/pkg/client/persistentvolumeclaim.go @@ -36,6 +36,7 @@ type PersistentVolumeClaimInterface interface { Get(name string) (*api.PersistentVolumeClaim, error) Create(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) Update(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) + UpdateStatus(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) Delete(name string) error Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) } @@ -87,6 +88,12 @@ func (c *persistentVolumeClaims) Update(claim *api.PersistentVolumeClaim) (resul return } +func (c *persistentVolumeClaims) UpdateStatus(claim *api.PersistentVolumeClaim) (result *api.PersistentVolumeClaim, err error) { + result = &api.PersistentVolumeClaim{} + err = c.client.Put().Namespace(c.namespace).Resource("persistentVolumeClaims").Name(claim.Name).SubResource("status").Body(claim).Do().Into(result) + return +} + func (c *persistentVolumeClaims) Delete(name string) error { return c.client.Delete().Namespace(c.namespace).Resource("persistentVolumeClaims").Name(name).Do().Error() } diff --git a/pkg/client/persistentvolumes.go b/pkg/client/persistentvolumes.go index 5a1ef13a285..147be2554cd 100644 --- a/pkg/client/persistentvolumes.go +++ b/pkg/client/persistentvolumes.go @@ -35,6 +35,7 @@ type PersistentVolumeInterface interface { Get(name string) (*api.PersistentVolume, error) Create(volume *api.PersistentVolume) (*api.PersistentVolume, error) Update(volume *api.PersistentVolume) (*api.PersistentVolume, error) + UpdateStatus(persistentVolume *api.PersistentVolume) (*api.PersistentVolume, error) Delete(name string) error Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) } @@ -82,6 +83,12 @@ func (c *persistentVolumes) Update(volume *api.PersistentVolume) (result *api.Pe return } +func (c *persistentVolumes) UpdateStatus(volume *api.PersistentVolume) (result *api.PersistentVolume, err error) { + result = &api.PersistentVolume{} + err = c.client.Put().Resource("persistentVolumes").Name(volume.Name).SubResource("status").Body(volume).Do().Into(result) + return +} + func (c *persistentVolumes) Delete(name string) error { return c.client.Delete().Resource("persistentVolumes").Name(name).Do().Error() } diff --git a/pkg/client/testclient/fake_persistent_volume_claims.go b/pkg/client/testclient/fake_persistent_volume_claims.go index 13604d4d338..407a401485c 100644 --- a/pkg/client/testclient/fake_persistent_volume_claims.go +++ b/pkg/client/testclient/fake_persistent_volume_claims.go @@ -43,13 +43,18 @@ func (c *FakePersistentVolumeClaims) Delete(name string) error { return err } -func (c *FakePersistentVolumeClaims) Create(persistentVolumeClaims *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { +func (c *FakePersistentVolumeClaims) Create(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { obj, err := c.Fake.Invokes(FakeAction{Action: "create-persistentVolumeClaims"}, &api.PersistentVolumeClaim{}) return obj.(*api.PersistentVolumeClaim), err } -func (c *FakePersistentVolumeClaims) Update(persistentVolumeClaims *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { - obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumeClaims", Value: persistentVolumeClaims.Name}, &api.PersistentVolumeClaim{}) +func (c *FakePersistentVolumeClaims) Update(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { + obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumeClaims", Value: claim.Name}, &api.PersistentVolumeClaim{}) + return obj.(*api.PersistentVolumeClaim), err +} + +func (c *FakePersistentVolumeClaims) UpdateStatus(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { + obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumeClaims", Value: claim}, &api.PersistentVolumeClaim{}) return obj.(*api.PersistentVolumeClaim), err } diff --git a/pkg/client/testclient/fake_persistent_volumes.go b/pkg/client/testclient/fake_persistent_volumes.go index 254f225f982..3ff2bbafe0d 100644 --- a/pkg/client/testclient/fake_persistent_volumes.go +++ b/pkg/client/testclient/fake_persistent_volumes.go @@ -43,13 +43,18 @@ func (c *FakePersistentVolumes) Delete(name string) error { return err } -func (c *FakePersistentVolumes) Create(persistentVolumes *api.PersistentVolume) (*api.PersistentVolume, error) { +func (c *FakePersistentVolumes) Create(pv *api.PersistentVolume) (*api.PersistentVolume, error) { obj, err := c.Fake.Invokes(FakeAction{Action: "create-persistentVolumes"}, &api.PersistentVolume{}) return obj.(*api.PersistentVolume), err } -func (c *FakePersistentVolumes) Update(persistentVolumes *api.PersistentVolume) (*api.PersistentVolume, error) { - obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumes", Value: persistentVolumes.Name}, &api.PersistentVolume{}) +func (c *FakePersistentVolumes) Update(pv *api.PersistentVolume) (*api.PersistentVolume, error) { + obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumes", Value: pv.Name}, &api.PersistentVolume{}) + return obj.(*api.PersistentVolume), err +} + +func (c *FakePersistentVolumes) UpdateStatus(pv *api.PersistentVolume) (*api.PersistentVolume, error) { + obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumes", Value: pv}, &api.PersistentVolume{}) return obj.(*api.PersistentVolume), err } From ac4d34e5d7a216be7a9c142eb97ef1127c26eba2 Mon Sep 17 00:00:00 2001 From: markturansky Date: Fri, 10 Apr 2015 11:41:12 -0400 Subject: [PATCH 2/2] Added PV & PVC client tests --- pkg/client/persistentvolume_test.go | 187 ++++++++++++++++ pkg/client/persistentvolumeclaim_test.go | 205 ++++++++++++++++++ .../fake_persistent_volume_claims.go | 2 +- .../testclient/fake_persistent_volumes.go | 2 +- 4 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 pkg/client/persistentvolume_test.go create mode 100644 pkg/client/persistentvolumeclaim_test.go diff --git a/pkg/client/persistentvolume_test.go b/pkg/client/persistentvolume_test.go new file mode 100644 index 00000000000..b72f4d9c441 --- /dev/null +++ b/pkg/client/persistentvolume_test.go @@ -0,0 +1,187 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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 client + +import ( + "net/url" + "testing" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" + "github.com/GoogleCloudPlatform/kubernetes/pkg/fields" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" +) + +func getPersistentVolumesResoureName() string { + if api.PreV1Beta3(testapi.Version()) { + return "persistentVolumes" + } + return "persistentvolumes" +} + +func TestPersistentVolumeCreate(t *testing.T) { + pv := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + }, + Spec: api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/foo"}, + }, + }, + } + + c := &testClient{ + Request: testRequest{ + Method: "POST", + Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", ""), + Query: buildQueryValues("", nil), + Body: pv, + }, + Response: Response{StatusCode: 200, Body: pv}, + } + + response, err := c.Setup().PersistentVolumes().Create(pv) + c.Validate(t, response, err) +} + +func TestPersistentVolumeGet(t *testing.T) { + persistentVolume := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + Namespace: "foo", + }, + Spec: api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/foo"}, + }, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", "abc"), + Query: buildQueryValues("", nil), + Body: nil, + }, + Response: Response{StatusCode: 200, Body: persistentVolume}, + } + + response, err := c.Setup().PersistentVolumes().Get("abc") + c.Validate(t, response, err) +} + +func TestPersistentVolumeList(t *testing.T) { + persistentVolumeList := &api.PersistentVolumeList{ + Items: []api.PersistentVolume{ + { + ObjectMeta: api.ObjectMeta{Name: "foo"}, + }, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", ""), + Query: buildQueryValues("", nil), + Body: nil, + }, + Response: Response{StatusCode: 200, Body: persistentVolumeList}, + } + response, err := c.Setup().PersistentVolumes().List(labels.Everything(), fields.Everything()) + c.Validate(t, response, err) +} + +func TestPersistentVolumeUpdate(t *testing.T) { + persistentVolume := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + ResourceVersion: "1", + }, + Spec: api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/foo"}, + }, + }, + } + c := &testClient{ + Request: testRequest{Method: "PUT", Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", "abc"), Query: buildQueryValues("", nil)}, + Response: Response{StatusCode: 200, Body: persistentVolume}, + } + response, err := c.Setup().PersistentVolumes().Update(persistentVolume) + c.Validate(t, response, err) +} + +func TestPersistentVolumeStatusUpdate(t *testing.T) { + persistentVolume := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + ResourceVersion: "1", + }, + Spec: api.PersistentVolumeSpec{ + Capacity: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + PersistentVolumeSource: api.PersistentVolumeSource{ + HostPath: &api.HostPathVolumeSource{Path: "/foo"}, + }, + }, + Status: api.PersistentVolumeStatus{ + Phase: api.VolumeBound, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "PUT", + Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", "abc") + "/status", + Query: buildQueryValues("", nil)}, + Response: Response{StatusCode: 200, Body: persistentVolume}, + } + response, err := c.Setup().PersistentVolumes().UpdateStatus(persistentVolume) + c.Validate(t, response, err) +} + +func TestPersistentVolumeDelete(t *testing.T) { + c := &testClient{ + Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getPersistentVolumesResoureName(), "", "foo"), Query: buildQueryValues("", nil)}, + Response: Response{StatusCode: 200}, + } + err := c.Setup().PersistentVolumes().Delete("foo") + c.Validate(t, nil, err) +} + +func TestPersistentVolumeWatch(t *testing.T) { + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: "/api/" + testapi.Version() + "/watch/" + getPersistentVolumesResoureName(), + Query: url.Values{"resourceVersion": []string{}}}, + Response: Response{StatusCode: 200}, + } + _, err := c.Setup().PersistentVolumes().Watch(labels.Everything(), fields.Everything(), "") + c.Validate(t, nil, err) +} diff --git a/pkg/client/persistentvolumeclaim_test.go b/pkg/client/persistentvolumeclaim_test.go new file mode 100644 index 00000000000..09ce5789ff6 --- /dev/null +++ b/pkg/client/persistentvolumeclaim_test.go @@ -0,0 +1,205 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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 client + +import ( + "net/url" + "testing" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" + "github.com/GoogleCloudPlatform/kubernetes/pkg/fields" + "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" +) + +func getPersistentVolumeClaimsResoureName() string { + if api.PreV1Beta3(testapi.Version()) { + return "persistentVolumeClaims" + } + return "persistentvolumeclaims" +} + +func TestPersistentVolumeClaimCreate(t *testing.T) { + ns := api.NamespaceDefault + pv := &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + }, + Spec: api.PersistentVolumeClaimSpec{ + AccessModes: []api.AccessModeType{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + }, + }, + } + + c := &testClient{ + Request: testRequest{ + Method: "POST", + Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, ""), + Query: buildQueryValues(ns, nil), + Body: pv, + }, + Response: Response{StatusCode: 200, Body: pv}, + } + + response, err := c.Setup().PersistentVolumeClaims(ns).Create(pv) + c.Validate(t, response, err) +} + +func TestPersistentVolumeClaimGet(t *testing.T) { + ns := api.NamespaceDefault + persistentVolumeClaim := &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + Namespace: "foo", + }, + Spec: api.PersistentVolumeClaimSpec{ + AccessModes: []api.AccessModeType{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + }, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, "abc"), + Query: buildQueryValues(ns, nil), + Body: nil, + }, + Response: Response{StatusCode: 200, Body: persistentVolumeClaim}, + } + + response, err := c.Setup().PersistentVolumeClaims(ns).Get("abc") + c.Validate(t, response, err) +} + +func TestPersistentVolumeClaimList(t *testing.T) { + ns := api.NamespaceDefault + persistentVolumeList := &api.PersistentVolumeClaimList{ + Items: []api.PersistentVolumeClaim{ + { + ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "ns"}, + }, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, ""), + Query: buildQueryValues(ns, nil), + Body: nil, + }, + Response: Response{StatusCode: 200, Body: persistentVolumeList}, + } + response, err := c.Setup().PersistentVolumeClaims(ns).List(labels.Everything(), fields.Everything()) + c.Validate(t, response, err) +} + +func TestPersistentVolumeClaimUpdate(t *testing.T) { + ns := api.NamespaceDefault + persistentVolumeClaim := &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + ResourceVersion: "1", + }, + Spec: api.PersistentVolumeClaimSpec{ + AccessModes: []api.AccessModeType{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + }, + }, + } + c := &testClient{ + Request: testRequest{Method: "PUT", Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, "abc"), Query: buildQueryValues(ns, nil)}, + Response: Response{StatusCode: 200, Body: persistentVolumeClaim}, + } + response, err := c.Setup().PersistentVolumeClaims(ns).Update(persistentVolumeClaim) + c.Validate(t, response, err) +} + +func TestPersistentVolumeClaimStatusUpdate(t *testing.T) { + ns := api.NamespaceDefault + persistentVolumeClaim := &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{ + Name: "abc", + ResourceVersion: "1", + }, + Spec: api.PersistentVolumeClaimSpec{ + AccessModes: []api.AccessModeType{ + api.ReadWriteOnce, + api.ReadOnlyMany, + }, + Resources: api.ResourceRequirements{ + Requests: api.ResourceList{ + api.ResourceName(api.ResourceStorage): resource.MustParse("10G"), + }, + }, + }, + Status: api.PersistentVolumeClaimStatus{ + Phase: api.ClaimBound, + }, + } + c := &testClient{ + Request: testRequest{ + Method: "PUT", + Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, "abc") + "/status", + Query: buildQueryValues(ns, nil)}, + Response: Response{StatusCode: 200, Body: persistentVolumeClaim}, + } + response, err := c.Setup().PersistentVolumeClaims(ns).UpdateStatus(persistentVolumeClaim) + c.Validate(t, response, err) +} + +func TestPersistentVolumeClaimDelete(t *testing.T) { + ns := api.NamespaceDefault + c := &testClient{ + Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getPersistentVolumeClaimsResoureName(), ns, "foo"), Query: buildQueryValues(ns, nil)}, + Response: Response{StatusCode: 200}, + } + err := c.Setup().PersistentVolumeClaims(ns).Delete("foo") + c.Validate(t, nil, err) +} + +func TestPersistentVolumeClaimWatch(t *testing.T) { + c := &testClient{ + Request: testRequest{ + Method: "GET", + Path: "/api/" + testapi.Version() + "/watch/" + getPersistentVolumeClaimsResoureName(), + Query: url.Values{"resourceVersion": []string{}}}, + Response: Response{StatusCode: 200}, + } + _, err := c.Setup().PersistentVolumeClaims(api.NamespaceAll).Watch(labels.Everything(), fields.Everything(), "") + c.Validate(t, nil, err) +} diff --git a/pkg/client/testclient/fake_persistent_volume_claims.go b/pkg/client/testclient/fake_persistent_volume_claims.go index 407a401485c..33cc1a30b79 100644 --- a/pkg/client/testclient/fake_persistent_volume_claims.go +++ b/pkg/client/testclient/fake_persistent_volume_claims.go @@ -54,7 +54,7 @@ func (c *FakePersistentVolumeClaims) Update(claim *api.PersistentVolumeClaim) (* } func (c *FakePersistentVolumeClaims) UpdateStatus(claim *api.PersistentVolumeClaim) (*api.PersistentVolumeClaim, error) { - obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumeClaims", Value: claim}, &api.PersistentVolumeClaim{}) + obj, err := c.Fake.Invokes(FakeAction{Action: "update-status-persistentVolumeClaims", Value: claim}, &api.PersistentVolumeClaim{}) return obj.(*api.PersistentVolumeClaim), err } diff --git a/pkg/client/testclient/fake_persistent_volumes.go b/pkg/client/testclient/fake_persistent_volumes.go index 3ff2bbafe0d..33cbfa1803e 100644 --- a/pkg/client/testclient/fake_persistent_volumes.go +++ b/pkg/client/testclient/fake_persistent_volumes.go @@ -54,7 +54,7 @@ func (c *FakePersistentVolumes) Update(pv *api.PersistentVolume) (*api.Persisten } func (c *FakePersistentVolumes) UpdateStatus(pv *api.PersistentVolume) (*api.PersistentVolume, error) { - obj, err := c.Fake.Invokes(FakeAction{Action: "update-persistentVolumes", Value: pv}, &api.PersistentVolume{}) + obj, err := c.Fake.Invokes(FakeAction{Action: "update-status-persistentVolumes", Value: pv}, &api.PersistentVolume{}) return obj.(*api.PersistentVolume), err }