本文整理汇总了Golang中k8s/io/apimachinery/pkg/runtime.EncodeOrDie函数的典型用法代码示例。如果您正苦于以下问题:Golang EncodeOrDie函数的具体用法?Golang EncodeOrDie怎么用?Golang EncodeOrDie使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EncodeOrDie函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestResourceNames
func TestResourceNames(t *testing.T) {
pods, svc := testData()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &svc.Items[0]),
}), testapi.Default.Codec()).
NamespaceParam("test")
test := &testVisitor{}
if b.Do().Err() == nil {
t.Errorf("unexpected non-error")
}
b.ResourceNames("pods", "foo", "services/baz")
err := b.Do().Visit(test.Handle)
if err != nil || len(test.Infos) != 2 {
t.Fatalf("unexpected response: %v %#v", err, test.Infos)
}
if !reflect.DeepEqual(&pods.Items[0], test.Objects()[0]) {
t.Errorf("unexpected object: \n%#v, expected: \n%#v", test.Objects()[0], &pods.Items[0])
}
if !reflect.DeepEqual(&svc.Items[0], test.Objects()[1]) {
t.Errorf("unexpected object: \n%#v, expected: \n%#v", test.Objects()[1], &svc.Items[0])
}
}
示例2: TestListObjectWithDifferentVersions
func TestListObjectWithDifferentVersions(t *testing.T) {
pods, svc := testData()
labelKey := metav1.LabelSelectorQueryParam(api.Registry.GroupOrDie(api.GroupName).GroupVersion.String())
obj, err := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(testapi.Default.Codec(), pods),
"/namespaces/test/services?" + labelKey + "=a%3Db": runtime.EncodeOrDie(testapi.Default.Codec(), svc),
}), testapi.Default.Codec()).
SelectorParam("a=b").
NamespaceParam("test").
ResourceTypeOrNameArgs(true, "pods,services").
Flatten().
Do().Object()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
list, ok := obj.(*api.List)
if !ok {
t.Fatalf("unexpected object: %#v", obj)
}
// resource version differs between type lists, so it's not possible to get a single version.
if list.ResourceVersion != "" || len(list.Items) != 3 {
t.Errorf("unexpected list: %#v", list)
}
}
示例3: TestLatest
func TestLatest(t *testing.T) {
r, _, _ := streamTestData()
newPod := &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "13"},
}
newPod2 := &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "test", ResourceVersion: "14"},
}
newSvc := &api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "15"},
}
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), newPod),
"/namespaces/test/pods/bar": runtime.EncodeOrDie(testapi.Default.Codec(), newPod2),
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), newSvc),
}), testapi.Default.Codec()).
NamespaceParam("other").Stream(r, "STDIN").Flatten().Latest()
test := &testVisitor{}
singleItemImplied := false
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
if err != nil || singleItemImplied || len(test.Infos) != 3 {
t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, test.Infos)
}
if !api.Semantic.DeepDerivative([]runtime.Object{newPod, newPod2, newSvc}, test.Objects()) {
t.Errorf("unexpected visited objects: %#v", test.Objects())
}
}
示例4: TestSelector
func TestSelector(t *testing.T) {
pods, svc := testData()
labelKey := metav1.LabelSelectorQueryParam(api.Registry.GroupOrDie(api.GroupName).GroupVersion.String())
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(testapi.Default.Codec(), pods),
"/namespaces/test/services?" + labelKey + "=a%3Db": runtime.EncodeOrDie(testapi.Default.Codec(), svc),
}), testapi.Default.Codec()).
SelectorParam("a=b").
NamespaceParam("test").
Flatten()
test := &testVisitor{}
singleItemImplied := false
if b.Do().Err() == nil {
t.Errorf("unexpected non-error")
}
b.ResourceTypeOrNameArgs(true, "pods,service")
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
if err != nil || singleItemImplied || len(test.Infos) != 3 {
t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, test.Infos)
}
if !api.Semantic.DeepDerivative([]runtime.Object{&pods.Items[0], &pods.Items[1], &svc.Items[0]}, test.Objects()) {
t.Errorf("unexpected visited objects: %#v", test.Objects())
}
if _, err := b.Do().ResourceMapping(); err == nil {
t.Errorf("unexpected non-error")
}
}
示例5: TestURLBuilder
func TestURLBuilder(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), &api.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "test"}})))
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), &api.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "test1"}})))
}))
defer s.Close()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClient(), testapi.Default.Codec()).
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{s.URL}}).
NamespaceParam("foo")
test := &testVisitor{}
err := b.Do().Visit(test.Handle)
if err != nil || len(test.Infos) != 2 {
t.Fatalf("unexpected response: %v %#v", err, test.Infos)
}
info := test.Infos[0]
if info.Name != "test" || info.Namespace != "foo" || info.Object == nil {
t.Errorf("unexpected info: %#v", info)
}
info = test.Infos[1]
if info.Name != "test1" || info.Namespace != "foo" || info.Object == nil {
t.Errorf("unexpected info: %#v", info)
}
}
示例6: TestMultipleResourceByTheSameName
func TestMultipleResourceByTheSameName(t *testing.T) {
pods, svcs := testData()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
"/namespaces/test/pods/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[1]),
"/namespaces/test/services/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &svcs.Items[0]),
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &svcs.Items[0]),
}), testapi.Default.Codec()).
NamespaceParam("test")
test := &testVisitor{}
singleItemImplied := false
if b.Do().Err() == nil {
t.Errorf("unexpected non-error")
}
b.ResourceTypeOrNameArgs(true, "pods,services", "foo", "baz")
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
if err != nil || singleItemImplied || len(test.Infos) != 4 {
t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, test.Infos)
}
if !api.Semantic.DeepDerivative([]runtime.Object{&pods.Items[0], &pods.Items[1], &svcs.Items[0], &svcs.Items[0]}, test.Objects()) {
t.Errorf("unexpected visited objects: %#v", test.Objects())
}
if _, err := b.Do().ResourceMapping(); err == nil {
t.Errorf("unexpected non-error")
}
}
示例7: streamTestData
func streamTestData() (io.Reader, *api.PodList, *api.ServiceList) {
pods, svc := testData()
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), pods)))
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), svc)))
}()
return r, pods, svc
}
示例8: TestNodeBuilder
func TestNodeBuilder(t *testing.T) {
node := &api.Node{
ObjectMeta: metav1.ObjectMeta{Name: "node1", Namespace: "should-not-have", ResourceVersion: "10"},
Spec: api.NodeSpec{},
Status: api.NodeStatus{
Capacity: api.ResourceList{
api.ResourceCPU: resource.MustParse("1000m"),
api.ResourceMemory: resource.MustParse("1Mi"),
},
},
}
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), node)))
}()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClient(), testapi.Default.Codec()).
NamespaceParam("test").Stream(r, "STDIN")
test := &testVisitor{}
err := b.Do().Visit(test.Handle)
if err != nil || len(test.Infos) != 1 {
t.Fatalf("unexpected response: %v %#v", err, test.Infos)
}
info := test.Infos[0]
if info.Name != "node1" || info.Namespace != "" || info.Object == nil {
t.Errorf("unexpected info: %#v", info)
}
}
示例9: TestListObject
func TestListObject(t *testing.T) {
pods, _ := testData()
labelKey := metav1.LabelSelectorQueryParam(api.Registry.GroupOrDie(api.GroupName).GroupVersion.String())
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(testapi.Default.Codec(), pods),
}), testapi.Default.Codec()).
SelectorParam("a=b").
NamespaceParam("test").
ResourceTypeOrNameArgs(true, "pods").
Flatten()
obj, err := b.Do().Object()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
list, ok := obj.(*api.List)
if !ok {
t.Fatalf("unexpected object: %#v", obj)
}
if list.ResourceVersion != pods.ResourceVersion || len(list.Items) != 2 {
t.Errorf("unexpected list: %#v", list)
}
mapping, err := b.Do().ResourceMapping()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if mapping.Resource != "pods" {
t.Errorf("unexpected resource mapping: %#v", mapping)
}
}
示例10: TestResourceByNameAndEmptySelector
func TestResourceByNameAndEmptySelector(t *testing.T) {
pods, _ := testData()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
}), testapi.Default.Codec()).
NamespaceParam("test").
SelectorParam("").
ResourceTypeOrNameArgs(true, "pods", "foo")
singleItemImplied := false
infos, err := b.Do().IntoSingleItemImplied(&singleItemImplied).Infos()
if err != nil || !singleItemImplied || len(infos) != 1 {
t.Fatalf("unexpected response: %v %t %#v", err, singleItemImplied, infos)
}
if !reflect.DeepEqual(&pods.Items[0], infos[0].Object) {
t.Errorf("unexpected object: %#v", infos[0])
}
mapping, err := b.Do().ResourceMapping()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if mapping.Resource != "pods" {
t.Errorf("unexpected resource mapping: %#v", mapping)
}
}
示例11: verifyObjects
func verifyObjects(t *testing.T, expected, actual []runtime.Object) {
var actualObj runtime.Object
var err error
if len(actual) != len(expected) {
t.Fatal(actual)
}
for i, obj := range actual {
switch obj.(type) {
case runtime.Unstructured, *runtime.Unknown:
actualObj, err = runtime.Decode(
api.Codecs.UniversalDecoder(),
[]byte(runtime.EncodeOrDie(api.Codecs.LegacyCodec(), obj)))
default:
actualObj = obj
err = nil
}
if err != nil {
t.Fatal(err)
}
if !api.Semantic.DeepEqual(expected[i], actualObj) {
t.Errorf("unexpected object: \n%#v\n%#v", expected[i], actualObj)
}
}
}
示例12: TestBind
func TestBind(t *testing.T) {
table := []struct {
binding *v1.Binding
}{
{binding: &v1.Binding{
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.NamespaceDefault,
Name: "foo",
},
Target: v1.ObjectReference{
Name: "foohost.kubernetes.mydomain.com",
},
}},
}
for _, item := range table {
handler := utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: "",
T: t,
}
server := httptest.NewServer(&handler)
defer server.Close()
client := clientset.NewForConfigOrDie(&restclient.Config{Host: server.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
b := binder{client}
if err := b.Bind(item.binding); err != nil {
t.Errorf("Unexpected error: %v", err)
continue
}
expectedBody := runtime.EncodeOrDie(testapi.Default.Codec(), item.binding)
handler.ValidateRequest(t, testapi.Default.ResourcePath("bindings", v1.NamespaceDefault, ""), "POST", &expectedBody)
}
}
示例13: streamTestObject
func streamTestObject(obj runtime.Object) io.Reader {
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(runtime.EncodeOrDie(testapi.Default.Codec(), obj)))
}()
return r
}
示例14: TestUpdatePetWithoutRetry
func TestUpdatePetWithoutRetry(t *testing.T) {
pcb1, pcb2 := makeTwoDifferntPCB()
// invalid pet with empty pod
invalidPcb := *pcb1
invalidPcb.pod = nil
testCases := []struct {
realPet *pcb
expectedPet *pcb
expectErr bool
requests int
}{
// case 0: error occurs, no need to update
{
realPet: pcb1,
expectedPet: &invalidPcb,
expectErr: true,
requests: 0,
},
// case 1: identical pet, no need to update
{
realPet: pcb1,
expectedPet: pcb1,
expectErr: false,
requests: 0,
},
// case 2: need to call update once
{
realPet: pcb1,
expectedPet: pcb2,
expectErr: false,
requests: 1,
},
}
for k, tc := range testCases {
body := runtime.EncodeOrDie(testapi.Default.Codec(), &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "empty_pod"}})
fakeHandler := utiltesting.FakeHandler{
StatusCode: 200,
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
client := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &api.Registry.GroupOrDie(v1.GroupName).GroupVersion}})
petClient := newPetClient(client)
err := petClient.Update(tc.realPet, tc.expectedPet)
if tc.expectErr != (err != nil) {
t.Errorf("case %d: expect error(%v), got err: %v", k, tc.expectErr, err)
}
fakeHandler.ValidateRequestCount(t, tc.requests)
testServer.Close()
}
}
示例15: TestResourceNamesWithoutResource
func TestResourceNamesWithoutResource(t *testing.T) {
pods, svc := testData()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &svc.Items[0]),
}), testapi.Default.Codec()).
NamespaceParam("test")
test := &testVisitor{}
if b.Do().Err() == nil {
t.Errorf("unexpected non-error")
}
b.ResourceNames("", "foo", "services/baz")
err := b.Do().Visit(test.Handle)
if err == nil || !strings.Contains(err.Error(), "must be RESOURCE/NAME") {
t.Fatalf("unexpected response: %v", err)
}
}