Documentation ¶
Overview ¶
Package testhelpers holds reusable functions that make it easier to write testintegration and end-to-end tests.
Index ¶
- Constants
- func AddTCPInstance(p *cloudsqlapi.AuthProxyWorkload, connectionString string)
- func AddUnixInstance(p *cloudsqlapi.AuthProxyWorkload, connectionString string, path string)
- func BuildAuthProxyWorkload(key types.NamespacedName, connectionString string) *cloudsqlapi.AuthProxyWorkload
- func BuildCronJob(name types.NamespacedName, appLabel string) *batchv1.CronJob
- func BuildDaemonSet(name types.NamespacedName, appLabel string) *appsv1.DaemonSet
- func BuildDeployment(name types.NamespacedName, appLabel string) *appsv1.Deployment
- func BuildDeploymentReplicaSet(d *appsv1.Deployment, scheme *runtime.Scheme) (*appsv1.ReplicaSet, string, error)
- func BuildDeploymentReplicaSetPods(d *appsv1.Deployment, rs *appsv1.ReplicaSet, podTemplateHash string, ...) ([]*corev1.Pod, error)
- func BuildJob(name types.NamespacedName, appLabel string) *batchv1.Job
- func BuildMSSQLPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
- func BuildMySQLPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
- func BuildMySQLUnixPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
- func BuildPgPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
- func BuildPgUnixPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
- func BuildSecret(secretName, user, password, dbName string) corev1.Secret
- func BuildStatefulSet(name types.NamespacedName, appLabel string) *appsv1.StatefulSet
- func GetConditionStatus(conditions []*metav1.Condition, condType string) metav1.ConditionStatus
- func ListPods(ctx context.Context, c client.Client, ns string, ...) (*corev1.PodList, error)
- func NewAuthProxyWorkload(key types.NamespacedName) *cloudsqlapi.AuthProxyWorkload
- func NewNamespaceName(prefix string) string
- func RetryUntilSuccess(attempts int, d time.Duration, f func() error) (err error)
- type TestCaseClient
- func (cc *TestCaseClient) ConfigureResources(proxy *cloudsqlapi.AuthProxyWorkload)
- func (cc *TestCaseClient) ConfigureSelector(proxy *cloudsqlapi.AuthProxyWorkload, appLabel string, kind string)
- func (cc *TestCaseClient) Create(ctx context.Context, proxy *cloudsqlapi.AuthProxyWorkload) error
- func (cc *TestCaseClient) CreateAuthProxyWorkload(ctx context.Context, key types.NamespacedName, appLabel string, ...) (*cloudsqlapi.AuthProxyWorkload, error)
- func (cc *TestCaseClient) CreateBusyboxDeployment(ctx context.Context, name types.NamespacedName, appLabel string) (*appsv1.Deployment, error)
- func (cc *TestCaseClient) CreateDeploymentReplicaSetAndPods(ctx context.Context, d *appsv1.Deployment) (*appsv1.ReplicaSet, []*corev1.Pod, error)
- func (cc *TestCaseClient) CreateOrPatchNamespace(ctx context.Context) error
- func (cc *TestCaseClient) CreateResource(ctx context.Context) (*cloudsqlapi.AuthProxyWorkload, error)
- func (cc *TestCaseClient) CreateWorkload(ctx context.Context, o client.Object) error
- func (cc *TestCaseClient) DeleteNamespace(ctx context.Context) error
- func (cc *TestCaseClient) DeleteResourceAndWait(ctx context.Context, res *cloudsqlapi.AuthProxyWorkload) error
- func (cc *TestCaseClient) ExpectContainerCount(ctx context.Context, key types.NamespacedName, count int) error
- func (cc *TestCaseClient) ExpectPodContainerCount(ctx context.Context, podSelector *metav1.LabelSelector, count int, ...) error
- func (cc *TestCaseClient) ExpectPodReady(ctx context.Context, podSelector *metav1.LabelSelector, allOrAny string) error
- func (cc *TestCaseClient) GetAuthProxyWorkloadAfterReconcile(ctx context.Context, key types.NamespacedName) (*cloudsqlapi.AuthProxyWorkload, error)
- func (cc *TestCaseClient) WaitForFinalizerOnResource(ctx context.Context, res *cloudsqlapi.AuthProxyWorkload) error
Constants ¶
const DefaultRetryInterval = 5 * time.Second
Variables ¶
This section is empty.
Functions ¶
func AddTCPInstance ¶ added in v0.3.0
func AddTCPInstance(p *cloudsqlapi.AuthProxyWorkload, connectionString string)
AddTCPInstance adds a database instance with a tcp connection, setting HostEnvName to "DB_HOST" and PortEnvName to "DB_PORT".
func AddUnixInstance ¶ added in v0.3.0
func AddUnixInstance(p *cloudsqlapi.AuthProxyWorkload, connectionString string, path string)
AddUnixInstance adds a database instance with a unix socket connection, setting UnixSocketPathEnvName to "DB_PATH".
func BuildAuthProxyWorkload ¶
func BuildAuthProxyWorkload(key types.NamespacedName, connectionString string) *cloudsqlapi.AuthProxyWorkload
BuildAuthProxyWorkload creates an AuthProxyWorkload object with a single instance with a tcp connection.
func BuildCronJob ¶
func BuildCronJob(name types.NamespacedName, appLabel string) *batchv1.CronJob
BuildCronJob creates CronJob object with a default pod template that will sleep for 60 seconds.
func BuildDaemonSet ¶
func BuildDaemonSet(name types.NamespacedName, appLabel string) *appsv1.DaemonSet
BuildDaemonSet creates a DaemonSet object with a default pod template that will sleep for 1 hour.
func BuildDeployment ¶
func BuildDeployment(name types.NamespacedName, appLabel string) *appsv1.Deployment
BuildDeployment creates a StatefulSet object with a default pod template that will sleep for 1 hour.
func BuildDeploymentReplicaSet ¶ added in v0.4.0
func BuildDeploymentReplicaSet(d *appsv1.Deployment, scheme *runtime.Scheme) (*appsv1.ReplicaSet, string, error)
BuildDeploymentReplicaSet mimics the behavior of the deployment controller built into kubernetes. It builds one ReplicaSet and DeploymentSpec.Replicas pods with the correct labels and ownership annotations as if it were in a live cluster.
func BuildDeploymentReplicaSetPods ¶ added in v0.4.0
func BuildDeploymentReplicaSetPods(d *appsv1.Deployment, rs *appsv1.ReplicaSet, podTemplateHash string, scheme *runtime.Scheme) ([]*corev1.Pod, error)
func BuildJob ¶
func BuildJob(name types.NamespacedName, appLabel string) *batchv1.Job
BuildJob creates a Job object with a default pod template that will sleep for 30 seconds.
func BuildMSSQLPodSpec ¶ added in v0.3.0
func BuildMSSQLPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
BuildMSSQLPodSpec creates a podspec specific to MySQL databases that will connect and run a trivial query. It also configures the pod's Liveness probe so that the pod's `Ready` condition is `Ready` when the database can connect.
func BuildMySQLPodSpec ¶ added in v0.3.0
func BuildMySQLPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
BuildMySQLPodSpec creates a podspec specific to MySQL databases that will connect and run a trivial query. It also configures the pod's Liveness probe so that the pod's `Ready` condition is `Ready` when the database can connect.
func BuildMySQLUnixPodSpec ¶ added in v0.3.0
func BuildMySQLUnixPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
BuildMySQLUnixPodSpec creates a podspec specific to MySQL databases that will connect via a unix socket and run a trivial query. It also configures the pod's Liveness probe so that the pod's `Ready` condition is `Ready` when the database can connect.
func BuildPgPodSpec ¶ added in v0.3.0
func BuildPgPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
BuildPgPodSpec creates a podspec specific to Postgres databases that will connect and run a trivial query. It also configures the pod's Liveness probe so that the pod's `Ready` condition is `Ready` when the database can connect.
func BuildPgUnixPodSpec ¶ added in v0.3.0
func BuildPgUnixPodSpec(mainPodSleep int, appLabel, secretName string) corev1.PodTemplateSpec
BuildPgUnixPodSpec creates a podspec specific to Postgres databases that will connect via a unix socket and run a trivial. It also configures the pod's Liveness probe so that the pod's `Ready` condition is `Ready` when the database can connect.
func BuildSecret ¶ added in v0.3.0
BuildSecret creates a Secret object containing database information to be used by the pod to connect to the database.
func BuildStatefulSet ¶
func BuildStatefulSet(name types.NamespacedName, appLabel string) *appsv1.StatefulSet
BuildStatefulSet creates a StatefulSet object with a default pod template that will sleep for 1 hour.
func GetConditionStatus ¶
func GetConditionStatus(conditions []*metav1.Condition, condType string) metav1.ConditionStatus
GetConditionStatus finds a condition where Condition.Type == condType and returns the status, or "" if no condition was found.
func ListPods ¶
func ListPods(ctx context.Context, c client.Client, ns string, selector *metav1.LabelSelector) (*corev1.PodList, error)
ListPods lists all the pods in a particular deployment.
func NewAuthProxyWorkload ¶ added in v0.3.0
func NewAuthProxyWorkload(key types.NamespacedName) *cloudsqlapi.AuthProxyWorkload
NewAuthProxyWorkload creates a new AuthProxyWorkload with the TypeMeta, name and namespace set.
func NewNamespaceName ¶
func RetryUntilSuccess ¶
RetryUntilSuccess runs `f` until it no longer returns an error, or it has returned an error `attempts` number of times. It sleeps duration `d` between failed attempts. It returns the error from the last attempt.
Types ¶
type TestCaseClient ¶
type TestCaseClient struct { Client client.Client Namespace string ConnectionString string ProxyImageURL string DBRootUsername string DBRootPassword string DBName string }
func (*TestCaseClient) ConfigureResources ¶ added in v0.3.0
func (cc *TestCaseClient) ConfigureResources(proxy *cloudsqlapi.AuthProxyWorkload)
ConfigureResources Configures resource requests
func (*TestCaseClient) ConfigureSelector ¶ added in v0.3.0
func (cc *TestCaseClient) ConfigureSelector(proxy *cloudsqlapi.AuthProxyWorkload, appLabel string, kind string)
ConfigureSelector Configures the workload selector on AuthProxyWorkload to use the label selector "app=${appLabel}"
func (*TestCaseClient) Create ¶ added in v0.3.0
func (cc *TestCaseClient) Create(ctx context.Context, proxy *cloudsqlapi.AuthProxyWorkload) error
func (*TestCaseClient) CreateAuthProxyWorkload ¶
func (cc *TestCaseClient) CreateAuthProxyWorkload(ctx context.Context, key types.NamespacedName, appLabel string, connectionString string, kind string) (*cloudsqlapi.AuthProxyWorkload, error)
CreateAuthProxyWorkload creates an AuthProxyWorkload in the kubernetes cluster.
func (*TestCaseClient) CreateBusyboxDeployment ¶
func (cc *TestCaseClient) CreateBusyboxDeployment(ctx context.Context, name types.NamespacedName, appLabel string) (*appsv1.Deployment, error)
CreateBusyboxDeployment creates a simple busybox deployment, using the key as its namespace and name. It also sets the label "app"= appLabel.
func (*TestCaseClient) CreateDeploymentReplicaSetAndPods ¶
func (cc *TestCaseClient) CreateDeploymentReplicaSetAndPods(ctx context.Context, d *appsv1.Deployment) (*appsv1.ReplicaSet, []*corev1.Pod, error)
CreateDeploymentReplicaSetAndPods mimics the behavior of the deployment controller built into kubernetes. It creates one ReplicaSet and DeploymentSpec.Replicas pods with the correct labels and ownership annotations as if it were in a live cluster. This will make it easier to test and debug the behavior of our pod injection webhooks.
func (*TestCaseClient) CreateOrPatchNamespace ¶
func (cc *TestCaseClient) CreateOrPatchNamespace(ctx context.Context) error
CreateOrPatchNamespace ensures that a namespace exists with the given name in kubernetes, or fails the test as fatal.
func (*TestCaseClient) CreateResource ¶
func (cc *TestCaseClient) CreateResource(ctx context.Context) (*cloudsqlapi.AuthProxyWorkload, error)
CreateResource creates a new workload resource in the TestCaseClient's namespace waits until the resource exists.
func (*TestCaseClient) CreateWorkload ¶
CreateWorkload Creates the workload in Kubernetes, waiting to confirm that the workload exists.
func (*TestCaseClient) DeleteNamespace ¶
func (cc *TestCaseClient) DeleteNamespace(ctx context.Context) error
func (*TestCaseClient) DeleteResourceAndWait ¶
func (cc *TestCaseClient) DeleteResourceAndWait(ctx context.Context, res *cloudsqlapi.AuthProxyWorkload) error
DeleteResourceAndWait issues a delete request for the resource and then waits for the resource to actually be deleted. This will return an error if the resource is not deleted within 15 seconds.
func (*TestCaseClient) ExpectContainerCount ¶
func (cc *TestCaseClient) ExpectContainerCount(ctx context.Context, key types.NamespacedName, count int) error
ExpectContainerCount finds a deployment and keeps checking until the number of containers on the deployment's PodSpec.Containers == count. Returns error after 30 seconds if the containers do not match.
func (*TestCaseClient) ExpectPodContainerCount ¶
func (cc *TestCaseClient) ExpectPodContainerCount(ctx context.Context, podSelector *metav1.LabelSelector, count int, allOrAny string) error
ExpectPodContainerCount finds a deployment and keeps checking until the number of containers on the deployment's PodSpec.Containers == count. Returns error after 30 seconds if the containers do not match.
func (*TestCaseClient) ExpectPodReady ¶ added in v0.3.0
func (cc *TestCaseClient) ExpectPodReady(ctx context.Context, podSelector *metav1.LabelSelector, allOrAny string) error
ExpectPodReady finds a deployment and keeps checking until the number of containers on the deployment's PodSpec.Containers == count. Returns error after 30 seconds if the containers do not match.
func (*TestCaseClient) GetAuthProxyWorkloadAfterReconcile ¶
func (cc *TestCaseClient) GetAuthProxyWorkloadAfterReconcile(ctx context.Context, key types.NamespacedName) (*cloudsqlapi.AuthProxyWorkload, error)
GetAuthProxyWorkloadAfterReconcile finds an AuthProxyWorkload resource named key, waits for its "UpToDate" condition to be "True", and the returns it. Fails after 30 seconds if the containers does not match.
func (*TestCaseClient) WaitForFinalizerOnResource ¶
func (cc *TestCaseClient) WaitForFinalizerOnResource(ctx context.Context, res *cloudsqlapi.AuthProxyWorkload) error
WaitForFinalizerOnResource queries the client to see if the resource has a finalizer.