Skip to content

Commit b496ff6

Browse files
committed
test(e2e): collapse the repeated daemon boot in the job tests
SonarCloud reported 29.6% duplication on new code against a 3% gate. The three tests differ only in the config they feed and the lines they expect, but each repeated the boot sequence — write config, start daemon, register cleanup, wait for the scheduler, join stdout and stderr — in full. Extracted to bootAndCapture, with a requireLogged helper for the assertions. The file drops from 160 to 113 lines and the tests read as what they check rather than how they get there. Signed-off-by: Sebastian Mendel <github@sebastianmendel.de>
1 parent f5c3d1b commit b496ff6

1 file changed

Lines changed: 36 additions & 56 deletions

File tree

e2e/unschedulable_job_test.go

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,50 @@ import (
2222
// These drive the real binary because the value that misled people was a log
2323
// line the daemon prints at startup.
2424

25+
// bootAndCapture starts the daemon on the given config, waits until it reports
26+
// a started scheduler, and returns everything it logged.
27+
//
28+
// The tests below differ only in the config they feed and the lines they
29+
// expect, so the boot sequence lives here rather than once per test.
30+
func bootAndCapture(t *testing.T, configBody string) string {
31+
t.Helper()
32+
33+
daemon := startDaemon(t, writeConfig(t, configBody))
34+
t.Cleanup(func() { daemon.shutdown(t, 15*time.Second) })
35+
36+
if err := daemon.waitForLog("Scheduler started", 15*time.Second); err != nil {
37+
t.Fatalf("daemon never reported a started scheduler: %v\nstdout=%s", err, daemon.stdout.String())
38+
}
39+
return daemon.stdout.String() + daemon.stderr.String()
40+
}
41+
42+
// requireLogged fails with the captured output when an expected line is absent.
43+
func requireLogged(t *testing.T, out string, wants ...string) {
44+
t.Helper()
45+
for _, want := range wants {
46+
if !strings.Contains(out, want) {
47+
t.Errorf("expected the daemon to log %q, got:\n%s", want, out)
48+
}
49+
}
50+
}
51+
2552
// TestE2E_UnschedulableJob_IsReported pins that the rejection is named at
2653
// error level and that the reported count reflects what is actually scheduled.
2754
func TestE2E_UnschedulableJob_IsReported(t *testing.T) {
2855
t.Parallel()
2956

30-
configPath := writeConfig(t, `[global]
57+
out := bootAndCapture(t, `[global]
3158
log-level = info
3259
3360
[job-local "broken"]
3461
schedule = not-a-schedule
3562
command = echo hi
3663
`)
3764

38-
daemon := startDaemon(t, configPath)
39-
t.Cleanup(func() { daemon.shutdown(t, 15*time.Second) })
40-
41-
if err := daemon.waitForLog("Scheduler started", 15*time.Second); err != nil {
42-
t.Fatalf("daemon never reported a started scheduler: %v\nstdout=%s", err, daemon.stdout.String())
43-
}
44-
45-
out := daemon.stdout.String() + daemon.stderr.String()
46-
47-
// The count has to describe reality. Reporting the configured total here
48-
// is what made a job that never runs look like a job that does.
49-
if !strings.Contains(out, "jobCount=0") {
50-
t.Errorf("expected jobCount=0 for a config whose only job was rejected, got:\n%s", out)
51-
}
52-
53-
// And the job has to be named, at a level that is not filtered out of
54-
// production logging.
55-
if !strings.Contains(out, "job will not run") {
56-
t.Errorf("the rejected job was not reported as unrunnable:\n%s", out)
57-
}
58-
if !strings.Contains(out, "broken") {
59-
t.Errorf("the report does not name the offending job:\n%s", out)
60-
}
65+
// The count has to describe reality — reporting the configured total is
66+
// what made a job that never runs look like a job that does — and the job
67+
// has to be named, at a level that is not filtered out in production.
68+
requireLogged(t, out, "jobCount=0", "job will not run", "broken")
6169
}
6270

6371
// TestE2E_SchedulableJob_ReportsHonestCount is the counterpart: a config whose
@@ -66,26 +74,15 @@ func TestE2E_UnschedulableJob_IsReported(t *testing.T) {
6674
func TestE2E_SchedulableJob_ReportsHonestCount(t *testing.T) {
6775
t.Parallel()
6876

69-
configPath := writeConfig(t, `[global]
77+
out := bootAndCapture(t, `[global]
7078
log-level = info
7179
7280
[job-local "fine"]
7381
schedule = @every 1h
7482
command = echo hi
7583
`)
7684

77-
daemon := startDaemon(t, configPath)
78-
t.Cleanup(func() { daemon.shutdown(t, 15*time.Second) })
79-
80-
if err := daemon.waitForLog("Scheduler started", 15*time.Second); err != nil {
81-
t.Fatalf("daemon never reported a started scheduler: %v\nstdout=%s", err, daemon.stdout.String())
82-
}
83-
84-
out := daemon.stdout.String() + daemon.stderr.String()
85-
86-
if !strings.Contains(out, "jobCount=1") {
87-
t.Errorf("expected jobCount=1 for one valid job, got:\n%s", out)
88-
}
85+
requireLogged(t, out, "jobCount=1")
8986
for _, unwanted := range []string{"job will not run", "some jobs were not scheduled"} {
9087
if strings.Contains(out, unwanted) {
9188
t.Errorf("a healthy config produced %q:\n%s", unwanted, out)
@@ -100,7 +97,7 @@ func TestE2E_SchedulableJob_ReportsHonestCount(t *testing.T) {
10097
func TestE2E_PartiallyUnschedulable_KeepsTheGoodJobs(t *testing.T) {
10198
t.Parallel()
10299

103-
configPath := writeConfig(t, `[global]
100+
out := bootAndCapture(t, `[global]
104101
log-level = info
105102
106103
[job-local "good"]
@@ -112,22 +109,5 @@ func TestE2E_PartiallyUnschedulable_KeepsTheGoodJobs(t *testing.T) {
112109
command = echo nope
113110
`)
114111

115-
daemon := startDaemon(t, configPath)
116-
t.Cleanup(func() { daemon.shutdown(t, 15*time.Second) })
117-
118-
if err := daemon.waitForLog("Scheduler started", 15*time.Second); err != nil {
119-
t.Fatalf("daemon never reported a started scheduler: %v\nstdout=%s", err, daemon.stdout.String())
120-
}
121-
122-
out := daemon.stdout.String() + daemon.stderr.String()
123-
124-
if !strings.Contains(out, "jobCount=1") {
125-
t.Errorf("expected jobCount=1 (one of two jobs scheduled), got:\n%s", out)
126-
}
127-
if !strings.Contains(out, "bad") {
128-
t.Errorf("the rejected job was not named:\n%s", out)
129-
}
130-
if !strings.Contains(out, "some jobs were not scheduled") {
131-
t.Errorf("the summary of rejected jobs was not reported:\n%s", out)
132-
}
112+
requireLogged(t, out, "jobCount=1", "bad", "some jobs were not scheduled")
133113
}

0 commit comments

Comments
 (0)