github.com/verrazzano/verrazzano@v1.7.0/tools/vz/cmd/helpers/cmd_context_test.go (about)

     1  // Copyright (c) 2022, 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  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/verrazzano/verrazzano/tools/vz/pkg/constants"
    12  	"k8s.io/cli-runtime/pkg/genericclioptions"
    13  )
    14  
    15  const (
    16  	testKubeConfig = "/tmp/kubeconfig"
    17  	testK8sContext = "testcontext"
    18  )
    19  
    20  // TestGetHTTPClient tests the functionality to return the right HTTP client.
    21  func TestGetHTTPClient(t *testing.T) {
    22  	httpClient := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetHTTPClient()
    23  	assert.NotNil(t, httpClient)
    24  }
    25  
    26  // TestGetOutputStream tests the functionality to return the output stream set in the command context.
    27  func TestGetOutputStream(t *testing.T) {
    28  	outputStream := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetOutputStream()
    29  	assert.NotNil(t, outputStream)
    30  }
    31  
    32  // TestGetInputStream tests the functionality to return the input stream set in the command context.
    33  func TestGetInputStream(t *testing.T) {
    34  	inputStream := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetInputStream()
    35  	assert.NotNil(t, inputStream)
    36  }
    37  
    38  // TestGetInputStream tests the functionality to return the input stream set in the command context.
    39  func TestGetErrorStream(t *testing.T) {
    40  	inputStream := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetErrorStream()
    41  	assert.NotNil(t, inputStream)
    42  }
    43  
    44  // TestGetKubeConfigGivenCommand tests the functionality to return the kube config set in the command context.
    45  func TestGetKubeConfigGivenCommand(t *testing.T) {
    46  	cmdWithKubeConfigAndCtx := getCommandWithoutFlags()
    47  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "")
    48  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
    49  	_, err := getKubeConfigGivenCommand(cmdWithKubeConfigAndCtx)
    50  	assert.Error(t, err)
    51  }
    52  
    53  // TestGetClient tests the functionality to return the go client based on the kubeconfig parameters set in the command context.
    54  func TestGetClient(t *testing.T) {
    55  	cmdWithKubeConfigAndCtx := getCommandWithoutFlags()
    56  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "")
    57  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
    58  	_, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetClient(cmdWithKubeConfigAndCtx)
    59  	assert.Error(t, err)
    60  }
    61  
    62  // TestGetClient tests the functionality to return the kube client based on the kubeconfig parameters set in the command context.
    63  func TestGetKubeClient(t *testing.T) {
    64  	cmdWithKubeConfigAndCtx := getCommandWithoutFlags()
    65  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "")
    66  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
    67  	_, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetKubeClient(cmdWithKubeConfigAndCtx)
    68  	assert.Error(t, err)
    69  }
    70  
    71  // TestGetClient tests the functionality to return the dynamic client based on the kubeconfig parameters set in the command context.
    72  func TestGetDynamicClient(t *testing.T) {
    73  	cmdWithKubeConfigAndCtx := getCommandWithoutFlags()
    74  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagKubeConfig, testKubeConfig, "")
    75  	cmdWithKubeConfigAndCtx.Flags().String(constants.GlobalFlagContext, testK8sContext, "")
    76  	_, err := NewRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}).GetDynamicClient(cmdWithKubeConfigAndCtx)
    77  	assert.Error(t, err)
    78  }