2015-03-13 14:49:38 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-13 14:49:38 +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 (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
2015-11-05 15:04:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/registry/generic"
|
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"
|
2015-11-03 19:43:27 +00:00
|
|
|
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
|
2015-03-13 14:49:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeConnectionInfoGetter struct {
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:18:45 +00:00
|
|
|
func (fakeConnectionInfoGetter) GetConnectionInfo(ctx api.Context, nodeName string) (string, uint, http.RoundTripper, error) {
|
2015-03-13 14:49:38 +00:00
|
|
|
return "http", 12345, nil, nil
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:43:27 +00:00
|
|
|
func newStorage(t *testing.T) (*REST, *etcdtesting.EtcdTestServer) {
|
|
|
|
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
|
2016-03-23 23:45:24 +00:00
|
|
|
restOptions := generic.RESTOptions{Storage: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1}
|
2016-02-22 09:15:22 +00:00
|
|
|
storage := NewStorage(restOptions, fakeConnectionInfoGetter{}, nil)
|
2015-11-30 11:48:23 +00:00
|
|
|
return storage.Node, server
|
2015-03-13 14:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validNewNode() *api.Node {
|
|
|
|
return &api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
},
|
|
|
|
},
|
2015-03-24 17:24:07 +00:00
|
|
|
Spec: api.NodeSpec{
|
|
|
|
ExternalID: "external",
|
2015-03-25 13:44:40 +00:00
|
|
|
},
|
|
|
|
Status: api.NodeStatus{
|
2015-03-24 17:24:07 +00:00
|
|
|
Capacity: api.ResourceList{
|
|
|
|
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
|
|
|
|
api.ResourceName(api.ResourceMemory): resource.MustParse("0"),
|
|
|
|
},
|
|
|
|
},
|
2015-03-13 14:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreate(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-03-13 14:49:38 +00:00
|
|
|
node := validNewNode()
|
2015-05-19 03:17:51 +00:00
|
|
|
node.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
|
2015-03-13 14:49:38 +00:00
|
|
|
test.TestCreate(
|
|
|
|
// valid
|
|
|
|
node,
|
|
|
|
// invalid
|
|
|
|
&api.Node{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "_-a123-a_"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-08-28 13:45:38 +00:00
|
|
|
func TestUpdate(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-08-28 13:45:38 +00:00
|
|
|
test.TestUpdate(
|
|
|
|
// valid
|
|
|
|
validNewNode(),
|
|
|
|
// updateFunc
|
|
|
|
func(obj runtime.Object) runtime.Object {
|
|
|
|
object := obj.(*api.Node)
|
|
|
|
object.Spec.Unschedulable = !object.Spec.Unschedulable
|
|
|
|
return object
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
func TestDelete(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-09-01 13:27:13 +00:00
|
|
|
test.TestDelete(validNewNode())
|
2015-03-13 14:49:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func TestGet(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-08-31 12:11:11 +00:00
|
|
|
test.TestGet(validNewNode())
|
2015-03-13 14:49:38 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func TestList(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-08-31 12:11:11 +00:00
|
|
|
test.TestList(validNewNode())
|
2015-03-13 14:49:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 13:27:13 +00:00
|
|
|
func TestWatch(t *testing.T) {
|
2015-11-03 19:43:27 +00:00
|
|
|
storage, server := newStorage(t)
|
|
|
|
defer server.Terminate(t)
|
2016-04-08 19:57:28 +00:00
|
|
|
test := registrytest.New(t, storage.Store).ClusterScope()
|
2015-08-28 09:24:34 +00:00
|
|
|
test.TestWatch(
|
|
|
|
validNewNode(),
|
|
|
|
// matching labels
|
|
|
|
[]labels.Set{
|
|
|
|
{"name": "foo"},
|
|
|
|
},
|
|
|
|
// not matching labels
|
|
|
|
[]labels.Set{
|
|
|
|
{"name": "bar"},
|
|
|
|
{"foo": "bar"},
|
|
|
|
},
|
|
|
|
// matching fields
|
|
|
|
[]fields.Set{
|
|
|
|
{"metadata.name": "foo"},
|
|
|
|
},
|
|
|
|
// not matchin fields
|
|
|
|
[]fields.Set{
|
|
|
|
{"metadata.name": "bar"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|