runnerd-core 0.0.23

Config, domain logic and API types shared by runnerd and its clients
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use super::*;
use crate::api::{JobMetrics, LogSummary};

fn job(state: JobState, conclusion: Option<&str>) -> JobSummary {
    JobSummary {
        id: 7,
        repository: "halyard/ledger".to_owned(),
        run_id: 1,
        run_attempt: 1,
        workflow: Some("CI".to_owned()),
        name: "build".to_owned(),
        branch: None,
        url: None,
        labels: vec![],
        state,
        conclusion: conclusion.map(str::to_owned),
        runner: None,
        timeline: Timeline {
            queued_at: 100,
            ..Timeline::default()
        },
        metrics: JobMetrics::default(),
        log: LogSummary::default(),
    }
}

fn timed(state: JobState, timeline: Timeline) -> JobSummary {
    JobSummary {
        timeline,
        ..job(state, None)
    }
}

fn step(status: &str, conclusion: Option<&str>) -> Step {
    Step {
        number: 1,
        name: "test".to_owned(),
        status: status.to_owned(),
        conclusion: conclusion.map(str::to_owned),
        started_at: Some(100),
        completed_at: None,
    }
}

fn sample(at: u64, memory: u64, cpu: u64) -> Sample {
    Sample {
        at,
        cpu,
        memory,
        network_received: 0,
        network_sent: 0,
    }
}

#[test]
fn a_job_another_machine_ran_is_neither_its_success_nor_its_failure() {
    for conclusion in ["success", "failure", "cancelled"] {
        let elsewhere = JobSummary {
            conclusion: Some(conclusion.to_owned()),
            ..timed(
                JobState::Elsewhere,
                Timeline {
                    queued_at: 100,
                    started_at: Some(110),
                    finished_at: Some(130),
                    ..Timeline::default()
                },
            )
        };
        assert_eq!(
            elsewhere.outcome(),
            Outcome::new("elsewhere", OutcomeKind::Ended),
            "{conclusion}"
        );
        assert!(
            !elsewhere.is_failure(),
            "another machine's {conclusion} job is not counted"
        );
    }
}

#[test]
fn a_job_cancelled_in_the_queue_did_not_run_elsewhere() {
    let cancelled = job(JobState::Elsewhere, Some("cancelled"));
    assert_eq!(
        cancelled.outcome(),
        Outcome::new("cancelled in queue", OutcomeKind::Ended)
    );
    assert_eq!(
        job(JobState::Elsewhere, Some("skipped")).outcome(),
        Outcome::new("skipped", OutcomeKind::Ended)
    );
    assert!(!cancelled.is_failure(), "a cancelled job did not fail");
    assert_eq!(cancelled.waited(160), None);
}

#[test]
fn a_timed_out_job_failed_and_reads_timed_out() {
    let timed_out = job(JobState::Completed, Some("timed_out"));
    assert_eq!(
        timed_out.outcome(),
        Outcome::new("timed out", OutcomeKind::Failed)
    );
    assert!(timed_out.is_failure(), "a timed-out job counts as failed");
}

#[test]
fn a_job_reads_as_failed_exactly_when_it_counts_as_failed() {
    let conclusions = [
        None,
        Some("success"),
        Some("failure"),
        Some("timed_out"),
        Some("cancelled"),
        Some("skipped"),
        Some("startup_failure"),
        Some("action_required"),
    ];
    let states = [
        JobState::Queued,
        JobState::Running,
        JobState::Completed,
        JobState::Lost,
        JobState::Elsewhere,
    ];
    for state in states {
        for conclusion in conclusions {
            let job = job(state, conclusion);
            assert_eq!(
                job.outcome().kind == OutcomeKind::Failed,
                job.is_failure(),
                "{state:?} {conclusion:?}"
            );
        }
    }
}

#[test]
fn a_waiting_job_has_waited_until_now_and_one_that_ended_unstarted_has_no_wait() {
    assert_eq!(job(JobState::Queued, None).waited(160), Some(60));
    assert_eq!(job(JobState::Elsewhere, None).waited(160), None);
}

