...

Source file src/k8s.io/kubectl/pkg/cmd/apiresources/apiversions_test.go

Documentation: k8s.io/kubectl/pkg/cmd/apiresources

     1  /*
     2  Copyright 2022 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 apiresources
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/cli-runtime/pkg/genericiooptions"
    26  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    27  )
    28  
    29  func TestAPIVersionsComplete(t *testing.T) {
    30  	tf := cmdtesting.NewTestFactory()
    31  	defer tf.Cleanup()
    32  	cmd := NewCmdAPIVersions(tf, genericiooptions.NewTestIOStreamsDiscard())
    33  	parentCmd := &cobra.Command{Use: "kubectl"}
    34  	parentCmd.AddCommand(cmd)
    35  	o := NewAPIVersionsOptions(genericiooptions.NewTestIOStreamsDiscard())
    36  
    37  	err := o.Complete(tf, cmd, []string{})
    38  	if err != nil {
    39  		t.Fatalf("Unexpected error: %v", err)
    40  	}
    41  
    42  	err = o.Complete(tf, cmd, []string{"foo"})
    43  	if err == nil {
    44  		t.Fatalf("An error was expected but not returned")
    45  	}
    46  	expectedError := `unexpected arguments: [foo]
    47  See 'kubectl api-versions -h' for help and examples`
    48  	if err.Error() != expectedError {
    49  		t.Fatalf("Unexpected error: %v\n expected: %v", err, expectedError)
    50  	}
    51  }
    52  
    53  func TestAPIVersionsRun(t *testing.T) {
    54  	dc := cmdtesting.NewFakeCachedDiscoveryClient()
    55  	dc.Groups = []*v1.APIGroup{
    56  		{
    57  			Name: "",
    58  			Versions: []v1.GroupVersionForDiscovery{
    59  				{GroupVersion: "v1"},
    60  			},
    61  		},
    62  		{
    63  			Name: "foo",
    64  			Versions: []v1.GroupVersionForDiscovery{
    65  				{GroupVersion: "foo/v1beta1"},
    66  				{GroupVersion: "foo/v1"},
    67  				{GroupVersion: "foo/v2"},
    68  			},
    69  		},
    70  		{
    71  			Name: "bar",
    72  			Versions: []v1.GroupVersionForDiscovery{
    73  				{GroupVersion: "bar/v1"},
    74  			},
    75  		},
    76  	}
    77  	tf := cmdtesting.NewTestFactory().WithDiscoveryClient(dc)
    78  	defer tf.Cleanup()
    79  
    80  	ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams()
    81  	cmd := NewCmdAPIVersions(tf, ioStreams)
    82  	cmd.Run(cmd, []string{})
    83  
    84  	if errOut.Len() > 0 {
    85  		t.Fatalf("unexpected error output: %s", errOut.String())
    86  	}
    87  
    88  	expectedOutput := `bar/v1
    89  foo/v1
    90  foo/v1beta1
    91  foo/v2
    92  v1
    93  `
    94  	if out.String() != expectedOutput {
    95  		t.Fatalf("unexpected output: %s\nexpected: %s", out.String(), expectedOutput)
    96  	}
    97  
    98  	expectedInvalidations := 1
    99  	if dc.Invalidations != expectedInvalidations {
   100  		t.Fatalf("unexpected invalidations: %d, expected: %d", dc.Invalidations, expectedInvalidations)
   101  	}
   102  }
   103  

View as plain text