testutils

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolPtr

func BoolPtr(b bool) *bool

BoolPtr creates a pointer to a bool value.

BoolPtr is a helper function for creating bool pointers in tests, which is useful when testing functions that accept optional bool parameters.

Parameters:

  • b: The bool value to convert to pointer

Returns a pointer to the bool value.

Example:

enabledPtr := BoolPtr(true)
// Use enabledPtr where *bool is expected

func CaptureOutput

func CaptureOutput(fn func()) string

CaptureOutput captures stdout output during test execution.

CaptureOutput redirects stdout to capture printed output and returns the captured content as a string. This is useful for testing functions that print to stdout.

Parameters:

  • fn: Function to execute while capturing output

Returns the captured stdout content as a string.

Example:

output := CaptureOutput(func() {
    fmt.Println("test output")
})
// output contains "test output\n"

func CreateTestCommand

func CreateTestCommand() *cobra.Command

CreateTestCommand creates a test cobra command with required setup.

CreateTestCommand initializes a basic cobra command that can be used in tests for verifying command configuration and flag setup.

Returns a configured cobra.Command instance ready for testing.

Example:

cmd := CreateTestCommand()
upgradeAction.DefineAction(cmd)
// Test the configured command

func CreateTestProfile

func CreateTestProfile(name string) *models.IdsecProfile

CreateTestProfile creates a basic test profile with the given name.

CreateTestProfile generates a standardized IdsecProfile instance for testing purposes. The profile includes ISP authentication configuration which is commonly used in login action tests.

Parameters:

  • name: The profile name to set

Returns a pointer to a fully configured IdsecProfile for testing.

Example:

profile := CreateTestProfile("test-profile")
// profile.ProfileName == "test-profile"

func CreateTestProfileWithAuth

func CreateTestProfileWithAuth(name, authName string, authMethod authmodels.IdsecAuthMethod) *models.IdsecProfile

CreateTestProfileWithAuth creates a test profile with specific authentication configuration.

CreateTestProfileWithAuth generates an IdsecProfile with customized authentication settings, allowing tests to specify the auth profile name and authentication method.

Parameters:

  • name: The profile name to set
  • authName: The name of the auth profile within the profile
  • authMethod: The authentication method to use

Returns a pointer to a configured IdsecProfile with custom auth settings.

Example:

profile := CreateTestProfileWithAuth("prod", "production", authmodels.Identity)
// profile has auth profile named "production" with Identity method

func CreateTestProfiles

func CreateTestProfiles(count int) []*models.IdsecProfile

CreateTestProfiles creates multiple test profiles with sequential naming.

CreateTestProfiles generates a slice of IdsecProfile instances with names following the pattern "profile1", "profile2", etc. This is useful for tests that need multiple profiles.

Parameters:

  • count: The number of profiles to create

Returns a slice of IdsecProfile pointers.

Example:

profiles := CreateTestProfiles(3)
// Returns profiles named "profile1", "profile2", "profile3"

func IntPtr

func IntPtr(i int) *int

IntPtr creates a pointer to an int value.

IntPtr is a helper function for creating int pointers in tests, which is useful when testing functions that accept optional int parameters.

Parameters:

  • i: The int value to convert to pointer

Returns a pointer to the int value.

Example:

countPtr := IntPtr(42)
// Use countPtr where *int is expected

func ProfileLoaderPtr

func ProfileLoaderPtr(loader profiles.ProfileLoader) *profiles.ProfileLoader

ProfileLoaderPtr converts a ProfileLoader to a ProfileLoader pointer.

ProfileLoaderPtr is a helper function for creating ProfileLoader pointers from ProfileLoader values, which is commonly needed for action constructors.

Parameters:

  • loader: The ProfileLoader interface to convert to pointer

Returns a pointer to the ProfileLoader interface.

Example:

mockLoader := &MockProfileLoader{}
loaderPtr := ProfileLoaderPtr(mockLoader)
action := NewIdsecLoginAction(loaderPtr)

