github.com/verrazzano/verrazzano@v1.7.0/tools/psr/psrctl/cmd/root/root_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 root
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/explain"
    14  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/list"
    15  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/start"
    16  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/stop"
    17  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/update"
    18  	"github.com/verrazzano/verrazzano/tools/vz/cmd/version"
    19  	"github.com/verrazzano/verrazzano/tools/vz/pkg/constants"
    20  	"github.com/verrazzano/verrazzano/tools/vz/test/helpers"
    21  	"k8s.io/cli-runtime/pkg/genericclioptions"
    22  )
    23  
    24  func TestNewRootCmd(t *testing.T) {
    25  
    26  	buf := new(bytes.Buffer)
    27  	errBuf := new(bytes.Buffer)
    28  	rc := helpers.NewFakeRootCmdContext(genericclioptions.IOStreams{In: os.Stdin, Out: buf, ErrOut: errBuf})
    29  	rootCmd := NewRootCmd(rc)
    30  	assert.NotNil(t, rootCmd)
    31  
    32  	// Verify the expected commands are defined
    33  	assert.Len(t, rootCmd.Commands(), 6)
    34  	foundCount := 0
    35  	for _, cmd := range rootCmd.Commands() {
    36  		switch cmd.Name() {
    37  		case explain.CommandName:
    38  			foundCount++
    39  		case list.CommandName:
    40  			foundCount++
    41  		case start.CommandName:
    42  			foundCount++
    43  		case stop.CommandName:
    44  			foundCount++
    45  		case update.CommandName:
    46  			foundCount++
    47  		case version.CommandName:
    48  			foundCount++
    49  		}
    50  	}
    51  	assert.Equal(t, 6, foundCount)
    52  
    53  	//// Verify help has the expected elements
    54  	rootCmd.SetArgs([]string{fmt.Sprintf("--%s", constants.GlobalFlagHelp)})
    55  	err := rootCmd.Execute()
    56  	assert.NoError(t, err)
    57  
    58  	result := buf.String()
    59  	assert.Contains(t, result, "Usage:")
    60  	assert.Contains(t, result, "Available Commands:")
    61  	assert.Contains(t, result, "Flags:")
    62  }