From a9b676e43e9b7838879b78acfe4cd2fa34c662ef Mon Sep 17 00:00:00 2001 From: Tomas Hozza Date: Mon, 2 Aug 2021 13:02:46 +0200 Subject: [PATCH] Weldr API: return the actual `getImageType()` error in `composeHandler()` Refactor the `composeHandler()` method to send the actual error returned by `getImageType()` as an API response. Modify tests to handle the changed error message in API calls. Signed-off-by: Tomas Hozza --- internal/weldr/api.go | 4 ++-- internal/weldr/api_test.go | 17 +++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 4fa359bcb..32d346fa0 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -2177,8 +2177,8 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request imageType, err := api.getImageType(distroName, cr.ComposeType) if err != nil { errors := responseError{ - ID: "UnknownComposeType", - Msg: fmt.Sprintf("Unknown compose type for architecture: %s", cr.ComposeType), + ID: "ComposeError", + Msg: fmt.Sprintf("Failed to get compose type %q: %v", cr.ComposeType, err), } statusResponseError(writer, http.StatusBadRequest, errors) return diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index fb500f456..dc7c028c4 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -771,7 +771,7 @@ func TestCompose(t *testing.T) { {false, "POST", "/api/v1/compose", fmt.Sprintf(`{"blueprint_name": "test","compose_type":"%s","branch":"master","ostree":{"ref":"/bad/ref","parent":"","url":"http://ostree/"}}`, test_distro.TestImageTypeName), http.StatusBadRequest, `{"status":false,"errors":[{"id":"InvalidChars","msg":"Invalid ostree ref"}]}`, expectedComposeOSTreeURL, []string{"build_id"}}, {false, "POST", "/api/v1/compose", fmt.Sprintf(`{"blueprint_name": "test-distro-2","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), http.StatusOK, `{"status": true}`, expectedComposeGoodDistro, []string{"build_id"}}, {false, "POST", "/api/v1/compose", fmt.Sprintf(`{"blueprint_name": "test-fedora-1","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), http.StatusBadRequest, `{"status": false,"errors":[{"id":"DistroError", "msg":"Unknown distribution: fedora-1"}]}`, nil, []string{"build_id"}}, - {false, "POST", "/api/v1/compose", `{"blueprint_name": "test-distro-2","compose_type": "imaginary_type","branch": "master"}`, http.StatusBadRequest, `{"status": false,"errors":[{"id":"UnknownComposeType", "msg":"Unknown compose type for architecture: imaginary_type"}]}`, nil, []string{"build_id"}}, + {false, "POST", "/api/v1/compose", `{"blueprint_name": "test-distro-2","compose_type": "imaginary_type","branch": "master"}`, http.StatusBadRequest, `{"status": false,"errors":[{"id":"ComposeError", "msg":"Failed to get compose type \"imaginary_type\": invalid image type: imaginary_type"}]}`, nil, []string{"build_id"}}, } tempdir, err := ioutil.TempDir("", "weldr-tests-") @@ -1637,7 +1637,8 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { fmt.Sprintf(`{"blueprint_name": "test","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), map[string][]string{test_distro.TestDistro2Name: {test_distro.TestImageTypeName}}, http.StatusBadRequest, - fmt.Sprintf(`{"status":false,"errors":[{"id":"UnknownComposeType","msg":"Unknown compose type for architecture: %s"}]}`, test_distro.TestImageTypeName), + fmt.Sprintf(`{"status":false,"errors":[{"id":"ComposeError","msg":"Failed to get compose type \"%[1]s\": image type \"%[1]s\" for distro \"%[2]s\" is denied by configuration"}]}`, + test_distro.TestImageTypeName, test_distro.TestDistro2Name), expectedComposeLocal, []string{"build_id"}, }, @@ -1655,7 +1656,8 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { fmt.Sprintf(`{"blueprint_name": "test","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), map[string][]string{test_distro.TestDistro2Name: {test_distro.TestImageTypeName, test_distro.TestImageType2Name}}, http.StatusBadRequest, - fmt.Sprintf(`{"status":false,"errors":[{"id":"UnknownComposeType","msg":"Unknown compose type for architecture: %s"}]}`, test_distro.TestImageTypeName), + fmt.Sprintf(`{"status":false,"errors":[{"id":"ComposeError","msg":"Failed to get compose type \"%[1]s\": image type \"%[1]s\" for distro \"%[2]s\" is denied by configuration"}]}`, + test_distro.TestImageTypeName, test_distro.TestDistro2Name), expectedComposeLocal, []string{"build_id"}, }, @@ -1664,7 +1666,8 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { fmt.Sprintf(`{"blueprint_name": "test","compose_type": "%s","branch": "master"}`, test_distro.TestImageType2Name), map[string][]string{test_distro.TestDistro2Name: {test_distro.TestImageTypeName, test_distro.TestImageType2Name}}, http.StatusBadRequest, - fmt.Sprintf(`{"status":false,"errors":[{"id":"UnknownComposeType","msg":"Unknown compose type for architecture: %s"}]}`, test_distro.TestImageType2Name), + fmt.Sprintf(`{"status":false,"errors":[{"id":"ComposeError","msg":"Failed to get compose type \"%[1]s\": image type \"%[1]s\" for distro \"%[2]s\" is denied by configuration"}]}`, + test_distro.TestImageType2Name, test_distro.TestDistro2Name), expectedComposeLocal2, []string{"build_id"}, }, @@ -1673,7 +1676,8 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { fmt.Sprintf(`{"blueprint_name": "test","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), map[string][]string{"*": {test_distro.TestImageTypeName}}, http.StatusBadRequest, - fmt.Sprintf(`{"status":false,"errors":[{"id":"UnknownComposeType","msg":"Unknown compose type for architecture: %s"}]}`, test_distro.TestImageTypeName), + fmt.Sprintf(`{"status":false,"errors":[{"id":"ComposeError","msg":"Failed to get compose type \"%[1]s\": image type \"%[1]s\" for distro \"%[2]s\" is denied by configuration"}]}`, + test_distro.TestImageTypeName, test_distro.TestDistro2Name), expectedComposeLocal, []string{"build_id"}, }, @@ -1682,7 +1686,8 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { fmt.Sprintf(`{"blueprint_name": "test","compose_type": "%s","branch": "master"}`, test_distro.TestImageTypeName), map[string][]string{test_distro.TestDistro2Name: {"*"}}, http.StatusBadRequest, - fmt.Sprintf(`{"status":false,"errors":[{"id":"UnknownComposeType","msg":"Unknown compose type for architecture: %s"}]}`, test_distro.TestImageTypeName), + fmt.Sprintf(`{"status":false,"errors":[{"id":"ComposeError","msg":"Failed to get compose type \"%[1]s\": image type \"%[1]s\" for distro \"%[2]s\" is denied by configuration"}]}`, + test_distro.TestImageTypeName, test_distro.TestDistro2Name), expectedComposeLocal, []string{"build_id"}, },