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