Skip to content

Commit

Permalink
handling pyxis version check errors to display preflight release url …
Browse files Browse the repository at this point in the history
…to user

Signed-off-by: Adam D. Cornett <adc@redhat.com>
  • Loading branch information
acornett21 committed Apr 10, 2024
1 parent 0670106 commit ebf2d66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/pyxis/pyxis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/pyxis/pyxis_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
Expand Down
32 changes: 32 additions & 0 deletions internal/pyxis/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ebf2d66

Please sign in to comment.