github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/helper/resource/testing_import_state_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func TestTest_importState(t *testing.T) {
    11  	mp := testProvider()
    12  	mp.ImportStateReturn = []*terraform.InstanceState{
    13  		&terraform.InstanceState{
    14  			ID:        "foo",
    15  			Ephemeral: terraform.EphemeralState{Type: "test_instance"},
    16  		},
    17  	}
    18  	mp.RefreshFn = func(
    19  		i *terraform.InstanceInfo,
    20  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
    21  		return s, nil
    22  	}
    23  
    24  	checked := false
    25  	checkFn := func(s []*terraform.InstanceState) error {
    26  		checked = true
    27  
    28  		if s[0].ID != "foo" {
    29  			return fmt.Errorf("bad: %#v", s)
    30  		}
    31  
    32  		return nil
    33  	}
    34  
    35  	mt := new(mockT)
    36  	Test(mt, TestCase{
    37  		Providers: map[string]terraform.ResourceProvider{
    38  			"test": mp,
    39  		},
    40  
    41  		Steps: []TestStep{
    42  			TestStep{
    43  				Config:           testConfigStrProvider,
    44  				ResourceName:     "test_instance.foo",
    45  				ImportState:      true,
    46  				ImportStateId:    "foo",
    47  				ImportStateCheck: checkFn,
    48  			},
    49  		},
    50  	})
    51  
    52  	if mt.failed() {
    53  		t.Fatalf("test failed: %s", mt.failMessage())
    54  	}
    55  	if !checked {
    56  		t.Fatal("didn't call check")
    57  	}
    58  }
    59  
    60  func TestTest_importStateFail(t *testing.T) {
    61  	mp := testProvider()
    62  	mp.ImportStateReturn = []*terraform.InstanceState{
    63  		&terraform.InstanceState{
    64  			ID:        "bar",
    65  			Ephemeral: terraform.EphemeralState{Type: "test_instance"},
    66  		},
    67  	}
    68  	mp.RefreshFn = func(
    69  		i *terraform.InstanceInfo,
    70  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
    71  		return s, nil
    72  	}
    73  
    74  	checked := false
    75  	checkFn := func(s []*terraform.InstanceState) error {
    76  		checked = true
    77  
    78  		if s[0].ID != "foo" {
    79  			return fmt.Errorf("bad: %#v", s)
    80  		}
    81  
    82  		return nil
    83  	}
    84  
    85  	mt := new(mockT)
    86  	Test(mt, TestCase{
    87  		Providers: map[string]terraform.ResourceProvider{
    88  			"test": mp,
    89  		},
    90  
    91  		Steps: []TestStep{
    92  			TestStep{
    93  				Config:           testConfigStrProvider,
    94  				ResourceName:     "test_instance.foo",
    95  				ImportState:      true,
    96  				ImportStateId:    "foo",
    97  				ImportStateCheck: checkFn,
    98  			},
    99  		},
   100  	})
   101  
   102  	if !mt.failed() {
   103  		t.Fatal("should fail")
   104  	}
   105  	if !checked {
   106  		t.Fatal("didn't call check")
   107  	}
   108  }
   109  
   110  func TestTest_importStateDetectId(t *testing.T) {
   111  	mp := testProvider()
   112  	mp.DiffReturn = nil
   113  	mp.ApplyFn = func(
   114  		info *terraform.InstanceInfo,
   115  		state *terraform.InstanceState,
   116  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   117  		if !diff.Destroy {
   118  			return &terraform.InstanceState{
   119  				ID: "foo",
   120  			}, nil
   121  		}
   122  
   123  		return nil, nil
   124  	}
   125  
   126  	mp.RefreshFn = func(
   127  		i *terraform.InstanceInfo,
   128  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   129  		return s, nil
   130  	}
   131  
   132  	mp.ImportStateFn = func(
   133  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   134  		if id != "foo" {
   135  			return nil, fmt.Errorf("bad import ID: %s", id)
   136  		}
   137  
   138  		return []*terraform.InstanceState{
   139  			&terraform.InstanceState{
   140  				ID:        "bar",
   141  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   142  			},
   143  		}, nil
   144  	}
   145  
   146  	checked := false
   147  	checkFn := func(s []*terraform.InstanceState) error {
   148  		checked = true
   149  
   150  		if s[0].ID != "bar" {
   151  			return fmt.Errorf("bad: %#v", s)
   152  		}
   153  
   154  		return nil
   155  	}
   156  
   157  	mt := new(mockT)
   158  	Test(mt, TestCase{
   159  		Providers: map[string]terraform.ResourceProvider{
   160  			"test": mp,
   161  		},
   162  
   163  		Steps: []TestStep{
   164  			TestStep{
   165  				Config: testConfigStr,
   166  			},
   167  			TestStep{
   168  				Config:           testConfigStr,
   169  				ResourceName:     "test_instance.foo",
   170  				ImportState:      true,
   171  				ImportStateCheck: checkFn,
   172  			},
   173  		},
   174  	})
   175  
   176  	if mt.failed() {
   177  		t.Fatalf("test failed: %s", mt.failMessage())
   178  	}
   179  	if !checked {
   180  		t.Fatal("didn't call check")
   181  	}
   182  }
   183  
   184  func TestTest_importStateIdPrefix(t *testing.T) {
   185  	mp := testProvider()
   186  	mp.DiffReturn = nil
   187  	mp.ApplyFn = func(
   188  		info *terraform.InstanceInfo,
   189  		state *terraform.InstanceState,
   190  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   191  		if !diff.Destroy {
   192  			return &terraform.InstanceState{
   193  				ID: "foo",
   194  			}, nil
   195  		}
   196  
   197  		return nil, nil
   198  	}
   199  
   200  	mp.RefreshFn = func(
   201  		i *terraform.InstanceInfo,
   202  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   203  		return s, nil
   204  	}
   205  
   206  	mp.ImportStateFn = func(
   207  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   208  		if id != "bazfoo" {
   209  			return nil, fmt.Errorf("bad import ID: %s", id)
   210  		}
   211  
   212  		return []*terraform.InstanceState{
   213  			{
   214  				ID:        "bar",
   215  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   216  			},
   217  		}, nil
   218  	}
   219  
   220  	checked := false
   221  	checkFn := func(s []*terraform.InstanceState) error {
   222  		checked = true
   223  
   224  		if s[0].ID != "bar" {
   225  			return fmt.Errorf("bad: %#v", s)
   226  		}
   227  
   228  		return nil
   229  	}
   230  
   231  	mt := new(mockT)
   232  	Test(mt, TestCase{
   233  		Providers: map[string]terraform.ResourceProvider{
   234  			"test": mp,
   235  		},
   236  
   237  		Steps: []TestStep{
   238  			{
   239  				Config: testConfigStr,
   240  			},
   241  			{
   242  				Config:              testConfigStr,
   243  				ResourceName:        "test_instance.foo",
   244  				ImportState:         true,
   245  				ImportStateCheck:    checkFn,
   246  				ImportStateIdPrefix: "baz",
   247  			},
   248  		},
   249  	})
   250  
   251  	if mt.failed() {
   252  		t.Fatalf("test failed: %s", mt.failMessage())
   253  	}
   254  	if !checked {
   255  		t.Fatal("didn't call check")
   256  	}
   257  }
   258  
   259  func TestTest_importStateVerify(t *testing.T) {
   260  	mp := testProvider()
   261  	mp.DiffReturn = nil
   262  	mp.ApplyFn = func(
   263  		info *terraform.InstanceInfo,
   264  		state *terraform.InstanceState,
   265  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   266  		if !diff.Destroy {
   267  			return &terraform.InstanceState{
   268  				ID: "foo",
   269  				Attributes: map[string]string{
   270  					"foo": "bar",
   271  				},
   272  			}, nil
   273  		}
   274  
   275  		return nil, nil
   276  	}
   277  
   278  	mp.RefreshFn = func(
   279  		i *terraform.InstanceInfo,
   280  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   281  		if len(s.Attributes) == 0 {
   282  			s.Attributes = map[string]string{
   283  				"id":  s.ID,
   284  				"foo": "bar",
   285  			}
   286  		}
   287  
   288  		return s, nil
   289  	}
   290  
   291  	mp.ImportStateFn = func(
   292  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   293  		if id != "foo" {
   294  			return nil, fmt.Errorf("bad import ID: %s", id)
   295  		}
   296  
   297  		return []*terraform.InstanceState{
   298  			&terraform.InstanceState{
   299  				ID:        "foo",
   300  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   301  			},
   302  		}, nil
   303  	}
   304  
   305  	mt := new(mockT)
   306  	Test(mt, TestCase{
   307  		Providers: map[string]terraform.ResourceProvider{
   308  			"test": mp,
   309  		},
   310  
   311  		Steps: []TestStep{
   312  			TestStep{
   313  				Config: testConfigStr,
   314  			},
   315  			TestStep{
   316  				Config:            testConfigStr,
   317  				ResourceName:      "test_instance.foo",
   318  				ImportState:       true,
   319  				ImportStateVerify: true,
   320  			},
   321  		},
   322  	})
   323  
   324  	if mt.failed() {
   325  		t.Fatalf("test failed: %s", mt.failMessage())
   326  	}
   327  }
   328  
   329  func TestTest_importStateVerifyFail(t *testing.T) {
   330  	mp := testProvider()
   331  	mp.DiffReturn = nil
   332  	mp.ApplyFn = func(
   333  		info *terraform.InstanceInfo,
   334  		state *terraform.InstanceState,
   335  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   336  		if !diff.Destroy {
   337  			return &terraform.InstanceState{
   338  				ID: "foo",
   339  				Attributes: map[string]string{
   340  					"foo": "bar",
   341  				},
   342  			}, nil
   343  		}
   344  
   345  		return nil, nil
   346  	}
   347  
   348  	mp.RefreshFn = func(
   349  		i *terraform.InstanceInfo,
   350  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   351  		return s, nil
   352  	}
   353  
   354  	mp.ImportStateFn = func(
   355  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   356  		if id != "foo" {
   357  			return nil, fmt.Errorf("bad import ID: %s", id)
   358  		}
   359  
   360  		return []*terraform.InstanceState{
   361  			&terraform.InstanceState{
   362  				ID:        "foo",
   363  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   364  			},
   365  		}, nil
   366  	}
   367  
   368  	mt := new(mockT)
   369  	Test(mt, TestCase{
   370  		Providers: map[string]terraform.ResourceProvider{
   371  			"test": mp,
   372  		},
   373  
   374  		Steps: []TestStep{
   375  			TestStep{
   376  				Config: testConfigStr,
   377  			},
   378  			TestStep{
   379  				Config:            testConfigStr,
   380  				ResourceName:      "test_instance.foo",
   381  				ImportState:       true,
   382  				ImportStateVerify: true,
   383  			},
   384  		},
   385  	})
   386  
   387  	if !mt.failed() {
   388  		t.Fatalf("test should fail")
   389  	}
   390  }