package imageproxy import ( "errors" "testing" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" "sneak.berlin/go/pixa/internal/imgcache" ) // TestNewProxy_RegistersItsCountersOnItsRegistry checks that NewProxy // registers the proxy's counters on the registry it is given, under the // names README.md gives them. The cache registers its own counters on // another registry. func TestNewProxy_RegistersItsCountersOnItsRegistry(t *testing.T) { t.Parallel() cache, err := imgcache.NewCache(setupTestDB(t), imgcache.Config{StateDir: t.TempDir(), MaxBytes: 1 << 30}, prometheus.NewRegistry()) if err != nil { t.Fatalf("imgcache.NewCache() error = %v", err) } registry := prometheus.NewRegistry() _, err = NewProxy(&Config{Cache: cache}, registry) if err != nil { t.Fatalf("NewProxy() error = %v", err) } names := []string{ "pixa_upstream_fetches_total", "pixa_upstream_fetch_bytes_total", "pixa_transforms_total", } count, err := testutil.GatherAndCount(registry, names...) if err != nil { t.Fatalf("testutil.GatherAndCount() error = %v", err) } if count != len(names) { t.Errorf("%d of the counters %v are on the registry, want %d", count, names, len(names)) } } // TestNewProxy_FailsWhenItsCountersCannotBeRegistered makes a second proxy // on the registry of a first. Its counters have the same names, so the // registry refuses them, and NewProxy must return that error. func TestNewProxy_FailsWhenItsCountersCannotBeRegistered(t *testing.T) { t.Parallel() cache, err := imgcache.NewCache(setupTestDB(t), imgcache.Config{StateDir: t.TempDir(), MaxBytes: 1 << 30}, prometheus.NewRegistry()) if err != nil { t.Fatalf("imgcache.NewCache() error = %v", err) } registry := prometheus.NewRegistry() _, err = NewProxy(&Config{Cache: cache}, registry) if err != nil { t.Fatalf("NewProxy() of the first proxy error = %v", err) } _, err = NewProxy(&Config{Cache: cache}, registry) t.Logf("NewProxy() of the second proxy error = %v", err) var alreadyRegistered prometheus.AlreadyRegisteredError if !errors.As(err, &alreadyRegistered) { t.Errorf("NewProxy() of the second proxy error = %v, want %T", err, alreadyRegistered) } }