github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/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_refreshInput(t *testing.T) {
    44  	b := TestLocal(t)
    45  	p := TestLocalProvider(t, b, "test")
    46  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
    47  
    48  	p.ConfigureFn = func(c *terraform.ResourceConfig) error {
    49  		if v, ok := c.Get("value"); !ok || v != "bar" {
    50  			return fmt.Errorf("no value set")
    51  		}
    52  
    53  		return nil
    54  	}
    55  
    56  	p.RefreshFn = nil
    57  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
    58  
    59  	mod, modCleanup := module.TestTree(t, "./test-fixtures/refresh-var-unset")
    60  	defer modCleanup()
    61  
    62  	// Enable input asking since it is normally disabled by default
    63  	b.OpInput = true
    64  	b.ContextOpts.UIInput = &terraform.MockUIInput{InputReturnString: "bar"}
    65  
    66  	op := testOperationRefresh()
    67  	op.Module = mod
    68  	op.UIIn = b.ContextOpts.UIInput
    69  
    70  	run, err := b.Operation(context.Background(), op)
    71  	if err != nil {
    72  		t.Fatalf("bad: %s", err)
    73  	}
    74  	<-run.Done()
    75  
    76  	if !p.RefreshCalled {
    77  		t.Fatal("refresh should be called")
    78  	}
    79  
    80  	checkState(t, b.StateOutPath, `
    81  test_instance.foo:
    82    ID = yes
    83  	`)
    84  }
    85  
    86  func TestLocal_refreshValidate(t *testing.T) {
    87  	b := TestLocal(t)
    88  	p := TestLocalProvider(t, b, "test")
    89  	terraform.TestStateFile(t, b.StatePath, testRefreshState())
    90  
    91  	p.RefreshFn = nil
    92  	p.RefreshReturn = &terraform.InstanceState{ID: "yes"}
    93  
    94  	mod, modCleanup := module.TestTree(t, "./test-fixtures/refresh")
    95  	defer modCleanup()
    96  
    97  	// Enable validation
    98  	b.OpValidation = true
    99  
   100  	op := testOperationRefresh()
   101  	op.Module = mod
   102  
   103  	run, err := b.Operation(context.Background(), op)
   104  	if err != nil {
   105  		t.Fatalf("bad: %s", err)
   106  	}
   107  	<-run.Done()
   108  
   109  	if !p.ValidateCalled {
   110  		t.Fatal("validate should be called")
   111  	}
   112  
   113  	checkState(t, b.StateOutPath, `
   114  test_instance.foo:
   115    ID = yes
   116  	`)
   117  }
   118  
   119  func testOperationRefresh() *backend.Operation {
   120  	return &backend.Operation{
   121  		Type: backend.OperationTypeRefresh,
   122  	}
   123  }
   124  
   125  // testRefreshState is just a common state that we use for testing refresh.
   126  func testRefreshState() *terraform.State {
   127  	return &terraform.State{
   128  		Version: 2,
   129  		Modules: []*terraform.ModuleState{
   130  			&terraform.ModuleState{
   131  				Path: []string{"root"},
   132  				Resources: map[string]*terraform.ResourceState{
   133  					"test_instance.foo": &terraform.ResourceState{
   134  						Type: "test_instance",
   135  						Primary: &terraform.InstanceState{
   136  							ID: "bar",
   137  						},
   138  					},
   139  				},
   140  				Outputs: map[string]*terraform.OutputState{},
   141  			},
   142  		},
   143  	}
   144  }