github.com/opentofu/opentofu@v1.7.1/internal/backend/remote-state/kubernetes/client_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 kubernetes
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/opentofu/opentofu/internal/backend"
    12  	"github.com/opentofu/opentofu/internal/encryption"
    13  	"github.com/opentofu/opentofu/internal/states/remote"
    14  	"github.com/opentofu/opentofu/internal/states/statemgr"
    15  )
    16  
    17  func TestRemoteClient_impl(t *testing.T) {
    18  	var _ remote.Client = new(RemoteClient)
    19  	var _ remote.ClientLocker = new(RemoteClient)
    20  }
    21  
    22  func TestRemoteClient(t *testing.T) {
    23  	testACC(t)
    24  	defer cleanupK8sResources(t)
    25  
    26  	b := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    27  		"secret_suffix": secretSuffix,
    28  	}))
    29  
    30  	state, err := b.StateMgr(backend.DefaultStateName)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	remote.TestClient(t, state.(*remote.State).Client)
    36  }
    37  
    38  func TestRemoteClientLocks(t *testing.T) {
    39  	testACC(t)
    40  	defer cleanupK8sResources(t)
    41  
    42  	b1 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    43  		"secret_suffix": secretSuffix,
    44  	}))
    45  
    46  	b2 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    47  		"secret_suffix": secretSuffix,
    48  	}))
    49  
    50  	s1, err := b1.StateMgr(backend.DefaultStateName)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	s2, err := b2.StateMgr(backend.DefaultStateName)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
    61  }
    62  
    63  func TestForceUnlock(t *testing.T) {
    64  	testACC(t)
    65  	defer cleanupK8sResources(t)
    66  
    67  	b1 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    68  		"secret_suffix": secretSuffix,
    69  	}))
    70  
    71  	b2 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    72  		"secret_suffix": secretSuffix,
    73  	}))
    74  
    75  	// first test with default
    76  	s1, err := b1.StateMgr(backend.DefaultStateName)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	info := statemgr.NewLockInfo()
    82  	info.Operation = "test"
    83  	info.Who = "clientA"
    84  
    85  	lockID, err := s1.Lock(info)
    86  	if err != nil {
    87  		t.Fatal("unable to get initial lock:", err)
    88  	}
    89  
    90  	// s1 is now locked, get the same state through s2 and unlock it
    91  	s2, err := b2.StateMgr(backend.DefaultStateName)
    92  	if err != nil {
    93  		t.Fatal("failed to get default state to force unlock:", err)
    94  	}
    95  
    96  	if err := s2.Unlock(lockID); err != nil {
    97  		t.Fatal("failed to force-unlock default state")
    98  	}
    99  
   100  	// now try the same thing with a named state
   101  	// first test with default
   102  	s1, err = b1.StateMgr("test")
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	info = statemgr.NewLockInfo()
   108  	info.Operation = "test"
   109  	info.Who = "clientA"
   110  
   111  	lockID, err = s1.Lock(info)
   112  	if err != nil {
   113  		t.Fatal("unable to get initial lock:", err)
   114  	}
   115  
   116  	// s1 is now locked, get the same state through s2 and unlock it
   117  	s2, err = b2.StateMgr("test")
   118  	if err != nil {
   119  		t.Fatal("failed to get named state to force unlock:", err)
   120  	}
   121  
   122  	if err = s2.Unlock(lockID); err != nil {
   123  		t.Fatal("failed to force-unlock named state")
   124  	}
   125  }