2018-10-10 23:42:57 +02:00
|
|
|
package pg
|
|
|
|
|
|
|
|
// Create the test database: createdb terraform_backend_pg_test
|
2019-02-27 01:46:06 +01:00
|
|
|
// TF_ACC=1 GO111MODULE=on go test -v -mod=vendor -timeout=2m -parallel=4 github.com/hashicorp/terraform/backend/remote-state/pg
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
2020-10-03 18:02:13 +02:00
|
|
|
"github.com/lib/pq"
|
2018-10-10 23:42:57 +02:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2019-02-27 01:46:06 +01:00
|
|
|
// Function to skip a test unless in ACCeptance test mode.
|
|
|
|
//
|
|
|
|
// A running Postgres server identified by env variable
|
|
|
|
// DATABASE_URL is required for acceptance tests.
|
2018-10-10 23:42:57 +02:00
|
|
|
func testACC(t *testing.T) {
|
2019-02-22 23:42:24 +01:00
|
|
|
skip := os.Getenv("TF_ACC") == ""
|
2018-10-10 23:42:57 +02:00
|
|
|
if skip {
|
2019-02-22 23:42:24 +01:00
|
|
|
t.Log("pg backend tests require setting TF_ACC")
|
2018-10-10 23:42:57 +02:00
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
if os.Getenv("DATABASE_URL") == "" {
|
|
|
|
os.Setenv("DATABASE_URL", "postgres://localhost/terraform_backend_pg_test?sslmode=disable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_impl(t *testing.T) {
|
|
|
|
var _ backend.Backend = new(Backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
connStr := getDatabaseUrl()
|
2020-10-03 18:02:13 +02:00
|
|
|
schemaName := pq.QuoteIdentifier(fmt.Sprintf("terraform_%s", t.Name()))
|
|
|
|
|
|
|
|
config := backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"conn_str": connStr,
|
|
|
|
"schema_name": schemaName,
|
|
|
|
})
|
|
|
|
schemaName = pq.QuoteIdentifier(schemaName)
|
|
|
|
|
2018-10-10 23:42:57 +02:00
|
|
|
dbCleaner, err := sql.Open("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), config).(*Backend)
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
t.Fatal("Backend could not be configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = b.db.Query(fmt.Sprintf("SELECT name, data FROM %s.%s LIMIT 1", schemaName, statesTableName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:09:43 +01:00
|
|
|
_, err = b.StateMgr(backend.DefaultStateName)
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:09:43 +01:00
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
2018-10-10 23:42:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c := s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
if c.Name != backend.DefaultStateName {
|
|
|
|
t.Fatal("RemoteClient name is not configured")
|
|
|
|
}
|
2020-09-29 23:18:34 +02:00
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
func TestBackendConfigSkipOptions(t *testing.T) {
|
2019-08-27 17:14:32 +02:00
|
|
|
testACC(t)
|
|
|
|
connStr := getDatabaseUrl()
|
2020-10-12 22:46:57 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
SkipSchemaCreation bool
|
|
|
|
SkipTableCreation bool
|
|
|
|
SkipIndexCreation bool
|
|
|
|
Setup func(t *testing.T, db *sql.DB, schemaName string)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "skip_schema_creation",
|
|
|
|
SkipSchemaCreation: true,
|
|
|
|
Setup: func(t *testing.T, db *sql.DB, schemaName string) {
|
|
|
|
// create the schema as a prerequisites
|
|
|
|
_, err := db.Query(fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schemaName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "skip_table_creation",
|
|
|
|
SkipTableCreation: true,
|
|
|
|
Setup: func(t *testing.T, db *sql.DB, schemaName string) {
|
|
|
|
// since the table needs to be already created the schema must be too
|
|
|
|
_, err := db.Query(fmt.Sprintf(`CREATE SCHEMA %s`, schemaName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = db.Query(fmt.Sprintf(`CREATE TABLE %s.%s (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
name TEXT,
|
|
|
|
data TEXT
|
|
|
|
)`, schemaName, statesTableName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "skip_index_creation",
|
|
|
|
SkipIndexCreation: true,
|
|
|
|
Setup: func(t *testing.T, db *sql.DB, schemaName string) {
|
|
|
|
// Everything need to exists for the index to be created
|
|
|
|
_, err := db.Query(fmt.Sprintf(`CREATE SCHEMA %s`, schemaName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = db.Query(fmt.Sprintf(`CREATE TABLE %s.%s (
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
name TEXT,
|
|
|
|
data TEXT
|
|
|
|
)`, schemaName, statesTableName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = db.Exec(fmt.Sprintf(`CREATE UNIQUE INDEX IF NOT EXISTS %s ON %s.%s (name)`, statesIndexName, schemaName, statesTableName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2019-08-27 17:14:32 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
schemaName := tc.Name
|
2019-08-27 17:14:32 +02:00
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
config := backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"conn_str": connStr,
|
|
|
|
"schema_name": schemaName,
|
|
|
|
"skip_schema_creation": tc.SkipSchemaCreation,
|
|
|
|
"skip_table_creation": tc.SkipTableCreation,
|
|
|
|
"skip_index_creation": tc.SkipIndexCreation,
|
|
|
|
})
|
|
|
|
schemaName = pq.QuoteIdentifier(schemaName)
|
|
|
|
db, err := sql.Open("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-27 17:14:32 +02:00
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
tc.Setup(t, db, schemaName)
|
|
|
|
defer db.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
|
2019-08-27 17:14:32 +02:00
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
b := backend.TestBackendConfig(t, New(), config).(*Backend)
|
2019-08-27 17:14:32 +02:00
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
if b == nil {
|
|
|
|
t.Fatal("Backend could not be configured")
|
|
|
|
}
|
2019-08-27 17:14:32 +02:00
|
|
|
|
2020-10-12 22:46:57 +02:00
|
|
|
// Make sure everything has been created
|
|
|
|
|
|
|
|
// This tests that both the schema and the table have been created
|
|
|
|
_, err = b.db.Query(fmt.Sprintf("SELECT name, data FROM %s.%s LIMIT 1", schemaName, statesTableName))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Make sure that the index exists
|
|
|
|
query := `select count(*) from pg_indexes where schemaname=$1 and tablename=$2 and indexname=$3;`
|
|
|
|
var count int
|
|
|
|
if err := b.db.QueryRow(query, tc.Name, statesTableName, statesIndexName).Scan(&count); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 1 {
|
|
|
|
t.Fatalf("The index has not been created (%d)", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c := s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
if c.Name != backend.DefaultStateName {
|
|
|
|
t.Fatal("RemoteClient name is not configured")
|
|
|
|
}
|
|
|
|
})
|
2019-08-27 17:14:32 +02:00
|
|
|
}
|
2020-10-12 22:46:57 +02:00
|
|
|
|
2019-08-27 17:14:32 +02:00
|
|
|
}
|
|
|
|
|
2018-10-10 23:42:57 +02:00
|
|
|
func TestBackendStates(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
connStr := getDatabaseUrl()
|
2020-10-03 18:02:13 +02:00
|
|
|
|
|
|
|
testCases := []string{
|
|
|
|
fmt.Sprintf("terraform_%s", t.Name()),
|
|
|
|
fmt.Sprintf("test with spaces: %s", t.Name()),
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
2020-10-03 18:02:13 +02:00
|
|
|
for _, schemaName := range testCases {
|
|
|
|
t.Run(schemaName, func(t *testing.T) {
|
|
|
|
dbCleaner, err := sql.Open("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dbCleaner.Query("DROP SCHEMA IF EXISTS %s CASCADE", pq.QuoteIdentifier(schemaName))
|
2018-10-10 23:42:57 +02:00
|
|
|
|
2020-10-03 18:02:13 +02:00
|
|
|
config := backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"conn_str": connStr,
|
|
|
|
"schema_name": schemaName,
|
|
|
|
})
|
|
|
|
b := backend.TestBackendConfig(t, New(), config).(*Backend)
|
2018-10-10 23:42:57 +02:00
|
|
|
|
2020-10-03 18:02:13 +02:00
|
|
|
if b == nil {
|
|
|
|
t.Fatal("Backend could not be configured")
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
|
2020-10-03 18:02:13 +02:00
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
})
|
|
|
|
}
|
2018-10-10 23:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendStateLocks(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
connStr := getDatabaseUrl()
|
|
|
|
schemaName := fmt.Sprintf("terraform_%s", t.Name())
|
|
|
|
dbCleaner, err := sql.Open("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dbCleaner.Query(fmt.Sprintf("DROP SCHEMA IF EXISTS %s CASCADE", schemaName))
|
|
|
|
|
2018-11-14 02:09:43 +01:00
|
|
|
config := backend.TestWrapConfig(map[string]interface{}{
|
2018-10-10 23:42:57 +02:00
|
|
|
"conn_str": connStr,
|
|
|
|
"schema_name": schemaName,
|
2018-11-14 02:09:43 +01:00
|
|
|
})
|
2018-10-10 23:42:57 +02:00
|
|
|
b := backend.TestBackendConfig(t, New(), config).(*Backend)
|
|
|
|
|
|
|
|
if b == nil {
|
|
|
|
t.Fatal("Backend could not be configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
bb := backend.TestBackendConfig(t, New(), config).(*Backend)
|
|
|
|
|
|
|
|
if bb == nil {
|
|
|
|
t.Fatal("Backend could not be configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
backend.TestBackendStateLocks(t, b, bb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDatabaseUrl() string {
|
|
|
|
return os.Getenv("DATABASE_URL")
|
|
|
|
}
|