testhelpers

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeBaseVersion added in v1.0.0

func MakeBaseVersion(namespace, twdName, imageName string, status temporaliov1alpha1.VersionStatus, createDeployment, hasDeployment bool) temporaliov1alpha1.BaseWorkerDeploymentVersion

func MakeBuildId

func MakeBuildId(twdName, imageName string, podSpec *corev1.PodTemplateSpec) string

MakeBuildId computes a build id based on the image and If no podSpec is provided, defaults to HelloWorldPodSpec with the given image name. If you provide your own podSpec, make sure to give the first container your desired image name if success is expected.

func MakeCurrentVersion

func MakeCurrentVersion(namespace, twdName, imageName string, healthy, createDeployment bool) *temporaliov1alpha1.CurrentWorkerDeploymentVersion

func MakeDeprecatedVersion added in v1.0.0

func MakeDeprecatedVersion(namespace, twdName, imageName string, status temporaliov1alpha1.VersionStatus, healthy, createDeployment, hasDeployment bool) *temporaliov1alpha1.DeprecatedWorkerDeploymentVersion

func MakePodSpec

func MakePodSpec(containers []corev1.Container, labels map[string]string, taskQueue string) corev1.PodTemplateSpec

MakePodSpec creates a pod spec with the given containers, labels, and task queue

func MakePodSpecWithImage added in v1.0.0

func MakePodSpecWithImage(imageName string) corev1.PodTemplateSpec

MakePodSpecWithImage creates a pod spec with an empty task queue and one container with the given image name.

func MakeTWDWithImage

func MakeTWDWithImage(name, namespace, imageName string) *temporaliov1alpha1.TemporalWorkerDeployment

func MakeTWDWithName

func MakeTWDWithName(name, namespace string) *temporaliov1alpha1.TemporalWorkerDeployment

func MakeTargetVersion

func MakeTargetVersion(namespace, twdName, imageName string, status temporaliov1alpha1.VersionStatus, rampPercentage int32, healthy, createDeployment bool) temporaliov1alpha1.TargetWorkerDeploymentVersion

func ModifyObj

func ModifyObj[T any](obj T, callback func(obj T) T) T

func NewWorker added in v1.0.0

func NewWorker(
	ctx context.Context,
	temporalDeploymentName, workerBuildId, temporalTaskQueue, temporalHostPort, temporalNamespace string,
	versioned bool,
) (w worker.Worker, stopFunc func(), err error)

func ProgressiveStep

func ProgressiveStep(rampPercentage int32, pauseDuration time.Duration) temporaliov1alpha1.RolloutStep

ProgressiveStep creates a progressive rollout step

func RunHelloWorldWorker

func RunHelloWorldWorker(ctx context.Context, podTemplateSpec corev1.PodTemplateSpec, callback func(stopFunc func(), err error))

RunHelloWorldWorker runs one worker per replica in the pod spec. callback is a function that can be called multiple times.

func SetTaskQueue added in v1.0.0

func SetTaskQueue(podSpec corev1.PodTemplateSpec, taskQueue string) corev1.PodTemplateSpec

SetTaskQueue sets or replaces the env var taskQueueEnvKey with the given string in all containers

Types

type DeploymentInfo added in v1.0.0

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

DeploymentInfo defines the necessary information about a Deployment, so that tests can recreate and validate state that is not visible in the TemporalWorkerDeployment status

func NewDeploymentInfo added in v1.0.0

func NewDeploymentInfo(imageName string, replicas int32) DeploymentInfo

type DeprecatedVersionInfo

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

func NewDeprecatedVersionInfo

func NewDeprecatedVersionInfo(imageName string, status temporaliov1alpha1.VersionStatus, healthy, createDeployment, hasDeployment bool) DeprecatedVersionInfo

type StatusBuilder

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

StatusBuilder provides a fluent interface for building expected status objects Versions will be built based on the TWD name and k8s namespace

func NewStatusBuilder

func NewStatusBuilder() *StatusBuilder

NewStatusBuilder creates a new status builder

func (*StatusBuilder) Build

Build returns the constructed status

func (*StatusBuilder) WithCurrentVersion

func (sb *StatusBuilder) WithCurrentVersion(imageName string, healthy, createDeployment bool) *StatusBuilder

WithCurrentVersion sets the current version in the status

func (*StatusBuilder) WithDeprecatedVersions added in v1.0.0

func (sb *StatusBuilder) WithDeprecatedVersions(infos ...DeprecatedVersionInfo) *StatusBuilder

WithDeprecatedVersions adds deprecated versions to the status. Note: The image name and replica count are not stored in the status. If you need to know those in your test case, use TestCaseBuilder.WithDeprecatedVersions instead, which will add your deprecated versions to the status and also save the image names and replica counts that you need in the Test Case info.

func (*StatusBuilder) WithName

func (sb *StatusBuilder) WithName(name string) *StatusBuilder

WithName sets the name

func (*StatusBuilder) WithNamespace

func (sb *StatusBuilder) WithNamespace(k8sNamespace string) *StatusBuilder

WithNamespace sets the namespace

