fix(tfstated): add unlock handler tests
This commit is contained in:
parent
5ef098f47e
commit
7251421f55
2 changed files with 44 additions and 8 deletions
|
@ -1,8 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
@ -23,12 +21,7 @@ func handleUnlock(db *database.DB) http.Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if success, err := db.Unlock(r.URL.Path, &lock); err != nil {
|
if success, err := db.Unlock(r.URL.Path, &lock); err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||||
_ = encode(w, http.StatusNotFound,
|
|
||||||
fmt.Errorf("state path not found: %s", r.URL.Path))
|
|
||||||
} else {
|
|
||||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
|
||||||
}
|
|
||||||
} else if success {
|
} else if success {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
} else {
|
} else {
|
||||||
|
|
43
cmd/tfstated/unlock_test.go
Normal file
43
cmd/tfstated/unlock_test.go
Normal file
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue