github.com/opentofu/opentofu@v1.7.1/internal/backend/remote-state/inmem/backend_test.go (about)

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