Skip to content

Commit

Permalink
httptransport: return Accepted when not ready
Browse files Browse the repository at this point in the history
This has the HTTP layer return Accepted (202) when the underlying
service reports it's not initialized.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Mar 23, 2021
1 parent 858c540 commit 8616cc6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
14 changes: 14 additions & 0 deletions httptransport/vulnerabilityreporthandler.go
Expand Up @@ -51,6 +51,20 @@ func VulnerabilityReportHandler(service matcher.Service, indexer indexer.Service
return
}

initd, err := service.Initialized(ctx)
if err != nil {
resp := &je.Response{
Code: "internal-server-error",
Message: err.Error(),
}
je.Error(w, resp, http.StatusInternalServerError)
return
}
if !initd {
w.WriteHeader(http.StatusAccepted)
return
}

indexReport, ok, err := indexer.IndexReport(ctx, manifest)
// check err first
if err != nil {
Expand Down
58 changes: 58 additions & 0 deletions httptransport/vulnerabilityreporthandler_test.go
@@ -0,0 +1,58 @@
package httptransport

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/quay/claircore"

"github.com/quay/clair/v4/indexer"
"github.com/quay/clair/v4/matcher"
)

func TestInitialized(t *testing.T) {
var initd bool
digest := claircore.MustParseDigest("sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
h := VulnerabilityReportHandler(
&matcher.Mock{
Initialized_: func(_ context.Context) (bool, error) {
return initd, nil
},
},
&indexer.Mock{
IndexReport_: func(ctx context.Context, d claircore.Digest) (*claircore.IndexReport, bool, error) {
if got, want := d.String(), digest.String(); got != want {
return nil, false, fmt.Errorf("unexpected digest: %v", got)
}
return nil, false, nil
},
},
)
srv := httptest.NewServer(h)
defer srv.Close()
c := srv.Client()

res, err := c.Get(srv.URL + "/" + digest.String())
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
t.Log(res.Status)
if res.StatusCode != http.StatusAccepted {
t.Errorf("unexpected response")
}

initd = true
res, err = c.Get(srv.URL + "/" + digest.String())
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
t.Log(res.Status)
if res.StatusCode != http.StatusNotFound {
t.Errorf("unexpected response")
}
}

0 comments on commit 8616cc6

Please sign in to comment.