test: probe branches, local upload errors, upstream errors, rpm/gc error paths

This commit is contained in:
2026-07-03 13:22:24 +10:00
parent 3d80837539
commit 6bcda813fd
4 changed files with 100 additions and 10 deletions
+46 -10
View File
@@ -227,12 +227,28 @@ func TestServerVirtualMerge(t *testing.T) {
func TestServerProbe(t *testing.T) {
requireStack(t)
body := fmt.Sprintf(`{"package_type":"generic","base_url":%q,"path":"data/file.bin"}`, upstream.URL)
resp, _ := req(t, "POST", "/api/v2/probe", body)
// Probe should reach the mock upstream and report reachable (200) or a
// structured result; either way not a server error.
if resp.StatusCode >= 500 {
t.Errorf("probe server error: %d", resp.StatusCode)
create := fmt.Sprintf(`{"name":"srv-probe","package_type":"generic","repo_type":"remote","base_url":%q,"stale_on_error":true}`, upstream.URL)
req(t, "POST", "/api/v2/remotes", create)
defer req(t, "DELETE", "/api/v2/remotes/srv-probe", "")
// Reachable path -> status 200 in the probe body.
if resp, b := req(t, "POST", "/api/v2/probe", `{"remote":"srv-probe","path":"data/file.bin"}`); resp.StatusCode != 200 || !strings.Contains(string(b), `"status":200`) {
t.Errorf("probe reachable: %d %s", resp.StatusCode, b)
}
// Missing upstream path -> upstream error reported (502) in the body.
if resp, b := req(t, "POST", "/api/v2/probe", `{"remote":"srv-probe","path":"missing"}`); resp.StatusCode != 200 || !strings.Contains(string(b), `"status":502`) {
t.Errorf("probe missing: %d %s", resp.StatusCode, b)
}
// Unknown remote -> 404 in the body.
if resp, b := req(t, "POST", "/api/v2/probe", `{"remote":"nope","path":"x"}`); resp.StatusCode != 200 || !strings.Contains(string(b), `"status":404`) {
t.Errorf("probe unknown: %d %s", resp.StatusCode, b)
}
// Bad requests.
if resp, _ := req(t, "POST", "/api/v2/probe", `{}`); resp.StatusCode != 400 {
t.Errorf("probe missing fields: %d", resp.StatusCode)
}
if resp, _ := req(t, "POST", "/api/v2/probe", `not json`); resp.StatusCode != 400 {
t.Errorf("probe invalid json: %d", resp.StatusCode)
}
}
@@ -386,11 +402,31 @@ func TestServerLocalRemoveAndMissing(t *testing.T) {
}
}
func TestServerProbeUnreachable(t *testing.T) {
func TestServerLocalUploadErrors(t *testing.T) {
requireStack(t)
resp, _ := req(t, "POST", "/api/v2/probe", `{"package_type":"generic","base_url":"http://127.0.0.1:1","path":"x"}`)
if resp.StatusCode >= 500 {
t.Errorf("probe of unreachable upstream should not 500: %d", resp.StatusCode)
// Uploading to a remote-type repo is rejected.
create := fmt.Sprintf(`{"name":"srv-uerr","package_type":"generic","repo_type":"remote","base_url":%q,"stale_on_error":true}`, upstream.URL)
req(t, "POST", "/api/v2/remotes", create)
defer req(t, "DELETE", "/api/v2/remotes/srv-uerr", "")
if resp, _ := put(t, "/api/v2/remotes/srv-uerr/files/x.bin", []byte("x")); resp.StatusCode != 400 {
t.Errorf("upload to remote repo should be 400, got %d", resp.StatusCode)
}
// Duplicate generic upload is a conflict.
req(t, "POST", "/api/v2/remotes", `{"name":"srv-dup","package_type":"generic","repo_type":"local"}`)
defer req(t, "DELETE", "/api/v2/remotes/srv-dup", "")
put(t, "/api/v2/remotes/srv-dup/files/dup.bin", []byte("one"))
if resp, _ := put(t, "/api/v2/remotes/srv-dup/files/dup.bin", []byte("two")); resp.StatusCode != 409 {
t.Errorf("duplicate upload should be 409, got %d", resp.StatusCode)
}
// Download of a missing local file is 404.
if resp, _ := req(t, "GET", "/api/v1/local/srv-dup/does/not/exist", ""); resp.StatusCode != 404 {
t.Errorf("missing local download should be 404, got %d", resp.StatusCode)
}
// Unknown virtual is 404.
if resp, _ := req(t, "GET", "/api/v1/virtual/nope/index.yaml", ""); resp.StatusCode != 404 {
t.Errorf("unknown virtual should be 404, got %d", resp.StatusCode)
}
}