96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"sync"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
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", schedulerMetricsAuthMiddleware("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 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{}) {}
|