forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packer_basic_example_test.go
125 lines (104 loc) · 3.74 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
package test
import (
"fmt"
"testing"
"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"
)
// 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",
}
// 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",
}
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)
}
}