bssh 2.1.2

Parallel SSH command execution tool for cluster management
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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Configuration tests.

use std::path::{Path, PathBuf};

use super::types::{Config, InteractiveMode, NodeConfig};
use super::utils::{expand_env_vars, expand_tilde};
use crate::test_helpers::EnvGuard;
use serial_test::serial;

#[test]
#[serial]
fn test_expand_env_vars() {
    let _test_var = EnvGuard::set("TEST_VAR", "test_value");
    let _test_user = EnvGuard::set("TEST_USER", "testuser");

    // Test ${VAR} syntax
    assert_eq!(expand_env_vars("Hello ${TEST_VAR}!"), "Hello test_value!");
    assert_eq!(expand_env_vars("${TEST_USER}@host"), "testuser@host");

    // Test $VAR syntax
    assert_eq!(expand_env_vars("Hello $TEST_VAR!"), "Hello test_value!");
    assert_eq!(expand_env_vars("$TEST_USER@host"), "testuser@host");

    // Test mixed
    assert_eq!(
        expand_env_vars("${TEST_USER}:$TEST_VAR"),
        "testuser:test_value"
    );

    // Test non-existent variable (should leave as-is)
    assert_eq!(expand_env_vars("${NONEXISTENT}"), "${NONEXISTENT}");
    assert_eq!(expand_env_vars("$NONEXISTENT"), "$NONEXISTENT");

    // Test no variables
    assert_eq!(expand_env_vars("no variables here"), "no variables here");
}

#[test]
#[serial]
fn test_expand_tilde() {
    let _home = EnvGuard::set("HOME", "/home/user");

    let path = Path::new("~/.ssh/config");
    let expanded = expand_tilde(path);

    assert_eq!(expanded, PathBuf::from("/home/user/.ssh/config"));
}

#[test]
fn test_config_parsing() {
    let yaml = r#"
defaults:
  user: admin
  port: 22
  ssh_key: ~/.ssh/id_rsa

interactive:
  default_mode: multiplex
  prompt_format: "[{node}] $ "
  history_file: ~/.bssh_history
  show_timestamps: true
  colors:
    node1: red
    node2: blue
  keybindings:
    switch_node: "Ctrl+T"
    broadcast_toggle: "Ctrl+A"

clusters:
  production:
    nodes:
      - web1.example.com
      - web2.example.com:2222
      - user@web3.example.com
    ssh_key: ~/.ssh/prod_key
    interactive:
      default_mode: single_node
      prompt_format: "prod> "

  staging:
    nodes:
      - host: staging1.example.com
        port: 2200
        user: deploy
      - staging2.example.com
    user: staging_user
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();
    assert_eq!(config.defaults.user, Some("admin".to_string()));
    assert_eq!(config.clusters.len(), 2);

    // Test global interactive config
    assert!(matches!(
        config.interactive.default_mode,
        InteractiveMode::Multiplex
    ));
    assert_eq!(config.interactive.prompt_format, "[{node}] $ ");
    assert_eq!(
        config.interactive.history_file,
        Some("~/.bssh_history".to_string())
    );
    assert!(config.interactive.show_timestamps);
    assert_eq!(
        config.interactive.colors.get("node1"),
        Some(&"red".to_string())
    );
    assert_eq!(config.interactive.keybindings.switch_node, "Ctrl+T");

    let prod_cluster = config.get_cluster("production").unwrap();
    assert_eq!(prod_cluster.nodes.len(), 3);
    assert_eq!(
        prod_cluster.defaults.ssh_key,
        Some("~/.ssh/prod_key".to_string())
    );

    // Test cluster-specific interactive config
    let prod_interactive = prod_cluster.interactive.as_ref().unwrap();
    assert!(matches!(
        prod_interactive.default_mode,
        InteractiveMode::SingleNode
    ));
    assert_eq!(prod_interactive.prompt_format, "prod> ");
}

#[test]
fn test_interactive_config_fallback() {
    let yaml = r#"
interactive:
  default_mode: multiplex
  prompt_format: "global> "
  show_timestamps: true

clusters:
  with_override:
    nodes:
      - host1
    interactive:
      default_mode: multiplex
      prompt_format: "override> "

  without_override:
    nodes:
      - host2
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Test cluster with override - merged config
    let with_override = config.get_interactive_config(Some("with_override"));
    assert_eq!(with_override.prompt_format, "override> ");
    assert!(matches!(
        with_override.default_mode,
        InteractiveMode::Multiplex
    ));
    // Note: show_timestamps uses cluster value (default false) since we can't tell if it was explicitly set

    // Test cluster without override (falls back to global)
    let without_override = config.get_interactive_config(Some("without_override"));
    assert_eq!(without_override.prompt_format, "global> ");
    assert!(matches!(
        without_override.default_mode,
        InteractiveMode::Multiplex
    ));
    assert!(without_override.show_timestamps);

    // Test global config when no cluster specified
    let global = config.get_interactive_config(None);
    assert_eq!(global.prompt_format, "global> ");
    assert!(matches!(global.default_mode, InteractiveMode::Multiplex));
}

