clitesting

package
v0.0.0-...-7b190fc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyAuthToken         = "auth_token"
	KeyRefreshToken      = "refresh_token"
	KeyConnectedEndpoint = "connected_endpoint"
	APIHost              = "https://"
)

Variables

View Source
var (
	ErrNotLoggedIn = errors.New("auth token isn't present in local cache - please run auth login")
)

Functions

This section is empty.

Types

type CLITest

type CLITest struct {
	// A function that will run once before any tests execute.
	SetupTest func(ctx context.Context, t *testing.T)
	// A function that will run once after all tests have run (optional)
	TeardownTest func(ctx context.Context, t *testing.T)
	// A function that will run before each test case (i.e. before each element in the []CLITestCase passed to RunTests)
	SetupTestCase func(ctx context.Context, t *testing.T, tc CLITestCase)
	// A function that will run after each test case
	TeardownTestCase func(ctx context.Context, t *testing.T, tc CLITestCase)
	// A Context object that can be used to maintain context between tests/test cases
	Ctx context.Context
	// newCmd function to test
	CmdToTest  func() *cobra.Command
	ParentCmds []func() *cobra.Command
	// function to initialize the command specific CmdOptions structure
	CmdOptionsInit func() CmdOpts
	// This is a function that will run before the command's Execute() for every test (i.e. a global PreExecute)
	PreExecute func(ctx context.Context, t *testing.T, tc CLITestCase, cmd *cobra.Command, cmdOpts CmdOpts)
	// should the Unauthenticated test be automatically added to the run
	AddUnauthenticatedTest bool
	// Args to set for unauthenticated test
	UnauthTestArgs []string
	// should the API Failure test be automatically added to the run
	AddFailTest      bool
	FailTestMethod   string
	FailTestEndpoint string
	FailTestPath     string
	FailTestArgs     []string
	DumpMocks        bool
	// contains filtered or unexported fields
}

func (*CLITest) RunTests

func (clitest *CLITest) RunTests(t *testing.T, testCases []CLITestCase)

type CLITestCase

type CLITestCase struct {
	// Description of the test case
	Name string
	// Arguments to pass while executing the command for this test case
	Args []string
	// Whether to inject a token into the localstate before starting the test
	TokenPresent    bool
	UseInvalidToken bool
	UseExpiredToken bool
	// Setup pseudo-tty for go-expect testing and call this function for test
	PseudoTTY func(ctx context.Context, t *testing.T, console *expect.Console)
	// Default timeout for go-expect responses in seconds
	ExpectTimeout int64
	// Whether an error is expected
	WantErr bool
	// String to compare error to
	ExpError string
	// A function that will run before the test and will setup Gock for the test
	GockSetup func(ctx context.Context, t *testing.T)
	// A function that will run before the command's Execute() method is called - a good place to call SetArgs
	PreExecute func(ctx context.Context, t *testing.T, cmd *cobra.Command, cmdOpts CmdOpts)
	// A function that will run to validate the output of the test
	ValidateOutput      ValidateOutputFunc
	ValidateAuditFields bool
}

CLITestCase is a structure used to define the specific test cases to be run via RunTests.

type CmdOpts

type CmdOpts interface {
	SetTTY(tty *os.File)
}
var EmptyOpts CmdOpts

type ValidateOutputFunc

type ValidateOutputFunc func(ctx context.Context, t *testing.T, cmdOpts CmdOpts, output []byte) bool

func ValidateOutputContains

func ValidateOutputContains(expectedString string) ValidateOutputFunc

ValidateOutputContains helper to assert if the output contains the given string

func ValidateOutputJson

func ValidateOutputJson(expectedJsonString string) ValidateOutputFunc

ValidateOutputJson helper to assert if the json output

func ValidateOutputMatch

func ValidateOutputMatch(expectedString string) ValidateOutputFunc

ValidateOutputContains helper to assert if the output is exactly same as the given string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL