github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/test/runtime_unversioned_test.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package test
    18  
    19  import (
    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 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/api/apitesting"
    27  	metav1 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/apis/meta/v1"
    28  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime"
    29  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    30  )
    31  
    32  func 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  
    66  func TestExperimentalEncodeDecodeStatus(t *testing.T) {
    67  	status := &metav1.Status{
    68  		Status:  metav1.StatusFailure,
    69  		Code:    200,
    70  		Reason:  metav1.StatusReasonUnknown,
    71  		Message: "",
    72  	}
    73  	_, codecs := TestScheme()
    74  	expCodec := apitesting.TestCodec(codecs, schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal})
    75  
    76  	encoded, err := runtime.Encode(expCodec, status)
    77  	if err != nil {
    78  		t.Errorf("unexpected error: %v", err)
    79  	}
    80  	typeMeta := metav1.TypeMeta{}
    81  	if err := json.Unmarshal(encoded, &typeMeta); err != nil {
    82  		t.Errorf("unexpected error: %v", err)
    83  	}
    84  	if typeMeta.Kind != "Status" {
    85  		t.Errorf("Kind is not set to \"Status\". Got %s", encoded)
    86  	}
    87  	if typeMeta.APIVersion != "v1" {
    88  		t.Errorf("APIVersion is not set to \"\". Got %s", encoded)
    89  	}
    90  	decoded, err := runtime.Decode(expCodec, encoded)
    91  	if err != nil {
    92  		t.Errorf("unexpected error: %v", err)
    93  	}
    94  	if !reflect.DeepEqual(status, decoded) {
    95  		t.Errorf("expected: %v, got: %v", status, decoded)
    96  	}
    97  }