Skip to content

Commit bf670d3

Browse files
authored
Merge pull request #5 from temporalio/jlegrone/address-pr-comments
Address PR review comments
2 parents f107c39 + b227d2b commit bf670d3

File tree

6 files changed

+98
-38
lines changed

6 files changed

+98
-38
lines changed

internal/planner/planner.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,8 @@ func getScaleDeployments(
258258
if d.Spec.Replicas != nil && *d.Spec.Replicas != replicas {
259259
scaleDeployments[version.Deployment] = uint32(replicas)
260260
}
261-
} else {
262-
if d.Spec.Replicas != nil && *d.Spec.Replicas != 0 {
263-
scaleDeployments[version.Deployment] = 0
264-
}
261+
} else if d.Spec.Replicas != nil && *d.Spec.Replicas != 0 {
262+
scaleDeployments[version.Deployment] = 0
265263
}
266264
case temporaliov1alpha1.VersionStatusRamping, temporaliov1alpha1.VersionStatusCurrent:
267265
// Scale up these deployments
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package testhelpers_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1"
10+
"github.com/temporalio/temporal-worker-controller/internal/testhelpers"
11+
)
12+
13+
// ExampleTestCase demonstrates how to create and use a TestCase for integration testing.
14+
// This example shows the basic structure and usage patterns for TestCase.
15+
func ExampleTestCase() {
16+
// Create a test case using the builder pattern
17+
testCaseBuilder := testhelpers.NewTestCase().
18+
WithInput(
19+
testhelpers.NewTemporalWorkerDeploymentBuilder().
20+
WithName("example-worker").
21+
WithNamespace("default").
22+
WithManualStrategy().
23+
WithTargetTemplate("v1"),
24+
).
25+
WithExpectedStatus(
26+
testhelpers.NewStatusBuilder().
27+
WithTargetVersion("v1", temporaliov1alpha1.VersionStatusInactive, -1, true, false),
28+
).
29+
WithWaitTime(5 * time.Second).
30+
WithSetupFunction(func(t *testing.T, ctx context.Context, tc testhelpers.TestCase, env testhelpers.TestEnv) {
31+
// Custom setup logic can go here
32+
fmt.Println("Setting up test environment")
33+
})
34+
35+
testCase := testCaseBuilder.BuildWithValues("example-worker", "default", "default")
36+
37+
// Access the TestCase fields
38+
twd := testCase.GetTWD()
39+
fmt.Printf("Testing deployment: %s/%s\n", twd.Namespace, twd.Name)
40+
fmt.Printf("Strategy: %s\n", twd.Spec.RolloutStrategy.Strategy)
41+
42+
// Output:
43+
// Testing deployment: default/example-worker
44+
// Strategy: Manual
45+
}
46+
47+
// ExampleTestCase_withExistingDeployments demonstrates how to create a TestCase
48+
// that starts with existing deprecated deployments.
49+
func ExampleTestCase_withExistingDeployments() {
50+
testCaseBuilder := testhelpers.NewTestCase().
51+
WithInput(
52+
testhelpers.NewTemporalWorkerDeploymentBuilder().
53+
WithName("worker-with-history").
54+
WithNamespace("test-ns").
55+
WithAllAtOnceStrategy().
56+
WithTargetTemplate("v2"),
57+
).
58+
WithExistingDeployments(testhelpers.DeploymentInfo{}).
59+
WithExpectedStatus(
60+
testhelpers.NewStatusBuilder().
61+
WithTargetVersion("v2", temporaliov1alpha1.VersionStatusInactive, -1, true, false),
62+
)
63+
64+
testCase := testCaseBuilder.BuildWithValues("worker-with-history", "test-ns", "default")
65+
66+
twd := testCase.GetTWD()
67+
fmt.Printf("Worker: %s/%s\n", twd.Namespace, twd.Name)
68+
69+
// Output:
70+
// Worker: test-ns/worker-with-history
71+
}

internal/testhelpers/make.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
TaskQueueEnvKey = "TEMPORAL_TASK_QUEUE"
16+
taskQueueEnvKey = "TEMPORAL_TASK_QUEUE"
1717
)
1818

