cfgd-csi 0.4.0

CSI Node plugin for cfgd module injection into Kubernetes pods
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
use super::*;

fn test_cache(dir: &std::path::Path) -> Arc<Cache> {
    Arc::new(Cache::new(dir.to_path_buf(), 1024 * 1024).unwrap())
}

fn test_node(cache: Arc<Cache>) -> CfgdNode {
    let mut registry = prometheus_client::registry::Registry::default();
    let metrics = Arc::new(CsiMetrics::new(&mut registry));
    CfgdNode::new(cache, metrics, "test-node".to_string())
}

#[tokio::test]
async fn node_get_capabilities_returns_expected() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let resp = node
        .node_get_capabilities(Request::new(NodeGetCapabilitiesRequest {}))
        .await
        .unwrap();
    let caps = resp.into_inner().capabilities;
    assert_eq!(caps.len(), 2);

    let rpc_types: Vec<i32> = caps
        .iter()
        .filter_map(|c| match &c.r#type {
            Some(node_service_capability::Type::Rpc(rpc)) => Some(rpc.r#type),
            _ => None,
        })
        .collect();
    assert!(rpc_types.contains(&(node_service_capability::rpc::Type::StageUnstageVolume as i32)));
    assert!(rpc_types.contains(&(node_service_capability::rpc::Type::GetVolumeStats as i32)));
}

#[tokio::test]
async fn node_get_info_returns_node_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let resp = node
        .node_get_info(Request::new(NodeGetInfoRequest {}))
        .await
        .unwrap();
    assert_eq!(resp.into_inner().node_id, "test-node");
}

#[tokio::test]
async fn node_publish_volume_missing_module_attr() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodePublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: "/tmp/target".to_string(),
        volume_context: HashMap::new(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(err.message().contains("module"));
}

#[tokio::test]
async fn node_publish_volume_missing_version_attr() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodePublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: "/tmp/target".to_string(),
        volume_context: [("module".to_string(), "nettools".to_string())]
            .into_iter()
            .collect(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(err.message().contains("version"));
}

#[tokio::test]
async fn node_publish_volume_missing_target_path() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodePublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: String::new(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_publish_volume_missing_volume_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodePublishVolumeRequest {
        volume_id: String::new(),
        target_path: "/tmp/target".to_string(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(err.message().contains("volume_id"));
}

#[tokio::test]
async fn node_stage_volume_missing_module_attr() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeStageVolumeRequest {
        volume_id: "vol-1".to_string(),
        staging_target_path: "/tmp/staging".to_string(),
        volume_context: HashMap::new(),
        ..Default::default()
    };
    let err = node.node_stage_volume(Request::new(req)).await.unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_stage_volume_missing_volume_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeStageVolumeRequest {
        volume_id: String::new(),
        staging_target_path: "/tmp/staging".to_string(),
        volume_context: HashMap::new(),
        ..Default::default()
    };
    let err = node.node_stage_volume(Request::new(req)).await.unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(err.message().contains("volume_id"));
}

#[tokio::test]
async fn node_stage_volume_missing_staging_path() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeStageVolumeRequest {
        volume_id: "vol-1".to_string(),
        staging_target_path: String::new(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node.node_stage_volume(Request::new(req)).await.unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(err.message().contains("staging_target_path"));
}

#[tokio::test]
async fn node_unstage_volume_succeeds() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeUnstageVolumeRequest {
        volume_id: "vol-1".to_string(),
        ..Default::default()
    };
    // unstage is a no-op (cache persists) — just verify it returns Ok
    let _resp = node
        .node_unstage_volume(Request::new(req))
        .await
        .expect("unstage should succeed for a valid volume_id");
}

#[tokio::test]
async fn node_unstage_volume_missing_volume_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeUnstageVolumeRequest {
        volume_id: String::new(),
        ..Default::default()
    };
    let err = node
        .node_unstage_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(
        err.message().contains("volume_id"),
        "error should mention volume_id: {}",
        err.message()
    );
}

