tatara-testing 0.2.208

Test utilities and in-memory server for tatara
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! In-memory store that mirrors the ClusterStore API without requiring Raft.
//!
//! This is the "virtualized tatara" — a complete, functional implementation of
//! the tatara state machine that runs entirely in-memory. It applies the same
//! state transitions as the Raft state machine but without consensus overhead.

use anyhow::Result;
use std::collections::HashMap;
use tokio::sync::RwLock;
use uuid::Uuid;

use tatara_core::cluster::types::{ClusterState, JobVersionEntry, NodeMeta};
use tatara_core::domain::allocation::{Allocation, AllocationState, TaskState};
use tatara_core::domain::event::{Event, EventKind};
use tatara_core::domain::job::{Job, JobSpec, JobStatus};
use tatara_core::domain::release::{Release, ReleaseStatus};

/// In-memory store that provides the same read/write interface as ClusterStore
/// but backed by a simple RwLock instead of Raft consensus.
///
/// State transitions mirror `src/cluster/raft_sm.rs` exactly.
pub struct InMemoryStore {
    state: RwLock<ClusterState>,
}

impl InMemoryStore {
    pub fn new() -> Self {
        Self {
            state: RwLock::new(ClusterState::default()),
        }
    }

    /// Create a store pre-populated with initial state.
    pub fn with_state(state: ClusterState) -> Self {
        Self {
            state: RwLock::new(state),
        }
    }

    // ── Reads ──

    pub async fn get_job(&self, id: &str) -> Option<Job> {
        let state = self.state.read().await;
        state.jobs.get(id).cloned()
    }

    pub async fn list_jobs(&self) -> Vec<Job> {
        let state = self.state.read().await;
        state.jobs.values().cloned().collect()
    }

    pub async fn get_allocation(&self, id: &Uuid) -> Option<Allocation> {
        let state = self.state.read().await;
        state.allocations.get(id).cloned()
    }

    pub async fn list_allocations(&self) -> Vec<Allocation> {
        let state = self.state.read().await;
        state.allocations.values().cloned().collect()
    }

    pub async fn list_allocations_for_job(&self, job_id: &str) -> Vec<Allocation> {
        let state = self.state.read().await;
        state
            .allocations
            .values()
            .filter(|a| a.job_id == job_id)
            .cloned()
            .collect()
    }

    pub async fn list_nodes(&self) -> Vec<NodeMeta> {
        let state = self.state.read().await;
        state.nodes.values().cloned().collect()
    }

    pub async fn get_job_history(&self, job_id: &str) -> Vec<JobVersionEntry> {
        let state = self.state.read().await;
        state.job_history.get(job_id).cloned().unwrap_or_default()
    }

    pub async fn list_events(
        &self,
        kind: Option<&EventKind>,
        since: Option<chrono::DateTime<chrono::Utc>>,
    ) -> Vec<Event> {
        let state = self.state.read().await;
        state
            .events
            .query(kind, since)
            .into_iter()
            .cloned()
            .collect()
    }

    pub async fn list_releases(&self) -> Vec<Release> {
        let state = self.state.read().await;
        state.releases.values().cloned().collect()
    }

    pub async fn get_release(&self, id: &Uuid) -> Option<Release> {
        let state = self.state.read().await;
        state.releases.get(id).cloned()
    }

    // ── Writes (mirrors raft_sm.rs apply logic) ──

    /// Submit a job. Mirrors PutJob command from raft_sm.
    pub async fn put_job(&self, job: Job) -> Result<Job> {
        let mut state = self.state.write().await;

        // Save version history snapshot
        let entry = JobVersionEntry {
            version: job.version,
            spec: JobSpec {
                id: job.id.clone(),
                job_type: job.job_type.clone(),
                groups: job.groups.clone(),
                constraints: job.constraints.clone(),
                meta: job.meta.clone(),
            },
            status: job.status.clone(),
            submitted_at: job.submitted_at,
        };
        state
            .job_history
            .entry(job.id.clone())
            .or_default()
            .push(entry);

        // Emit event
        state.events.push(Event::new(
            EventKind::JobSubmitted,
            serde_json::json!({
                "job_id": job.id,
                "version": job.version,
            }),
        ));

        state.jobs.insert(job.id.clone(), job.clone());
        Ok(job)
    }