#[test]
#[serial]
fn test_backendai_env_parsing() {
    // Set up Backend.AI environment variables; guards restore prior values on drop.
    let _hosts = EnvGuard::set("BACKENDAI_CLUSTER_HOSTS", "sub1,main1");
    let _host = EnvGuard::set("BACKENDAI_CLUSTER_HOST", "main1");
    let _role = EnvGuard::set("BACKENDAI_CLUSTER_ROLE", "main");
    let _user = EnvGuard::set("USER", "testuser");

    let cluster = Config::from_backendai_env().unwrap();

    // Should have 2 nodes when role is "main"
    assert_eq!(cluster.nodes.len(), 2);

    // Check first node (should include port 2200)
    match &cluster.nodes[0] {
        NodeConfig::Simple(host) => {
            assert_eq!(host, "testuser@sub1:2200");
        }
        _ => panic!("Expected Simple node config"),
    }

    // Test with sub role - should skip the first (main) node.
    // The new guard shadows the outer `_role` binding; when it drops at the end
    // of this test it'll restore to "main" (the value saved by `_role`), and
    // then the outer `_role` drops and restores to the pre-test value.
    let _role_sub = EnvGuard::set("BACKENDAI_CLUSTER_ROLE", "sub");
    let cluster = Config::from_backendai_env().unwrap();
    assert_eq!(cluster.nodes.len(), 1);

    match &cluster.nodes[0] {
        NodeConfig::Simple(host) => {
            assert_eq!(host, "testuser@main1:2200");
        }
        _ => panic!("Expected Simple node config"),
    }
}

#[test]
fn test_jump_host_global_default() {
    let yaml = r#"
defaults:
  user: admin
  jump_host: bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
      - host: prod2.internal
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Both nodes should inherit global jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("bastion.example.com".to_string())
    );
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("bastion.example.com".to_string())
    );

    // get_cluster_jump_host should also return global default
    assert_eq!(
        config.get_cluster_jump_host(Some("production")),
        Some("bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_cluster_override() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
      - host: prod2.internal
    jump_host: prod-bastion.example.com

  staging:
    nodes:
      - host: staging1.internal
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // production cluster nodes should use cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion.example.com".to_string())
    );
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("prod-bastion.example.com".to_string())
    );

    // staging cluster should fall back to global default
    assert_eq!(
        config.get_jump_host("staging", 0),
        Some("global-bastion.example.com".to_string())
    );

    // get_cluster_jump_host should return cluster-level jump_host
    assert_eq!(
        config.get_cluster_jump_host(Some("production")),
        Some("prod-bastion.example.com".to_string())
    );
    assert_eq!(
        config.get_cluster_jump_host(Some("staging")),
        Some("global-bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_node_override() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
        jump_host: prod1-bastion.example.com
      - host: prod2.internal
      - host: prod3.internal
        jump_host: prod3-bastion:2222
    jump_host: prod-bastion.example.com
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // prod1 should use node-level jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod1-bastion.example.com".to_string())
    );

    // prod2 should use cluster-level jump_host (no node override)
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("prod-bastion.example.com".to_string())
    );

    // prod3 should use node-level jump_host with custom port
    assert_eq!(
        config.get_jump_host("production", 2),
        Some("prod3-bastion:2222".to_string())
    );
}

#[test]
fn test_jump_host_explicit_disable() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
      - host: prod2.internal
        jump_host: ""
    jump_host: prod-bastion.example.com

  direct_access:
    nodes:
      - host: direct1.example.com
    jump_host: ""
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // prod1 should use cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion.example.com".to_string())
    );

    // prod2 should have no jump_host (explicitly disabled with empty string)
    assert_eq!(config.get_jump_host("production", 1), None);

    // direct_access cluster disables jump_host at cluster level
    assert_eq!(config.get_jump_host("direct_access", 0), None);

    // get_cluster_jump_host should return None for explicitly disabled
    assert_eq!(config.get_cluster_jump_host(Some("direct_access")), None);
}

#[test]
fn test_jump_host_no_config() {
    let yaml = r#"
defaults:
  user: admin

clusters:
  production:
    nodes:
      - host: prod1.internal
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // No jump_host configured anywhere
    assert_eq!(config.get_jump_host("production", 0), None);
    assert_eq!(config.get_cluster_jump_host(Some("production")), None);
    assert_eq!(config.get_cluster_jump_host(None), None);
}