#[tokio::test]
async fn node_unpublish_volume_missing_target_path() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeUnpublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: String::new(),
    };
    let err = node
        .node_unpublish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_unpublish_volume_missing_volume_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeUnpublishVolumeRequest {
        volume_id: String::new(),
        target_path: "/tmp/target".to_string(),
    };
    let err = node
        .node_unpublish_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_get_volume_stats_returns_usage() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));

    // Create a fake volume directory with some content
    let vol_dir = dir.path().join("vol-content");
    std::fs::create_dir_all(&vol_dir).unwrap();
    std::fs::write(vol_dir.join("file1.txt"), "hello").unwrap();
    std::fs::write(vol_dir.join("file2.txt"), "world!").unwrap();

    let req = NodeGetVolumeStatsRequest {
        volume_id: "vol-1".to_string(),
        volume_path: vol_dir.to_str().unwrap().to_string(),
        ..Default::default()
    };
    let resp = node.node_get_volume_stats(Request::new(req)).await.unwrap();
    let usage = resp.into_inner().usage;
    assert_eq!(usage.len(), 2);

    // Bytes entry
    let bytes_entry = usage
        .iter()
        .find(|u| u.unit == volume_usage::Unit::Bytes as i32)
        .unwrap();
    assert_eq!(bytes_entry.used, 11); // "hello" (5) + "world!" (6)
    assert_eq!(bytes_entry.total, 11);
    assert_eq!(bytes_entry.available, 0);

    // Inodes entry
    let inodes_entry = usage
        .iter()
        .find(|u| u.unit == volume_usage::Unit::Inodes as i32)
        .unwrap();
    assert_eq!(inodes_entry.used, 3); // root dir + 2 files
    assert_eq!(inodes_entry.total, 3);
}

#[tokio::test]
async fn node_get_volume_stats_missing_volume_id() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeGetVolumeStatsRequest {
        volume_id: String::new(),
        volume_path: "/tmp".to_string(),
        ..Default::default()
    };
    let err = node
        .node_get_volume_stats(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_get_volume_stats_missing_volume_path() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeGetVolumeStatsRequest {
        volume_id: "vol-1".to_string(),
        volume_path: String::new(),
        ..Default::default()
    };
    let err = node
        .node_get_volume_stats(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[tokio::test]
async fn node_get_volume_stats_nonexistent_path() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeGetVolumeStatsRequest {
        volume_id: "vol-1".to_string(),
        volume_path: "/nonexistent/cfgd-test-path".to_string(),
        ..Default::default()
    };
    let err = node
        .node_get_volume_stats(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::NotFound);
}

#[tokio::test]
async fn node_expand_volume_unimplemented() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeExpandVolumeRequest {
        volume_id: "vol-1".to_string(),
        ..Default::default()
    };
    let err = node
        .node_expand_volume(Request::new(req))
        .await
        .unwrap_err();
    assert_eq!(err.code(), tonic::Code::Unimplemented);
}

#[test]
fn resolve_oci_ref_uses_provided_value() {
    let attrs: HashMap<String, String> =
        [("ociRef".to_string(), "ghcr.io/myorg/mod:v1".to_string())]
            .into_iter()
            .collect();
    assert_eq!(resolve_oci_ref(&attrs, "mod", "v1"), "ghcr.io/myorg/mod:v1");
}

#[test]
fn resolve_oci_ref_falls_back_to_default() {
    let attrs: HashMap<String, String> = HashMap::new();
    assert_eq!(
        resolve_oci_ref(&attrs, "nettools", "1.0"),
        "cfgd-modules/nettools:1.0"
    );
}

#[test]
fn is_mountpoint_false_for_regular_dir() {
    let dir = tempfile::tempdir().unwrap();
    assert!(!is_mountpoint(dir.path()));
}

#[test]
fn is_mountpoint_false_for_nonexistent() {
    assert!(!is_mountpoint(Path::new("/nonexistent/path/cfgd-test")));
}

#[tokio::test]
async fn node_unpublish_volume_nonexistent_target_succeeds() {
    // CSI spec: unpublish of a path that doesn't exist should be idempotent success
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let target = dir.path().join("does-not-exist");
    let req = NodeUnpublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: target.to_str().unwrap().to_string(),
    };
    // Should succeed (unmount returns Ok for ENOENT, remove_dir warns but doesn't fail)
    assert!(node.node_unpublish_volume(Request::new(req)).await.is_ok());
}

