testhelpers

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: Apache-2.0 Imports: 54 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSimpleInstance

func CreateSimpleInstance(k8sEnv K8sOperatorEnvironment, instanceName string, version string, edition string)

CreateSimpleInstance creates a basic v1alpha1.Instance object named 'instanceName'. 'version' and 'edition' should match rules of TestImageForVersion(). Depends on the Ginkgo asserts.

func CreateSimplePDB

func CreateSimplePDB(k8sEnv K8sOperatorEnvironment, instanceName string)

CreateSimplePDB creates a simple PDB 'pdb1' inside 'instanceName' Instance. Depends on the Ginkgo asserts.

func CreateSimplePdbWithDbObj

func CreateSimplePdbWithDbObj(k8sEnv K8sOperatorEnvironment, database *v1alpha1.Database)

CreateSimplePdbWithDbObj creates simple PDB by given database object.

func DeployOperator

func DeployOperator(ctx context.Context, k8sClient client.Client, namespace string) (func() error, error)

DeployOperator deploys an operator and returns a cleanup function to delete all cluster level objects created outside of the namespace.

func GCloudServiceAccount

func GCloudServiceAccount() string

GCloudServiceAccount returns the GCloud service account name.

func InsertSimpleData

func InsertSimpleData(k8sEnv K8sOperatorEnvironment)

InsertSimpleData creates 'test_table' in pdb1 and inserts a test row.

func IsCanaryJob

func IsCanaryJob() bool

Returns true if 'PROW_CANARY_JOB' env is set. Canary Job is supposed to host all long-running tests.

func K8sCreateAndGet

func K8sCreateAndGet(k8sClient client.Client, ctx context.Context, objKey client.ObjectKey, obj client.Object, createdObj client.Object)

K8sCreateAndGet calls k8s Create() with retry and then wait for the object to be created. Updates 'createdObj' with the created object.

func K8sCreateWithRetry

func K8sCreateWithRetry(k8sClient client.Client, ctx context.Context, obj client.Object)

K8sCreateWithRetry calls k8s Create() with retry as k8s might require this in some cases (e.g. conflicts).

func K8sDeleteWithRetry

func K8sDeleteWithRetry(k8sClient client.Client, ctx context.Context, obj client.Object)

K8sDeleteWithRetry calls k8s Delete() with retry as k8s might require this in some cases (e.g. conflicts).

func K8sExec

func K8sExec(pod string, ns string, container string, cmd string) (string, error)

K8sExec execs a command in a pod and returns a string result. Depends on the Ginkgo asserts. kubectl exec <pod> <cmd> -n <ns> -c <container>

func K8sExecuteSql

func K8sExecuteSql(pod string, ns string, sql string) (string, error)

K8sExecuteSql executes multiple sql statements in an Oracle pod e.g. sql := `alter session set container=pdb1; create table test_table (name varchar(100)); insert into test_table values ('Hello World'); commit;` out, err = testhelpers.K8sExecuteSql("mydb-sts-0", "db", sql) Depends on the Ginkgo asserts. Please escape any bash special characters.

func K8sExecuteSqlOrFail

func K8sExecuteSqlOrFail(pod, ns, sql string) string

K8sExecuteSqlOrFail is the same as K8sExecuteSql but raises a ginkgo assert on failure.

func K8sGetAndUpdateStatusWithRetry

func K8sGetAndUpdateStatusWithRetry(k8sClient client.Client,
	ctx context.Context,
	objKey client.ObjectKey,
	emptyObj client.Object,
	modifyObjectFunc func(*client.Object))

Simple helper to make the Get-Modify-UpdateStatus-Retry cycle easier Get a fresh version of the object into 'emptyObj' using 'objKey' Apply user-supplied modifyObjectFunc() which should modify the 'emptyObj' Try to update 'emptyObj' status in k8s, retry if needed

func K8sGetAndUpdateWithRetry

func K8sGetAndUpdateWithRetry(k8sClient client.Client,
	ctx context.Context,
	objKey client.ObjectKey,
	emptyObj client.Object,
	modifyObjectFunc func(*client.Object))