#[test]
fn test_jump_host_nonexistent_cluster() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Nonexistent cluster should return global default from get_cluster_jump_host
    assert_eq!(
        config.get_cluster_jump_host(Some("nonexistent")),
        Some("global-bastion.example.com".to_string())
    );

    // get_jump_host for nonexistent cluster returns global default
    assert_eq!(
        config.get_jump_host("nonexistent", 0),
        Some("global-bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_simple_node_config() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - simple-node.internal
    jump_host: prod-bastion.example.com
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Simple node (string) should inherit cluster jump_host
    // Simple nodes cannot have node-level jump_host override
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion.example.com".to_string())
    );
}

#[test]
#[serial]
fn test_jump_host_env_var_expansion() {
    // Set up test environment variables; guards restore prior values on drop.
    let _host = EnvGuard::set("TEST_BASTION_HOST", "bastion.example.com");
    let _port = EnvGuard::set("TEST_BASTION_PORT", "2222");

    let yaml = r#"
defaults:
  jump_host: ${TEST_BASTION_HOST}

clusters:
  production:
    nodes:
      - host: prod1.internal
        jump_host: $TEST_BASTION_HOST:$TEST_BASTION_PORT
      - host: prod2.internal
    jump_host: ${TEST_BASTION_HOST}:${TEST_BASTION_PORT}
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Node-level with $VAR syntax
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("bastion.example.com:2222".to_string())
    );

    // Cluster-level with ${VAR} syntax
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("bastion.example.com:2222".to_string())
    );

    // Global default with ${VAR} syntax
    assert_eq!(
        config.get_cluster_jump_host(Some("staging")),
        Some("bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_out_of_bounds_node_index() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
    jump_host: prod-bastion.example.com
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Node at index 0 exists - should use cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion.example.com".to_string())
    );

    // Node at index 1 does not exist - should fall back to cluster level
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("prod-bastion.example.com".to_string())
    );

    // Node at index 100 does not exist - should fall back to cluster level
    assert_eq!(
        config.get_jump_host("production", 100),
        Some("prod-bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_mixed_simple_detailed_nodes() {
    let yaml = r#"
defaults:
  jump_host: global-bastion.example.com

clusters:
  production:
    nodes:
      - simple-node1.internal
      - host: detailed-node1.internal
        jump_host: special-bastion.example.com
      - simple-node2.internal
      - host: detailed-node2.internal
    jump_host: prod-bastion.example.com
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Simple node at index 0 - inherits cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion.example.com".to_string())
    );

    // Detailed node at index 1 - uses node-level jump_host
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("special-bastion.example.com".to_string())
    );

    // Simple node at index 2 - inherits cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 2),
        Some("prod-bastion.example.com".to_string())
    );

    // Detailed node at index 3 without jump_host - inherits cluster jump_host
    assert_eq!(
        config.get_jump_host("production", 3),
        Some("prod-bastion.example.com".to_string())
    );
}

#[test]
fn test_jump_host_with_port_format() {
    let yaml = r#"
defaults:
  jump_host: bastion.example.com:2222

clusters:
  production:
    nodes:
      - host: prod1.internal
        jump_host: prod-bastion:3333
      - host: prod2.internal
    jump_host: cluster-bastion:4444
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Node with port in jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("prod-bastion:3333".to_string())
    );

    // Cluster with port in jump_host
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("cluster-bastion:4444".to_string())
    );

    // Global default with port
    assert_eq!(
        config.get_cluster_jump_host(Some("staging")),
        Some("bastion.example.com:2222".to_string())
    );
}

#[test]
fn test_jump_host_multi_hop_format() {
    let yaml = r#"
defaults:
  jump_host: hop1.example.com,hop2.example.com

clusters:
  production:
    nodes:
      - host: prod1.internal
        jump_host: jumpA,jumpB,jumpC
      - host: prod2.internal
    jump_host: bastion1,bastion2
"#;

    let config: Config = serde_yaml::from_str(yaml).unwrap();

    // Node with multi-hop jump_host
    assert_eq!(
        config.get_jump_host("production", 0),
        Some("jumpA,jumpB,jumpC".to_string())
    );

    // Cluster with multi-hop jump_host
    assert_eq!(
        config.get_jump_host("production", 1),
        Some("bastion1,bastion2".to_string())
    );

    // Global default with multi-hop
    assert_eq!(
        config.get_cluster_jump_host(None),
        Some("hop1.example.com,hop2.example.com".to_string())
    );
}