Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Separate ImportState Tests Into All Applicable Tests #8944

Closed
bflad opened this issue Jun 11, 2019 · 1 comment · Fixed by #10708
Closed

Migrate Separate ImportState Tests Into All Applicable Tests #8944

bflad opened this issue Jun 11, 2019 · 1 comment · Fixed by #10708
Labels
technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.

Comments

@bflad
Copy link
Contributor

bflad commented Jun 11, 2019

Description

An older common practice with acceptance testing was creating a separate acceptance test, that performed the ImportState testing:

func TestAccExampleThing_basic(t *testing.T) { // not flagged!
    resource.ParallelTest(t, resource.TestCase{
        PreCheck:     func() { testAccPreCheck(t) },
        Providers:    testAccProviders,
        CheckDestroy: testAccCheckExampleThingDestroy,
        Steps: []resource.TestStep{
            {
                Config: testAccExampleThingConfig(),
                Check: resource.ComposeTestCheckFunc(
                    resource.TestCheckResourceAttrSet("example_thing.test", "attr1"),
                ),
            },
        },
    })
}

func TestAccExampleThing_importBasic(t *testing.T) { // flagged!
    resource.ParallelTest(t, resource.TestCase{
        PreCheck:     func() { testAccPreCheck(t) },
        Providers:    testAccProviders,
        CheckDestroy: testAccCheckExampleThingDestroy,
        Steps: []resource.TestStep{
            {
                Config: testAccExampleThingConfig(),
                Check: resource.ComposeTestCheckFunc(
                    resource.TestCheckResourceAttrSet("example_thing.test", "attr1"),
                ),
            },
            {
                ResourceName:      "example_thing.test",
                ImportState:       true,
                ImportStateVerify: true,
            },
        },
    })
}

This testing setup creates duplicate test infrastructure for little benefit (only an additional API call) and can miss other permutations of Terraform configurations that do not successfully import. It is generally preferred to just add the ImportState testing to all existing applicable tests (except tests like _disappears tests, which the infrastructure won't be importable).

In the above example, its preferable to just have:

func TestAccExampleThing_basic(t *testing.T) {
    resource.ParallelTest(t, resource.TestCase{
        PreCheck:     func() { testAccPreCheck(t) },
        Providers:    testAccProviders,
        CheckDestroy: testAccCheckExampleThingDestroy,
        Steps: []resource.TestStep{
            {
                Config: testAccExampleThingConfig(),
                Check: resource.ComposeTestCheckFunc(
                    resource.TestCheckResourceAttrSet("example_thing.test", "attr1"),
                ),
            },
            {
                ResourceName:      "example_thing.test",
                ImportState:       true,
                ImportStateVerify: true,
            },
        },
    })
}

Many of these in the Terraform AWS Provider can be found by enabling the tfproviderlint AT002 check, since the acceptance test function naming scheme included _import or _Import. Some resources may require updates to fix ImportStateVerify issues. There are also some false positive test names that we can have the linter ignore via: //lintignore:AT002

Definition of Done

  • In GNUmakefile, add -AT002 to tfproviderlint command under lint target and have TravisCI testing pass
@bflad bflad added tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. technical-debt Addresses areas of the codebase that need refactoring or redesign. labels Jun 11, 2019
@ryndaniels ryndaniels self-assigned this Aug 19, 2019
@ghost
Copy link

ghost commented Mar 29, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost unassigned ryndaniels Mar 29, 2020
@ghost ghost locked and limited conversation to collaborators Mar 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
technical-debt Addresses areas of the codebase that need refactoring or redesign. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants