返回 CodeWhale
control_socket_selection.rs
根目录 / crates / app-server / src / runtime_api / tests / control_socket_selection.rs
1 //! The control endpoint is a pure function of the selected runtime store.
2 //!
3 //! These cover the decision, not the transport: `daemon_socket`'s own tests
4 //! cover the path derivation, and the owner tests cover the socket being
5 //! bound. What is pinned here is which endpoint this `serve` run picks, and
6 //! that it picks the same one however the store was spelled.
7 use super::*;
8 use codewhale_engine::test_support::{EnvVarGuard, lock_test_env};
9
10 fn inputs() -> codewhale_paths::daemon_socket::SocketPathInputs {
11 codewhale_paths::daemon_socket::SocketPathInputs {
12 explicit: None,
13 codewhale_home_override: None,
14 xdg_runtime_dir: None,
15 user_home: Some(PathBuf::from("/cw-test-home/whale")),
16 macos: false,
17 }
18 }
19
20 fn default_socket() -> PathBuf {
21 PathBuf::from("/cw-test-home/whale/.codewhale/run/daemon.sock")
22 }
23
24 fn temp_root(tag: &str) -> PathBuf {
25 let root =
26 codewhale_engine::test_support::test_scratch_dir(&format!("cw-control-socket-{tag}"));
27 std::fs::create_dir_all(&root).expect("fixture root");
28 root
29 }
30
31 fn derived_name(path: &Path) -> String {
32 path.file_name()
33 .and_then(|name| name.to_str())
34 .unwrap_or_default()
35 .to_string()
36 }
37
38 #[test]
39 fn an_explicit_socket_selection_always_wins() -> Result<()> {
40 let resolved = store_selected_control_socket_with(
41 Some(PathBuf::from("/tmp/explicit.sock")),
42 Path::new("/tmp/opened-store"),
43 Some(Path::new("/tmp/opened-store")),
44 &inputs(),
45 )?;
46 assert_eq!(resolved, PathBuf::from("/tmp/explicit.sock"));
47 Ok(())
48 }
49
50 #[test]
51 fn the_default_selection_keeps_the_well_known_socket() -> Result<()> {
52 let resolved =
53 store_selected_control_socket_with(None, Path::new("/tmp/opened-store"), None, &inputs())?;
54 assert_eq!(resolved, default_socket());
55 Ok(())
56 }
57
58 #[test]
59 fn a_selected_store_derives_its_own_sibling_endpoint() -> Result<()> {
60 let root = temp_root("derived");
61 let store = root.join("runtime-store");
62 std::fs::create_dir_all(&store)?;
63 let canonical = store.canonicalize()?;
64
65 let resolved = store_selected_control_socket_with(None, &store, Some(&canonical), &inputs())?;
66 assert_eq!(
67 resolved.parent().unwrap(),
68 default_socket().parent().unwrap()
69 );
70 let name = derived_name(&resolved);
71 assert!(name.starts_with("store-"), "derived endpoint: {name}");
72 assert!(name.ends_with(".sock"), "derived endpoint: {name}");
73
74 // Same store, same endpoint — and a different store cannot share it.
75 let again = store_selected_control_socket_with(None, &store, Some(&canonical), &inputs())?;
76 assert_eq!(resolved, again);
77 let other = root.join("other-store");
78 std::fs::create_dir_all(&other)?;
79 let other_canonical = other.canonicalize()?;
80 let elsewhere =
81 store_selected_control_socket_with(None, &other, Some(&other_canonical), &inputs())?;
82 assert_ne!(resolved, elsewhere);
83 Ok(())
84 }
85
86 #[test]
87 fn a_store_the_override_does_not_name_falls_back_to_the_default_endpoint() -> Result<()> {
88 // The opened store is not the one the override selected (the selection
89 // changed under us, or the store came from the fallback chain). The
90 // default endpoint is the conservative answer: the strict receipt
91 // comparison there still refuses a wrong-store attachment.
92 let resolved = store_selected_control_socket_with(
93 None,
94 Path::new("/tmp/some-other-store"),
95 Some(Path::new("/tmp/the-override-store")),
96 &inputs(),
97 )?;
98 assert_eq!(resolved, default_socket());
99 Ok(())
100 }
101
102 #[cfg(unix)]
103 #[test]
104 fn an_aliased_store_selection_still_derives_its_own_endpoint() -> Result<()> {
105 // The store chain resolves the override as written; the control plane
106 // canonicalizes it. Comparing the two in different forms would let a
107 // symlinked (or relative, or `/var`-shaped on macOS) override keep the
108 // default endpoint, which is exactly the collision this derivation exists
109 // to remove.
110 let root = temp_root("alias");
111 let real = root.join("real");
112 std::fs::create_dir_all(real.join("store"))?;
113 let link = root.join("link");
114 std::os::unix::fs::symlink(&real, &link)?;
115 let aliased_store = link.join("store");
116 let canonical_override = aliased_store.canonicalize()?;
117 assert_ne!(aliased_store, canonical_override, "fixture must alias");
118
119 let resolved = store_selected_control_socket_with(
120 None,
121 &aliased_store,
122 Some(&canonical_override),
123 &inputs(),
124 )?;
125 let name = derived_name(&resolved);
126 assert!(
127 name.starts_with("store-"),
128 "an aliased selection must still derive its own endpoint, got {name}"
129 );
130 Ok(())
131 }
132
133 #[cfg(unix)]
134 #[tokio::test]
135 async fn the_runtime_dir_override_reaches_the_control_endpoint() -> Result<()> {
136 let _env = lock_test_env();
137 // A short root on purpose: unix socket paths have a hard kernel limit, and
138 // a fixture under the (long) macOS temp dir cannot hold a derived name.
139 let root = std::env::var_os("CODEWHALE_TEST_SHORT_TMPDIR")
140 .map_or_else(|| PathBuf::from("/tmp"), PathBuf::from)
141 .join(format!("cw-control-socket-env-{}", std::process::id()));
142 let home = root.join("home");
143 let store = root.join("runtime-store");
144 std::fs::create_dir_all(&home)?;
145 std::fs::create_dir_all(&store)?;
146 let _home = EnvVarGuard::set("CODEWHALE_HOME", &home);
147 let _runtime_dir = EnvVarGuard::set("CODEWHALE_RUNTIME_DIR", &store);
148
149 let resolved = store_selected_control_socket(None, &store).await?;
150 let name = derived_name(&resolved);
151 assert!(
152 name.starts_with("store-"),
153 "a selected store must reach the derived endpoint, got {}",
154 resolved.display()
155 );
156 assert!(
157 resolved.starts_with(codewhale_paths::resolve_root_owned_ancestors(&home)),
158 "the derived endpoint stays beside the well-known one, got {}",
159 resolved.display()
160 );
161 Ok(())
162 }
163
163 lines RUST