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 May 13, 2024
1 parent 673a0a9 commit 83ee11a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 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 @@ -431,6 +435,14 @@ func (p *pyxisClient) createTestResults(ctx context.Context, testResults *TestRe
}

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(string(body), "not recognized") || strings.Contains(string(body), "not supported")) {
logger.Error(errors.New("invalid preflight version, 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,
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-preflight-version":
response.WriteHeader(http.StatusBadRequest)
mustWrite(response, `{"detail":"not recognized"}`)
case request.Header["X-Api-Key"][0] == "my-unsupported-preflight-version":
response.WriteHeader(http.StatusBadRequest)
mustWrite(response, `{"detail":"not supported"}`)
case request.Method == http.MethodPatch && request.Header["X-Api-Key"][0] == "my-bad-results-patch-api-token":
response.WriteHeader(http.StatusInternalServerError)
default:
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 @@ -305,6 +305,38 @@ var _ = Describe("Pyxis Submit", func() {
})
})

Context("createTestResults 400 version not supported", func() {
BeforeEach(func() {
pyxisClient.APIToken = "my-unsupported-preflight-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-preflight-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 83ee11a

Please sign in to comment.