From 82eba5801d2f692a70c01491f849c07c5c664eb9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 30 May 2017 11:07:56 -0400 Subject: [PATCH] Test losing and reacquiring a consul lock --- backend/remote-state/consul/client_test.go | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/backend/remote-state/consul/client_test.go b/backend/remote-state/consul/client_test.go index dc7988135..910c5e9eb 100644 --- a/backend/remote-state/consul/client_test.go +++ b/backend/remote-state/consul/client_test.go @@ -139,3 +139,52 @@ func TestConsul_destroyLock(t *testing.T) { t.Fatalf("lock key not cleaned up at: %s", pair.Key) } } + +func TestConsul_lostLock(t *testing.T) { + srv := newConsulTestServer(t) + defer srv.Stop() + + path := fmt.Sprintf("tf-unit/%s", time.Now().String()) + + // create 2 instances to get 2 remote.Clients + sA, err := backend.TestBackendConfig(t, New(), map[string]interface{}{ + "address": srv.HTTPAddr, + "path": path, + }).State(backend.DefaultStateName) + if err != nil { + t.Fatal(err) + } + + sB, err := backend.TestBackendConfig(t, New(), map[string]interface{}{ + "address": srv.HTTPAddr, + "path": path + "-not-used", + }).State(backend.DefaultStateName) + if err != nil { + t.Fatal(err) + } + + info := state.NewLockInfo() + info.Operation = "test-lost-lock" + id, err := sA.Lock(info) + if err != nil { + t.Fatal(err) + } + + reLocked := make(chan struct{}) + testLockHook = func() { + close(reLocked) + } + + // now we use the second client to break the lock + kv := sB.(*remote.State).Client.(*RemoteClient).Client.KV() + _, err = kv.Delete(path+lockSuffix, nil) + if err != nil { + t.Fatal(err) + } + + <-reLocked + + if err := sA.Unlock(id); err != nil { + t.Fatal(err) + } +}