arcbox-docker 0.6.3

Docker REST API compatibility layer for ArcBox
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use super::*;
use tempfile::tempdir;

#[test]
fn test_context_hash() {
    // Docker uses SHA256 of the context name.
    let hash = DockerContextManager::context_hash("arcbox");
    assert_eq!(hash.len(), 64); // SHA256 produces 64 hex chars
    assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
}

#[test]
fn test_create_and_remove_context() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Initially no context.
    assert!(!manager.context_exists());

    // Create context.
    manager.create_context().unwrap();
    assert!(manager.context_exists());

    // Verify meta.json content.
    let meta_path = manager.context_meta_path();
    let meta_content = fs::read_to_string(&meta_path).unwrap();
    let meta: ContextMeta = serde_json::from_str(&meta_content).unwrap();
    assert_eq!(meta.name, "arcbox");
    assert!(meta.endpoints.docker.host.starts_with("unix://"));

    // Remove context.
    manager.remove_context().unwrap();
    assert!(!manager.context_exists());
}

#[test]
fn test_set_and_restore_default() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Create context first.
    manager.create_context().unwrap();

    // Set up an existing default context.
    let config = DockerConfig {
        current_context: Some("desktop-linux".to_string()),
        ..DockerConfig::default()
    };
    manager.write_docker_config(&config).unwrap();

    // Set arcbox as default.
    manager.set_default().unwrap();
    assert!(manager.is_default().unwrap());
    assert_eq!(
        manager.current_context().unwrap(),
        Some("arcbox".to_string())
    );

    // Restore previous default.
    manager.restore_default().unwrap();
    assert!(!manager.is_default().unwrap());
    assert_eq!(
        manager.current_context().unwrap(),
        Some("desktop-linux".to_string())
    );
}

#[test]
fn test_enable_disable() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Enable creates context and sets default.
    manager.enable().unwrap();
    assert!(manager.context_exists());
    assert!(manager.is_default().unwrap());

    // Disable restores previous default.
    manager.disable().unwrap();
    assert!(manager.context_exists()); // Context still exists
    assert!(!manager.is_default().unwrap()); // But not default
}

#[test]
fn test_status() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path.clone(), docker_config_dir);

    let status = manager.status();
    assert!(!status.context_exists);
    assert!(!status.is_default);
    assert_eq!(status.socket_path, socket_path);
    assert!(!status.socket_exists);

    // Create socket file and context.
    fs::write(&socket_path, "").unwrap();
    manager.enable().unwrap();

    let status = manager.status();
    assert!(status.context_exists);
    assert!(status.is_default);
    assert!(status.socket_exists);
}

#[test]
fn test_preserves_other_config_fields() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");
    fs::create_dir_all(&docker_config_dir).unwrap();

    // Create a config with extra fields.
    let config_path = docker_config_dir.join("config.json");
    let initial_config = r#"{
        "currentContext": "desktop-linux",
        "credsStore": "osxkeychain",
        "auths": {
            "https://index.docker.io/v1/": {}
        }
    }"#;
    fs::write(&config_path, initial_config).unwrap();

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);
    manager.create_context().unwrap();
    manager.set_default().unwrap();

    // Read back and verify other fields are preserved.
    let updated_config = fs::read_to_string(&config_path).unwrap();
    let config: serde_json::Value = serde_json::from_str(&updated_config).unwrap();

    assert_eq!(config["currentContext"], "arcbox");
    assert_eq!(config["credsStore"], "osxkeychain");
    assert!(config["auths"].is_object());
}

#[test]
fn test_create_context_is_idempotent() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Create context twice - should not fail.
    manager.create_context().unwrap();
    let first_meta = fs::read_to_string(manager.context_meta_path()).unwrap();

    manager.create_context().unwrap();
    let second_meta = fs::read_to_string(manager.context_meta_path()).unwrap();

    // Content should be identical.
    assert_eq!(first_meta, second_meta);
}

