2020-08-11 17:23:42 +02:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2021-02-16 13:19:22 +01:00
|
|
|
"github.com/hashicorp/terraform/command/arguments"
|
|
|
|
"github.com/hashicorp/terraform/command/clistate"
|
|
|
|
"github.com/hashicorp/terraform/command/views"
|
2020-08-11 17:23:42 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/initwd"
|
2021-02-16 13:19:22 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2020-08-11 17:23:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalContext(t *testing.T) {
|
|
|
|
configDir := "./testdata/empty"
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
|
|
|
|
defer configCleanup()
|
|
|
|
|
2021-02-16 13:19:22 +01:00
|
|
|
streams, _ := terminal.StreamsForTesting(t)
|
|
|
|
view := views.NewView(streams)
|
|
|
|
stateLocker := clistate.NewLocker(0, views.NewStateLocker(arguments.ViewHuman, view))
|
|
|
|
|
2020-08-11 17:23:42 +02:00
|
|
|
op := &backend.Operation{
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
Workspace: backend.DefaultStateName,
|
2021-02-16 13:19:22 +01:00
|
|
|
StateLocker: stateLocker,
|
2020-08-11 17:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, _, diags := b.Context(op)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatalf("unexpected error: %s", diags.Err().Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context() retains a lock on success
|
|
|
|
assertBackendStateLocked(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalContext_error(t *testing.T) {
|
|
|
|
configDir := "./testdata/apply"
|
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
_, configLoader, configCleanup := initwd.MustLoadConfigForTests(t, configDir)
|
|
|
|
defer configCleanup()
|
|
|
|
|
2021-02-16 13:19:22 +01:00
|
|
|
streams, _ := terminal.StreamsForTesting(t)
|
|
|
|
view := views.NewView(streams)
|
|
|
|
stateLocker := clistate.NewLocker(0, views.NewStateLocker(arguments.ViewHuman, view))
|
|
|
|
|
2020-08-11 17:23:42 +02:00
|
|
|
op := &backend.Operation{
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
Workspace: backend.DefaultStateName,
|
2021-02-16 13:19:22 +01:00
|
|
|
StateLocker: stateLocker,
|
2020-08-11 17:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, _, diags := b.Context(op)
|
|
|
|
if !diags.HasErrors() {
|
|
|
|
t.Fatal("unexpected success")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context() unlocks the state on failure
|
|
|
|
assertBackendStateUnlocked(t, b)
|
|
|
|
}
|