2015-08-07 05:36:39 +00:00
|
|
|
/*
|
2015-08-07 06:29:31 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
2015-08-07 05:36:39 +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
|
2015-08-07 06:29:31 +00:00
|
|
|
|
2015-08-07 05:36:39 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-07 06:29:31 +00:00
|
|
|
|
2015-08-07 05:36:39 +00:00
|
|
|
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"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-08-07 05:36:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-08-20 14:16:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/registrytest"
|
2015-08-07 05:36:39 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/tools"
|
|
|
|
)
|
|
|
|
|
2015-09-25 02:51:09 +00:00
|
|
|
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient) {
|
2015-10-09 22:14:03 +00:00
|
|
|
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t, "extensions")
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, statusStorage := NewREST(etcdStorage)
|
|
|
|
return storage, statusStorage, fakeClient
|
2015-08-07 05:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
func newValidDaemonSet() *extensions.DaemonSet {
|
|
|
|
return &extensions.DaemonSet{
|
2015-08-07 05:36:39 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-08-28 13:45:38 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
2015-08-07 05:36:39 +00:00
|
|
|
},
|
2015-10-09 22:49:10 +00:00
|
|
|
Spec: extensions.DaemonSetSpec{
|
2015-08-28 13:45:38 +00:00
|
|
|
Selector: map[string]string{"a": "b"},
|
|
|
|
Template: &api.PodTemplateSpec{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Labels: map[string]string{"a": "b"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Image: "test_image",
|
|
|
|
ImagePullPolicy: api.PullIfNotPresent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-08-07 05:36:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-08-28 13:45:38 +00:00
|
|
|
}
|
2015-08-07 05:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:58:25 +00:00
|
|
|
var validDaemonSet = newValidDaemonSet()
|
2015-08-07 05:36:39 +00:00
|
|
|
|
2015-08-20 14:16:39 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-08-31 12:11:11 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-09-08 23:58:25 +00:00
|
|
|
ds := newValidDaemonSet()
|
|
|
|
ds.ObjectMeta = api.ObjectMeta{}
|
2015-08-20 14:16:39 +00:00
|
|
|
test.TestCreate(
|
|
|
|
// valid
|
2015-09-08 23:58:25 +00:00
|
|
|
ds,
|
2015-08-28 13:45:38 +00:00
|
|
|
// invalid (invalid selector)
|
2015-10-09 22:49:10 +00:00
|
|
|
&extensions.DaemonSet{
|
|
|
|
Spec: extensions.DaemonSetSpec{
|
2015-08-20 14:16:39 +00:00
|
|
|
Selector: map[string]string{},
|
2015-09-08 23:58:25 +00:00
|
|
|
Template: validDaemonSet.Spec.Template,
|
2015-08-20 14:16:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-08-28 13:45:38 +00:00
|
|
|
func TestUpdate(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-08-31 12:11:11 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-08-28 13:45:38 +00:00
|
|
|
test.TestUpdate(
|
|
|
|
// valid
|
2015-09-08 23:58:25 +00:00
|
|
|
newValidDaemonSet(),
|
2015-08-28 13:45:38 +00:00
|
|
|
// updateFunc
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
2015-10-09 22:49:10 +00:00
|
|
|
object := obj.(*extensions.DaemonSet)
|
2015-08-28 13:45:38 +00:00
|
|
|
object.Spec.Template.Spec.NodeSelector = map[string]string{"c": "d"}
|
|
|
|
return object
|
2015-08-07 05:36:39 +00:00
|
|
|
},
|
2015-08-28 13:45:38 +00:00
|
|
|
// invalid updateFunc
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
2015-10-09 22:49:10 +00:00
|
|
|
object := obj.(*extensions.DaemonSet)
|
2015-08-28 13:45:38 +00:00
|
|
|
object.UID = "newUID"
|
|
|
|
return object
|
|
|
|
},
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
2015-10-09 22:49:10 +00:00
|
|
|
object := obj.(*extensions.DaemonSet)
|
2015-08-28 13:45:38 +00:00
|
|
|
object.Name = ""
|
|
|
|
return object
|
|
|
|
},
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
2015-10-09 22:49:10 +00:00
|
|
|
object := obj.(*extensions.DaemonSet)
|
2015-08-28 13:45:38 +00:00
|
|
|
object.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
|
|
|
|
return object
|
|
|
|
},
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
2015-10-09 22:49:10 +00:00
|
|
|
object := obj.(*extensions.DaemonSet)
|
2015-08-28 13:45:38 +00:00
|
|
|
object.Spec.Selector = map[string]string{}
|
|
|
|
return object
|
|
|
|
},
|
|
|
|
)
|
2015-08-07 05:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 13:27:13 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-09-01 13:27:13 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-09-08 23:58:25 +00:00
|
|
|
test.TestDelete(newValidDaemonSet())
|
2015-09-01 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func TestGet(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-08-31 12:11:11 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-09-08 23:58:25 +00:00
|
|
|
test.TestGet(newValidDaemonSet())
|
2015-08-07 05:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func TestList(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-08-31 12:11:11 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-09-08 23:58:25 +00:00
|
|
|
test.TestList(newValidDaemonSet())
|
2015-08-07 05:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func TestWatch(t *testing.T) {
|
2015-09-25 02:51:09 +00:00
|
|
|
storage, _, fakeClient := newStorage(t)
|
2015-08-31 12:11:11 +00:00
|
|
|
test := registrytest.New(t, fakeClient, storage.Etcd)
|
2015-08-28 09:24:34 +00:00
|
|
|
test.TestWatch(
|
2015-09-08 23:58:25 +00:00
|
|
|
validDaemonSet,
|
2015-08-28 09:24:34 +00:00
|
|
|
// matching labels
|
|
|
|
[]labels.Set{
|
|
|
|
{"a": "b"},
|
|
|
|
},
|
|
|
|
// not matching labels
|
|
|
|
[]labels.Set{
|
|
|
|
{"a": "c"},
|
|
|
|
{"foo": "bar"},
|
|
|
|
},
|
|
|
|
// matching fields
|
|
|
|
[]fields.Set{
|
|
|
|
{"metadata.name": "foo"},
|
|
|
|
},
|
|
|
|
// notmatching fields
|
|
|
|
[]fields.Set{
|
|
|
|
{"metadata.name": "bar"},
|
|
|
|
{"name": "foo"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2015-09-25 02:51:09 +00:00
|
|
|
|
|
|
|
// TODO TestUpdateStatus
|