github.com/lorbuschris/terraform@v0.11.12-beta1/backend/remote-state/inmem/backend_test.go (about)

     1  package inmem
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/backend"
     7  	"github.com/hashicorp/terraform/state/remote"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestBackend_impl(t *testing.T) {
    12  	var _ backend.Backend = new(Backend)
    13  }
    14  
    15  func TestBackendConfig(t *testing.T) {
    16  	defer Reset()
    17  	testID := "test_lock_id"
    18  
    19  	config := map[string]interface{}{
    20  		"lock_id": testID,
    21  	}
    22  
    23  	b := backend.TestBackendConfig(t, New(), config).(*Backend)
    24  
    25  	s, err := b.State(backend.DefaultStateName)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	c := s.(*remote.State).Client.(*RemoteClient)
    31  	if c.Name != backend.DefaultStateName {
    32  		t.Fatal("client name is not configured")
    33  	}
    34  
    35  	if err := locks.unlock(backend.DefaultStateName, testID); err != nil {
    36  		t.Fatalf("default state should have been locked: %s", err)
    37  	}
    38  }
    39  
    40  func TestBackend(t *testing.T) {
    41  	defer Reset()
    42  	b := backend.TestBackendConfig(t, New(), nil).(*Backend)
    43  	backend.TestBackendStates(t, b)
    44  }
    45  
    46  func TestBackendLocked(t *testing.T) {
    47  	defer Reset()
    48  	b1 := backend.TestBackendConfig(t, New(), nil).(*Backend)
    49  	b2 := backend.TestBackendConfig(t, New(), nil).(*Backend)
    50  
    51  	backend.TestBackendStateLocks(t, b1, b2)
    52  }
    53  
    54  // use the this backen to test the remote.State implementation
    55  func TestRemoteState(t *testing.T) {
    56  	defer Reset()
    57  	b := backend.TestBackendConfig(t, New(), nil)
    58  
    59  	workspace := "workspace"
    60  
    61  	// create a new workspace in this backend
    62  	s, err := b.State(workspace)
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	// force overwriting the remote state
    68  	newState := terraform.NewState()
    69  
    70  	if err := s.WriteState(newState); err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	if err := s.PersistState(); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if err := s.RefreshState(); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	savedState := s.State()
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	if savedState.Lineage != newState.Lineage {
    88  		t.Fatal("saved state has incorrect lineage")
    89  	}
    90  }