Skip to content

Commit

Permalink
feat: add test for force-unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
tobikris committed Mar 29, 2023
1 parent bfa5c2c commit fe1b110
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions pkg/server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fe1b110

Please sign in to comment.