Logging: - structured slog with LOG_FORMAT=json|text and a stable key vocabulary - periodic stats record with queue depths, db pool, goroutines, memory, dns/probe - no per-certificate logging (hot path is counters only) UI (Bucketeer branding, DESIGN.md): - light+dark themes (OS default, persisted header toggle, no first-paint flash) - official mascot: lossless WebP light/dark logos + favicon, embedded and served with cache headers (1.2MB source PNGs -> ~27/22KB webp) - Assets/Contents views, pagination (offset) and native virtual scrolling (content-visibility) so large result sets do not blow up the DOM - search with debounce/abort + URL state, exposure chips, sort, summary counts Readability + indexing: - internal/index listers for S3-compatible, GCS, Azure, Firebase, OpenSearch (metadata only: keys/sizes/dates; never object contents) - asset_entries table + store write/search; readable resources are indexed and searchable in the UI - deep, unauthenticated, read-only DB probes (PROBE_DEEP): Redis PING, PostgreSQL StartupMessage, MongoDB OP_MSG listDatabases; OpenSearch/Neptune HTTP reads - Azure account-level listing fixed: 403 is reported reachable, not auth_required, since anonymous access is per-container DNS: - curated reliable resolvers by default; DNS_SERVERS=fresh adds the compiled-in Fresh-Resolvers snapshot with a bounded startup sample (warming all 6.7k at once broke the container resolver) - random healthy selection + failover + per-server cooldown Other: prober Close fixes a shutdown hang; config/README updated.
122 lines
3.3 KiB
Go
122 lines
3.3 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"tea.karlcom.de/karl/bucketeer/internal/store"
|
|
)
|
|
|
|
type fakeStore struct {
|
|
lastFilter store.AssetFilter
|
|
}
|
|
|
|
func (f *fakeStore) ListAssets(ctx context.Context, fl store.AssetFilter) ([]store.Asset, error) {
|
|
f.lastFilter = fl
|
|
return []store.Asset{{ID: 1, Provider: "aws", Service: "s3", ProviderHost: "b.s3.amazonaws.com", Exposure: "public_read"}}, nil
|
|
}
|
|
|
|
func (f *fakeStore) CountAssets(ctx context.Context, fl store.AssetFilter) (int, error) {
|
|
return 1, nil
|
|
}
|
|
|
|
func (f *fakeStore) SearchEntries(ctx context.Context, query string, limit, offset int) ([]store.IndexEntry, int, error) {
|
|
return []store.IndexEntry{{ID: 7, Kind: "object", Name: "photos/a.jpg", ProviderHost: "b.s3.amazonaws.com"}}, 1, nil
|
|
}
|
|
|
|
func (f *fakeStore) Stats(ctx context.Context) (store.Stats, error) {
|
|
return store.Stats{Total: 1, Readable: 1, Entries: 1}, nil
|
|
}
|
|
|
|
func TestAPI(t *testing.T) {
|
|
fs := &fakeStore{}
|
|
srv := httptest.NewServer(New(fs, Config{}))
|
|
defer srv.Close()
|
|
|
|
resp, err := http.Get(srv.URL + "/api/assets?provider=aws&q=bucket&limit=5")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d", resp.StatusCode)
|
|
}
|
|
var payload struct {
|
|
Assets []store.Asset `json:"assets"`
|
|
Total int `json:"total"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(payload.Assets) != 1 || payload.Total != 1 || fs.lastFilter.Provider != "aws" || fs.lastFilter.Query != "bucket" || fs.lastFilter.Limit != 5 {
|
|
t.Fatalf("unexpected assets/filter: %+v %+v", payload, fs.lastFilter)
|
|
}
|
|
|
|
entriesResp, err := http.Get(srv.URL + "/api/entries?q=photos")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer entriesResp.Body.Close()
|
|
var entriesPayload struct {
|
|
Entries []store.IndexEntry `json:"entries"`
|
|
Total int `json:"total"`
|
|
}
|
|
if err := json.NewDecoder(entriesResp.Body).Decode(&entriesPayload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(entriesPayload.Entries) != 1 || entriesPayload.Total != 1 {
|
|
t.Fatalf("unexpected entries: %+v", entriesPayload)
|
|
}
|
|
|
|
statsResp, err := http.Get(srv.URL + "/api/stats")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer statsResp.Body.Close()
|
|
var st store.Stats
|
|
if err := json.NewDecoder(statsResp.Body).Decode(&st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st.Total != 1 {
|
|
t.Fatalf("stats = %+v", st)
|
|
}
|
|
}
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
srv := httptest.NewServer(New(&fakeStore{}, Config{User: "u", Pass: "p"}))
|
|
defer srv.Close()
|
|
|
|
resp, _ := http.Get(srv.URL + "/api/stats")
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d, want 401", resp.StatusCode)
|
|
}
|
|
resp.Body.Close()
|
|
|
|
req, _ := http.NewRequest(http.MethodGet, srv.URL+"/api/stats", nil)
|
|
req.SetBasicAuth("u", "p")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestIndexServed(t *testing.T) {
|
|
srv := httptest.NewServer(New(&fakeStore{}, Config{}))
|
|
defer srv.Close()
|
|
resp, err := http.Get(srv.URL + "/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if ct := resp.Header.Get("Content-Type"); ct != "text/html; charset=utf-8" {
|
|
t.Fatalf("content-type = %q", ct)
|
|
}
|
|
}
|