2018-10-10 23:42:57 +02:00
|
|
|
package pg
|
|
|
|
|
|
|
|
// Create the test database: createdb terraform_backend_pg_test
|
2019-02-26 01:05:53 +01:00
|
|
|
// TF_ACC=1 make test TEST=./backend/remote-state/pg TESTARGS='-v -run ^TestBackend'
|
2018-10-10 23:42:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
// verify that we are doing ACC tests or the Postgres tests specifically
|
|
|
|
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()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendStates(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")
|
|
|
|
}
|
|
|
|
|
|
|
|
backend.TestBackendStates(t, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|