-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_scheduled_test.go
More file actions
156 lines (141 loc) · 4.57 KB
/
Copy pathe2e_scheduled_test.go
File metadata and controls
156 lines (141 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// TestE2EScheduledFiresAtTick proves the real wiring end to end: a scheduled
// service is not started at boot, the daemon stays alive with no running
// children, and the run fires at the next cron minute. Cron's granularity is
// one minute, so this test waits up to ~65s of wall clock (the timing logic
// itself is covered deterministically by the synctest scheduler tests).
func TestE2EScheduledFiresAtTick(t *testing.T) {
if testing.Short() {
t.Skip("waits for a real cron minute boundary")
}
dir := t.TempDir()
marker := filepath.Join(dir, "ran")
bootedAt := time.Now()
td := startDaemon(t, fmt.Sprintf(`
processes:
- name: job
command: /bin/sh
args: ["-c", "date >> %s"]
startup: scheduled
schedule: "* * * * *"
`, marker))
defer td.kill()
// The job must not run at boot. Only checkable when boot wasn't within a
// breath of a minute boundary (the first legitimate tick).
if bootedAt.Second() < 55 {
if _, err := os.Stat(marker); err == nil {
t.Fatal("scheduled job ran at boot; it must only run at cron ticks")
}
}
// Status must identify the service as scheduled, not stopped/pending.
resp := td.sendCommand("status job")
if !strings.Contains(resp, "scheduled") {
t.Fatalf("status = %q, want it to mention scheduled", resp)
}
// The run fires at the next minute boundary: within 65s of boot.
deadline := bootedAt.Add(65 * time.Second)
for {
if _, err := os.Stat(marker); err == nil {
break
}
if time.Now().After(deadline) {
t.Fatal("scheduled job did not run within 65s")
}
time.Sleep(200 * time.Millisecond)
}
// The daemon must still be alive after the oneshot-style run exits
// cleanly (exit 0 must not trigger any shutdown action).
time.Sleep(500 * time.Millisecond)
if !td.daemonAlive() {
t.Fatal("daemon exited after scheduled run completed")
}
resp = td.sendCommand("status job")
if !strings.Contains(resp, "scheduled") {
t.Fatalf("status after run = %q, want it to mention scheduled", resp)
}
}
// TestE2EScheduledReload verifies a reload can add and remove scheduled
// services: an added one is registered (status shows the next run) without
// being started, and a removed one disappears. Tick firing after reload is
// covered by the synctest runner tests; waiting out a real minute here would
// double the suite's wall clock for no extra signal.
func TestE2EScheduledReload(t *testing.T) {
td := startDaemon(t, `
processes:
- name: app
command: sleep
args: ["300"]
on-success: ignore
on-failure: shutdown
`)
defer td.kill()
td.updateConfig(`
processes:
- name: app
command: sleep
args: ["300"]
on-success: ignore
on-failure: shutdown
- name: job
command: /bin/true
startup: scheduled
schedule: "0 3 * * *"
`)
resp := td.sendCommand("reload")
if strings.Contains(resp, "error") {
t.Fatalf("reload failed: %s", resp)
}
resp = td.sendCommand("status job")
if !strings.Contains(resp, "scheduled (next run") {
t.Fatalf("status after add = %q, want scheduled with next run", resp)
}
// Reload must not have started the scheduled service.
if strings.Contains(resp, "running") {
t.Fatalf("scheduled job running right after reload: %s", resp)
}
// Remove it again; the daemon must survive and forget the service.
td.updateConfig(`
processes:
- name: app
command: sleep
args: ["300"]
on-success: ignore
on-failure: shutdown
`)
resp = td.sendCommand("reload")
if strings.Contains(resp, "error") {
t.Fatalf("second reload failed: %s", resp)
}
resp = td.sendCommand("status job")
if !strings.Contains(resp, "unknown") {
t.Fatalf("status after remove = %q, want unknown service", resp)
}
if !td.daemonAlive() {
t.Fatal("daemon died across scheduled reloads")
}
// Stop gracefully: app (sleep 300) inherits the daemon's stdout pipe, and
// the deferred kill()'s cmd.Wait would block on that pipe until the orphan
// exits. SIGTERM lets the daemon stop its children first.
td.stop()
}