github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/serializer/yaml/meta_test.go (about)

     1  /*
     2  Copyright 2019 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 yaml
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    24  )
    25  
    26  func TestInterpret(t *testing.T) {
    27  	testCases := []struct {
    28  		name     string
    29  		input    string
    30  		expected *schema.GroupVersionKind
    31  		errFn    func(error) bool
    32  	}{
    33  		{
    34  			name: "YAMLSuccessfullyInterpretVK",
    35  			input: `apiVersion: v1
    36  kind: Service`,
    37  			expected: &schema.GroupVersionKind{Version: "v1", Kind: "Service"},
    38  		},
    39  		{
    40  			name: "YAMLSuccessfullyInterpretGVK",
    41  			input: `apiVersion: core/v2
    42  kind: Deployment`,
    43  			expected: &schema.GroupVersionKind{Group: "core", Version: "v2", Kind: "Deployment"},
    44  		},
    45  		{
    46  			name:     "YAMLSuccessfullyInterpretV",
    47  			input:    `apiVersion: v1`,
    48  			expected: &schema.GroupVersionKind{Version: "v1"},
    49  		},
    50  		{
    51  			name:     "YAMLSuccessfullyInterpretK",
    52  			input:    `kind: Service`,
    53  			expected: &schema.GroupVersionKind{Kind: "Service"},
    54  		},
    55  		{
    56  			name:     "YAMLSuccessfullyInterpretEmptyString",
    57  			input:    ``,
    58  			expected: &schema.GroupVersionKind{},
    59  		},
    60  		{
    61  			name:     "YAMLSuccessfullyInterpretEmptyDoc",
    62  			input:    `---`,
    63  			expected: &schema.GroupVersionKind{},
    64  		},
    65  		{
    66  			name: "YAMLSuccessfullyInterpretMultiDoc",
    67  			input: `---
    68  apiVersion: v1
    69  kind: Service
    70  ---
    71  apiVersion: v2
    72  kind: Deployment`,
    73  			expected: &schema.GroupVersionKind{Version: "v1", Kind: "Service"},
    74  		},
    75  		{
    76  			name:     "YAMLSuccessfullyInterpretOnlyG",
    77  			input:    `apiVersion: core/`,
    78  			expected: &schema.GroupVersionKind{Group: "core"},
    79  		},
    80  		{
    81  			name:     "YAMLSuccessfullyWrongFormat",
    82  			input:    `foo: bar`,
    83  			expected: &schema.GroupVersionKind{},
    84  		},
    85  		{
    86  			name:  "YAMLFailInterpretWrongSyntax",
    87  			input: `foo`,
    88  			errFn: func(err error) bool { return err != nil },
    89  		},
    90  		{
    91  			name:     "JSONSuccessfullyInterpretVK",
    92  			input:    `{"apiVersion": "v3", "kind": "DaemonSet"}`,
    93  			expected: &schema.GroupVersionKind{Version: "v3", Kind: "DaemonSet"},
    94  		},
    95  		{
    96  			name:     "JSONSuccessfullyInterpretGVK",
    97  			input:    `{"apiVersion": "core/v2", "kind": "Deployment"}`,
    98  			expected: &schema.GroupVersionKind{Group: "core", Version: "v2", Kind: "Deployment"},
    99  		},
   100  		{
   101  			name:     "JSONSuccessfullyInterpretV",
   102  			input:    `{"apiVersion": "v1"}`,
   103  			expected: &schema.GroupVersionKind{Version: "v1"},
   104  		},
   105  		{
   106  			name:     "JSONSuccessfullyInterpretK",
   107  			input:    `{"kind": "Service"}`,
   108  			expected: &schema.GroupVersionKind{Kind: "Service"},
   109  		},
   110  		{
   111  			name:     "JSONSuccessfullyInterpretEmptyString",
   112  			input:    ``,
   113  			expected: &schema.GroupVersionKind{},
   114  		},
   115  		{
   116  			name:     "JSONSuccessfullyInterpretEmptyObject",
   117  			input:    `{}`,
   118  			expected: &schema.GroupVersionKind{},
   119  		},
   120  		{
   121  			name: "JSONSuccessfullyInterpretMultiDoc",
   122  			input: `{"apiVersion": "v1", "kind": "Service"}, 
   123  {"apiVersion": "v2", "kind": "Deployment"}`,
   124  			expected: &schema.GroupVersionKind{Version: "v1", Kind: "Service"},
   125  		},
   126  		{
   127  			name:     "JSONSuccessfullyWrongFormat",
   128  			input:    `{"foo": "bar"}`,
   129  			expected: &schema.GroupVersionKind{},
   130  		},
   131  		{
   132  			name:  "JSONFailInterpretArray",
   133  			input: `[]`,
   134  			errFn: func(err error) bool { return err != nil },
   135  		},
   136  		{
   137  			name:  "JSONFailInterpretWrongSyntax",
   138  			input: `{"foo"`,
   139  			errFn: func(err error) bool { return err != nil },
   140  		},
   141  	}
   142  
   143  	for _, test := range testCases {
   144  		t.Run(test.name, func(t *testing.T) {
   145  			actual, err := DefaultMetaFactory.Interpret([]byte(test.input))
   146  			switch {
   147  			case test.errFn != nil:
   148  				if !test.errFn(err) {
   149  					t.Errorf("unexpected error: %v", err)
   150  				}
   151  			case err != nil:
   152  				t.Errorf("unexpected error: %v", err)
   153  			case !reflect.DeepEqual(test.expected, actual):
   154  				t.Errorf("outcome mismatch -- expected: %#v, actual: %#v",
   155  					test.expected, actual,
   156  				)
   157  			}
   158  		})
   159  	}
   160  }