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.
https://github.com/kedacore/keda/issues/2214
- Loading branch information
Siva Guruvareddiar
committed
Dec 29, 2023
1 parent
f60c1f3
commit 8575dbc
Showing
3 changed files
with
244 additions
and
12 deletions.
There are no files selected for viewing
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
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
191 changes: 191 additions & 0 deletions
191
tests/scalers/aws/aws_managed_prometheus/aws_managed_prometheus_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,191 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package aws_managed_prometheus_test | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/amp" | ||
"github.com/joho/godotenv" | ||
. "github.com/kedacore/keda/v2/tests/helper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Load environment variables from .env file | ||
var _ = godotenv.Load("../../../.env") | ||
|
||
const ( | ||
testName = "aws-amp-test" | ||
) | ||
|
||
type templateData struct { | ||
TestNamespace string | ||
DeploymentName string | ||
ScaledObjectName string | ||
SecretName string | ||
AwsAccessKeyID string | ||
AwsSecretAccessKey string | ||
AwsRegion string | ||
WorkspaceId string | ||
} | ||
|
||
const ( | ||
secretTemplate = `apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: {{.SecretName}} | ||
namespace: {{.TestNamespace}} | ||
data: | ||
AWS_ACCESS_KEY_ID: {{.AwsAccessKeyID}} | ||
AWS_SECRET_ACCESS_KEY: {{.AwsSecretAccessKey}} | ||
` | ||
|
||
triggerAuthenticationTemplate = `apiVersion: keda.sh/v1alpha1 | ||
kind: TriggerAuthentication | ||
metadata: | ||
name: keda-trigger-auth-aws-credentials | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
secretTargetRef: | ||
- parameter: awsAccessKeyID # Required. | ||
name: {{.SecretName}} # Required. | ||
key: AWS_ACCESS_KEY_ID # Required. | ||
- parameter: awsSecretAccessKey # Required. | ||
name: {{.SecretName}} # Required. | ||
key: AWS_SECRET_ACCESS_KEY # Required. | ||
` | ||
|
||
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: nginx | ||
image: nginxinc/nginx-unprivileged | ||
ports: | ||
- containerPort: 80 | ||
` | ||
|
||
scaledObjectTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: {{.ScaledObjectName}} | ||
namespace: {{.TestNamespace}} | ||
labels: | ||
app: {{.DeploymentName}} | ||
spec: | ||
scaleTargetRef: | ||
name: {{.DeploymentName}} | ||
maxReplicaCount: 2 | ||
minReplicaCount: 0 | ||
cooldownPeriod: 1 | ||
advanced: | ||
horizontalPodAutoscalerConfig: | ||
behavior: | ||
scaleDown: | ||
stabilizationWindowSeconds: 15 | ||
triggers: | ||
- type: prometheus | ||
authenticationRef: | ||
name: keda-trigger-auth-aws-credentials | ||
metadata: | ||
awsRegion: {{.AwsRegion}} | ||
serverAddress: "https://aps-workspaces.{{.AwsRegion}}.amazonaws.com/workspaces/{{.WorkspaceId}}" | ||
query: "vector(100)" | ||
threshold: "50.0" | ||
` | ||
) | ||
|
||
var ( | ||
testNamespace = fmt.Sprintf("%s-ns", testName) | ||
deploymentName = fmt.Sprintf("%s-deployment", testName) | ||
scaledObjectName = fmt.Sprintf("%s-so", testName) | ||
secretName = fmt.Sprintf("%s-secret", testName) | ||
workspaceId = fmt.Sprintf("workspace-%d", GetRandomNumber()) | ||
awsAccessKeyID = os.Getenv("TF_AWS_ACCESS_KEY") | ||
awsSecretAccessKey = os.Getenv("TF_AWS_SECRET_KEY") | ||
awsRegion = os.Getenv("TF_AWS_REGION") | ||
maxReplicaCount = 2 | ||
minReplicaCount = 0 | ||
) | ||
|
||
func TestScaler(t *testing.T) { | ||
require.NotEmpty(t, awsAccessKeyID, "AwsAccessKeyID env variable is required for AWS e2e test") | ||
require.NotEmpty(t, awsSecretAccessKey, "awsSecretAccessKey env variable is required for AWS e2e test") | ||
|
||
t.Log("--- setting up ---") | ||
|
||
ampClient := createAMPClient() | ||
workspaceOutput, _ := ampClient.CreateWorkspace(context.Background(), nil) | ||
workspaceId = *workspaceOutput.WorkspaceId | ||
|
||
t.Log("--- workspaceId ---", workspaceId) | ||
|
||
kc := GetKubernetesClient(t) | ||
|
||
data, templates := getTemplateData() | ||
CreateKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
t.Log("--- assert ---") | ||
expectedReplicaCountNumber := 0 // as mentioned above, as the AMP returns 100 and the threshold set to 50, the expected replica count is 100 / 50 = 2 | ||
assert.Truef(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, expectedReplicaCountNumber, 60, 1), | ||
"replica count should be %d after a minute", expectedReplicaCountNumber) | ||
|
||
t.Log("--- cleaning up ---") | ||
// deleteWSInput := &.DeleteWorkspaceInput{ | ||
// WorkspaceId: &workspaceId, | ||
// } | ||
// ampClient.DeleteWorkspace(context.Background(), deleteWSInput, nil) | ||
DeleteKubernetesResources(t, testNamespace, data, templates) | ||
|
||
} | ||
|
||
func getTemplateData() (templateData, []Template) { | ||
fmt.Print("--- workspaceId ---", workspaceId) | ||
|
||
return templateData{ | ||
TestNamespace: testNamespace, | ||
SecretName: secretName, | ||
AwsAccessKeyID: base64.StdEncoding.EncodeToString([]byte(awsAccessKeyID)), | ||
AwsSecretAccessKey: base64.StdEncoding.EncodeToString([]byte(awsSecretAccessKey)), | ||
AwsRegion: awsRegion, | ||
DeploymentName: deploymentName, | ||
ScaledObjectName: scaledObjectName, | ||
WorkspaceId: workspaceId, | ||
}, []Template{ | ||
{Name: "deploymentTemplate", Config: deploymentTemplate}, | ||
{Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate}, | ||
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate}, | ||
} | ||
} | ||
|
||
func createAMPClient() *amp.Client { | ||
configOptions := make([]func(*config.LoadOptions) error, 0) | ||
configOptions = append(configOptions, config.WithRegion(awsRegion)) | ||
cfg, _ := config.LoadDefaultConfig(context.TODO(), configOptions...) | ||
cfg.Credentials = credentials.NewStaticCredentialsProvider(awsAccessKeyID, awsSecretAccessKey, "") | ||
return amp.NewFromConfig(cfg) | ||
} |