2017-08-01 18:11:04 +02:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
2018-10-02 23:56:29 +02:00
|
|
|
"flag"
|
|
|
|
"os"
|
2017-08-01 18:11:04 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-10-02 23:56:29 +02:00
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2018-10-01 23:35:23 +02:00
|
|
|
statespkg "github.com/hashicorp/terraform/states"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
2020-10-18 16:01:48 +02:00
|
|
|
|
|
|
|
_ "github.com/hashicorp/terraform/internal/logging"
|
2017-08-01 18:11:04 +02:00
|
|
|
)
|
|
|
|
|
2018-10-02 23:56:29 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:11:04 +02:00
|
|
|
func TestBackend_impl(t *testing.T) {
|
|
|
|
var _ backend.Backend = new(Backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig(t *testing.T) {
|
2017-08-01 22:15:52 +02:00
|
|
|
defer Reset()
|
2017-08-01 18:11:04 +02:00
|
|
|
testID := "test_lock_id"
|
|
|
|
|
|
|
|
config := map[string]interface{}{
|
|
|
|
"lock_id": testID,
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
|
2017-08-01 18:11:04 +02:00
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
2017-08-01 18:11:04 +02:00
|
|
|
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) {
|
2017-08-01 22:15:52 +02:00
|
|
|
defer Reset()
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStates(t, b)
|
2017-08-01 18:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendLocked(t *testing.T) {
|
2017-08-01 22:15:52 +02:00
|
|
|
defer Reset()
|
2018-03-21 02:43:02 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
|
|
|
|
b2 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
|
2017-08-01 18:11:04 +02:00
|
|
|
|
2018-02-21 03:05:58 +01:00
|
|
|
backend.TestBackendStateLocks(t, b1, b2)
|
2017-08-01 18:11:04 +02:00
|
|
|
}
|
2017-08-01 23:50:53 +02:00
|
|
|
|
|
|
|
// use the this backen to test the remote.State implementation
|
|
|
|
func TestRemoteState(t *testing.T) {
|
|
|
|
defer Reset()
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), hcl.EmptyBody())
|
2017-08-01 23:50:53 +02:00
|
|
|
|
|
|
|
workspace := "workspace"
|
|
|
|
|
|
|
|
// create a new workspace in this backend
|
2018-10-01 23:35:23 +02:00
|
|
|
s, err := b.StateMgr(workspace)
|
2017-08-01 23:50:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// force overwriting the remote state
|
2018-10-01 23:35:23 +02:00
|
|
|
newState := statespkg.NewState()
|
2017-08-01 23:50:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|