1#[cfg(test)]
20mod tests {
21 use std::fs;
22 use std::os::unix::fs::PermissionsExt;
23 use std::path::{Path, PathBuf};
24 use std::process::Command;
25
26 fn repo_root() -> PathBuf {
27 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
28 .parent()
29 .expect("xtask has a parent")
30 .to_path_buf()
31 }
32
33 fn write_shim(dir: &Path, name: &str, body: &str) {
34 let path = dir.join(name);
35 fs::write(&path, body).expect("write shim");
36 let mut perms = fs::metadata(&path).expect("stat shim").permissions();
37 perms.set_mode(0o755);
38 fs::set_permissions(&path, perms).expect("chmod shim");
39 }
40
41 struct Handoff {
43 prep: String,
44 env: String,
45 }
46
47 fn run_deploy() -> Handoff {
53 use std::sync::atomic::{AtomicUsize, Ordering};
54 static SEQ: AtomicUsize = AtomicUsize::new(0);
55 let unique = format!(
56 "{}-{}",
57 std::process::id(),
58 SEQ.fetch_add(1, Ordering::Relaxed)
59 );
60 let root = repo_root();
61 let script = root.join(".forgejo/scripts/deploy.sh");
62 assert!(
63 script.exists(),
64 "deploy.sh not found at {}",
65 script.display()
66 );
67
68 let tmp = std::env::temp_dir().join(format!("nomisync-deploy-render-{unique}"));
69 let _ = fs::remove_dir_all(&tmp);
70 fs::create_dir_all(&tmp).expect("temp dir");
71 let prep_file = tmp.join("prep.sh");
72 let env_file = tmp.join("env.txt");
73
74 write_shim(
77 &tmp,
78 "ci-image",
79 &format!(
80 "#!/usr/bin/env bash\nprintf '%s' \"$PREP_SCRIPT\" > {}\n\
81 {{ echo \"PROJECT=$PROJECT\"; echo \"TARGET=$TARGET\"; echo \"IMAGE=$IMAGE\"; \
82 echo \"PREP_SECRETS=$PREP_SECRETS\"; echo \"PREP_ENV=$PREP_ENV\"; }} > {}\n\
83 echo 'sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'\n",
84 prep_file.display(),
85 env_file.display()
86 ),
87 );
88 write_shim(
90 &tmp,
91 "git",
92 "#!/usr/bin/env bash\ncase \"$1\" in\n ls-remote) echo \"$SHA refs/heads/master\";;\nesac\nexit 0\n",
93 );
94 write_shim(&tmp, "kubectl", "#!/usr/bin/env bash\nexit 0\n");
96
97 let path = format!(
98 "{}:{}",
99 tmp.display(),
100 std::env::var("PATH").unwrap_or_default()
101 );
102 let _ = Command::new("timeout")
103 .args(["25", "bash", &script.to_string_lossy()])
104 .env("PATH", path)
105 .env("REPO", "rayslava/nomisync")
106 .env("SHA", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
107 .env("GIT_TOKEN", "token")
108 .env("GITHUB_RUN_NUMBER", "1")
109 .env("GITHUB_RUN_ID", "1")
110 .current_dir(&root)
111 .output()
112 .expect("run deploy.sh");
113
114 let prep = fs::read_to_string(&prep_file).unwrap_or_else(|_| {
115 panic!(
116 "deploy.sh never called ci-image — it died before the handoff. \
117 That is what an unterminated heredoc or a bad expansion does."
118 )
119 });
120 let env = fs::read_to_string(&env_file).expect("ci-image env capture");
121 let _ = fs::remove_dir_all(&tmp);
122 Handoff { prep, env }
123 }
124
125 #[test]
126 fn deploy_reaches_ci_image_with_a_prepare_script() {
127 let _processes = crate::test_support::processes();
128 let h = run_deploy();
129 assert!(
130 h.prep.contains("nomisync-manifest.json"),
131 "the prepare script is missing its manifest fetch, so the heredoc was truncated:\n{}",
132 h.prep
133 );
134 assert!(
135 h.env.contains("PROJECT=nomisync")
136 && h.env.contains("IMAGE=registry.do.rayslava.com/nomisync:"),
137 "ci-image was called with the wrong identity:\n{}",
138 h.env
139 );
140 }
141
142 #[test]
143 fn the_quoted_heredoc_does_not_eat_inner_dollars() {
144 let _processes = crate::test_support::processes();
148 let h = run_deploy();
149 assert!(
150 h.prep.contains("print(m$1)"),
151 "$1 was expanded away before ci-image saw it — is the PREP heredoc still quoted?\n{}",
152 h.prep
153 );
154 }
155
156 #[test]
157 fn the_prepare_heredoc_is_quoted() {
158 let source = fs::read_to_string(repo_root().join(".forgejo/scripts/deploy.sh"))
162 .expect("read deploy.sh");
163 assert!(
164 source.contains("PREP_SCRIPT=\"$(cat <<'PREP'"),
165 "the prepare heredoc is no longer quoted; the outer shell will expand and execute its body"
166 );
167 }
168
169 #[test]
170 fn the_prepare_script_is_valid_bash() {
171 let _processes = crate::test_support::processes();
172 let h = run_deploy();
173 let dir = std::env::temp_dir().join(format!(
174 "nomisync-prepare-{}-{:?}",
175 std::process::id(),
176 std::thread::current().id()
177 ));
178 fs::create_dir_all(&dir).expect("temp dir");
179 let path = dir.join("prepare.sh");
180 fs::write(&path, &h.prep).expect("write prepare");
181
182 let out = Command::new("bash")
183 .args(["-n", &path.to_string_lossy()])
184 .output()
185 .expect("bash -n");
186 let _ = fs::remove_dir_all(&dir);
187
188 assert!(
189 out.status.success(),
190 "the prepare script is not valid bash:\n{}",
191 String::from_utf8_lossy(&out.stderr)
192 );
193 }
194
195 #[test]
196 fn the_prepare_script_keeps_its_provenance_checks() {
197 let _processes = crate::test_support::processes();
201 let h = run_deploy();
202 for needle in [
203 "manifest commit",
204 "manifest run",
205 "hash mismatch against the manifest",
206 "sha256sum -c -",
207 ] {
208 assert!(
209 h.prep.contains(needle),
210 "the prepare script lost its check for {needle:?}:\n{}",
211 h.prep
212 );
213 }
214 }
215
216 #[test]
217 fn the_run_id_reaches_the_prepare_container() {
218 let _processes = crate::test_support::processes();
222 let h = run_deploy();
223 assert!(
224 h.env.contains("PREP_ENV=GITHUB_RUN_ID="),
225 "GITHUB_RUN_ID is not being passed to the prepare container:\n{}",
226 h.env
227 );
228 }
229
230 #[test]
231 fn the_prepare_script_keeps_the_s3_keys_off_the_command_line() {
232 let _processes = crate::test_support::processes();
233 let h = run_deploy();
234 let dir = tempfile::tempdir().expect("temp dir");
235 let script = dir.path().join("prepare.sh");
236 fs::write(&script, &h.prep).expect("write prepare");
237 let argv = dir.path().join("argv");
238 let config = dir.path().join("config");
239 let config_path = dir.path().join("config-path");
240 write_shim(
241 dir.path(),
242 "s3cmd",
243 &format!(
244 "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {argv}\n\
245 for a in \"$@\"; do case \"$a\" in --config=*) c=\"${{a#--config=}}\"; \
246 cp \"$c\" {config}; printf '%s' \"$c\" > {path};; esac; done\nexit 1\n",
247 argv = argv.display(),
248 config = config.display(),
249 path = config_path.display()
250 ),
251 );
252 let path = format!(
253 "{}:{}",
254 dir.path().display(),
255 std::env::var("PATH").unwrap_or_default()
256 );
257 let out = Command::new("timeout")
258 .args(["25", "bash", &script.to_string_lossy()])
259 .env("PATH", path)
260 .env("ARTIFACTS_ACCESS_KEY_ID", "access-key-sentinel")
261 .env("ARTIFACTS_SECRET_ACCESS_KEY", "secret-key-sentinel")
262 .env("ARTIFACTS_ENDPOINT", "s3.example.test")
263 .env("ARTIFACTS_BUCKET", "bucket")
264 .env("SHA", "deadbeef")
265 .env("GITHUB_RUN_ID", "1")
266 .output()
267 .expect("run prepare");
268
269 assert!(
270 !out.status.success(),
271 "a failing s3cmd must stop the prepare script"
272 );
273 let argv = fs::read_to_string(&argv).expect("s3cmd was called");
274 assert!(
275 !argv.contains("key-sentinel"),
276 "an S3 key reached s3cmd's argv:\n{argv}"
277 );
278 let config = fs::read_to_string(&config).expect("s3cmd got a --config file");
279 for line in [
280 "access_key = access-key-sentinel",
281 "secret_key = secret-key-sentinel",
282 "host_base = s3.example.test",
283 "host_bucket = %(bucket)s.s3.example.test",
284 ] {
285 assert!(config.contains(line), "config lacks {line:?}:\n{config}");
286 }
287 let used = fs::read_to_string(&config_path).expect("config path recorded");
288 assert!(
289 !Path::new(&used).exists(),
290 "the config file with the keys outlived the script: {used}"
291 );
292 }
293
294 #[test]
295 fn both_deployments_are_pinned_by_digest() {
296 let source = fs::read_to_string(repo_root().join(".forgejo/scripts/deploy.sh"))
299 .expect("read deploy.sh");
300 assert!(
301 source.contains("${SHA}@${DIGEST}"),
302 "the GitOps bump writes a bare tag; it should pin the digest ci-image returned"
303 );
304 assert!(
305 source.contains("MANIFESTS=\"$WEB_MANIFEST $SSHD_MANIFEST\""),
306 "both Deployments must start in the same edit set, or web and sshd can split"
307 );
308 assert!(
309 source.contains("sed -i -E") && source.contains("$MANIFESTS"),
310 "the bump must rewrite that whole set in one sed"
311 );
312 }
313
314 #[test]
315 fn the_fleet_migration_job_is_bumped_with_the_deployments() {
316 let source = fs::read_to_string(repo_root().join(".forgejo/scripts/deploy.sh"))
321 .expect("read deploy.sh");
322 assert!(
323 source.contains("JOB_MANIFEST="),
324 "the fleet migration Job is not part of the GitOps bump"
325 );
326 assert!(
327 source.contains("[ -f \"$JOB_MANIFEST\" ] && MANIFESTS=\"$MANIFESTS $JOB_MANIFEST\""),
328 "the Job must join the edit set when its manifest exists, and be skipped \
329 — not fatal — while it does not, exactly as the Deployments bootstrap"
330 );
331 }
332
333 #[test]
334 fn the_release_artifact_carries_the_fleet_migrator() {
335 let source = fs::read_to_string(repo_root().join(".forgejo/scripts/deploy.sh"))
339 .expect("read deploy.sh");
340 assert!(
341 source.contains("release-fleet-migrate"),
342 "deploy.sh does not assert the migrator binary survived the tarball"
343 );
344
345 let dockerfile =
346 fs::read_to_string(repo_root().join("Dockerfile")).expect("read Dockerfile");
347 assert!(
348 dockerfile.contains("COPY release-fleet-migrate"),
349 "the image does not carry the migrator the Job runs"
350 );
351 assert!(
352 dockerfile.contains("ENTRYPOINT [\"/web\"]"),
353 "the migrator must be a Job command, never the container entrypoint"
354 );
355 }
356}