github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/providers/test/resource_import_other_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestResourceImportOther(t *testing.T) {
    13  	resource.UnitTest(t, resource.TestCase{
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckResourceDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: strings.TrimSpace(`
    19  resource "test_resource_import_other" "foo" {
    20  }
    21  				`),
    22  			},
    23  			{
    24  				ImportState:  true,
    25  				ResourceName: "test_resource_import_other.foo",
    26  				ImportStateCheck: func(iss []*terraform.InstanceState) error {
    27  					if got, want := len(iss), 2; got != want {
    28  						return fmt.Errorf("wrong number of resources %d; want %d", got, want)
    29  					}
    30  
    31  					byID := make(map[string]*terraform.InstanceState, len(iss))
    32  					for _, is := range iss {
    33  						byID[is.ID] = is
    34  					}
    35  
    36  					if is, ok := byID["import_other_main"]; !ok {
    37  						return fmt.Errorf("no instance with id import_other_main in state")
    38  					} else if got, want := is.Ephemeral.Type, "test_resource_import_other"; got != want {
    39  						return fmt.Errorf("import_other_main has wrong type %q; want %q", got, want)
    40  					} else if got, want := is.Attributes["computed"], "hello!"; got != want {
    41  						return fmt.Errorf("import_other_main has wrong value %q for its computed attribute; want %q", got, want)
    42  					}
    43  					if is, ok := byID["import_other_other"]; !ok {
    44  						return fmt.Errorf("no instance with id import_other_other in state")
    45  					} else if got, want := is.Ephemeral.Type, "test_resource_defaults"; got != want {
    46  						return fmt.Errorf("import_other_other has wrong type %q; want %q", got, want)
    47  					}
    48  
    49  					return nil
    50  				},
    51  			},
    52  		},
    53  	})
    54  }