From a00a0caa70506e0ecc14f52da947c3e59443d444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Budai?= Date: Wed, 27 Nov 2019 10:09:27 +0100 Subject: [PATCH] api tests: refactor weldr and jobqueue api tests to use common helpers The helper functions in both api packages were more or less same. However, over time they have been slowly diverging. This commit extract the helpers into one common package to make the tests more maintainable and to deduplicate the code. --- internal/jobqueue/api_test.go | 71 +---------- internal/test/helpers.go | 144 +++++++++++++++++++++ internal/weldr/api_test.go | 231 +++++++--------------------------- 3 files changed, 198 insertions(+), 248 deletions(-) create mode 100644 internal/test/helpers.go diff --git a/internal/jobqueue/api_test.go b/internal/jobqueue/api_test.go index 9ac513165..75864532b 100644 --- a/internal/jobqueue/api_test.go +++ b/internal/jobqueue/api_test.go @@ -1,76 +1,17 @@ package jobqueue_test import ( - "bytes" - "encoding/json" - "io/ioutil" "net/http" - "net/http/httptest" - "reflect" - "strings" "testing" "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/jobqueue" "github.com/osbuild/osbuild-composer/internal/store" + "github.com/osbuild/osbuild-composer/internal/test" "github.com/google/uuid" ) -func sendHTTP(api *jobqueue.API, method, path, body string) { - req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body))) - req.Header.Set("Content-Type", "application/json") - resp := httptest.NewRecorder() - api.ServeHTTP(resp, req) -} - -func testRoute(t *testing.T, api *jobqueue.API, method, path, body string, expectedStatus int, expectedJSON string) { - req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body))) - req.Header.Set("Content-Type", "application/json") - resp := httptest.NewRecorder() - api.ServeHTTP(resp, req) - - if resp.Code != expectedStatus { - t.Errorf("%s: expected status %v, but got %v", path, expectedStatus, resp.Code) - return - } - - replyJSON, err := ioutil.ReadAll(resp.Body) - if err != nil { - t.Errorf("%s: could not read reponse body: %v", path, err) - return - } - - if expectedJSON == "" { - if len(replyJSON) != 0 { - t.Errorf("%s: expected no response body, but got:\n%s", path, replyJSON) - } - return - } - - var reply, expected interface{} - err = json.Unmarshal(replyJSON, &reply) - if err != nil { - t.Errorf("%s: %v\n%s", path, err, string(replyJSON)) - return - } - - if expectedJSON == "*" { - return - } - - err = json.Unmarshal([]byte(expectedJSON), &expected) - if err != nil { - t.Errorf("%s: expected JSON is invalid: %v", path, err) - return - } - - if !reflect.DeepEqual(reply, expected) { - t.Errorf("%s: reply != expected:\n reply: %s\nexpected: %s", path, strings.TrimSpace(string(replyJSON)), expectedJSON) - return - } -} - func TestBasic(t *testing.T) { var cases = []struct { Method string @@ -92,7 +33,7 @@ func TestBasic(t *testing.T) { for _, c := range cases { api := jobqueue.New(nil, store.New(nil)) - testRoute(t, api, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, false, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) } } @@ -106,7 +47,7 @@ func TestCreate(t *testing.T) { t.Fatalf("error pushing compose: %v", err) } - testRoute(t, api, "POST", "/job-queue/v1/jobs", `{}`, http.StatusCreated, + test.TestRoute(t, api, false, "POST", "/job-queue/v1/jobs", `{}`, http.StatusCreated, `{"id":"ffffffff-ffff-ffff-ffff-ffffffffffff","pipeline":{"build":{"pipeline":{"stages":[{"name":"org.osbuild.dnf","options":{"repos":[{"metalink":"https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever\u0026arch=$basearch","gpgkey":"-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQINBFturGcBEACv0xBo91V2n0uEC2vh69ywCiSyvUgN/AQH8EZpCVtM7NyjKgKm\nbbY4G3R0M3ir1xXmvUDvK0493/qOiFrjkplvzXFTGpPTi0ypqGgxc5d0ohRA1M75\nL+0AIlXoOgHQ358/c4uO8X0JAA1NYxCkAW1KSJgFJ3RjukrfqSHWthS1d4o8fhHy\nKJKEnirE5hHqB50dafXrBfgZdaOs3C6ppRIePFe2o4vUEapMTCHFw0woQR8Ah4/R\nn7Z9G9Ln+0Cinmy0nbIDi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Jn9eII6HIPkW+2m5N1JsUyJQe4cbtZE5Yh3TRA0dm7\n+zoBRfCXkOW4krchbgww/ptVmzMMP7GINJdROrJnsGl5FVeid9qHzV7aZycWSma7\nCxBYB1J8HCbty5NjtD6XMYRrMLxXugvX6Q4NPPH+2NKjzX4SIDejS6JjgrP3KA3O\npMuo7ZHMfveBngv8yP+ZD/1sS6l+dfExvdaJdOdgFCnp4p3gPbw5+Lv70HrMjA==\n=BfZ/\n-----END PGP PUBLIC KEY BLOCK-----\n","checksum":"sha256:9f596e18f585bee30ac41c11fb11a83ed6b11d5b341c1cb56ca4015d7717cb97"}],"packages":["dnf","e2fsprogs","policycoreutils","qemu-img","systemd","grub2-pc","tar"],"releasever":"30","basearch":"x86_64"}}]},"runner":"org.osbuild.fedora30"},"stages":[{"name":"org.osbuild.dnf","options":{"repos":[{"metalink":"https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever\u0026arch=$basearch","gpgkey":"-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nmQINBFturGcBEACv0xBo91V2n0uEC2vh69ywCiSyvUgN/AQH8EZpCVtM7NyjKgKm\nbbY4G3R0M3ir1xXmvUDvK0493/qOiFrjkplvzXFTGpPTi0ypqGgxc5d0ohRA1M75\nL+0AIlXoOgHQ358/c4uO8X0JAA1NYxCkAW1KSJgFJ3RjukrfqSHWthS1d4o8fhHy\nKJKEnirE5hHqB50dafXrBfgZdaOs3C6ppRIePFe2o4vUEapMTCHFw0woQR8Ah4/R\nn7Z9G9Ln+0Cinmy0nbIDiZJ+pgLAXCOWBfDUzcOjDGKvcpoZharA07c0q1/5ojzO\n4F0Fh4g/BUmtrASwHfcIbjHyCSr1j/3Iz883iy07gJY5Yhiuaqmp0o0f9fgHkG53\n2xCU1owmACqaIBNQMukvXRDtB2GJMuKa/asTZDP6R5re+iXs7+s9ohcRRAKGyAyc\nYKIQKcaA+6M8T7/G+TPHZX6HJWqJJiYB+EC2ERblpvq9TPlLguEWcmvjbVc31nyq\nSDoO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\n-----END PGP PUBLIC KEY BLOCK-----\n","checksum":"sha256:9f596e18f585bee30ac41c11fb11a83ed6b11d5b341c1cb56ca4015d7717cb97"}],"packages":["policycoreutils","selinux-policy-targeted","kernel","firewalld","chrony","langpacks-en"],"exclude_packages":["dracut-config-rescue"],"releasever":"30","basearch":"x86_64"}},{"name":"org.osbuild.fix-bls","options":{}},{"name":"org.osbuild.locale","options":{"language":"en_US"}},{"name":"org.osbuild.grub2","options":{"root_fs_uuid":"76a22bf4-f153-4541-b6c7-0332c0dfaeac","boot_fs_uuid":"00000000-0000-0000-0000-000000000000","kernel_opts":"ro biosdevname=0 net.ifnames=0"}},{"name":"org.osbuild.selinux","options":{"file_contexts":"etc/selinux/targeted/contexts/files/file_contexts"}}],"assembler":{"name":"org.osbuild.tar","options":{"filename":"root.tar.xz"}}},"targets":[{"name":"org.osbuild.local","options":{"location":"/var/lib/osbuild-composer/outputs/ffffffff-ffff-ffff-ffff-ffffffffffff"}}]}`) } @@ -121,14 +62,14 @@ func testUpdateTransition(t *testing.T, from, to string, expectedStatus int) { t.Fatalf("error pushing compose: %v", err) } if from != "WAITING" { - sendHTTP(api, "POST", "/job-queue/v1/jobs", `{}`) + test.SendHTTP(api, false, "POST", "/job-queue/v1/jobs", `{}`) if from != "RUNNING" { - sendHTTP(api, "PATCH", "/job-queue/v1/jobs/ffffffff-ffff-ffff-ffff-ffffffffffff", `{"status":"`+from+`"}`) + test.SendHTTP(api, false, "PATCH", "/job-queue/v1/jobs/ffffffff-ffff-ffff-ffff-ffffffffffff", `{"status":"`+from+`"}`) } } } - testRoute(t, api, "PATCH", "/job-queue/v1/jobs/ffffffff-ffff-ffff-ffff-ffffffffffff", `{"status":"`+to+`"}`, expectedStatus, ``) + test.TestRoute(t, api, false, "PATCH", "/job-queue/v1/jobs/ffffffff-ffff-ffff-ffff-ffffffffffff", `{"status":"`+to+`"}`, expectedStatus, ``) } func TestUpdate(t *testing.T) { diff --git a/internal/test/helpers.go b/internal/test/helpers.go new file mode 100644 index 000000000..6cd77f153 --- /dev/null +++ b/internal/test/helpers.go @@ -0,0 +1,144 @@ +package test + +import ( + "bytes" + "context" + "encoding/json" + "io/ioutil" + "net" + "net/http" + "net/http/httptest" + "os" + "reflect" + "strings" + "testing" +) + +type API interface { + ServeHTTP(writer http.ResponseWriter, request *http.Request) +} + +func externalRequest(method, path, body string) *http.Response { + client := http.Client{ + Transport: &http.Transport{ + DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { + return net.Dial("unix", "/run/weldr/api.socket") + }, + }, + } + + req, err := http.NewRequest(method, "http://localhost"+path, bytes.NewReader([]byte(body))) + if err != nil { + panic(err) + } + + if method == "POST" { + req.Header.Set("Content-Type", "application/json") + } + + resp, err := client.Do(req) + if err != nil { + panic(err) + } + + return resp +} + +func internalRequest(api API, method, path, body string) *http.Response { + req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body))) + req.Header.Set("Content-Type", "application/json") + resp := httptest.NewRecorder() + api.ServeHTTP(resp, req) + + return resp.Result() +} + +func SendHTTP(api API, external bool, method, path, body string) *http.Response { + if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 { + if !external { + return nil + } + return externalRequest(method, path, body) + } else { + return internalRequest(api, method, path, body) + } +} + +// this function serves to drop fields that shouldn't be tested from the unmarshalled json objects +func dropFields(obj interface{}, fields ...string) { + switch v := obj.(type) { + // if the interface type is a map attempt to delete the fields + case map[string]interface{}: + for i, field := range fields { + if _, ok := v[field]; ok { + delete(v, field) + // if the field is found remove it from the fields slice + if len(fields) < i-1 { + fields = append(fields[:i], fields[i+1:]...) + } else { + fields = fields[:i] + } + } + } + // call dropFields on the remaining elements since they may contain a map containing the field + for _, val := range v { + dropFields(val, fields...) + } + // if the type is a list of interfaces call dropFields on each interface + case []interface{}: + for _, element := range v { + dropFields(element, fields...) + } + default: + return + } +} + +func TestRoute(t *testing.T, api API, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) { + resp := SendHTTP(api, external, method, path, body) + if resp == nil { + t.Skip("This test is for internal testing only") + } + + if resp.StatusCode != expectedStatus { + t.Errorf("%s: expected status %v, but got %v", path, expectedStatus, resp.StatusCode) + } + + replyJSON, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Errorf("%s: could not read response body: %v", path, err) + return + } + + if expectedJSON == "" { + if len(replyJSON) != 0 { + t.Errorf("%s: expected no response body, but got:\n%s", path, replyJSON) + } + return + } + + var reply, expected interface{} + err = json.Unmarshal(replyJSON, &reply) + if err != nil { + t.Errorf("%s: %v\n%s", path, err, string(replyJSON)) + return + } + + if expectedJSON == "*" { + return + } + + err = json.Unmarshal([]byte(expectedJSON), &expected) + if err != nil { + t.Errorf("%s: expected JSON is invalid: %v", path, err) + return + } + + dropFields(reply, ignoreFields...) + dropFields(expected, ignoreFields...) + + if !reflect.DeepEqual(reply, expected) { + t.Errorf("%s: reply != expected:\n reply: %s\nexpected: %s", path, strings.TrimSpace(string(replyJSON)), expectedJSON) + return + } +} diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 8a3087529..7217a33f6 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -1,157 +1,22 @@ package weldr_test import ( - "bytes" - "context" - "encoding/json" - "io/ioutil" "math/rand" - "net" "net/http" - "net/http/httptest" "os" - "reflect" "strconv" - "strings" "testing" "time" rpmmd_mock "github.com/osbuild/osbuild-composer/internal/mocks/rpmmd" "github.com/osbuild/osbuild-composer/internal/store" + "github.com/osbuild/osbuild-composer/internal/test" "github.com/osbuild/osbuild-composer/internal/weldr" "github.com/osbuild/osbuild-composer/internal/distro" _ "github.com/osbuild/osbuild-composer/internal/distro/test" ) -func externalRequest(method, path, body string) *http.Response { - client := http.Client{ - Transport: &http.Transport{ - DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { - return net.Dial("unix", "/run/weldr/api.socket") - }, - }, - } - - req, err := http.NewRequest(method, "http://localhost"+path, bytes.NewReader([]byte(body))) - if err != nil { - panic(err) - } - - if method == "POST" { - req.Header.Set("Content-Type", "application/json") - } - - resp, err := client.Do(req) - if err != nil { - panic(err) - } - - return resp -} - -func internalRequest(api *weldr.API, method, path, body string) *http.Response { - req := httptest.NewRequest(method, path, bytes.NewReader([]byte(body))) - - if method == "POST" { - req.Header.Set("Content-Type", "application/json") - } - resp := httptest.NewRecorder() - api.ServeHTTP(resp, req) - - return resp.Result() -} - -func sendHTTP(api *weldr.API, external bool, method, path, body string) *http.Response { - if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 { - if !external { - return nil - } - return externalRequest(method, path, body) - } else { - return internalRequest(api, method, path, body) - } -} - -// this function serves to drop fields that shouldn't be tested from the unmarshalled json objects -func dropFields(obj interface{}, fields ...string) { - switch v := obj.(type) { - // if the interface type is a map attempt to delete the fields - case map[string]interface{}: - for i, field := range fields { - if _, ok := v[field]; ok { - delete(v, field) - // if the field is found remove it from the fields slice - if len(fields) < i-1 { - fields = append(fields[:i], fields[i+1:]...) - } else { - fields = fields[:i] - } - } - } - // call dropFields on the remaining elements since they may contain a map containing the field - for _, val := range v { - dropFields(val, fields...) - } - // if the type is a list of interfaces call dropFields on each interface - case []interface{}: - for _, element := range v { - dropFields(element, fields...) - } - default: - return - } -} - -func testRoute(t *testing.T, api *weldr.API, external bool, method, path, body string, expectedStatus int, expectedJSON string, ignoreFields ...string) { - resp := sendHTTP(api, external, method, path, body) - if resp == nil { - t.Skip("This test is for internal testing only") - } - - if resp.StatusCode != expectedStatus { - t.Errorf("%s: expected status %v, but got %v", path, expectedStatus, resp.StatusCode) - } - - replyJSON, err := ioutil.ReadAll(resp.Body) - if err != nil { - t.Errorf("%s: could not read response body: %v", path, err) - return - } - - if expectedJSON == "" { - if len(replyJSON) != 0 { - t.Errorf("%s: expected no response body, but got:\n%s", path, replyJSON) - } - return - } - - var reply, expected interface{} - err = json.Unmarshal(replyJSON, &reply) - if err != nil { - t.Errorf("%s: %v\n%s", path, err, string(replyJSON)) - return - } - - if expectedJSON == "*" { - return - } - - err = json.Unmarshal([]byte(expectedJSON), &expected) - if err != nil { - t.Errorf("%s: expected JSON is invalid: %v", path, err) - return - } - - dropFields(reply, ignoreFields...) - dropFields(expected, ignoreFields...) - - if !reflect.DeepEqual(reply, expected) { - t.Errorf("%s: reply != expected:\n reply: %s\nexpected: %s", path, strings.TrimSpace(string(replyJSON)), expectedJSON) - return - } -} - func createWeldrAPI(fixture rpmmd_mock.Fixture) (*weldr.API, *store.Store) { s := store.New(nil) rpm := rpmmd_mock.NewRPMMDMock(fixture) @@ -183,7 +48,7 @@ func TestBasic(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } } @@ -200,7 +65,7 @@ func TestBlueprintsNew(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) } } @@ -217,8 +82,8 @@ func TestBlueprintsWorkspace(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) } } @@ -238,12 +103,12 @@ func TestBlueprintsInfo(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test1","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test2","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - sendHTTP(api, true, "POST", "/api/v0/blueprints/workspace", `{"name":"test2","description":"Test","packages":[{"name":"systemd","version":"123"}],"version":"0.0.0"}`) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test2", ``) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test1", ``) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test1","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test2","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/workspace", `{"name":"test2","description":"Test","packages":[{"name":"systemd","version":"123"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test2", ``) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test1", ``) } } @@ -259,9 +124,9 @@ func TestBlueprintsFreeze(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - sendHTTP(api, false, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"dep-package1","version":"*"}],"version":"0.0.0"}`) - testRoute(t, api, false, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, false, "DELETE", "/api/v0/blueprints/delete/test", ``) + test.SendHTTP(api, false, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"dep-package1","version":"*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, false, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, false, "DELETE", "/api/v0/blueprints/delete/test", ``) } } @@ -278,10 +143,10 @@ func TestBlueprintsDiff(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - sendHTTP(api, true, "POST", "/api/v0/blueprints/workspace", `{"name":"test","description":"Test","packages":[{"name":"systemd","version":"123"}],"version":"0.0.0"}`) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test", ``) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/workspace", `{"name":"test","description":"Test","packages":[{"name":"systemd","version":"123"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test", ``) } } @@ -298,9 +163,9 @@ func TestBlueprintsDelete(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test", ``) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/test", ``) } } @@ -310,13 +175,13 @@ func TestBlueprintsChanges(t *testing.T) { id := strconv.Itoa(rand.Int()) ignoreFields := []string{"commit", "timestamp"} - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - testRoute(t, api, true, "GET", "/api/v0/blueprints/changes/failing"+id, ``, http.StatusOK, `{"blueprints":[],"errors":[{"id":"UnknownBlueprint","msg":"failing`+id+`"}],"limit":20,"offset":0}`, ignoreFields...) - testRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":1}],"errors":[],"limit":20,"offset":0}`, ignoreFields...) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/"+id, ``) - sendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - testRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":2}],"errors":[],"limit":20,"offset":0}`, ignoreFields...) - sendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/"+id, ``) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/failing"+id, ``, http.StatusOK, `{"blueprints":[],"errors":[{"id":"UnknownBlueprint","msg":"failing`+id+`"}],"limit":20,"offset":0}`, ignoreFields...) + test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":1}],"errors":[],"limit":20,"offset":0}`, ignoreFields...) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/"+id, ``) + test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.0.0 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":2}],"errors":[],"limit":20,"offset":0}`, ignoreFields...) + test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/"+id, ``) } func TestCompose(t *testing.T) { @@ -335,9 +200,9 @@ func TestCompose(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, c.External, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) - testRoute(t, api, c.External, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON, c.IgnoreFields...) - sendHTTP(api, c.External, "DELETE", "/api/v0/blueprints/delete/test", ``) + test.SendHTTP(api, c.External, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.TestRoute(t, api, c.External, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON, c.IgnoreFields...) + test.SendHTTP(api, c.External, "DELETE", "/api/v0/blueprints/delete/test", ``) } } @@ -359,23 +224,23 @@ func TestComposeQueue(t *testing.T) { for _, c := range cases { api, s := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, false, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) + test.SendHTTP(api, false, "POST", "/api/v0/blueprints/new", `{"name":"test","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.0"}`) // create job and leave it waiting - sendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) + test.SendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) // create job and leave it running - sendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) + test.SendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) s.PopCompose() // create job and mark it as finished - sendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) + test.SendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) job := s.PopCompose() s.UpdateCompose(job.ComposeID, "FINISHED") // create job and mark it as failed - sendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) + test.SendHTTP(api, false, "POST", "/api/v0/compose", `{"blueprint_name": "test","compose_type": "tar","branch": "master"}`) job = s.PopCompose() s.UpdateCompose(job.ComposeID, "FAILED") - testRoute(t, api, false, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON, c.IgnoreFields...) - sendHTTP(api, false, "DELETE", "/api/v0/blueprints/delete/test", ``) + test.TestRoute(t, api, false, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON, c.IgnoreFields...) + test.SendHTTP(api, false, "DELETE", "/api/v0/blueprints/delete/test", ``) } } @@ -393,8 +258,8 @@ func TestSourcesNew(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``) } } @@ -412,9 +277,9 @@ func TestSourcesDelete(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(rpmmd_mock.BaseFixture) - sendHTTP(api, true, "POST", "/api/v0/projects/source/new", `{"name": "fish","url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`) - testRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) - sendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``) + test.SendHTTP(api, true, "POST", "/api/v0/projects/source/new", `{"name": "fish","url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`) + test.TestRoute(t, api, true, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON) + test.SendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``) } } @@ -432,7 +297,7 @@ func TestProjectsDepsolve(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } } @@ -453,7 +318,7 @@ func TestProjectsInfo(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } } @@ -475,7 +340,7 @@ func TestModulesInfo(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } } @@ -494,7 +359,7 @@ func TestProjectsList(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } } @@ -516,6 +381,6 @@ func TestModulesList(t *testing.T) { for _, c := range cases { api, _ := createWeldrAPI(c.Fixture) - testRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) + test.TestRoute(t, api, true, "GET", c.Path, ``, c.ExpectedStatus, c.ExpectedJSON) } }