Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #80 from hashicorp/test-download-url
Browse files Browse the repository at this point in the history
Add test for consul enterprise installation
  • Loading branch information
Etiene authored Aug 30, 2018
2 parents f57c640 + a6a1178 commit ca3600a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/consul-ami/consul.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"variables": {
"aws_region": "us-east-1",
"consul_version": "1.2.2",
"download_url": ""
"download_url": "{{env `CONSUL_DOWNLOAD_URL`}}"
},
"builders": [{
"name": "ubuntu16-ami",
Expand Down Expand Up @@ -51,7 +51,7 @@
},{
"type": "shell",
"inline": [
"if [[ -n '{{user `download_url`}}' ]]; then",
"if test -n \"{{user `download_url`}}\"; then",
" /tmp/terraform-aws-consul/modules/install-consul/install-consul --download-url {{user `download_url`}};",
"else",
" /tmp/terraform-aws-consul/modules/install-consul/install-consul --version {{user `consul_version`}};",
Expand Down
9 changes: 7 additions & 2 deletions modules/install-consul/install-consul
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function fetch_binary {
download_url="https://releases.hashicorp.com/consul/${version}/consul_${version}_linux_amd64.zip"
fi

log_info "Downloading Consul from $download_url to $DOWNLOAD_PACKAGE_PATH"
log_info "Downloading Consul to $DOWNLOAD_PACKAGE_PATH"
curl -o "$DOWNLOAD_PACKAGE_PATH" "$download_url" --location --silent --fail --show-error
}

Expand Down Expand Up @@ -334,7 +334,12 @@ function install {
install_tls_certificates "$path" "$user" "$ca_file_path" "$cert_file_path" "$key_file_path"
fi

log_info "Consul install complete!"
if command -v consul; then
log_info "Consul install complete!";
else
log_info "Could not find consul command. Aborting.";
exit 1;
fi
}

install "$@"
5 changes: 2 additions & 3 deletions test/consul_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
// Test the example in the root folder
func TestConsulClusterWithUbuntuAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "ubuntu16-ami", ".", "../examples/consul-ami/consul.json")
runConsulClusterTest(t, "ubuntu16-ami", ".", "../examples/consul-ami/consul.json", "ubuntu", "")
}

// Test the example in the root folder
func TestConsulClusterWithAmazonLinuxAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "amazon-linux-ami", ".", "../examples/consul-ami/consul.json")
runConsulClusterTest(t, "amazon-linux-ami", ".", "../examples/consul-ami/consul.json", "ec2-user", "")
}

6 changes: 2 additions & 4 deletions test/consul_cluster_with_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import "testing"

func TestConsulClusterWithEncryptionUbuntuAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "ubuntu16-ami", "examples/example-with-encryption", "../examples/example-with-encryption/packer/consul-with-certs.json")
runConsulClusterTest(t, "ubuntu16-ami", "examples/example-with-encryption", "../examples/example-with-encryption/packer/consul-with-certs.json", "ubuntu", "")
}

func TestConsulClusterWithEncryptionAmazonLinuxAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "amazon-linux-ami", "examples/example-with-encryption", "../examples/example-with-encryption/packer/consul-with-certs.json")
runConsulClusterTest(t, "amazon-linux-ami", "examples/example-with-encryption", "../examples/example-with-encryption/packer/consul-with-certs.json", "ec2-user", "")
}


27 changes: 27 additions & 0 deletions test/consul_enterprise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test

import (
"os"
"testing"
)

// Test the example in the root folder
func TestConsulInstallFromURLWithUbuntuAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "ubuntu16-ami", ".", "../examples/consul-ami/consul.json", "ubuntu", getUrlFromEnv(t))
}

func TestConsulInstallFromURLWithAmazonLinuxAmi(t *testing.T) {
t.Parallel()
runConsulClusterTest(t, "amazon-linux-ami", ".", "../examples/consul-ami/consul.json", "ec2-user", getUrlFromEnv(t))
}

