mirror of https://github.com/k3s-io/k3s
136 lines
2.9 KiB
Go
136 lines
2.9 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/coreos/go-etcd/etcd"
|
|
)
|
|
|
|
type fakeEtcdGetSet struct {
|
|
get func(key string, sort, recursive bool) (*etcd.Response, error)
|
|
set func(key, value string, ttl uint64) (*etcd.Response, error)
|
|
}
|
|
|
|
func TestIsNotFoundErr(t *testing.T) {
|
|
try := func(err error, isNotFound bool) {
|
|
if IsEtcdNotFound(err) != isNotFound {
|
|
t.Errorf("Expected %#v to return %v, but it did not", err, isNotFound)
|
|
}
|
|
}
|
|
try(&etcd.EtcdError{ErrorCode: 100}, true)
|
|
try(&etcd.EtcdError{ErrorCode: 101}, false)
|
|
try(nil, false)
|
|
try(fmt.Errorf("some other kind of error"), false)
|
|
}
|
|
|
|
type testMarshalType struct {
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
func TestExtractList(t *testing.T) {
|
|
fakeClient := MakeFakeEtcdClient(t)
|
|
fakeClient.Data["/some/key"] = EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: &etcd.Node{
|
|
Nodes: []*etcd.Node{
|
|
{
|
|
Value: `{"id":"foo"}`,
|
|
},
|
|
{
|
|
Value: `{"id":"bar"}`,
|
|
},
|
|
{
|
|
Value: `{"id":"baz"}`,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
expect := []testMarshalType{
|
|
{"foo"},
|
|
{"bar"},
|
|
{"baz"},
|
|
}
|
|
var got []testMarshalType
|
|
helper := EtcdHelper{fakeClient}
|
|
err := helper.ExtractList("/some/key", &got)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error %#v", err)
|
|
}
|
|
if !reflect.DeepEqual(got, expect) {
|
|
t.Errorf("Wanted %#v, got %#v", expect, got)
|
|
}
|
|
}
|
|
|
|
func TestExtractObj(t *testing.T) {
|
|
fakeClient := MakeFakeEtcdClient(t)
|
|
expect := testMarshalType{ID: "foo"}
|
|
fakeClient.Set("/some/key", MakeJSONString(expect), 0)
|
|
helper := EtcdHelper{fakeClient}
|
|
var got testMarshalType
|
|
err := helper.ExtractObj("/some/key", &got, false)
|
|
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) {
|
|
fakeClient := MakeFakeEtcdClient(t)
|
|
fakeClient.Data["/some/key"] = EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: nil,
|
|
},
|
|
E: &etcd.EtcdError{
|
|
ErrorCode: 100,
|
|
},
|
|
}
|
|
fakeClient.Data["/some/key2"] = EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: nil,
|
|
},
|
|
}
|
|
fakeClient.Data["/some/key3"] = EtcdResponseWithError{
|
|
R: &etcd.Response{
|
|
Node: &etcd.Node{
|
|
Value: "",
|
|
},
|
|
},
|
|
}
|
|
helper := EtcdHelper{fakeClient}
|
|
try := func(key string) {
|
|
var got testMarshalType
|
|
err := helper.ExtractObj(key, &got, false)
|
|
if err == nil {
|
|
t.Errorf("%s: wanted error but didn't get one", key)
|
|
}
|
|
err = helper.ExtractObj(key, &got, true)
|
|
if err != nil {
|
|
t.Errorf("%s: didn't want error but got %#v", key, err)
|
|
}
|
|
}
|
|
|
|
try("/some/key")
|
|
try("/some/key2")
|
|
try("/some/key3")
|
|
}
|
|
|
|
func TestSetObj(t *testing.T) {
|
|
obj := testMarshalType{ID: "foo"}
|
|
fakeClient := MakeFakeEtcdClient(t)
|
|
helper := EtcdHelper{fakeClient}
|
|
err := helper.SetObj("/some/key", obj)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error %#v", err)
|
|
}
|
|
expect := MakeJSONString(obj)
|
|
got := fakeClient.Data["/some/key"].R.Node.Value
|
|
if expect != got {
|
|
t.Errorf("Wanted %v, got %v", expect, got)
|
|
}
|
|
}
|