summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-10-12 15:46:30 +0200
committerJulien Dessaux2024-10-12 15:46:30 +0200
commit7251421f55a3f1115d34ca48e3683d7907fa6507 (patch)
treec370afd4ef98adb60fe58e5e7f1f37ee94184e5e
parentfix(tfstated): add lock handler tests (diff)
downloadtfstated-7251421f55a3f1115d34ca48e3683d7907fa6507.tar.gz
tfstated-7251421f55a3f1115d34ca48e3683d7907fa6507.tar.bz2
tfstated-7251421f55a3f1115d34ca48e3683d7907fa6507.zip
fix(tfstated): add unlock handler tests
-rw-r--r--cmd/tfstated/unlock.go9
-rw-r--r--cmd/tfstated/unlock_test.go43
2 files changed, 44 insertions, 8 deletions
diff --git a/cmd/tfstated/unlock.go b/cmd/tfstated/unlock.go
index af5ad57..e8bddd9 100644
--- a/cmd/tfstated/unlock.go
+++ b/cmd/tfstated/unlock.go
@@ -1,8 +1,6 @@
package main
import (
- "database/sql"
- "errors"
"fmt"
"net/http"
@@ -23,12 +21,7 @@ func handleUnlock(db *database.DB) http.Handler {
return
}
if success, err := db.Unlock(r.URL.Path, &lock); err != nil {
- if errors.Is(err, sql.ErrNoRows) {
- _ = encode(w, http.StatusNotFound,
- fmt.Errorf("state path not found: %s", r.URL.Path))
- } else {
- _ = errorResponse(w, http.StatusInternalServerError, err)
- }
+ _ = errorResponse(w, http.StatusInternalServerError, err)
} else if success {
w.WriteHeader(http.StatusOK)
} else {
diff --git a/cmd/tfstated/unlock_test.go b/cmd/tfstated/unlock_test.go
new file mode 100644
index 0000000..c259614
--- /dev/null
+++ b/cmd/tfstated/unlock_test.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "io"
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+func TestUnlock(t *testing.T) {
+ tests := []struct {
+ method string
+ uri *url.URL
+ body io.Reader
+ expect string
+ status int
+ msg string
+ }{
+ {"UNLOCK", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
+ {"UNLOCK", &url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"},
+ {"UNLOCK", &url.URL{Path: "/non_existent_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on non existent state"},
+ {"LOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"},
+ {"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF\"}"), "", http.StatusConflict, "valid but wrong lock data on a locked state"},
+ {"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid and correct lock data on a locked state"},
+ {"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid and correct lock data on a now unlocked state"},
+ }
+ for _, tt := range tests {
+ runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
+ if err != nil {
+ t.Fatalf("failed %s with error: %+v", tt.method, err)
+ } else if r.StatusCode != tt.status {
+ t.Fatalf("%s %s should %s, got %s", tt.method, tt.msg, http.StatusText(tt.status), http.StatusText(r.StatusCode))
+ } else if tt.expect != "" {
+ if body, err := io.ReadAll(r.Body); err != nil {
+ t.Fatalf("failed to read body with error: %+v", err)
+ } else if strings.Compare(string(body), tt.expect) != 0 {
+ t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
+ }
+ }
+ })
+ }
+}