2017-04-22 22:42:37 +02:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
)
|
|
|
|
|
|
|
|
// verify that we are doing ACC tests or the Swift tests specifically
|
|
|
|
func testACC(t *testing.T) {
|
|
|
|
skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_SWIFT_TEST") == ""
|
|
|
|
if skip {
|
|
|
|
t.Log("swift backend tests require setting TF_ACC or TF_SWIFT_TEST")
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
t.Log("swift backend acceptance tests enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_impl(t *testing.T) {
|
|
|
|
var _ backend.Backend = new(Backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
v := os.Getenv("OS_AUTH_URL")
|
|
|
|
if v == "" {
|
|
|
|
t.Fatal("OS_AUTH_URL must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendConfig(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
|
|
|
|
// Build config
|
2018-06-25 09:23:14 +02:00
|
|
|
container := fmt.Sprintf("terraform-state-swift-testconfig-%x", time.Now().Unix())
|
|
|
|
archiveContainer := fmt.Sprintf("%s_archive", container)
|
|
|
|
|
2017-04-22 22:42:37 +02:00
|
|
|
config := map[string]interface{}{
|
2018-06-25 09:23:14 +02:00
|
|
|
"archive_container": archiveContainer,
|
|
|
|
"container": container,
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)).(*Backend)
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
if b.container != container {
|
|
|
|
t.Fatal("Incorrect container was provided.")
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
2018-06-25 09:23:14 +02:00
|
|
|
if b.archiveContainer != archiveContainer {
|
|
|
|
t.Fatal("Incorrect archive_container was provided.")
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
container := fmt.Sprintf("terraform-state-swift-testbackend-%x", time.Now().Unix())
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
be0 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-04-22 22:42:37 +02:00
|
|
|
"container": container,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
be1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"container": container,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-04-22 22:42:37 +02:00
|
|
|
|
|
|
|
client := &RemoteClient{
|
2018-06-25 09:23:14 +02:00
|
|
|
client: be0.client,
|
|
|
|
container: be0.container,
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
defer client.deleteContainer()
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
backend.TestBackendStates(t, be0)
|
|
|
|
backend.TestBackendStateLocks(t, be0, be1)
|
|
|
|
backend.TestBackendStateForceUnlock(t, be0, be1)
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackendArchive(t *testing.T) {
|
|
|
|
testACC(t)
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
container := fmt.Sprintf("terraform-state-swift-testarchive-%x", time.Now().Unix())
|
2017-04-22 22:42:37 +02:00
|
|
|
archiveContainer := fmt.Sprintf("%s_archive", container)
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
be0 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-04-22 22:42:37 +02:00
|
|
|
"archive_container": archiveContainer,
|
|
|
|
"container": container,
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
be1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"archive_container": archiveContainer,
|
|
|
|
"container": container,
|
|
|
|
})).(*Backend)
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
defer func() {
|
|
|
|
client := &RemoteClient{
|
|
|
|
client: be0.client,
|
|
|
|
container: be0.container,
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
aclient := &RemoteClient{
|
|
|
|
client: be0.client,
|
|
|
|
container: be0.archiveContainer,
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
defer client.deleteContainer()
|
|
|
|
client.deleteContainer()
|
|
|
|
aclient.deleteContainer()
|
|
|
|
}()
|
2017-04-22 22:42:37 +02:00
|
|
|
|
2018-06-25 09:23:14 +02:00
|
|
|
backend.TestBackendStates(t, be0)
|
|
|
|
backend.TestBackendStateLocks(t, be0, be1)
|
|
|
|
backend.TestBackendStateForceUnlock(t, be0, be1)
|
2017-04-22 22:42:37 +02:00
|
|
|
}
|