hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.36k stars 1.75k forks source link

Consider adding automated testing of importing resources without provider-default values provided #20401

Open SarahFrench opened 2 days ago

SarahFrench commented 2 days ago

What kind of contribution is this issue about?

Other (specify in details)

Details

Currently we generate acceptance tests from example files. These typically include:

The import step is only tested in a context where there are provider-default values available; the tests are run in an environment where GOOGLE_PROJECT, GOOGLE_REGION etc environment variables are set. This means that we miss resources where the import process isn’t written in a way where the project (etc) is derived from the user-provided id string. This is a blindspot of our testing that could be improved.

For example this acceptance test, TestAccPubsubTopic_pubsubTopicBasicExample, could be updated like this:

func TestAccPubsubTopic_pubsubTopicBasicExample(t *testing.T) {
    t.Parallel()

    context := map[string]interface{}{
        "random_suffix": acctest.RandString(t, 10),
    }

    acctest.VcrTest(t, resource.TestCase{
        PreCheck:                 func() { acctest.AccTestPreCheck(t) },
        ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
        CheckDestroy:             testAccCheckPubsubTopicDestroyProducer(t),
        Steps: []resource.TestStep{
            {
                Config: testAccPubsubTopic_pubsubTopicBasicExample(context),
            },
            {
+               // Test import with provider-default values
                ResourceName:            "google_pubsub_topic.example",
                ImportState:             true,
                ImportStateVerify:       true,
                ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
            },
            {
+               // Test import with provider-default values unset
+               PreConfig: func (){
+                   t.Setenv("GOOGLE_PROJECT", "") //unset
+                   t.Setenv("GOOGLE_REGION", "") //unset
+                   t.Setenv("GOOGLE_ZONE", "") //unset
+               },
                ResourceName:            "google_pubsub_topic.example",
                ImportState:             true,
                ImportStateVerify:       true,
                ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
            },
        },
    })
}

References

No response