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