#[tokio::test]
async fn node_unpublish_volume_empty_dir_cleans_up() {
    // Unpublish on a non-mounted directory should succeed and remove the dir
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let target = dir.path().join("empty-target");
    std::fs::create_dir_all(&target).unwrap();
    assert!(target.exists());

    let req = NodeUnpublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: target.to_str().unwrap().to_string(),
    };
    assert!(node.node_unpublish_volume(Request::new(req)).await.is_ok());
    // The directory should be cleaned up after unpublish
    assert!(
        !target.exists(),
        "target dir should be removed after unpublish"
    );
}

#[tokio::test]
async fn node_get_volume_stats_with_subdirectories() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));

    let vol = dir.path().join("nested-vol");
    let subdir = vol.join("subdir");
    std::fs::create_dir_all(&subdir).unwrap();
    std::fs::write(vol.join("root.txt"), "abc").unwrap(); // 3 bytes
    std::fs::write(subdir.join("nested.txt"), "defgh").unwrap(); // 5 bytes

    let req = NodeGetVolumeStatsRequest {
        volume_id: "vol-1".to_string(),
        volume_path: vol.to_str().unwrap().to_string(),
        ..Default::default()
    };
    let resp = node.node_get_volume_stats(Request::new(req)).await.unwrap();
    let usage = resp.into_inner().usage;

    let bytes_entry = usage
        .iter()
        .find(|u| u.unit == volume_usage::Unit::Bytes as i32)
        .unwrap();
    assert_eq!(bytes_entry.used, 8); // 3 + 5

    let inodes_entry = usage
        .iter()
        .find(|u| u.unit == volume_usage::Unit::Inodes as i32)
        .unwrap();
    // root dir (1) + subdir entry in readdir (1) + root.txt (1) + nested.txt (1) + subdir itself walked = total 4
    // Actually: walk counts root(1), then readdir(root): subdir(+1), root.txt(+1), then walk(subdir): nested.txt(+1) = 4
    assert_eq!(inodes_entry.used, 4);
}

#[tokio::test]
async fn node_get_volume_stats_with_symlink() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));

    let vol = dir.path().join("symlink-vol");
    std::fs::create_dir_all(&vol).unwrap();
    std::fs::write(vol.join("real.txt"), "data").unwrap(); // 4 bytes

    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(vol.join("real.txt"), vol.join("link.txt")).unwrap();

        let req = NodeGetVolumeStatsRequest {
            volume_id: "vol-1".to_string(),
            volume_path: vol.to_str().unwrap().to_string(),
            ..Default::default()
        };
        let resp = node.node_get_volume_stats(Request::new(req)).await.unwrap();
        let usage = resp.into_inner().usage;

        let inodes_entry = usage
            .iter()
            .find(|u| u.unit == volume_usage::Unit::Inodes as i32)
            .unwrap();
        // root dir (1) + real.txt (1) + link.txt symlink (1) = 3
        assert_eq!(inodes_entry.used, 3);

        let bytes_entry = usage
            .iter()
            .find(|u| u.unit == volume_usage::Unit::Bytes as i32)
            .unwrap();
        // real.txt (4) + symlink size (varies by OS, but should include it)
        assert!(bytes_entry.used >= 4, "should include real file bytes");
    }
}

#[test]
fn walk_volume_stats_empty_dir() {
    let dir = tempfile::tempdir().unwrap();
    let (bytes, inodes) = walk_volume_stats(dir.path());
    assert_eq!(bytes, 0);
    assert_eq!(inodes, 1); // just the root dir
}

#[test]
fn walk_volume_stats_nonexistent() {
    let (bytes, inodes) = walk_volume_stats(Path::new("/nonexistent/cfgd-test-walk"));
    assert_eq!(bytes, 0);
    assert_eq!(inodes, 1); // root counted, walk fails silently
}

#[test]
fn require_volume_id_accepts_nonempty() {
    // Should return Ok(()) for any non-empty string
    require_volume_id("vol-1").expect("non-empty volume_id should be accepted");
    require_volume_id("a").expect("single-char volume_id should be accepted");
}

#[test]
fn require_volume_id_rejects_empty() {
    let err = require_volume_id("").unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
    assert!(
        err.message().contains("volume_id"),
        "error should mention volume_id: {}",
        err.message()
    );
}