    /// Update job status. Mirrors UpdateJobStatus command.
    pub async fn update_job_status(&self, job_id: &str, status: JobStatus) -> Result<Job> {
        let mut state = self.state.write().await;

        let job = state
            .jobs
            .get_mut(job_id)
            .ok_or_else(|| anyhow::anyhow!("Job not found: {}", job_id))?;

        job.status = status.clone();
        job.version += 1;
        let result = job.clone();

        // Emit event
        let kind = match status {
            JobStatus::Dead => EventKind::JobStopped,
            _ => EventKind::JobUpdated,
        };
        state.events.push(Event::new(
            kind,
            serde_json::json!({
                "job_id": job_id,
                "status": format!("{:?}", status),
            }),
        ));

        Ok(result)
    }

    /// Submit an allocation. Mirrors PutAllocation command.
    pub async fn put_allocation(&self, alloc: Allocation) -> Result<Allocation> {
        let mut state = self.state.write().await;

        state.events.push(Event::new(
            EventKind::AllocationPlaced,
            serde_json::json!({
                "alloc_id": alloc.id.to_string(),
                "job_id": alloc.job_id,
                "node_id": alloc.node_id,
            }),
        ));

        state.allocations.insert(alloc.id, alloc.clone());
        Ok(alloc)
    }

    /// Update allocation state. Mirrors UpdateAllocation command.
    pub async fn update_allocation_state(
        &self,
        alloc_id: Uuid,
        new_state: AllocationState,
        task_states: HashMap<String, TaskState>,
    ) -> Result<Allocation> {
        let mut state = self.state.write().await;

        let alloc = state
            .allocations
            .get_mut(&alloc_id)
            .ok_or_else(|| anyhow::anyhow!("Allocation not found: {}", alloc_id))?;

        let kind = match new_state {
            AllocationState::Running => EventKind::AllocationStarted,
            AllocationState::Complete => EventKind::AllocationCompleted,
            AllocationState::Failed => EventKind::AllocationFailed,
            _ => EventKind::AllocationPlaced,
        };

        alloc.state = new_state;
        alloc.task_states = task_states;
        let result = alloc.clone();

        state.events.push(Event::new(
            kind,
            serde_json::json!({
                "alloc_id": alloc_id.to_string(),
            }),
        ));

        Ok(result)
    }

    /// Register a node. Mirrors RegisterNode command.
    pub async fn register_node(&self, meta: NodeMeta) -> Result<()> {
        let mut state = self.state.write().await;

        state.events.push(Event::new(
            EventKind::NodeJoined,
            serde_json::json!({
                "node_id": meta.node_id,
                "hostname": meta.hostname,
            }),
        ));

        state.nodes.insert(meta.node_id, meta);
        Ok(())
    }

    /// Emit a raw event.
    pub async fn emit_event(&self, event: Event) {
        let mut state = self.state.write().await;
        state.events.push(event);
    }

    /// Rollback a job to a previous version. Mirrors RollbackJob command.
    pub async fn rollback_job(&self, job_id: &str, version: u64) -> Result<Job> {
        let mut state = self.state.write().await;

        let history = state
            .job_history
            .get(job_id)
            .ok_or_else(|| anyhow::anyhow!("Job not found: {}", job_id))?;

        let entry = history
            .iter()
            .find(|e| e.version == version)
            .ok_or_else(|| anyhow::anyhow!("Version {} not found for job {}", version, job_id))?
            .clone();

        let job = state
            .jobs
            .get_mut(job_id)
            .ok_or_else(|| anyhow::anyhow!("Job not found: {}", job_id))?;

        job.groups = entry.spec.groups;
        job.constraints = entry.spec.constraints;
        job.meta = entry.spec.meta;
        job.version += 1;
        job.status = JobStatus::Pending;
        let result = job.clone();

        state.events.push(Event::new(
            EventKind::JobUpdated,
            serde_json::json!({
                "job_id": job_id,
                "action": "rollback",
                "target_version": version,
            }),
        ));

        Ok(result)
    }

    /// Create a release. Mirrors PutRelease command.
    pub async fn put_release(&self, release: Release) -> Result<Release> {
        let mut state = self.state.write().await;
        state.releases.insert(release.id, release.clone());
        Ok(release)
    }

