2015-09-24 16:29:40 +00:00
|
|
|
// +build integration,!no-etcd
|
2014-08-23 01:12:16 +00:00
|
|
|
|
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-08-23 01:12:16 +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 integration
|
|
|
|
|
|
|
|
import (
|
2014-10-07 20:51:28 +00:00
|
|
|
"strconv"
|
2014-08-23 01:12:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
|
|
|
"k8s.io/kubernetes/pkg/storage"
|
2015-12-10 14:03:59 +00:00
|
|
|
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
2015-11-10 11:23:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
|
|
|
"k8s.io/kubernetes/test/integration/framework"
|
2015-10-09 14:49:01 +00:00
|
|
|
|
2015-12-10 14:03:59 +00:00
|
|
|
etcd "github.com/coreos/etcd/client"
|
2015-10-09 14:49:01 +00:00
|
|
|
"golang.org/x/net/context"
|
2014-08-23 01:12:16 +00:00
|
|
|
)
|
|
|
|
|
2015-07-27 09:59:09 +00:00
|
|
|
func TestSet(t *testing.T) {
|
2015-04-09 21:50:27 +00:00
|
|
|
client := framework.NewEtcdClient()
|
2015-12-10 14:03:59 +00:00
|
|
|
keysAPI := etcd.NewKeysAPI(client)
|
2016-01-26 06:17:11 +00:00
|
|
|
etcdStorage := etcdstorage.NewEtcdStorage(client, testapi.Default.Codec(), "", false)
|
2015-10-09 14:49:01 +00:00
|
|
|
ctx := context.TODO()
|
2015-04-09 21:50:27 +00:00
|
|
|
framework.WithEtcdKey(func(key string) {
|
2015-07-24 11:09:49 +00:00
|
|
|
testObject := api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2015-10-09 14:49:01 +00:00
|
|
|
if err := etcdStorage.Set(ctx, key, &testObject, nil, 0); err != nil {
|
2014-08-23 01:12:16 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-12-10 14:03:59 +00:00
|
|
|
resp, err := keysAPI.Get(ctx, key, nil)
|
2014-08-23 01:12:16 +00:00
|
|
|
if err != nil || resp.Node == nil {
|
|
|
|
t.Fatalf("unexpected error: %v %v", err, resp)
|
|
|
|
}
|
2015-12-21 05:27:49 +00:00
|
|
|
decoded, err := runtime.Decode(testapi.Default.Codec(), []byte(resp.Node.Value))
|
2015-07-24 11:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected response: %#v", resp.Node)
|
|
|
|
}
|
|
|
|
result := *decoded.(*api.ServiceAccount)
|
|
|
|
if !api.Semantic.DeepEqual(testObject, result) {
|
|
|
|
t.Errorf("expected: %#v got: %#v", testObject, result)
|
2014-08-23 01:12:16 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:59:09 +00:00
|
|
|
func TestGet(t *testing.T) {
|
2015-04-09 21:50:27 +00:00
|
|
|
client := framework.NewEtcdClient()
|
2015-12-10 14:03:59 +00:00
|
|
|
keysAPI := etcd.NewKeysAPI(client)
|
2016-01-26 06:17:11 +00:00
|
|
|
etcdStorage := etcdstorage.NewEtcdStorage(client, testapi.Default.Codec(), "", false)
|
2015-10-09 14:49:01 +00:00
|
|
|
ctx := context.TODO()
|
2015-04-09 21:50:27 +00:00
|
|
|
framework.WithEtcdKey(func(key string) {
|
2015-07-24 11:09:49 +00:00
|
|
|
testObject := api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2015-12-21 05:27:49 +00:00
|
|
|
coded, err := runtime.Encode(testapi.Default.Codec(), &testObject)
|
2015-07-24 11:09:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-12-10 14:03:59 +00:00
|
|
|
_, err = keysAPI.Set(ctx, key, string(coded), nil)
|
2014-08-23 01:12:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-07-24 11:09:49 +00:00
|
|
|
result := api.ServiceAccount{}
|
2015-10-09 14:49:01 +00:00
|
|
|
if err := etcdStorage.Get(ctx, key, &result, false); err != nil {
|
2014-08-23 01:12:16 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2015-07-24 11:09:49 +00:00
|
|
|
// Propagate ResourceVersion (it is set automatically).
|
|
|
|
testObject.ObjectMeta.ResourceVersion = result.ObjectMeta.ResourceVersion
|
|
|
|
if !api.Semantic.DeepEqual(testObject, result) {
|
|
|
|
t.Errorf("expected: %#v got: %#v", testObject, result)
|
2014-08-23 01:12:16 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-19 23:59:43 +00:00
|
|
|
func TestWriteTTL(t *testing.T) {
|
|
|
|
client := framework.NewEtcdClient()
|
2015-12-10 14:03:59 +00:00
|
|
|
keysAPI := etcd.NewKeysAPI(client)
|
2016-01-26 06:17:11 +00:00
|
|
|
etcdStorage := etcdstorage.NewEtcdStorage(client, testapi.Default.Codec(), "", false)
|
2015-10-09 14:49:01 +00:00
|
|
|
ctx := context.TODO()
|
2015-08-19 23:59:43 +00:00
|
|
|
framework.WithEtcdKey(func(key string) {
|
|
|
|
testObject := api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2015-10-09 14:49:01 +00:00
|
|
|
if err := etcdStorage.Set(ctx, key, &testObject, nil, 0); err != nil {
|
2015-08-19 23:59:43 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
result := &api.ServiceAccount{}
|
2016-03-21 06:15:00 +00:00
|
|
|
err := etcdStorage.GuaranteedUpdate(ctx, key, result, false, nil, func(obj runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
|
2015-08-19 23:59:43 +00:00
|
|
|
if in, ok := obj.(*api.ServiceAccount); !ok || in.Name != "foo" {
|
|
|
|
t.Fatalf("unexpected existing object: %v", obj)
|
|
|
|
}
|
|
|
|
if res.TTL != 0 {
|
|
|
|
t.Fatalf("unexpected TTL: %#v", res)
|
|
|
|
}
|
|
|
|
ttl := uint64(10)
|
|
|
|
out := &api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "out"}}
|
|
|
|
return out, &ttl, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if result.Name != "out" {
|
|
|
|
t.Errorf("unexpected response: %#v", result)
|
|
|
|
}
|
2015-12-10 14:03:59 +00:00
|
|
|
if res, err := keysAPI.Get(ctx, key, nil); err != nil || res == nil || res.Node.TTL != 10 {
|
2015-08-19 23:59:43 +00:00
|
|
|
t.Fatalf("unexpected get: %v %#v", err, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = &api.ServiceAccount{}
|
2016-03-21 06:15:00 +00:00
|
|
|
err = etcdStorage.GuaranteedUpdate(ctx, key, result, false, nil, func(obj runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
|
2015-08-19 23:59:43 +00:00
|
|
|
if in, ok := obj.(*api.ServiceAccount); !ok || in.Name != "out" {
|
|
|
|
t.Fatalf("unexpected existing object: %v", obj)
|
|
|
|
}
|
|
|
|
if res.TTL <= 1 {
|
|
|
|
t.Fatalf("unexpected TTL: %#v", res)
|
|
|
|
}
|
|
|
|
out := &api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "out2"}}
|
|
|
|
return out, nil, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if result.Name != "out2" {
|
|
|
|
t.Errorf("unexpected response: %#v", result)
|
|
|
|
}
|
2015-12-10 14:03:59 +00:00
|
|
|
if res, err := keysAPI.Get(ctx, key, nil); err != nil || res == nil || res.Node.TTL <= 1 {
|
2015-08-19 23:59:43 +00:00
|
|
|
t.Fatalf("unexpected get: %v %#v", err, res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-23 01:12:16 +00:00
|
|
|
func TestWatch(t *testing.T) {
|
2015-04-09 21:50:27 +00:00
|
|
|
client := framework.NewEtcdClient()
|
2015-12-10 14:03:59 +00:00
|
|
|
keysAPI := etcd.NewKeysAPI(client)
|
2016-01-26 06:17:11 +00:00
|
|
|
etcdStorage := etcdstorage.NewEtcdStorage(client, testapi.Default.Codec(), etcdtest.PathPrefix(), false)
|
2015-10-09 14:49:01 +00:00
|
|
|
ctx := context.TODO()
|
2015-04-09 21:50:27 +00:00
|
|
|
framework.WithEtcdKey(func(key string) {
|
2015-03-11 17:10:09 +00:00
|
|
|
key = etcdtest.AddPrefix(key)
|
2015-12-10 14:03:59 +00:00
|
|
|
resp, err := keysAPI.Set(ctx, key, runtime.EncodeOrDie(testapi.Default.Codec(), &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}), nil)
|
2014-08-23 01:12:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
expectedVersion := resp.Node.ModifiedIndex
|
|
|
|
|
|
|
|
// watch should load the object at the current index
|
2015-12-04 08:58:24 +00:00
|
|
|
w, err := etcdStorage.Watch(ctx, key, "0", storage.Everything)
|
2015-04-17 16:23:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-08-23 01:12:16 +00:00
|
|
|
event := <-w.ResultChan()
|
|
|
|
if event.Type != watch.Added || event.Object == nil {
|
|
|
|
t.Fatalf("expected first value to be set to ADDED, got %#v", event)
|
|
|
|
}
|
|
|
|
|
|
|
|
// version should match what we set
|
|
|
|
pod := event.Object.(*api.Pod)
|
2014-10-07 20:51:28 +00:00
|
|
|
if pod.ResourceVersion != strconv.FormatUint(expectedVersion, 10) {
|
2014-08-23 01:12:16 +00:00
|
|
|
t.Errorf("expected version %d, got %#v", expectedVersion, pod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// should be no events in the stream
|
|
|
|
select {
|
|
|
|
case event, ok := <-w.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("channel closed unexpectedly")
|
|
|
|
}
|
|
|
|
t.Fatalf("unexpected object in channel: %#v", event)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// should return the previously deleted item in the watch, but with the latest index
|
2015-12-10 14:03:59 +00:00
|
|
|
resp, err = keysAPI.Delete(ctx, key, nil)
|
2014-08-23 01:12:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
expectedVersion = resp.Node.ModifiedIndex
|
|
|
|
event = <-w.ResultChan()
|
|
|
|
if event.Type != watch.Deleted {
|
2014-08-25 00:34:05 +00:00
|
|
|
t.Errorf("expected deleted event %#v", event)
|
2014-08-23 01:12:16 +00:00
|
|
|
}
|
|
|
|
pod = event.Object.(*api.Pod)
|
2014-10-07 20:51:28 +00:00
|
|
|
if pod.ResourceVersion != strconv.FormatUint(expectedVersion, 10) {
|
2014-08-23 01:12:16 +00:00
|
|
|
t.Errorf("expected version %d, got %#v", expectedVersion, pod)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|