1919
func MakeTWD(
@@ -64,7 +64,7 @@ func MakeTWD(
6464
// MakePodSpec creates a pod spec with the given containers, labels, and task queue
6565
func MakePodSpec(containers []corev1.Container, labels map[string]string, taskQueue string) corev1.PodTemplateSpec {
6666
for i := range containers {
67-
containers[i].Env = append(containers[i].Env, corev1.EnvVar{Name: TaskQueueEnvKey, Value: taskQueue})
67+
containers[i].Env = append(containers[i].Env, corev1.EnvVar{Name: taskQueueEnvKey, Value: taskQueue})
6868
}
6969

7070
return corev1.PodTemplateSpec{
@@ -84,19 +84,19 @@ func MakePodSpecWithImage(imageName string) corev1.PodTemplateSpec {
8484
"")
8585
}
8686

87-
// SetTaskQueue sets or replaces the env var TaskQueueEnvKey with the given string in all containers
87+
// SetTaskQueue sets or replaces the env var taskQueueEnvKey with the given string in all containers
8888
func SetTaskQueue(podSpec corev1.PodTemplateSpec, taskQueue string) corev1.PodTemplateSpec {
8989
for i, c := range podSpec.Spec.Containers {
9090
found := false
9191
for j, e := range c.Env {
92-
if e.Name == TaskQueueEnvKey {
92+
if e.Name == taskQueueEnvKey {
9393
found = true
9494
podSpec.Spec.Containers[i].Env[j].Value = taskQueue
9595
}
9696
}
9797
if !found {
9898
podSpec.Spec.Containers[i].Env = append(podSpec.Spec.Containers[i].Env, corev1.EnvVar{
99-
Name: TaskQueueEnvKey,
99+
Name: taskQueueEnvKey,
100100
Value: taskQueue,
101101
})
102102
}

internal/testhelpers/test_builder.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,25 +209,37 @@ func (sb *StatusBuilder) Build() *temporaliov1alpha1.TemporalWorkerDeploymentSta
209209
return ret
210210
}
211211

212+
// TestCase represents a single test scenario for integration testing of TemporalWorkerDeployment controllers.
213+
// It encapsulates the input state, expected outputs, and any additional setup required for the test.
212214
type TestCase struct {
213-
// If starting from a particular state, specify that in input.Status
215+
// twd is the TemporalWorkerDeployment resource to test with.
216+
// If starting from a particular state, specify that in input.Status.
214217
twd *temporaliov1alpha1.TemporalWorkerDeployment
218+
219+
// existingDeploymentReplicas specifies the number of replicas for each deprecated build.
215220
// TemporalWorkerDeploymentStatus only tracks the names of the Deployments for deprecated
216221
// versions, so for test scenarios that start with existing deprecated version Deployments,
217222
// specify the number of replicas for each deprecated build here.
218223
existingDeploymentReplicas map[string]int32
224+
225+
// existingDeploymentImages specifies the images for each deprecated build.
219226
// TemporalWorkerDeploymentStatus only tracks the build ids of the Deployments for deprecated
220227
// versions, not their images so for test scenarios that start with existing deprecated version Deployments,
221228
// specify the images for each deprecated build here.
222229
existingDeploymentImages map[string]string
223-
expectedStatus *temporaliov1alpha1.TemporalWorkerDeploymentStatus
224-
// validate that deployments have correct # of replicas. TODO(carlydf): validate replica count for more than just the deprecated versions
230+
231+
// expectedStatus is the expected TemporalWorkerDeploymentStatus after the test completes.
232+
expectedStatus *temporaliov1alpha1.TemporalWorkerDeploymentStatus
233+
234+
// expectedDeploymentReplicas validates that deployments have correct number of replicas.
235+
// TODO(carlydf): validate replica count for more than just the deprecated versions
225236
expectedDeploymentReplicas map[string]int32
226-
// Time to delay before checking expected status
237+
238+
// waitTime is the duration to delay before checking expected status.
227239
waitTime *time.Duration
228240

229-
// Arbitrary function called at the end of setting up the environment specified by input.Status.
230-
// Can be used for additional state creation / destruction
241+
// setupFunc is an arbitrary function called at the end of setting up the environment specified by input.Status.
242+
// It can be used for additional state creation or destruction.
231243
setupFunc func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)
232244
}
233245

internal/testhelpers/workers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func newVersionedWorker(ctx context.Context, podTemplateSpec corev1.PodTemplateS
3737
if err != nil {
3838
return nil, nil, err
3939
}
40-
temporalTaskQueue, err := getEnv(podTemplateSpec, TaskQueueEnvKey)
40+
temporalTaskQueue, err := getEnv(podTemplateSpec, taskQueueEnvKey)
4141
if err != nil {
4242
return nil, nil, err
4343
}

internal/tests/README.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,7 @@ If that becomes an issue, it can be changed, but I think using one `temporaltest
7575
functional test and sharing it across test cases will make the tests run a lot faster as we add more
7676
test cases.
7777

78-
Each test case should pass in:
79-
```go
80-
type TestCase struct {
81-
// If starting from a particular state, specify that in input.Status
82-
twd *temporaliov1alpha1.TemporalWorkerDeployment
83-
// TemporalWorkerDeploymentStatus only tracks the names of the Deployments for deprecated
84-
// versions, so for test scenarios that start with existing deprecated version Deployments,
85-
// specify the number of replicas for each deprecated build here.
86-
existingDeploymentReplicas map[string]int32
87-
// TemporalWorkerDeploymentStatus only tracks the build ids of the Deployments for deprecated
88-
// versions, not their images so for test scenarios that start with existing deprecated version Deployments,
89-
// specify the images for each deprecated build here.
90-
existingDeploymentImages map[string]string
91-
expectedStatus *temporaliov1alpha1.TemporalWorkerDeploymentStatus
92-
// Time to delay before checking expected status
93-
waitTime *time.Duration
94-
95-
// Arbitrary function called at the end of setting up the environment specified by input.Status.
96-
// Can be used for additional state creation / destruction
97-
setupFunc func(t *testing.T, ctx context.Context, tc TestCase, env TestEnv)
98-
}
99-
```
78+
Each test case should use the `testhelpers.TestCase` struct. See the godoc for `testhelpers.TestCase` for detailed field descriptions and the examples in `testhelpers/example_test.go` for usage patterns.
10079

10180
Each test function should follow the same pattern:
10281
1. Set up test environment (including preliminary state)

0 commit comments

Comments
 (0)