From ebf2d66fdb8c1b6e44c72a65363b70ceac616a1a Mon Sep 17 00:00:00 2001 From: "Adam D. Cornett" Date: Wed, 10 Apr 2024 10:20:04 -0700 Subject: [PATCH] handling pyxis version check errors to display preflight release url to user Signed-off-by: Adam D. Cornett --- internal/pyxis/pyxis.go | 17 ++++++++++++++-- internal/pyxis/pyxis_suite_test.go | 6 ++++++ internal/pyxis/submit_test.go | 32 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/internal/pyxis/pyxis.go b/internal/pyxis/pyxis.go index 217e3f5d8..abf851730 100644 --- a/internal/pyxis/pyxis.go +++ b/internal/pyxis/pyxis.go @@ -4,14 +4,17 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" + "strings" "github.com/go-logr/logr" "github.com/shurcooL/graphql" "github.com/redhat-openshift-ecosystem/openshift-preflight/internal/log" + "github.com/redhat-openshift-ecosystem/openshift-preflight/version" ) const ( @@ -409,6 +412,7 @@ func (p *pyxisClient) updateProject(ctx context.Context, certProject *CertProjec } func (p *pyxisClient) createTestResults(ctx context.Context, testResults *TestResults) (*TestResults, error) { + logger := logr.FromContextOrDiscard(ctx) b, err := json.Marshal(testResults) if err != nil { return nil, fmt.Errorf("could not marshal test results: %w", err) @@ -430,16 +434,25 @@ func (p *pyxisClient) createTestResults(ctx context.Context, testResults *TestRe return nil, fmt.Errorf("could not read body: %w", err) } + bodyAsString := string(body) if ok := checkStatus(resp.StatusCode); !ok { + // checking to see if the users is using an unsupported versions + // if so display preflights latest download url to the user + if resp.StatusCode == http.StatusBadRequest && + (strings.Contains(bodyAsString, "not recognized") || strings.Contains(bodyAsString, "not supported")) { + logger.Error(errors.New("invalid preflight version, please please download the latest version and re-submit"), + "release-url", fmt.Sprintf("https://%s/releases/latest", version.Version.Name)) + } + return nil, fmt.Errorf( "status code: %d: body: %s", resp.StatusCode, - string(body)) + bodyAsString) } newTestResults := TestResults{} if err := json.Unmarshal(body, &newTestResults); err != nil { - return nil, fmt.Errorf("could not unmarshal body: %s: %w", string(body), err) + return nil, fmt.Errorf("could not unmarshal body: %s: %w", bodyAsString, err) } return &newTestResults, nil diff --git a/internal/pyxis/pyxis_suite_test.go b/internal/pyxis/pyxis_suite_test.go index 3db263ae4..b87e380a5 100644 --- a/internal/pyxis/pyxis_suite_test.go +++ b/internal/pyxis/pyxis_suite_test.go @@ -157,6 +157,12 @@ func pyxisTestResultsHandler(ctx context.Context) http.HandlerFunc { switch { case request.Header["X-Api-Key"][0] == "my-bad-testresults-api-token": response.WriteHeader(http.StatusUnauthorized) + case request.Header["X-Api-Key"][0] == "my-unrecognized-perflight-version": + response.WriteHeader(http.StatusBadRequest) + mustWrite(response, `{"detail":"not recognized"}`) + case request.Header["X-Api-Key"][0] == "my-unsupported-perflight-version": + response.WriteHeader(http.StatusBadRequest) + mustWrite(response, `{"detail":"not supported"}`) default: mustWrite(response, `{"image":"quay.io/awesome/image:latest","passed": true}`) } diff --git a/internal/pyxis/submit_test.go b/internal/pyxis/submit_test.go index ee3a1dd01..f0577fc76 100644 --- a/internal/pyxis/submit_test.go +++ b/internal/pyxis/submit_test.go @@ -293,6 +293,38 @@ var _ = Describe("Pyxis Submit", func() { }) }) + Context("createTestResults 400 version not supported", func() { + BeforeEach(func() { + pyxisClient.APIToken = "my-unsupported-perflight-version" + pyxisClient.ProjectID = "my-awesome-project-id" + }) + Context("when a project is submitted", func() { + Context("and an unsupported preflight version is sent to createTestResults", func() { + It("should error", func() { + certResults, err := pyxisClient.SubmitResults(ctx, &certInput) + Expect(err).To(HaveOccurred()) + Expect(certResults).To(BeNil()) + }) + }) + }) + }) + + Context("createTestResults 400 version not recognized", func() { + BeforeEach(func() { + pyxisClient.APIToken = "my-unrecognized-perflight-version" + pyxisClient.ProjectID = "my-awesome-project-id" + }) + Context("when a project is submitted", func() { + Context("and an unrecognized preflight version is sent to createTestResults", func() { + It("should error", func() { + certResults, err := pyxisClient.SubmitResults(ctx, &certInput) + Expect(err).To(HaveOccurred()) + Expect(certResults).To(BeNil()) + }) + }) + }) + }) + Context("GetProject", func() { Context("when a project is submitted", func() { Context("and it is not already In Progress", func() {