forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_queue_test.go
216 lines (184 loc) · 6.11 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//go:build e2e
// +build e2e
package azure_queue_test
import (
"context"
"encoding/base64"
"fmt"
"os"
"testing"
"time"
"github.com/Azure/azure-storage-queue-go/azqueue"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/scalers/azure"
kedautil "github.com/kedacore/keda/v2/pkg/util"
. "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
t.Log("--- setting up ---")
require.NotEmpty(t, connectionString, "TF_AZURE_STORAGE_CONNECTION_STRING env variable is required for azure queue test")
queueURL, messageURL := createQueue(t)
// 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(t, kc, messageURL)
testScaleOut(t, kc, messageURL)
testScaleIn(t, kc, messageURL)
// cleanup
DeleteKubernetesResources(t, testNamespace, data, templates)
cleanupQueue(t, queueURL)
}
func createQueue(t *testing.T) (azqueue.QueueURL, azqueue.MessagesURL) {
// Create Queue
httpClient := kedautil.CreateHTTPClient(DefaultHTTPTimeOut, false)
credential, endpoint, err := azure.ParseAzureStorageQueueConnection(
context.Background(), httpClient, kedav1alpha1.AuthPodIdentity{Provider: kedav1alpha1.PodIdentityProviderNone},
connectionString, "", "")
assert.NoErrorf(t, err, "cannot parse storage connection string - %s", err)
p := azqueue.NewPipeline(credential, azqueue.PipelineOptions{})
serviceURL := azqueue.NewServiceURL(*endpoint, p)
queueURL := serviceURL.NewQueueURL(queueName)
_, err = queueURL.Create(context.Background(), azqueue.Metadata{})
assert.NoErrorf(t, err, "cannot create storage queue - %s", err)
messageURL := queueURL.NewMessagesURL()
t.Logf("Queue %s created", queueName)
return queueURL, messageURL
}
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(t *testing.T, kc *kubernetes.Clientset, messageURL azqueue.MessagesURL) {
t.Log("--- testing activation ---")
addMessages(t, messageURL, 3)
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, 0, 60)
}
func testScaleOut(t *testing.T, kc *kubernetes.Clientset, messageURL azqueue.MessagesURL) {
t.Log("--- testing scale out ---")
addMessages(t, messageURL, 5)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1),
"replica count should be 1 after 1 minute")
}
func testScaleIn(t *testing.T, kc *kubernetes.Clientset, messageURL azqueue.MessagesURL) {
t.Log("--- testing scale in ---")
_, err := messageURL.Clear(context.Background())
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(t *testing.T, messageURL azqueue.MessagesURL, count int) {
for i := 0; i < count; i++ {
msg := fmt.Sprintf("Message - %d", i)
_, err := messageURL.Enqueue(context.Background(), msg, 0*time.Second, time.Hour)
assert.NoErrorf(t, err, "cannot enqueue message - %s", err)
t.Logf("Message queued")
}
}
func cleanupQueue(t *testing.T, queueURL azqueue.QueueURL) {
t.Log("--- cleaning up ---")
_, err := queueURL.Delete(context.Background())
assert.NoErrorf(t, err, "cannot delete storage queue - %s", err)
t.Logf("Queue %s deleted", queueName)
}