test

package
v5.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ClientConfig mocked for testing
	ClientConfig = api.ClientConfig{
		Authentication: auth,
		Debug:          true,
		Host:           "domain.com",
		Timeout:        "15s",
		Token:          "secret",
	}
)

Functions

func CheckStringContains

func CheckStringContains(t *testing.T, got, expected string)

CheckStringContains check if string contains the expected value

func CheckStringOmits

func CheckStringOmits(t *testing.T, got, expected string)

CheckStringOmits check if string doesn't contain the expected value

func EmptyRun

func EmptyRun(*cobra.Command, []string)

EmptyRun an empty run

func ExecuteCommand

func ExecuteCommand(root *cobra.Command, args ...string) (output string, err error)

ExecuteCommand execute a command

func ResetCommandLineFlagSet

func ResetCommandLineFlagSet()

ResetCommandLineFlagSet resets the flagset

func ResetConfigManager

func ResetConfigManager()

ResetConfigManager reset the config manager

func RunCommandTests

func RunCommandTests(t *testing.T, cmdTests map[string]CommandTest)

RunCommandTests runs all set test scenarios *** USAGE ***

func TestFooCommands(t *testing.T) {
	scenarios := make(map[string]test.CommandTest)
	scenarios["'Foo' command should throw error without args"] =
		test.CommandTest{
			Cmd:     NewRootCommand,
			CmdArgs: []string{"foo"},11
			ShouldContainErrors: []string{`No args set!`},
		}
	scenarios["'Foo' command run successfully with args"] =
	test.CommandTest{
		Cmd:     NewRootCommand,
		CmdArgs: []string{"foo","bar"},11
		ShouldContain: []string{`Run smoothly!`},
	}

// Both tests will run

	test.RunCommandTests(t, scenarios)
}

func SetupConfigManager

func SetupConfigManager()

SetupConfigManager setup a new empty config manager if not done already

func SetupContext

func SetupContext(contextName string, clientConfig api.ClientConfig, basicAuth api.BasicAuthentication)

SetupContext add a new context in Config

func SetupMasterContext

func SetupMasterContext()

SetupMasterContext add a new context named "master" in Config

func TestingHTTPClient

func TestingHTTPClient(handler http.Handler) (*http.Client, func())

TestingHTTPClient starts a httptest.Server that serves the provided handler and returns an http.Client that always dials to this server.

Types

type CommandTest

type CommandTest struct {
	Setup               func()
	Teardown            func()
	Cmd                 func() *cobra.Command
	CmdArgs             []string
	ProcessOutput       func(t *testing.T, s string)
	ShouldContainErrors []string
	HasCustomError      error
	ShouldContain       []string
	ShouldNotContain    []string
}

CommandTest struct to define a test scenario

Jump to

Keyboard shortcuts

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