Simple helper to make the Get-Modify-Update-Retry cycle easier Get a fresh version of the object into 'emptyObj' using 'objKey' Apply user-supplied modifyObjectFunc() which should modify the 'emptyObj' Try to update 'emptyObj' in k8s, retry if needed

func K8sGetWithRetry

func K8sGetWithRetry(k8sClient client.Client, ctx context.Context, instKey client.ObjectKey, obj client.Object)

K8sGetWithRetry calls k8s Get() with retry as k8s might require this in some cases (e.g. conflicts).

func K8sVerifyUserConnectivity

func K8sVerifyUserConnectivity(pod, ns, pdb string, userCred map[string]string)

K8sVerifyUserConnectivity verified user connectivity on "oracledb" container. Or raise ginkgo assertion on failure. 5 retried in 30 second for each user is performed to workaround potential password sync latency between Config Server and Oracle DB.

func PrintClusterObjects

func PrintClusterObjects()

Print cluster objects - events, pods, pvcs for all namespaces in the cluster

func PrintENV

func PrintENV()

Print ENV variables

func PrintEvents

func PrintEvents()

PrintEvents for all namespaces in the cluster.

func PrintLogs

func PrintLogs(namespace string, env envtest.Environment, dumpLogsFor []string, instances []string)

Print logs from requested containers

func PrintPVCs

func PrintPVCs()

Print PVCs for all namespaces in the cluster

func PrintPods

func PrintPods()

Print pods for all namespaces in the cluster

func PrintSVCs

func PrintSVCs()

Print svcs for all namespaces in the cluster

func PrintSimpleDebugInfo

func PrintSimpleDebugInfo(k8sEnv K8sOperatorEnvironment, instanceName string, CDBName string)

Prints logs for a typical single-instance test scenario in case of failure: Prints logs for 'manager', 'dbdaemon', 'oracledb' containers. Prints cluster objects. Stores Oracle trace logs to a local dir (or Prow Artifacts).

func RandName

func RandName(base string) string

RandName generates a name suitable for use as a namespace with a given prefix.

func RunReconcilerTestSuite

func RunReconcilerTestSuite(t *testing.T, k8sClient *client.Client, k8sManager *ctrl.Manager, description string, controllers func() []Reconciler)

RunReconcilerTestSuite runs all specs in the current package against a specialized testing environment. Before running the suite, this function configures the test environment by taking the following actions:

  • Starting a control plane consisting of an etcd process and a Kubernetes API server process.
  • Installing CRDs into the control plane
  • Starting an in-process manager in a dedicated goroutine with the given reconcilers installed in it.

These components will be torn down after the suite runs.

func SetupServiceAccountBindingBetweenGcpAndK8s

func SetupServiceAccountBindingBetweenGcpAndK8s(k8sEnv K8sOperatorEnvironment)

SetupServiceAccountBindingBetweenGcpAndK8s creates IAM policy binding between k8s service account <projectId>.svc.id.goog[<NAMESPACE>/default] and google service account.

func StoreOracleLogs

func StoreOracleLogs(pod string, ns string, instanceName string, CDBName string) error

StoreOracleLogs saves Oracle's trace logs from oracledb pod. Stores to $ARTIFACTS in case of a Prow job or in a temporary directory if running locally.

func TestImageForVersion

func TestImageForVersion(version string, edition string, extra string) string

TestImageForVersion returns service image for integration tests. Image paths are predefined in the env variables TEST_IMAGE_ORACLE_*.

func VerifySimpleData

func VerifySimpleData(k8sEnv K8sOperatorEnvironment)

VerifySimpleData checks that the test row in 'pdb1' exists.

func WaitForInstanceConditionState

func WaitForInstanceConditionState(k8sEnv K8sOperatorEnvironment, key client.ObjectKey, condition string, targetStatus metav1.ConditionStatus, targetReason string, timeout time.Duration)

WaitForInstanceConditionState waits until the Instance condition object status = targetStatus and reason = targetReason. Depends on the Ginkgo asserts.