#[test]
fn a_job_never_says_more_of_its_wait_passed_while_runnerd_was_down_than_it_waited() {
    let down = |unseen: u64| {
        timed(
            JobState::Completed,
            Timeline {
                queued_at: 100,
                started_at: Some(160),
                unseen,
                ..Timeline::default()
            },
        )
        .unseen_note(200)
    };
    assert_eq!(down(0), None);
    assert_eq!(
        down(45).as_deref(),
        Some("45s of it while runnerd was not running")
    );
    assert_eq!(
        down(3600).as_deref(),
        Some("1m 00s of it while runnerd was not running")
    );
}

#[test]
fn a_final_job_without_a_finish_time_claims_no_run_time() {
    let started = Timeline {
        queued_at: 100,
        started_at: Some(110),
        ..Timeline::default()
    };
    assert_eq!(timed(JobState::Running, started.clone()).ran(170), Some(60));
    assert_eq!(timed(JobState::Elsewhere, started).ran(170), None);
}

#[test]
fn a_runner_starting_shows_before_the_job_starts() {
    let starting = timed(
        JobState::Queued,
        Timeline {
            queued_at: 100,
            container_started_at: Some(120),
            ..Timeline::default()
        },
    );
    let phases: Vec<(PhaseKind, u64, bool)> = starting
        .phases()
        .iter()
        .map(|phase| (phase.kind, phase.seconds(150), phase.to.is_none()))
        .collect();
    assert_eq!(
        phases,
        [
            (PhaseKind::Waiting, 20, false),
            (PhaseKind::Starting, 30, true),
        ]
    );
}

#[test]
fn a_job_that_ended_without_a_runner_stopped_waiting_when_it_ended() {
    let cancelled = timed(
        JobState::Elsewhere,
        Timeline {
            queued_at: 100,
            finished_at: Some(130),
            ..Timeline::default()
        },
    );
    let phases = cancelled.phases();
    assert_eq!(phases.len(), 1, "only the wait: {phases:?}");
    assert_eq!(
        (phases[0].kind, phases[0].seconds(1000)),
        (PhaseKind::Waiting, 30)
    );
}

#[test]
fn a_warm_runners_container_start_is_the_first_milestone() {
    let warm = Timeline {
        queued_at: 100,
        container_started_at: Some(40),
        started_at: Some(101),
        ..Timeline::default()
    };
    let labels: Vec<&str> = warm.milestones().iter().map(|m| m.label).collect();
    assert_eq!(labels, ["Container started", "Queued", "Job started"]);
}

#[test]
fn a_timed_out_step_failed_and_a_cancelled_one_did_not() {
    assert_eq!(
        step("completed", Some("timed_out")).outcome().kind,
        OutcomeKind::Failed
    );
    assert_eq!(
        step("completed", Some("cancelled")).outcome().kind,
        OutcomeKind::Ended
    );
    assert_eq!(
        step("in_progress", None).outcome().kind,
        OutcomeKind::Running
    );
    assert_eq!(step("in_progress", None).took(130), Some(30));
}

#[test]
fn a_running_jobs_peak_rises_with_its_samples_and_survives_a_lagging_read() {
    let mut running = Job {
        summary: job(JobState::Running, None),
        steps: vec![],
        record: None,
        admission: None,
    };
    running.summary.metrics.memory_peak = Some(2 << 30);
    let stored = running.clone();
    running.observe(&sample(200, 3 << 30, 500));
    assert_eq!(running.summary.metrics.memory_peak, Some(3 << 30));
    running.reread(stored.clone());
    assert_eq!(
        running.summary.metrics.memory_peak,
        Some(3 << 30),
        "a read that lags the samples keeps the high-water mark"
    );
    let mut finished = stored;
    finished.summary.state = JobState::Completed;
    running.reread(finished);
    assert_eq!(
        running.summary.metrics.memory_peak,
        Some(2 << 30),
        "a final job's peak is the daemon's total"
    );
}

