github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/pg/client_test.go (about)

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