#[test]
fn require_attr_accepts_present() {
    let attrs: HashMap<String, String> = [("module".to_string(), "nettools".to_string())]
        .into_iter()
        .collect();
    assert_eq!(require_attr(&attrs, "module").unwrap(), "nettools");
}

#[test]
fn require_attr_rejects_missing() {
    let attrs: HashMap<String, String> = HashMap::new();
    let err = require_attr(&attrs, "module").unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[test]
fn require_attr_rejects_empty_value() {
    let attrs: HashMap<String, String> = [("module".to_string(), String::new())]
        .into_iter()
        .collect();
    let err = require_attr(&attrs, "module").unwrap_err();
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[test]
fn resolve_oci_ref_ignores_empty_override() {
    let attrs: HashMap<String, String> = [("ociRef".to_string(), String::new())]
        .into_iter()
        .collect();
    assert_eq!(
        resolve_oci_ref(&attrs, "nettools", "1.0"),
        "cfgd-modules/nettools:1.0"
    );
}

// -------------------------------------------------------------------------
// registry_of — OCI reference parsing.
// -------------------------------------------------------------------------

#[test]
fn registry_of_extracts_dotted_host() {
    // "first path segment contains '.'" → host registry
    assert_eq!(registry_of("ghcr.io/org/mod:tag"), "ghcr.io");
}

#[test]
fn registry_of_extracts_host_with_port() {
    // colon in head → registry with explicit port
    assert_eq!(
        registry_of("myreg.local:5000/org/mod:tag"),
        "myreg.local:5000"
    );
}

#[test]
fn registry_of_returns_empty_for_default_namespace() {
    // No dot, no colon, not localhost → no explicit registry; ref is in the
    // reserved cfgd-modules namespace and registry is the empty string.
    assert_eq!(registry_of("cfgd-modules/nettools:v1"), "");
}

#[test]
fn registry_of_treats_localhost_as_registry() {
    // `localhost` is the documented exception: no dot, but explicitly a
    // registry host.
    assert_eq!(registry_of("localhost/org/mod:tag"), "localhost");
}

#[test]
fn registry_of_returns_empty_for_unslashed_ref() {
    // No '/' in the ref → first_slash == ref.len(), head == whole ref. As
    // long as it has no dot/colon and isn't `localhost`, registry is "".
    assert_eq!(registry_of("just-a-name"), "");
}

// -------------------------------------------------------------------------
// check_registry_allowed — allow-list semantics.
// -------------------------------------------------------------------------

#[test]
fn check_registry_allowed_passes_when_allow_list_unset() {
    // None means "no enforcement" — every ref passes regardless of registry.
    assert!(check_registry_allowed("ghcr.io/org/mod:v1", None).is_ok());
    assert!(check_registry_allowed("cfgd-modules/foo:v1", None).is_ok());
}

#[test]
fn check_registry_allowed_passes_default_namespace_under_allow_list() {
    // Ref with no explicit registry (default cfgd-modules namespace) is
    // always allowed: it can't reach the network without caller-provided
    // registry, so allow-listing it would be redundant.
    let allow = vec!["ghcr.io".to_string()];
    assert!(check_registry_allowed("cfgd-modules/foo:v1", Some(&allow)).is_ok());
}

#[test]
fn check_registry_allowed_passes_when_registry_in_list() {
    let allow = vec!["ghcr.io".to_string(), "quay.io".to_string()];
    assert!(check_registry_allowed("quay.io/org/mod:tag", Some(&allow)).is_ok());
}

#[test]
fn check_registry_allowed_rejects_when_registry_not_in_list() {
    let allow = vec!["ghcr.io".to_string()];
    let err = check_registry_allowed("docker.io/org/mod:tag", Some(&allow))
        .expect_err("docker.io not in allow-list");
    assert_eq!(err.code(), tonic::Code::PermissionDenied);
    assert!(err.message().contains("docker.io"));
    assert!(
        err.message().contains("CFGD_CSI_ALLOWED_REGISTRIES"),
        "error should reference the env var so the operator can fix it: {}",
        err.message()
    );
}

// -------------------------------------------------------------------------
// parse_allowed_registries_from_env — env-var parsing.
// -------------------------------------------------------------------------

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_returns_none_when_unset() {
    // SAFETY: serialised — no other test mutates this var concurrently.
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert!(parse_allowed_registries_from_env().is_none());
}

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_returns_none_for_wildcard() {
    // SAFETY: serialised.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "*");
    }
    let got = parse_allowed_registries_from_env();
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert!(
        got.is_none(),
        "'*' means 'allow any' and must collapse to None"
    );
}

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_splits_csv_with_trimming() {
    // SAFETY: serialised.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, " ghcr.io , quay.io ,, docker.io ");
    }
    let got = parse_allowed_registries_from_env();
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert_eq!(
        got,
        Some(vec![
            "ghcr.io".to_string(),
            "quay.io".to_string(),
            "docker.io".to_string()
        ]),
        "CSV split + trim + drop empty"
    );
}

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_returns_none_for_empty_string() {
    // SAFETY: serialised.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "   ");
    }
    let got = parse_allowed_registries_from_env();
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert!(got.is_none(), "whitespace-only → None");
}

