141 lines
3.9 KiB
Go
141 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
opsdomain "github.com/geo-platform/tenant-api/internal/ops/domain"
|
|
internalscheduler "github.com/geo-platform/tenant-api/internal/scheduler"
|
|
)
|
|
|
|
func TestSchedulerHTTPAddrDefaultsToLoopback(t *testing.T) {
|
|
if got := schedulerHTTPAddr("", 8081); got != "127.0.0.1:8081" {
|
|
t.Fatalf("schedulerHTTPAddr() = %q, want 127.0.0.1:8081", got)
|
|
}
|
|
if got := schedulerHTTPAddr("0.0.0.0", 8081); got != "0.0.0.0:8081" {
|
|
t.Fatalf("schedulerHTTPAddr() = %q, want 0.0.0.0:8081", got)
|
|
}
|
|
}
|
|
|
|
func TestSchedulerMetricsAuthMiddleware(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
tests := []struct {
|
|
name string
|
|
headerName string
|
|
header string
|
|
wantStatus int
|
|
}{
|
|
{name: "missing token", wantStatus: http.StatusUnauthorized},
|
|
{name: "bad bearer token", headerName: "Authorization", header: "Bearer bad", wantStatus: http.StatusUnauthorized},
|
|
{name: "valid bearer token", headerName: "Authorization", header: "Bearer secret-token", wantStatus: http.StatusNoContent},
|
|
{name: "valid internal token header", headerName: "X-Internal-Metrics-Token", header: "secret-token", wantStatus: http.StatusNoContent},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
router := gin.New()
|
|
router.GET("/metrics", schedulerMetricsAuthMiddlewareWithToken(func() string { return "secret-token" }), func(c *gin.Context) {
|
|
c.Status(http.StatusNoContent)
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
if tt.headerName != "" {
|
|
req.Header.Set(tt.headerName, tt.header)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(rec, req)
|
|
if rec.Code != tt.wantStatus {
|
|
t.Fatalf("status = %d, want %d", rec.Code, tt.wantStatus)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMonitoringDailyRunOptionsUsesJobBatchSize(t *testing.T) {
|
|
batchSize := 88
|
|
got := monitoringDailyRunOptions(internalscheduler.JobRunContext{
|
|
Job: &opsdomain.SchedulerJob{BatchSize: &batchSize},
|
|
Config: map[string]any{
|
|
"max_materialize_per_run": 999,
|
|
"max_materialize_per_plan": 12,
|
|
"max_materialize_per_brand": 4,
|
|
},
|
|
})
|
|
|
|
if got.MaxMaterializePerRun != 88 {
|
|
t.Fatalf("MaxMaterializePerRun = %d, want 88", got.MaxMaterializePerRun)
|
|
}
|
|
if got.MaxMaterializePerPlan != 12 || got.MaxMaterializePerBrand != 4 {
|
|
t.Fatalf("unexpected plan/brand options: %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestGenerationStateCheckRunOptionsUsesOpsConfig(t *testing.T) {
|
|
batchSize := 50
|
|
got := generationStateCheckRunOptions(internalscheduler.JobRunContext{
|
|
Job: &opsdomain.SchedulerJob{
|
|
BatchSize: &batchSize,
|
|
TimeoutSeconds: 7,
|
|
},
|
|
Config: map[string]any{
|
|
"lookback": "2h",
|
|
},
|
|
})
|
|
|
|
if got.BatchSize != 50 {
|
|
t.Fatalf("BatchSize = %d, want 50", got.BatchSize)
|
|
}
|
|
if got.Timeout != 7*time.Second {
|
|
t.Fatalf("Timeout = %s, want 7s", got.Timeout)
|
|
}
|
|
if got.Lookback != 2*time.Hour {
|
|
t.Fatalf("Lookback = %s, want 2h", got.Lookback)
|
|
}
|
|
}
|
|
|
|
func TestStartSchedulerWorkerWaitsForRunToReturn(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
var wg sync.WaitGroup
|
|
started := make(chan struct{})
|
|
startSchedulerWorker(ctx, &wg, "test_worker", testSchedulerLogger{}, func(ctx context.Context) {
|
|
close(started)
|
|
<-ctx.Done()
|
|
})
|
|
|
|
select {
|
|
case <-started:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("worker did not start")
|
|
}
|
|
|
|
cancel()
|
|
if ok := waitSchedulerWorkers(&wg, time.Second, testSchedulerLogger{}); !ok {
|
|
t.Fatal("expected worker shutdown wait to complete")
|
|
}
|
|
}
|
|
|
|
func TestWaitSchedulerWorkersTimesOut(t *testing.T) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
defer wg.Done()
|
|
|
|
if ok := waitSchedulerWorkers(&wg, time.Nanosecond, testSchedulerLogger{}); ok {
|
|
t.Fatal("expected worker shutdown wait to time out")
|
|
}
|
|
}
|
|
|
|
type testSchedulerLogger struct{}
|
|
|
|
func (testSchedulerLogger) Infof(string, ...interface{}) {}
|
|
func (testSchedulerLogger) Errorf(string, ...interface{}) {}
|