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
//! `ApplyConfig`: a duplicate name refused whole, what it records in
//! the registry, and an unregistered app named back in the reply.
use super::*;
/// One app as a Flockfile would declare it: the config, plus the keys
/// the document literally wrote. `declared` is what an apply keys on, so
/// a fixture that left it empty would declare nothing and apply nothing.
fn declared(name: &str, script: &str, keys: &[&str]) -> DeclaredApp {
DeclaredApp {
config: AppConfig::minimal(name, script),
declared: keys.iter().map(|k| (*k).to_string()).collect(),
declared_env: BTreeSet::new(),
}
}
/// `handle_apply_config` reads the override store once for the whole
/// file, so a second entry of the same name merges against the store as
/// the first entry found it and the first entry's record is lost.
/// `normalize_all` refuses a duplicate on the `Start` path and is not on
/// this one.
#[tokio::test(start_paused = true)]
async fn apply_config_refuses_a_request_naming_one_app_twice() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
let reply = reply_of(
dispatch(
envelope(
2,
Request::ApplyConfig {
apps: vec![
declared("web", "./one", &["script"]),
declared("web", "./two", &["script"]),
],
reset: ResetDepth::None,
},
),
&h.ctx,
)
.await,
);
let err = reply.result.unwrap_err();
assert_eq!(err.code, RpcErrorCode::InvalidConfig);
assert!(err.message.contains("web"), "the name is named: {err:?}");
// Refused BEFORE anything was touched, which is the half an error
// code alone does not prove: the flock still runs what `Start`
// registered, not either of the two scripts the request carried.
let flock = list_flock(&h.ctx, 3).await;
let roll = h.ctx.registry.roll(&flock, 0);
assert_eq!(roll.apps[0].app.script, "./srv");
}
/// The `Scale` arm's reasoning, applied to this one: a change that
/// reached the stored spec and not the roll is undone by the next
/// reboot.
#[tokio::test(start_paused = true)]
async fn apply_config_records_what_it_applied_in_the_registry() {
let h = harness(vec![ProcScript::never_exits()]);
start_web(&h).await;
let reply = reply_of(
dispatch(
envelope(
2,
Request::ApplyConfig {
apps: vec![DeclaredApp {
config: {
let mut app = AppConfig::minimal("web", "./srv");
app.max_restarts = 99;
app
},
declared: ["max_restarts"].iter().map(|k| (*k).to_string()).collect(),
declared_env: BTreeSet::new(),
}],
reset: ResetDepth::None,
},
),
&h.ctx,
)
.await,
);
let Response::Applied(report) = reply.result.unwrap() else {
panic!("expected applied")
};
assert_eq!(report.len(), 1);
assert_eq!(report[0].name, "web");
assert_eq!(report[0].applied, vec!["max_restarts".to_string()]);
assert_eq!(report[0].refused, None);
let flock = list_flock(&h.ctx, 3).await;
let roll = h.ctx.registry.roll(&flock, 0);
assert_eq!(roll.apps[0].app.max_restarts, 99);
}
/// One app that cannot be applied must not cost the rest of the file its
/// load, so a miss is a per-app refusal inside an `Ok`, never an `Err`.
#[tokio::test(start_paused = true)]
async fn apply_config_refuses_an_unregistered_app_inside_the_reply() {
let h = harness(vec![]);
let reply = reply_of(
dispatch(
envelope(
1,
Request::ApplyConfig {
apps: vec![declared("ghost", "./srv", &["script"])],
reset: ResetDepth::None,
},
),
&h.ctx,
)
.await,
);
let Response::Applied(report) = reply.result.unwrap() else {
panic!("expected applied")
};
assert_eq!(report.len(), 1);
let refused = report[0].refused.as_deref().unwrap_or_default();
assert!(
refused.contains("ghost"),
"the refusal names the app: {refused}"
);
}