github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/inmem/backend_test.go (about)

     1  package inmem
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/hcl/v2"
    11  
    12  	"github.com/hashicorp/terraform/backend"
    13  	"github.com/hashicorp/terraform/helper/logging"
    14  	"github.com/hashicorp/terraform/state/remote"
    15  	statespkg "github.com/hashicorp/terraform/states"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	flag.Parse()
    20  
    21  	if testing.Verbose() {
    22  		// if we're verbose, use the logging requested by TF_LOG
    23  		logging.SetOutput()
    24  	} else {
    25  		// otherwise silence all logs
    26  		log.SetOutput(ioutil.Discard)
    27  	}
    28  
    29  	os.Exit(m.Run())
    30  }
    31  
    32  func TestBackend_impl(t *testing.T) {
    33  	var _ backend.Backend = new(Backend)
    34  }
    35  
    36  func TestBackendConfig(t *testing.T) {
    37  	defer Reset()
    38  	testID := "test_lock_id"
    39  
    40  	config := map[string]interface{}{
    41  		"lock_id": testID,
    42  	}
    43  
    44  	b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
    45  
    46  	s, err := b.StateMgr(backend.DefaultStateName)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	c := s.(*remote.State).Client.(*RemoteClient)
    52  	if c.Name != backend.DefaultStateName {
    53  		t.Fatal("client name is not configured")
    54  	}
    55  
    56  	if err := locks.unlock(backend.DefaultStateName, testID); err != nil {
    57  		t.Fatalf("default state should have been locked: %s", err)
    58  	}
    59  }
    60  
    61  func TestBackend(t *testing.T) {
    62  	defer Reset()
    63  	b := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
    64  	backend.TestBackendStates(t, b)
    65  }
    66  
    67  func TestBackendLocked(t *testing.T) {
    68  	defer Reset()
    69  	b1 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
    70  	b2 := backend.TestBackendConfig(t, New(), hcl.EmptyBody()).(*Backend)
    71  
    72  	backend.TestBackendStateLocks(t, b1, b2)
    73  }
    74  
    75  // use the this backen to test the remote.State implementation
    76  func TestRemoteState(t *testing.T) {
    77  	defer Reset()
    78  	b := backend.TestBackendConfig(t, New(), hcl.EmptyBody())
    79  
    80  	workspace := "workspace"
    81  
    82  	// create a new workspace in this backend
    83  	s, err := b.StateMgr(workspace)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	// force overwriting the remote state
    89  	newState := statespkg.NewState()
    90  
    91  	if err := s.WriteState(newState); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if err := s.PersistState(); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	if err := s.RefreshState(); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  }