testschematic

package
v1.4.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const DefaultSchematicsApiURL = "https://schematics.cloud.ibm.com"
View Source
const DefaultWaitJobCompleteMinutes = int16(120) // default 2 hrs wait time
View Source
const SchematicsJobStatusCompleted = "COMPLETED"

IBM schematics job status

View Source
const SchematicsJobStatusCreated = "CREATED"
View Source
const SchematicsJobStatusFailed = "FAILED"
View Source
const SchematicsJobStatusInProgress = "INPROGRESS"
View Source
const SchematicsJobTypeApply = "APPLY"
View Source
const SchematicsJobTypeDestroy = "DESTROY"
View Source
const SchematicsJobTypePlan = "PLAN"
View Source
const SchematicsJobTypeUpload = "TAR_WORKSPACE_UPLOAD"

IBM schematics job types

Variables

This section is empty.

Functions

func CreateSchematicTar

func CreateSchematicTar(projectPath string, includePatterns *[]string) (string, error)

CreateSchematicTar will accept a path to a Terraform project and an array of file patterns to include, and will create a TAR file in a temporary location that contains all of the project's files that match the supplied file patterns. This TAR file can then be uploaded to a Schematics Workspace template. Returns a string of the complete TAR file path and file name. Error is returned if any issues happen while creating TAR file.

Types

type IamAuthenticatorSvcI

type IamAuthenticatorSvcI interface {
	Authenticate(*http.Request) error
	AuthenticationType() string
	RequestToken() (*core.IamTokenServerResponse, error)
	Validate() error
}

interface for external IBMCloud IAM Authenticator api. Can be mocked for tests

type NetrcCredential

type NetrcCredential struct {
	Host     string // hostname or machine name of the entry
	Username string // user name
	Password string // password or token
}

type SchematicsTestService

type SchematicsTestService struct {
	SchematicsApiSvc          SchematicsApiSvcI     // the main schematics service interface
	ApiAuthenticator          IamAuthenticatorSvcI  // the authenticator used for schematics api calls
	WorkspaceID               string                // workspace ID used for tests
	TemplateID                string                // workspace template ID used for tests
	TestOptions               *TestSchematicOptions // additional testing options
	TerraformTestStarted      bool                  // keeps track of when actual Terraform resource testing has begin, used for proper test teardown logic
	TerraformResourcesCreated bool                  // keeps track of when we start deploying resources, used for proper test teardown logic
}

main data struct for all schematic test methods

func (*SchematicsTestService) CreateApplyJob

CreateApplyJob will initiate a new APPLY action on an existing terraform Schematics Workspace. Will return a result object containing details about the new action.

func (*SchematicsTestService) CreateAuthenticator

func (svc *SchematicsTestService) CreateAuthenticator(ibmcloudApiKey string)

CreateAuthenticator will accept a valid IBM cloud API key, and set a valid Authenticator object that will be used in the external provider service for schematics.

func (*SchematicsTestService) CreateDestroyJob

CreateDestroyJob will initiate a new DESTROY action on an existing terraform Schematics Workspace. Will return a result object containing details about the new action.

func (*SchematicsTestService) CreatePlanJob

CreatePlanJob will initiate a new PLAN action on an existing terraform Schematics Workspace. Will return a result object containing details about the new action.

func (*SchematicsTestService) CreateTestWorkspace

func (svc *SchematicsTestService) CreateTestWorkspace(name string, resourceGroup string, templateFolder string, terraformVersion string, tags []string) (*schematics.WorkspaceResponse, error)

CreateTestWorkspace will create a new IBM Schematics Workspace that will be used for testing.

func (*SchematicsTestService) DeleteWorkspace

func (svc *SchematicsTestService) DeleteWorkspace() (string, error)

DeleteWorkspace will delete the existing workspace created for the test service.

func (*SchematicsTestService) FindLatestWorkspaceJobByName

