Skip to content

Commit 10bfae2

Browse files
committed
Merge pull request #3 from gruntwork-io/refine-package-interface-2
Refine package interface 2
2 parents 57e5784 + 23bf240 commit 10bfae2

8 files changed

Lines changed: 150 additions & 95 deletions

File tree

apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func Apply(ao *ApplyOptions) (string, error) {
3333

3434
// Apply the Terraform template
3535
logger.Println("Running terraform apply...")
36-
if ao.AttemptTerraformRetry {
36+
if len(ao.RetryableTerraformErrors) > 0 {
3737
output, err = terraform.ApplyAndGetOutputWithRetry(ao.TemplatePath, ao.Vars, ao.RetryableTerraformErrors, logger)
3838
} else {
3939
output, err = terraform.ApplyAndGetOutput(ao.TemplatePath, ao.Vars, logger)

apply_options.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type ApplyOptions struct {
66
TestName string // the name of the test to run, for logging purposes.
77
TemplatePath string // the relative or absolute path to the terraform template to be applied.
88
Vars map[string]string // the vars to pass to the terraform template.
9-
AttemptTerraformRetry bool // if true, if a known error message occurs, automatically attempt a retry.
109
RetryableTerraformErrors map[string]string // a map of error messages which we expect on this template and which should invoke a second terraform apply, along with an additional message offering details on this error text.
1110
TfRemoteStateS3BucketName string // S3 bucket name where terraform.tfstate files should be stored for running any terraform runs. Bucket should already exist.
1211
TfRemoteStateS3BucketRegion string // AWS Region where the TfRemoteStateS3BucketName exists.

util.go renamed to rand_resources.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gruntwork-io/terratest/aws"
88
"github.com/gruntwork-io/terratest/util"
9+
"strings"
910
)
1011

1112
// A RandomResourceCollection is simply a typed holder for random resources we need as we do a Terraform run.
@@ -28,7 +29,7 @@ type RandomResourceCollectionOpts struct {
2829
ForbiddenRegions []string // A list of strings
2930
}
3031

31-
func CreateRandomResourceCollectionOptions() *RandomResourceCollectionOpts {
32+
func NewRandomResourceCollectionOptions() *RandomResourceCollectionOpts {
3233
return &RandomResourceCollectionOpts{}
3334
}
3435

@@ -65,10 +66,30 @@ func CreateRandomResourceCollection(ro *RandomResourceCollectionOpts) (*RandomRe
6566
}
6667

6768
// Destroy any persistent resources referenced in the given RandomResourceCollection.
68-
func (r *RandomResourceCollection) DestroyResources() (error) {
69+
func (r *RandomResourceCollection) DestroyResources() error {
6970
if r != nil && r.AwsRegion != "" && r.KeyPair.Name != "" {
7071
return aws.DeleteEC2KeyPair(r.AwsRegion, r.KeyPair.Name)
7172
} else {
7273
return nil
7374
}
75+
}
76+
77+
// Return the AWS Availability Zones for a given AWS region
78+
func (r *RandomResourceCollection) FetchAwsAvailabilityZones() []string {
79+
if r != nil && r.AwsRegion != "" {
80+
return aws.GetAvailabilityZones(r.AwsRegion)
81+
}
82+
return nil
83+
}
84+
85+
// Return the AWS Availability Zones as a list of comma-separated values
86+
func (r *RandomResourceCollection) FetchAwsAvailabilityZonesAsString() string {
87+
if r != nil && r.AwsRegion != "" {
88+
return strings.Join(aws.GetAvailabilityZones(r.AwsRegion), ",")
89+
}
90+
return ""
91+
}
92+
93+
func (r *RandomResourceCollection) GetRandomPrivateCidrBlock(prefix int) string {
94+
return util.GetRandomPrivateCidrBlock(prefix)
7495
}

rand_resources_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Integration tests that test cross-package functionality in AWS.
2+
package terratest
3+
4+
import (
5+
"testing"
6+
)
7+
8+
func TestCreateRandomResourceCollectionOptionsForbiddenRegionsWorks(t *testing.T) {
9+
t.Parallel()
10+
11+
ro := NewRandomResourceCollectionOptions()
12+
13+
// Specify every region but us-east-1
14+
ro.ForbiddenRegions = []string{
15+
"us-west-1",
16+
"us-west-2",
17+
"eu-west-1",
18+
"eu-central-1",
19+
"ap-northeast-1",
20+
"ap-northeast-2",
21+
"ap-southeast-1",
22+
"ap-southeast-2",
23+
"sa-east-1"}
24+
25+
rand, err := CreateRandomResourceCollection(ro)
26+
if err != nil {
27+
t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error())
28+
}
29+
30+
if rand.AwsRegion != "us-east-1" {
31+
t.Fatalf("Failed to correctly forbid AWS regions. Only valid response should have been us-east-1, but was: %s", rand.AwsRegion)
32+
}
33+
}
34+
35+
func TestFetchAwsAvailabilityZones(t *testing.T) {
36+
t.Parallel()
37+
38+
ro := NewRandomResourceCollectionOptions()
39+
rand, err := CreateRandomResourceCollection(ro)
40+
if err != nil {
41+
t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error())
42+
}
43+
44+
// Manually set the AWS Region to us-west-2 for testing purposes
45+
rand.AwsRegion = "us-west-2"
46+
actual := rand.FetchAwsAvailabilityZones()
47+
expected := []string{"us-west-2a","us-west-2b","us-west-2c"}
48+
//expected := []string{"us-west-2a,us-west-2b,us-west-2c"}
49+
50+
for index,_ := range expected {
51+
if actual[index] != expected[index] {
52+
t.Fatalf("Expected: %s, but received %s", expected[index], actual[index])
53+
}
54+
}
55+
}
56+
57+
func TestFetchAwsAvailabilityZonesAsString(t *testing.T) {
58+
t.Parallel()
59+
60+
ro := NewRandomResourceCollectionOptions()
61+
rand, err := CreateRandomResourceCollection(ro)
62+
if err != nil {
63+
t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error())
64+
}
65+
66+
// Manually set the AWS Region to us-west-2 for testing purposes
67+
rand.AwsRegion = "us-west-2"
68+
actual := rand.FetchAwsAvailabilityZonesAsString()
69+
expected := "us-west-2a,us-west-2b,us-west-2c"
70+
71+
if actual != expected {
72+
t.Fatalf("Expected: %s, but received %s", expected, actual)
73+
}
74+
}
75+
76+
func TestGetRandomPrivateCidrBlock(t *testing.T) {
77+
t.Parallel()
78+
79+
ro := NewRandomResourceCollectionOptions()
80+
rand, err := CreateRandomResourceCollection(ro)
81+
if err != nil {
82+
t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error())
83+
}
84+
85+
actual := rand.GetRandomPrivateCidrBlock(18)
86+
actualPrefix := string(actual[len(actual)-3:])
87+
expPrefix := "/18"
88+
89+
if actualPrefix != expPrefix {
90+
t.Fatalf("Expected: %s, but received: %s", expPrefix, actualPrefix)
91+
}
92+
}

terratest_test.go

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestUploadKeyPair(t *testing.T) {
3636
func TestTerraformApplyOnMinimalExample(t *testing.T) {
3737
t.Parallel()
3838

39-
ro := CreateRandomResourceCollectionOptions()
39+
ro := NewRandomResourceCollectionOptions()
4040
rand, err := CreateRandomResourceCollection(ro)
4141
defer rand.DestroyResources()
4242
if err != nil {
@@ -54,36 +54,6 @@ func TestTerraformApplyOnMinimalExample(t *testing.T) {
5454
ao.TestName = "Test - TestTerraformApplyOnMinimalExample"
5555
ao.TemplatePath = path.Join(fixtureDir, "minimal-example")
5656
ao.Vars = vars
57-
ao.AttemptTerraformRetry = false
58-
59-
_, err = ApplyAndDestroy(ao)
60-
if err != nil {
61-
t.Fatalf("Failed to ApplyAndDestroy: %s", err.Error())
62-
}
63-
}
64-
65-
func TestTerraformApplyOnMinimalExampleWithRetry(t *testing.T) {
66-
t.Parallel()
67-
68-
ro := CreateRandomResourceCollectionOptions()
69-
rand, err := CreateRandomResourceCollection(ro)
70-
defer rand.DestroyResources()
71-
if err != nil {
72-
t.Errorf("Failed to create random resource collection: %s\n", err.Error())
73-
}
74-
75-
vars := make(map[string]string)
76-
vars["aws_region"] = rand.AwsRegion
77-
vars["ec2_key_name"] = rand.KeyPair.Name
78-
vars["ec2_instance_name"] = rand.UniqueId
79-
vars["ec2_image"] = rand.AmiId
80-
81-
ao := NewApplyOptions()
82-
ao.UniqueId = rand.UniqueId
83-
ao.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetry"
84-
ao.TemplatePath = path.Join(fixtureDir, "minimal-example")
85-
ao.Vars = vars
86-
ao.AttemptTerraformRetry = true
8757

8858
_, err = ApplyAndDestroy(ao)
8959
if err != nil {
@@ -94,7 +64,7 @@ func TestTerraformApplyOnMinimalExampleWithRetry(t *testing.T) {
9464
func TestApplyOrDestroyFailsOnTerraformError(t *testing.T) {
9565
t.Parallel()
9666

97-
ro := CreateRandomResourceCollectionOptions()
67+
ro := NewRandomResourceCollectionOptions()
9868
rand, err := CreateRandomResourceCollection(ro)
9969
defer rand.DestroyResources()
10070
if err != nil {
@@ -112,7 +82,6 @@ func TestApplyOrDestroyFailsOnTerraformError(t *testing.T) {
11282
ao.TestName = "Test - TestApplyOrDestroyFailsOnTerraformError"
11383
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
11484
ao.Vars = vars
115-
ao.AttemptTerraformRetry = true
11685

11786
_, err = ApplyAndDestroy(ao)
11887
if err != nil {
@@ -127,7 +96,7 @@ func TestApplyOrDestroyFailsOnTerraformError(t *testing.T) {
12796
func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages(t *testing.T) {
12897
t.Parallel()
12998

130-
ro := CreateRandomResourceCollectionOptions()
99+
ro := NewRandomResourceCollectionOptions()
131100
rand, err := CreateRandomResourceCollection(ro)
132101
defer rand.DestroyResources()
133102
if err != nil {
@@ -145,7 +114,6 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages(t *testing.T)
145114
ao.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages"
146115
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
147116
ao.Vars = vars
148-
ao.AttemptTerraformRetry = true
149117
ao.RetryableTerraformErrors = make(map[string]string)
150118
ao.RetryableTerraformErrors["aws_instance.demo: Error launching source instance: InvalidKeyPair.NotFound"] = "This error was deliberately added to the template."
151119

@@ -166,7 +134,7 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessages(t *testing.T)
166134
func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry(t *testing.T) {
167135
t.Parallel()
168136

169-
ro := CreateRandomResourceCollectionOptions()
137+
ro := NewRandomResourceCollectionOptions()
170138
rand, err := CreateRandomResourceCollection(ro)
171139
defer rand.DestroyResources()
172140
if err != nil {
@@ -184,7 +152,6 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry(t
184152
ao.TestName = "Test - TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry"
185153
ao.TemplatePath = path.Join(fixtureDir, "minimal-example-with-error")
186154
ao.Vars = vars
187-
ao.AttemptTerraformRetry = true
188155
ao.RetryableTerraformErrors = make(map[string]string)
189156
ao.RetryableTerraformErrors["I'm a message that shouldn't show up in the output"] = ""
190157

@@ -203,7 +170,7 @@ func TestTerraformApplyOnMinimalExampleWithRetryableErrorMessagesDoesNotRetry(t
203170
func TestTerraformApplyAvoidsForbiddenRegion(t *testing.T) {
204171
t.Parallel()
205172

206-
ro := CreateRandomResourceCollectionOptions()
173+
ro := NewRandomResourceCollectionOptions()
207174

208175
// Specify every region but us-east-1
209176
ro.ForbiddenRegions = []string{
@@ -234,7 +201,6 @@ func TestTerraformApplyAvoidsForbiddenRegion(t *testing.T) {
234201
ao.TestName = "Test - TestTerraformApplyAvoidsForbiddenRegion"
235202
ao.TemplatePath = path.Join(fixtureDir, "minimal-example")
236203
ao.Vars = vars
237-
ao.AttemptTerraformRetry = false
238204

239205
_, err = ApplyAndDestroy(ao)
240206
if err != nil {

util/network.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ func GetRandomPrivateCidrBlock(routingPrefix int) string {
3333
o4 = Random(0, 255)
3434
}
3535

36-
case 31:
37-
fallthrough
38-
case 30:
39-
fallthrough
40-
case 29:
41-
fallthrough
42-
case 28:
43-
fallthrough
44-
case 27:
45-
fallthrough
46-
case 26:
47-
fallthrough
48-
case 25:
36+
case 31, 30, 29, 28, 27, 26, 25:
4937
fallthrough
5038
case 24:
5139
o1 = RandomInt([]int{10, 172, 192})
@@ -56,17 +44,34 @@ func GetRandomPrivateCidrBlock(routingPrefix int) string {
5644
o3 = Random(0, 255)
5745
o4 = 0
5846
case 172:
59-
o2 = Random(16, 31)
60-
o3 = Random(0, 255)
47+
o2 = 16
48+
o3 = 0
6149
o4 = 0
6250
case 192:
6351
o2 = 168
64-
o3 = Random(0, 255)
52+
o3 = 0
6553
o4 = 0
6654
}
55+
case 23, 22, 21, 20, 19:
56+
fallthrough
57+
case 18:
58+
o1 = RandomInt([]int{10, 172, 192})
6759

60+
switch o1 {
61+
case 10:
62+
o2 = 0
63+
o3 = 0
64+
o4 = 0
65+
case 172:
66+
o2 = 16
67+
o3 = 0
68+
o4 = 0
69+
case 192:
70+
o2 = 168
71+
o3 = 0
72+
o4 = 0
73+
}
6874
}
69-
7075
return fmt.Sprintf("%d.%d.%d.%d/%d", o1, o2, o3, o4, routingPrefix)
7176
}
7277

util/network_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ func TestGetFirstTwoOctets(t *testing.T) {
1414
}
1515

1616
// Deferred to save time
17-
func TestGetRandomPrivateCidrBlock(t *testing.T) {
18-
}
17+
//func TestGetRandomPrivateCidrBlock(t *testing.T) {
18+
// t.Parallel()
19+
//
20+
// for i := 0; i < 10; i++ {
21+
// fmt.Println(GetRandomPrivateCidrBlock(18))
22+
// }
23+
//}

util_test.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)