func SetEnvVar

func SetEnvVar(key, value string) func()

SetEnvVar sets an environment variable for testing and returns a cleanup function.

SetEnvVar temporarily sets an environment variable to a specific value and returns a function that restores the original value when called. This ensures tests don't affect the global environment state.

Parameters:

  • key: Environment variable name
  • value: Value to set

Returns a cleanup function that restores the original environment state.

Example:

cleanup := SetEnvVar("GITHUB_URL", "github.example.com")
defer cleanup()
// Test code that uses GITHUB_URL

func SetupTestCommand

func SetupTestCommand(name string) *cobra.Command

SetupTestCommand creates a basic cobra command for testing.

SetupTestCommand creates a minimal cobra command with the given name that can be used as a parent command in tests. It includes basic configuration suitable for most test scenarios.

Parameters:

  • name: The command name to use

Returns a pointer to a configured cobra.Command.

Example:

cmd := SetupTestCommand("test")
// cmd.Use == "test"

func SetupTestCommandWithFlags

func SetupTestCommandWithFlags(name string, flagDefs map[string]string) *cobra.Command

SetupTestCommandWithFlags creates a cobra command with predefined flags.

SetupTestCommandWithFlags creates a cobra command and adds flags based on the provided flag definitions. The flagDefs map should have flag names as keys and flag configurations as values.

Parameters:

  • name: The command name to use
  • flagDefs: Map of flag name to flag configuration

Returns a pointer to a configured cobra.Command with flags.

Note: This is a basic implementation that can be extended based on specific flag type requirements.

func StringPtr

func StringPtr(s string) *string

StringPtr creates a pointer to a string value.

StringPtr is a helper function for creating string pointers in tests, which is useful when testing functions that accept optional string parameters.

Parameters:

  • s: The string value to convert to pointer

Returns a pointer to the string value.

Example:

namePtr := StringPtr("test-name")
// Use namePtr where *string is expected

Types

type MockFlagValue added in v0.1.12

type MockFlagValue struct {
	Val string
}

MockFlagValue is a mock implementation of pflag.Value for testing

func (*MockFlagValue) Set added in v0.1.12

func (m *MockFlagValue) Set(s string) error

Set sets the flag value

func (*MockFlagValue) String added in v0.1.12

func (m *MockFlagValue) String() string

String returns the string representation of the flag value

func (*MockFlagValue) Type added in v0.1.12

func (m *MockFlagValue) Type() string

Type returns the type of the flag

type MockProfileLoader

type MockProfileLoader struct {
	LoadProfileFunc        func(string) (*models.IdsecProfile, error)
	SaveProfileFunc        func(*models.IdsecProfile) error
	LoadAllProfilesFunc    func() ([]*models.IdsecProfile, error)
	LoadDefaultProfileFunc func() (*models.IdsecProfile, error)
	DeleteProfileFunc      func(string) error
	ClearAllProfilesFunc   func() error
	ProfileExistsFunc      func(string) bool
}

MockProfileLoader is a shared mock implementation of the profiles.ProfileLoader interface.

MockProfileLoader provides configurable mock behavior for all ProfileLoader methods through function fields. This allows tests to customize behavior for specific test scenarios while maintaining a consistent mock implementation across all tests.

Each method checks if a corresponding function field is set and calls it, otherwise returns sensible defaults (nil, nil for most cases).

func NewMockProfileLoader

func NewMockProfileLoader() *MockProfileLoader

NewMockProfileLoader creates a new MockProfileLoader instance.

NewMockProfileLoader returns a MockProfileLoader with all function fields set to nil, providing default behavior. Tests can customize behavior by setting the appropriate function fields.

Returns a pointer to a MockProfileLoader that implements profiles.ProfileLoader.

Example:

mock := NewMockProfileLoader()
mock.LoadProfileFunc = func(name string) (*models.IdsecProfile, error) {
    return &models.IdsecProfile{ProfileName: name}, nil
}
var loader profiles.ProfileLoader = mock