func (svc *SchematicsTestService) FindLatestWorkspaceJobByName(jobName string) (*schematics.WorkspaceActivity, error)

FindLatestWorkspaceJobByName will find the latest executed job of the type supplied and return data about that job. This can be used to find a job by its type when the jobID is not known. A "NotFound" error will be thrown if there are no existing jobs of the provided type.

func (*SchematicsTestService) GetRefreshToken

func (svc *SchematicsTestService) GetRefreshToken() (string, error)

GetRefreshToken will use a previously established Authenticator to create a new IAM Token object, if existing is not valid, and return the refresh token propery from the token object.

func (*SchematicsTestService) GetWorkspaceJobDetail

func (svc *SchematicsTestService) GetWorkspaceJobDetail(jobID string) (*schematics.WorkspaceActivity, error)

GetWorkspaceJobDetail will return a data structure with full details about an existing Schematics Workspace activity for the given Job ID.

func (*SchematicsTestService) InitializeSchematicsService

func (svc *SchematicsTestService) InitializeSchematicsService() error

InitializeSchematicsService will initialize the external service object for schematicsv1 and assign it to a property of the receiver for later use.

func (*SchematicsTestService) UpdateTestTemplateVars

func (svc *SchematicsTestService) UpdateTestTemplateVars(vars []TestSchematicTerraformVar) error

UpdateTestTemplateVars will update an existing Schematics Workspace terraform template with a Variablestore, which will set terraform input variables for test runs.

func (*SchematicsTestService) UploadTarToWorkspace

func (svc *SchematicsTestService) UploadTarToWorkspace(tarPath string) error

UploadTarToWorkspace will accept a file path for an existing TAR file, containing files for a Terraform test case, and upload it to an existing Schematics Workspace.

func (*SchematicsTestService) WaitForFinalJobStatus

func (svc *SchematicsTestService) WaitForFinalJobStatus(jobID string) (string, error)

WaitForFinalJobStatus will look up details about the given activity and check the status value. If the status implies that the activity has not completed yet, this function will keep checking the status value until either the activity has finished, or a configured time threshold has been reached. Returns the final status value of the activity when it has finished. Returns an error if the activity does not finish before the configured time threshold.

type TestSchematicOptions

type TestSchematicOptions struct {
	Testing                 *testing.T `copier:"-"` // Testing The current test object
	TarIncludePatterns      []string
	BestRegionYAMLPath      string                         // BestRegionYAMLPath Path to the yaml containing regions and weights
	DefaultRegion           string                         // DefaultRegion default region if automatic detection fails
	ResourceGroup           string                         // ResourceGroup IBM Cloud resource group to use
	Region                  string                         // Region to use
	RequiredEnvironmentVars map[string]string              // RequiredEnvironmentVars
	TerraformVars           []TestSchematicTerraformVar    // TerraformVars variables to pass to terraform
	TemplateFolder          string                         // folder that contains terraform template, defaults to "."
	Tags                    []string                       // Tags optional tags to add
	Prefix                  string                         // Prefix to use when creating resources
	WaitJobCompleteMinutes  int16                          // number of minutes to wait for schematic job completions
	SchematicsApiURL        string                         // OPTIONAL: base URL for schematics API
	DeleteWorkspaceOnFail   bool                           // if there is a failure, should test delete the workspace and logs, default of false
	TerraformVersion        string                         // OPTIONAL: Schematics terraform version to use for template. If not supplied will be determined from required version in project
	NetrcSettings           []NetrcCredential              // array of .netrc credentials that will be set for schematics workspace
	WorkspaceEnvVars        []WorkspaceEnvironmentVariable // array of ENV variables to set inside workspace
	CloudInfoService        cloudinfo.CloudInfoServiceI    // OPTIONAL: Supply if you need multiple tests to share info service and data
	SchematicsApiSvc        SchematicsApiSvcI              // OPTIONAL: service pointer for interacting with external schematics api
	// contains filtered or unexported fields
}