func (*StatusBuilder) WithTargetVersion

func (sb *StatusBuilder) WithTargetVersion(imageName string, status temporaliov1alpha1.VersionStatus, rampPercentage int32, healthy bool, createDeployment bool) *StatusBuilder

WithTargetVersion sets the target version in the status. Set createDeployment to true if the test runner should create the Deployment, or false if you expect the controller to create it.. Target Version is required.

type TemporalWorkerDeploymentBuilder

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

TemporalWorkerDeploymentBuilder provides a fluent interface for building test TWD objects

func NewTemporalWorkerDeploymentBuilder

func NewTemporalWorkerDeploymentBuilder() *TemporalWorkerDeploymentBuilder

NewTemporalWorkerDeploymentBuilder creates a new builder with sensible defaults

func (*TemporalWorkerDeploymentBuilder) Build

Build returns the constructed TemporalWorkerDeployment

func (*TemporalWorkerDeploymentBuilder) WithAllAtOnceStrategy

WithAllAtOnceStrategy sets the rollout strategy to all-at-once

func (*TemporalWorkerDeploymentBuilder) WithGate

WithGate sets the rollout strategy have a gate workflow

func (*TemporalWorkerDeploymentBuilder) WithLabels

WithLabels sets the labels

func (*TemporalWorkerDeploymentBuilder) WithManualStrategy added in v1.0.0

WithManualStrategy sets the rollout strategy to manual

func (*TemporalWorkerDeploymentBuilder) WithName

WithName sets the name

func (*TemporalWorkerDeploymentBuilder) WithNamespace

WithNamespace sets the namespace

func (*TemporalWorkerDeploymentBuilder) WithProgressiveStrategy

WithProgressiveStrategy sets the rollout strategy to progressive with given steps

func (*TemporalWorkerDeploymentBuilder) WithReplicas

WithReplicas sets the number of replicas

func (*TemporalWorkerDeploymentBuilder) WithStatus added in v1.0.0

func (*TemporalWorkerDeploymentBuilder) WithTargetTemplate added in v1.0.0

WithTargetTemplate sets the template of the worker deployment to a pod spec with the given image name, thus defining the target version.

func (*TemporalWorkerDeploymentBuilder) WithTemporalConnection

func (b *TemporalWorkerDeploymentBuilder) WithTemporalConnection(connectionName string) *TemporalWorkerDeploymentBuilder

WithTemporalConnection sets the temporal connection name

func (*TemporalWorkerDeploymentBuilder) WithTemporalNamespace

func (b *TemporalWorkerDeploymentBuilder) WithTemporalNamespace(temporalNamespace string) *TemporalWorkerDeploymentBuilder

WithTemporalNamespace sets the temporal namespace

type TestCase

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

TestCase represents a single test scenario for integration testing of TemporalWorkerDeployment controllers. It encapsulates the input state, expected outputs, and any additional setup required for the test.

Example

ExampleTestCase demonstrates how to create and use a TestCase for integration testing. This example shows the basic structure and usage patterns for TestCase.

package main

import (
	"context"
	"fmt"
	"testing"
	"time"

	temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1"
	"github.com/temporalio/temporal-worker-controller/internal/testhelpers"
)

func main() {
	// Create a test case using the builder pattern
	testCaseBuilder := testhelpers.NewTestCase().
		WithInput(
			testhelpers.NewTemporalWorkerDeploymentBuilder().
				WithName("example-worker").
				WithNamespace("default").
				WithManualStrategy().
				WithTargetTemplate("v1"),
		).
		WithExpectedStatus(
			testhelpers.NewStatusBuilder().
				WithTargetVersion("v1", temporaliov1alpha1.VersionStatusInactive, -1, true, false),
		).
		WithWaitTime(5 * time.Second).
		WithSetupFunction(func(t *testing.T, ctx context.Context, tc testhelpers.TestCase, env testhelpers.TestEnv) {
			// Custom setup logic can go here
			fmt.Println("Setting up test environment")
		})

	testCase := testCaseBuilder.BuildWithValues("example-worker", "default", "default")

	// Access the TestCase fields
	twd := testCase.GetTWD()
	fmt.Printf("Testing deployment: %s/%s\n", twd.Namespace, twd.Name)
	fmt.Printf("Strategy: %s\n", twd.Spec.RolloutStrategy.Strategy)

}
Output:
Testing deployment: default/example-worker
Strategy: Manual
Example (WithExistingDeployments)

ExampleTestCase_withExistingDeployments demonstrates how to create a TestCase that starts with existing deprecated deployments.

package main

import (
	"fmt"

	temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1"
	"github.com/temporalio/temporal-worker-controller/internal/testhelpers"
)

func main() {
	testCaseBuilder := testhelpers.NewTestCase().
		WithInput(
			testhelpers.NewTemporalWorkerDeploymentBuilder().
				WithName("worker-with-history").
				WithNamespace("test-ns").
				WithAllAtOnceStrategy().
				WithTargetTemplate("v2"),
		).
		WithExistingDeployments(testhelpers.DeploymentInfo{}).
		WithExpectedStatus(
			testhelpers.NewStatusBuilder().
				WithTargetVersion("v2", temporaliov1alpha1.VersionStatusInactive, -1, true, false),
		)

	testCase := testCaseBuilder.BuildWithValues("worker-with-history", "test-ns", "default")

	twd := testCase.GetTWD()
	fmt.Printf("Worker: %s/%s\n", twd.Namespace, twd.Name)

}
Output:
Worker: test-ns/worker-with-history

