github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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_importStateVerify(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  				Attributes: map[string]string{
   192  					"foo": "bar",
   193  				},
   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  		if len(s.Attributes) == 0 {
   204  			s.Attributes = map[string]string{
   205  				"id":  s.ID,
   206  				"foo": "bar",
   207  			}
   208  		}
   209  
   210  		return s, nil
   211  	}
   212  
   213  	mp.ImportStateFn = func(
   214  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   215  		if id != "foo" {
   216  			return nil, fmt.Errorf("bad import ID: %s", id)
   217  		}
   218  
   219  		return []*terraform.InstanceState{
   220  			&terraform.InstanceState{
   221  				ID:        "foo",
   222  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   223  			},
   224  		}, nil
   225  	}
   226  
   227  	mt := new(mockT)
   228  	Test(mt, TestCase{
   229  		Providers: map[string]terraform.ResourceProvider{
   230  			"test": mp,
   231  		},
   232  
   233  		Steps: []TestStep{
   234  			TestStep{
   235  				Config: testConfigStr,
   236  			},
   237  			TestStep{
   238  				ResourceName:      "test_instance.foo",
   239  				ImportState:       true,
   240  				ImportStateVerify: true,
   241  			},
   242  		},
   243  	})
   244  
   245  	if mt.failed() {
   246  		t.Fatalf("test failed: %s", mt.failMessage())
   247  	}
   248  }
   249  
   250  func TestTest_importStateVerifyFail(t *testing.T) {
   251  	mp := testProvider()
   252  	mp.DiffReturn = nil
   253  	mp.ApplyFn = func(
   254  		info *terraform.InstanceInfo,
   255  		state *terraform.InstanceState,
   256  		diff *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   257  		if !diff.Destroy {
   258  			return &terraform.InstanceState{
   259  				ID: "foo",
   260  				Attributes: map[string]string{
   261  					"foo": "bar",
   262  				},
   263  			}, nil
   264  		}
   265  
   266  		return nil, nil
   267  	}
   268  
   269  	mp.RefreshFn = func(
   270  		i *terraform.InstanceInfo,
   271  		s *terraform.InstanceState) (*terraform.InstanceState, error) {
   272  		return s, nil
   273  	}
   274  
   275  	mp.ImportStateFn = func(
   276  		info *terraform.InstanceInfo, id string) ([]*terraform.InstanceState, error) {
   277  		if id != "foo" {
   278  			return nil, fmt.Errorf("bad import ID: %s", id)
   279  		}
   280  
   281  		return []*terraform.InstanceState{
   282  			&terraform.InstanceState{
   283  				ID:        "foo",
   284  				Ephemeral: terraform.EphemeralState{Type: "test_instance"},
   285  			},
   286  		}, nil
   287  	}
   288  
   289  	mt := new(mockT)
   290  	Test(mt, TestCase{
   291  		Providers: map[string]terraform.ResourceProvider{
   292  			"test": mp,
   293  		},
   294  
   295  		Steps: []TestStep{
   296  			TestStep{
   297  				Config: testConfigStr,
   298  			},
   299  			TestStep{
   300  				ResourceName:      "test_instance.foo",
   301  				ImportState:       true,
   302  				ImportStateVerify: true,
   303  			},
   304  		},
   305  	})
   306  
   307  	if !mt.failed() {
   308  		t.Fatalf("test should fail")
   309  	}
   310  }