#[test]
fn test_remove_default_context_restores_previous() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Set up: create context with a previous default.
    let config = DockerConfig {
        current_context: Some("orbstack".to_string()),
        ..DockerConfig::default()
    };
    manager.create_context().unwrap();
    manager.write_docker_config(&config).unwrap();
    manager.set_default().unwrap();

    assert!(manager.is_default().unwrap());

    // Remove context - should restore orbstack as default.
    manager.remove_context().unwrap();

    assert!(!manager.context_exists());
    assert_eq!(
        manager.current_context().unwrap(),
        Some("orbstack".to_string())
    );
}

#[test]
fn test_set_default_without_previous_context() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // No config.json exists - fresh install scenario.
    manager.create_context().unwrap();
    manager.set_default().unwrap();

    assert!(manager.is_default().unwrap());

    // Restore should clear the context (no previous).
    manager.restore_default().unwrap();
    assert!(manager.current_context().unwrap().is_none());
}

#[test]
fn test_multiple_enable_disable_cycles() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Set up initial context.
    let config = DockerConfig {
        current_context: Some("default".to_string()),
        ..DockerConfig::default()
    };
    fs::create_dir_all(manager.docker_config_dir()).unwrap();
    manager.write_docker_config(&config).unwrap();

    // Cycle 1.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());
    manager.disable().unwrap();
    assert_eq!(
        manager.current_context().unwrap(),
        Some("default".to_string())
    );

    // Cycle 2.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());
    manager.disable().unwrap();
    assert_eq!(
        manager.current_context().unwrap(),
        Some("default".to_string())
    );

    // Cycle 3.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());
    manager.disable().unwrap();
    assert_eq!(
        manager.current_context().unwrap(),
        Some("default".to_string())
    );
}

#[test]
fn test_set_default_fails_without_context() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Try to set default without creating context first.
    let result = manager.set_default();
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("does not exist"));
}

#[test]
fn test_disable_when_not_default_is_noop() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Create context but don't set as default.
    manager.create_context().unwrap();
    assert!(!manager.is_default().unwrap());

    // Disable should succeed but do nothing.
    manager.disable().unwrap();
    assert!(!manager.is_default().unwrap());
}

#[test]
fn test_remove_nonexistent_context_is_noop() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Remove context that doesn't exist - should not fail.
    manager.remove_context().unwrap();
    assert!(!manager.context_exists());
}

#[test]
fn test_handles_empty_config_json() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");
    fs::create_dir_all(&docker_config_dir).unwrap();

    // Create empty config.json.
    let config_path = docker_config_dir.join("config.json");
    fs::write(&config_path, "{}").unwrap();

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Should handle empty config gracefully.
    assert!(manager.current_context().unwrap().is_none());
    assert!(!manager.is_default().unwrap());

    // Enable should work.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());
}

#[test]
fn test_context_meta_json_format() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("test.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);
    manager.create_context().unwrap();

    // Read and verify the meta.json structure matches Docker's format.
    let meta_content = fs::read_to_string(manager.context_meta_path()).unwrap();
    let meta: serde_json::Value = serde_json::from_str(&meta_content).unwrap();

    // Docker expects PascalCase keys.
    assert!(meta.get("Name").is_some());
    assert!(meta.get("Metadata").is_some());
    assert!(meta.get("Endpoints").is_some());

    // Verify nested structure.
    let endpoints = meta.get("Endpoints").unwrap();
    let docker_endpoint = endpoints.get("docker").unwrap();
    assert!(docker_endpoint.get("Host").is_some());
    assert!(docker_endpoint.get("SkipTLSVerify").is_some());

    // Verify Host format.
    let host = docker_endpoint.get("Host").unwrap().as_str().unwrap();
    assert!(host.starts_with("unix://"));
    assert!(host.contains("test.sock"));
}

#[test]
fn test_context_hash_is_deterministic() {
    // Same context name should always produce the same hash.
    let hash1 = DockerContextManager::context_hash("arcbox");
    let hash2 = DockerContextManager::context_hash("arcbox");
    let hash3 = DockerContextManager::context_hash("arcbox");

    assert_eq!(hash1, hash2);
    assert_eq!(hash2, hash3);

    // Different names produce different hashes.
    let other_hash = DockerContextManager::context_hash("other-context");
    assert_ne!(hash1, other_hash);
}

