123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- Copyright 2015 The Kubernetes Authors.
- 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"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/pkg/api/unversioned"
- "k8s.io/kubernetes/pkg/fields"
- "k8s.io/kubernetes/pkg/labels"
- "k8s.io/kubernetes/pkg/registry/generic"
- "k8s.io/kubernetes/pkg/registry/registrytest"
- "k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
- etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
- )
- func newStorage(t *testing.T) (*REST, *etcdtesting.EtcdTestServer) {
- etcdStorage, server := registrytest.NewEtcdStorage(t, "")
- restOptions := generic.RESTOptions{StorageConfig: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1, ResourcePrefix: "namespaces"}
- namespaceStorage, _, _ := NewREST(restOptions)
- return namespaceStorage, server
- }
- func validNewNamespace() *api.Namespace {
- return &api.Namespace{
- ObjectMeta: api.ObjectMeta{
- Name: "foo",
- },
- }
- }
- func TestCreate(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- test := registrytest.New(t, storage.Store).ClusterScope()
- namespace := validNewNamespace()
- namespace.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
- test.TestCreate(
- // valid
- namespace,
- // invalid
- &api.Namespace{
- ObjectMeta: api.ObjectMeta{Name: "bad value"},
- },
- )
- }
- func TestCreateSetsFields(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- namespace := validNewNamespace()
- ctx := api.NewContext()
- _, err := storage.Create(ctx, namespace)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- object, err := storage.Get(ctx, "foo")
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- actual := object.(*api.Namespace)
- if actual.Name != namespace.Name {
- t.Errorf("unexpected namespace: %#v", actual)
- }
- if len(actual.UID) == 0 {
- t.Errorf("expected namespace UID to be set: %#v", actual)
- }
- if actual.Status.Phase != api.NamespaceActive {
- t.Errorf("expected namespace phase to be set to active, but %v", actual.Status.Phase)
- }
- }
- func TestDelete(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- test := registrytest.New(t, storage.Store).ClusterScope().ReturnDeletedObject()
- test.TestDelete(validNewNamespace())
- }
- func TestGet(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- test := registrytest.New(t, storage.Store).ClusterScope()
- test.TestGet(validNewNamespace())
- }
- func TestList(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- test := registrytest.New(t, storage.Store).ClusterScope()
- test.TestList(validNewNamespace())
- }
- func TestWatch(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- test := registrytest.New(t, storage.Store).ClusterScope()
- test.TestWatch(
- validNewNamespace(),
- // matching labels
- []labels.Set{},
- // not matching labels
- []labels.Set{
- {"foo": "bar"},
- },
- // matching fields
- []fields.Set{
- {"metadata.name": "foo"},
- {"name": "foo"},
- },
- // not matching fields
- []fields.Set{
- {"metadata.name": "bar"},
- },
- )
- }
- func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- key := etcdtest.AddPrefix("namespaces/foo")
- ctx := api.NewContext()
- now := unversioned.Now()
- namespace := &api.Namespace{
- ObjectMeta: api.ObjectMeta{
- Name: "foo",
- DeletionTimestamp: &now,
- },
- Spec: api.NamespaceSpec{
- Finalizers: []api.FinalizerName{api.FinalizerKubernetes},
- },
- Status: api.NamespaceStatus{Phase: api.NamespaceActive},
- }
- if err := storage.Storage.Create(ctx, key, namespace, nil, 0); err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if _, err := storage.Delete(ctx, "foo", nil); err == nil {
- t.Errorf("unexpected error: %v", err)
- }
- }
- func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) {
- storage, server := newStorage(t)
- defer server.Terminate(t)
- key := etcdtest.AddPrefix("namespaces/foo")
- ctx := api.NewContext()
- now := unversioned.Now()
- namespace := &api.Namespace{
- ObjectMeta: api.ObjectMeta{
- Name: "foo",
- DeletionTimestamp: &now,
- },
- Spec: api.NamespaceSpec{
- Finalizers: []api.FinalizerName{},
- },
- Status: api.NamespaceStatus{Phase: api.NamespaceActive},
- }
- if err := storage.Storage.Create(ctx, key, namespace, nil, 0); err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if _, err := storage.Delete(ctx, "foo", nil); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- }
|