github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/pg/backend_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/hashicorp/terraform/backend/remote-state/pg
     5  
     6  import (
     7  	"database/sql"
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/hashicorp/terraform/backend"
    13  	"github.com/hashicorp/terraform/state/remote"
    14  	_ "github.com/lib/pq"
    15  )
    16  
    17  // Function to skip a test unless in ACCeptance test mode.
    18  //
    19  // A running Postgres server identified by env variable
    20  // DATABASE_URL is required for acceptance tests.
    21  func testACC(t *testing.T) {
    22  	skip := os.Getenv("TF_ACC") == ""
    23  	if skip {
    24  		t.Log("pg backend tests require setting TF_ACC")
    25  		t.Skip()
    26  	}
    27  	if os.Getenv("DATABASE_URL") == "" {
    28  		os.Setenv("DATABASE_URL", "postgres://localhost/terraform_backend_pg_test?sslmode=disable")
    29  	}
    30  }
    31  
    32  func TestBackend_impl(t *testing.T) {
    33  	var _ backend.Backend = new(Backend)
    34  }
    35  
    36  func TestBackendConfig(t *testing.T) {
    37  	testACC(t)
    38  	connStr := getDatabaseUrl()
    39  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
    40  	dbCleaner, err := sql.Open("postgres", connStr)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
    45  
    46  	config := backend.TestWrapConfig(map[string]interface{}{
    47  		"conn_str":    connStr,
    48  		"schema_name": schemaName,
    49  	})
    50  	b := backend.TestBackendConfig(t, New(), config).(*Backend)
    51  
    52  	if b == nil {
    53  		t.Fatal("Backend could not be configured")
    54  	}
    55  
    56  	_, err = b.db.Query(fmt.Sprintf("SELECT name, data FROM %s.%s LIMIT 1", schemaName, statesTableName))
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	_, err = b.StateMgr(backend.DefaultStateName)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  
    66  	s, err := b.StateMgr(backend.DefaultStateName)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	c := s.(*remote.State).Client.(*RemoteClient)
    71  	if c.Name != backend.DefaultStateName {
    72  		t.Fatal("RemoteClient name is not configured")
    73  	}
    74  }
    75  
    76  func TestBackendConfigSkipSchema(t *testing.T) {
    77  	testACC(t)
    78  	connStr := getDatabaseUrl()
    79  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
    80  	db, err := sql.Open("postgres", connStr)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	// create the schema as a prerequisites
    86  	db.Query(fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s", schemaName))
    87  	defer db.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
    88  
    89  	config := backend.TestWrapConfig(map[string]interface{}{
    90  		"conn_str":             connStr,
    91  		"schema_name":          schemaName,
    92  		"skip_schema_creation": true,
    93  	})
    94  	b := backend.TestBackendConfig(t, New(), config).(*Backend)
    95  
    96  	if b == nil {
    97  		t.Fatal("Backend could not be configured")
    98  	}
    99  
   100  	_, err = b.db.Query(fmt.Sprintf("SELECT name, data FROM %s.%s LIMIT 1", schemaName, statesTableName))
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	_, err = b.StateMgr(backend.DefaultStateName)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	s, err := b.StateMgr(backend.DefaultStateName)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	c := s.(*remote.State).Client.(*RemoteClient)
   115  	if c.Name != backend.DefaultStateName {
   116  		t.Fatal("RemoteClient name is not configured")
   117  	}
   118  }
   119  
   120  func TestBackendStates(t *testing.T) {
   121  	testACC(t)
   122  	connStr := getDatabaseUrl()
   123  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
   124  	dbCleaner, err := sql.Open("postgres", connStr)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
   129  
   130  	config := backend.TestWrapConfig(map[string]interface{}{
   131  		"conn_str":    connStr,
   132  		"schema_name": schemaName,
   133  	})
   134  	b := backend.TestBackendConfig(t, New(), config).(*Backend)
   135  
   136  	if b == nil {
   137  		t.Fatal("Backend could not be configured")
   138  	}
   139  
   140  	backend.TestBackendStates(t, b)
   141  }
   142  
   143  func TestBackendStateLocks(t *testing.T) {
   144  	testACC(t)
   145  	connStr := getDatabaseUrl()
   146  	schemaName := fmt.Sprintf("terraform_%s", t.Name())
   147  	dbCleaner, err := sql.Open("postgres", connStr)
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
   152  
   153  	config := backend.TestWrapConfig(map[string]interface{}{
   154  		"conn_str":    connStr,
   155  		"schema_name": schemaName,
   156  	})
   157  	b := backend.TestBackendConfig(t, New(), config).(*Backend)
   158  
   159  	if b == nil {
   160  		t.Fatal("Backend could not be configured")
   161  	}
   162  
   163  	bb := backend.TestBackendConfig(t, New(), config).(*Backend)
   164  
   165  	if bb == nil {
   166  		t.Fatal("Backend could not be configured")
   167  	}
   168  
   169  	backend.TestBackendStateLocks(t, b, bb)
   170  }
   171  
   172  func getDatabaseUrl() string {
   173  	return os.Getenv("DATABASE_URL")
   174  }