github.com/opentofu/opentofu@v1.7.1/internal/backend/remote-state/consul/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 consul
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/hashicorp/consul/sdk/testutil"
    17  	"github.com/opentofu/opentofu/internal/backend"
    18  	"github.com/opentofu/opentofu/internal/encryption"
    19  )
    20  
    21  func TestBackend_impl(t *testing.T) {
    22  	var _ backend.Backend = new(Backend)
    23  }
    24  
    25  func newConsulTestServer(t *testing.T) *testutil.TestServer {
    26  	if os.Getenv("TF_ACC") == "" && os.Getenv("TF_CONSUL_TEST") == "" {
    27  		t.Skipf("consul server tests require setting TF_ACC or TF_CONSUL_TEST")
    28  	}
    29  
    30  	srv, err := testutil.NewTestServerConfigT(t, func(c *testutil.TestServerConfig) {
    31  		c.LogLevel = "warn"
    32  
    33  		if !flag.Parsed() {
    34  			flag.Parse()
    35  		}
    36  
    37  		if !testing.Verbose() {
    38  			c.Stdout = io.Discard
    39  			c.Stderr = io.Discard
    40  		}
    41  	})
    42  
    43  	if err != nil {
    44  		t.Fatalf("failed to create consul test server: %s", err)
    45  	}
    46  
    47  	srv.WaitForSerfCheck(t)
    48  	srv.WaitForLeader(t)
    49  
    50  	return srv
    51  }
    52  
    53  func TestBackend(t *testing.T) {
    54  	srv := newConsulTestServer(t)
    55  	defer func() { _ = srv.Stop() }()
    56  
    57  	path := fmt.Sprintf("tf-unit/%s", time.Now().String())
    58  
    59  	// Get the backend. We need two to test locking.
    60  	b1 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    61  		"address": srv.HTTPAddr,
    62  		"path":    path,
    63  	}))
    64  
    65  	b2 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    66  		"address": srv.HTTPAddr,
    67  		"path":    path,
    68  	}))
    69  
    70  	// Test
    71  	backend.TestBackendStates(t, b1)
    72  	backend.TestBackendStateLocks(t, b1, b2)
    73  }
    74  
    75  func TestBackend_lockDisabled(t *testing.T) {
    76  	srv := newConsulTestServer(t)
    77  	defer func() { _ = srv.Stop() }()
    78  
    79  	path := fmt.Sprintf("tf-unit/%s", time.Now().String())
    80  
    81  	// Get the backend. We need two to test locking.
    82  	b1 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    83  		"address": srv.HTTPAddr,
    84  		"path":    path,
    85  		"lock":    false,
    86  	}))
    87  
    88  	b2 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
    89  		"address": srv.HTTPAddr,
    90  		"path":    path + "different", // Diff so locking test would fail if it was locking
    91  		"lock":    false,
    92  	}))
    93  
    94  	// Test
    95  	backend.TestBackendStates(t, b1)
    96  	backend.TestBackendStateLocks(t, b1, b2)
    97  }
    98  
    99  func TestBackend_gzip(t *testing.T) {
   100  	srv := newConsulTestServer(t)
   101  	defer func() { _ = srv.Stop() }()
   102  
   103  	// Get the backend
   104  	b := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), backend.TestWrapConfig(map[string]interface{}{
   105  		"address": srv.HTTPAddr,
   106  		"path":    fmt.Sprintf("tf-unit/%s", time.Now().String()),
   107  		"gzip":    true,
   108  	}))
   109  
   110  	// Test
   111  	backend.TestBackendStates(t, b)
   112  }