    /// Update release status. Mirrors UpdateReleaseStatus command.
    pub async fn update_release_status(
        &self,
        release_id: Uuid,
        status: ReleaseStatus,
    ) -> Result<Release> {
        let mut state = self.state.write().await;

        let release = state
            .releases
            .get_mut(&release_id)
            .ok_or_else(|| anyhow::anyhow!("Release not found: {}", release_id))?;

        release.status = status;
        Ok(release.clone())
    }

    /// Drain a node (set ineligible + emit event). Mirrors DrainNode command.
    pub async fn drain_node(&self, node_id: u64) -> Result<()> {
        let mut state = self.state.write().await;

        if let Some(node) = state.nodes.get_mut(&node_id) {
            node.eligible = false;
            state.events.push(Event::new(
                EventKind::NodeDraining,
                serde_json::json!({ "node_id": node_id }),
            ));
            Ok(())
        } else {
            anyhow::bail!("Node not found: {}", node_id)
        }
    }

    /// Set node eligibility. Mirrors SetNodeEligibility command.
    pub async fn set_node_eligibility(&self, node_id: u64, eligible: bool) -> Result<()> {
        let mut state = self.state.write().await;

        if let Some(node) = state.nodes.get_mut(&node_id) {
            node.eligible = eligible;
            Ok(())
        } else {
            anyhow::bail!("Node not found: {}", node_id)
        }
    }

    /// Direct access to the state for assertions in tests.
    pub async fn state(&self) -> tokio::sync::RwLockReadGuard<'_, ClusterState> {
        self.state.read().await
    }
}

impl Default for InMemoryStore {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixtures;
    use tatara_core::domain::job::{JobType, Resources, Task, TaskConfig, TaskGroup};

    #[tokio::test]
    async fn test_put_and_get_job() {
        let store = InMemoryStore::new();
        let job = fixtures::job("test-job");

        store.put_job(job.clone()).await.unwrap();

        let retrieved = store.get_job("test-job").await;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().id, "test-job");
    }

    #[tokio::test]
    async fn test_job_version_history() {
        let store = InMemoryStore::new();
        let job = fixtures::job("versioned-job");

        store.put_job(job).await.unwrap();
        store
            .update_job_status("versioned-job", JobStatus::Running)
            .await
            .unwrap();

        let history = store.get_job_history("versioned-job").await;
        assert_eq!(history.len(), 1); // Initial submission
        assert_eq!(history[0].version, 1);
    }

    #[tokio::test]
    async fn test_events_emitted_on_job_submit() {
        let store = InMemoryStore::new();
        let job = fixtures::job("event-job");

        store.put_job(job).await.unwrap();

        let events = store
            .list_events(Some(&EventKind::JobSubmitted), None)
            .await;
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].payload["job_id"], "event-job");
    }

    #[tokio::test]
    async fn test_rollback_job() {
        let store = InMemoryStore::new();
        let job = fixtures::job_with_group("rollback-test", "web", 3, 500, 256);

        store.put_job(job).await.unwrap();
        store
            .update_job_status("rollback-test", JobStatus::Running)
            .await
            .unwrap();

        // Rollback to version 1
        let rolled_back = store.rollback_job("rollback-test", 1).await.unwrap();
        assert_eq!(rolled_back.status, JobStatus::Pending);
        assert_eq!(rolled_back.groups[0].count, 3);
    }

    #[tokio::test]
    async fn test_release_lifecycle() {
        let store = InMemoryStore::new();

        let mut release = Release::new(
            "myapp".to_string(),
            "github:user/myapp".to_string(),
            "job-1".to_string(),
        );
        release.status = ReleaseStatus::Active;

        let created = store.put_release(release).await.unwrap();
        assert_eq!(created.status, ReleaseStatus::Active);

        let updated = store
            .update_release_status(created.id, ReleaseStatus::Superseded)
            .await
            .unwrap();
        assert_eq!(updated.status, ReleaseStatus::Superseded);
    }

    #[tokio::test]
    async fn test_node_drain() {
        let store = InMemoryStore::new();
        let node = fixtures::node_meta(1, "test-node", 4000, 8192);

        store.register_node(node).await.unwrap();
        store.drain_node(1).await.unwrap();

        let nodes = store.list_nodes().await;
        assert!(!nodes[0].eligible);

        let events = store
            .list_events(Some(&EventKind::NodeDraining), None)
            .await;
        assert_eq!(events.len(), 1);
    }
}