mirror of https://github.com/k3s-io/k3s
60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
/*
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
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.
|
|
*/
|
|
|
|
package runtime_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
)
|
|
|
|
func TestEncode(t *testing.T) {
|
|
pod := &api.Pod{
|
|
Labels: map[string]string{"name": "foo"},
|
|
}
|
|
obj := runtime.Object(pod)
|
|
data, err := runtime.DefaultScheme.Encode(obj)
|
|
obj2, err2 := runtime.DefaultScheme.Decode(data)
|
|
if err != nil || err2 != nil {
|
|
t.Fatalf("Failure: '%v' '%v'", err, err2)
|
|
}
|
|
if _, ok := obj2.(*api.Pod); !ok {
|
|
t.Fatalf("Got wrong type")
|
|
}
|
|
if !reflect.DeepEqual(obj2, pod) {
|
|
t.Errorf("Expected:\n %#v,\n Got:\n %#v", &pod, obj2)
|
|
}
|
|
}
|
|
|
|
func TestBadJSONRejection(t *testing.T) {
|
|
badJSONMissingKind := []byte(`{ }`)
|
|
if _, err := runtime.DefaultScheme.Decode(badJSONMissingKind); err == nil {
|
|
t.Errorf("Did not reject despite lack of kind field: %s", badJSONMissingKind)
|
|
}
|
|
badJSONUnknownType := []byte(`{"kind": "bar"}`)
|
|
if _, err1 := runtime.DefaultScheme.Decode(badJSONUnknownType); err1 == nil {
|
|
t.Errorf("Did not reject despite use of unknown type: %s", badJSONUnknownType)
|
|
}
|
|
/*badJSONKindMismatch := []byte(`{"kind": "Pod"}`)
|
|
if err2 := DecodeInto(badJSONKindMismatch, &Minion{}); err2 == nil {
|
|
t.Errorf("Kind is set but doesn't match the object type: %s", badJSONKindMismatch)
|
|
}*/
|
|
}
|