github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/resource/testing_import_state_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/terraform"
     9  )
    10  
    11  func TestTest_importState(t *testing.T) {
    12  	t.Skip("test requires new provider implementation")
    13  
    14  	mp := testProvider()
    15  	mp.ImportStateReturn = []*terraform.InstanceState{
    16  		{
    17  			ID:        "foo",
    18  			Ephemeral: terraform.EphemeralState{Type: "test_instance"},
    19  		},
    20  	}
    21  	mp.RefreshFn = func(
    22  		i *terraform.InstanceInfo,
    23  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
    24  		return s, nil
    25  	}
    26  
    27  	checked := false
    28  	checkFn := func(s []*terraform.InstanceState) error {
    29  		checked = true
    30  
    31  		if s[0].ID != "foo" {
    32  			return fmt.Errorf("bad: %#v", s)
    33  		}
    34  
    35  		return nil
    36  	}
    37  
    38  	mt := new(mockT)
    39  	Test(mt, TestCase{
    40  		Providers: map[string]terraform.ResourceProvider{
    41  			"test": mp,
    42  		},
    43  
    44  		Steps: []TestStep{
    45  			{
    46  				Config:           testConfigStrProvider,
    47  				ResourceName:     "test_instance.foo",
    48  				ImportState:      true,
    49  				ImportStateId:    "foo",
    50  				ImportStateCheck: checkFn,
    51  			},
    52  		},
    53  	})
    54  
    55  	if mt.failed() {
    56  		t.Fatalf("test failed: %s", mt.failMessage())
    57  	}
    58  	if !checked {
    59  		t.Fatal("didn't call check")
    60  	}
    61  }
    62  
    63  func TestTest_importStateFail(t *testing.T) {
    64  	t.Skip("test requires new provider implementation")
    65  
    66  	mp := testProvider()
    67  	mp.ImportStateReturn = []*terraform.InstanceState{
    68  		{
    69  			ID:        "bar",
    70  			Ephemeral: terraform.EphemeralState{Type: "test_instance"},
    71  		},
    72  	}
    73  	mp.RefreshFn = func(
    74  		i *terraform.InstanceInfo,
    75  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
    76  		return s, nil
    77  	}
    78  
    79  	checked := false
    80  	checkFn := func(s []*terraform.InstanceState) error {
    81  		checked = true
    82  
    83  		if s[0].ID != "foo" {
    84  			return fmt.Errorf("bad: %#v", s)
    85  		}
    86  
    87  		return nil
    88  	}
    89  
    90  	mt := new(mockT)
    91  	Test(mt, TestCase{
    92  		Providers: map[string]terraform.ResourceProvider{
    93  			"test": mp,
    94  		},
    95  
    96  		Steps: []TestStep{
    97  			{
    98  				Config:           testConfigStrProvider,
    99  				ResourceName:     "test_instance.foo",
   100  				ImportState:      true,
   101  				ImportStateId:    "foo",
   102  				ImportStateCheck: checkFn,
   103  			},
   104  		},
   105  	})
   106  
   107  	if !mt.failed() {
   108  		t.Fatal("should fail")
   109  	}
   110  	if !checked {
   111  		t.Fatal("didn't call check")
   112  	}
   113  }
   114  
   115  func TestTest_importStateDetectId(t *testing.T) {
   116  	t.Skip("test requires new provider implementation")
   117  
   118  	mp := testProvider()
   119  	mp.DiffReturn = nil
   120  	mp.ApplyFn = func(
   121  		info *terraform.InstanceInfo,
   122  		state *terraform.InstanceState,
   123  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   124  		if !diff.Destroy {
   125  			return &terraform.InstanceState{
   126  				ID: "foo",
   127  			}, nil
   128  		}
   129  
   130  		return nil, nil
   131  	}
   132  
   133  	mp.RefreshFn = func(
   134  		i *terraform.InstanceInfo,
   135  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   136  		return s, nil
   137  	}
   138  
   139  	mp.ImportStateFn = func(
   140  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   141  		if id != "foo" {
   142  			return nil, fmt.Errorf("bad import ID: %s", id)
   143  		}
   144  
   145  		return []*terraform.InstanceState{
   146  			{
   147  				ID:        "bar",
   148  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   149  			},
   150  		}, nil
   151  	}
   152  
   153  	checked := false
   154  	checkFn := func(s []*terraform.InstanceState) error {
   155  		checked = true
   156  
   157  		if s[0].ID != "bar" {
   158  			return fmt.Errorf("bad: %#v", s)
   159  		}
   160  
   161  		return nil
   162  	}
   163  
   164  	mt := new(mockT)
   165  	Test(mt, TestCase{
   166  		Providers: map[string]terraform.ResourceProvider{
   167  			"test": mp,
   168  		},
   169  
   170  		Steps: []TestStep{
   171  			{
   172  				Config: testConfigStr,
   173  			},
   174  			{
   175  				Config:           testConfigStr,
   176  				ResourceName:     "test_instance.foo",
   177  				ImportState:      true,
   178  				ImportStateCheck: checkFn,
   179  			},
   180  		},
   181  	})
   182  
   183  	if mt.failed() {
   184  		t.Fatalf("test failed: %s", mt.failMessage())
   185  	}
   186  	if !checked {
   187  		t.Fatal("didn't call check")
   188  	}
   189  }
   190  
   191  func TestTest_importStateIdPrefix(t *testing.T) {
   192  	t.Skip("test requires new provider implementation")
   193  
   194  	mp := testProvider()
   195  	mp.DiffReturn = nil
   196  	mp.ApplyFn = func(
   197  		info *terraform.InstanceInfo,
   198  		state *terraform.InstanceState,
   199  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   200  		if !diff.Destroy {
   201  			return &terraform.InstanceState{
   202  				ID: "foo",
   203  			}, nil
   204  		}
   205  
   206  		return nil, nil
   207  	}
   208  
   209  	mp.RefreshFn = func(
   210  		i *terraform.InstanceInfo,
   211  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   212  		return s, nil
   213  	}
   214  
   215  	mp.ImportStateFn = func(
   216  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   217  		if id != "bazfoo" {
   218  			return nil, fmt.Errorf("bad import ID: %s", id)
   219  		}
   220  
   221  		return []*terraform.InstanceState{
   222  			{
   223  				ID:        "bar",
   224  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   225  			},
   226  		}, nil
   227  	}
   228  
   229  	checked := false
   230  	checkFn := func(s []*terraform.InstanceState) error {
   231  		checked = true
   232  
   233  		if s[0].ID != "bar" {
   234  			return fmt.Errorf("bad: %#v", s)
   235  		}
   236  
   237  		return nil
   238  	}
   239  
   240  	mt := new(mockT)
   241  	Test(mt, TestCase{
   242  		Providers: map[string]terraform.ResourceProvider{
   243  			"test": mp,
   244  		},
   245  
   246  		Steps: []TestStep{
   247  			{
   248  				Config: testConfigStr,
   249  			},
   250  			{
   251  				Config:              testConfigStr,
   252  				ResourceName:        "test_instance.foo",
   253  				ImportState:         true,
   254  				ImportStateCheck:    checkFn,
   255  				ImportStateIdPrefix: "baz",
   256  			},
   257  		},
   258  	})
   259  
   260  	if mt.failed() {
   261  		t.Fatalf("test failed: %s", mt.failMessage())
   262  	}
   263  	if !checked {
   264  		t.Fatal("didn't call check")
   265  	}
   266  }
   267  
   268  func TestTest_importStateVerify(t *testing.T) {
   269  	t.Skip("test requires new provider implementation")
   270  
   271  	mp := testProvider()
   272  	mp.DiffReturn = nil
   273  	mp.ApplyFn = func(
   274  		info *terraform.InstanceInfo,
   275  		state *terraform.InstanceState,
   276  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   277  		if !diff.Destroy {
   278  			return &terraform.InstanceState{
   279  				ID: "foo",
   280  				Attributes: map[string]string{
   281  					"foo": "bar",
   282  				},
   283  			}, nil
   284  		}
   285  
   286  		return nil, nil
   287  	}
   288  
   289  	mp.RefreshFn = func(
   290  		i *terraform.InstanceInfo,
   291  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   292  		if len(s.Attributes) == 0 {
   293  			s.Attributes = map[string]string{
   294  				"id":  s.ID,
   295  				"foo": "bar",
   296  			}
   297  		}
   298  
   299  		return s, nil
   300  	}
   301  
   302  	mp.ImportStateFn = func(
   303  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   304  		if id != "foo" {
   305  			return nil, fmt.Errorf("bad import ID: %s", id)
   306  		}
   307  
   308  		return []*terraform.InstanceState{
   309  			{
   310  				ID:        "foo",
   311  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   312  			},
   313  		}, nil
   314  	}
   315  
   316  	mt := new(mockT)
   317  	Test(mt, TestCase{
   318  		Providers: map[string]terraform.ResourceProvider{
   319  			"test": mp,
   320  		},
   321  
   322  		Steps: []TestStep{
   323  			{
   324  				Config: testConfigStr,
   325  			},
   326  			{
   327  				Config:            testConfigStr,
   328  				ResourceName:      "test_instance.foo",
   329  				ImportState:       true,
   330  				ImportStateVerify: true,
   331  			},
   332  		},
   333  	})
   334  
   335  	if mt.failed() {
   336  		t.Fatalf("test failed: %s", mt.failMessage())
   337  	}
   338  }
   339  
   340  func TestTest_importStateVerifyFail(t *testing.T) {
   341  	t.Skip("test requires new provider implementation")
   342  
   343  	mp := testProvider()
   344  	mp.DiffReturn = nil
   345  	mp.ApplyFn = func(
   346  		info *terraform.InstanceInfo,
   347  		state *terraform.InstanceState,
   348  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   349  		if !diff.Destroy {
   350  			return &terraform.InstanceState{
   351  				ID: "foo",
   352  				Attributes: map[string]string{
   353  					"foo": "bar",
   354  				},
   355  			}, nil
   356  		}
   357  
   358  		return nil, nil
   359  	}
   360  
   361  	mp.RefreshFn = func(
   362  		i *terraform.InstanceInfo,
   363  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   364  		return s, nil
   365  	}
   366  
   367  	mp.ImportStateFn = func(
   368  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   369  		if id != "foo" {
   370  			return nil, fmt.Errorf("bad import ID: %s", id)
   371  		}
   372  
   373  		return []*terraform.InstanceState{
   374  			{
   375  				ID:        "foo",
   376  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   377  			},
   378  		}, nil
   379  	}
   380  
   381  	mt := new(mockT)
   382  	Test(mt, TestCase{
   383  		Providers: map[string]terraform.ResourceProvider{
   384  			"test": mp,
   385  		},
   386  
   387  		Steps: []TestStep{
   388  			{
   389  				Config: testConfigStr,
   390  			},
   391  			{
   392  				Config:            testConfigStr,
   393  				ResourceName:      "test_instance.foo",
   394  				ImportState:       true,
   395  				ImportStateVerify: true,
   396  			},
   397  		},
   398  	})
   399  
   400  	if !mt.failed() {
   401  		t.Fatalf("test should fail")
   402  	}
   403  }
   404  
   405  func TestTest_importStateIdFunc(t *testing.T) {
   406  	t.Skip("test requires new provider implementation")
   407  
   408  	mp := testProvider()
   409  	mp.ImportStateFn = func(
   410  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   411  		if id != "foo:bar" {
   412  			return nil, fmt.Errorf("bad import ID: %s", id)
   413  		}
   414  
   415  		return []*terraform.InstanceState{
   416  			{
   417  				ID:        "foo",
   418  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   419  			},
   420  		}, nil
   421  	}
   422  
   423  	mp.RefreshFn = func(
   424  		i *terraform.InstanceInfo,
   425  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   426  		return s, nil
   427  	}
   428  
   429  	checked := false
   430  	checkFn := func(s []*terraform.InstanceState) error {
   431  		checked = true
   432  
   433  		if s[0].ID != "foo" {
   434  			return fmt.Errorf("bad: %#v", s)
   435  		}
   436  
   437  		return nil
   438  	}
   439  
   440  	mt := new(mockT)
   441  	Test(mt, TestCase{
   442  		Providers: map[string]terraform.ResourceProvider{
   443  			"test": mp,
   444  		},
   445  
   446  		Steps: []TestStep{
   447  			{
   448  				Config:            testConfigStrProvider,
   449  				ResourceName:      "test_instance.foo",
   450  				ImportState:       true,
   451  				ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", nil },
   452  				ImportStateCheck:  checkFn,
   453  			},
   454  		},
   455  	})
   456  
   457  	if mt.failed() {
   458  		t.Fatalf("test failed: %s", mt.failMessage())
   459  	}
   460  	if !checked {
   461  		t.Fatal("didn't call check")
   462  	}
   463  }
   464  
   465  func TestTest_importStateIdFuncFail(t *testing.T) {
   466  	t.Skip("test requires new provider implementation")
   467  
   468  	mp := testProvider()
   469  	mp.ImportStateFn = func(
   470  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   471  		if id != "foo:bar" {
   472  			return nil, fmt.Errorf("bad import ID: %s", id)
   473  		}
   474  
   475  		return []*terraform.InstanceState{
   476  			{
   477  				ID:        "foo",
   478  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   479  			},
   480  		}, nil
   481  	}
   482  
   483  	mp.RefreshFn = func(
   484  		i *terraform.InstanceInfo,
   485  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   486  		return s, nil
   487  	}
   488  
   489  	checkFn := func(s []*terraform.InstanceState) error {
   490  		if s[0].ID != "foo" {
   491  			return fmt.Errorf("bad: %#v", s)
   492  		}
   493  
   494  		return nil
   495  	}
   496  
   497  	mt := new(mockT)
   498  	Test(mt, TestCase{
   499  		Providers: map[string]terraform.ResourceProvider{
   500  			"test": mp,
   501  		},
   502  
   503  		Steps: []TestStep{
   504  			{
   505  				Config:            testConfigStrProvider,
   506  				ResourceName:      "test_instance.foo",
   507  				ImportState:       true,
   508  				ImportStateIdFunc: func(*terraform.State) (string, error) { return "foo:bar", errors.New("foobar") },
   509  				ImportStateCheck:  checkFn,
   510  			},
   511  		},
   512  	})
   513  
   514  	if !mt.failed() {
   515  		t.Fatalf("test should fail")
   516  	}
   517  }