TestSchematicOptions is the main data struct containing all options related to running a Terraform unit test wihtin IBM Schematics Workspaces

func TestSchematicOptionsDefault

func TestSchematicOptionsDefault(originalOptions *TestSchematicOptions) *TestSchematicOptions

TestSchematicOptionsDefault is a constructor for struct TestSchematicOptions. This function will accept an existing instance of TestSchematicOptions values, and return a new instance of TestSchematicOptions with the original values set along with appropriate default values for any properties that were not set in the original options.

func (*TestSchematicOptions) AddNetrcCredential

func (options *TestSchematicOptions) AddNetrcCredential(hostname string, username string, password string)

AddNetrcCredential is a helper function for TestSchematicOptions that will append a new netrc credential struct to the appropriate array in the options struct

func (*TestSchematicOptions) AddNetrcCredentialByEnv

func (options *TestSchematicOptions) AddNetrcCredentialByEnv(hostname string, usernameEnvKey string, passwordEnvKey string) error

AddNetrcCredential is a helper function for TestSchematicOptions that will append a new netrc credential struct to the appropriate options array, by retrieving the username and password by lookup up supplied environment variable keys. error returned if any keys are missing in local environment

func (*TestSchematicOptions) AddNetrcCredentialByEnvDefault

func (options *TestSchematicOptions) AddNetrcCredentialByEnvDefault(hostname string) error

AddNetrcCredentialByEnvDefault is a helper function for TestSchematicOptions that will append a new netrc credential struct to the appropriate options array, by retrieving the username and password by looking up the default environment variable keys error returned if any keys are missing in local environment

func (*TestSchematicOptions) AddWorkspaceEnvVar

func (options *TestSchematicOptions) AddWorkspaceEnvVar(key string, value string, hidden bool, secure bool)

AddWorkspaceEnvVar is a helper function for TestSchematicOptions that will append a new ENV entry to options that will be added to the workspace during the test

func (*TestSchematicOptions) AddWorkspaceEnvVarFromLocalEnv

func (options *TestSchematicOptions) AddWorkspaceEnvVarFromLocalEnv(key string, hidden bool, secure bool) error

AddWorkspaceEnvVarFromLocalEnv is a helper function for TestSchematicOptions that will append a new ENV entry to options that will be added to the workspace during the test. The value for the environment variable will be queried from the local OS under the same env var key. error returned if the key is not set in local environment.

func (*TestSchematicOptions) Clone

func (options *TestSchematicOptions) Clone() (*TestSchematicOptions, error)

Clone makes a deep copy of most fields on the Options object and returns it.

NOTE: options.SshAgent and options.Logger CANNOT be deep copied (e.g., the SshAgent struct contains channels and listeners that can't be meaningfully copied), so the original values are retained.

func (*TestSchematicOptions) RunSchematicTest

func (options *TestSchematicOptions) RunSchematicTest() error

RunSchematicTest will use the supplied options to run an end-to-end Terraform test of a project in an IBM Cloud Schematics Workspace. This test will include the following steps: 1. create test workspace 2. create and upload tar file of terraform project to workspace 3. configure supplied test variables in workspace 4. run PLAN/APPLY/DESTROY steps on workspace to provision and destroy resources 5. delete the test workspace

type TestSchematicTerraformVar

type TestSchematicTerraformVar struct {
	Name     string      // name of variable
	Value    interface{} // value of variable
	DataType string      // the TERRAFORM DATA TYPE of the varialbe (not golang type)
	Secure   bool        // true if value should be hidden
}

type WorkspaceEnvironmentVariable

type WorkspaceEnvironmentVariable struct {
	Key    string // key name to set in workspace
	Value  string // value of env var
	Hidden bool   // metadata to hide this env var
	Secure bool   // metadata to mark value as sensitive
}

Jump to

Keyboard shortcuts

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