Skip to content

Commit

Permalink
tests/resource/aws_key_pair: Randomize name in test configurations (#…
Browse files Browse the repository at this point in the history
…11890)

Previously, the acceptance testing could have naming issues such as:

```
--- FAIL: TestAccAWSKeyPair_tags (3.90s)
    testing.go:640: Step 0 error: errors during apply:

        Error: Error import KeyPair: InvalidKeyPair.Duplicate: The keypair 'tf-acc-key-pair' already exists.
```

Output from acceptance testing:

```
--- PASS: TestAccAWSKeyPair_disappears (12.55s)
--- PASS: TestAccAWSKeyPair_basic (16.17s)
--- PASS: TestAccAWSKeyPair_namePrefix (16.18s)
--- PASS: TestAccAWSKeyPair_generatedName (16.20s)
--- PASS: TestAccAWSKeyPair_tags (37.64s)
```
  • Loading branch information
bflad authored Mar 10, 2020
1 parent 83371c7 commit a19aa73
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions aws/resource_aws_key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
Expand Down Expand Up @@ -61,19 +62,20 @@ func TestAccAWSKeyPair_basic(t *testing.T) {
var keyPair ec2.KeyPairInfo
fingerprint := "d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62"
resourceName := "aws_key_pair.a_key_pair"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSKeyPairConfig,
Config: testAccAWSKeyPairConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists(resourceName, &keyPair),
testAccCheckAWSKeyPairFingerprint(&keyPair, fingerprint),
resource.TestCheckResourceAttr(resourceName, "fingerprint", fingerprint),
resource.TestCheckResourceAttr(resourceName, "key_name", "tf-acc-key-pair"),
resource.TestCheckResourceAttr(resourceName, "key_name", rName),
),
},
{
Expand All @@ -89,14 +91,15 @@ func TestAccAWSKeyPair_basic(t *testing.T) {
func TestAccAWSKeyPair_tags(t *testing.T) {
var keyPair ec2.KeyPairInfo
resourceName := "aws_key_pair.a_key_pair"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSKeyPairConfigTags1("key1", "value1"),
Config: testAccAWSKeyPairConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists(resourceName, &keyPair),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
Expand All @@ -110,7 +113,7 @@ func TestAccAWSKeyPair_tags(t *testing.T) {
ImportStateVerifyIgnore: []string{"public_key"},
},
{
Config: testAccAWSKeyPairConfigTags2("key1", "value1updated", "key2", "value2"),
Config: testAccAWSKeyPairConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists(resourceName, &keyPair),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
Expand All @@ -119,7 +122,7 @@ func TestAccAWSKeyPair_tags(t *testing.T) {
),
},
{
Config: testAccAWSKeyPairConfigTags1("key2", "value2"),
Config: testAccAWSKeyPairConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists(resourceName, &keyPair),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
Expand Down Expand Up @@ -189,14 +192,15 @@ func TestAccAWSKeyPair_namePrefix(t *testing.T) {
func TestAccAWSKeyPair_disappears(t *testing.T) {
var keyPair ec2.KeyPairInfo
resourceName := "aws_key_pair.a_key_pair"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSKeyPairDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSKeyPairConfig,
Config: testAccAWSKeyPairConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSKeyPairExists(resourceName, &keyPair),
testAccCheckAWSKeyPairDisappears(&keyPair),
Expand Down Expand Up @@ -302,38 +306,40 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestC
}
}

const testAccAWSKeyPairConfig = `
func testAccAWSKeyPairConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_key_pair" "a_key_pair" {
key_name = "tf-acc-key-pair"
key_name = %[1]q
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}
`
`, rName)
}

func testAccAWSKeyPairConfigTags1(tagKey1, tagValue1 string) string {
func testAccAWSKeyPairConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_key_pair" "a_key_pair" {
key_name = "tf-acc-key-pair"
key_name = %[1]q
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
tags = {
%[1]q = %[2]q
%[2]q = %[3]q
}
}
`, tagKey1, tagValue1)
`, rName, tagKey1, tagValue1)
}

func testAccAWSKeyPairConfigTags2(tagKey1, tagValue1, tagKey2, tagValue2 string) string {
func testAccAWSKeyPairConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_key_pair" "a_key_pair" {
key_name = "tf-acc-key-pair"
key_name = %[1]q
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
tags = {
%[1]q = %[2]q
%[3]q = %[4]q
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, tagKey1, tagValue1, tagKey2, tagValue2)
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

const testAccAWSKeyPairConfig_generatedName = `
Expand Down

0 comments on commit a19aa73

Please sign in to comment.