Download internal/server/admin_tasks_test.go from a3216/gcli2api: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/server/admin_tasks_test.go
- Command line
-
hf download hf://spaces/a3216/gcli2api/internal/server/admin_tasks_test.go
-
curl -L -o admin_tasks_test.go https://huggingface.co/spaces/a3216/gcli2api/resolve/main/internal/server/admin_tasks_test.go
1.81 kB
| package server | |
| import ( | |
| "net/http" | |
| "net/http/httptest" | |
| "os" | |
| "path/filepath" | |
| "strings" | |
| "testing" | |
| ) | |
| type fakeTasks struct{ block chan struct{} } | |
| func (f *fakeTasks) RunCheckinNow() { <-f.block } | |
| func (f *fakeTasks) RunActivityNow() {} | |
| func (f *fakeTasks) RunKeepaliveNow() {} | |
| func (f *fakeTasks) RunTravelNow() {} | |
| func (f *fakeTasks) RunGrowthNow() {} | |
| func TestAdminTaskRunOnce(t *testing.T) { | |
| dir := t.TempDir() | |
| audit, err := NewAuditLog(filepath.Join(dir, "audit.log"), "k") | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| block := make(chan struct{}) | |
| h := NewHandler(Config{ | |
| AdminEnabled: true, | |
| APIKey: "k", | |
| Tasks: &fakeTasks{block: block}, | |
| Audit: audit, | |
| }) | |
| req := func() *httptest.ResponseRecorder { | |
| r := httptest.NewRequest(http.MethodPost, "/admin/tasks/checkin/run", nil) | |
| r.Header.Set("Authorization", "Bearer k") | |
| w := httptest.NewRecorder() | |
| h.ServeHTTP(w, r) | |
| return w | |
| } | |
| if req().Code != http.StatusAccepted { | |
| t.Fatal("first run should be 202") | |
| } | |
| if req().Code != http.StatusConflict { | |
| t.Fatal("second run should be 409 while the first is blocked") | |
| } | |
| close(block) | |
| unknown := httptest.NewRequest(http.MethodPost, "/admin/tasks/school/run", nil) | |
| unknown.Header.Set("Authorization", "Bearer k") | |
| w := httptest.NewRecorder() | |
| h.ServeHTTP(w, unknown) | |
| if w.Code != http.StatusNotFound { | |
| t.Fatalf("unknown task %d", w.Code) | |
| } | |
| raw, err := os.ReadFile(audit.Path()) | |
| if err != nil || !strings.Contains(string(raw), `"action":"task.run"`) { | |
| t.Fatalf("audit missing task.run: %v %s", err, raw) | |
| } | |
| } | |
| func TestMetricsOffIs404(t *testing.T) { | |
| h := NewHandler(Config{}) | |
| w := httptest.NewRecorder() | |
| h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/metrics", nil)) | |
| if w.Code != http.StatusNotFound { | |
| t.Fatalf("metrics off got %d", w.Code) | |
| } | |
| } | |