// -------------------------------------------------------------------------
// node_stage_volume / node_publish_volume — exercise the post-validation
// branches by driving requests with a bogus ociRef so the cache pull fails.
// Catches the metrics emit + Status::internal mapping paths that pure
// validation tests don't reach.
// -------------------------------------------------------------------------

#[tokio::test]
#[serial_test::serial]
async fn node_stage_volume_cache_pull_failure_returns_internal_status() {
    // `test_node()` caches CFGD_CSI_ALLOWED_REGISTRIES at construction. A
    // parallel `*_rejects_disallowed_registry` test that sets it would
    // pin this Node to its allow-list, making 127.0.0.1 fail with
    // PermissionDenied instead of the Internal we test for. Force-unset
    // for the duration of this test.
    let _g = cfgd_core::test_helpers::EnvVarGuard::unset(ALLOWED_REGISTRIES_ENV);
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let req = NodeStageVolumeRequest {
        volume_id: "vol-1".to_string(),
        staging_target_path: dir.path().join("staging").to_str().unwrap().to_string(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
            // Bogus ociRef pointing at a port nothing is listening on —
            // cfgd-core's pull_module will surface Err via Cache::get_or_pull.
            (
                "ociRef".to_string(),
                "127.0.0.1:1/cfgd-csi-test/nettools:1.0".to_string(),
            ),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_stage_volume(Request::new(req))
        .await
        .expect_err("bogus oci_ref → cache pull fails → Status::internal");
    assert_eq!(err.code(), tonic::Code::Internal);
    assert!(
        err.message().contains("cache pull failed"),
        "internal error must mention cache pull failure: {}",
        err.message()
    );
}

#[tokio::test]
#[serial_test::serial]
async fn node_publish_volume_cache_pull_failure_returns_internal_status() {
    // Same race as the stage-volume counterpart: see comment above.
    let _g = cfgd_core::test_helpers::EnvVarGuard::unset(ALLOWED_REGISTRIES_ENV);
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let target = dir.path().join("publish-target");
    let req = NodePublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: target.to_str().unwrap().to_string(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
            (
                "ociRef".to_string(),
                "127.0.0.1:1/cfgd-csi-test/nettools:1.0".to_string(),
            ),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .expect_err("bogus oci_ref → cache pull fails → Status::internal");
    assert_eq!(err.code(), tonic::Code::Internal);
    assert!(
        err.message().contains("cache pull failed"),
        "internal error must mention cache pull failure: {}",
        err.message()
    );
}

#[tokio::test]
#[serial_test::serial]
async fn node_stage_volume_rejects_disallowed_registry() {
    // SAFETY: serialised — env mutation is process-global.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "ghcr.io");
    }
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }

    let req = NodeStageVolumeRequest {
        volume_id: "vol-1".to_string(),
        staging_target_path: dir.path().join("staging").to_str().unwrap().to_string(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
            (
                "ociRef".to_string(),
                "docker.io/cfgd-csi-test/nettools:1.0".to_string(),
            ),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_stage_volume(Request::new(req))
        .await
        .expect_err("docker.io not in allow-list → PermissionDenied");
    assert_eq!(err.code(), tonic::Code::PermissionDenied);
    assert!(
        err.message().contains("docker.io"),
        "rejection must name the disallowed host: {}",
        err.message()
    );
}

#[tokio::test]
#[serial_test::serial]
async fn node_publish_volume_rejects_disallowed_registry() {
    // SAFETY: serialised — env mutation is process-global.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "ghcr.io");
    }
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }

    let req = NodePublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: dir.path().join("target").to_str().unwrap().to_string(),
        volume_context: [
            ("module".to_string(), "nettools".to_string()),
            ("version".to_string(), "1.0".to_string()),
            (
                "ociRef".to_string(),
                "quay.io/cfgd-csi-test/nettools:1.0".to_string(),
            ),
        ]
        .into_iter()
        .collect(),
        ..Default::default()
    };
    let err = node
        .node_publish_volume(Request::new(req))
        .await
        .expect_err("quay.io not in allow-list → PermissionDenied");
    assert_eq!(err.code(), tonic::Code::PermissionDenied);
    assert!(
        err.message().contains("quay.io"),
        "rejection must name the disallowed host: {}",
        err.message()
    );
}

