-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot_test.go
More file actions
2688 lines (2261 loc) · 80.8 KB
/
snapshot_test.go
File metadata and controls
2688 lines (2261 loc) · 80.8 KB
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
package rigging
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestFlattenConfig_NestedStructs(t *testing.T) {
type Database struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
Password string `conf:"name:password,secret"`
}
type Config struct {
AppName string `conf:"name:app.name"`
Database Database `conf:"prefix:database"`
}
cfg := &Config{
AppName: "myapp",
Database: Database{
Host: "db.example.com",
Port: 5432,
Password: "dbpass",
},
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "AppName", KeyPath: "app.name", SourceName: "env", Secret: false},
{FieldPath: "Database.Host", KeyPath: "database.host", SourceName: "file", Secret: false},
{FieldPath: "Database.Port", KeyPath: "database.port", SourceName: "file", Secret: false},
{FieldPath: "Database.Password", KeyPath: "database.password", SourceName: "env", Secret: true},
},
}
storeProvenance(cfg, prov)
defer deleteProvenance(cfg)
result := flattenConfig(cfg)
// Check nested fields are flattened with dot notation
if result["app.name"] != "myapp" {
t.Errorf("Expected app.name=myapp, got: %v", result["app.name"])
}
if result["database.host"] != "db.example.com" {
t.Errorf("Expected database.host=db.example.com, got: %v", result["database.host"])
}
if result["database.port"] != int64(5432) {
t.Errorf("Expected database.port=5432, got: %v (type: %T)", result["database.port"], result["database.port"])
}
}
func TestFlattenConfig_OptionalHandling(t *testing.T) {
type Config struct {
Required string `conf:"name:required"`
Optional Optional[string] `conf:"name:optional"`
NotSet Optional[int] `conf:"name:notset"`
}
cfg := &Config{
Required: "value",
Optional: Optional[string]{Value: "set", Set: true},
NotSet: Optional[int]{Value: 0, Set: false},
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Required", KeyPath: "required", SourceName: "env", Secret: false},
{FieldPath: "Optional", KeyPath: "optional", SourceName: "file", Secret: false},
},
}
storeProvenance(cfg, prov)
defer deleteProvenance(cfg)
result := flattenConfig(cfg)
// Check required field is present
if result["required"] != "value" {
t.Errorf("Expected required=value, got: %v", result["required"])
}
// Check set optional is present
if result["optional"] != "set" {
t.Errorf("Expected optional=set, got: %v", result["optional"])
}
// Check unset optional is NOT present (omitted)
if _, exists := result["notset"]; exists {
t.Errorf("Expected notset to be omitted, but it exists: %v", result["notset"])
}
}
func TestFlattenConfig_SecretRedaction(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Password string `conf:"name:password,secret"`
APIKey string `conf:"name:api_key,secret"`
}
cfg := &Config{
Host: "localhost",
Password: "secret123",
APIKey: "key-abc-123",
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Host", KeyPath: "host", SourceName: "env", Secret: false},
{FieldPath: "Password", KeyPath: "password", SourceName: "env", Secret: true},
{FieldPath: "APIKey", KeyPath: "api_key", SourceName: "env", Secret: true},
},
}
storeProvenance(cfg, prov)
defer deleteProvenance(cfg)
result := flattenConfig(cfg)
// Check non-secret field is not redacted
if result["host"] != "localhost" {
t.Errorf("Expected host=localhost, got: %v", result["host"])
}
// Check secret fields are redacted
if result["password"] != "***redacted***" {
t.Errorf("Expected password to be redacted, got: %v", result["password"])
}
if result["api_key"] != "***redacted***" {
t.Errorf("Expected api_key to be redacted, got: %v", result["api_key"])
}
// Ensure actual secrets are not in result
for key, val := range result {
if strVal, ok := val.(string); ok {
if strVal == "secret123" || strVal == "key-abc-123" {
t.Errorf("Secret value found in result at key %s: %v", key, val)
}
}
}
}
func TestFlattenConfig_EmptyConfig(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
}
cfg := &Config{} // Zero values
result := flattenConfig(cfg)
// Empty config should still produce a map with zero values
if result["host"] != "" {
t.Errorf("Expected host to be empty string, got: %v", result["host"])
}
if result["port"] != int64(0) {
t.Errorf("Expected port to be 0, got: %v", result["port"])
}
}
func TestFlattenConfig_NilConfig(t *testing.T) {
var cfg *struct{}
result := flattenConfig(cfg)
// Nil config should return empty map
if result == nil {
t.Error("Expected empty map, got nil")
}
if len(result) != 0 {
t.Errorf("Expected empty map, got: %v", result)
}
}
func TestFlattenConfig_DifferentTypes(t *testing.T) {
type Config struct {
StringVal string `conf:"name:string_val"`
IntVal int `conf:"name:int_val"`
FloatVal float64 `conf:"name:float_val"`
BoolVal bool `conf:"name:bool_val"`
DurationVal time.Duration `conf:"name:duration_val"`
SliceVal []string `conf:"name:slice_val"`
TimeVal time.Time `conf:"name:time_val"`
}
testTime := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC)
cfg := &Config{
StringVal: "hello",
IntVal: 42,
FloatVal: 3.14,
BoolVal: true,
DurationVal: 5 * time.Second,
SliceVal: []string{"a", "b", "c"},
TimeVal: testTime,
}
result := flattenConfig(cfg)
// Check all types are correctly flattened
if result["string_val"] != "hello" {
t.Errorf("Expected string_val=hello, got: %v", result["string_val"])
}
if result["int_val"] != int64(42) {
t.Errorf("Expected int_val=42, got: %v (type: %T)", result["int_val"], result["int_val"])
}
if result["float_val"] != 3.14 {
t.Errorf("Expected float_val=3.14, got: %v", result["float_val"])
}
if result["bool_val"] != true {
t.Errorf("Expected bool_val=true, got: %v", result["bool_val"])
}
if result["duration_val"] != "5s" {
t.Errorf("Expected duration_val=5s, got: %v", result["duration_val"])
}
// Check slice
sliceVal, ok := result["slice_val"].([]string)
if !ok {
t.Errorf("Expected slice_val to be []string, got: %T", result["slice_val"])
} else if len(sliceVal) != 3 || sliceVal[0] != "a" || sliceVal[1] != "b" || sliceVal[2] != "c" {
t.Errorf("Expected slice_val=[a,b,c], got: %v", sliceVal)
}
// Check time is formatted as RFC3339
if result["time_val"] != "2024-01-15T10:30:00Z" {
t.Errorf("Expected time_val=2024-01-15T10:30:00Z, got: %v", result["time_val"])
}
}
func TestFlattenConfig_NoProvenance(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
}
cfg := &Config{
Host: "localhost",
Port: 8080,
}
// Don't store provenance - should still work
result := flattenConfig(cfg)
// Should still flatten correctly without provenance
if result["host"] != "localhost" {
t.Errorf("Expected host=localhost, got: %v", result["host"])
}
if result["port"] != int64(8080) {
t.Errorf("Expected port=8080, got: %v", result["port"])
}
}
func TestFlattenConfig_SecretRedactionWithoutProvenance(t *testing.T) {
type Config struct {
Token string `conf:"secret"`
Host string
}
cfg := &Config{
Token: "token-123",
Host: "localhost",
}
// Ensure no provenance is available.
ReleaseProvenance(cfg)
result := flattenConfig(cfg)
if result["token"] != "***redacted***" {
t.Errorf("expected token to be redacted, got: %v", result["token"])
}
if result["host"] != "localhost" {
t.Errorf("expected host to remain visible, got: %v", result["host"])
}
}
func TestFlattenConfig_DeeplyNested(t *testing.T) {
type Inner struct {
Value string `conf:"name:value"`
}
type Middle struct {
Inner Inner `conf:"prefix:inner"`
}
type Config struct {
Middle Middle `conf:"prefix:middle"`
}
cfg := &Config{
Middle: Middle{
Inner: Inner{
Value: "deep",
},
},
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Middle.Inner.Value", KeyPath: "middle.inner.value", SourceName: "file", Secret: false},
},
}
storeProvenance(cfg, prov)
defer deleteProvenance(cfg)
result := flattenConfig(cfg)
// Check deeply nested field
if result["middle.inner.value"] != "deep" {
t.Errorf("Expected middle.inner.value=deep, got: %v", result["middle.inner.value"])
}
}
func TestApplyExclusions_ExactPathMatching(t *testing.T) {
config := map[string]any{
"database.host": "localhost",
"database.port": 5432,
"database.password": "secret",
"api.key": "apikey123",
}
exclude := []string{"database.password", "api.key"}
result := applyExclusions(config, exclude)
// Check excluded fields are removed
if _, exists := result["database.password"]; exists {
t.Error("Expected database.password to be excluded")
}
if _, exists := result["api.key"]; exists {
t.Error("Expected api.key to be excluded")
}
// Check non-excluded fields are preserved
if result["database.host"] != "localhost" {
t.Errorf("Expected database.host=localhost, got: %v", result["database.host"])
}
if result["database.port"] != 5432 {
t.Errorf("Expected database.port=5432, got: %v", result["database.port"])
}
}
func TestApplyExclusions_CaseInsensitiveMatching(t *testing.T) {
config := map[string]any{
"Database.Host": "localhost",
"database.port": 5432,
"DATABASE.PASSWORD": "secret",
}
// Exclude with different case
exclude := []string{"database.host", "DATABASE.PORT", "Database.Password"}
result := applyExclusions(config, exclude)
// All should be excluded regardless of case
if _, exists := result["Database.Host"]; exists {
t.Error("Expected Database.Host to be excluded (case-insensitive)")
}
if _, exists := result["database.port"]; exists {
t.Error("Expected database.port to be excluded (case-insensitive)")
}
if _, exists := result["DATABASE.PASSWORD"]; exists {
t.Error("Expected DATABASE.PASSWORD to be excluded (case-insensitive)")
}
// Result should be empty
if len(result) != 0 {
t.Errorf("Expected empty result, got: %v", result)
}
}
func TestApplyExclusions_EmptyExclusionList(t *testing.T) {
config := map[string]any{
"database.host": "localhost",
"database.port": 5432,
}
result := applyExclusions(config, []string{})
// All fields should be preserved
if len(result) != len(config) {
t.Errorf("Expected %d fields, got %d", len(config), len(result))
}
if result["database.host"] != "localhost" {
t.Errorf("Expected database.host=localhost, got: %v", result["database.host"])
}
if result["database.port"] != 5432 {
t.Errorf("Expected database.port=5432, got: %v", result["database.port"])
}
}
func TestApplyExclusions_NilExclusionList(t *testing.T) {
config := map[string]any{
"database.host": "localhost",
"database.port": 5432,
}
result := applyExclusions(config, nil)
// All fields should be preserved
if len(result) != len(config) {
t.Errorf("Expected %d fields, got %d", len(config), len(result))
}
}
func TestApplyExclusions_NonExistentPaths(t *testing.T) {
config := map[string]any{
"database.host": "localhost",
"database.port": 5432,
}
// Exclude paths that don't exist
exclude := []string{"nonexistent.field", "another.missing"}
result := applyExclusions(config, exclude)
// All original fields should be preserved
if len(result) != len(config) {
t.Errorf("Expected %d fields, got %d", len(config), len(result))
}
if result["database.host"] != "localhost" {
t.Errorf("Expected database.host=localhost, got: %v", result["database.host"])
}
if result["database.port"] != 5432 {
t.Errorf("Expected database.port=5432, got: %v", result["database.port"])
}
}
func TestApplyExclusions_EmptyConfig(t *testing.T) {
config := map[string]any{}
exclude := []string{"database.password"}
result := applyExclusions(config, exclude)
// Result should be empty
if len(result) != 0 {
t.Errorf("Expected empty result, got: %v", result)
}
}
func TestApplyExclusions_PreservesOriginalConfig(t *testing.T) {
config := map[string]any{
"database.host": "localhost",
"database.password": "secret",
}
exclude := []string{"database.password"}
_ = applyExclusions(config, exclude)
// Original config should not be modified
if _, exists := config["database.password"]; !exists {
t.Error("Original config should not be modified")
}
if config["database.password"] != "secret" {
t.Errorf("Original config value should be preserved, got: %v", config["database.password"])
}
}
// CreateSnapshot unit tests
func TestCreateSnapshot_NilConfig(t *testing.T) {
var cfg *struct{}
snapshot, err := CreateSnapshot(cfg)
if err != ErrNilConfig {
t.Errorf("Expected ErrNilConfig, got: %v", err)
}
if snapshot != nil {
t.Errorf("Expected nil snapshot, got: %v", snapshot)
}
}
func TestCreateSnapshot_WithoutProvenance(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
}
cfg := &Config{
Host: "localhost",
Port: 8080,
}
// Don't store provenance - should still work
snapshot, err := CreateSnapshot(cfg)
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
if snapshot == nil {
t.Fatal("Expected snapshot, got nil")
}
// Check basic fields
if snapshot.Version != SnapshotVersion {
t.Errorf("Expected version=%s, got: %s", SnapshotVersion, snapshot.Version)
}
if snapshot.Timestamp.IsZero() {
t.Error("Expected non-zero timestamp")
}
if snapshot.Config["host"] != "localhost" {
t.Errorf("Expected host=localhost, got: %v", snapshot.Config["host"])
}
}
func TestCreateSnapshot_EmptyConfig(t *testing.T) {
type Config struct{}
cfg := &Config{}
snapshot, err := CreateSnapshot(cfg)
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
if snapshot == nil {
t.Fatal("Expected snapshot, got nil")
}
// Empty config should produce valid snapshot with empty config map
if snapshot.Version != SnapshotVersion {
t.Errorf("Expected version=%s, got: %s", SnapshotVersion, snapshot.Version)
}
if snapshot.Config == nil {
t.Error("Expected non-nil config map")
}
}
func TestCreateSnapshot_VersionAndTimestamp(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
}
cfg := &Config{Host: "localhost"}
before := time.Now().UTC()
snapshot, err := CreateSnapshot(cfg)
after := time.Now().UTC()
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Check version
if snapshot.Version != "1.0" {
t.Errorf("Expected version=1.0, got: %s", snapshot.Version)
}
// Check timestamp is within expected range
if snapshot.Timestamp.Before(before) || snapshot.Timestamp.After(after) {
t.Errorf("Timestamp %v not in expected range [%v, %v]", snapshot.Timestamp, before, after)
}
// Check timestamp is UTC
if snapshot.Timestamp.Location() != time.UTC {
t.Errorf("Expected UTC timestamp, got: %v", snapshot.Timestamp.Location())
}
}
func TestCreateSnapshot_WithProvenance(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Password string `conf:"name:password,secret"`
}
cfg := &Config{
Host: "localhost",
Password: "secret123",
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Host", KeyPath: "host", SourceName: "env:HOST", Secret: false},
{FieldPath: "Password", KeyPath: "password", SourceName: "env:PASSWORD", Secret: true},
},
}
storeProvenance(cfg, prov)
defer deleteProvenance(cfg)
snapshot, err := CreateSnapshot(cfg)
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Check provenance is included
if len(snapshot.Provenance) != 2 {
t.Errorf("Expected 2 provenance entries, got: %d", len(snapshot.Provenance))
}
// Check secrets are redacted in config
if snapshot.Config["password"] != "***redacted***" {
t.Errorf("Expected password to be redacted, got: %v", snapshot.Config["password"])
}
// Check non-secret is not redacted
if snapshot.Config["host"] != "localhost" {
t.Errorf("Expected host=localhost, got: %v", snapshot.Config["host"])
}
}
func TestCreateSnapshot_WithExclusions(t *testing.T) {
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
Password string `conf:"name:password"`
}
cfg := &Config{
Host: "localhost",
Port: 8080,
Password: "secret",
}
snapshot, err := CreateSnapshot(cfg, WithExcludeFields("password", "port"))
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Check excluded fields are not present
if _, exists := snapshot.Config["password"]; exists {
t.Error("Expected password to be excluded")
}
if _, exists := snapshot.Config["port"]; exists {
t.Error("Expected port to be excluded")
}
// Check non-excluded field is present
if snapshot.Config["host"] != "localhost" {
t.Errorf("Expected host=localhost, got: %v", snapshot.Config["host"])
}
}
// Property-based tests for CreateSnapshot
func TestCreateSnapshotProperties_SecretRedaction(t *testing.T) {
// **Feature: snapshot-core, Property 2: Secret Redaction Completeness**
// **Validates: Requirements 1.5**
// For any configuration with fields marked as secret, the snapshot config
// SHALL contain "***redacted***" for all secret field paths.
type Config struct {
Host string `conf:"name:host"`
Password string `conf:"name:password,secret"`
APIKey string `conf:"name:api_key,secret"`
Token string `conf:"name:token,secret"`
}
// Test with various secret values
testCases := []struct {
password string
apiKey string
token string
}{
{"secret1", "key1", "tok1"},
{"", "", ""},
{"very-long-secret-value-that-should-still-be-redacted", "another-key", "another-token"},
{"special!@#$%^&*()", "key with spaces", "token\nwith\nnewlines"},
}
for _, tc := range testCases {
cfg := &Config{
Host: "localhost",
Password: tc.password,
APIKey: tc.apiKey,
Token: tc.token,
}
prov := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Host", KeyPath: "host", SourceName: "env", Secret: false},
{FieldPath: "Password", KeyPath: "password", SourceName: "env", Secret: true},
{FieldPath: "APIKey", KeyPath: "api_key", SourceName: "env", Secret: true},
{FieldPath: "Token", KeyPath: "token", SourceName: "env", Secret: true},
},
}
storeProvenance(cfg, prov)
snapshot, err := CreateSnapshot(cfg)
deleteProvenance(cfg)
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Property: ALL secret fields must be redacted
secretFields := []string{"password", "api_key", "token"}
for _, field := range secretFields {
if snapshot.Config[field] != "***redacted***" {
t.Errorf("Secret field %s not redacted, got: %v", field, snapshot.Config[field])
}
}
// Property: Non-secret fields must NOT be redacted
if snapshot.Config["host"] != "localhost" {
t.Errorf("Non-secret field host should not be redacted, got: %v", snapshot.Config["host"])
}
}
}
func TestCreateSnapshotProperties_FieldExclusion(t *testing.T) {
// **Feature: snapshot-core, Property 3: Field Exclusion Correctness**
// **Validates: Requirements 4.1**
// For any configuration and exclusion list, the snapshot config
// SHALL NOT contain any field paths that match the exclusion list.
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
Password string `conf:"name:password"`
APIKey string `conf:"name:api_key"`
Debug bool `conf:"name:debug"`
}
cfg := &Config{
Host: "localhost",
Port: 8080,
Password: "secret",
APIKey: "key123",
Debug: true,
}
// Test various exclusion combinations
exclusionTests := []struct {
exclude []string
expected map[string]bool // fields that should be present
}{
{
exclude: []string{},
expected: map[string]bool{"host": true, "port": true, "password": true, "api_key": true, "debug": true},
},
{
exclude: []string{"password"},
expected: map[string]bool{"host": true, "port": true, "api_key": true, "debug": true},
},
{
exclude: []string{"password", "api_key"},
expected: map[string]bool{"host": true, "port": true, "debug": true},
},
{
exclude: []string{"host", "port", "password", "api_key", "debug"},
expected: map[string]bool{},
},
{
exclude: []string{"PASSWORD", "API_KEY"}, // case-insensitive
expected: map[string]bool{"host": true, "port": true, "debug": true},
},
}
for _, tc := range exclusionTests {
snapshot, err := CreateSnapshot(cfg, WithExcludeFields(tc.exclude...))
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Property: Excluded fields must NOT appear
for _, excluded := range tc.exclude {
normalizedKey := strings.ToLower(excluded)
if _, exists := snapshot.Config[normalizedKey]; exists {
t.Errorf("Excluded field %s should not appear in snapshot", excluded)
}
}
// Property: Non-excluded fields must appear
for field := range tc.expected {
if _, exists := snapshot.Config[field]; !exists {
t.Errorf("Non-excluded field %s should appear in snapshot", field)
}
}
}
}
func TestCreateSnapshotProperties_ProvenancePreservation(t *testing.T) {
// **Feature: snapshot-core, Property 6: Provenance Preservation**
// **Validates: Requirements 1.2**
// For any configuration with provenance data, the snapshot's Provenance field
// SHALL contain entries matching the provenance returned by GetProvenance.
type Config struct {
Host string `conf:"name:host"`
Port int `conf:"name:port"`
Password string `conf:"name:password,secret"`
}
cfg := &Config{
Host: "localhost",
Port: 8080,
Password: "secret",
}
originalProv := &Provenance{
Fields: []FieldProvenance{
{FieldPath: "Host", KeyPath: "host", SourceName: "env:HOST", Secret: false},
{FieldPath: "Port", KeyPath: "port", SourceName: "file:config.yaml", Secret: false},
{FieldPath: "Password", KeyPath: "password", SourceName: "env:PASSWORD", Secret: true},
},
}
storeProvenance(cfg, originalProv)
defer deleteProvenance(cfg)
snapshot, err := CreateSnapshot(cfg)
if err != nil {
t.Fatalf("CreateSnapshot failed: %v", err)
}
// Property: Provenance count must match
if len(snapshot.Provenance) != len(originalProv.Fields) {
t.Errorf("Expected %d provenance entries, got %d", len(originalProv.Fields), len(snapshot.Provenance))
}
// Property: Each provenance entry must be preserved
provMap := make(map[string]FieldProvenance)
for _, p := range snapshot.Provenance {
provMap[p.FieldPath] = p
}
for _, orig := range originalProv.Fields {
snapshotProv, exists := provMap[orig.FieldPath]
if !exists {
t.Errorf("Provenance for %s not found in snapshot", orig.FieldPath)
continue
}
if snapshotProv.KeyPath != orig.KeyPath {
t.Errorf("KeyPath mismatch for %s: expected %s, got %s", orig.FieldPath, orig.KeyPath, snapshotProv.KeyPath)
}
if snapshotProv.SourceName != orig.SourceName {
t.Errorf("SourceName mismatch for %s: expected %s, got %s", orig.FieldPath, orig.SourceName, snapshotProv.SourceName)
}
if snapshotProv.Secret != orig.Secret {
t.Errorf("Secret mismatch for %s: expected %v, got %v", orig.FieldPath, orig.Secret, snapshotProv.Secret)
}
}
}
// ExpandPath and ExpandPathWithTime unit tests
func TestExpandPathWithTime_SingleTimestamp(t *testing.T) {
// Test template with single {{timestamp}}
testTime := time.Date(2024, 1, 15, 10, 30, 45, 0, time.UTC)
template := "config-{{timestamp}}.json"
result := ExpandPathWithTime(template, testTime)
expected := "config-20240115-103045.json"
if result != expected {
t.Errorf("Expected %s, got: %s", expected, result)
}
}
func TestExpandPathWithTime_MultipleTimestamps(t *testing.T) {
// Test template with multiple {{timestamp}} occurrences
testTime := time.Date(2024, 6, 20, 14, 5, 9, 0, time.UTC)
template := "{{timestamp}}/config-{{timestamp}}.json"
result := ExpandPathWithTime(template, testTime)
expected := "20240620-140509/config-20240620-140509.json"
if result != expected {
t.Errorf("Expected %s, got: %s", expected, result)
}
}
func TestExpandPathWithTime_NoVariables(t *testing.T) {
// Test template with no variables (unchanged)
testTime := time.Date(2024, 1, 15, 10, 30, 45, 0, time.UTC)
template := "config/snapshot.json"
result := ExpandPathWithTime(template, testTime)
if result != template {
t.Errorf("Expected unchanged path %s, got: %s", template, result)
}
}
func TestExpandPathWithTime_EmptyPath(t *testing.T) {
testTime := time.Date(2024, 1, 15, 10, 30, 45, 0, time.UTC)
template := ""
result := ExpandPathWithTime(template, testTime)
if result != "" {
t.Errorf("Expected empty string, got: %s", result)
}
}
func TestExpandPathWithTime_TimestampOnly(t *testing.T) {
testTime := time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC)
template := "{{timestamp}}"
result := ExpandPathWithTime(template, testTime)
expected := "20241231-235959"
if result != expected {
t.Errorf("Expected %s, got: %s", expected, result)
}
}
func TestExpandPathWithTime_NonUTCTime(t *testing.T) {
// Test that non-UTC times are converted to UTC for formatting
loc, _ := time.LoadLocation("America/New_York")
testTime := time.Date(2024, 1, 15, 10, 30, 45, 0, loc) // EST
template := "config-{{timestamp}}.json"
result := ExpandPathWithTime(template, testTime)
// 10:30:45 EST = 15:30:45 UTC
expected := "config-20240115-153045.json"
if result != expected {
t.Errorf("Expected %s, got: %s", expected, result)
}
}
func TestExpandPath_UsesCurrentTime(t *testing.T) {
// Test ExpandPath vs ExpandPathWithTime consistency
template := "config-{{timestamp}}.json"
before := time.Now().UTC()
result := ExpandPath(template)
after := time.Now().UTC()
// The result should contain a timestamp between before and after
// We can't check exact value, but we can verify format
if !strings.HasPrefix(result, "config-") || !strings.HasSuffix(result, ".json") {
t.Errorf("Unexpected format: %s", result)
}
// Extract timestamp from result
timestampStr := strings.TrimPrefix(result, "config-")
timestampStr = strings.TrimSuffix(timestampStr, ".json")
// Verify it's a valid timestamp format (YYYYMMDD-HHMMSS)
if len(timestampStr) != 15 { // 8 + 1 + 6
t.Errorf("Expected timestamp length 15, got %d: %s", len(timestampStr), timestampStr)
}
// Parse the timestamp to verify it's in the expected range
parsedTime, err := time.Parse("20060102-150405", timestampStr)
if err != nil {
t.Errorf("Failed to parse timestamp %s: %v", timestampStr, err)
}
// Allow 1 second tolerance for test execution time
if parsedTime.Before(before.Add(-time.Second)) || parsedTime.After(after.Add(time.Second)) {
t.Errorf("Timestamp %v not in expected range [%v, %v]", parsedTime, before, after)
}
}
func TestExpandPath_EquivalentToExpandPathWithTime(t *testing.T) {
// Verify that ExpandPath produces the same result as ExpandPathWithTime
// when called with the same time
template := "snapshots/{{timestamp}}/config.json"
// Get current time and call both functions
now := time.Now()
expectedResult := ExpandPathWithTime(template, now)
// ExpandPath uses time.Now() internally, so we can't get exact match
// but we can verify the format is consistent
result := ExpandPath(template)
// Both should have the same structure
if !strings.HasPrefix(result, "snapshots/") || !strings.HasSuffix(result, "/config.json") {
t.Errorf("Unexpected format from ExpandPath: %s", result)
}
if !strings.HasPrefix(expectedResult, "snapshots/") || !strings.HasSuffix(expectedResult, "/config.json") {
t.Errorf("Unexpected format from ExpandPathWithTime: %s", expectedResult)
}
}
// Property-based tests for ExpandPath
func TestExpandPathProperties_TemplateExpansionConsistency(t *testing.T) {
// **Feature: snapshot-core, Property 4: Template Expansion Consistency**
// **Validates: Requirements 3.1, 3.2, 3.3**
// For any path template containing {{timestamp}}, expanding with a given time
// SHALL replace all occurrences with the same formatted timestamp string,
// and paths without templates SHALL remain unchanged.
// Test with various times across different edge cases
testTimes := []time.Time{
time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), // New Year midnight
time.Date(2024, 12, 31, 23, 59, 59, 0, time.UTC), // End of year
time.Date(2024, 6, 15, 12, 30, 45, 0, time.UTC), // Mid-year
time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), // Y2K
time.Date(2099, 12, 31, 23, 59, 59, 0, time.UTC), // Far future
time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), // Unix epoch
time.Date(2024, 2, 29, 12, 0, 0, 0, time.UTC), // Leap year
}