-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
209 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAcc_IamPolicy_DeleteByID(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping acceptance test.") | ||
} | ||
|
||
env := InitEnv(t) | ||
|
||
terraformDir := "./test-fixtures/iam-policy" | ||
|
||
terraformOptions := getTerraformOptions(terraformDir, env) | ||
|
||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
arn := terraform.Output(t, terraformOptions, "arn") | ||
assertIamPolicyExists(t, env, arn) | ||
|
||
id := terraform.Output(t, terraformOptions, "id") | ||
writeConfigID(t, terraformDir, "aws_iam_policy", id) | ||
|
||
defer os.Remove(terraformDir + "/config.yml") | ||
|
||
logBuffer, err := runBinary(t, terraformDir, "YES\n") | ||
require.NoError(t, err) | ||
|
||
assertIamPolicyDeleted(t, env, arn) | ||
|
||
fmt.Println(logBuffer) | ||
} | ||
|
||
func assertIamPolicyExists(t *testing.T, env EnvVars, arn string) { | ||
assert.True(t, iamPolicyExists(t, env, arn)) | ||
} | ||
|
||
func assertIamPolicyDeleted(t *testing.T, env EnvVars, arn string) { | ||
assert.False(t, iamPolicyExists(t, env, arn)) | ||
} | ||
|
||
func iamPolicyExists(t *testing.T, env EnvVars, arn string) bool { | ||
opts := &iam.GetPolicyInput{ | ||
PolicyArn: &arn, | ||
} | ||
|
||
_, err := env.AWSClient.IAMAPI.GetPolicy(opts) | ||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
t.Fatal() | ||
} | ||
if ec2err.Code() == "NoSuchEntity" { | ||
return false | ||
} | ||
t.Fatal(err) | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
provider "aws" { | ||
version = "~> 2.0" | ||
|
||
profile = var.profile | ||
region = var.region | ||
} | ||
|
||
terraform { | ||
# The configuration for this backend will be filled in by Terragrunt | ||
backend "s3" { | ||
} | ||
} | ||
|
||
resource "aws_iam_user" "test" { | ||
name = "awsweeper-test-acc" | ||
|
||
tags = { | ||
awsweeper = "test-acc" | ||
} | ||
} | ||
|
||
resource "aws_iam_role" "test" { | ||
name = "awsweeper-test-acc" | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "ec2.amazonaws.com" | ||
}, | ||
"Effect": "Allow", | ||
"Sid": "" | ||
} | ||
] | ||
} | ||
EOF | ||
|
||
tags = { | ||
awsweeper = "test-acc" | ||
} | ||
} | ||
|
||
resource "aws_iam_group" "test" { | ||
name = "awsweeper-test-acc" | ||
} | ||
|
||
resource "aws_iam_policy" "test" { | ||
name = "awsweeper-test-acc" | ||
description = "A test policy" | ||
|
||
policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"ec2:Describe*" | ||
], | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_policy_attachment" "test" { | ||
name = "awsweeper-test-acc" | ||
users = [aws_iam_user.test.name] | ||
roles = [aws_iam_role.test.name] | ||
groups = [aws_iam_group.test.name] | ||
policy_arn = aws_iam_policy.test.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output "id" { | ||
value = aws_iam_policy.test.id | ||
} | ||
|
||
output "arn" { | ||
value = aws_iam_policy.test.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
variable "profile" { | ||
description = "The named profile for the AWS account that will be deployed to" | ||
} | ||
|
||
variable "region" { | ||
description = "The AWS region to deploy to" | ||
} | ||
|
||
variable "name" { | ||
description = "The name of test" | ||
} |