2014-06-23 18:32:11 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-06-23 18:32:11 +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.
|
|
|
|
*/
|
|
|
|
|
2014-06-30 19:00:14 +00:00
|
|
|
package tools
|
2014-06-17 23:23:52 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-14 19:51:20 +00:00
|
|
|
"errors"
|
2014-06-17 23:23:52 +00:00
|
|
|
"fmt"
|
2015-05-28 12:39:32 +00:00
|
|
|
"math/rand"
|
|
|
|
"net"
|
2015-03-03 18:22:42 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-03-11 17:10:09 +00:00
|
|
|
"path"
|
2014-06-17 23:23:52 +00:00
|
|
|
"reflect"
|
2015-05-28 12:39:32 +00:00
|
|
|
"strconv"
|
2014-08-05 18:43:19 +00:00
|
|
|
"sync"
|
2014-06-17 23:23:52 +00:00
|
|
|
"testing"
|
2015-05-28 12:39:32 +00:00
|
|
|
"time"
|
2014-06-17 23:23:52 +00:00
|
|
|
|
2014-07-20 06:26:26 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-11-13 15:38:13 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
2015-01-26 17:52:50 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
2014-09-02 17:55:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-11 17:10:09 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools/etcdtest"
|
2014-06-17 23:23:52 +00:00
|
|
|
"github.com/coreos/go-etcd/etcd"
|
2015-03-03 18:22:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-06-17 23:23:52 +00:00
|
|
|
)
|
|
|
|
|
2015-05-04 22:45:33 +00:00
|
|
|
const validEtcdVersion = "etcd 2.0.9"
|
|
|
|
|
2014-07-31 11:33:29 +00:00
|
|
|
type TestResource struct {
|
2014-12-01 05:31:52 +00:00
|
|
|
api.TypeMeta `json:",inline"`
|
|
|
|
api.ObjectMeta `json:"metadata"`
|
|
|
|
Value int `json:"value"`
|
2014-07-31 11:33:29 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 04:14:18 +00:00
|
|
|
func (*TestResource) IsAnAPIObject() {}
|
|
|
|
|
|
|
|
var scheme *runtime.Scheme
|
2014-09-11 17:02:53 +00:00
|
|
|
var codec runtime.Codec
|
2014-08-04 00:23:56 +00:00
|
|
|
|
2014-07-31 11:33:29 +00:00
|
|
|
func init() {
|
2014-09-11 17:02:53 +00:00
|
|
|
scheme = runtime.NewScheme()
|
2014-09-08 04:14:18 +00:00
|
|
|
scheme.AddKnownTypes("", &TestResource{})
|
2015-04-08 23:28:28 +00:00
|
|
|
scheme.AddKnownTypes(testapi.Version(), &TestResource{})
|
|
|
|
codec = runtime.CodecFor(scheme, testapi.Version())
|
2015-01-26 17:52:50 +00:00
|
|
|
scheme.AddConversionFuncs(
|
|
|
|
func(in *TestResource, out *TestResource, s conversion.Scope) error {
|
|
|
|
*out = *in
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2014-07-31 11:33:29 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 23:07:20 +00:00
|
|
|
func TestIsEtcdNotFound(t *testing.T) {
|
2014-06-17 23:23:52 +00:00
|
|
|
try := func(err error, isNotFound bool) {
|
|
|
|
if IsEtcdNotFound(err) != isNotFound {
|
|
|
|
t.Errorf("Expected %#v to return %v, but it did not", err, isNotFound)
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 11:48:06 +00:00
|
|
|
try(EtcdErrorNotFound, true)
|
2014-06-17 23:23:52 +00:00
|
|
|
try(&etcd.EtcdError{ErrorCode: 101}, false)
|
|
|
|
try(nil, false)
|
|
|
|
try(fmt.Errorf("some other kind of error"), false)
|
|
|
|
}
|
|
|
|
|
2015-04-08 23:28:28 +00:00
|
|
|
// Returns an encoded version of api.Pod with the given name.
|
|
|
|
func getEncodedPod(name string) string {
|
|
|
|
pod, _ := testapi.Codec().Encode(&api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: name},
|
|
|
|
})
|
|
|
|
return string(pod)
|
|
|
|
}
|
|
|
|
|
2014-09-23 23:58:08 +00:00
|
|
|
func TestExtractToList(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
fakeClient.Data[key] = EtcdResponseWithError{
|
2014-06-17 23:23:52 +00:00
|
|
|
R: &etcd.Response{
|
2014-08-29 00:48:07 +00:00
|
|
|
EtcdIndex: 10,
|
2014-06-17 23:23:52 +00:00
|
|
|
Node: &etcd.Node{
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: true,
|
2014-06-17 23:23:52 +00:00
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/foo",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("foo"),
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: false,
|
2014-08-11 20:29:15 +00:00
|
|
|
ModifiedIndex: 1,
|
2014-06-17 23:23:52 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/bar",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("bar"),
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: false,
|
2014-08-11 20:29:15 +00:00
|
|
|
ModifiedIndex: 2,
|
2014-06-17 23:23:52 +00:00
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/baz",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("baz"),
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: false,
|
2014-08-11 20:29:15 +00:00
|
|
|
ModifiedIndex: 3,
|
2014-06-17 23:23:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-09-23 23:58:08 +00:00
|
|
|
expect := api.PodList{
|
2014-10-23 20:51:34 +00:00
|
|
|
ListMeta: api.ListMeta{ResourceVersion: "10"},
|
2014-09-23 23:58:08 +00:00
|
|
|
Items: []api.Pod{
|
2015-01-26 17:52:50 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", ResourceVersion: "3"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
2014-09-23 23:58:08 +00:00
|
|
|
},
|
2014-06-17 23:23:52 +00:00
|
|
|
}
|
2014-08-11 20:29:15 +00:00
|
|
|
|
2014-09-23 23:58:08 +00:00
|
|
|
var got api.PodList
|
|
|
|
err := helper.ExtractToList("/some/key", &got)
|
2014-06-17 23:23:52 +00:00
|
|
|
if err != nil {
|
2014-09-23 23:58:08 +00:00
|
|
|
t.Errorf("Unexpected error %v", err)
|
2014-08-29 00:48:07 +00:00
|
|
|
}
|
2014-09-23 23:58:08 +00:00
|
|
|
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", e, a)
|
2014-06-17 23:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 15:23:25 +00:00
|
|
|
// TestExtractToListAcrossDirectories ensures that the client excludes directories and flattens tree-response - simulates cross-namespace query
|
|
|
|
func TestExtractToListAcrossDirectories(t *testing.T) {
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
fakeClient.Data[key] = EtcdResponseWithError{
|
2014-10-03 15:23:25 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
EtcdIndex: 10,
|
|
|
|
Node: &etcd.Node{
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: true,
|
2014-10-03 15:23:25 +00:00
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/directory1",
|
2014-10-22 17:02:02 +00:00
|
|
|
Value: `{"name": "directory1"}`,
|
2014-10-03 15:23:25 +00:00
|
|
|
Dir: true,
|
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/foo",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("foo"),
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: false,
|
|
|
|
ModifiedIndex: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "/baz",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("baz"),
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: false,
|
2015-04-24 11:07:32 +00:00
|
|
|
ModifiedIndex: 3,
|
2014-10-03 15:23:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/directory2",
|
2014-10-22 17:02:02 +00:00
|
|
|
Value: `{"name": "directory2"}`,
|
2014-10-03 15:23:25 +00:00
|
|
|
Dir: true,
|
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/bar",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("bar"),
|
2014-10-03 15:23:25 +00:00
|
|
|
ModifiedIndex: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := api.PodList{
|
2014-10-23 20:51:34 +00:00
|
|
|
ListMeta: api.ListMeta{ResourceVersion: "10"},
|
2014-10-03 15:23:25 +00:00
|
|
|
Items: []api.Pod{
|
2015-01-26 22:03:48 +00:00
|
|
|
// We expect list to be sorted by directory (e.g. namespace) first, then by name.
|
2015-01-26 17:52:50 +00:00
|
|
|
{
|
2015-04-24 11:07:32 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", ResourceVersion: "3"},
|
2015-01-26 17:52:50 +00:00
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
2014-10-03 15:23:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var got api.PodList
|
|
|
|
err := helper.ExtractToList("/some/key", &got)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractToListExcludesDirectories(t *testing.T) {
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
fakeClient.Data[key] = EtcdResponseWithError{
|
2014-10-03 15:23:25 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
EtcdIndex: 10,
|
|
|
|
Node: &etcd.Node{
|
2015-01-26 22:03:48 +00:00
|
|
|
Dir: true,
|
2014-10-03 15:23:25 +00:00
|
|
|
Nodes: []*etcd.Node{
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/foo",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("foo"),
|
2014-10-03 15:23:25 +00:00
|
|
|
ModifiedIndex: 1,
|
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/bar",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("bar"),
|
2014-10-03 15:23:25 +00:00
|
|
|
ModifiedIndex: 2,
|
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/baz",
|
2015-04-08 23:28:28 +00:00
|
|
|
Value: getEncodedPod("baz"),
|
2014-10-03 15:23:25 +00:00
|
|
|
ModifiedIndex: 3,
|
|
|
|
},
|
|
|
|
{
|
2015-01-26 22:03:48 +00:00
|
|
|
Key: "/directory",
|
2014-10-22 17:02:02 +00:00
|
|
|
Value: `{"name": "directory"}`,
|
2014-10-03 15:23:25 +00:00
|
|
|
Dir: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect := api.PodList{
|
2014-10-23 20:51:34 +00:00
|
|
|
ListMeta: api.ListMeta{ResourceVersion: "10"},
|
2014-10-03 15:23:25 +00:00
|
|
|
Items: []api.Pod{
|
2015-01-26 17:52:50 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "bar", ResourceVersion: "2"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "baz", ResourceVersion: "3"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
},
|
2014-10-03 15:23:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var got api.PodList
|
|
|
|
err := helper.ExtractToList("/some/key", &got)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v", err)
|
|
|
|
}
|
|
|
|
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
|
|
|
t.Errorf("Expected %#v, got %#v", e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 23:23:52 +00:00
|
|
|
func TestExtractObj(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2015-01-26 17:52:50 +00:00
|
|
|
expect := api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
|
|
|
Spec: api.PodSpec{
|
2015-06-02 21:40:05 +00:00
|
|
|
RestartPolicy: api.RestartPolicyAlways,
|
|
|
|
DNSPolicy: api.DNSClusterFirst,
|
2015-01-26 17:52:50 +00:00
|
|
|
},
|
|
|
|
}
|
2015-03-11 17:10:09 +00:00
|
|
|
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &expect), 0)
|
2014-07-22 22:05:43 +00:00
|
|
|
var got api.Pod
|
2014-06-27 19:54:45 +00:00
|
|
|
err := helper.ExtractObj("/some/key", &got, false)
|
2014-06-17 23:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, expect) {
|
|
|
|
t.Errorf("Wanted %#v, got %#v", expect, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractObjNotFoundErr(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key1 := etcdtest.AddPrefix("/some/key")
|
|
|
|
fakeClient.Data[key1] = EtcdResponseWithError{
|
2014-06-17 23:23:52 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
|
|
|
E: &etcd.EtcdError{
|
|
|
|
ErrorCode: 100,
|
|
|
|
},
|
|
|
|
}
|
2015-03-11 17:10:09 +00:00
|
|
|
key2 := etcdtest.AddPrefix("/some/key2")
|
|
|
|
fakeClient.Data[key2] = EtcdResponseWithError{
|
2014-06-17 23:23:52 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: nil,
|
|
|
|
},
|
|
|
|
}
|
2015-03-11 17:10:09 +00:00
|
|
|
key3 := etcdtest.AddPrefix("/some/key3")
|
|
|
|
fakeClient.Data[key3] = EtcdResponseWithError{
|
2014-06-17 23:23:52 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
try := func(key string) {
|
2014-07-22 22:05:43 +00:00
|
|
|
var got api.Pod
|
2014-06-27 19:54:45 +00:00
|
|
|
err := helper.ExtractObj(key, &got, false)
|
2014-06-17 23:23:52 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%s: wanted error but didn't get one", key)
|
|
|
|
}
|
2014-06-27 19:54:45 +00:00
|
|
|
err = helper.ExtractObj(key, &got, true)
|
2014-06-17 23:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: didn't want error but got %#v", key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try("/some/key")
|
|
|
|
try("/some/key2")
|
|
|
|
try("/some/key3")
|
|
|
|
}
|
|
|
|
|
2014-09-24 00:47:05 +00:00
|
|
|
func TestCreateObj(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-09-24 00:47:05 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
2015-03-02 22:28:21 +00:00
|
|
|
returnedObj := &api.Pod{}
|
|
|
|
err := helper.CreateObj("/some/key", obj, returnedObj, 5)
|
2014-09-24 00:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-11-13 15:38:13 +00:00
|
|
|
data, err := testapi.Codec().Encode(obj)
|
2014-09-24 00:47:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2015-03-11 17:10:09 +00:00
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
node := fakeClient.Data[key].R.Node
|
2014-09-24 00:47:05 +00:00
|
|
|
if e, a := string(data), node.Value; e != a {
|
|
|
|
t.Errorf("Wanted %v, got %v", e, a)
|
|
|
|
}
|
|
|
|
if e, a := uint64(5), fakeClient.LastSetTTL; e != a {
|
|
|
|
t.Errorf("Wanted %v, got %v", e, a)
|
|
|
|
}
|
2015-03-02 22:28:21 +00:00
|
|
|
if obj.ResourceVersion != returnedObj.ResourceVersion || obj.Name != returnedObj.Name {
|
|
|
|
t.Errorf("If set was successful but returned object did not have correct resource version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateObjNilOutParam(t *testing.T) {
|
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
2015-03-02 22:28:21 +00:00
|
|
|
err := helper.CreateObj("/some/key", obj, nil, 5)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-09-24 00:47:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 23:23:52 +00:00
|
|
|
func TestSetObj(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
2015-03-02 22:28:21 +00:00
|
|
|
returnedObj := &api.Pod{}
|
|
|
|
err := helper.SetObj("/some/key", obj, returnedObj, 5)
|
2014-06-17 23:23:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-11-13 15:38:13 +00:00
|
|
|
data, err := testapi.Codec().Encode(obj)
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect := string(data)
|
2015-03-11 17:10:09 +00:00
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
got := fakeClient.Data[key].R.Node.Value
|
2014-08-04 00:23:56 +00:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
2015-02-05 08:05:36 +00:00
|
|
|
if e, a := uint64(5), fakeClient.LastSetTTL; e != a {
|
|
|
|
t.Errorf("Wanted %v, got %v", e, a)
|
|
|
|
}
|
2015-03-02 22:28:21 +00:00
|
|
|
if obj.ResourceVersion != returnedObj.ResourceVersion || obj.Name != returnedObj.Name {
|
|
|
|
t.Errorf("If set was successful but returned object did not have correct resource version")
|
|
|
|
}
|
2014-08-04 00:23:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 05:53:27 +00:00
|
|
|
func TestSetObjFailCAS(t *testing.T) {
|
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
|
|
|
fakeClient.CasErr = fakeClient.NewError(123)
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
2015-03-06 05:53:27 +00:00
|
|
|
err := helper.SetObj("/some/key", obj, nil, 5)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expecting error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 00:23:56 +00:00
|
|
|
func TestSetObjWithVersion(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2014-08-04 00:23:56 +00:00
|
|
|
fakeClient.TestIndex = true
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
fakeClient.Data[key] = EtcdResponseWithError{
|
2014-08-04 00:23:56 +00:00
|
|
|
R: &etcd.Response{
|
|
|
|
Node: &etcd.Node{
|
2014-11-13 15:38:13 +00:00
|
|
|
Value: runtime.EncodeOrDie(testapi.Codec(), obj),
|
2014-08-04 00:23:56 +00:00
|
|
|
ModifiedIndex: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-02 22:28:21 +00:00
|
|
|
returnedObj := &api.Pod{}
|
|
|
|
err := helper.SetObj("/some/key", obj, returnedObj, 7)
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-11-13 15:38:13 +00:00
|
|
|
data, err := testapi.Codec().Encode(obj)
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect := string(data)
|
2015-03-11 17:10:09 +00:00
|
|
|
got := fakeClient.Data[key].R.Node.Value
|
2014-08-04 00:23:56 +00:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
2015-02-05 08:05:36 +00:00
|
|
|
if e, a := uint64(7), fakeClient.LastSetTTL; e != a {
|
|
|
|
t.Errorf("Wanted %v, got %v", e, a)
|
|
|
|
}
|
2015-03-02 22:28:21 +00:00
|
|
|
if obj.ResourceVersion != returnedObj.ResourceVersion || obj.Name != returnedObj.Name {
|
|
|
|
t.Errorf("If set was successful but returned object did not have correct resource version")
|
|
|
|
}
|
2014-08-04 00:23:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 02:23:22 +00:00
|
|
|
func TestSetObjWithoutResourceVersioner(t *testing.T) {
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-04-24 11:07:32 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
helper.Versioner = nil
|
2015-03-02 22:28:21 +00:00
|
|
|
returnedObj := &api.Pod{}
|
|
|
|
err := helper.SetObj("/some/key", obj, returnedObj, 3)
|
2015-03-11 17:10:09 +00:00
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-11-13 15:38:13 +00:00
|
|
|
data, err := testapi.Codec().Encode(obj)
|
2014-07-22 22:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect := string(data)
|
2015-03-11 17:10:09 +00:00
|
|
|
got := fakeClient.Data[key].R.Node.Value
|
2014-06-17 23:23:52 +00:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
2015-02-05 08:05:36 +00:00
|
|
|
if e, a := uint64(3), fakeClient.LastSetTTL; e != a {
|
|
|
|
t.Errorf("Wanted %v, got %v", e, a)
|
|
|
|
}
|
2015-03-02 22:28:21 +00:00
|
|
|
if obj.ResourceVersion != returnedObj.ResourceVersion || obj.Name != returnedObj.Name {
|
|
|
|
t.Errorf("If set was successful but returned object did not have correct resource version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetObjNilOutParam(t *testing.T) {
|
|
|
|
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2015-04-24 11:07:32 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
|
|
|
helper.Versioner = nil
|
2015-03-02 22:28:21 +00:00
|
|
|
err := helper.SetObj("/some/key", obj, nil, 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-06-17 23:23:52 +00:00
|
|
|
}
|
2014-07-20 06:26:26 +00:00
|
|
|
|
2015-04-12 22:22:04 +00:00
|
|
|
func TestGuaranteedUpdate(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2014-07-31 11:33:29 +00:00
|
|
|
fakeClient.TestIndex = true
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2014-07-31 11:33:29 +00:00
|
|
|
|
|
|
|
// Create a new node.
|
2015-03-11 17:10:09 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
|
2015-05-23 00:17:49 +00:00
|
|
|
err := helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
|
|
|
return obj, nil
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
data, err := codec.Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect := string(data)
|
|
|
|
got := fakeClient.Data[key].R.Node.Value
|
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update an existing node.
|
|
|
|
callbackCalled := false
|
|
|
|
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
|
|
|
|
err = helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
|
|
|
callbackCalled = true
|
|
|
|
|
|
|
|
if in.(*TestResource).Value != 1 {
|
|
|
|
t.Errorf("Callback input was not current set value")
|
|
|
|
}
|
|
|
|
|
|
|
|
return objUpdate, nil
|
|
|
|
}))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
data, err = codec.Encode(objUpdate)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect = string(data)
|
|
|
|
got = fakeClient.Data[key].R.Node.Value
|
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !callbackCalled {
|
|
|
|
t.Errorf("tryUpdate callback should have been called.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGuaranteedUpdateTTL(t *testing.T) {
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
|
|
|
fakeClient.TestIndex = true
|
|
|
|
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
|
|
|
|
|
|
|
// Create a new node.
|
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
|
|
|
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
|
|
|
|
err := helper.GuaranteedUpdate("/some/key", &TestResource{}, true, func(in runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
|
|
|
|
if res.TTL != 0 {
|
|
|
|
t.Fatalf("unexpected response meta: %#v", res)
|
|
|
|
}
|
|
|
|
ttl := uint64(10)
|
|
|
|
return obj, &ttl, nil
|
2014-07-31 11:33:29 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-08-06 02:23:22 +00:00
|
|
|
data, err := codec.Encode(obj)
|
2014-07-31 11:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect := string(data)
|
2015-03-11 17:10:09 +00:00
|
|
|
got := fakeClient.Data[key].R.Node.Value
|
2014-07-31 11:33:29 +00:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
2015-05-23 00:17:49 +00:00
|
|
|
if fakeClient.Data[key].R.Node.TTL != 10 {
|
|
|
|
t.Errorf("expected TTL set: %d", fakeClient.Data[key].R.Node.TTL)
|
|
|
|
}
|
2014-07-31 11:33:29 +00:00
|
|
|
|
|
|
|
// Update an existing node.
|
|
|
|
callbackCalled := false
|
2014-10-23 20:51:34 +00:00
|
|
|
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 2}
|
2015-05-23 00:17:49 +00:00
|
|
|
err = helper.GuaranteedUpdate("/some/key", &TestResource{}, true, func(in runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
|
|
|
|
if res.TTL != 10 {
|
|
|
|
t.Fatalf("unexpected response meta: %#v", res)
|
|
|
|
}
|
2014-07-31 11:33:29 +00:00
|
|
|
callbackCalled = true
|
|
|
|
|
|
|
|
if in.(*TestResource).Value != 1 {
|
|
|
|
t.Errorf("Callback input was not current set value")
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:17:49 +00:00
|
|
|
return objUpdate, nil, nil
|
2014-07-31 11:33:29 +00:00
|
|
|
})
|
2015-05-23 00:17:49 +00:00
|
|
|
|
2014-07-31 11:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
2014-08-06 02:23:22 +00:00
|
|
|
data, err = codec.Encode(objUpdate)
|
2014-07-31 11:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect = string(data)
|
2015-03-11 17:10:09 +00:00
|
|
|
got = fakeClient.Data[key].R.Node.Value
|
2014-07-31 11:33:29 +00:00
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
2015-05-23 00:17:49 +00:00
|
|
|
if fakeClient.Data[key].R.Node.TTL != 10 {
|
|
|
|
t.Errorf("expected TTL remained set: %d", fakeClient.Data[key].R.Node.TTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update an existing node and change ttl
|
|
|
|
callbackCalled = false
|
|
|
|
objUpdate = &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 3}
|
|
|
|
err = helper.GuaranteedUpdate("/some/key", &TestResource{}, true, func(in runtime.Object, res ResponseMeta) (runtime.Object, *uint64, error) {
|
|
|
|
if res.TTL != 10 {
|
|
|
|
t.Fatalf("unexpected response meta: %#v", res)
|
|
|
|
}
|
|
|
|
callbackCalled = true
|
|
|
|
|
|
|
|
if in.(*TestResource).Value != 2 {
|
|
|
|
t.Errorf("Callback input was not current set value")
|
|
|
|
}
|
|
|
|
|
|
|
|
newTTL := uint64(20)
|
|
|
|
return objUpdate, &newTTL, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
data, err = codec.Encode(objUpdate)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
expect = string(data)
|
|
|
|
got = fakeClient.Data[key].R.Node.Value
|
|
|
|
if expect != got {
|
|
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
|
|
}
|
|
|
|
if fakeClient.Data[key].R.Node.TTL != 20 {
|
|
|
|
t.Errorf("expected TTL changed: %d", fakeClient.Data[key].R.Node.TTL)
|
|
|
|
}
|
2014-07-31 11:33:29 +00:00
|
|
|
|
|
|
|
if !callbackCalled {
|
|
|
|
t.Errorf("tryUpdate callback should have been called.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:22:04 +00:00
|
|
|
func TestGuaranteedUpdateNoChange(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2014-08-14 19:51:20 +00:00
|
|
|
fakeClient.TestIndex = true
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2014-08-14 19:51:20 +00:00
|
|
|
|
|
|
|
// Create a new node.
|
2015-03-11 17:10:09 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
|
2015-05-23 00:17:49 +00:00
|
|
|
err := helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
|
|
|
return obj, nil
|
|
|
|
}))
|
2014-08-14 19:51:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update an existing node with the same data
|
|
|
|
callbackCalled := false
|
2014-10-23 20:51:34 +00:00
|
|
|
objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
|
2015-05-23 00:17:49 +00:00
|
|
|
err = helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
2015-02-11 23:35:05 +00:00
|
|
|
fakeClient.Err = errors.New("should not be called")
|
2014-08-14 19:51:20 +00:00
|
|
|
callbackCalled = true
|
2015-05-23 00:17:49 +00:00
|
|
|
return objUpdate, nil
|
|
|
|
}))
|
2014-08-14 19:51:20 +00:00
|
|
|
if err != nil {
|
2015-02-11 23:35:05 +00:00
|
|
|
t.Fatalf("Unexpected error %#v", err)
|
2014-08-14 19:51:20 +00:00
|
|
|
}
|
|
|
|
if !callbackCalled {
|
|
|
|
t.Errorf("tryUpdate callback should have been called.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:22:04 +00:00
|
|
|
func TestGuaranteedUpdateKeyNotFound(t *testing.T) {
|
2015-02-11 18:45:34 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
|
|
|
fakeClient.TestIndex = true
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2015-02-11 18:45:34 +00:00
|
|
|
|
|
|
|
// Create a new node.
|
2015-03-11 17:10:09 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
2015-02-11 18:45:34 +00:00
|
|
|
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1}
|
|
|
|
|
2015-05-23 00:17:49 +00:00
|
|
|
f := SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
|
|
|
return obj, nil
|
|
|
|
})
|
2015-02-11 18:45:34 +00:00
|
|
|
|
|
|
|
ignoreNotFound := false
|
2015-04-12 22:22:04 +00:00
|
|
|
err := helper.GuaranteedUpdate("/some/key", &TestResource{}, ignoreNotFound, f)
|
2015-02-11 18:45:34 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error for key not found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreNotFound = true
|
2015-04-12 22:22:04 +00:00
|
|
|
err = helper.GuaranteedUpdate("/some/key", &TestResource{}, ignoreNotFound, f)
|
2015-02-11 18:45:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %v.", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:22:04 +00:00
|
|
|
func TestGuaranteedUpdate_CreateCollision(t *testing.T) {
|
2014-08-21 04:27:19 +00:00
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
2014-08-05 18:43:19 +00:00
|
|
|
fakeClient.TestIndex = true
|
2015-03-11 17:10:09 +00:00
|
|
|
helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix())
|
|
|
|
key := etcdtest.AddPrefix("/some/key")
|
2014-08-05 18:43:19 +00:00
|
|
|
|
2015-03-11 17:10:09 +00:00
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
2014-08-05 18:43:19 +00:00
|
|
|
|
|
|
|
const concurrency = 10
|
|
|
|
var wgDone sync.WaitGroup
|
|
|
|
var wgForceCollision sync.WaitGroup
|
|
|
|
wgDone.Add(concurrency)
|
|
|
|
wgForceCollision.Add(concurrency)
|
|
|
|
|
|
|
|
for i := 0; i < concurrency; i++ {
|
|
|
|
// Increment TestResource.Value by 1
|
|
|
|
go func() {
|
|
|
|
defer wgDone.Done()
|
|
|
|
|
|
|
|
firstCall := true
|
2015-05-23 00:17:49 +00:00
|
|
|
err := helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) {
|
2014-08-05 18:43:19 +00:00
|
|
|
defer func() { firstCall = false }()
|
|
|
|
|
|
|
|
if firstCall {
|
2015-04-12 22:22:04 +00:00
|
|
|
// Force collision by joining all concurrent GuaranteedUpdate operations here.
|
2014-08-05 18:43:19 +00:00
|
|
|
wgForceCollision.Done()
|
|
|
|
wgForceCollision.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
currValue := in.(*TestResource).Value
|
2014-10-23 20:51:34 +00:00
|
|
|
obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: currValue + 1}
|
2015-05-23 00:17:49 +00:00
|
|
|
return obj, nil
|
|
|
|
}))
|
2014-08-05 18:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error %#v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wgDone.Wait()
|
|
|
|
|
|
|
|
// Check that stored TestResource has received all updates.
|
2015-03-11 17:10:09 +00:00
|
|
|
body := fakeClient.Data[key].R.Node.Value
|
2014-08-05 18:43:19 +00:00
|
|
|
stored := &TestResource{}
|
2014-08-06 02:23:22 +00:00
|
|
|
if err := codec.DecodeInto([]byte(body), stored); err != nil {
|
2014-08-05 18:43:19 +00:00
|
|
|
t.Errorf("Error decoding stored value: %v", body)
|
|
|
|
}
|
|
|
|
if stored.Value != concurrency {
|
|
|
|
t.Errorf("Some of the writes were lost. Stored value: %d", stored.Value)
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 18:22:42 +00:00
|
|
|
|
|
|
|
func TestGetEtcdVersion_ValidVersion(t *testing.T) {
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-04 22:45:33 +00:00
|
|
|
fmt.Fprint(w, validEtcdVersion)
|
2015-03-03 18:22:42 +00:00
|
|
|
}))
|
|
|
|
defer testServer.Close()
|
|
|
|
|
2015-05-02 07:13:52 +00:00
|
|
|
var version string
|
2015-03-03 18:22:42 +00:00
|
|
|
var err error
|
2015-05-02 07:13:52 +00:00
|
|
|
if version, err = GetEtcdVersion(testServer.URL); err != nil {
|
2015-03-03 18:22:42 +00:00
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
2015-05-04 22:45:33 +00:00
|
|
|
assert.Equal(t, validEtcdVersion, version, "Unexpected version")
|
2015-03-03 18:22:42 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcdVersion_ErrorStatus(t *testing.T) {
|
|
|
|
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
}))
|
|
|
|
defer testServer.Close()
|
|
|
|
|
2015-05-02 07:13:52 +00:00
|
|
|
_, err := GetEtcdVersion(testServer.URL)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetEtcdVersion_NotListening(t *testing.T) {
|
2015-05-28 12:39:32 +00:00
|
|
|
portIsOpen := func(port int) bool {
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:"+strconv.Itoa(port), 1*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
port := rand.Intn((1 << 16) - 1)
|
|
|
|
for tried := 0; portIsOpen(port); tried++ {
|
|
|
|
if tried >= 10 {
|
|
|
|
t.Fatal("Couldn't find a closed TCP port to continue testing")
|
|
|
|
}
|
|
|
|
port++
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := GetEtcdVersion("http://127.0.0.1:" + strconv.Itoa(port))
|
2015-03-03 18:22:42 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
2015-03-11 17:10:09 +00:00
|
|
|
|
|
|
|
func TestPrefixEtcdKey(t *testing.T) {
|
|
|
|
fakeClient := NewFakeEtcdClient(t)
|
|
|
|
prefix := path.Join("/", etcdtest.PathPrefix())
|
|
|
|
helper := NewEtcdHelper(fakeClient, testapi.Codec(), prefix)
|
|
|
|
|
|
|
|
baseKey := "/some/key"
|
|
|
|
|
|
|
|
// Verify prefix is added
|
|
|
|
keyBefore := baseKey
|
|
|
|
keyAfter := helper.PrefixEtcdKey(keyBefore)
|
|
|
|
|
|
|
|
assert.Equal(t, keyAfter, path.Join(prefix, baseKey), "Prefix incorrectly added by EtcdHelper")
|
|
|
|
|
|
|
|
// Verify prefix is not added
|
|
|
|
keyBefore = path.Join(prefix, baseKey)
|
|
|
|
keyAfter = helper.PrefixEtcdKey(keyBefore)
|
|
|
|
|
|
|
|
assert.Equal(t, keyBefore, keyAfter, "Prefix incorrectly added by EtcdHelper")
|
|
|
|
}
|