...

Source file src/k8s.io/kubectl/pkg/explain/typename_test.go

Documentation: k8s.io/kubectl/pkg/explain

     1  /*
     2  Copyright 2017 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 explain
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  	tst "k8s.io/kubectl/pkg/util/openapi/testing"
    24  )
    25  
    26  var resources = tst.NewFakeResources("test-swagger.json")
    27  
    28  func TestReferenceTypename(t *testing.T) {
    29  	schema := resources.LookupResource(schema.GroupVersionKind{
    30  		Group:   "",
    31  		Version: "v1",
    32  		Kind:    "OneKind",
    33  	})
    34  	if schema == nil {
    35  		t.Fatal("Couldn't find schema v1.OneKind")
    36  	}
    37  
    38  	tests := []struct {
    39  		name     string
    40  		path     []string
    41  		expected string
    42  	}{
    43  		{
    44  			// Kind is "Object"
    45  			name:     "test1",
    46  			path:     []string{},
    47  			expected: "Object",
    48  		},
    49  		{
    50  			// Reference is equal to pointed type "Object"
    51  			name:     "test2",
    52  			path:     []string{"field1"},
    53  			expected: "Object",
    54  		},
    55  		{
    56  			// Reference is equal to pointed type "string"
    57  			name:     "test3",
    58  			path:     []string{"field1", "primitive"},
    59  			expected: "string",
    60  		},
    61  		{
    62  			// Array of object of reference to string
    63  			name:     "test4",
    64  			path:     []string{"field2"},
    65  			expected: "[]map[string]string",
    66  		},
    67  		{
    68  			// Array of integer
    69  			name:     "test5",
    70  			path:     []string{"field1", "array"},
    71  			expected: "[]integer",
    72  		},
    73  	}
    74  
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			s, err := LookupSchemaForField(schema, tt.path)
    78  			if err != nil {
    79  				t.Fatalf("Invalid tt.path %v: %v", tt.path, err)
    80  			}
    81  			got := GetTypeName(s)
    82  			if got != tt.expected {
    83  				t.Errorf("Got %q, expected %q", got, tt.expected)
    84  			}
    85  		})
    86  	}
    87  
    88  }
    89  

View as plain text