func (*TestCase) GetExistingDeploymentImages added in v1.0.0

func (tc *TestCase) GetExistingDeploymentImages() map[string]string

func (*TestCase) GetExistingDeploymentReplicas added in v1.0.0

func (tc *TestCase) GetExistingDeploymentReplicas() map[string]int32

func (*TestCase) GetExpectedDeploymentReplicas added in v1.0.0

func (tc *TestCase) GetExpectedDeploymentReplicas() map[string]int32

func (*TestCase) GetExpectedStatus

func (*TestCase) GetSetupFunc added in v1.0.0

func (tc *TestCase) GetSetupFunc() func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)

func (*TestCase) GetTWD

func (*TestCase) GetValidatorFunc added in v1.0.0

func (tc *TestCase) GetValidatorFunc() func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)

func (*TestCase) GetWaitTime

func (tc *TestCase) GetWaitTime() *time.Duration

type TestCaseBuilder

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

TestCaseBuilder provides a fluent interface for building test cases

func NewTestCase

func NewTestCase() *TestCaseBuilder

NewTestCase creates a new test case builder

func NewTestCaseWithValues

func NewTestCaseWithValues(name, k8sNamespace, temporalNamespace string) *TestCaseBuilder

NewTestCaseWithValues creates a new test case builder with the given values

func (*TestCaseBuilder) Build

func (tcb *TestCaseBuilder) Build() TestCase

Build returns the constructed test case

func (*TestCaseBuilder) BuildWithValues

func (tcb *TestCaseBuilder) BuildWithValues(name, k8sNamespace, temporalNamespace string) TestCase

BuildWithValues populates all fields affected by test name, k8s namespace, and temporal namespace and returns the constructed test case

func (*TestCaseBuilder) WithExistingDeployments added in v1.0.0

func (tcb *TestCaseBuilder) WithExistingDeployments(existingDeploymentInfos ...DeploymentInfo) *TestCaseBuilder

WithExistingDeployments adds info to create existing deployments, indexed by the build id that the given image would result in

func (*TestCaseBuilder) WithExpectedDeployments added in v1.0.0

func (tcb *TestCaseBuilder) WithExpectedDeployments(expectedDeploymentInfos ...DeploymentInfo) *TestCaseBuilder

WithExpectedDeployments adds info verify deployments, indexed by the build id that the given image would result in

func (*TestCaseBuilder) WithExpectedStatus

func (tcb *TestCaseBuilder) WithExpectedStatus(statusBuilder *StatusBuilder) *TestCaseBuilder

WithExpectedStatus sets the expected status

func (*TestCaseBuilder) WithInput

WithInput sets the input TWD

func (*TestCaseBuilder) WithSetupFunction added in v1.0.0

func (tcb *TestCaseBuilder) WithSetupFunction(f func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)) *TestCaseBuilder

WithSetupFunction defines a function that the test case will call while setting up the state, after creating the initial Status.

func (*TestCaseBuilder) WithValidatorFunction added in v1.0.0

func (tcb *TestCaseBuilder) WithValidatorFunction(f func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)) *TestCaseBuilder

WithValidatorFunction defines a function that the test case will call after the TWD expected state has been validated. Can be used to validate any other arbitrary state.

func (*TestCaseBuilder) WithWaitTime

func (tcb *TestCaseBuilder) WithWaitTime(waitTime time.Duration) *TestCaseBuilder

WithWaitTime sets the wait time. Use this if you are expecting no change to the initial status and want to ensure that after some time, there is still no change.

type TestEnv added in v1.0.0

type TestEnv struct {
	K8sClient k8sclient.Client
	// Manager of the worker controller. Used to set controller ownership metadata on Deployments
	// created during test setup to make it seem as if they were created by the controller.
	Mgr        manager.Manager
	Ts         *temporaltest.TestServer
	Connection *temporaliov1alpha1.TemporalConnection
	// TemporalWorkerDeploymentStatus only tracks the build ids and Deployment names of the Deployments that have been
	// created, so for test scenarios that start with existing Deployments, specify the number of replicas for each.
	ExistingDeploymentReplicas map[string]int32
	// TemporalWorkerDeploymentStatus only tracks the build ids and Deployment names of the Deployments that have been
	// created, so for test scenarios that start with existing Deployments, specify the image names here, so that the
	// test runner can generate the same pod spec and build id as the controller.
	ExistingDeploymentImages map[string]string
	// TemporalWorkerDeploymentStatus only tracks the build ids and Deployment names of the Deployments that have been
	// created, so for test scenarios that check the replicas of Deployments after Reconciliation, specify the number
	// of replicas for each.
	ExpectedDeploymentReplicas map[string]int32
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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