@@ -21,19 +21,31 @@ func testManager() *Manager {
2121 }
2222}
2323
24+ // cleanSnapshots destroys all zvolta-managed snapshots on the test dataset.
25+ func cleanSnapshots (t * testing.T , mgr * Manager ) {
26+ t .Helper ()
27+ grouped , _ := mgr .ListManaged (testDataset )
28+ for _ , snaps := range grouped {
29+ for _ , s := range snaps {
30+ _ = mgr .ZFS .DestroySnapshot (s .Snapshot .Dataset , s .Snapshot .SnapName )
31+ }
32+ }
33+ }
34+
2435func TestIntegrationCreateAndList (t * testing.T ) {
2536 mgr := testManager ()
26- now := time .Date (2026 , 3 , 5 , 10 , 0 , 0 , 0 , time .UTC )
37+ cleanSnapshots (t , mgr )
38+ defer cleanSnapshots (t , mgr )
39+
40+ now := time .Date (2025 , 1 , 1 , 10 , 0 , 0 , 0 , time .UTC )
2741
28- // Create snapshots across multiple tiers
2942 tiers := []Tier {TierHourly , TierDaily }
3043 for _ , tier := range tiers {
3144 if err := mgr .Create (testDataset , tier , now ); err != nil {
3245 t .Fatalf ("Create %s: %v" , tier , err )
3346 }
3447 }
3548
36- // List and verify
3749 grouped , err := mgr .ListManaged (testDataset )
3850 if err != nil {
3951 t .Fatalf ("ListManaged: %v" , err )
@@ -55,27 +67,22 @@ func TestIntegrationCreateAndList(t *testing.T) {
5567 t .Errorf ("expected snapshot for tier %s at %v" , tier , now )
5668 }
5769 }
58-
59- // Clean up
60- for _ , tier := range tiers {
61- name := FormatName ("zvolta_" , tier , now )
62- _ = mgr .ZFS .DestroySnapshot (testDataset , name )
63- }
6470}
6571
6672func TestIntegrationPrune (t * testing.T ) {
6773 mgr := testManager ()
74+ cleanSnapshots (t , mgr )
75+ defer cleanSnapshots (t , mgr )
6876
69- // Create 5 hourly snapshots
70- base := time .Date (2026 , 3 , 5 , 10 , 0 , 0 , 0 , time .UTC )
77+ // Use unique timestamps that don't overlap with other tests
78+ base := time .Date (2025 , 2 , 1 , 10 , 0 , 0 , 0 , time .UTC )
7179 for i := 0 ; i < 5 ; i ++ {
7280 ts := base .Add (time .Duration (i ) * time .Hour )
7381 if err := mgr .Create (testDataset , TierHourly , ts ); err != nil {
7482 t .Fatalf ("Create: %v" , err )
7583 }
7684 }
7785
78- // Verify 5 exist
7986 grouped , err := mgr .ListManaged (testDataset )
8087 if err != nil {
8188 t .Fatalf ("ListManaged: %v" , err )
@@ -84,7 +91,6 @@ func TestIntegrationPrune(t *testing.T) {
8491 t .Fatalf ("expected 5 hourly snapshots, got %d" , len (grouped [TierHourly ]))
8592 }
8693
87- // Prune to keep 2
8894 removed , err := mgr .Prune (testDataset , TierHourly , 2 , false )
8995 if err != nil {
9096 t .Fatalf ("Prune: %v" , err )
@@ -93,7 +99,6 @@ func TestIntegrationPrune(t *testing.T) {
9399 t .Errorf ("expected 3 removed, got %d" , len (removed ))
94100 }
95101
96- // Verify 2 remain (the newest)
97102 grouped , err = mgr .ListManaged (testDataset )
98103 if err != nil {
99104 t .Fatalf ("ListManaged after prune: %v" , err )
@@ -102,32 +107,27 @@ func TestIntegrationPrune(t *testing.T) {
102107 t .Errorf ("expected 2 hourly after prune, got %d" , len (grouped [TierHourly ]))
103108 }
104109
105- // Verify the remaining are the newest two (13:00 and 14:00)
110+ // Verify the remaining are the newest two (13:00 and 14:00 UTC )
106111 for _ , s := range grouped [TierHourly ] {
107112 if s .Parsed .Timestamp .Hour () < 13 {
108- t .Errorf ("expected only 13:00 and 14:00 to remain, got %v" , s .Parsed .Timestamp )
113+ t .Errorf ("expected only newest to remain, got %v" , s .Parsed .Timestamp )
109114 }
110115 }
111-
112- // Clean up remaining
113- for _ , s := range grouped [TierHourly ] {
114- _ = mgr .ZFS .DestroySnapshot (testDataset , s .Snapshot .SnapName )
115- }
116116}
117117
118118func TestIntegrationDryRun (t * testing.T ) {
119119 mgr := testManager ()
120+ cleanSnapshots (t , mgr )
121+ defer cleanSnapshots (t , mgr )
120122
121- // Create 3 snapshots
122- base := time .Date (2026 , 3 , 5 , 20 , 0 , 0 , 0 , time .UTC )
123+ base := time .Date (2025 , 3 , 1 , 20 , 0 , 0 , 0 , time .UTC )
123124 for i := 0 ; i < 3 ; i ++ {
124125 ts := base .Add (time .Duration (i ) * time .Hour )
125126 if err := mgr .Create (testDataset , TierDaily , ts ); err != nil {
126127 t .Fatalf ("Create: %v" , err )
127128 }
128129 }
129130
130- // Dry-run prune to keep 1
131131 removed , err := mgr .Prune (testDataset , TierDaily , 1 , true )
132132 if err != nil {
133133 t .Fatalf ("Prune dry-run: %v" , err )
@@ -136,30 +136,32 @@ func TestIntegrationDryRun(t *testing.T) {
136136 t .Errorf ("dry-run should report 2 removals, got %d" , len (removed ))
137137 }
138138
139- // Verify all 3 still exist (dry-run shouldn't delete)
140139 grouped , err := mgr .ListManaged (testDataset )
141140 if err != nil {
142141 t .Fatalf ("ListManaged: %v" , err )
143142 }
144143 if len (grouped [TierDaily ]) != 3 {
145144 t .Errorf ("dry-run should not delete, expected 3, got %d" , len (grouped [TierDaily ]))
146145 }
147-
148- // Clean up
149- for _ , s := range grouped [TierDaily ] {
150- _ = mgr .ZFS .DestroySnapshot (testDataset , s .Snapshot .SnapName )
151- }
152146}
153147
154148func TestIntegrationLastSnapshotTimes (t * testing.T ) {
155149 mgr := testManager ()
150+ cleanSnapshots (t , mgr )
151+ defer cleanSnapshots (t , mgr )
156152
157- ts1 := time .Date (2026 , 3 , 5 , 8 , 0 , 0 , 0 , time .UTC )
158- ts2 := time .Date (2026 , 3 , 5 , 9 , 0 , 0 , 0 , time .UTC )
153+ ts1 := time .Date (2025 , 4 , 1 , 8 , 0 , 0 , 0 , time .UTC )
154+ ts2 := time .Date (2025 , 4 , 1 , 9 , 0 , 0 , 0 , time .UTC )
159155
160- _ = mgr .Create (testDataset , TierHourly , ts1 )
161- _ = mgr .Create (testDataset , TierHourly , ts2 )
162- _ = mgr .Create (testDataset , TierDaily , ts1 )
156+ if err := mgr .Create (testDataset , TierHourly , ts1 ); err != nil {
157+ t .Fatalf ("Create hourly ts1: %v" , err )
158+ }
159+ if err := mgr .Create (testDataset , TierHourly , ts2 ); err != nil {
160+ t .Fatalf ("Create hourly ts2: %v" , err )
161+ }
162+ if err := mgr .Create (testDataset , TierDaily , ts1 ); err != nil {
163+ t .Fatalf ("Create daily ts1: %v" , err )
164+ }
163165
164166 times , err := mgr .LastSnapshotTimes (testDataset )
165167 if err != nil {
@@ -172,9 +174,4 @@ func TestIntegrationLastSnapshotTimes(t *testing.T) {
172174 if ! times [TierDaily ].Equal (ts1 ) {
173175 t .Errorf ("daily last = %v, want %v" , times [TierDaily ], ts1 )
174176 }
175-
176- // Clean up
177- _ = mgr .ZFS .DestroySnapshot (testDataset , FormatName ("zvolta_" , TierHourly , ts1 ))
178- _ = mgr .ZFS .DestroySnapshot (testDataset , FormatName ("zvolta_" , TierHourly , ts2 ))
179- _ = mgr .ZFS .DestroySnapshot (testDataset , FormatName ("zvolta_" , TierDaily , ts1 ))
180177}
0 commit comments