From 5959766cbd69aa11ccb09ac82a10bcc485c8da3b Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 15 Oct 2024 00:10:14 +0200 Subject: chore(tfstated): simplify some code --- cmd/tfstated/delete_test.go | 16 ++++++++-------- cmd/tfstated/get_test.go | 14 +++++++------- cmd/tfstated/lock_test.go | 24 ++++++++++++------------ cmd/tfstated/main_test.go | 2 +- cmd/tfstated/post_test.go | 32 ++++++++++++++++---------------- cmd/tfstated/unlock_test.go | 20 ++++++++++---------- pkg/scrypto/aes256_test.go | 3 +-- 7 files changed, 55 insertions(+), 56 deletions(-) diff --git a/cmd/tfstated/delete_test.go b/cmd/tfstated/delete_test.go index 748896d..970fbcf 100644 --- a/cmd/tfstated/delete_test.go +++ b/cmd/tfstated/delete_test.go @@ -11,20 +11,20 @@ import ( func TestDelete(t *testing.T) { tests := []struct { method string - uri *url.URL + 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"}, + {"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) { + 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 { @@ -32,7 +32,7 @@ func TestDelete(t *testing.T) { } 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 { + } else if string(body) != tt.expect { t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body)) } } diff --git a/cmd/tfstated/get_test.go b/cmd/tfstated/get_test.go index aa56026..5ffcfd0 100644 --- a/cmd/tfstated/get_test.go +++ b/cmd/tfstated/get_test.go @@ -11,19 +11,19 @@ import ( func TestGet(t *testing.T) { tests := []struct { method string - uri *url.URL + uri url.URL body io.Reader expect string status int msg string }{ - {"GET", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, - {"GET", &url.URL{Path: "/non_existent_get"}, strings.NewReader(""), "", http.StatusOK, "non existent"}, - {"POST", &url.URL{Path: "/test_get"}, strings.NewReader("the_test_get"), "", http.StatusOK, "/test_get"}, - {"GET", &url.URL{Path: "/test_get"}, nil, "the_test_get", http.StatusOK, "/test_get"}, + {"GET", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, + {"GET", url.URL{Path: "/non_existent_get"}, strings.NewReader(""), "", http.StatusOK, "non existent"}, + {"POST", url.URL{Path: "/test_get"}, strings.NewReader("the_test_get"), "", http.StatusOK, "/test_get"}, + {"GET", url.URL{Path: "/test_get"}, nil, "the_test_get", http.StatusOK, "/test_get"}, } for _, tt := range tests { - runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) { + 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 { @@ -31,7 +31,7 @@ func TestGet(t *testing.T) { } 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 { + } else if string(body) != tt.expect { t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body)) } } diff --git a/cmd/tfstated/lock_test.go b/cmd/tfstated/lock_test.go index 9d8261b..fa454be 100644 --- a/cmd/tfstated/lock_test.go +++ b/cmd/tfstated/lock_test.go @@ -11,24 +11,24 @@ import ( func TestLock(t *testing.T) { tests := []struct { method string - uri *url.URL + uri url.URL body io.Reader expect string status int msg string }{ - {"LOCK", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, - {"LOCK", &url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"}, - {"LOCK", &url.URL{Path: "/non_existent_lock"}, strings.NewReader("{}"), "", http.StatusBadRequest, "invalid lock data on non existent state"}, - {"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"}, - {"GET", &url.URL{Path: "/test_lock"}, nil, "", http.StatusOK, "/test_lock"}, - {"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"\"}"), "", http.StatusBadRequest, "invalid lock data on already locked state"}, - {"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on already locked state"}, - {"POST", &url.URL{Path: "/test_lock", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_lock"), "", http.StatusOK, "/test_lock"}, - {"GET", &url.URL{Path: "/test_lock"}, nil, "the_test_lock", http.StatusOK, "/test_lock"}, + {"LOCK", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, + {"LOCK", url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"}, + {"LOCK", url.URL{Path: "/non_existent_lock"}, strings.NewReader("{}"), "", http.StatusBadRequest, "invalid lock data on non existent state"}, + {"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"}, + {"GET", url.URL{Path: "/test_lock"}, nil, "", http.StatusOK, "/test_lock"}, + {"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"\"}"), "", http.StatusBadRequest, "invalid lock data on already locked state"}, + {"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on already locked state"}, + {"POST", url.URL{Path: "/test_lock", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_lock"), "", http.StatusOK, "/test_lock"}, + {"GET", url.URL{Path: "/test_lock"}, nil, "the_test_lock", http.StatusOK, "/test_lock"}, } for _, tt := range tests { - runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) { + 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 { @@ -36,7 +36,7 @@ func TestLock(t *testing.T) { } 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 { + } else if string(body) != tt.expect { t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body)) } } diff --git a/cmd/tfstated/main_test.go b/cmd/tfstated/main_test.go index 7dece9d..4b819b3 100644 --- a/cmd/tfstated/main_test.go +++ b/cmd/tfstated/main_test.go @@ -13,7 +13,7 @@ import ( "git.adyxax.org/adyxax/tfstated/pkg/database" ) -var baseURI = &url.URL{ +var baseURI = url.URL{ Host: "127.0.0.1:8081", Path: "/", Scheme: "http", diff --git a/cmd/tfstated/post_test.go b/cmd/tfstated/post_test.go index d346bad..55308c0 100644 --- a/cmd/tfstated/post_test.go +++ b/cmd/tfstated/post_test.go @@ -11,28 +11,28 @@ import ( func TestPost(t *testing.T) { tests := []struct { method string - uri *url.URL + uri url.URL body io.Reader expect string status int msg string }{ - {"POST", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, - {"POST", &url.URL{Path: "/test_post"}, nil, "", http.StatusBadRequest, "without a body"}, - {"POST", &url.URL{Path: "/test_post"}, strings.NewReader("the_test_post"), "", http.StatusOK, "without lock ID in query string"}, - {"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, - {"POST", &url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post2"), "", http.StatusConflict, "with a lock ID on an unlocked state"}, - {"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, - {"LOCK", &url.URL{Path: "/test_post"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "/test_post"}, - {"POST", &url.URL{Path: "/test_post", RawQuery: "ID=ffffffff-ffff-ffff-ffff-ffffffffffff"}, strings.NewReader("the_test_post3"), "", http.StatusConflict, "with a wrong lock ID on a locked state"}, - {"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, - {"POST", &url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post4"), "", http.StatusOK, "with a correct lock ID on a locked state"}, - {"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post4", http.StatusOK, "/test_post"}, - {"POST", &url.URL{Path: "/test_post"}, strings.NewReader("the_test_post5"), "", http.StatusOK, "without lock ID in query string on a locked state"}, - {"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post5", http.StatusOK, "/test_post"}, + {"POST", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"}, + {"POST", url.URL{Path: "/test_post"}, nil, "", http.StatusBadRequest, "without a body"}, + {"POST", url.URL{Path: "/test_post"}, strings.NewReader("the_test_post"), "", http.StatusOK, "without lock ID in query string"}, + {"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, + {"POST", url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post2"), "", http.StatusConflict, "with a lock ID on an unlocked state"}, + {"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, + {"LOCK", url.URL{Path: "/test_post"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "/test_post"}, + {"POST", url.URL{Path: "/test_post", RawQuery: "ID=ffffffff-ffff-ffff-ffff-ffffffffffff"}, strings.NewReader("the_test_post3"), "", http.StatusConflict, "with a wrong lock ID on a locked state"}, + {"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"}, + {"POST", url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post4"), "", http.StatusOK, "with a correct lock ID on a locked state"}, + {"GET", url.URL{Path: "/test_post"}, nil, "the_test_post4", http.StatusOK, "/test_post"}, + {"POST", url.URL{Path: "/test_post"}, strings.NewReader("the_test_post5"), "", http.StatusOK, "without lock ID in query string on a locked state"}, + {"GET", url.URL{Path: "/test_post"}, nil, "the_test_post5", http.StatusOK, "/test_post"}, } for _, tt := range tests { - runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) { + 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 { @@ -40,7 +40,7 @@ func TestPost(t *testing.T) { } 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 { + } else if string(body) != tt.expect { t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body)) } } diff --git a/cmd/tfstated/unlock_test.go b/cmd/tfstated/unlock_test.go index c259614..f5af5f7 100644 --- a/cmd/tfstated/unlock_test.go +++ b/cmd/tfstated/unlock_test.go @@ -11,22 +11,22 @@ import ( func TestUnlock(t *testing.T) { tests := []struct { method string - uri *url.URL + 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"}, + {"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) { + 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 { @@ -34,7 +34,7 @@ func TestUnlock(t *testing.T) { } 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 { + } else if string(body) != tt.expect { t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body)) } } diff --git a/pkg/scrypto/aes256_test.go b/pkg/scrypto/aes256_test.go index 9914625..d07221d 100644 --- a/pkg/scrypto/aes256_test.go +++ b/pkg/scrypto/aes256_test.go @@ -3,7 +3,6 @@ package scrypto import ( "encoding/hex" "slices" - "strings" "testing" ) @@ -18,7 +17,7 @@ func TestAES256KeyHex(t *testing.T) { if slices.Compare(testKey, key[:]) != 0 { t.Errorf("got %v, wanted %v", testKey, key[:]) } - if strings.Compare(testKeyHex, key.Hex()) != 0 { + if testKeyHex != key.Hex() { t.Errorf("got %v, wanted %v", testKeyHex, key.Hex()) } } -- cgit v1.2.3