github.com/opentofu/opentofu@v1.7.1/internal/backend/remote-state/pg/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 pg
     7  
     8  // Create the test database: createdb terraform_backend_pg_test
     9  // TF_ACC=1 GO111MODULE=on go test -v -mod=vendor -timeout=2m -parallel=4 github.com/opentofu/opentofu/backend/remote-state/pg
    10  
    11  import (
    12  	"database/sql"
    13  	"fmt"
    14  	"testing"
    15  
    16  	"github.com/opentofu/opentofu/internal/backend"
    17  	"github.com/opentofu/opentofu/internal/encryption"
    18  	"github.com/opentofu/opentofu/internal/states/remote"
    19  )
    20  
    21  func TestRemoteClient_impl(t *testing.T) {
    22  	var _ remote.Client = new(RemoteClient)
    23  	var _ remote.ClientLocker = new(RemoteClient)
    24  }
    25  
    26  func TestRemoteClient(t *testing.T) {
    27  	testACC(t)
    28  	connStr := getDatabaseUrl()
    29  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
    30  	dbCleaner, err := sql.Open("postgres", connStr)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
    35  
    36  	config := backend.TestWrapConfig(map[string]interface{}{
    37  		"conn_str":    connStr,
    38  		"schema_name": schemaName,
    39  	})
    40  	b := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), config).(*Backend)
    41  
    42  	if b == nil {
    43  		t.Fatal("Backend could not be configured")
    44  	}
    45  
    46  	s, err := b.StateMgr(backend.DefaultStateName)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	remote.TestClient(t, s.(*remote.State).Client)
    52  }
    53  
    54  func TestRemoteLocks(t *testing.T) {
    55  	testACC(t)
    56  	connStr := getDatabaseUrl()
    57  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
    58  	dbCleaner, err := sql.Open("postgres", connStr)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
    63  
    64  	config := backend.TestWrapConfig(map[string]interface{}{
    65  		"conn_str":    connStr,
    66  		"schema_name": schemaName,
    67  	})
    68  
    69  	b1 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), config).(*Backend)
    70  	s1, err := b1.StateMgr(backend.DefaultStateName)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	b2 := backend.TestBackendConfig(t, New(encryption.StateEncryptionDisabled()), config).(*Backend)
    76  	s2, err := b2.StateMgr(backend.DefaultStateName)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
    82  }