k3s/pkg/registry/persistentvolume/etcd/etcd_test.go

246 lines
7.0 KiB
Go
Raw Normal View History

2015-03-26 19:50:36 +00:00
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
2015-03-26 19:50:36 +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 etcd
import (
"testing"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/api/testapi"
2015-08-18 15:03:54 +00:00
"k8s.io/kubernetes/pkg/registry/registrytest"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
"k8s.io/kubernetes/pkg/tools/etcdtest"
"k8s.io/kubernetes/pkg/util"
2015-03-26 19:50:36 +00:00
"github.com/coreos/go-etcd/etcd"
)
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient) {
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
storage, statusStorage := NewREST(etcdStorage)
return storage, statusStorage, fakeClient
2015-03-26 19:50:36 +00:00
}
func validNewPersistentVolume(name string) *api.PersistentVolume {
pv := &api.PersistentVolume{
ObjectMeta: api.ObjectMeta{
Name: name,
},
Spec: api.PersistentVolumeSpec{
Capacity: api.ResourceList{
api.ResourceName(api.ResourceStorage): resource.MustParse("10G"),
},
AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
2015-03-26 19:50:36 +00:00
PersistentVolumeSource: api.PersistentVolumeSource{
HostPath: &api.HostPathVolumeSource{Path: "/foo"},
},
2015-05-29 20:12:10 +00:00
PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRetain,
2015-03-26 19:50:36 +00:00
},
Status: api.PersistentVolumeStatus{
2015-05-29 20:12:10 +00:00
Phase: api.VolumePending,
Message: "bar",
Reason: "foo",
},
2015-03-26 19:50:36 +00:00
}
return pv
}
func validChangedPersistentVolume() *api.PersistentVolume {
pv := validNewPersistentVolume("foo")
pv.ResourceVersion = "1"
return pv
}
func TestCreate(t *testing.T) {
storage, _, fakeClient := newStorage(t)
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
2015-03-26 19:50:36 +00:00
pv := validNewPersistentVolume("foo")
pv.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
2015-03-26 19:50:36 +00:00
test.TestCreate(
// valid
pv,
2015-08-20 14:16:39 +00:00
func(ctx api.Context, obj runtime.Object) error {
return registrytest.SetObject(fakeClient, storage.KeyFunc, ctx, obj)
},
func(ctx api.Context, obj runtime.Object) (runtime.Object, error) {
return registrytest.GetObject(fakeClient, storage.KeyFunc, storage.NewFunc, ctx, obj)
},
2015-03-26 19:50:36 +00:00
// invalid
&api.PersistentVolume{
ObjectMeta: api.ObjectMeta{Name: "*BadName!"},
},
)
}
2015-08-28 13:45:38 +00:00
func TestUpdate(t *testing.T) {
storage, _, fakeClient := newStorage(t)
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
test.TestUpdate(
// valid
validNewPersistentVolume("foo"),
func(ctx api.Context, obj runtime.Object) error {
return registrytest.SetObject(fakeClient, storage.KeyFunc, ctx, obj)
},
func(resourceVersion uint64) {
registrytest.SetResourceVersion(fakeClient, resourceVersion)
},
func(ctx api.Context, obj runtime.Object) (runtime.Object, error) {
return registrytest.GetObject(fakeClient, storage.KeyFunc, storage.NewFunc, ctx, obj)
},
// updateFunc
func(obj runtime.Object) runtime.Object {
object := obj.(*api.PersistentVolume)
object.Spec.Capacity = api.ResourceList{
api.ResourceName(api.ResourceStorage): resource.MustParse("20G"),
}
return object
},
)
}
2015-03-26 19:50:36 +00:00
func TestDelete(t *testing.T) {
ctx := api.NewContext()
storage, _, fakeClient := newStorage(t)
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
2015-03-26 19:50:36 +00:00
pv := validChangedPersistentVolume()
key, _ := storage.KeyFunc(ctx, pv.Name)
key = etcdtest.AddPrefix(key)
2015-03-26 19:50:36 +00:00
createFn := func() runtime.Object {
fakeClient.Data[key] = tools.EtcdResponseWithError{
2015-03-26 19:50:36 +00:00
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(testapi.Codec(), pv),
2015-03-26 19:50:36 +00:00
ModifiedIndex: 1,
},
},
}
return pv
}
gracefulSetFn := func() bool {
if fakeClient.Data[key].R.Node == nil {
2015-03-26 19:50:36 +00:00
return false
}
return fakeClient.Data[key].R.Node.TTL == 30
2015-03-26 19:50:36 +00:00
}
test.TestDelete(createFn, gracefulSetFn)
2015-03-26 19:50:36 +00:00
}
func TestEtcdGetPersistentVolumes(t *testing.T) {
storage, _, fakeClient := newStorage(t)
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
2015-03-26 19:50:36 +00:00
persistentVolume := validNewPersistentVolume("foo")
test.TestGet(persistentVolume)
2015-03-26 19:50:36 +00:00
}
2015-08-18 15:03:54 +00:00
func TestEtcdListPersistentVolumes(t *testing.T) {
storage, _, fakeClient := newStorage(t)
2015-08-18 15:03:54 +00:00
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
key := etcdtest.AddPrefix(storage.KeyRootFunc(test.TestContext()))
persistentVolume := validNewPersistentVolume("foo")
test.TestList(
persistentVolume,
func(objects []runtime.Object) []runtime.Object {
return registrytest.SetObjectsForKey(fakeClient, key, objects)
2015-03-26 19:50:36 +00:00
},
2015-08-18 15:03:54 +00:00
func(resourceVersion uint64) {
registrytest.SetResourceVersion(fakeClient, resourceVersion)
})
2015-03-26 19:50:36 +00:00
}
func TestPersistentVolumesDecode(t *testing.T) {
storage, _, _ := newStorage(t)
2015-03-26 19:50:36 +00:00
expected := validNewPersistentVolume("foo")
body, err := testapi.Codec().Encode(expected)
2015-03-26 19:50:36 +00:00
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
actual := storage.New()
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
2015-03-26 19:50:36 +00:00
t.Fatalf("unexpected error: %v", err)
}
if !api.Semantic.DeepEqual(expected, actual) {
t.Errorf("mismatch: %s", util.ObjectDiff(expected, actual))
}
}
func TestDeletePersistentVolumes(t *testing.T) {
ctx := api.NewContext()
storage, _, fakeClient := newStorage(t)
2015-03-26 19:50:36 +00:00
persistentVolume := validNewPersistentVolume("foo")
name := persistentVolume.Name
key, _ := storage.KeyFunc(ctx, name)
key = etcdtest.AddPrefix(key)
2015-03-26 19:50:36 +00:00
fakeClient.ChangeIndex = 1
fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(testapi.Codec(), persistentVolume),
2015-03-26 19:50:36 +00:00
ModifiedIndex: 1,
CreatedIndex: 1,
},
},
}
_, err := storage.Delete(ctx, name, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestEtcdUpdateStatus(t *testing.T) {
storage, statusStorage, fakeClient := newStorage(t)
2015-03-26 19:50:36 +00:00
fakeClient.TestIndex = true
ctx := api.NewContext()
2015-03-26 19:50:36 +00:00
key, _ := storage.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
2015-03-26 19:50:36 +00:00
pvStart := validNewPersistentVolume("foo")
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), pvStart), 0)
2015-03-26 19:50:36 +00:00
pvIn := &api.PersistentVolume{
ObjectMeta: api.ObjectMeta{
Name: "foo",
ResourceVersion: "1",
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
},
}
expected := *pvStart
expected.ResourceVersion = "2"
expected.Labels = pvIn.Labels
expected.Status = pvIn.Status
_, _, err := statusStorage.Update(ctx, pvIn)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
pvOut, err := storage.Get(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
2015-03-26 19:50:36 +00:00
}
if !api.Semantic.DeepEqual(&expected, pvOut) {
t.Errorf("unexpected object: %s", util.ObjectDiff(&expected, pvOut))
2015-03-26 19:50:36 +00:00
}
}