// -------------------------------------------------------------------------
// CfgdNode::new — registry allow-list constructor paths. The current
// constructor logs a warn/info but otherwise stores the parsed list; assert
// the field is what the env parser produced so the logging-branch arm is
// covered.
// -------------------------------------------------------------------------

#[test]
#[serial_test::serial]
fn cfgd_node_new_with_empty_allow_list_stores_empty_vec() {
    // SAFETY: serialised — env mutation is process-global.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, ",,,");
    }
    let dir = tempfile::tempdir().unwrap();
    let mut registry = prometheus_client::registry::Registry::default();
    let metrics = Arc::new(CsiMetrics::new(&mut registry));
    let node = CfgdNode::new(test_cache(dir.path()), metrics, "test-node".to_string());
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    // ",,," parses to Some(empty vec) — every entry is filtered out as empty
    // after split+trim. Constructor's "explicitly empty" branch logs a warn.
    assert_eq!(
        node.allowed_registries,
        Some(Vec::new()),
        "empty-but-set allow-list must be Some(empty), not None"
    );
}

#[test]
#[serial_test::serial]
fn cfgd_node_new_with_configured_allow_list_stores_parsed_entries() {
    // SAFETY: serialised.
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "ghcr.io,quay.io");
    }
    let dir = tempfile::tempdir().unwrap();
    let mut registry = prometheus_client::registry::Registry::default();
    let metrics = Arc::new(CsiMetrics::new(&mut registry));
    let node = CfgdNode::new(test_cache(dir.path()), metrics, "test-node".to_string());
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert_eq!(
        node.allowed_registries,
        Some(vec!["ghcr.io".to_string(), "quay.io".to_string()])
    );
}

// -------------------------------------------------------------------------
// check_registry_allowed — extra edge cases for the explicit-empty list
// branch (every ref refused except default-namespace refs).
// -------------------------------------------------------------------------

#[test]
fn check_registry_allowed_empty_list_rejects_explicit_registry_refs() {
    // Some(empty) → fail-closed for refs with an explicit registry host.
    let allow: Vec<String> = Vec::new();
    let err = check_registry_allowed("ghcr.io/org/mod:tag", Some(&allow))
        .expect_err("empty allow-list must reject ghcr.io ref");
    assert_eq!(err.code(), tonic::Code::PermissionDenied);
}

#[test]
fn check_registry_allowed_empty_list_still_allows_default_namespace() {
    // Default-namespace refs (no explicit registry) bypass the allow-list
    // check because they can't reach the network without a caller-provided
    // registry. Pinning this means an over-zealous future refactor that
    // tightens the empty-list branch to reject *everything* must update
    // the documented contract.
    let allow: Vec<String> = Vec::new();
    assert!(check_registry_allowed("cfgd-modules/foo:v1", Some(&allow)).is_ok());
}

// -------------------------------------------------------------------------
// node_unpublish_volume — empty volume_id branch was already covered above;
// also test the spawn_blocking unmount happy path on a non-mounted target
// to exercise the join-await branch (the "warn on remove_dir" arm) by
// pre-creating a directory that does not get removable because of perms.
// -------------------------------------------------------------------------

