forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform_ssh_example_test.go
169 lines (134 loc) · 6.32 KB
/
terraform_ssh_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package test
import (
"fmt"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
)
// An example of how to test the Terraform module in examples/terraform-ssh-example using Terratest. The test also
// shows an example of how to break a test down into "stages" so you can skip stages by setting environment variables
// (e.g., skip stage "teardown" by setting the environment variable "SKIP_teardown=true"), which speeds up iteration
// when running this test over and over again locally.
func TestTerraformSshExample(t *testing.T) {
t.Parallel()
exampleFolder := "../examples/terraform-ssh-example"
// At the end of the test, run `terraform destroy` to clean up any resources that were created
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)
})
// Deploy the example
test_structure.RunTestStage(t, "setup", func() {
terraformOptions, keyPair := configureTerraformOptions(t, exampleFolder)
// Save the options and key pair so later test stages can use them
test_structure.SaveTerraformOptions(t, exampleFolder, terraformOptions)
test_structure.SaveEc2KeyPair(t, exampleFolder, keyPair)
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
})
// Make sure we can SSH to the public Instance directly from the public Internet and the private Instance by using
// the public Instance as a jump host
test_structure.RunTestStage(t, "validate", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, exampleFolder)
keyPair := test_structure.LoadEc2KeyPair(t, exampleFolder)
testSshToPublicHost(t, terraformOptions, keyPair)
testSshToPrivateHost(t, terraformOptions, keyPair)
})
}
func configureTerraformOptions(t *testing.T, exampleFolder string) (*terraform.Options, *aws.Ec2Keypair) {
// A unique ID we can use to namespace resources so we don't clash with anything already in the AWS account or
// tests running in parallel
uniqueId := random.UniqueId()
// Give this EC2 Instance and other resources in the Terraform code a name with a unique ID so it doesn't clash
// with anything else in the AWS account.
instanceName := fmt.Sprintf("terratest-ssh-example-%s", uniqueId)
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := aws.GetRandomRegion(t, nil, nil)
// Create an EC2 KeyPair that we can use for SSH access
keyPairName := fmt.Sprintf("terratest-ssh-example-%s", uniqueId)
keyPair := aws.CreateAndImportEC2KeyPair(t, awsRegion, keyPairName)
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: exampleFolder,
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"aws_region": awsRegion,
"instance_name": instanceName,
"key_pair_name": keyPairName,
},
}
return terraformOptions, keyPair
}
func testSshToPublicHost(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair) {
// Run `terraform output` to get the value of an output variable
publicInstanceIp := terraform.Output(t, terraformOptions, "public_instance_ip")
// We're going to try to SSH to the instance IP, using the Key Pair we created earlier, and the user "ubuntu",
// as we know the Instance is running an Ubuntu AMI that has such a user
publicHost := ssh.Host{
Hostname: publicInstanceIp,
SshKeyPair: keyPair.KeyPair,
SshUserName: "ubuntu",
}
// It can take a minute or so for the Instance to boot up, so retry a few times
maxRetries := 15
timeBetweenRetries := 5 * time.Second
description := fmt.Sprintf("SSH to public host %s", publicInstanceIp)
// Run a simple echo command on the server
expectedText := "Hello, World"
command := fmt.Sprintf("echo -n '%s'", expectedText)
// Verify that we can SSH to the Instance and run commands
retry.DoWithRetry(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
actualText, err := ssh.CheckSshCommandE(t, publicHost, command)
if err != nil {
return "", err
}
if strings.TrimSpace(actualText) != expectedText {
return "", fmt.Errorf("Expected SSH command to return '%s' but got '%s'", expectedText, actualText)
}
return "", nil
})
}
func testSshToPrivateHost(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair) {
// Run `terraform output` to get the value of an output variable
publicInstanceIp := terraform.Output(t, terraformOptions, "public_instance_ip")
privateInstanceIp := terraform.Output(t, terraformOptions, "private_instance_ip")
// We're going to try to SSH to the private instance using the public instance as a jump host. For both instances,
// we are using the Key Pair we created earlier, and the user "ubuntu", as we know the Instances are running an
// Ubuntu AMI that has such a user
publicHost := ssh.Host{
Hostname: publicInstanceIp,
SshKeyPair: keyPair.KeyPair,
SshUserName: "ubuntu",
}
privateHost := ssh.Host{
Hostname: privateInstanceIp,
SshKeyPair: keyPair.KeyPair,
SshUserName: "ubuntu",
}
// It can take a minute or so for the Instance to boot up, so retry a few times
maxRetries := 15
timeBetweenRetries := 5 * time.Second
description := fmt.Sprintf("SSH to private host %s via public host %s", publicInstanceIp, privateInstanceIp)
// Run a simple echo command on the server
expectedText := "Hello, World"
command := fmt.Sprintf("echo -n '%s'", expectedText)
// Verify that we can SSH to the Instance and run commands
retry.DoWithRetry(t, description, maxRetries, timeBetweenRetries, func() (string, error) {
actualText, err := ssh.CheckPrivateSshConnectionE(t, publicHost, privateHost, command)
if err != nil {
return "", err
}
if strings.TrimSpace(actualText) != expectedText {
return "", fmt.Errorf("Expected SSH command to return '%s' but got '%s'", expectedText, actualText)
}
return "", nil
})
}