package dataplane_test import ( "context" "fmt" "net/http" "testing" "tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/dataplane" ) // board is the Duty telemetry/daily-board of the Mandate platform, // which runs on a schedule. Its CronJob made the Job board-29371260. var board = dataplane.DutyRef{Namespace: "telemetry", Name: "daily-board"} func (c *fakeControl) Schedule(_ context.Context, duty dataplane.DutyRef) (dataplane.ScheduleIdentity, error) { if duty != board { return dataplane.ScheduleIdentity{}, dataplane.ErrUnknownDuty } return dataplane.ScheduleIdentity{Mandate: "platform", ServiceAccount: "simmer-platform"}, nil } func (c *fakeControl) Start(_ context.Context, duty dataplane.DutyRef, job string) (dataplane.StartAnswer, error) { c.mu.Lock() defer c.mu.Unlock() c.starts = append(c.starts, job) switch { case c.refuseStart != nil: return dataplane.StartAnswer{}, c.refuseStart case job != "board-29371260": return dataplane.StartAnswer{}, dataplane.ErrNotTheSchedule } return dataplane.StartAnswer{Session: "platform-b0a2d", Started: true, Message: "started"}, nil } // schedulePods adds the pods that may or may not start board. func schedulePods(add func(string, dataplane.Caller, dataplane.Pod)) { labels := map[string]string{v1alpha1.LabelSchedule: "daily-board", v1alpha1.LabelMandate: "platform"} caller, pod := jobPod("telemetry", "board-29371260-x8k2p", labels) add("schedule", caller, pod) caller, pod = jobPod("telemetry", "board-29371200-done0", labels) pod.Phase = "Succeeded" add("finished-schedule", caller, pod) // A Job that a role or a template let label its pod as the // schedule's, which no CronJob of the operator's made. caller, pod = jobPod("telemetry", "forged-x8k2p", labels) add("forged-schedule", caller, pod) caller, pod = jobPod("telemetry", "board-29371260-other", labels) caller.ServiceAccount, pod.ServiceAccount = "simmer-backups", "simmer-backups" add("schedule-of-another-account", caller, pod) caller, pod = jobPod("telemetry", "board-29371260-nolab", map[string]string{v1alpha1.LabelMandate: "platform"}) add("unlabeled-schedule", caller, pod) caller, pod = jobPod("telemetry", "board-29371260-other", map[string]string{v1alpha1.LabelSchedule: "daily-board", v1alpha1.LabelMandate: "backups"}) add("schedule-of-another-mandate", caller, pod) } func TestTheStartRouteAcceptsOnlyTheDutysSchedulePods(t *testing.T) { cases := []struct { token string want int }{ {"", http.StatusUnauthorized}, {"own", http.StatusForbidden}, {"hook", http.StatusForbidden}, {"probe", http.StatusForbidden}, {"finished-schedule", http.StatusForbidden}, {"forged-schedule", http.StatusForbidden}, {"schedule-of-another-account", http.StatusForbidden}, {"unlabeled-schedule", http.StatusForbidden}, {"schedule-of-another-mandate", http.StatusForbidden}, {"schedule", http.StatusOK}, } for _, c := range cases { t.Run(c.token, func(t *testing.T) { got := newWorld(t).send("POST", "/v1/duties/telemetry/daily-board/start", c.token, "") if got.Code != c.want { t.Errorf("answered %d, want %d: %s", got.Code, c.want, got.Body) } }) } } func TestAStartReachesControlWithTheCallersJob(t *testing.T) { w := newWorld(t) answer := decode[dataplane.StartAnswer](t, w.send("POST", "/v1/duties/telemetry/daily-board/start", "schedule", "")) if fmt.Sprint(w.control.starts) != "[board-29371260]" || !answer.Started || answer.Session != "platform-b0a2d" { t.Errorf("control got %v and answered %+v", w.control.starts, answer) } } func TestAStartThatControlRefusesKeepsItsStatus(t *testing.T) { cases := []struct { name, path string refusal error want, starts int }{ {"a Duty with no schedule", "/v1/duties/telemetry/alerts-tuning/start", nil, http.StatusNotFound, 0}, {"a Duty that is no name", "/v1/duties/telemetry/Notes:Board/start", nil, http.StatusNotFound, 0}, {"a Duty that may not start", "/v1/duties/telemetry/daily-board/start", fmt.Errorf("%w: a Stop exists", dataplane.ErrStartRefused), http.StatusConflict, 1}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { w := newWorld(t) w.control.refuseStart = c.refusal got := w.send("POST", c.path, "schedule", "") if got.Code != c.want || len(w.control.starts) != c.starts { t.Errorf("answered %d and control got %v, want %d and %d starts: %s", got.Code, w.control.starts, c.want, c.starts, got.Body) } }) } } func TestTheClientStartsAScheduledDuty(t *testing.T) { s := serve(t, "schedule") answer, err := s.client.Start(t.Context(), board) if err != nil || !answer.Started || answer.Session != "platform-b0a2d" { t.Errorf("answered %+v, %v; want the started Session", answer, err) } } func TestADutyRefIsNamespaceAndName(t *testing.T) { cases := []struct { text string ok bool }{ {"telemetry/daily-board", true}, {"daily-board", false}, {"telemetry/Notes:Board", false}, } for _, c := range cases { t.Run(c.text, func(t *testing.T) { if _, err := dataplane.ParseDutyRef(c.text); (err == nil) != c.ok { t.Errorf("parsed with %v, want ok %v", err, c.ok) } }) } }