mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
test(observability-proxy): cover metrics concurrency and flag overrides
The per-user concurrency cap on the metrics root had no test, so dropping it from the handler went unnoticed. The new test holds one request in the upstream, expects the same user's second one to be refused and another user's to go through. Overrides of the metrics flags were only checked for their defaults and refusals. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
1ac7fd29bf
commit
81cd278f63
2 files changed
+55
No files matched your search
@@ -60,6 +60,21 @@ func TestParseFlagsOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsMetricsOverrides(t *testing.T) {
|
||||
o, err := parseFlags([]string{
|
||||
"--root-tenant=tenant-x", "--metrics-shortterm-store=fast", "--metrics-longterm-store=slow",
|
||||
"--metrics-shortterm-retention=24h", "--metrics-max-range=168h", "--metrics-max-points=500", "--metrics-max-timeout=10s",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := o.metrics
|
||||
if m.RootTenant != "tenant-x" || m.ShortTermStore != "fast" || m.LongTermStore != "slow" || m.ShortTermRetention != 24*time.Hour ||
|
||||
m.Limits.MaxRange != 168*time.Hour || m.Limits.MaxPoints != 500 || m.Limits.MaxTimeout != 10*time.Second {
|
||||
t.Errorf("overrides not applied: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsInvalid(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--root-tenant="},
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -155,6 +156,45 @@ func TestServerMetricsStatusCodes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerMetricsConcurrency(t *testing.T) {
|
||||
entered, release := make(chan struct{}, 3), make(chan struct{})
|
||||
h := newHarness(t, Config{Limiter: LimiterConfig{MaxConcurrentPerUser: 1}}, func(w http.ResponseWriter, _ *http.Request) {
|
||||
entered <- struct{}{}
|
||||
<-release
|
||||
promUpstream(w, nil)
|
||||
})
|
||||
// Registered after the harness so it runs before the servers close.
|
||||
var once sync.Once
|
||||
t.Cleanup(func() { once.Do(func() { close(release) }) })
|
||||
held := make(chan int, 2)
|
||||
hold := func(token string) {
|
||||
go func() { held <- h.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", token) }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatalf("request of %s did not reach the upstream", token)
|
||||
}
|
||||
}
|
||||
hold("good")
|
||||
refused := make(chan int, 1)
|
||||
go func() { refused <- h.status(http.MethodPost, metricsRoot+"api/v1/query?query=up", "good") }()
|
||||
select {
|
||||
case got := <-refused:
|
||||
if got != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("concurrent request answered %d, want 422", got)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("concurrent request of the same user reached the upstream")
|
||||
}
|
||||
hold("bob-token")
|
||||
once.Do(func() { close(release) })
|
||||
for range 2 {
|
||||
if got := <-held; got != http.StatusOK {
|
||||
t.Fatalf("held request answered %d", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultMetricsUpstreamURL(t *testing.T) {
|
||||
s := NewServer(Config{ClusterDomain: "cozy.local", Metrics: MetricsConfig{RootTenant: "tenant-root"}}, nil, nil, nil)
|
||||
if got, want := s.cfg.Metrics.UpstreamURL("shortterm"), "http://vmselect-shortterm.tenant-root.svc.cozy.local:8481/select/0/prometheus"; got != want {
|
||||
|
||||
Reference in new issue
Block a user