github.com/jzbruno/terraform@v0.10.3-0.20180104230435-18975d727047/backend/local/backend_refresh_test.go (about)

     1  package local
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/backend"
     9  	"github.com/hashicorp/terraform/config/module"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestLocal_refresh(t *testing.T) {
    14  	b := TestLocal(t)
    15  	p := TestLocalProvider(t, b, "test")
    16  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
    17  
    18  	p.RefreshFn = nil
    19  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
    20  
    21  	mod, modCleanup := module.TestTree(t, "./test-fixtures/refresh")
    22  	defer modCleanup()
    23  
    24  	op := testOperationRefresh()
    25  	op.Module = mod
    26  
    27  	run, err := b.Operation(context.Background(), op)
    28  	if err != nil {
    29  		t.Fatalf("bad: %s", err)
    30  	}
    31  	<-run.Done()
    32  
    33  	if !p.RefreshCalled {
    34  		t.Fatal("refresh should be called")
    35  	}
    36  
    37  	checkState(t, b.StateOutPath, `
    38  test_instance.foo:
    39    ID = yes
    40    provider = provider.test
    41  	`)
    42  }
    43  
    44  func TestLocal_refreshNilModule(t *testing.T) {
    45  	b := TestLocal(t)
    46  	p := TestLocalProvider(t, b, "test")
    47  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
    48  
    49  	p.RefreshFn = nil
    50  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
    51  
    52  	op := testOperationRefresh()
    53  	op.Module = nil
    54  
    55  	run, err := b.Operation(context.Background(), op)
    56  	if err != nil {
    57  		t.Fatalf("bad: %s", err)
    58  	}
    59  	<-run.Done()
    60  
    61  	if !p.RefreshCalled {
    62  		t.Fatal("refresh should be called")
    63  	}
    64  
    65  	checkState(t, b.StateOutPath, `
    66  test_instance.foo:
    67    ID = yes
    68    provider = provider.test
    69  	`)
    70  }
    71  
    72  // GH-12174
    73  func TestLocal_refreshNilModuleWithInput(t *testing.T) {
    74  	b := TestLocal(t)
    75  	p := TestLocalProvider(t, b, "test")
    76  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
    77  
    78  	p.RefreshFn = nil
    79  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
    80  
    81  	b.OpInput = true
    82  
    83  	op := testOperationRefresh()
    84  	op.Module = nil
    85  
    86  	run, err := b.Operation(context.Background(), op)
    87  	if err != nil {
    88  		t.Fatalf("bad: %s", err)
    89  	}
    90  	<-run.Done()
    91  
    92  	if !p.RefreshCalled {
    93  		t.Fatal("refresh should be called")
    94  	}
    95  
    96  	checkState(t, b.StateOutPath, `
    97  test_instance.foo:
    98    ID = yes
    99    provider = provider.test
   100  	`)
   101  }
   102  
   103  func TestLocal_refreshInput(t *testing.T) {
   104  	b := TestLocal(t)
   105  	p := TestLocalProvider(t, b, "test")
   106  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
   107  
   108  	p.ConfigureFn = func(c *terraform.ResourceConfig) error {
   109  		if v, ok := c.Get("value"); !ok || v != "bar" {
   110  			return fmt.Errorf("no value set")
   111  		}
   112  
   113  		return nil
   114  	}
   115  
   116  	p.RefreshFn = nil
   117  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
   118  
   119  	mod, modCleanup := module.TestTree(t, "./test-fixtures/refresh-var-unset")
   120  	defer modCleanup()
   121  
   122  	// Enable input asking since it is normally disabled by default
   123  	b.OpInput = true
   124  	b.ContextOpts.UIInput = &terraform.MockUIInput{InputReturnString: "bar"}
   125  
   126  	op := testOperationRefresh()
   127  	op.Module = mod
   128  	op.UIIn = b.ContextOpts.UIInput
   129  
   130  	run, err := b.Operation(context.Background(), op)
   131  	if err != nil {
   132  		t.Fatalf("bad: %s", err)
   133  	}
   134  	<-run.Done()
   135  
   136  	if !p.RefreshCalled {
   137  		t.Fatal("refresh should be called")
   138  	}
   139  
   140  	checkState(t, b.StateOutPath, `
   141  test_instance.foo:
   142    ID = yes
   143    provider = provider.test
   144  	`)
   145  }
   146  
   147  func TestLocal_refreshValidate(t *testing.T) {
   148  	b := TestLocal(t)
   149  	p := TestLocalProvider(t, b, "test")
   150  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
   151  
   152  	p.RefreshFn = nil
   153  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
   154  
   155  	mod, modCleanup := module.TestTree(t, "./test-fixtures/refresh")
   156  	defer modCleanup()
   157  
   158  	// Enable validation
   159  	b.OpValidation = true
   160  
   161  	op := testOperationRefresh()
   162  	op.Module = mod
   163  
   164  	run, err := b.Operation(context.Background(), op)
   165  	if err != nil {
   166  		t.Fatalf("bad: %s", err)
   167  	}
   168  	<-run.Done()
   169  
   170  	if !p.ValidateCalled {
   171  		t.Fatal("validate should be called")
   172  	}
   173  
   174  	checkState(t, b.StateOutPath, `
   175  test_instance.foo:
   176    ID = yes
   177    provider = provider.test
   178  	`)
   179  }
   180  
   181  func testOperationRefresh() *backend.Operation {
   182  	return &backend.Operation{
   183  		Type: backend.OperationTypeRefresh,
   184  	}
   185  }
   186  
   187  // testRefreshState is just a common state that we use for testing refresh.
   188  func testRefreshState() *terraform.State {
   189  	return &terraform.State{
   190  		Version: 2,
   191  		Modules: []*terraform.ModuleState{
   192  			&terraform.ModuleState{
   193  				Path: []string{"root"},
   194  				Resources: map[string]*terraform.ResourceState{
   195  					"test_instance.foo": &terraform.ResourceState{
   196  						Type: "test_instance",
   197  						Primary: &terraform.InstanceState{
   198  							ID: "bar",
   199  						},
   200  					},
   201  				},
   202  				Outputs: map[string]*terraform.OutputState{},
   203  			},
   204  		},
   205  	}
   206  }