#[test]
fn a_run_that_failed_since_its_runs_were_read_counts_as_failed() {
    let mut earlier = job(JobState::Completed, Some("success"));
    earlier.id = 6;
    earlier.timeline.started_at = Some(110);
    earlier.timeline.finished_at = Some(170);
    let mut this = Job {
        summary: job(JobState::Running, None),
        steps: vec![],
        record: None,
        admission: None,
    };
    this.summary.timeline.started_at = Some(200);
    let runs = [earlier, this.summary.clone()];
    this.summary.state = JobState::Completed;
    this.summary.conclusion = Some("failure".to_owned());
    this.summary.timeline.finished_at = Some(300);
    let compared = this.compare(&runs, 400);
    assert_eq!((compared.failed, compared.finished), (1, 2));
    assert_eq!(compared.median_ran, Some(60), "the lower of 60 and 100");
}

#[test]
fn runs_another_machine_ran_do_not_set_this_jobs_waits() {
    let ran_here = |id: u64, state: JobState, started: u64| JobSummary {
        id,
        conclusion: Some("success".to_owned()),
        ..timed(
            state,
            Timeline {
                queued_at: 100,
                started_at: Some(started),
                finished_at: Some(started + 60),
                ..Timeline::default()
            },
        )
    };
    let this = Job {
        summary: ran_here(7, JobState::Completed, 130),
        steps: vec![],
        record: None,
        admission: None,
    };
    let runs = [
        ran_here(5, JobState::Elsewhere, 100),
        ran_here(6, JobState::Elsewhere, 101),
        this.summary.clone(),
    ];
    let compared = this.compare(&runs, 400);
    assert_eq!(compared.finished, 1);
    assert_eq!(compared.median_waited, Some(30));
}

#[test]
fn steps_that_meet_at_a_second_share_no_sample() {
    let series = SampleSeries {
        job: 7,
        interval: 2,
        samples: vec![
            sample(100, 1, 1000),
            sample(102, 5, 3000),
            sample(104, 2, 0),
        ],
    };
    let first = series.usage_between(98, 102);
    let second = series.usage_between(102, 104);
    assert_eq!(first.map(|usage| usage.memory_peak), Some(5));
    assert_eq!(second.map(|usage| usage.memory_peak), Some(2));
    assert_eq!(series.usage_between(104, 105), None);
}

#[test]
fn a_job_whose_name_is_not_known_yet_is_named_by_its_id() {
    let mut unnamed = job(JobState::Running, None);
    unnamed.name.clear();
    assert_eq!(unnamed.title().to_string(), "CI / job 7");
    unnamed.workflow = None;
    assert_eq!(unnamed.title().to_string(), "job 7");
}

#[test]
fn what_held_a_jobs_cpu_back_is_told_only_from_figures_the_kernel_kept() {
    let held = |periods, throttled, stalled| {
        JobSummary {
            metrics: JobMetrics {
                cpu_periods: periods,
                cpu_throttled: throttled,
                cpu_stalled: stalled,
                ..JobMetrics::default()
            },
            ..timed(
                JobState::Completed,
                Timeline {
                    queued_at: 100,
                    started_at: Some(100),
                    finished_at: Some(617),
                    ..Timeline::default()
                },
            )
        }
        .cpu_held(700)
    };
    // The Android gate at 9 CPUs: held in about a fifth of its busy periods, and waiting longer than that, as the
    // host's other work adds to the cap's holds.
    assert_eq!(
        held(Some(4_000), Some(720), Some(160_000)).as_deref(),
        Some("held at its CPU cap in 18% of its busy time, waited for a CPU 30% of its run")
    );
    assert_eq!(
        held(Some(4_000), Some(0), Some(0)).as_deref(),
        Some("never held at its CPU cap, never waited for a CPU")
    );
    assert_eq!(
        held(Some(4_000), Some(3), None).as_deref(),
        Some("held at its CPU cap in under 1% of its busy time"),
        "a hold that is not 0 never reads as 0%"
    );
    // No quota's figures, as without the cpu controller: unknown, not never.
    assert_eq!(
        held(None, None, Some(51_700)).as_deref(),
        Some("waited for a CPU 10% of its run")
    );
    assert_eq!(
        held(Some(0), Some(0), None),
        None,
        "no busy period, no share"
    );
    assert_eq!(held(None, None, None), None);
}