#[tokio::test]
async fn node_unpublish_volume_succeeds_for_existing_dir_and_removes_it() {
    // Distinct from `node_unpublish_volume_empty_dir_cleans_up`: that test
    // confirms cleanup; this one pins the response shape (empty unpublish
    // response) so a future refactor that introduces a payload must update
    // both expectations.
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));
    let target = dir.path().join("ephemeral-target");
    std::fs::create_dir_all(&target).unwrap();

    let req = NodeUnpublishVolumeRequest {
        volume_id: "vol-1".to_string(),
        target_path: target.to_str().unwrap().to_string(),
    };
    let resp = node
        .node_unpublish_volume(Request::new(req))
        .await
        .expect("unpublish on a non-mounted existing dir must succeed");
    // Response is the empty unit message — accessing it pins the shape.
    let _: NodeUnpublishVolumeResponse = resp.into_inner();
    assert!(!target.exists(), "target removed after unpublish");
}

// -------------------------------------------------------------------------
// node_get_volume_stats — additional usage entry coverage for the bytes
// vs inodes unit dispatch. The default tests don't pin the unit ordering;
// this one checks the response contains exactly the expected two units
// independent of order so a renderer that re-orders the vec won't break
// downstream consumers.
// -------------------------------------------------------------------------

#[tokio::test]
async fn node_get_volume_stats_returns_exactly_two_units_bytes_and_inodes() {
    let dir = tempfile::tempdir().unwrap();
    let node = test_node(test_cache(dir.path()));

    let vol = dir.path().join("stats-vol");
    std::fs::create_dir_all(&vol).unwrap();

    let req = NodeGetVolumeStatsRequest {
        volume_id: "vol-1".to_string(),
        volume_path: vol.to_str().unwrap().to_string(),
        ..Default::default()
    };
    let resp = node.node_get_volume_stats(Request::new(req)).await.unwrap();
    let inner = resp.into_inner();
    let units: std::collections::HashSet<i32> = inner.usage.iter().map(|u| u.unit).collect();
    assert!(units.contains(&(volume_usage::Unit::Bytes as i32)));
    assert!(units.contains(&(volume_usage::Unit::Inodes as i32)));
    assert_eq!(units.len(), 2, "exactly two distinct units");
    assert!(
        inner.volume_condition.is_none(),
        "no volume_condition is exposed (kubelet treats None as healthy)"
    );
}

// -------------------------------------------------------------------------
// Edge-case branch coverage for require_volume_id / require_attr / resolve_oci_ref.
// These are tiny helpers but the cases below pin behaviour that downstream
// staging/publishing flows depend on.
// -------------------------------------------------------------------------

#[test]
fn require_volume_id_error_message_includes_volume_id_token() {
    let err = require_volume_id("").unwrap_err();
    // Error message format is fixed contract — kubelet's CSI client logs the
    // tonic Status message verbatim, so changing it would change operator logs.
    assert!(
        err.message().contains("volume_id is required"),
        "exact wording matters: {}",
        err.message()
    );
}

#[test]
fn require_attr_present_value_is_returned_verbatim() {
    let attrs: HashMap<String, String> = [
        ("module".to_string(), "nettools".to_string()),
        ("other".to_string(), "value".to_string()),
    ]
    .into_iter()
    .collect();
    let v = require_attr(&attrs, "module").expect("present key returns value");
    assert_eq!(v, "nettools");
}

#[test]
fn require_attr_missing_message_includes_key_name() {
    let attrs: HashMap<String, String> = HashMap::new();
    let err = require_attr(&attrs, "ociRef").unwrap_err();
    assert!(
        err.message().contains("ociRef"),
        "error must name the missing attr: {}",
        err.message()
    );
    assert_eq!(err.code(), tonic::Code::InvalidArgument);
}

#[test]
fn resolve_oci_ref_provided_overrides_default_namespace() {
    // The provided ociRef wins over the synthetic cfgd-modules/{module}:{version}
    // even when both the override and module/version are present.
    let attrs: HashMap<String, String> = [
        (
            "ociRef".to_string(),
            "registry.example/team/mod:tag".to_string(),
        ),
        ("module".to_string(), "nettools".to_string()),
    ]
    .into_iter()
    .collect();
    let r = resolve_oci_ref(&attrs, "nettools", "1.0");
    assert_eq!(r, "registry.example/team/mod:tag");
}

