github.com/hugorut/terraform@v1.1.3/src/cloud/backend_refresh_test.go (about)

     1  package cloud
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hugorut/terraform/src/backend"
    10  	"github.com/hugorut/terraform/src/command/arguments"
    11  	"github.com/hugorut/terraform/src/command/clistate"
    12  	"github.com/hugorut/terraform/src/command/views"
    13  	"github.com/hugorut/terraform/src/initwd"
    14  	"github.com/hugorut/terraform/src/plans"
    15  	"github.com/hugorut/terraform/src/states/statemgr"
    16  	"github.com/hugorut/terraform/src/terminal"
    17  	"github.com/mitchellh/cli"
    18  )
    19  
    20  func testOperationRefresh(t *testing.T, configDir string) (*backend.Operation, func(), func(*testing.T) *terminal.TestOutput) {
    21  	t.Helper()
    22  
    23  	return testOperationRefreshWithTimeout(t, configDir, 0)
    24  }
    25  
    26  func testOperationRefreshWithTimeout(t *testing.T, configDir string, timeout time.Duration) (*backend.Operation, func(), func(*testing.T) *terminal.TestOutput) {
    27  	t.Helper()
    28  
    29  	_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
    30  
    31  	streams, done := terminal.StreamsForTesting(t)
    32  	view := views.NewView(streams)
    33  	stateLockerView := views.NewStateLocker(arguments.ViewHuman, view)
    34  	operationView := views.NewOperation(arguments.ViewHuman, false, view)
    35  
    36  	return &backend.Operation{
    37  		ConfigDir:    configDir,
    38  		ConfigLoader: configLoader,
    39  		PlanRefresh:  true,
    40  		StateLocker:  clistate.NewLocker(timeout, stateLockerView),
    41  		Type:         backend.OperationTypeRefresh,
    42  		View:         operationView,
    43  	}, configCleanup, done
    44  }
    45  
    46  func TestCloud_refreshBasicActuallyRunsApplyRefresh(t *testing.T) {
    47  	b, bCleanup := testBackendWithName(t)
    48  	defer bCleanup()
    49  
    50  	op, configCleanup, done := testOperationRefresh(t, "./testdata/refresh")
    51  	defer configCleanup()
    52  	defer done(t)
    53  
    54  	op.UIOut = b.CLI
    55  	b.CLIColor = b.cliColorize()
    56  	op.PlanMode = plans.RefreshOnlyMode
    57  	op.Workspace = testBackendSingleWorkspaceName
    58  
    59  	run, err := b.Operation(context.Background(), op)
    60  	if err != nil {
    61  		t.Fatalf("error starting operation: %v", err)
    62  	}
    63  
    64  	<-run.Done()
    65  	if run.Result != backend.OperationSuccess {
    66  		t.Fatalf("operation failed: %s", b.CLI.(*cli.MockUi).ErrorWriter.String())
    67  	}
    68  
    69  	output := b.CLI.(*cli.MockUi).OutputWriter.String()
    70  	if !strings.Contains(output, "Proceeding with 'terraform apply -refresh-only -auto-approve'") {
    71  		t.Fatalf("expected TFC header in output: %s", output)
    72  	}
    73  
    74  	stateMgr, _ := b.StateMgr(testBackendSingleWorkspaceName)
    75  	// An error suggests that the state was not unlocked after apply
    76  	if _, err := stateMgr.Lock(statemgr.NewLockInfo()); err != nil {
    77  		t.Fatalf("unexpected error locking state after apply: %s", err.Error())
    78  	}
    79  }