From fe1b1108ca7edddd386b205f34785d5e91409cd7 Mon Sep 17 00:00:00 2001 From: Tobias Krischer Date: Thu, 30 Mar 2023 01:42:56 +0200 Subject: [PATCH] feat: add test for force-unlock --- pkg/server/handler.go | 4 +++- pkg/server/handler_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/server/handler.go b/pkg/server/handler.go index 4919da6..8033d07 100644 --- a/pkg/server/handler.go +++ b/pkg/server/handler.go @@ -93,6 +93,7 @@ func Lock(w http.ResponseWriter, r *http.Request, state *terraform.State, body [ if err := json.Unmarshal(body, &state.Lock); err != nil { log.Errorf("failed to unmarshal lock info: %v", err) HTTPResponse(w, r, http.StatusBadRequest, "") + return } if ok, err := locker.Lock(state); err != nil { @@ -111,8 +112,9 @@ func Unlock(w http.ResponseWriter, r *http.Request, state *terraform.State, body log.Debugf("try to unlock state with id %s", state.ID) if err := json.Unmarshal(body, &state.Lock); err != nil { - log.Errorf("failed to unmarshal lock info: %v", err) + log.Errorf("failed to unmarshal lock info (probably force-unlock): %v", err) HTTPResponse(w, r, http.StatusBadRequest, "") + return } if ok, err := locker.Unlock(state); err != nil { diff --git a/pkg/server/handler_test.go b/pkg/server/handler_test.go index 989c78e..0550c1d 100644 --- a/pkg/server/handler_test.go +++ b/pkg/server/handler_test.go @@ -117,6 +117,31 @@ func TestServerHandler(t *testing.T) { } } +func TestServerHandler_ForceUnlock(t *testing.T) { + s := httptest.NewServer(NewStateHandler(t, "./handler_test")) + defer s.Close() + + address, err := url.JoinPath(s.URL, "/state/project1/example") + if err != nil { + t.Fatal(err) + } + + terraformOptions := terraformOptions(t, "./handler_test", address) + + terraform.Init(t, terraformOptions) + + simulateLock(t, address, true) + + if _, err := terraform.RunTerraformCommandE(t, terraformOptions, "force-unlock", "-force", "random-id"); err == nil { + t.Fatal("expected error") + } + + if _, err = terraform.RunTerraformCommandE(t, terraformOptions, "force-unlock", "-force", "cf290ef3-6090-410e-9784-d017a4b1536a"); err == nil { + t.Fatal("expected error") + } + +} + func simulateLock(t *testing.T, address string, doLock bool) { method := "LOCK" if !doLock {