#[test]
fn test_context_directory_structure() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir.clone());
    manager.create_context().unwrap();

    // Verify Docker-compatible directory structure.
    let contexts_base = docker_config_dir.join("contexts");
    let meta_dir = contexts_base.join("meta");

    assert!(contexts_base.exists());
    assert!(meta_dir.exists());

    // Context should be in a hash-named directory.
    let hash = DockerContextManager::context_hash(ARCBOX_CONTEXT_NAME);
    let hashed_dir = meta_dir.join(&hash);
    assert!(hashed_dir.exists());

    // meta.json should exist in the context directory.
    let meta_path = hashed_dir.join("meta.json");
    assert!(meta_path.exists());
}

#[test]
fn test_full_lifecycle() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path.clone(), docker_config_dir);

    // 1. Initial state - nothing exists.
    assert!(!manager.context_exists());
    let status = manager.status();
    assert!(!status.context_exists);
    assert!(!status.is_default);

    // 2. Enable integration.
    manager.enable().unwrap();
    assert!(manager.context_exists());
    assert!(manager.is_default().unwrap());

    // 3. Create socket file (simulating daemon running).
    fs::write(&socket_path, "").unwrap();
    let status = manager.status();
    assert!(status.socket_exists);

    // 4. Disable integration.
    manager.disable().unwrap();
    assert!(manager.context_exists()); // Context still exists.
    assert!(!manager.is_default().unwrap());

    // 5. Re-enable.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());

    // 6. Remove completely.
    manager.remove_context().unwrap();
    assert!(!manager.context_exists());
    assert!(!manager.is_default().unwrap());
}

#[test]
fn test_switching_from_orbstack() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");
    fs::create_dir_all(&docker_config_dir).unwrap();

    // Simulate existing OrbStack setup.
    let config_path = docker_config_dir.join("config.json");
    let orbstack_config = r#"{
        "currentContext": "orbstack",
        "credsStore": "osxkeychain",
        "auths": {},
        "plugins": {
            "debug": {"hooks": "exec"}
        }
    }"#;
    fs::write(&config_path, orbstack_config).unwrap();

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Enable ArcBox.
    manager.enable().unwrap();
    assert!(manager.is_default().unwrap());

    // Verify OrbStack config is preserved.
    let updated_config = fs::read_to_string(&config_path).unwrap();
    let config: serde_json::Value = serde_json::from_str(&updated_config).unwrap();
    assert_eq!(config["currentContext"], "arcbox");
    assert_eq!(config["credsStore"], "osxkeychain");
    assert!(config["plugins"].is_object());

    // Disable - should restore OrbStack.
    manager.disable().unwrap();
    let restored_config = fs::read_to_string(&config_path).unwrap();
    let config: serde_json::Value = serde_json::from_str(&restored_config).unwrap();
    assert_eq!(config["currentContext"], "orbstack");
}

#[test]
fn test_socket_path_with_spaces() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("path with spaces/docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path.clone(), docker_config_dir);
    manager.create_context().unwrap();

    // Verify the socket path is correctly stored.
    let meta_content = fs::read_to_string(manager.context_meta_path()).unwrap();
    let meta: ContextMeta = serde_json::from_str(&meta_content).unwrap();

    assert!(meta.endpoints.docker.host.contains("path with spaces"));
    assert_eq!(
        meta.endpoints.docker.host,
        format!("unix://{}", socket_path.display())
    );
}

#[test]
fn test_context_status_display() {
    let temp_dir = tempdir().unwrap();
    let socket_path = temp_dir.path().join("docker.sock");
    let docker_config_dir = temp_dir.path().join(".docker");

    let manager = DockerContextManager::with_config_dir(socket_path, docker_config_dir);

    // Get status and verify Display impl.
    let status = manager.status();
    let display = format!("{status}");

    assert!(display.contains("Context exists:"));
    assert!(display.contains("Is default:"));
    assert!(display.contains("Socket path:"));
    assert!(display.contains("Socket exists:"));
}