forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker_basic_example_test.go
210 lines (172 loc) · 7.27 KB
/
packer_basic_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
terratest_aws "github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/packer"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Occasionally, a Packer build may fail due to intermittent issues (e.g., brief network outage or EC2 issue). We try
// to make our tests resilient to that by specifying those known common errors here and telling our builds to retry if
// they hit those errors.
var DefaultRetryablePackerErrors = map[string]string{
"Script disconnected unexpectedly": "Occasionally, Packer seems to lose connectivity to AWS, perhaps due to a brief network outage",
"can not open /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_xenial_InRelease": "Occasionally, apt-get fails on ubuntu to update the cache",
}
var DefaultTimeBetweenPackerRetries = 15 * time.Second
const DefaultMaxPackerRetries = 3
// An example of how to test the Packer template in examples/packer-basic-example using Terratest.
func TestPackerBasicExample(t *testing.T) {
t.Parallel()
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)
packerOptions := &packer.Options{
// The path to where the Packer template is located
Template: "../examples/packer-basic-example/build.json",
// Variables to pass to our Packer build using -var options
Vars: map[string]string{
"aws_region": awsRegion,
},
// Only build the AWS AMI
Only: "amazon-ebs",
// Configure retries for intermittent errors
RetryableErrors: DefaultRetryablePackerErrors,
TimeBetweenRetries: DefaultTimeBetweenPackerRetries,
MaxRetries: DefaultMaxPackerRetries,
}
// Make sure the Packer build completes successfully
amiID := packer.BuildArtifact(t, packerOptions)
// Clean up the AMI after we're done
defer terratest_aws.DeleteAmiAndAllSnapshots(t, awsRegion, amiID)
// Check if AMI is shared/not shared with account
requestingAccount := terratest_aws.CanonicalAccountId
randomAccount := "123456789012" // Random Account
ec2Client := terratest_aws.NewEc2Client(t, awsRegion)
ShareAmi(t, amiID, requestingAccount, ec2Client)
accountsWithLaunchPermissions := terratest_aws.GetAccountsWithLaunchPermissionsForAmi(t, awsRegion, amiID)
assert.NotContains(t, accountsWithLaunchPermissions, randomAccount)
assert.Contains(t, accountsWithLaunchPermissions, requestingAccount)
// Check if AMI is public
MakeAmiPublic(t, amiID, ec2Client)
amiIsPublic := terratest_aws.GetAmiPubliclyAccessible(t, awsRegion, amiID)
assert.True(t, amiIsPublic)
}
// An example of how to test the Packer template in examples/packer-basic-example using Terratest
// with the VarFiles option. This test generates a temporary *.json file containing the value
// for the `aws_region` variable.
func TestPackerBasicExampleWithVarFile(t *testing.T) {
t.Parallel()
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)
// Create temporary packer variable file to store aws region
varFile, err := ioutil.TempFile("", "*.json")
require.NoError(t, err, "Did not expect temp file creation to cause error")
// Be sure to clean up temp file
defer os.Remove(varFile.Name())
// Write random generated aws region to temporary json file
varFileContent := []byte(fmt.Sprintf("{ \"aws_region\": \"%s\" }", awsRegion))
_, err = varFile.Write(varFileContent)
require.NoError(t, err, "Did not expect writing to temp file %s to cause error", varFile.Name())
packerOptions := &packer.Options{
// The path to where the Packer template is located
Template: "../examples/packer-basic-example/build.json",
// Variable file to to pass to our Packer build using -var-file option
VarFiles: []string{
varFile.Name(),
},
// Only build the AWS AMI
Only: "amazon-ebs",
// Configure retries for intermittent errors
RetryableErrors: DefaultRetryablePackerErrors,
TimeBetweenRetries: DefaultTimeBetweenPackerRetries,
MaxRetries: DefaultMaxPackerRetries,
}
// Make sure the Packer build completes successfully
amiID := packer.BuildArtifact(t, packerOptions)
// Clean up the AMI after we're done
defer terratest_aws.DeleteAmiAndAllSnapshots(t, awsRegion, amiID)
// Check if AMI is shared/not shared with account
requestingAccount := terratest_aws.CanonicalAccountId
randomAccount := "123456789012" // Random Account
ec2Client := terratest_aws.NewEc2Client(t, awsRegion)
ShareAmi(t, amiID, requestingAccount, ec2Client)
accountsWithLaunchPermissions := terratest_aws.GetAccountsWithLaunchPermissionsForAmi(t, awsRegion, amiID)
assert.NotContains(t, accountsWithLaunchPermissions, randomAccount)
assert.Contains(t, accountsWithLaunchPermissions, requestingAccount)
// Check if AMI is public
MakeAmiPublic(t, amiID, ec2Client)
amiIsPublic := terratest_aws.GetAmiPubliclyAccessible(t, awsRegion, amiID)
assert.True(t, amiIsPublic)
}
func TestPackerMultipleConcurrentAmis(t *testing.T) {
t.Parallel()
// Build a map of 3 randomId <-> packer.Options, in 3 random AWS Regions
// then build all of these AMIs in parallel and make sure that there are
// no errors.
var identifierToOptions = map[string]*packer.Options{}
for i := 0; i < 3; i++ {
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)
packerOptions := &packer.Options{
// The path to where the Packer template is located
Template: "../examples/packer-basic-example/build.json",
// Variables to pass to our Packer build using -var options
Vars: map[string]string{
"aws_region": awsRegion,
"ami_base_name": fmt.Sprintf("%s-terratest-packer", random.UniqueId()),
},
// Only build the AWS AMI
Only: "amazon-ebs",
// Configure retries for intermittent errors
RetryableErrors: DefaultRetryablePackerErrors,
TimeBetweenRetries: DefaultTimeBetweenPackerRetries,
MaxRetries: DefaultMaxPackerRetries,
}
identifierToOptions[random.UniqueId()] = packerOptions
}
resultMap := packer.BuildArtifacts(t, identifierToOptions)
// Clean up the AMIs after we're done
for key, amiId := range resultMap {
awsRegion := identifierToOptions[key].Vars["aws_region"]
terratest_aws.DeleteAmiAndAllSnapshots(t, awsRegion, amiId)
}
}
func ShareAmi(t *testing.T, amiID string, accountID string, ec2Client *ec2.EC2) {
input := &ec2.ModifyImageAttributeInput{
ImageId: aws.String(amiID),
LaunchPermission: &ec2.LaunchPermissionModifications{
Add: []*ec2.LaunchPermission{
{
UserId: aws.String(accountID),
},
},
},
}
_, err := ec2Client.ModifyImageAttribute(input)
if err != nil {
t.Fatal(err)
}
}
func MakeAmiPublic(t *testing.T, amiID string, ec2Client *ec2.EC2) {
input := &ec2.ModifyImageAttributeInput{
ImageId: aws.String(amiID),
LaunchPermission: &ec2.LaunchPermissionModifications{
Add: []*ec2.LaunchPermission{
{
Group: aws.String("all"),
},
},
},
}
_, err := ec2Client.ModifyImageAttribute(input)
if err != nil {
t.Fatal(err)
}
}