-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
httptransport: return Accepted when not ready
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
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |