1/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package test
18
19import (
20	"encoding/json"
21	"reflect"
22	"testing"
23
24	// TODO: Ideally we should create the necessary package structure in e.g.,
25	// pkg/conversion/test/... instead of importing pkg/api here.
26	apitesting "k8s.io/apimachinery/pkg/api/apitesting"
27	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28	"k8s.io/apimachinery/pkg/runtime"
29	"k8s.io/apimachinery/pkg/runtime/schema"
30)
31
32func TestV1EncodeDecodeStatus(t *testing.T) {
33	status := &metav1.Status{
34		Status:  metav1.StatusFailure,
35		Code:    200,
36		Reason:  metav1.StatusReasonUnknown,
37		Message: "",
38	}
39
40	_, codecs := TestScheme()
41	codec := apitesting.TestCodec(codecs, schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal})
42
43	encoded, err := runtime.Encode(codec, status)
44	if err != nil {
45		t.Errorf("unexpected error: %v", err)
46	}
47	typeMeta := metav1.TypeMeta{}
48	if err := json.Unmarshal(encoded, &typeMeta); err != nil {
49		t.Errorf("unexpected error: %v", err)
50	}
51	if typeMeta.Kind != "Status" {
52		t.Errorf("Kind is not set to \"Status\". Got %v", string(encoded))
53	}
54	if typeMeta.APIVersion != "v1" {
55		t.Errorf("APIVersion is not set to \"v1\". Got %v", string(encoded))
56	}
57	decoded, err := runtime.Decode(codec, encoded)
58	if err != nil {
59		t.Errorf("unexpected error: %v", err)
60	}
61	if !reflect.DeepEqual(status, decoded) {
62		t.Errorf("expected: %v, got: %v", status, decoded)
63	}
64}
65
66func TestExperimentalEncodeDecodeStatus(t *testing.T) {
67	status := &metav1.Status{
68		Status:  metav1.StatusFailure,
69		Code:    200,
70		Reason:  metav1.StatusReasonUnknown,
71		Message: "",
72	}
73	// TODO: caesarxuchao: use the testapi.Extensions.Codec() once the PR that
74	// moves experimental from v1 to v1beta1 got merged.
75	_, codecs := TestScheme()
76	expCodec := apitesting.TestCodec(codecs, schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal})
77
78	encoded, err := runtime.Encode(expCodec, status)
79	if err != nil {
80		t.Errorf("unexpected error: %v", err)
81	}
82	typeMeta := metav1.TypeMeta{}
83	if err := json.Unmarshal(encoded, &typeMeta); err != nil {
84		t.Errorf("unexpected error: %v", err)
85	}
86	if typeMeta.Kind != "Status" {
87		t.Errorf("Kind is not set to \"Status\". Got %s", encoded)
88	}
89	if typeMeta.APIVersion != "v1" {
90		t.Errorf("APIVersion is not set to \"\". Got %s", encoded)
91	}
92	decoded, err := runtime.Decode(expCodec, encoded)
93	if err != nil {
94		t.Errorf("unexpected error: %v", err)
95	}
96	if !reflect.DeepEqual(status, decoded) {
97		t.Errorf("expected: %v, got: %v", status, decoded)
98	}
99}
100