github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/schema/group_version_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 schema
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestGroupVersionParse(t *testing.T) {
    24  	tests := []struct {
    25  		input string
    26  		out   GroupVersion
    27  		err   func(error) bool
    28  	}{
    29  		{input: "v1", out: GroupVersion{Version: "v1"}},
    30  		{input: "v2", out: GroupVersion{Version: "v2"}},
    31  		{input: "/v1", out: GroupVersion{Version: "v1"}},
    32  		{input: "v1/", out: GroupVersion{Group: "v1"}},
    33  		{input: "/v1/", err: func(err error) bool { return err.Error() == "unexpected GroupVersion string: /v1/" }},
    34  		{input: "v1/a", out: GroupVersion{Group: "v1", Version: "a"}},
    35  	}
    36  	for i, test := range tests {
    37  		out, err := ParseGroupVersion(test.input)
    38  		if test.err == nil && err != nil || err == nil && test.err != nil {
    39  			t.Errorf("%d: unexpected error: %v", i, err)
    40  			continue
    41  		}
    42  		if test.err != nil && !test.err(err) {
    43  			t.Errorf("%d: unexpected error: %v", i, err)
    44  			continue
    45  		}
    46  		if out != test.out {
    47  			t.Errorf("%d: unexpected output: %#v", i, out)
    48  		}
    49  	}
    50  }
    51  
    52  func TestGroupResourceParse(t *testing.T) {
    53  	tests := []struct {
    54  		input string
    55  		out   GroupResource
    56  	}{
    57  		{input: "v1", out: GroupResource{Resource: "v1"}},
    58  		{input: ".v1", out: GroupResource{Group: "v1"}},
    59  		{input: "v1.", out: GroupResource{Resource: "v1"}},
    60  		{input: "v1.a", out: GroupResource{Group: "a", Resource: "v1"}},
    61  		{input: "b.v1.a", out: GroupResource{Group: "v1.a", Resource: "b"}},
    62  	}
    63  	for i, test := range tests {
    64  		out := ParseGroupResource(test.input)
    65  		if out != test.out {
    66  			t.Errorf("%d: unexpected output: %#v", i, out)
    67  		}
    68  	}
    69  }
    70  
    71  func TestParseResourceArg(t *testing.T) {
    72  	tests := []struct {
    73  		input string
    74  		gvr   *GroupVersionResource
    75  		gr    GroupResource
    76  	}{
    77  		{input: "v1", gr: GroupResource{Resource: "v1"}},
    78  		{input: ".v1", gr: GroupResource{Group: "v1"}},
    79  		{input: "v1.", gr: GroupResource{Resource: "v1"}},
    80  		{input: "v1.a", gr: GroupResource{Group: "a", Resource: "v1"}},
    81  		{input: "b.v1.a", gvr: &GroupVersionResource{Group: "a", Version: "v1", Resource: "b"}, gr: GroupResource{Group: "v1.a", Resource: "b"}},
    82  	}
    83  	for i, test := range tests {
    84  		gvr, gr := ParseResourceArg(test.input)
    85  		if (gvr != nil && test.gvr == nil) || (gvr == nil && test.gvr != nil) || (test.gvr != nil && *gvr != *test.gvr) {
    86  			t.Errorf("%d: unexpected output: %#v", i, gvr)
    87  		}
    88  		if gr != test.gr {
    89  			t.Errorf("%d: unexpected output: %#v", i, gr)
    90  		}
    91  	}
    92  }
    93  
    94  func TestKindForGroupVersionKinds(t *testing.T) {
    95  	gvks := GroupVersions{
    96  		GroupVersion{Group: "batch", Version: "v1"},
    97  		GroupVersion{Group: "batch", Version: "v2alpha1"},
    98  		GroupVersion{Group: "policy", Version: "v1beta1"},
    99  	}
   100  	cases := []struct {
   101  		input  []GroupVersionKind
   102  		target GroupVersionKind
   103  		ok     bool
   104  	}{
   105  		{
   106  			input:  []GroupVersionKind{{Group: "batch", Version: "v2alpha1", Kind: "ScheduledJob"}},
   107  			target: GroupVersionKind{Group: "batch", Version: "v2alpha1", Kind: "ScheduledJob"},
   108  			ok:     true,
   109  		},
   110  		{
   111  			input:  []GroupVersionKind{{Group: "batch", Version: "v3alpha1", Kind: "CronJob"}},
   112  			target: GroupVersionKind{Group: "batch", Version: "v1", Kind: "CronJob"},
   113  			ok:     true,
   114  		},
   115  		{
   116  			input:  []GroupVersionKind{{Group: "policy", Version: "v1beta1", Kind: "PodDisruptionBudget"}},
   117  			target: GroupVersionKind{Group: "policy", Version: "v1beta1", Kind: "PodDisruptionBudget"},
   118  			ok:     true,
   119  		},
   120  		{
   121  			input:  []GroupVersionKind{{Group: "apps", Version: "v1alpha1", Kind: "StatefulSet"}},
   122  			target: GroupVersionKind{},
   123  			ok:     false,
   124  		},
   125  	}
   126  
   127  	for i, c := range cases {
   128  		target, ok := gvks.KindForGroupVersionKinds(c.input)
   129  		if c.target != target {
   130  			t.Errorf("%d: unexpected target: %v, expected %v", i, target, c.target)
   131  		}
   132  		if c.ok != ok {
   133  			t.Errorf("%d: unexpected ok: %v, expected %v", i, ok, c.ok)
   134  		}
   135  	}
   136  }
   137  
   138  func TestParseKindArg(t *testing.T) {
   139  	tests := []struct {
   140  		input string
   141  		gvk   *GroupVersionKind
   142  		gk    GroupKind
   143  	}{
   144  		{input: "Pod", gk: GroupKind{Kind: "Pod"}},
   145  		{input: ".apps", gk: GroupKind{Group: "apps"}},
   146  		{input: "Pod.", gk: GroupKind{Kind: "Pod"}},
   147  		{input: "StatefulSet.apps", gk: GroupKind{Group: "apps", Kind: "StatefulSet"}},
   148  		{input: "StatefulSet.v1.apps", gvk: &GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}, gk: GroupKind{Group: "v1.apps", Kind: "StatefulSet"}},
   149  	}
   150  	for i, test := range tests {
   151  		t.Run(test.input, func(t *testing.T) {
   152  			gvk, gk := ParseKindArg(test.input)
   153  			if (gvk != nil && test.gvk == nil) || (gvk == nil && test.gvk != nil) || (test.gvk != nil && *gvk != *test.gvk) {
   154  				t.Errorf("%d: expected output: %#v, got: %#v", i, test.gvk, gvk)
   155  			}
   156  			if gk != test.gk {
   157  				t.Errorf("%d: expected output: %#v, got: %#v", i, test.gk, gk)
   158  			}
   159  		})
   160  	}
   161  }
   162  
   163  func TestParseGroupKind(t *testing.T) {
   164  	tests := []struct {
   165  		input string
   166  		out   GroupKind
   167  	}{
   168  		{input: "Pod", out: GroupKind{Kind: "Pod"}},
   169  		{input: ".StatefulSet", out: GroupKind{Group: "StatefulSet"}},
   170  		{input: "StatefulSet.apps", out: GroupKind{Group: "apps", Kind: "StatefulSet"}},
   171  	}
   172  	for i, test := range tests {
   173  		t.Run(test.input, func(t *testing.T) {
   174  			out := ParseGroupKind(test.input)
   175  			if out != test.out {
   176  				t.Errorf("%d: expected output: %#v, got: %#v", i, test.out, out)
   177  			}
   178  		})
   179  	}
   180  }
   181  
   182  func TestToAPIVersionAndKind(t *testing.T) {
   183  	tests := []struct {
   184  		desc         string
   185  		input        GroupVersionKind
   186  		GroupVersion string
   187  		Kind         string
   188  	}{
   189  		{
   190  			desc:         "gvk object is not empty",
   191  			input:        GroupVersionKind{Version: "V1", Kind: "pod"},
   192  			GroupVersion: "V1",
   193  			Kind:         "pod",
   194  		},
   195  		{
   196  			desc:         "gvk object is empty",
   197  			input:        GroupVersionKind{},
   198  			GroupVersion: "",
   199  			Kind:         "",
   200  		},
   201  	}
   202  	for i, test := range tests {
   203  		version, kind := test.input.ToAPIVersionAndKind()
   204  		if version != test.GroupVersion {
   205  			t.Errorf("%d: expected version: %#v, got: %#v", i, test.GroupVersion, version)
   206  		}
   207  		if kind != test.Kind {
   208  			t.Errorf("%d: expected kind: %#v, got: %#v", i, test.Kind, kind)
   209  		}
   210  	}
   211  }
   212  
   213  func TestBestMatch(t *testing.T) {
   214  	tests := []struct {
   215  		desc    string
   216  		kinds   []GroupVersionKind
   217  		targets []GroupVersionKind
   218  		output  GroupVersionKind
   219  	}{
   220  		{
   221  			desc:    "targets and kinds have match items",
   222  			kinds:   []GroupVersionKind{{Version: "V1", Kind: "pod"}, {Version: "V2", Kind: "pod"}},
   223  			targets: []GroupVersionKind{{Version: "V1", Kind: "pod"}},
   224  			output:  GroupVersionKind{Version: "V1", Kind: "pod"},
   225  		},
   226  		{
   227  			desc:    "targets and kinds do not have match items",
   228  			kinds:   []GroupVersionKind{{Version: "V1", Kind: "pod"}, {Version: "V2", Kind: "pod"}},
   229  			targets: []GroupVersionKind{{Version: "V3", Kind: "pod"}, {Version: "V4", Kind: "pod"}},
   230  			output:  GroupVersionKind{Version: "V3", Kind: "pod"},
   231  		},
   232  	}
   233  
   234  	for i, test := range tests {
   235  		out := bestMatch(test.kinds, test.targets)
   236  		if out != test.output {
   237  			t.Errorf("%d: expected out: %#v, got: %#v", i, test.output, out)
   238  		}
   239  	}
   240  }