// To test this on circle ci you need a url set as an environment variable, CONSUL_AMI_TEMPLATE_VAR_DOWNLOAD_URL
// which you would also have to set locally if you want to run this test locally.
func getUrlFromEnv(t *testing.T) string {
url := os.Getenv("CONSUL_AMI_TEMPLATE_VAR_DOWNLOAD_URL")
if url == "" {
t.Fatalf("Please set the environment variable CONSUL_AMI_TEMPLATE_VAR_DOWNLOAD_URL.\n")
}
return url
}
61 changes: 52 additions & 9 deletions test/consul_helpers.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package test

import (
"errors"
"fmt"
"strings"
"testing"
"time"
"fmt"
"github.com/hashicorp/consul/api"
"errors"
"github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/gruntwork-io/terratest/modules/terraform"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/hashicorp/consul/api"
)

const REPO_ROOT = "../"
const CONSUL_CLUSTER_EXAMPLE_VAR_AMI_ID = "ami_id"
const CONSUL_CLUSTER_EXAMPLE_VAR_SSH_KEY_NAME = "ssh_key_name"
const CONSUL_CLUSTER_EXAMPLE_VAR_CLUSTER_NAME = "cluster_name"
const CONSUL_CLUSTER_EXAMPLE_VAR_NUM_SERVERS = "num_servers"
const CONSUL_CLUSTER_EXAMPLE_VAR_NUM_CLIENTS = "num_clients"
Expand All @@ -37,21 +41,24 @@ const AWS_DEFAULT_REGION_ENV_VAR = "AWS_DEFAULT_REGION"
// 2. Building the AMI in the consul-ami example with the given build name
// 3. Deploying that AMI using the consul-cluster Terraform code
// 4. Checking that the Consul cluster comes up within a reasonable time period and can respond to requests
func runConsulClusterTest(t *testing.T, packerBuildName string, examplesFolder string, packerTemplatePath string) {
func runConsulClusterTest(t *testing.T, packerBuildName string, examplesFolder string, packerTemplatePath string, sshUser string, enterpriseUrl string) {
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, REPO_ROOT, examplesFolder)

test_structure.RunTestStage(t, "setup_ami", func() {
awsRegion := aws.GetRandomRegion(t, nil, nil)
test_structure.SaveString(t, exampleFolder, SAVED_AWS_REGION, awsRegion)

amiId := buildAmi(t, packerTemplatePath, packerBuildName, awsRegion)
amiId := buildAmi(t, packerTemplatePath, packerBuildName, awsRegion, enterpriseUrl)
test_structure.SaveAmiId(t, exampleFolder, amiId)
})

defer test_structure.RunTestStage(t, "teardown", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, exampleFolder)
terraform.Destroy(t, terraformOptions)

keyPair := test_structure.LoadEc2KeyPair(t, exampleFolder)
aws.DeleteEC2KeyPair(t, keyPair)

amiId := test_structure.LoadAmiId(t, exampleFolder)
awsRegion := test_structure.LoadString(t, exampleFolder, SAVED_AWS_REGION)
aws.DeleteAmi(t, awsRegion, amiId)
Expand All @@ -62,13 +69,17 @@ func runConsulClusterTest(t *testing.T, packerBuildName string, examplesFolder s
awsRegion := test_structure.LoadString(t, exampleFolder, SAVED_AWS_REGION)
amiId := test_structure.LoadAmiId(t, exampleFolder)

keyPair := aws.CreateAndImportEC2KeyPair(t, awsRegion, uniqueId)
test_structure.SaveEc2KeyPair(t, exampleFolder, keyPair)

terraformOptions := &terraform.Options{
TerraformDir: exampleFolder,
Vars: map[string]interface{}{
CONSUL_CLUSTER_EXAMPLE_VAR_CLUSTER_NAME: uniqueId,
CONSUL_CLUSTER_EXAMPLE_VAR_NUM_SERVERS: CONSUL_CLUSTER_EXAMPLE_DEFAULT_NUM_SERVERS,
CONSUL_CLUSTER_EXAMPLE_VAR_NUM_CLIENTS: CONSUL_CLUSTER_EXAMPLE_DEFAULT_NUM_CLIENTS,
CONSUL_CLUSTER_EXAMPLE_VAR_AMI_ID: amiId,
CONSUL_CLUSTER_EXAMPLE_VAR_SSH_KEY_NAME: keyPair.Name,
},
EnvVars: map[string]string{
AWS_DEFAULT_REGION_ENV_VAR: awsRegion,
Expand All @@ -79,9 +90,14 @@ func runConsulClusterTest(t *testing.T, packerBuildName string, examplesFolder s
terraform.InitAndApply(t, terraformOptions)
})

test_structure.RunTestStage(t,"validate", func() {
test_structure.RunTestStage(t, "validate", func() {
awsRegion := test_structure.LoadString(t, exampleFolder, SAVED_AWS_REGION)
terraformOptions := test_structure.LoadTerraformOptions(t, exampleFolder)
keyPair := test_structure.LoadEc2KeyPair(t, exampleFolder)

if len(enterpriseUrl) > 0 {
checkEnterpriseInstall(t, CONSUL_CLUSTER_EXAMPLE_OUTPUT_SERVER_ASG_NAME, terraformOptions, awsRegion, sshUser, keyPair)
}

// Check the Consul servers
checkConsulClusterIsWorking(t, CONSUL_CLUSTER_EXAMPLE_OUTPUT_SERVER_ASG_NAME, terraformOptions, awsRegion)
Expand Down Expand Up @@ -148,3 +164,30 @@ func createConsulClient(t *testing.T, ipAddress string) *api.Client {

return client
}

func checkEnterpriseInstall(t *testing.T, asgNameOutputVar string, terratestOptions *terraform.Options, awsRegion string, sshUser string, keyPair *aws.Ec2Keypair) {
asgName := terraform.OutputRequired(t, terratestOptions, asgNameOutputVar)
nodeIpAddress := getIpAddressOfAsgInstance(t, asgName, awsRegion)

host := ssh.Host{
Hostname: nodeIpAddress,
SshUserName: sshUser,
SshKeyPair: keyPair.KeyPair,
}

maxRetries := 10
sleepBetweenRetries := 10 * time.Second

output := retry.DoWithRetry(t, "Check Enterprise Install", maxRetries, sleepBetweenRetries, func() (string, error) {
out, err := ssh.CheckSshCommandE(t, host, "consul --help")
if err != nil {
return "", fmt.Errorf("Error running consul command: %s\n", err)
}

return out, nil
})

if !strings.Contains(output, "license") {
t.Fatalf("This consul package is not the enterprise version.\n")
}
}
12 changes: 8 additions & 4 deletions test/terratest_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/packer"
)

const CONSUL_AMI_TEMPLATE_VAR_REGION = "aws_region"
const CONSUL_AMI_TEMPLATE_VAR_DOWNLOAD_URL = "CONSUL_DOWNLOAD_URL"

// Use Packer to build the AMI in the given packer template, with the given build name, and return the AMI's ID
func buildAmi(t *testing.T, packerTemplatePath string, packerBuildName string, awsRegion string) string {
options := &packer.Options {
func buildAmi(t *testing.T, packerTemplatePath string, packerBuildName string, awsRegion string, downloadUrl string) string {
options := &packer.Options{
Template: packerTemplatePath,
Only: packerBuildName,
Only: packerBuildName,
Vars: map[string]string{
CONSUL_AMI_TEMPLATE_VAR_REGION: awsRegion,
},
Env: map[string]string{
CONSUL_AMI_TEMPLATE_VAR_DOWNLOAD_URL: downloadUrl,
},
}

return packer.BuildAmi(t, options)
}

0 comments on commit ca3600a

Please sign in to comment.