forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: aviadlevy <aviadlevy1@gmail.com>
- Loading branch information
Showing
1 changed file
with
178 additions
and
0 deletions.
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
tests/scalers_go/custom_hpa_name/custom_hpa_name_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package custom_hpa_name | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/joho/godotenv" | ||
. "github.com/kedacore/keda/v2/tests" | ||
"github.com/stretchr/testify/assert" | ||
autoscalingv2 "k8s.io/api/autoscaling/v2" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// Load environment variables from .env file | ||
var _ = godotenv.Load("../../.env") | ||
|
||
type templateData struct { | ||
TestNamespace string | ||
DeploymentName string | ||
ScaledObjectName string | ||
CustomHpaName string | ||
} | ||
|
||
const ( | ||
deploymentTemplate = ` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{.DeploymentName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
selector: | ||
matchLabels: | ||
run: {{.DeploymentName}} | ||
replicas: 1 | ||
template: | ||
metadata: | ||
labels: | ||
run: {{.DeploymentName}} | ||
spec: | ||
containers: | ||
- name: {{.DeploymentName}} | ||
image: k8s.gcr.io/hpa-example | ||
ports: | ||
- containerPort: 80 | ||
resources: | ||
limits: | ||
cpu: 500m | ||
requests: | ||
cpu: 200m | ||
imagePullPolicy: IfNotPresent | ||
` | ||
|
||
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: cpu | ||
metadata: | ||
type: Utilization | ||
value: "50" | ||
` | ||
|
||
scaledObjectTemplateWithCustomName = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: {{.ScaledObjectName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
scaleTargetRef: | ||
name: {{.DeploymentName}} | ||
minReplicaCount: 1 | ||
maxReplicaCount: 1 | ||
cooldownPeriod: 10 | ||
advanced: | ||
horizontalPodAutoscalerConfig: | ||
name: {{.CustomHpaName}} | ||
triggers: | ||
- type: cpu | ||
metadata: | ||
type: Utilization | ||
value: "50" | ||
` | ||
) | ||
|
||
func TestCustomToDefault(t *testing.T) { | ||
// setup | ||
testName := "custom-to-default-hpa-name" | ||
scaledObjectName := fmt.Sprintf("%s-so", testName) | ||
defaultHpaName := fmt.Sprintf("keda-hpa-%s", scaledObjectName) | ||
customHpaName := fmt.Sprintf("%s-custom", testName) | ||
test(t, testName, customHpaName, scaledObjectTemplateWithCustomName, "custom", | ||
defaultHpaName, scaledObjectTemplate, "default") | ||
} | ||
|
||
func TestDefaultToCustom(t *testing.T) { | ||
// setup | ||
testName := "default-to-custom-hpa-name" | ||
scaledObjectName := fmt.Sprintf("%s-so", testName) | ||
defaultHpaName := fmt.Sprintf("keda-hpa-%s", scaledObjectName) | ||
customHpaName := fmt.Sprintf("%s-custom", testName) | ||
test(t, testName, defaultHpaName, scaledObjectTemplate, "default", | ||
customHpaName, scaledObjectTemplateWithCustomName, "custom") | ||
} | ||
|
||
func test(t *testing.T, testName string, firstHpaName string, firstSOTemplate string, firstHpaDescription string, | ||
secondHpaName string, secondSOTemplate string, secondHpaDescription string) { | ||
testNamespace := fmt.Sprintf("%s-ns", testName) | ||
deploymentName := fmt.Sprintf("%s-deployment", testName) | ||
scaledObjectName := fmt.Sprintf("%s-so", testName) | ||
customHpaName := fmt.Sprintf("%s-custom", testName) | ||
// Create kubernetes resources | ||
kc := GetKubernetesClient(t) | ||
data := getTemplateData(testNamespace, deploymentName, scaledObjectName, customHpaName) | ||
templates := []string{deploymentTemplate, firstSOTemplate} | ||
|
||
CreateKubernetesResources(t, kc, testNamespace, data, templates...) | ||
|
||
t.Logf("--- validate hpa is with %s name ---", firstHpaDescription) | ||
hpa, _ := WaitForHpaCreation(t, kc, firstHpaName, testNamespace, 10, 1) | ||
assert.Equal(t, firstHpaName, hpa.Name) | ||
|
||
t.Log("--- change hpa name ---") | ||
templatesCustomName := []string{secondSOTemplate} | ||
KubectlApplyMultipleWithTemplate(t, data, templatesCustomName...) | ||
|
||
t.Logf("--- validate new hpa is with %s name ---", secondHpaDescription) | ||
hpa, _ = WaitForHpaCreation(t, kc, secondHpaName, testNamespace, 10, 1) | ||
assert.Equal(t, secondHpaName, hpa.Name) | ||
|
||
t.Logf("--- validate hpa with %s name does not exists ---", firstHpaDescription) | ||
_, err := WaitForHpaCreation(t, kc, firstHpaName, testNamespace, 10, 1) | ||
assert.True(t, errors.IsNotFound(err)) | ||
|
||
// cleanup | ||
DeleteKubernetesResources(t, kc, testNamespace, data, templates...) | ||
} | ||
|
||
func getTemplateData(testNamespace string, deploymentName string, scaledObjectName string, customHpaName string) templateData { | ||
return templateData{ | ||
TestNamespace: testNamespace, | ||
DeploymentName: deploymentName, | ||
ScaledObjectName: scaledObjectName, | ||
CustomHpaName: customHpaName, | ||
} | ||
} | ||
|
||
func WaitForHpaCreation(t *testing.T, kc *kubernetes.Clientset, name, namespace string, | ||
iterations, intervalSeconds int) (*autoscalingv2.HorizontalPodAutoscaler, error) { | ||
hpa := &autoscalingv2.HorizontalPodAutoscaler{} | ||
var err error | ||
for i := 0; i < iterations; i++ { | ||
hpa, err = kc.AutoscalingV2().HorizontalPodAutoscalers(namespace).Get(context.Background(), name, metav1.GetOptions{}) | ||
t.Log("Waiting for hpa creation") | ||
if err == nil { | ||
return hpa, err | ||
} | ||
time.Sleep(time.Duration(intervalSeconds) * time.Second) | ||
} | ||
return hpa, err | ||
} |