#[test]
fn registry_of_returns_empty_for_localhost_subdir_prefix() {
    // `localhost-foo` does NOT match the localhost exception (which requires
    // the literal token "localhost"), so it's treated as a default-namespace
    // ref with no registry.
    assert_eq!(registry_of("localhost-foo/mod:tag"), "");
}

#[test]
fn registry_of_handles_ref_with_port_only_first_segment() {
    // First slash splits "host:port" from the rest — the colon rule kicks in.
    assert_eq!(
        registry_of("registry.internal:8443/foo/bar"),
        "registry.internal:8443"
    );
}

// -------------------------------------------------------------------------
// check_registry_allowed — additional explicit-empty + allow-list
// edge-case coverage. Pins the fail-closed posture of an empty allow-list
// for ANY explicit-registry ref.
// -------------------------------------------------------------------------

#[test]
fn check_registry_allowed_localhost_must_be_in_list_to_pass() {
    // `localhost` IS treated as a registry by registry_of, so an allow-list
    // that doesn't include `localhost` must reject it.
    let allow = vec!["ghcr.io".to_string()];
    let err = check_registry_allowed("localhost/team/mod:tag", Some(&allow))
        .expect_err("localhost not in allow-list must be rejected");
    assert_eq!(err.code(), tonic::Code::PermissionDenied);
    assert!(
        err.message().contains("localhost"),
        "error must name the rejected registry: {}",
        err.message()
    );
}

#[test]
fn check_registry_allowed_passes_when_localhost_in_list() {
    let allow = vec!["localhost".to_string()];
    check_registry_allowed("localhost/team/mod:tag", Some(&allow))
        .expect("localhost in allow-list must pass");
}

// -------------------------------------------------------------------------
// parse_allowed_registries_from_env — extra arms.
// -------------------------------------------------------------------------

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_single_entry_returns_some_with_one_item() {
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "ghcr.io");
    }
    let got = parse_allowed_registries_from_env();
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert_eq!(got, Some(vec!["ghcr.io".to_string()]));
}

#[test]
#[serial_test::serial]
fn parse_allowed_registries_from_env_wildcard_with_whitespace_is_still_wildcard() {
    unsafe {
        std::env::set_var(ALLOWED_REGISTRIES_ENV, "  *  ");
    }
    let got = parse_allowed_registries_from_env();
    unsafe {
        std::env::remove_var(ALLOWED_REGISTRIES_ENV);
    }
    assert!(
        got.is_none(),
        "trimmed `*` must still collapse to wildcard (None)"
    );
}

// -------------------------------------------------------------------------
// walk_volume_stats — symlink-only directory + nested deep paths.
// -------------------------------------------------------------------------

#[cfg(unix)]
#[test]
fn walk_volume_stats_directory_of_symlinks_counts_each_symlink_inode() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("real.txt");
    std::fs::write(&target, "abc").unwrap();

    let links_dir = dir.path().join("links");
    std::fs::create_dir_all(&links_dir).unwrap();
    std::os::unix::fs::symlink(&target, links_dir.join("a")).unwrap();
    std::os::unix::fs::symlink(&target, links_dir.join("b")).unwrap();
    std::os::unix::fs::symlink(&target, links_dir.join("c")).unwrap();

    let (_bytes, inodes) = walk_volume_stats(&links_dir);
    // Root dir + 3 symlinks
    assert_eq!(inodes, 4);
}

#[test]
fn walk_volume_stats_deeply_nested_dirs_counts_all_entries() {
    let dir = tempfile::tempdir().unwrap();
    // 5-deep nested dirs each with one file
    let mut p = dir.path().to_path_buf();
    for i in 0..5 {
        p = p.join(format!("d{i}"));
        std::fs::create_dir_all(&p).unwrap();
        std::fs::write(p.join("f.txt"), format!("content-{i}")).unwrap();
    }
    let (bytes, inodes) = walk_volume_stats(dir.path());
    // Each of the 5 levels has: 1 subdir + 1 file = 2 entries; plus root.
    // root(1) + (d0 + f.txt)(2) + (d1 + f.txt)(2) + ... = 1 + 5*2 = 11
    assert_eq!(inodes, 11);
    // Each file has bytes "content-N" = 9 bytes; 5 files = 45 bytes.
    assert_eq!(bytes, 45);
}