olai-stack-runtime 0.0.6

Run trestle environments on Docker Compose: a ComposeEngine over the Docker CLI, status, health and events
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
408
409
410
411
412
//! Running store instances on a [`FakeEngine`]: revisions side by side, credentials kept out
//! of the tree, events and health, and reconciling after a crash.
#![cfg(feature = "testing")]

use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;

use futures::StreamExt;
use olai_stack_runtime::{
    ComposeEngine, EventAction, FakeCall, FakeEngine, Health, InstanceError, Instances, Layer,
    ReconcileOptions, SecretError, SecretProvider, SecretProviders, SecretValue, UserVars,
    instance_project,
};
use olai_stack_state::{Draft, InstanceId, SecretRef, SourceBinding, Store, VarSet, VarValue};
use olai_stack_topology::{EnvManifest, PlanCtx, Selection, port_conflicts};

/// Secrets from a map, as a provider called `vault`.
struct Vault(BTreeMap<&'static str, &'static str>);

impl SecretProvider for Vault {
    fn name(&self) -> &str {
        "vault"
    }

    fn get(&self, key: &str) -> Result<SecretValue, SecretError> {
        self.0
            .get(key)
            .map(|v| SecretValue::new(*v))
            .ok_or_else(|| SecretError::Missing {
                provider: "vault".into(),
                key: key.into(),
            })
    }
}

const PG_SENTINEL: &str = "pgSENTINELvalue0123456789";
const S3_SENTINEL: &str = "s3SENTINELvalue0123456789";

struct Fixture {
    _dir: tempfile::TempDir,
    store: Arc<Store>,
    engine: FakeEngine,
    instances: Instances<FakeEngine>,
    state: std::path::PathBuf,
}

fn fixture() -> Fixture {
    let dir = tempfile::tempdir().unwrap();
    let state = dir.path().to_owned();
    let store = Arc::new(Store::open(&state.join("state.db")).unwrap());
    let engine = FakeEngine::new();
    let instances = instances(&store, &engine, &state);
    Fixture {
        _dir: dir,
        store,
        engine,
        instances,
        state,
    }
}

fn instances(store: &Arc<Store>, engine: &FakeEngine, state: &Path) -> Instances<FakeEngine> {
    Instances::new(Arc::clone(store), engine.clone(), state)
        .with_providers(
            SecretProviders::new().with(Vault([("pg", PG_SENTINEL), ("s3", S3_SENTINEL)].into())),
        )
        .with_source(UserVars::new(
            [("FROM_USER".to_owned(), "yes".to_owned())].into(),
        ))
        .without_port_probe()
}

/// `demo` (meshed) with `dev` at revisions 1 (envoy + rustfs) and 2 (+ postgres, mlflow).
fn two_revisions(f: &Fixture) -> (InstanceId, InstanceId) {
    let ws = f.store.create_workspace("demo", true).unwrap();
    let manifest = |modules: &[&str]| {
        EnvManifest::new(
            Selection::modules(modules.iter().copied()),
            PlanCtx::default(),
        )
    };
    let env = f
        .store
        .create_environment(&ws.id, "dev", Draft::new(manifest(&["envoy", "rustfs"])))
        .unwrap();
    f.store.commit_draft(&env.id).unwrap();
    f.store
        .update_draft(
            &env.id,
            &Draft::new(manifest(&["envoy", "rustfs", "postgres", "mlflow"])),
        )
        .unwrap();
    f.store.commit_draft(&env.id).unwrap();
    let r1 = f.store.create_instance(&env.id, 1).unwrap().id;
    let r2 = f.store.create_instance(&env.id, 2).unwrap().id;
    (r1, r2)
}

fn walk(dir: &Path, out: &mut Vec<(String, String)>) {
    for entry in std::fs::read_dir(dir).unwrap() {
        let path = entry.unwrap().path();
        if path.is_dir() {
            walk(&path, out);
        } else {
            let text = String::from_utf8_lossy(&std::fs::read(&path).unwrap()).into_owned();
            out.push((path.display().to_string(), text));
        }
    }
}

