Documentation
¶
Overview ¶
Package common contains general functions that are used by various packages and unit tests in ibmcloud-terratest-wrapper module
Package common provides utilities for working with Git repositories.
Index ¶
- Constants
- Variables
- func ColorizeString(color, message string) string
- func ConditionalAdd(amap map[string]interface{}, key string, value string, compareValue string)
- func ConvertArrayToJsonString(arr interface{}) (string, error)
- func CopyDirectory(src string, dst string, fileFilter ...func(string) bool) error
- func CopyFile(source, destination string) error
- func DetermineAuthMethod(repoURL string) (transport.AuthMethod, error)
- func FormatJsonStringPretty(jsonString string) (string, error)
- func GenerateSshRsaPublicKey() (string, error)
- func GenerateTempGPGKeyPairBase64() (privateKeyBase64 string, publicKeyBase64 string, err error)
- func GetAllRefsAsString(stackRef *StackRef) string
- func GetAllUnresolvedRefsAsString(stackRef *StackRef) string
- func GetBaseRepoAndBranch(repo string, branch string) (string, string)
- func GetBeforeAfterDiff(jsonString string) (string, error)
- func GetCurrentPrRepoAndBranch() (string, string, error)
- func GetRequiredEnvVars(t *testing.T, variableNames []string) map[string]string
- func GetTagsFromTravis() []string
- func GitRootPath(fromPath string) (string, error)
- func IntArrayContains(arr []int, val int) bool
- func IsArray(v interface{}) bool
- func IsJsonEqual(jsonFile1 string, jsonFile2 string) (bool, error)
- func LoadMapFromYaml(filePath string) (map[string]interface{}, error)
- func MergeMaps(maps ...map[string]interface{}) map[string]interface{}
- func PrintAllRefs(stackRef *StackRef)
- func PrintStructAsJson(data interface{})
- func PrintUnresolvedRefs(stackRef *StackRef)
- func ResolveReferences(stackRef *StackRef)
- func RetrievePrivateKey(sshPvtKey string) (interface{}, error)
- func SanitizeSensitiveData(inputJSON string, secureList map[string]interface{}) (string, error)
- func SortMap(m map[string]interface{})
- func SortMapKeys(m map[string]interface{}) []string
- func SortSlice(slice []interface{})
- func StrArrayContains(arr []string, val string) bool
- func StringContainsIgnoreCase(s, substr string) bool
- type Color
- type ConfigRefs
- type Ref
- type StackRef
- type TestLogger
- func (t *TestLogger) Custom(level, message, color string)
- func (t *TestLogger) Debug(message string)
- func (t *TestLogger) EnableDateTime(enable bool)
- func (t *TestLogger) Error(message string)
- func (t *TestLogger) Info(message string)
- func (t *TestLogger) SetPrefix(prefix string)
- func (t *TestLogger) ShortCustom(message, color string)
- func (t *TestLogger) ShortDebug(message string)
- func (t *TestLogger) ShortError(message string)
- func (t *TestLogger) ShortInfo(message string)
Constants ¶
const SANITIZE_STRING = "SECURE_VALUE_HIDDEN_HASH:"
SANITIZE_STRING is the string used to replace sensitive values.
Variables ¶
var Colors = Color{
Reset: "\033[0m",
Red: "\033[31m",
Green: "\033[32m",
Yellow: "\033[33m",
Blue: "\033[34m",
Orange: "\033[38;5;208m",
Purple: "\033[35m",
Cyan: "\033[36m",
}
Colors instance to access color codes
Functions ¶
func ColorizeString ¶ added in v1.39.6
ColorizeString returns a string with the specified color Use the Colors struct to access the color codes Example: ColorizeString(Colors.Red, "This is red")
func ConditionalAdd ¶
Adds value to map[key] only if value != compareValue
func ConvertArrayToJsonString ¶
ConvertArrayToJsonString is a helper function that will take an array of Golang data types, and return a string of the array formatted as a JSON array. Helpful to convert Golang arrays into a format that Terraform can consume.
func CopyDirectory ¶ added in v1.11.0
CopyDirectory copies a directory from source to destination, with optional file filtering. src Source directory to copy from dst Destination directory to copy to fileFilter Optional function to filter files to copy Returns an error if the operation fails.
func CopyFile ¶ added in v1.10.10
CopyFile copies a file from source to destination. Returns an error if the operation fails.
func DetermineAuthMethod ¶ added in v1.11.0
func DetermineAuthMethod(repoURL string) (transport.AuthMethod, error)
DetermineAuthMethod determines the appropriate authentication method for a given repository URL. The function supports both HTTPS and SSH-based repositories.
For HTTPS repositories: - It first checks if the GIT_TOKEN environment variable is set. If so, it uses this as the Personal Access Token (PAT). - If the GIT_TOKEN environment variable is not set, no authentication is used for HTTPS repositories.
For SSH repositories: - It first checks if the SSH_PRIVATE_KEY environment variable is set. If so, it uses this as the SSH private key. - If the SSH_PRIVATE_KEY environment variable is not set, it attempts to use the default SSH key located at ~/.ssh/id_rsa. - If neither the environment variable nor the default key is available, no authentication is used for SSH repositories.
Parameters: - repoURL: The URL of the Git repository.
Returns: - An appropriate AuthMethod based on the repository URL and available credentials. - An error if there's an issue parsing the SSH private key or if the private key cannot be cast to an ssh.Signer.
func FormatJsonStringPretty ¶ added in v1.23.6
func GenerateSshRsaPublicKey ¶ added in v1.10.0
Generate an SSH RSA Keypair (4096 bits), and return the PublicKey in OpenSSH Authorized Key format. Used for tests to generate unique throw-away (but valid) SSH key to supply to test inputs. SPECIAL NOTE: the newline character at end of key will be trimmed and not included!
func GenerateTempGPGKeyPairBase64 ¶ added in v1.33.0
GenerateTempGPGKeyPairBase64 generates a temporary GPG key pair and returns the private and public keys in base64 format. The function first creates a new pair of keys using the openpgp.NewEntity function with a SHA256 hash configuration. It then serializes the private and public keys into bytes.Buffer variables. If any error occurs during the creation or serialization of the keys, the function returns the error. Finally, the function encodes the serialized private and public keys into base64 format and returns them as strings. The function returns two strings: the first is the base64-encoded private key and the second is the base64-encoded public key.
func GetAllRefsAsString ¶ added in v1.39.0
GetAllRefsAsString returns all references in a StackRef structure as a single string
func GetAllUnresolvedRefsAsString ¶ added in v1.39.0
GetAllUnresolvedRefsAsString returns all unresolved references in a StackRef structure as a single string
func GetBaseRepoAndBranch ¶ added in v1.11.0
GetBaseRepoAndBranch determines the base repository URL and branch name based on a hierarchy of sources. The function first checks the provided arguments, then checks environment variables, and finally, if neither source provides the values, it uses Git logic to fetch the details. This function is useful in scenarios where you want to determine the base repository and branch details from multiple potential sources.
Parameters:
- repo: The initial repository URL to consider. This can be an empty string if you want the function to determine the repository URL from other sources.
- branch: The initial branch name to consider. This can be an empty string if you want the function to determine the branch name from other sources.
Returns: - A string representing the base repository URL. - A string representing the base branch name. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.
func GetBeforeAfterDiff ¶
GetBeforeAfterDiff takes a JSON string as input and returns a string with the differences between the "before" and "after" objects in the JSON.
For example, given the JSON string:
{"before": {"a": 1, "b": 2}, "after": {"a": 2, "b": 3}}
the function would return the string:
"Before: {"b": 2}\nAfter: {"a": 2, "b": 3}"
func GetCurrentPrRepoAndBranch ¶ added in v1.11.0
GetCurrentPrRepoAndBranch returns the repository URL and branch name of the current PR.
Returns: - A string representing the repository URL of the current PR. - A string representing the branch name of the current PR. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.
func GetRequiredEnvVars ¶
GetRequiredEnvVars returns a map containing required environment variables and their values Fails the test if any are missing
func GetTagsFromTravis ¶
func GetTagsFromTravis() []string
GetTagsFromTravis Generates a list of tags to add to resources if running in Travis. Returns empty list if not in Travis
func GitRootPath ¶
GitRootPath returns the root directory of the current Git repository.
Parameters: - fromPath: The directory from which the Git command should be executed.
Returns: - A string representing the path to the root directory of the Git repository. - An error if the command fails or the directory is not part of a Git repository.
func IntArrayContains ¶ added in v1.5.1
IntArrayContains is a helper function that will check an array and see if an int value is already present
func IsArray ¶
func IsArray(v interface{}) bool
IsArray is a simple helper function that will determine if a given Golang value is a slice or array.
func IsJsonEqual ¶ added in v1.23.0
IsJsonEqual validates whether the two JSON files are equal or not
func LoadMapFromYaml ¶ added in v1.8.0
LoadMapFromYaml loads a YAML file into a map[string]interface{}. It returns the resulting map and any error encountered.
func PrintAllRefs ¶ added in v1.39.0
func PrintAllRefs(stackRef *StackRef)
PrintAllRefs prints all references in a StackRef structure
func PrintStructAsJson ¶ added in v1.39.0
func PrintStructAsJson(data interface{})
PrintStructAsJson prints a struct as a formatted JSON string
func PrintUnresolvedRefs ¶ added in v1.39.0
func PrintUnresolvedRefs(stackRef *StackRef)
PrintUnresolvedRefs prints the unresolved references in a StackRef structure
func ResolveReferences ¶ added in v1.39.0
func ResolveReferences(stackRef *StackRef)
ResolveReferences traverses the StackRef structure and resolves references
func RetrievePrivateKey ¶ added in v1.32.0
func SanitizeSensitiveData ¶ added in v1.23.6
SanitizeSensitiveData takes a JSON string and a list of sensitive keys and replaces the values of the sensitive keys with a predefined string.
func SortMap ¶ added in v1.23.0
func SortMap(m map[string]interface{})
SortMap sorts the map and any nested structures inside it. It modifies the original map.
func SortMapKeys ¶ added in v1.23.0
SortMapKeys sorts the keys of a map and returns them as a slice.
func SortSlice ¶ added in v1.23.0
func SortSlice(slice []interface{})
SortSlice sorts the slices recursively. It also takes care of any nested slices or maps inside the slice.
func StrArrayContains ¶
StrArrayContains is a helper function that will check an array and see if a value is already present
func StringContainsIgnoreCase ¶ added in v1.28.1
StringContainsIgnoreCase checks if a string contains a substring, ignoring case. Returns true if the string contains the substring, false otherwise.
Types ¶
type Color ¶ added in v1.39.6
type Color struct { // Reset ANSI color code Reset string // Red ANSI color code Red string // Green ANSI color code Green string // Yellow ANSI color code Yellow string // Blue ANSI color code Blue string // Orange ANSI color code Orange string // Purple ANSI color code Purple string // Cyan ANSI color code Cyan string }
Color struct to hold ANSI color codes
type ConfigRefs ¶ added in v1.39.0
ConfigRefs represents the configuration references for a stack member
func ProcessMembers ¶ added in v1.39.0
func ProcessMembers(members []*project.ProjectConfig) []ConfigRefs
ProcessMembers processes the stack members and returns a slice of ConfigRefs structs
type Ref ¶ added in v1.39.0
type Ref struct { Name string Ref *string ResolvedValue *string Resolved bool // contains filtered or unexported fields }
Ref represents a reference with its name, resolved value, and flags
func GetAllRefs ¶ added in v1.39.0
GetAllRefs returns all references in a StackRef structure
func GetAllUnresolvedRefs ¶ added in v1.39.0
GetAllUnresolvedRefs returns all unresolved references in a StackRef structure
func ProcessInputs ¶ added in v1.39.0
ProcessInputs processes the input values and returns a slice of Ref structs
func ProcessOutputs ¶ added in v1.39.0
func ProcessOutputs(outputs []project.OutputValue) []Ref
ProcessOutputs processes the output values and returns a slice of Ref structs
type StackRef ¶ added in v1.39.0
type StackRef struct { Name string ID string Inputs []Ref Outputs []Ref Members []ConfigRefs Resolved bool }
StackRef represents the stack reference with inputs, outputs, and members
func CreateStackRefStruct ¶ added in v1.39.0
func CreateStackRefStruct(stackConfig *project.StackDefinition, members []*project.ProjectConfig) (*StackRef, error)
type TestLogger ¶ added in v1.39.6
type TestLogger struct {
// contains filtered or unexported fields
}
TestLogger struct to hold the logger configuration
func NewTestLogger ¶ added in v1.39.6
func NewTestLogger(testName string) *TestLogger
NewTestLogger initializes the custom logger
func NewTestLoggerWithPrefix ¶ added in v1.39.6
func NewTestLoggerWithPrefix(testName, prefix string) *TestLogger
NewTestLoggerWithPrefix initializes the custom logger with a prefix
func (*TestLogger) Custom ¶ added in v1.39.6
func (t *TestLogger) Custom(level, message, color string)
Custom logs a message with a custom level name and color
func (*TestLogger) Debug ¶ added in v1.39.6
func (t *TestLogger) Debug(message string)
Debug logs a debug message
func (*TestLogger) EnableDateTime ¶ added in v1.39.6
func (t *TestLogger) EnableDateTime(enable bool)
EnableDateTime enables or disables the date and time stamp in the log
func (*TestLogger) Error ¶ added in v1.39.6
func (t *TestLogger) Error(message string)
Error logs an error message
func (*TestLogger) Info ¶ added in v1.39.6
func (t *TestLogger) Info(message string)
Info logs an info message
func (*TestLogger) SetPrefix ¶ added in v1.39.6
func (t *TestLogger) SetPrefix(prefix string)
SetPrefix sets the prefix for the logger
func (*TestLogger) ShortCustom ¶ added in v1.39.6
func (t *TestLogger) ShortCustom(message, color string)
ShortCustom logs a message with a custom color without caller information or level prefix
func (*TestLogger) ShortDebug ¶ added in v1.39.6
func (t *TestLogger) ShortDebug(message string)
ShortDebug logs a short debug message without caller information or DEBUG prefix
func (*TestLogger) ShortError ¶ added in v1.39.6
func (t *TestLogger) ShortError(message string)
ShortError logs a short error message without caller information or ERROR prefix but message is red
func (*TestLogger) ShortInfo ¶ added in v1.39.6
func (t *TestLogger) ShortInfo(message string)
ShortInfo logs a short info message without caller information or INFO prefix