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.
chore: add aws-pod-identity e2e tests (kedacore#3685)
* chore: add aws-pod-identity e2e tests Signed-off-by: Jorge Turrado <[email protected]>
- Loading branch information
Showing
16 changed files
with
1,332 additions
and
16 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
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
228 changes: 228 additions & 0 deletions
228
tests/scalers/aws/aws_cloudwatch_pod_identity/aws_cloudwatch_pod_identity_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,228 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package aws_cloudwatch_pod_identity_test | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/cloudwatch" | ||
"github.com/joho/godotenv" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
. "github.com/kedacore/keda/v2/tests/helper" | ||
) | ||
|
||
// Load environment variables from .env file | ||
var _ = godotenv.Load("../../../.env") | ||
|
||
const ( | ||
testName = "aws-cloudwatch-pod-identity-test" | ||
) | ||
|
||
type templateData struct { | ||
TestNamespace string | ||
DeploymentName string | ||
ScaledObjectName string | ||
SecretName string | ||
AwsAccessKeyID string | ||
AwsSecretAccessKey string | ||
AwsRegion string | ||
CloudWatchMetricName string | ||
CloudWatchMetricNamespace string | ||
CloudWatchMetricDimensionName string | ||
CloudWatchMetricDimensionValue string | ||
} | ||
|
||
const ( | ||
triggerAuthenticationTemplate = `apiVersion: keda.sh/v1alpha1 | ||
kind: TriggerAuthentication | ||
metadata: | ||
name: keda-trigger-auth-aws-credentials | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
podIdentity: | ||
provider: aws-eks | ||
` | ||
|
||
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: | ||
serviceAccountName: default | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
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 | ||
triggers: | ||
- type: aws-cloudwatch | ||
authenticationRef: | ||
name: keda-trigger-auth-aws-credentials | ||
metadata: | ||
awsRegion: {{.AwsRegion}} | ||
namespace: {{.CloudWatchMetricNamespace}} | ||
dimensionName: {{.CloudWatchMetricDimensionName}} | ||
dimensionValue: {{.CloudWatchMetricDimensionValue}} | ||
metricName: {{.CloudWatchMetricName}} | ||
targetMetricValue: "1" | ||
activationTargetMetricValue: "5" | ||
minMetricValue: "0" | ||
metricCollectionTime: "120" | ||
metricStatPeriod: "30" | ||
identityOwner: operator | ||
` | ||
) | ||
|
||
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) | ||
cloudwatchMetricName = fmt.Sprintf("%s-keda-metric-%d", testName, GetRandomNumber()) | ||
awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY") | ||
awsSecretAccessKey = os.Getenv("AWS_SECRET_KEY") | ||
awsRegion = os.Getenv("AWS_REGION") | ||
cloudwatchMetricNamespace = "KEDA" | ||
cloudwatchMetricDimensionName = "dimensionName" | ||
cloudwatchMetricDimensionValue = "dimensionValue" | ||
maxReplicaCount = 2 | ||
minReplicaCount = 0 | ||
) | ||
|
||
func TestCloudWatchScaler(t *testing.T) { | ||
// setup cloudwatch | ||
cloudwatchClient := createCloudWatchClient() | ||
setCloudWatchCustomMetric(t, cloudwatchClient, 0) | ||
|
||
// Create kubernetes resources | ||
kc := GetKubernetesClient(t) | ||
data, templates := getTemplateData() | ||
CreateKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 1), | ||
"replica count should be %d after 1 minute", minReplicaCount) | ||
|
||
// test scaling | ||
testActivation(t, kc, cloudwatchClient) | ||
testScaleUp(t, kc, cloudwatchClient) | ||
testScaleDown(t, kc, cloudwatchClient) | ||
|
||
// cleanup | ||
DeleteKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
setCloudWatchCustomMetric(t, cloudwatchClient, 0) | ||
} | ||
|
||
func testActivation(t *testing.T, kc *kubernetes.Clientset, cloudwatchClient *cloudwatch.CloudWatch) { | ||
t.Log("--- testing activation ---") | ||
setCloudWatchCustomMetric(t, cloudwatchClient, 3) | ||
|
||
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, minReplicaCount, 60) | ||
} | ||
|
||
func testScaleUp(t *testing.T, kc *kubernetes.Clientset, cloudwatchClient *cloudwatch.CloudWatch) { | ||
t.Log("--- testing scale up ---") | ||
setCloudWatchCustomMetric(t, cloudwatchClient, 10) | ||
|
||
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 60, 3), | ||
"replica count should be %d after 3 minutes", maxReplicaCount) | ||
} | ||
|
||
func testScaleDown(t *testing.T, kc *kubernetes.Clientset, cloudwatchClient *cloudwatch.CloudWatch) { | ||
t.Log("--- testing scale down ---") | ||
|
||
setCloudWatchCustomMetric(t, cloudwatchClient, 0) | ||
|
||
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 3), | ||
"replica count should be %d after 3 minutes", minReplicaCount) | ||
} | ||
|
||
func setCloudWatchCustomMetric(t *testing.T, cloudwatchClient *cloudwatch.CloudWatch, value float64) { | ||
_, err := cloudwatchClient.PutMetricDataWithContext(context.Background(), &cloudwatch.PutMetricDataInput{ | ||
MetricData: []*cloudwatch.MetricDatum{ | ||
{ | ||
MetricName: aws.String(cloudwatchMetricName), | ||
Dimensions: []*cloudwatch.Dimension{ | ||
{ | ||
Name: aws.String(cloudwatchMetricDimensionName), | ||
Value: aws.String(cloudwatchMetricDimensionValue), | ||
}, | ||
}, | ||
Unit: aws.String("None"), | ||
Value: aws.Float64(value), | ||
}, | ||
}, | ||
Namespace: aws.String(cloudwatchMetricNamespace), | ||
}) | ||
assert.NoErrorf(t, err, "failed to set cloudwatch metric - %s", err) | ||
} | ||
|
||
func createCloudWatchClient() *cloudwatch.CloudWatch { | ||
sess := session.Must(session.NewSession(&aws.Config{ | ||
Region: aws.String(awsRegion), | ||
})) | ||
|
||
return cloudwatch.New(sess, &aws.Config{ | ||
Region: aws.String(awsRegion), | ||
Credentials: credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, ""), | ||
}) | ||
} | ||
|
||
func getTemplateData() (templateData, []Template) { | ||
return templateData{ | ||
TestNamespace: testNamespace, | ||
DeploymentName: deploymentName, | ||
ScaledObjectName: scaledObjectName, | ||
SecretName: secretName, | ||
AwsAccessKeyID: base64.StdEncoding.EncodeToString([]byte(awsAccessKeyID)), | ||
AwsSecretAccessKey: base64.StdEncoding.EncodeToString([]byte(awsSecretAccessKey)), | ||
AwsRegion: awsRegion, | ||
CloudWatchMetricName: cloudwatchMetricName, | ||
CloudWatchMetricNamespace: cloudwatchMetricNamespace, | ||
CloudWatchMetricDimensionName: cloudwatchMetricDimensionName, | ||
CloudWatchMetricDimensionValue: cloudwatchMetricDimensionValue, | ||
}, []Template{ | ||
{Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate}, | ||
{Name: "deploymentTemplate", Config: deploymentTemplate}, | ||
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate}, | ||
} | ||
} |
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
Oops, something went wrong.