#[tokio::test]
async fn two_revisions_run_side_by_side() {
    let f = fixture();
    let (r1, r2) = two_revisions(&f);
    let a = f.instances.start(&r1).await.unwrap();
    let b = f.instances.start(&r2).await.unwrap();

    assert_eq!(
        f.engine.projects(),
        ["demo_dev_r1".to_owned(), "demo_dev_r2".to_owned()].into()
    );
    let (pa, pb) = (
        f.instances.placement(&r1).unwrap(),
        f.instances.placement(&r2).unwrap(),
    );
    assert_ne!(pa.slot, pb.slot);
    assert_eq!(pa.host_label, "demo-dev-r1");
    assert_eq!(pb.host_label, "demo-dev-r2");
    assert_eq!(pa.mesh_network.as_deref(), Some("trestle-demo-mesh"));
    assert!(port_conflicts([("r1", &a.plan), ("r2", &b.plan)]).is_empty());
    assert_eq!(a.plan.gateway_host_port(), Some(9080));
    assert_eq!(b.plan.gateway_host_port(), Some(10080));

    // Each tree is its own, under the state directory.
    assert!(a.dir.starts_with(f.state.join("instances")));
    let head = std::fs::read_to_string(b.dir.join("compose.yaml")).unwrap();
    assert!(head.contains("name: demo_dev_r2"), "{head}");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt as _;
        let mode = std::fs::metadata(&a.dir).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o700);
    }

    // A second instance of a running revision would share its project.
    let env = f.store.instance(&r1).unwrap().environment;
    let again = f.store.create_instance(&env, 1).unwrap();
    assert!(matches!(
        f.instances.start(&again.id).await,
        Err(InstanceError::RevisionRunning { revision: 1, .. })
    ));

    f.instances.stop(&r1, false).await.unwrap();
    assert_eq!(f.engine.projects(), ["demo_dev_r2".to_owned()].into());
    // The slot stays leased while the instance exists, so its ports don't move.
    assert_eq!(f.instances.placement(&r1).unwrap().slot, pa.slot);
    f.instances.remove(&r2, true).await.unwrap();
    assert!(f.engine.projects().is_empty());
    assert!(!b.dir.exists());
}

#[tokio::test]
async fn a_sentinel_secret_never_reaches_the_tree() {
    let f = fixture();
    let (_, r2) = two_revisions(&f);
    // The Postgres and RustFS credentials come from the vault, through the workspace and
    // the instance's overrides.
    let ws = f.store.workspace("demo").unwrap().unwrap();
    let mut vars = VarSet::new();
    vars.set(
        "LOCAL_POSTGRES_PASSWORD",
        VarValue::Secret(SecretRef {
            provider: "vault".into(),
            key: "pg".into(),
        }),
    );
    f.store.set_workspace_vars(&ws.id, &vars).unwrap();
    let mut overrides = VarSet::new();
    overrides.set(
        "LOCAL_RUSTFS_SECRET_KEY",
        VarValue::Secret(SecretRef {
            provider: "vault".into(),
            key: "s3".into(),
        }),
    );
    f.store.set_overrides(&r2, &overrides).unwrap();

    let started = f.instances.start(&r2).await.unwrap();
    let mut files = Vec::new();
    walk(&started.dir, &mut files);
    assert!(!files.is_empty());
    for (path, text) in &files {
        for sentinel in [PG_SENTINEL, S3_SENTINEL] {
            assert!(!text.contains(sentinel), "{sentinel} in {path}");
        }
    }
    // They reached Compose through its environment instead.
    let env = f
        .engine
        .calls()
        .into_iter()
        .find_map(|c| match c {
            FakeCall::Up { env, .. } => Some(env),
            _ => None,
        })
        .unwrap();
    for sentinel in [PG_SENTINEL, S3_SENTINEL] {
        assert!(
            env.values().any(|v| v.contains(sentinel)),
            "{sentinel} not passed"
        );
    }
    // And the effective environment masks them.
    let effective = f.instances.effective_env(&r2).await.unwrap();
    let masked = effective.masked();
    let pg = &masked["LOCAL_POSTGRES_PASSWORD"];
    assert!(pg.secret);
    assert_eq!(pg.value, olai_stack_runtime::MASK);
    assert_eq!(pg.provenance, Layer::WorkspaceVars);
    assert_eq!(pg.shadowed, [Layer::CatalogDefaults]);
    assert!(!format!("{effective:?}").contains(PG_SENTINEL));
}

#[tokio::test]
async fn an_unknown_secret_provider_fails_without_fallback() {
    let f = fixture();
    let (r1, _) = two_revisions(&f);
    let mut overrides = VarSet::new();
    overrides.set(
        "LOCAL_POSTGRES_PASSWORD",
        VarValue::Secret(SecretRef {
            provider: "keychain".into(),
            key: "pg".into(),
        }),
    );
    f.store.set_overrides(&r1, &overrides).unwrap();
    assert!(matches!(
        f.instances.start(&r1).await,
        Err(InstanceError::Secret(SecretError::UnknownProvider(p))) if p == "keychain"
    ));
    assert!(f.engine.projects().is_empty());
}

#[tokio::test]
async fn sources_overlay_the_store_and_placement_keys_stay_put() {
    let f = fixture();
    let (r1, _) = two_revisions(&f);
    f.store
        .set_sources(&r1, &[SourceBinding::new("user")])
        .unwrap();
    let mut overrides = VarSet::new();
    overrides.set("COMPOSE_PROJECT_NAME", VarValue::Literal("hijacked".into()));
    f.store.set_overrides(&r1, &overrides).unwrap();
    let env = f.instances.effective_env(&r1).await.unwrap();
    let vars = env.vars();
    assert_eq!(vars["FROM_USER"].provenance, Layer::Source("user".into()));
    let project = &vars["COMPOSE_PROJECT_NAME"];
    assert_eq!(project.value.expose(), "demo_dev_r1");
    assert_eq!(project.provenance, Layer::Placement);
    assert_eq!(project.shadowed, [Layer::InstanceOverrides]);

    // A source the instance names but nobody configured is an error.
    f.store
        .set_sources(&r1, &[SourceBinding::new("neon")])
        .unwrap();
    assert!(matches!(
        f.instances.effective_env(&r1).await,
        Err(InstanceError::Source(_))
    ));
}