func (*MockProfileLoader) AsProfileLoader

func (m *MockProfileLoader) AsProfileLoader() *profiles.ProfileLoader

AsProfileLoader returns the mock as a profiles.ProfileLoader interface.

AsProfileLoader provides a convenient way to get the MockProfileLoader as a ProfileLoader interface pointer, which is commonly needed in test setup functions.

Returns a pointer to the ProfileLoader interface.

Example:

mock := NewMockProfileLoader()
loader := mock.AsProfileLoader()
action := NewIdsecProfilesAction(loader)

func (*MockProfileLoader) ClearAllProfiles

func (m *MockProfileLoader) ClearAllProfiles() error

ClearAllProfiles clears all profiles using the configured mock function or returns nil.

func (*MockProfileLoader) DeleteProfile

func (m *MockProfileLoader) DeleteProfile(profileName string) error

DeleteProfile deletes a profile using the configured mock function or returns nil.

func (*MockProfileLoader) LoadAllProfiles

func (m *MockProfileLoader) LoadAllProfiles() ([]*models.IdsecProfile, error)

LoadAllProfiles loads all profiles using the configured mock function or returns empty slice.

func (*MockProfileLoader) LoadDefaultProfile

func (m *MockProfileLoader) LoadDefaultProfile() (*models.IdsecProfile, error)

LoadDefaultProfile loads the default profile using the configured mock function or returns nil.

func (*MockProfileLoader) LoadProfile

func (m *MockProfileLoader) LoadProfile(name string) (*models.IdsecProfile, error)

LoadProfile loads a profile by name using the configured mock function or returns defaults.

func (*MockProfileLoader) ProfileExists

func (m *MockProfileLoader) ProfileExists(profileName string) bool

ProfileExists checks if a profile exists using the configured mock function or returns false.

func (*MockProfileLoader) SaveProfile

func (m *MockProfileLoader) SaveProfile(profile *models.IdsecProfile) error

SaveProfile saves a profile using the configured mock function or returns nil.

type TestComplexType

type TestComplexType struct {
	ID   string `mapstructure:"id"`
	Type string `mapstructure:"type"`
}

TestComplexType represents a nested struct for complex type testing.

TestComplexType provides a simple nested structure that can be used to test complex type parsing and serialization in service actions.

type TestSchema

type TestSchema struct {
	Name        string            `mapstructure:"name" validate:"required"`
	Count       int               `mapstructure:"count"`
	Tags        []string          `mapstructure:"tags"`
	Metadata    map[string]string `mapstructure:"metadata"`
	ComplexData []TestComplexType `mapstructure:"complex_data" desc:"Complex data field"`
	Choices     string            `mapstructure:"choices" choices:"option1,option2,option3"`
}

TestSchema represents a common test schema structure for service action testing.

TestSchema provides a standardized struct with various field types and validation tags that can be used across different action tests. It includes common patterns like required fields, choice validation, and complex types.

func CreateTestSchema

func CreateTestSchema() *TestSchema

CreateTestSchema creates a TestSchema instance with default test values.

CreateTestSchema generates a TestSchema with sensible default values for testing purposes. This provides a consistent starting point for tests that need a populated schema.

Returns a pointer to a TestSchema with default test values.

Example:

schema := CreateTestSchema()
// schema.Name == "test-schema"

func CreateTestSchemaWithName

func CreateTestSchemaWithName(name string) *TestSchema

CreateTestSchemaWithName creates a TestSchema with a specific name.

CreateTestSchemaWithName generates a TestSchema with the provided name and default values for other fields. This is useful when tests need to verify name-specific behavior.

Parameters:

  • name: The name to set in the schema

Returns a pointer to a TestSchema with the specified name.

Example:

schema := CreateTestSchemaWithName("custom-name")
// schema.Name == "custom-name"

Jump to

Keyboard shortcuts

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