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