Skip to content

Commit

Permalink
Test fixes (#1275)
Browse files Browse the repository at this point in the history
* allow deprecated images in ami.go

* add another instance type option

* Lookup a valid rds engine version rather than hard code

* a few more rds test fixes

* No need to test that AWS is doing AWS correctly

* upgrade to supported version of ubuntu

* update ubuntu-test docker image

* reference new tags

* expand default instance type options

* enforce default subnets

* increase timeouts, set specific ubuntu version
  • Loading branch information
Andrew Ellison authored Apr 27, 2023
1 parent 6c49de6 commit 0d72561
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 118 deletions.
4 changes: 2 additions & 2 deletions examples/packer-docker-example/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"filters": {
"virtualization-type": "hvm",
"architecture": "x86_64",
"name": "*ubuntu-xenial-16.04-amd64-server-*",
"name": "*ubuntu-jammy-22.04-amd64-server-*",
"block-device-mapping.volume-type": "gp2",
"root-device-type": "ebs"
},
Expand All @@ -27,7 +27,7 @@
},{
"name": "ubuntu-docker",
"type": "docker",
"image": "gruntwork/ubuntu-test:16.04",
"image": "gruntwork/ubuntu-test:22.04",
"commit": true,
"changes": ["ENTRYPOINT [\"\"]"]
}],
Expand Down
4 changes: 2 additions & 2 deletions examples/packer-docker-example/build.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data "amazon-ami" "aws" {
filters = {
architecture = "x86_64"
"block-device-mapping.volume-type" = "gp2"
name = "*ubuntu-xenial-16.04-amd64-server-*"
name = "*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
Expand All @@ -48,7 +48,7 @@ source "amazon-ebs" "ubuntu-ami" {
source "docker" "ubuntu-docker" {
changes = ["ENTRYPOINT [\"\"]"]
commit = true
image = "gruntwork/ubuntu-test:16.04"
image = "gruntwork/ubuntu-test:22.04"
}

build {
Expand Down
2 changes: 1 addition & 1 deletion examples/packer-docker-example/configure-sinatra-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sudo apt-get update
sudo apt-get install -y make zlib1g-dev build-essential ruby ruby-dev

echo "Installing Sinatra"
sudo gem install sinatra json --no-rdoc --no-ri
sudo gem install sinatra json

echo "Moving $APP_RB_SRC to $APP_RB_DST"
mkdir -p "$(dirname "$APP_RB_DST")"
Expand Down
4 changes: 4 additions & 0 deletions examples/terraform-asg-scp-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,9 @@ data "aws_subnets" "default_subnets" {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
filter {
name = "defaultForAz"
values = [true]
}
}

8 changes: 6 additions & 2 deletions examples/terraform-redeploy-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ data "template_file" "user_data" {
}

# ---------------------------------------------------------------------------------------------------------------------
# FOR THIS EXAMPLE, WE JUST RUN A PLAIN UBUNTU 16.04 AMI
# FOR THIS EXAMPLE, WE JUST RUN A PLAIN UBUNTU 22.04 AMI
# ---------------------------------------------------------------------------------------------------------------------

data "aws_ami" "ubuntu" {
Expand All @@ -120,7 +120,7 @@ data "aws_ami" "ubuntu" {

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}

Expand Down Expand Up @@ -299,5 +299,9 @@ data "aws_subnets" "default" {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
filter {
name = "defaultForAz"
values = [true]
}
}

2 changes: 1 addition & 1 deletion examples/terraform-ssh-password-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ data "aws_ami" "ubuntu" {

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-*-amd64-server-*"]
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}

5 changes: 3 additions & 2 deletions modules/aws/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ func GetMostRecentAmiIdE(t testing.TestingT, region string, ownerId string, filt
}

input := ec2.DescribeImagesInput{
Filters: ec2Filters,
Owners: []*string{aws.String(ownerId)},
Filters: ec2Filters,
IncludeDeprecated: aws.Bool(true),
Owners: []*string{aws.String(ownerId)},
}

out, err := ec2Client.DescribeImages(&input)
Expand Down
2 changes: 1 addition & 1 deletion modules/aws/asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func createTestEC2Instance(t *testing.T, region string, name string) ec2.Instanc
imageID := GetAmazonLinuxAmi(t, region)
params := &ec2.RunInstancesInput{
ImageId: aws.String(imageID),
InstanceType: aws.String("t2.micro"),
InstanceType: aws.String(GetRecommendedInstanceType(t, region, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})),
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
}
Expand Down
25 changes: 25 additions & 0 deletions modules/aws/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,31 @@ func instanceTypeExistsForEngineAndRegionE(client *rds.RDS, engine string, engin
return false, nil
}

// GetValidEngineVersion returns a string containing a valid RDS engine version for the provided region and engine type.
// This function will fail the test if no valid engine is found.
func GetValidEngineVersion(t testing.TestingT, region string, engine string, majorVersion string) string {
out, err := GetValidEngineVersionE(t, region, engine, majorVersion)
require.NoError(t, err)
return out
}

// GetValidEngineVersionE returns a string containing a valid RDS engine version or an error if no valid version is found.
func GetValidEngineVersionE(t testing.TestingT, region string, engine string, majorVersion string) (string, error) {
client, err := NewRdsClientE(t, region)
if err != nil {
return "", err
}
input := rds.DescribeDBEngineVersionsInput{
Engine: aws.String(engine),
EngineVersion: aws.String(majorVersion),
}
out, err := client.DescribeDBEngineVersions(&input)
if err != nil || len(out.DBEngineVersions) == 0 {
return "", err
}
return *out.DBEngineVersions[0].EngineVersion, nil
}

// ParameterForDbInstanceNotFound is an error that occurs when the parameter group specified is not found for the DB instance
type ParameterForDbInstanceNotFound struct {
ParameterName string
Expand Down
66 changes: 33 additions & 33 deletions modules/aws/rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,46 @@ import (

func TestGetRecommendedRdsInstanceTypeHappyPath(t *testing.T) {
type TestingScenerios struct {
name string
region string
databaseEngine string
databaseEngineVersion string
instanceTypes []string
expected string
name string
region string
databaseEngine string
engineMajorVersion string
instanceTypes []string
expected string
}

testingScenerios := []TestingScenerios{
{
name: "US region, mysql, first offering available",
region: "us-east-2",
databaseEngine: "mysql",
databaseEngineVersion: "8.0.32",
instanceTypes: []string{"db.t2.micro", "db.t3.micro"},
expected: "db.t2.micro",
name: "US region, mysql, first offering available",
region: "us-east-2",
databaseEngine: "mysql",
engineMajorVersion: "8.0",
instanceTypes: []string{"db.t2.micro", "db.t3.micro", "db.t3.small"},
expected: "db.t2.micro",
},
{
name: "EU region, postgres, 2nd offering available based on region",
region: "eu-north-1",
databaseEngine: "postgres",
databaseEngineVersion: "13.5",
instanceTypes: []string{"db.t2.micro", "db.m5.large"},
expected: "db.m5.large",
name: "EU region, postgres, 2nd offering available based on region",
region: "eu-north-1",
databaseEngine: "postgres",
engineMajorVersion: "13",
instanceTypes: []string{"db.t2.micro", "db.m5.large"},
expected: "db.m5.large",
},
{
name: "US region, oracle-ee, 2nd offering available based on db type",
region: "us-west-2",
databaseEngine: "oracle-ee",
databaseEngineVersion: "19.0.0.0.ru-2021-01.rur-2021-01.r1",
instanceTypes: []string{"db.m5d.xlarge", "db.m5.large"},
expected: "db.m5.large",
name: "US region, oracle-ee, 2nd offering available based on db type",
region: "us-west-2",
databaseEngine: "oracle-ee",
engineMajorVersion: "19",
instanceTypes: []string{"db.m5d.xlarge", "db.m5.large"},
expected: "db.m5d.xlarge",
},
{
name: "US region, oracle-ee, 2nd offering available based on db engine version",
region: "us-west-2",
databaseEngine: "oracle-ee",
databaseEngineVersion: "19.0.0.0.ru-2021-01.rur-2021-01.r1",
instanceTypes: []string{"db.t3.micro", "db.t3.small"},
expected: "db.t3.small",
name: "US region, oracle-ee, 2nd offering available based on db engine version",
region: "us-west-2",
databaseEngine: "oracle-ee",
engineMajorVersion: "19",
instanceTypes: []string{"db.t3.micro", "db.t3.small"},
expected: "db.t3.small",
},
}

Expand All @@ -57,8 +57,8 @@ func TestGetRecommendedRdsInstanceTypeHappyPath(t *testing.T) {

t.Run(scenerio.name, func(t *testing.T) {
t.Parallel()

actual, err := GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, scenerio.databaseEngineVersion, scenerio.instanceTypes)
engineVersion := GetValidEngineVersion(t, scenerio.region, scenerio.databaseEngine, scenerio.engineMajorVersion)
actual, err := GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, engineVersion, scenerio.instanceTypes)
assert.NoError(t, err)
assert.Equal(t, scenerio.expected, actual)
})
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestGetRecommendedRdsInstanceTypeErrors(t *testing.T) {
region: "us-east-1",
databaseEngine: "oracle-ee",
databaseEngineVersion: "19.0.0.0.ru-2021-01.rur-2021-01.r1",
instanceTypes: []string{"db.r5d.large"},
instanceTypes: []string{"db.r5a.large"},
},
{
name: "No instance type available for engine version",
Expand Down
45 changes: 0 additions & 45 deletions modules/aws/vpc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aws

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -81,25 +80,13 @@ func TestGetDefaultSubnetIDsForVpc(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
defaultVpcBeforeSubnetCreation := GetDefaultVpc(t, region)

// Creates a subnet in the default VPC with deferred deletion
// and fetches vpc object again
subnetName := fmt.Sprintf("%s-subnet", t.Name())
subnet := createPrivateSubnetInDefaultVpc(t, defaultVpcBeforeSubnetCreation.Id, subnetName, region)
defer deleteSubnet(t, *subnet.SubnetId, region)
defaultVpc := GetDefaultVpc(t, region)

defaultSubnetIDs := GetDefaultSubnetIDsForVpc(t, *defaultVpc)
assert.NotEmpty(t, defaultSubnetIDs)
// Checks that the amount of default subnets is smaller than
// total number of subnets in default vpc
assert.True(t, len(defaultSubnetIDs) < len(defaultVpc.Subnets))

availabilityZones := []string{}
for _, id := range defaultSubnetIDs {
// check if the recently created subnet does not come up here
assert.NotEqual(t, id, subnet.SubnetId)
// default subnets are by default public
// https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html
assert.True(t, IsPublicSubnet(t, id, region))
Expand Down Expand Up @@ -231,38 +218,6 @@ func createSubnet(t *testing.T, vpcId string, routeTableId string, region string
return *createSubnetOutput.Subnet
}

func createPrivateSubnetInDefaultVpc(t *testing.T, vpcId string, subnetName string, region string) ec2.Subnet {
ec2Client := NewEc2Client(t, region)

createSubnetOutput, err := ec2Client.CreateSubnet(&ec2.CreateSubnetInput{
CidrBlock: aws.String("172.31.172.0/24"),
VpcId: aws.String(vpcId),
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String("subnet"),
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String(subnetName),
},
},
},
},
})
require.NoError(t, err)

return *createSubnetOutput.Subnet
}

func deleteSubnet(t *testing.T, subnetId string, region string) {
ec2Client := NewEc2Client(t, region)

_, err := ec2Client.DeleteSubnet(&ec2.DeleteSubnetInput{
SubnetId: aws.String(subnetId),
})
require.NoError(t, err)
}

func createVpc(t *testing.T, region string) ec2.Vpc {
ec2Client := NewEc2Client(t, region)

Expand Down
2 changes: 1 addition & 1 deletion modules/slack/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestValidateSlackMessage(t *testing.T) {
retry.DoWithRetry(
t,
"wait for slack message",
6, 10*time.Second,
10, 10*time.Second,
func() (string, error) {
err := ValidateExpectedSlackMessageE(t, token, channelID, msgTxt, 10, 5*time.Minute)
return "", err
Expand Down
17 changes: 6 additions & 11 deletions test-docker-images/gruntwork-ubuntu-test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:18.04
FROM ubuntu:22.04

# Reduce Docker image size per https://blog.replicated.com/refactoring-a-dockerfile-for-image-size/
# - dnsutils: Install handy DNS checking tools like dig
Expand All @@ -15,8 +15,8 @@ RUN DEBIAN_FRONTEND=noninteractive \
dnsutils \
jq \
libcrypt-hcesha-perl \
python \
python-pip \
python3 \
python3-pip \
rsyslog \
software-properties-common \
sudo \
Expand All @@ -25,14 +25,9 @@ RUN DEBIAN_FRONTEND=noninteractive \
rm -rf /var/lib/apt/lists/*

# Install the AWS CLI per https://docs.aws.amazon.com/cli/latest/userguide/installing.html. The last line upgrades pip
# to the latest version. Note that we need to remove python-pip before we can use the updated pip, as pip does not
# automatically remove the ubuntu managed pip. We also need to refresh the cached pip path in the current bash session so
# that it picks up the new pip.
RUN pip install --upgrade setuptools && \
pip install --upgrade pip && \
apt-get remove -y python-pip python-pip-whl && \
hash pip && \
pip install awscli --upgrade
# to the latest version.
RUN pip3 install --upgrade setuptools && \
pip3 install awscli --upgrade

# Install the latest version of Docker, Consumer Edition
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
Expand Down
6 changes: 3 additions & 3 deletions test/packer_basic_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPackerBasicExample(t *testing.T) {
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})

// website::tag::1::Read Packer's template and set AWS Region variable.
packerOptions := &packer.Options{
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestPackerBasicExampleWithVarFile(t *testing.T) {
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})

// Create temporary packer variable file to store aws region
varFile, err := ioutil.TempFile("", "*.json")
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestPackerMultipleConcurrentAmis(t *testing.T) {
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})

packerOptions := &packer.Options{
// The path to where the Packer template is located
Expand Down
2 changes: 1 addition & 1 deletion test/terraform_aws_example_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestTerraformAwsExamplePlan(t *testing.T) {
awsRegion := aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})
instanceType := aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})

// website::tag::1::Configure Terraform setting path to Terraform code, EC2 instance name, and AWS Region. We also
// configure the options with default retryable errors to handle the most common retryable errors encountered in
Expand Down
Loading

0 comments on commit 0d72561

Please sign in to comment.