#[tokio::test]
async fn health_and_events_follow_the_engine() {
    let f = fixture();
    let (r1, _) = two_revisions(&f);
    f.engine.script(
        "demo_dev_r1",
        [("envoy", Health::Healthy), ("rustfs", Health::Starting)],
    );
    let mut events = f.instances.events(&r1).await.unwrap();
    f.instances.start(&r1).await.unwrap();
    assert_eq!(
        f.instances.health(&r1).await.unwrap(),
        [
            ("envoy".to_owned(), Health::Healthy),
            ("rustfs".to_owned(), Health::Starting)
        ]
        .into()
    );
    f.engine
        .set_health("demo_dev_r1", "rustfs", Health::Healthy);
    let mut saw_health = false;
    while let Some(Ok(event)) = events.next().await {
        assert_eq!(event.instance, r1);
        if event.action == EventAction::Health(Health::Healthy) {
            assert_eq!(event.service, "rustfs");
            saw_health = true;
            break;
        }
    }
    assert!(saw_health);
}

#[tokio::test]
async fn a_killed_runtime_reconciles_on_restart() {
    let f = fixture();
    let (r1, r2) = two_revisions(&f);
    f.instances.start(&r1).await.unwrap();
    f.instances.start(&r2).await.unwrap();
    // While no runtime ran: r2's project went away, and someone left an instance-shaped
    // project behind that the store doesn't know.
    f.engine
        .down(
            &f.instances.dir(&r2),
            &olai_stack_runtime::DownOptions::default(),
        )
        .await
        .unwrap();
    f.engine.adopt(&instance_project("demo", "ghost", 1));
    f.engine.adopt("someone-elses-project");
    let ghost = tempfile::tempdir().unwrap();
    std::fs::write(ghost.path().join("compose.yaml"), "name: demo_ghost_r2\n").unwrap();
    f.engine
        .up(ghost.path(), &olai_stack_runtime::UpOptions::default())
        .await
        .unwrap();
    // The store still says r1 runs but forgot it (a crash mid-start before recording).
    f.store
        .set_desired(&r1, olai_stack_state::Desired::Stopped)
        .unwrap();

    // A fresh runtime on the same store and Docker.
    let restarted = instances(&f.store, &f.engine, &f.state);
    let report = restarted
        .reconcile(ReconcileOptions {
            remove_orphans: true,
        })
        .await
        .unwrap();
    assert_eq!(report.adopted, std::slice::from_ref(&r1));
    assert_eq!(report.stale, std::slice::from_ref(&r2));
    assert_eq!(report.orphans, ["demo_ghost_r1", "demo_ghost_r2"]);
    // Only the orphan whose files Compose knows of can be taken down.
    assert_eq!(report.removed, ["demo_ghost_r2"]);
    assert_eq!(
        f.store.instance(&r1).unwrap().desired,
        olai_stack_state::Desired::Running
    );
    assert_eq!(
        f.store.instance(&r2).unwrap().desired,
        olai_stack_state::Desired::Stopped
    );
    assert!(f.engine.projects().contains("someone-elses-project"));
    assert!(!f.engine.projects().contains("demo_ghost_r2"));
}

/// A source called `echo` that hands back the config it was given, as `CONFIG`.
struct Echo;

#[async_trait::async_trait]
impl olai_stack_runtime::Source for Echo {
    fn name(&self) -> &str {
        "echo"
    }

    async fn overlay(
        &self,
        ctx: &olai_stack_runtime::SourceCtx,
    ) -> Result<olai_stack_runtime::Overlay, olai_stack_runtime::SourceError> {
        Ok(olai_stack_runtime::Overlay {
            vars: [("CONFIG".to_owned(), ctx.config.to_string())].into(),
            ..Default::default()
        })
    }
}

#[tokio::test]
async fn a_source_sees_its_binding_config_over_the_workspaces() {
    let f = fixture();
    let instances = instances(&f.store, &f.engine, &f.state).with_source(Echo);
    let (r1, _) = two_revisions(&f);
    let ws = f.store.workspace("demo").unwrap().unwrap();
    f.store
        .set_workspace_source(
            &ws.id,
            "echo",
            Some(serde_json::json!({"project": "p", "branch": "main"})),
        )
        .unwrap();
    f.store
        .set_sources(
            &r1,
            &[SourceBinding::new("echo").with_config(serde_json::json!({"branch": "dev"}))],
        )
        .unwrap();
    let env = instances.effective_env(&r1).await.unwrap();
    assert_eq!(
        env.get("CONFIG").unwrap().expose(),
        r#"{"branch":"dev","project":"p"}"#
    );

    // Without a binding config the workspace's applies as is.
    f.store
        .set_sources(&r1, &[SourceBinding::new("echo")])
        .unwrap();
    let env = instances.effective_env(&r1).await.unwrap();
    assert_eq!(
        env.get("CONFIG").unwrap().expose(),
        r#"{"branch":"main","project":"p"}"#
    );
}