forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_queue_test.go
190 lines (163 loc) · 5.25 KB
/
azure_queue_test.go
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
188
189
190
//go:build e2e
// +build e2e
package azure_queue_test
import (
"context"
"encoding/base64"
"fmt"
"os"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azqueue"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes"
. "github.com/kedacore/keda/v2/tests/helper"
)
// Load environment variables from .env file
var _ = godotenv.Load("../../../.env")
const (
testName = "azure-queue-test"
)
var (
connectionString = os.Getenv("TF_AZURE_STORAGE_CONNECTION_STRING")
testNamespace = fmt.Sprintf("%s-ns", testName)
secretName = fmt.Sprintf("%s-secret", testName)
deploymentName = fmt.Sprintf("%s-deployment", testName)
scaledObjectName = fmt.Sprintf("%s-so", testName)
queueName = fmt.Sprintf("queue-%d", GetRandomNumber())
)
type templateData struct {
TestNamespace string
SecretName string
Connection string
DeploymentName string
ScaledObjectName string
QueueName string
}
const (
secretTemplate = `
apiVersion: v1
kind: Secret
metadata:
name: {{.SecretName}}
namespace: {{.TestNamespace}}
data:
AzureWebJobsStorage: {{.Connection}}
`
deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
template:
metadata:
labels:
app: {{.DeploymentName}}
spec:
containers:
- name: {{.DeploymentName}}
image: ghcr.io/kedacore/tests-azure-queue
env:
- name: FUNCTIONS_WORKER_RUNTIME
value: node
- name: AzureWebJobsStorage
valueFrom:
secretKeyRef:
name: {{.SecretName}}
key: AzureWebJobsStorage
`
scaledObjectTemplate = `
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {{.ScaledObjectName}}
namespace: {{.TestNamespace}}
spec:
scaleTargetRef:
name: {{.DeploymentName}}
pollingInterval: 5
minReplicaCount: 0
maxReplicaCount: 1
cooldownPeriod: 10
triggers:
- type: azure-queue
metadata:
queueName: {{.QueueName}}
connectionFromEnv: AzureWebJobsStorage
activationQueueLength: "5"
`
)
func TestScaler(t *testing.T) {
// setup
ctx := context.Background()
t.Log("--- setting up ---")
require.NotEmpty(t, connectionString, "TF_AZURE_STORAGE_CONNECTION_STRING env variable is required for azure queue test")
queueClient, err := azqueue.NewQueueClientFromConnectionString(connectionString, queueName, nil)
assert.NoErrorf(t, err, "cannot create the queue client - %s", err)
_, err = queueClient.Create(ctx, nil)
assert.NoErrorf(t, err, "cannot create the queue - %s", err)
// Create kubernetes resources
kc := GetKubernetesClient(t)
data, templates := getTemplateData()
CreateKubernetesResources(t, kc, testNamespace, data, templates)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 1),
"replica count should be 0 after 1 minute")
// test scaling
testActivation(ctx, t, kc, queueClient)
testScaleOut(ctx, t, kc, queueClient)
testScaleIn(ctx, t, kc, queueClient)
// cleanup
DeleteKubernetesResources(t, testNamespace, data, templates)
_, err = queueClient.Delete(ctx, nil)
assert.NoErrorf(t, err, "cannot create the queue - %s", err)
}
func getTemplateData() (templateData, []Template) {
base64ConnectionString := base64.StdEncoding.EncodeToString([]byte(connectionString))
return templateData{
TestNamespace: testNamespace,
SecretName: secretName,
Connection: base64ConnectionString,
DeploymentName: deploymentName,
ScaledObjectName: scaledObjectName,
QueueName: queueName,
}, []Template{
{Name: "secretTemplate", Config: secretTemplate},
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate},
}
}
func testActivation(ctx context.Context, t *testing.T, kc *kubernetes.Clientset, client *azqueue.QueueClient) {
t.Log("--- testing activation ---")
addMessages(ctx, t, client, 3)
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, 0, 60)
}
func testScaleOut(ctx context.Context, t *testing.T, kc *kubernetes.Clientset, client *azqueue.QueueClient) {
t.Log("--- testing scale out ---")
addMessages(ctx, t, client, 5)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1),
"replica count should be 1 after 1 minute")
}
func testScaleIn(ctx context.Context, t *testing.T, kc *kubernetes.Clientset, client *azqueue.QueueClient) {
t.Log("--- testing scale in ---")
_, err := client.ClearMessages(ctx, nil)
assert.NoErrorf(t, err, "cannot clear queue - %s", err)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 1),
"replica count should be 0 after 1 minute")
}
func addMessages(ctx context.Context, t *testing.T, client *azqueue.QueueClient, count int) {
for i := 0; i < count; i++ {
msg := fmt.Sprintf("Message - %d", i)
_, err := client.EnqueueMessage(ctx, msg, nil)
assert.NoErrorf(t, err, "cannot enqueue message - %s", err)
t.Logf("Message queued")
}
}