func WaitForObjectConditionState

func WaitForObjectConditionState(k8sEnv K8sOperatorEnvironment,
	key client.ObjectKey,
	emptyObj client.Object,
	condition string,
	targetStatus metav1.ConditionStatus,
	targetReason string,
	timeout time.Duration)

WaitForObjectConditionState waits until the k8s object condition object status = targetStatus and reason = targetReason. Objects supported: v1alpha1. {Instance, Import, Export} Depends on the Ginkgo asserts.

Types

type FakeClientFactory

type FakeClientFactory struct {
	Caclient *FakeConfigAgentClient
}

FakeClientFactory is a simple factory to create our FakeConfigAgentClient.

func (*FakeClientFactory) New

New returns a new fake ConfigAgent.

func (*FakeClientFactory) Reset

func (g *FakeClientFactory) Reset()

Reset clears the inner ConfigAgent.

type FakeConfigAgentClient

type FakeConfigAgentClient struct {
	// contains filtered or unexported fields
}

FakeConfigAgentClient a client for capturing calls the various ConfigAgent api.

func (*FakeConfigAgentClient) BootstrapDatabase

BootstrapDatabase wrapper.

func (*FakeConfigAgentClient) BootstrapDatabaseCalledCnt

func (cli *FakeConfigAgentClient) BootstrapDatabaseCalledCnt() int

BootstrapDatabaseCalledCnt returns call count.

func (*FakeConfigAgentClient) BootstrapStandby

BootstrapStandby wrapper.

func (*FakeConfigAgentClient) BounceDatabase

BounceDatabase wrapper.

func (*FakeConfigAgentClient) CheckStatus

CheckStatus wrapper.

func (*FakeConfigAgentClient) CreateCDB

CreateCDB wrapper.

func (*FakeConfigAgentClient) CreateCDBUser

CreateCDBUser wrapper.

func (*FakeConfigAgentClient) CreateDatabase

CreateDatabase wrapper.

func (*FakeConfigAgentClient) CreateListener

CreateListener wrapper.

func (*FakeConfigAgentClient) CreateUsers

CreateUsers wrapper.

func (*FakeConfigAgentClient) DataPumpExport

DataPumpExport wrapper.

func (*FakeConfigAgentClient) DataPumpExportCalledCnt

func (cli *FakeConfigAgentClient) DataPumpExportCalledCnt() int

DataPumpExportCalledCnt returns call count.

func (*FakeConfigAgentClient) DataPumpImport

DataPumpImport wrapper.

func (*FakeConfigAgentClient) DataPumpImportCalledCnt

func (cli *FakeConfigAgentClient) DataPumpImportCalledCnt() int

DataPumpImportCalledCnt returns call count.

func (*FakeConfigAgentClient) DeleteOperation

DeleteOperation wrapper.

func (*FakeConfigAgentClient) DeleteOperationCalledCnt

func (cli *FakeConfigAgentClient) DeleteOperationCalledCnt() int

DeleteOperationCalledCnt returns call count.

func (*FakeConfigAgentClient) FetchServiceImageMetaData

func (*FakeConfigAgentClient) GetOperation

GetOperation wrapper.

func (*FakeConfigAgentClient) GetOperationCalledCnt

func (cli *FakeConfigAgentClient) GetOperationCalledCnt() int

GetOperationCalledCnt returns call count.

func (*FakeConfigAgentClient) GetParameterTypeValue

GetParameterTypeValue wrapper.

func (*FakeConfigAgentClient) ListOperations

ListOperations wrapper.

func (*FakeConfigAgentClient) NextGetOperationStatus

func (cli *FakeConfigAgentClient) NextGetOperationStatus() FakeOperationStatus

func (*FakeConfigAgentClient) PhysicalBackup

PhysicalBackup wrapper.

func (*FakeConfigAgentClient) PhysicalBackupCalledCnt

func (cli *FakeConfigAgentClient) PhysicalBackupCalledCnt() int

PhysicalBackupCalledCnt returns call count.

