From 9b76f6e138bd3d25634aa47e945bbc756346892c Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 7 Feb 2017 10:33:05 -0500 Subject: [PATCH] Move TestRemoteLocks to state/remote This was legacy remote state client and backends can use this test function without an import cycle. --- backend/remote-state/consul/client_test.go | 25 ++++++---- backend/remote-state/testing.go | 55 ---------------------- state/remote/remote_test.go | 32 ------------- state/remote/s3_test.go | 2 +- state/remote/testing.go | 44 +++++++++++++++++ 5 files changed, 61 insertions(+), 97 deletions(-) diff --git a/backend/remote-state/consul/client_test.go b/backend/remote-state/consul/client_test.go index ed330015c..09d018fda 100644 --- a/backend/remote-state/consul/client_test.go +++ b/backend/remote-state/consul/client_test.go @@ -32,21 +32,28 @@ func TestRemoteClient(t *testing.T) { func TestConsul_stateLock(t *testing.T) { addr := os.Getenv("CONSUL_HTTP_ADDR") if addr == "" { - t.Log("consul lock tests require a running consul instance") + t.Log("consul lock tests require CONSUL_HTTP_ADDR") t.Skip() } - path := "testing" //fmt.Sprintf("tf-unit/%s", time.Now().String()) + path := fmt.Sprintf("tf-unit/%s", time.Now().String()) // create 2 instances to get 2 remote.Clients - a := backend.TestBackendConfig(t, New(), map[string]interface{}{ + sA, err := backend.TestBackendConfig(t, New(), map[string]interface{}{ "address": addr, "path": path, - }) - b := backend.TestBackendConfig(t, New(), map[string]interface{}{ - "address": addr, - "path": path, - }) + }).State() + if err != nil { + t.Fatal(err) + } - remotestate.TestRemoteLocks(t, a, b) + sB, err := backend.TestBackendConfig(t, New(), map[string]interface{}{ + "address": addr, + "path": path, + }).State() + if err != nil { + t.Fatal(err) + } + + remote.TestRemoteLocks(t, sA.(*remote.State).Client, sB.(*remote.State).Client) } diff --git a/backend/remote-state/testing.go b/backend/remote-state/testing.go index db0894ac7..17d9b80f5 100644 --- a/backend/remote-state/testing.go +++ b/backend/remote-state/testing.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/hashicorp/terraform/backend" - "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/state/remote" ) @@ -16,57 +15,3 @@ func TestClient(t *testing.T, raw backend.Backend) { remote.TestClient(t, b.client) } - -// Test the lock implementation for a remote.Client. -// This test requires 2 backend instances, in oder to have multiple remote -// clients since some implementations may tie the client to the lock, or may -// have reentrant locks. -func TestRemoteLocks(t *testing.T, a, b backend.Backend) { - sA, err := a.State() - if err != nil { - t.Fatal("failed to get state from backend A:", err) - } - - sB, err := b.State() - if err != nil { - t.Fatal("failed to get state from backend B:", err) - } - - lockerA, ok := sA.(state.Locker) - if !ok { - t.Fatal("client A not a state.Locker") - } - - lockerB, ok := sB.(state.Locker) - if !ok { - t.Fatal("client B not a state.Locker") - } - - if err := lockerA.Lock("test client A"); err != nil { - t.Fatal("unable to get initial lock:", err) - } - - if err := lockerB.Lock("test client B"); err == nil { - lockerA.Unlock() - t.Fatal("client B obtained lock while held by client A") - } else { - t.Log("lock info error:", err) - } - - if err := lockerA.Unlock(); err != nil { - t.Fatal("error unlocking client A", err) - } - - if err := lockerB.Lock("test client B"); err != nil { - t.Fatal("unable to obtain lock from client B") - } - - if err := lockerB.Unlock(); err != nil { - t.Fatal("error unlocking client B:", err) - } - - // unlock should be repeatable - if err := lockerA.Unlock(); err != nil { - t.Fatal("Unlock error from client A when state was not locked:", err) - } -} diff --git a/state/remote/remote_test.go b/state/remote/remote_test.go index 5fa20f4eb..db3c795c8 100644 --- a/state/remote/remote_test.go +++ b/state/remote/remote_test.go @@ -44,38 +44,6 @@ func testClient(t *testing.T, c Client) { } } -func testClientLocks(t *testing.T, c Client) { - s3Client := c.(*S3Client) - - // initial lock - if err := s3Client.Lock("test"); err != nil { - t.Fatal(err) - } - - // second lock should fail - if err := s3Client.Lock("test"); err == nil { - t.Fatal("expected error, got nil") - } - - // unlock should work - if err := s3Client.Unlock(); err != nil { - t.Fatal(err) - } - - // now we should be able to lock again - if err := s3Client.Lock("test"); err != nil { - t.Fatal(err) - } - - // unlock should be idempotent - if err := s3Client.Unlock(); err != nil { - t.Fatal(err) - } - if err := s3Client.Unlock(); err != nil { - t.Fatal(err) - } -} - func TestRemoteClient_noPayload(t *testing.T) { s := &State{ Client: nilClient{}, diff --git a/state/remote/s3_test.go b/state/remote/s3_test.go index f29426fa7..7a0983589 100644 --- a/state/remote/s3_test.go +++ b/state/remote/s3_test.go @@ -169,7 +169,7 @@ func TestS3ClientLocks(t *testing.T) { createDynamoDBTable(t, s3Client, bucketName) - testClientLocks(t, client) + TestRemoteLocks(t, client, client) } // create the dynamoDB table, and wait until we can query it. diff --git a/state/remote/testing.go b/state/remote/testing.go index bc874a458..d7de10b16 100644 --- a/state/remote/testing.go +++ b/state/remote/testing.go @@ -41,3 +41,47 @@ func TestClient(t *testing.T, c Client) { t.Fatalf("bad: %#v", p) } } + +// Test the lock implementation for a remote.Client. +// This test requires 2 client instances, in oder to have multiple remote +// clients since some implementations may tie the client to the lock, or may +// have reentrant locks. +func TestRemoteLocks(t *testing.T, a, b Client) { + lockerA, ok := a.(state.Locker) + if !ok { + t.Fatal("client A not a state.Locker") + } + + lockerB, ok := b.(state.Locker) + if !ok { + t.Fatal("client B not a state.Locker") + } + + if err := lockerA.Lock("test client A"); err != nil { + t.Fatal("unable to get initial lock:", err) + } + + if err := lockerB.Lock("test client B"); err == nil { + lockerA.Unlock() + t.Fatal("client B obtained lock while held by client A") + } else { + t.Log("lock info error:", err) + } + + if err := lockerA.Unlock(); err != nil { + t.Fatal("error unlocking client A", err) + } + + if err := lockerB.Lock("test client B"); err != nil { + t.Fatal("unable to obtain lock from client B") + } + + if err := lockerB.Unlock(); err != nil { + t.Fatal("error unlocking client B:", err) + } + + // unlock should be repeatable + if err := lockerA.Unlock(); err != nil { + t.Fatal("Unlock error from client A when state was not locked:", err) + } +}