github.com/verrazzano/verrazzano@v1.7.1/tools/vz/cmd/helpers/cmd_context_test.go (about) 1 // Copyright (c) 2022, 2024, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package helpers 5 6 import ( 7 "github.com/verrazzano/verrazzano/tools/vz/test/helpers" 8 "os" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/verrazzano/verrazzano/tools/vz/pkg/constants" 13 "k8s.io/cli-runtime/pkg/genericclioptions" 14 ) 15 16 const ( 17 testKubeConfig = "/tmp/kubeconfig" 18 testK8sContext = "testcontext" 19 ) 20 21 // TestGetHTTPClient tests the functionality to return the right HTTP client. 22 func TestGetHTTPClient(t *testing.T) { 23 rc := helpers.NewFakeRootCmdContextWithFiles(t) 24 defer helpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 25 httpClient := rc.GetHTTPClient() 26 assert.NotNil(t, httpClient) 27 } 28 29 // TestGetOutputStream tests the functionality to return the output stream set in the command context. 30 func TestGetOutputStream(t *testing.T) { 31 rc := helpers.NewFakeRootCmdContextWithFiles(t) 32 defer helpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 33 outputStream := rc.GetOutputStream() 34 assert.NotNil(t, outputStream) 35 } 36 37 // TestGetInputStream tests the functionality to return the input stream set in the command context. 38 func TestGetInputStream(t *testing.T) { 39 rc := helpers.NewFakeRootCmdContextWithFiles(t) 40 defer helpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 41 inputStream := rc.GetInputStream() 42 assert.NotNil(t, inputStream) 43 } 44 45 // TestGetInputStream tests the functionality to return the input stream set in the command context. 46 func TestGetErrorStream(t *testing.T) { 47 rc := helpers.NewFakeRootCmdContextWithFiles(t) 48 defer helpers.CleanUpNewFakeRootCmdContextWithFiles(rc) 49 errorStream := rc.GetErrorStream() 50 assert.NotNil(t, errorStream) 51 } 52 53 // TestGetKubeConfigGivenCommand tests the functionality to return the kube config set in the command context. 54 func TestGetKubeConfigGivenCommand(t *testing.T) { 55 cmdWithKubeConfigAndCtx := getCommandWithoutFlags() 56 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "") 57 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "") 58 _, err := getKubeConfigGivenCommand(cmdWithKubeConfigAndCtx) 59 assert.Error(t, err) 60 } 61 62 // TestGetClient tests the functionality to return the go client based on the kubeconfig parameters set in the command context. 63 func TestGetClient(t *testing.T) { 64 cmdWithKubeConfigAndCtx := getCommandWithoutFlags() 65 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "") 66 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "") 67 68 _, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetClient(cmdWithKubeConfigAndCtx) 69 assert.Error(t, err) 70 } 71 72 // TestGetClient tests the functionality to return the kube client based on the kubeconfig parameters set in the command context. 73 func TestGetKubeClient(t *testing.T) { 74 cmdWithKubeConfigAndCtx := getCommandWithoutFlags() 75 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "") 76 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "") 77 _, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetKubeClient(cmdWithKubeConfigAndCtx) 78 assert.Error(t, err) 79 } 80 81 // TestGetClient tests the functionality to return the dynamic client based on the kubeconfig parameters set in the command context. 82 func TestGetDynamicClient(t *testing.T) { 83 cmdWithKubeConfigAndCtx := getCommandWithoutFlags() 84 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "") 85 cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "") 86 _, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetDynamicClient(cmdWithKubeConfigAndCtx) 87 assert.Error(t, err) 88 }