test: server Run, upstream 500/401 branches, virtual dead members, local DB accessor
This commit is contained in:
@@ -121,3 +121,10 @@ func TestLocalErrorPaths(t *testing.T) {
|
|||||||
t.Errorf("remove = %d, want 500", c)
|
t.Errorf("remove = %d, want 500", c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocalHandlerDBAccessor(t *testing.T) {
|
||||||
|
db := closedDB(t)
|
||||||
|
if NewLocalHandler(db, nil).DB() != db {
|
||||||
|
t.Error("DB() should return the handler's database")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -108,6 +108,11 @@ func mockUpstream(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte("protected payload 2"))
|
w.Write([]byte("protected payload 2"))
|
||||||
case "/token":
|
case "/token":
|
||||||
w.Write([]byte(`{"token":"minted-token","expires_in":300}`))
|
w.Write([]byte(`{"token":"minted-token","expires_in":300}`))
|
||||||
|
case "/err500":
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
case "/noauth": // 401 with an unusable challenge (no realm)
|
||||||
|
w.Header().Set("Www-Authenticate", `Bearer service="reg"`)
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
default:
|
default:
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
@@ -324,6 +329,25 @@ func TestFetchUpstreamError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFetchUpstreamStatusErrors(t *testing.T) {
|
||||||
|
requireStack(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
p := prov(t, models.PackageGeneric)
|
||||||
|
|
||||||
|
r := seed(t, genericRemote("eng-500"))
|
||||||
|
_, err := testEngine.Fetch(ctx, r, "err500", p)
|
||||||
|
var pe *ProxyError
|
||||||
|
if err == nil || !asProxyError(err, &pe) || pe.Status != http.StatusInternalServerError {
|
||||||
|
t.Errorf("expected 500 ProxyError, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r = seed(t, genericRemote("eng-noauth"))
|
||||||
|
_, err = testEngine.Fetch(ctx, r, "noauth", p)
|
||||||
|
if err == nil || !asProxyError(err, &pe) || pe.Status != http.StatusUnauthorized {
|
||||||
|
t.Errorf("expected 401 ProxyError, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBearerTokenParsing(t *testing.T) {
|
func TestBearerTokenParsing(t *testing.T) {
|
||||||
// Non-Bearer challenges and missing realms are rejected.
|
// Non-Bearer challenges and missing realms are rejected.
|
||||||
if _, _, err := fetchBearerToken(context.Background(), "Basic realm=x", models.Remote{}); err == nil {
|
if _, _, err := fetchBearerToken(context.Background(), "Basic realm=x", models.Remote{}); err == nil {
|
||||||
|
|||||||
@@ -484,6 +484,33 @@ func TestRunOnListener(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRun(t *testing.T) {
|
||||||
|
requireStack(t)
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
errc := make(chan error, 1)
|
||||||
|
go func() { errc <- testSrv.Run(ctx) }()
|
||||||
|
time.Sleep(300 * time.Millisecond) // let it bind and start serving
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case err := <-errc:
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Run returned error: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(12 * time.Second):
|
||||||
|
t.Fatal("Run did not shut down")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerVirtualUnreachableMembers(t *testing.T) {
|
||||||
|
requireStack(t)
|
||||||
|
// A virtual whose only member does not exist -> no members reachable.
|
||||||
|
req(t, "POST", "/api/v2/virtuals", `{"name":"srv-vbad","package_type":"helm","members":["nonexistent-member"]}`)
|
||||||
|
defer req(t, "DELETE", "/api/v2/virtuals/srv-vbad", "")
|
||||||
|
if resp, _ := req(t, "GET", "/api/v1/virtual/srv-vbad/index.yaml", ""); resp.StatusCode != 502 {
|
||||||
|
t.Errorf("virtual with dead members = %d, want 502", resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServerVirtualLocalPyPIMerge(t *testing.T) {
|
func TestServerVirtualLocalPyPIMerge(t *testing.T) {
|
||||||
requireStack(t)
|
requireStack(t)
|
||||||
for _, n := range []string{"a", "b"} {
|
for _, n := range []string{"a", "b"} {
|
||||||
|
|||||||
Reference in New Issue
Block a user