Skip to content

Commit

Permalink
Update tests to handle codec changes
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Jan 22, 2016
1 parent 2fd38a7 commit 33085c0
Show file tree
Hide file tree
Showing 34 changed files with 147 additions and 181 deletions.
4 changes: 2 additions & 2 deletions contrib/mesos/pkg/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestExecutorLaunchAndKillTask(t *testing.T) {
taskInfo, err := podTask.BuildTaskInfo()
assert.Equal(t, nil, err, "must be able to build task info")

data, err := testapi.Default.Codec().Encode(pod)
data, err := runtime.Encode(testapi.Default.Codec(), pod)
assert.Equal(t, nil, err, "must be able to encode a pod's spec data")

taskInfo.Data = data
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestExecutorFrameworkMessage(t *testing.T) {
taskInfo, err := podTask.BuildTaskInfo()
assert.Equal(t, nil, err, "must be able to build task info")

data, _ := testapi.Default.Codec().Encode(pod)
data, _ := runtime.Encode(testapi.Default.Codec(), pod)
taskInfo.Data = data

mockDriver.On(
Expand Down
6 changes: 3 additions & 3 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func TestExampleObjectSchemas(t *testing.T) {
return
}
if strings.Contains(name, "scheduler-policy-config") {
if err := schedulerapilatest.Codec.DecodeInto(data, expectedType); err != nil {
if err := runtime.DecodeInto(schedulerapilatest.Codec, data, expectedType); err != nil {
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
return
}
Expand Down Expand Up @@ -516,14 +516,14 @@ func TestReadme(t *testing.T) {
if err != nil {
t.Errorf("%s could not be converted to JSON: %v\n%s", path, err, string(content))
}
if err := testapi.Default.Codec().DecodeInto(json, expectedType); err != nil {
if err := runtime.DecodeInto(testapi.Default.Codec(), json, expectedType); err != nil {
t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(content))
continue
}
if errors := validateObject(expectedType); len(errors) > 0 {
t.Errorf("%s did not validate correctly: %v", path, errors)
}
_, err = testapi.Default.Codec().Encode(expectedType)
_, err = runtime.Encode(testapi.Default.Codec(), expectedType)
if err != nil {
t.Errorf("Could not encode object: %v", err)
continue
Expand Down
63 changes: 63 additions & 0 deletions pkg/api/testapi/testapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ limitations under the License.
package testapi

import (
"encoding/json"
"reflect"
"testing"

"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime"
)

// TODO these tests don't add much value for testing things that have groups
Expand Down Expand Up @@ -61,3 +66,61 @@ func TestResourcePath(t *testing.T) {
}
}
}

var status = &unversioned.Status{
Status: unversioned.StatusFailure,
Code: 200,
Reason: unversioned.StatusReasonUnknown,
Message: "",
}

func TestV1EncodeDecodeStatus(t *testing.T) {
v1Codec := Default.Codec()

encoded, err := runtime.Encode(v1Codec, status)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
typeMeta := unversioned.TypeMeta{}
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
t.Errorf("unexpected error: %v", err)
}
if typeMeta.Kind != "Status" {
t.Errorf("Kind is not set to \"Status\". Got %v", string(encoded))
}
if typeMeta.APIVersion != "v1" {
t.Errorf("APIVersion is not set to \"v1\". Got %v", string(encoded))
}
decoded, err := runtime.Decode(v1Codec, encoded)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(status, decoded) {
t.Errorf("expected: %#v, got: %#v", status, decoded)
}
}

func TestExperimentalEncodeDecodeStatus(t *testing.T) {
extensionCodec := Extensions.Codec()
encoded, err := runtime.Encode(extensionCodec, status)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
typeMeta := unversioned.TypeMeta{}
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
t.Errorf("unexpected error: %v", err)
}
if typeMeta.Kind != "Status" {
t.Errorf("Kind is not set to \"Status\". Got %s", encoded)
}
if typeMeta.APIVersion != "v1" {
t.Errorf("APIVersion is not set to \"\". Got %s", encoded)
}
decoded, err := runtime.Decode(extensionCodec, encoded)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(status, decoded) {
t.Errorf("expected: %v, got: %v", status, decoded)
}
}
1 change: 1 addition & 0 deletions pkg/api/testing/compat/compatibility_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"testing"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/runtime"
Expand Down
62 changes: 0 additions & 62 deletions pkg/apimachinery/registered/registered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ limitations under the License.
package registered

import (
"encoding/json"
"reflect"
"testing"

"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery"
"k8s.io/kubernetes/pkg/runtime"
)

func TestAllPreferredGroupVersions(t *testing.T) {
Expand Down Expand Up @@ -70,61 +66,3 @@ func TestAllPreferredGroupVersions(t *testing.T) {
reset()
}
}

var status = &unversioned.Status{
Status: unversioned.StatusFailure,
Code: 200,
Reason: unversioned.StatusReasonUnknown,
Message: "",
}

func TestV1EncodeDecodeStatus(t *testing.T) {
v1Codec := testapi.Default.Codec()

encoded, err := runtime.Encode(v1Codec, status)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
typeMeta := unversioned.TypeMeta{}
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
t.Errorf("unexpected error: %v", err)
}
if typeMeta.Kind != "Status" {
t.Errorf("Kind is not set to \"Status\". Got %v", string(encoded))
}
if typeMeta.APIVersion != "v1" {
t.Errorf("APIVersion is not set to \"v1\". Got %v", string(encoded))
}
decoded, err := runtime.Decode(v1Codec, encoded)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(status, decoded) {
t.Errorf("expected: %#v, got: %#v", status, decoded)
}
}

func TestExperimentalEncodeDecodeStatus(t *testing.T) {
extensionCodec := testapi.Extensions.Codec()
encoded, err := runtime.Encode(extensionCodec, status)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
typeMeta := unversioned.TypeMeta{}
if err := json.Unmarshal(encoded, &typeMeta); err != nil {
t.Errorf("unexpected error: %v", err)
}
if typeMeta.Kind != "Status" {
t.Errorf("Kind is not set to \"Status\". Got %s", encoded)
}
if typeMeta.APIVersion != "v1" {
t.Errorf("APIVersion is not set to \"\". Got %s", encoded)
}
decoded, err := runtime.Decode(extensionCodec, encoded)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(status, decoded) {
t.Errorf("expected: %v, got: %v", status, decoded)
}
}
1 change: 1 addition & 0 deletions pkg/apis/componentconfig/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"testing"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/apis/componentconfig"
Expand Down
1 change: 0 additions & 1 deletion pkg/apiserver/resthandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
apierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/strategicpatch"
Expand Down
2 changes: 0 additions & 2 deletions pkg/client/unversioned/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
"k8s.io/kubernetes/pkg/version"
)

const nameRequiredError = "resource name may not be empty"

func TestGetServerVersion(t *testing.T) {
expect := version.Info{
Major: "foo",
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/unversioned/helper_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestNegotiateVersion(t *testing.T) {
{
name: "explicit version supported",
config: &unversioned.Config{GroupVersion: testapi.Default.GroupVersion()},
serverVersions: []string{"/ersion1", testapi.Default.GroupVersion().String()},
serverVersions: []string{"/version1", testapi.Default.GroupVersion().String()},
clientVersions: []uapi.GroupVersion{{Version: "version1"}, *testapi.Default.GroupVersion()},
expectedVersion: testapi.Default.GroupVersion(),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestCreatePods(t *testing.T) {
Spec: controllerSpec.Spec.Template.Spec,
}
fakeHandler.ValidateRequest(t, testapi.Default.ResourcePath("pods", api.NamespaceDefault, ""), "POST", nil)
actualPod, err := client.Codec.Decode([]byte(fakeHandler.RequestBody))
actualPod, err := runtime.Decode(client.Codec, []byte(fakeHandler.RequestBody))
if err != nil {
t.Errorf("Unexpected error: %#v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ func TestExampleObjects(t *testing.T) {
}

for name, scenario := range scenarios {
o := testclient.NewObjects(api.Scheme, api.Scheme)
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/"+name, o, api.Scheme); err != nil {
codec := api.Codecs.UniversalDecoder()
o := testclient.NewObjects(api.Scheme, codec)
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/"+name, o, codec); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -332,11 +333,12 @@ func TestExampleObjects(t *testing.T) {
}

func TestBindingWithExamples(t *testing.T) {
o := testclient.NewObjects(api.Scheme, api.Scheme)
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/claims/claim-01.yaml", o, api.Scheme); err != nil {
codec := api.Codecs.UniversalDecoder()
o := testclient.NewObjects(api.Scheme, codec)
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/claims/claim-01.yaml", o, codec); err != nil {
t.Fatal(err)
}
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/volumes/local-01.yaml", o, api.Scheme); err != nil {
if err := testclient.AddObjectsFromPath("../../../docs/user-guide/persistent-volumes/volumes/local-01.yaml", o, codec); err != nil {
t.Fatal(err)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/conversion/unversioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

// TODO: Ideally we should create the necessary package structure in e.g.,
// pkg/conversion/test/... instead of importing pkg/api here.
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func refJson(t *testing.T, o runtime.Object) string {
}

_, _, codec := NewAPIFactory()
json, err := codec.Encode(&api.SerializedReference{Reference: *ref})
json, err := runtime.Encode(codec, &api.SerializedReference{Reference: *ref})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
28 changes: 13 additions & 15 deletions pkg/kubelet/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apimachinery/registered"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/securitycontext"

"github.com/ghodss/yaml"
//"github.com/ghodss/yaml"
)

func noDefault(*api.Pod) error { return nil }
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestDecodeSinglePod(t *testing.T) {
SecurityContext: &api.PodSecurityContext{},
},
}
json, err := testapi.Default.Codec().Encode(pod)
json, err := runtime.Encode(testapi.Default.Codec(), pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -72,15 +73,12 @@ func TestDecodeSinglePod(t *testing.T) {
}

for _, gv := range registered.EnabledVersionsForGroup(api.GroupName) {
externalPod, err := testapi.Default.Converter().ConvertToVersion(pod, gv.String())
s, _ := api.Codecs.SerializerForFileExtension("yaml")
encoder := api.Codecs.EncoderForVersion(s, gv)
yaml, err := runtime.Encode(encoder, pod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPod)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
Expand Down Expand Up @@ -122,7 +120,7 @@ func TestDecodePodList(t *testing.T) {
podList := &api.PodList{
Items: []api.Pod{*pod},
}
json, err := testapi.Default.Codec().Encode(podList)
json, err := runtime.Encode(testapi.Default.Codec(), podList)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand All @@ -138,21 +136,21 @@ func TestDecodePodList(t *testing.T) {
}

for _, gv := range registered.EnabledVersionsForGroup(api.GroupName) {
externalPodList, err := testapi.Default.Converter().ConvertToVersion(podList, gv.String())
if err != nil {
t.Errorf("unexpected error: %v", err)
}
yaml, err := yaml.Marshal(externalPodList)
s, _ := api.Codecs.SerializerForFileExtension("yaml")
encoder := api.Codecs.EncoderForVersion(s, gv)
yaml, err := runtime.Encode(encoder, podList)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

parsed, podListOut, err = tryDecodePodList(yaml, noDefault)
if !parsed {
t.Errorf("expected to have parsed file: (%s)", string(yaml))
t.Errorf("expected to have parsed file: (%s): %v", string(yaml), err)
continue
}
if err != nil {
t.Errorf("unexpected error: %v (%s)", err, string(yaml))
continue
}
if !reflect.DeepEqual(podList, &podListOut) {
t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, &podListOut, string(yaml))
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/config/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestReadPodsFromFile(t *testing.T) {
if err != nil {
t.Fatalf("%s: error in versioning the pod: %v", testCase.desc, err)
}
fileContents, err := testapi.Default.Codec().Encode(versionedPod)
fileContents, err := runtime.Encode(testapi.Default.Codec(), versionedPod)
if err != nil {
t.Fatalf("%s: error in encoding the pod: %v", testCase.desc, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/config/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestExtractPodsFromHTTP(t *testing.T) {
if err != nil {
t.Fatalf("%s: error in versioning the pods: %s", testCase.desc, err)
}
data, err := testapi.Default.Codec().Encode(versionedPods)
data, err := runtime.Encode(testapi.Default.Codec(), versionedPods)
if err != nil {
t.Fatalf("%s: error in encoding the pod: %v", testCase.desc, err)
}
Expand Down
Loading

0 comments on commit 33085c0

Please sign in to comment.