-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtemporalworker_webhook_test.go
More file actions
187 lines (165 loc) · 6.47 KB
/
temporalworker_webhook_test.go
File metadata and controls
187 lines (165 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc.
package v1alpha1_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1"
"github.com/temporalio/temporal-worker-controller/internal/testhelpers"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
func TestTemporalWorkerDeployment_ValidateCreate(t *testing.T) {
tests := map[string]struct {
obj runtime.Object
errorMsg string
}{
"valid temporal worker deployment": {
obj: testhelpers.MakeTWDWithName("valid-worker", ""),
},
"temporal worker deployment with name too long": {
obj: testhelpers.MakeTWDWithName("this-is-a-very-long-temporal-worker-deployment-name-that-exceeds-the-maximum-allowed-length-of-sixty-three-characters", ""),
errorMsg: "cannot be more than 63 characters",
},
"invalid object type": {
obj: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
},
errorMsg: "expected a TemporalWorkerDeployment",
},
"missing rollout steps": {
obj: testhelpers.ModifyObj(testhelpers.MakeTWDWithName("prog-rollout-missing-steps", ""), func(obj *temporaliov1alpha1.TemporalWorkerDeployment) *temporaliov1alpha1.TemporalWorkerDeployment {
obj.Spec.RolloutStrategy.Strategy = temporaliov1alpha1.UpdateProgressive
obj.Spec.RolloutStrategy.Steps = nil
return obj
}),
errorMsg: "spec.rollout.steps: Invalid value: null: steps are required for Progressive rollout",
},
"ramp value for step <= previous step": {
obj: testhelpers.ModifyObj(testhelpers.MakeTWDWithName("prog-rollout-decreasing-ramps", ""), func(obj *temporaliov1alpha1.TemporalWorkerDeployment) *temporaliov1alpha1.TemporalWorkerDeployment {
obj.Spec.RolloutStrategy.Strategy = temporaliov1alpha1.UpdateProgressive
obj.Spec.RolloutStrategy.Steps = []temporaliov1alpha1.RolloutStep{
{5, metav1.Duration{Duration: time.Minute}},
{10, metav1.Duration{Duration: time.Minute}},
{9, metav1.Duration{Duration: time.Minute}},
{50, metav1.Duration{Duration: time.Minute}},
{50, metav1.Duration{Duration: time.Minute}},
{75, metav1.Duration{Duration: time.Minute}},
}
return obj
}),
errorMsg: "[spec.rollout.steps[2].rampPercentage: Invalid value: 9: rampPercentage must increase between each step, spec.rollout.steps[4].rampPercentage: Invalid value: 50: rampPercentage must increase between each step]",
},
"pause duration < 30s": {
obj: testhelpers.ModifyObj(testhelpers.MakeTWDWithName("prog-rollout-decreasing-ramps", ""), func(obj *temporaliov1alpha1.TemporalWorkerDeployment) *temporaliov1alpha1.TemporalWorkerDeployment {
obj.Spec.RolloutStrategy.Strategy = temporaliov1alpha1.UpdateProgressive
obj.Spec.RolloutStrategy.Steps = []temporaliov1alpha1.RolloutStep{
{10, metav1.Duration{Duration: time.Minute}},
{25, metav1.Duration{Duration: 10 * time.Second}},
{50, metav1.Duration{Duration: time.Minute}},
}
return obj
}),
errorMsg: `spec.rollout.steps[1].pauseDuration: Invalid value: "10s": pause duration must be at least 30s`,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
webhook := &temporaliov1alpha1.TemporalWorkerDeployment{}
assertAdmission := func(warnings admission.Warnings, err error) {
if tc.errorMsg != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errorMsg)
} else {
require.NoError(t, err)
}
// Warnings should always be nil for this implementation
assert.Nil(t, warnings)
}
// Verify that create and update enforce the same rules
assertAdmission(webhook.ValidateCreate(ctx, tc.obj))
assertAdmission(webhook.ValidateUpdate(ctx, nil, tc.obj))
})
}
}
func TestTemporalWorkerDeployment_ValidateUpdate(t *testing.T) {
tests := map[string]struct {
oldObj runtime.Object
newObj runtime.Object
errorMsg string
}{
"valid update": {
oldObj: nil,
newObj: testhelpers.MakeTWDWithName("valid-worker", ""),
},
"update with name too long": {
oldObj: nil,
newObj: testhelpers.MakeTWDWithName("this-is-a-very-long-temporal-worker-deployment-name-that-exceeds-the-maximum-allowed-length-of-sixty-three-characters", ""),
errorMsg: "cannot be more than 63 characters",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
webhook := &temporaliov1alpha1.TemporalWorkerDeployment{}
warnings, err := webhook.ValidateUpdate(ctx, tc.oldObj, tc.newObj)
if tc.errorMsg != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tc.errorMsg)
} else {
require.NoError(t, err)
}
// Warnings should always be nil for this implementation
assert.Nil(t, warnings)
})
}
}
func TestTemporalWorkerDeployment_ValidateDelete(t *testing.T) {
ctx := context.Background()
webhook := &temporaliov1alpha1.TemporalWorkerDeployment{}
obj := &temporaliov1alpha1.TemporalWorkerDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: "worker",
},
}
warnings, err := webhook.ValidateDelete(ctx, obj)
// ValidateDelete should always return nil, nil
assert.NoError(t, err)
assert.Nil(t, warnings)
}
func TestTemporalWorkerDeployment_Default(t *testing.T) {
tests := map[string]struct {
obj runtime.Object
expected func(t *testing.T, obj *temporaliov1alpha1.TemporalWorkerDeployment)
}{
"sets default sunset strategy delays": {
obj: testhelpers.MakeTWDWithName("default-sunset-delays", ""),
expected: func(t *testing.T, obj *temporaliov1alpha1.TemporalWorkerDeployment) {
require.NotNil(t, obj.Spec.SunsetStrategy.ScaledownDelay)
assert.Equal(t, time.Hour, obj.Spec.SunsetStrategy.ScaledownDelay.Duration)
require.NotNil(t, obj.Spec.SunsetStrategy.DeleteDelay)
assert.Equal(t, 24*time.Hour, obj.Spec.SunsetStrategy.DeleteDelay.Duration)
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
webhook := &temporaliov1alpha1.TemporalWorkerDeployment{}
err := webhook.Default(ctx, tc.obj)
require.NoError(t, err)
obj, ok := tc.obj.(*temporaliov1alpha1.TemporalWorkerDeployment)
require.True(t, ok)
tc.expected(t, obj)
})
}
}