func (*FakeConfigAgentClient) PhysicalRestore

PhysicalRestore wrapper.

func (*FakeConfigAgentClient) PhysicalRestoreCalledCnt

func (cli *FakeConfigAgentClient) PhysicalRestoreCalledCnt() int

PhysicalRestoreCalledCnt returns call count.

func (*FakeConfigAgentClient) RecoverConfigFile

RecoverConfigFile wrapper.

func (*FakeConfigAgentClient) Reset

func (cli *FakeConfigAgentClient) Reset()

Reset reset's the config agent's counters.

func (*FakeConfigAgentClient) SetAsyncBootstrapDatabase

func (cli *FakeConfigAgentClient) SetAsyncBootstrapDatabase(async bool)

func (*FakeConfigAgentClient) SetAsyncPhysicalBackup

func (cli *FakeConfigAgentClient) SetAsyncPhysicalBackup(async bool)

func (*FakeConfigAgentClient) SetAsyncPhysicalRestore

func (cli *FakeConfigAgentClient) SetAsyncPhysicalRestore(async bool)

func (*FakeConfigAgentClient) SetMethodToRespFunc

func (cli *FakeConfigAgentClient) SetMethodToRespFunc(m map[string]func(interface{}) (interface{}, error))

func (*FakeConfigAgentClient) SetNextGetOperationStatus

func (cli *FakeConfigAgentClient) SetNextGetOperationStatus(status FakeOperationStatus)

func (*FakeConfigAgentClient) SetParameter

SetParameter wrapper.

func (*FakeConfigAgentClient) UpdateUsers

UpdateUsers wrapper.

func (*FakeConfigAgentClient) UsersChanged

UsersChanged wrapper.

func (*FakeConfigAgentClient) VerifyPhysicalBackup

VerifyPhysicalBackup wrapper.

func (*FakeConfigAgentClient) VerifyPhysicalBackupCalledCnt

func (cli *FakeConfigAgentClient) VerifyPhysicalBackupCalledCnt() int

VerifyPhysicalBackupCalledCnt returns call count.

type FakeOperationStatus

type FakeOperationStatus int32

FakeOperationStatus is an enum type for LRO statuses managed by FakeConfigAgentClient.

const (
	//StatusUndefined undefined.
	StatusUndefined FakeOperationStatus = iota
	//StatusRunning running.
	StatusRunning
	//StatusDone done.
	StatusDone
	//StatusDoneWithError done with error.
	StatusDoneWithError
	//StatusNotFound not found.
	StatusNotFound
)

type K8sOperatorEnvironment

type K8sOperatorEnvironment struct {
	Env               envtest.Environment
	Namespace         string
	Ctx               context.Context
	K8sClient         client.Client
	OperCleanup       func() error // Operator deployment cleanup callback.
	TestFailed        bool         // If true then dump container logs.
	K8sServiceAccount string
}

K8sOperatorEnvironment is a helper for integration testing.

Encapsulates all necessary variables to work with the test cluster Can be created/destroyed multiple times within one test suite Depends on the Ginkgo asserts Example usage:

// Global variable, to be accessible by AfterSuite. var k8sEnv = testhelpers.K8sEnvironment{} // In case of Ctrl-C, clean up the last valid k8sEnv.

AfterSuite(func() {
	k8sEnv.Close()
})

...

BeforeEach(func() {
	k8sEnv.Init(testhelpers.RandName("k8s-env-stress-test"))
})
AfterEach(func() {
	k8sEnv.Close()
})

func (*K8sOperatorEnvironment) Close

func (k8sEnv *K8sOperatorEnvironment) Close()

Close cleans cluster objects and uninstalls operator.

func (*K8sOperatorEnvironment) Init

func (k8sEnv *K8sOperatorEnvironment) Init(namespace string)

Init the environment, install CRDs, deploy operator, create 'namespace'.

type Reconciler

type Reconciler interface {
	SetupWithManager(manager ctrl.Manager) error
}

Reconciler is the interface to setup a reconciler for testing.

Jump to

Keyboard shortcuts

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