opentofu/backend/remote-state/inmem/backend_test.go
Martin Atkins 39e609d5fd vendor: switch to HCL 2.0 in the HCL repository
Previously we were using the experimental HCL 2 repository, but now we'll
shift over to the v2 import path within the main HCL repository as part of
actually releasing HCL 2.0 as stable.

This is a mechanical search/replace to the new import paths. It also
switches to the v2.0.0 release of HCL, which includes some new code that
Terraform didn't previously have but should not change any behavior that
matters for Terraform's purposes.

For the moment the experimental HCL2 repository is still an indirect
dependency via terraform-config-inspect, so it remains in our go.sum and
vendor directories for the moment. Because terraform-config-inspect uses
a much smaller subset of the HCL2 functionality, this does still manage
to prune the vendor directory a little. A subsequent release of
terraform-config-inspect should allow us to completely remove that old
repository in a future commit.
2019-10-02 15:10:21 -07:00

103 lines
2.1 KiB
Go

package inmem
import (
"flag"
"io/ioutil"
"log"
"os"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/state/remote"
statespkg "github.com/hashicorp/terraform/states"
)
func TestMain(m *testing.M) {
flag.Parse()
if testing.Verbose() {
// if we're verbose, use the logging requested by TF_LOG
logging.SetOutput()
} else {
// otherwise silence all logs
log.SetOutput(ioutil.Discard)
}
os.Exit(m.Run())
}
func TestBackend_impl(t *testing.T) {
var _ backend.Backend = new(Backend)
}
func TestBackendConfig(t *testing.T) {
defer Reset()
testID := "test_lock_id"
config := map[string]interface{}{
"lock_id": testID,
}
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
s, err := b.StateMgr(backend.DefaultStateName)
if err != nil {
t.Fatal(err)
}
c := s.(*remote.State).Client.(*RemoteClient)
if c.Name != backend.DefaultStateName {
t.Fatal("client name is not configured")
}
if err := locks.unlock(backend.DefaultStateName, testID); err != nil {
t.Fatalf("default state should have been locked: %s", err)
}
}
func TestBackend(t *testing.T) {
defer Reset()
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
backend.TestBackendStates(t, b)
}
func TestBackendLocked(t *testing.T) {
defer Reset()
b1 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
b2 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
backend.TestBackendStateLocks(t, b1, b2)
}
// use the this backen to test the remote.State implementation
func TestRemoteState(t *testing.T) {
defer Reset()
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody())
workspace := "workspace"
// create a new workspace in this backend
s, err := b.StateMgr(workspace)
if err != nil {
t.Fatal(err)
}
// force overwriting the remote state
newState := statespkg.NewState()
if err := s.WriteState(newState); err != nil {
t.Fatal(err)
}
if err := s.PersistState(); err != nil {
t.Fatal(err)
}
if err := s.RefreshState(); err != nil {
t.Fatal(err)
}
}