|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import aws.sdk.kotlin.runtime.auth.credentials.EnvironmentCredentialsProvider |
| 5 | +import aws.sdk.kotlin.services.secretsmanager.SecretsManagerClient |
| 6 | +import aws.sdk.kotlin.services.secretsmanager.model.GetSecretValueRequest |
| 7 | +import com.example.autoscaling.createAutoScalingGroup |
| 8 | +import com.example.autoscaling.deleteSpecificAutoScalingGroup |
| 9 | +import com.example.autoscaling.describeAccountLimits |
| 10 | +import com.example.autoscaling.describeAutoScalingGroups |
| 11 | +import com.example.autoscaling.describeAutoScalingInstance |
| 12 | +import com.example.autoscaling.describeScalingActivities |
| 13 | +import com.example.autoscaling.disableMetricsCollection |
| 14 | +import com.example.autoscaling.enableMetricsCollection |
| 15 | +import com.example.autoscaling.getAutoScalingGroups |
| 16 | +import com.example.autoscaling.getSpecificAutoScaling |
| 17 | +import com.example.autoscaling.setDesiredCapacity |
| 18 | +import com.example.autoscaling.terminateInstanceInAutoScalingGroup |
| 19 | +import com.example.autoscaling.updateAutoScalingGroup |
| 20 | +import com.google.gson.Gson |
| 21 | +import kotlinx.coroutines.delay |
| 22 | +import kotlinx.coroutines.runBlocking |
| 23 | +import org.junit.jupiter.api.BeforeAll |
| 24 | +import org.junit.jupiter.api.DisplayName |
| 25 | +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation |
| 26 | +import org.junit.jupiter.api.Nested |
| 27 | +import org.junit.jupiter.api.Order |
| 28 | +import org.junit.jupiter.api.Test |
| 29 | +import org.junit.jupiter.api.TestInstance |
| 30 | +import org.junit.jupiter.api.TestMethodOrder |
| 31 | +import java.io.IOException |
| 32 | +import java.util.Random |
| 33 | +import kotlin.system.exitProcess |
| 34 | + |
| 35 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 36 | +@TestMethodOrder(OrderAnnotation::class) |
| 37 | +class AutoScalingTest { |
| 38 | + private var groupName = "" |
| 39 | + private var groupNameSc = "" |
| 40 | + private var launchTemplateName = "" |
| 41 | + private var vpcZoneId = "" |
| 42 | + private var serviceLinkedRoleARN = "" |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + @Throws(IOException::class) |
| 46 | + fun setUp() = |
| 47 | + runBlocking { |
| 48 | + val random = Random() |
| 49 | + val randomNum = random.nextInt(10000 - 1 + 1) + 1 |
| 50 | + // Get the values from AWS Secrets Manager. |
| 51 | + val gson = Gson() |
| 52 | + val json = getSecretValues() |
| 53 | + val values = gson.fromJson(json, SecretValues::class.java) |
| 54 | + groupName = values.groupName.toString() + randomNum |
| 55 | + launchTemplateName = values.launchTemplateName.toString() |
| 56 | + vpcZoneId = values.vpcZoneId.toString() |
| 57 | + serviceLinkedRoleARN = values.serviceLinkedRoleARN.toString() |
| 58 | + groupNameSc = values.groupNameSc.toString() + randomNum |
| 59 | + // Uncomment this code block if you prefer using a config.properties file to retrieve AWS values required for these tests. |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @Order(1) |
| 64 | + fun testScenario() = |
| 65 | + runBlocking { |
| 66 | + println("**** Create an Auto Scaling group named $groupName") |
| 67 | + createAutoScalingGroup(groupName, launchTemplateName, serviceLinkedRoleARN, vpcZoneId) |
| 68 | + |
| 69 | + println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned") |
| 70 | + delay(60000) |
| 71 | + |
| 72 | + val instanceId = getSpecificAutoScaling(groupName) |
| 73 | + if (instanceId.compareTo("") == 0) { |
| 74 | + println("Error - no instance Id value") |
| 75 | + exitProcess(1) |
| 76 | + } else { |
| 77 | + println("The instance Id value is $instanceId") |
| 78 | + } |
| 79 | + |
| 80 | + println("**** Describe Auto Scaling with the Id value $instanceId") |
| 81 | + describeAutoScalingInstance(instanceId) |
| 82 | + |
| 83 | + println("**** Enable metrics collection $instanceId") |
| 84 | + enableMetricsCollection(groupName) |
| 85 | + |
| 86 | + println("**** Update an Auto Scaling group to update max size to 3") |
| 87 | + updateAutoScalingGroup(groupName, launchTemplateName, serviceLinkedRoleARN) |
| 88 | + |
| 89 | + println("**** Describe all Auto Scaling groups to show the current state of the groups") |
| 90 | + describeAutoScalingGroups(groupName) |
| 91 | + |
| 92 | + println("**** Describe account details") |
| 93 | + describeAccountLimits() |
| 94 | + |
| 95 | + println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned") |
| 96 | + delay(60000) |
| 97 | + |
| 98 | + println("**** Set desired capacity to 2") |
| 99 | + setDesiredCapacity(groupName) |
| 100 | + |
| 101 | + println("**** Get the two instance Id values and state") |
| 102 | + getAutoScalingGroups(groupName) |
| 103 | + |
| 104 | + println("**** List the scaling activities that have occurred for the group") |
| 105 | + describeScalingActivities(groupName) |
| 106 | + |
| 107 | + println("**** Terminate an instance in the Auto Scaling group") |
| 108 | + terminateInstanceInAutoScalingGroup(instanceId) |
| 109 | + |
| 110 | + println("**** Stop the metrics collection") |
| 111 | + disableMetricsCollection(groupName) |
| 112 | + |
| 113 | + println("**** Delete the Auto Scaling group") |
| 114 | + deleteSpecificAutoScalingGroup(groupName) |
| 115 | + } |
| 116 | + |
| 117 | + private suspend fun getSecretValues(): String { |
| 118 | + val secretName = "test/autoscale" |
| 119 | + val valueRequest = |
| 120 | + GetSecretValueRequest { |
| 121 | + secretId = secretName |
| 122 | + } |
| 123 | + SecretsManagerClient { |
| 124 | + region = "us-east-1" |
| 125 | + }.use { secretClient -> |
| 126 | + val valueResponse = secretClient.getSecretValue(valueRequest) |
| 127 | + return valueResponse.secretString.toString() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Nested |
| 132 | + @DisplayName("A class used to get test values from test/autoscale (an AWS Secrets Manager secret)") |
| 133 | + internal class SecretValues { |
| 134 | + val groupName: String? = null |
| 135 | + val groupNameSc: String? = null |
| 136 | + val launchTemplateName: String? = null |
| 137 | + val vpcZoneId: String? = null |
| 138 | + val serviceLinkedRoleARN: String? = null |
| 139 | + } |
| 140 | +} |
0 commit comments