summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/tfstated/delete.go16
-rw-r--r--cmd/tfstated/delete_test.go41
-rw-r--r--pkg/database/states.go14
3 files changed, 58 insertions, 13 deletions
diff --git a/cmd/tfstated/delete.go b/cmd/tfstated/delete.go
index 82811b0..3b708d5 100644
--- a/cmd/tfstated/delete.go
+++ b/cmd/tfstated/delete.go
@@ -1,8 +1,6 @@
package main
import (
- "database/sql"
- "errors"
"fmt"
"net/http"
@@ -17,15 +15,13 @@ func handleDelete(db *database.DB) http.Handler {
return
}
- if err := db.DeleteState(r.URL.Path); err != nil {
- if errors.Is(err, sql.ErrNoRows) {
- _ = errorResponse(w, http.StatusNotFound,
- fmt.Errorf("state path not found: %s", r.URL.Path))
- } else {
- _ = errorResponse(w, http.StatusInternalServerError, err)
- }
- } else {
+ if success, err := db.DeleteState(r.URL.Path); err != nil {
+ _ = errorResponse(w, http.StatusInternalServerError, err)
+ } else if success {
w.WriteHeader(http.StatusOK)
+ } else {
+ _ = errorResponse(w, http.StatusNotFound,
+ fmt.Errorf("state path not found: %s", r.URL.Path))
}
})
}
diff --git a/cmd/tfstated/delete_test.go b/cmd/tfstated/delete_test.go
new file mode 100644
index 0000000..748896d
--- /dev/null
+++ b/cmd/tfstated/delete_test.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "io"
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+func TestDelete(t *testing.T) {
+ tests := []struct {
+ method string
+ uri *url.URL
+ body io.Reader
+ expect string
+ status int
+ msg string
+ }{
+ {"DELETE", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
+ {"DELETE", &url.URL{Path: "/non_existent_delete"}, nil, "", http.StatusNotFound, "non existent"},
+ {"POST", &url.URL{Path: "/test_delete"}, strings.NewReader("the_test_delete"), "", http.StatusOK, "/test_delete"},
+ {"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusOK, "/test_delete"},
+ {"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusNotFound, "/test_delete"},
+ }
+ 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))
+ }
+ }
+ })
+ }
+}
diff --git a/pkg/database/states.go b/pkg/database/states.go
index e848924..46a5536 100644
--- a/pkg/database/states.go
+++ b/pkg/database/states.go
@@ -5,9 +5,17 @@ import (
"fmt"
)
-func (db *DB) DeleteState(name string) error {
- _, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name)
- return err
+// returns true in case of successful deletion
+func (db *DB) DeleteState(name string) (bool, error) {
+ result, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name)
+ if err != nil {
+ return false, err
+ }
+ n, err := result.RowsAffected()
+ if err != nil {
+ return false, err
+ }
+ return n == 1, nil
}
func (db *DB) GetState(name string) ([]byte, error) {