Skip to content

Commit

Permalink
r/aws_iot_thing_type: Tidy up acceptance tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Aug 31, 2023
1 parent 716f985 commit e3bf552
Showing 1 changed file with 81 additions and 108 deletions.
189 changes: 81 additions & 108 deletions internal/service/iot/thing_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iot"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfiot "github.com/hashicorp/terraform-provider-aws/internal/service/iot"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccIoTThingType_basic(t *testing.T) {
ctx := acctest.Context(t)
rInt := sdkacctest.RandInt()
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_thing_type.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
Expand All @@ -29,27 +30,51 @@ func TestAccIoTThingType_basic(t *testing.T) {
CheckDestroy: testAccCheckThingTypeDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccThingTypeConfig_basic(rInt),
Config: testAccThingTypeConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, "aws_iot_thing_type.foo"),
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "name", fmt.Sprintf("tf_acc_iot_thing_type_%d", rInt)),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags.%", "0"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags_all.%", "0"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "deprecated", "false"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: "aws_iot_thing_type.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccIoTThingType_disappears(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_thing_type.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, iot.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckThingTypeDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccThingTypeConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, resourceName),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfiot.ResourceThingType(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccIoTThingType_full(t *testing.T) {
ctx := acctest.Context(t)
rInt := sdkacctest.RandInt()
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_thing_type.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
Expand All @@ -58,26 +83,27 @@ func TestAccIoTThingType_full(t *testing.T) {
CheckDestroy: testAccCheckThingTypeDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccThingTypeConfig_full(rInt),
Config: testAccThingTypeConfig_full(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, "aws_iot_thing_type.foo"),
resource.TestCheckResourceAttrSet("aws_iot_thing_type.foo", "arn"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.description", "MyDescription"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "properties.0.searchable_attributes.#", "3"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "deprecated", "true"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "tags_all.%", "1"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "deprecated", "true"),
resource.TestCheckResourceAttr(resourceName, "properties.0.description", "MyDescription"),
resource.TestCheckResourceAttr(resourceName, "properties.0.searchable_attributes.#", "3"),
resource.TestCheckTypeSetElemAttr(resourceName, "properties.0.searchable_attributes.*", "foo"),
resource.TestCheckTypeSetElemAttr(resourceName, "properties.0.searchable_attributes.*", "bar"),
resource.TestCheckTypeSetElemAttr(resourceName, "properties.0.searchable_attributes.*", "baz"),
),
},
{
ResourceName: "aws_iot_thing_type.foo",
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccThingTypeConfig_fullUpdated(rInt),
Config: testAccThingTypeConfig_full(rName, false),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("aws_iot_thing_type.foo", "deprecated", "false"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "deprecated", "false"),
),
},
},
Expand All @@ -86,8 +112,8 @@ func TestAccIoTThingType_full(t *testing.T) {

func TestAccIoTThingType_tags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandString(5)
resourceName := "aws_iot_thing_type.foo"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_iot_thing_type.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
Expand All @@ -96,11 +122,11 @@ func TestAccIoTThingType_tags(t *testing.T) {
CheckDestroy: testAccCheckThingTypeDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccThingTypeConfig_tags1(rName, "key1", "user@example"),
Config: testAccThingTypeConfig_tags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, "aws_iot_thing_type.foo"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
Expand All @@ -109,18 +135,18 @@ func TestAccIoTThingType_tags(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccThingTypeConfig_tags2(rName, "key1", "user@example", "key2", "value2"),
Config: testAccThingTypeConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, "aws_iot_thing_type.foo"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "user@example"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccThingTypeConfig_tags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, "aws_iot_thing_type.foo"),
testAccCheckThingTypeExists(ctx, resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
Expand All @@ -129,52 +155,18 @@ func TestAccIoTThingType_tags(t *testing.T) {
})
}

func TestAccIoTThingType_disappears(t *testing.T) {
ctx := acctest.Context(t)
resourceName := "aws_iot_thing_type.foo"
rInt := sdkacctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, iot.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckThingTypeDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccThingTypeConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckThingTypeExists(ctx, resourceName),
acctest.CheckResourceDisappears(ctx, acctest.Provider, tfiot.ResourceThingType(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckThingTypeExists(ctx context.Context, name string) resource.TestCheckFunc {
func testAccCheckThingTypeExists(ctx context.Context, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", name)
return fmt.Errorf("Not found: %s", n)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).IoTConn(ctx)
input := &iot.ListThingTypesInput{}

output, err := conn.ListThingTypesWithContext(ctx, input)

if err != nil {
return err
}
_, err := tfiot.FindThingTypeByName(ctx, conn, rs.Primary.ID)

for _, rule := range output.ThingTypes {
if aws.StringValue(rule.ThingTypeName) == rs.Primary.ID {
return nil
}
}

return fmt.Errorf("IoT Topic Rule (%s) not found", rs.Primary.ID)
return err
}
}

Expand All @@ -187,68 +179,49 @@ func testAccCheckThingTypeDestroy(ctx context.Context) resource.TestCheckFunc {
continue
}

params := &iot.DescribeThingTypeInput{
ThingTypeName: aws.String(rs.Primary.ID),
_, err := tfiot.FindThingTypeByName(ctx, conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

_, err := conn.DescribeThingTypeWithContext(ctx, params)
if err == nil {
return fmt.Errorf("Expected IoT Thing Type to be destroyed, %s found", rs.Primary.ID)
if err != nil {
return err
}

return fmt.Errorf("IoT Thing Type %s still exists", rs.Primary.ID)
}

return nil
}
}

func testAccThingTypeConfig_basic(rName int) string {
func testAccThingTypeConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%d"
resource "aws_iot_thing_type" "test" {
name = %[1]q
}
`, rName)
}

func testAccThingTypeConfig_full(rName int) string {
func testAccThingTypeConfig_full(rName string, deprecated bool) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%d"
deprecated = true
resource "aws_iot_thing_type" "test" {
name = %[1]q
deprecated = %[2]t
properties {
description = "MyDescription"
searchable_attributes = ["foo", "bar", "baz"]
}
tags = {
testtag = "MyTagValue"
}
}
`, rName)
}

func testAccThingTypeConfig_fullUpdated(rName int) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%d"
deprecated = false
properties {
description = "MyDescription"
searchable_attributes = ["foo", "bar", "baz"]
}
tags = {
testtag = "MyTagValue"
}
}
`, rName)
`, rName, deprecated)
}

func testAccThingTypeConfig_tags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%[1]s"
resource "aws_iot_thing_type" "test" {
name = %[1]q
deprecated = false
tags = {
Expand All @@ -260,8 +233,8 @@ resource "aws_iot_thing_type" "foo" {

func testAccThingTypeConfig_tags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_iot_thing_type" "foo" {
name = "tf_acc_iot_thing_type_%[1]s"
resource "aws_iot_thing_type" "test" {
name = %[1]q
deprecated = false
tags = {
Expand Down

0 comments on commit e3bf552

Please sign in to comment.