Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76fc9a3

Browse files
committed
fix: choose the last snapshot when reset to the latest snapshot is requested
1 parent 23b6230 commit 76fc9a3

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

‎engine/internal/provision/mode_local.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"os/exec"
1515
"regexp"
16+
"sort"
1617
"strconv"
1718
"sync"
1819
"sync/atomic"
@@ -409,6 +410,22 @@ func (p *Provisioner) getSnapshot(snapshotID string) (*resources.Snapshot, error
409410
return nil, errors.Errorf("snapshot %q not found", snapshotID)
410411
}
411412

413+
return getLatestSnapshot(snapshots)
414+
}
415+
416+
func getLatestSnapshot(snapshots []resources.Snapshot) (*resources.Snapshot, error) {
417+
if len(snapshots) == 0 {
418+
return nil, errors.New("no snapshots available")
419+
}
420+
421+
sort.Slice(snapshots, func(i, j int) bool {
422+
if !snapshots[i].DataStateAt.IsZero() && !snapshots[j].DataStateAt.IsZero() {
423+
return snapshots[i].DataStateAt.After(snapshots[j].DataStateAt)
424+
}
425+
426+
return snapshots[i].CreatedAt.After(snapshots[j].CreatedAt)
427+
})
428+
412429
return &snapshots[0], nil
413430
}
414431

‎engine/internal/provision/mode_local_test.go‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,53 @@ func TestParsingDockerImage(t *testing.T) {
190190
}
191191
})
192192
}
193+
194+
func TestLatestSnapshot(t *testing.T) {
195+
t.Run("Test selecting the latest snapshot ID", func(t *testing.T) {
196+
dateTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
197+
198+
testCases := []struct {
199+
snapshots []resources.Snapshot
200+
expectedID string
201+
err error
202+
}{
203+
{
204+
err: errors.New("no snapshots available"),
205+
},
206+
{
207+
snapshots: []resources.Snapshot{
208+
{ID: "test1", CreatedAt: dateTime},
209+
{ID: "test2", CreatedAt: dateTime.Add(time.Hour)},
210+
{ID: "test3", CreatedAt: dateTime.Add(-time.Hour)},
211+
},
212+
expectedID: "test2",
213+
},
214+
{
215+
snapshots: []resources.Snapshot{
216+
{ID: "test1", DataStateAt: dateTime},
217+
{ID: "test2", DataStateAt: dateTime.Add(time.Hour)},
218+
{ID: "test3", DataStateAt: dateTime.Add(2 * time.Hour)},
219+
},
220+
expectedID: "test3",
221+
},
222+
{
223+
snapshots: []resources.Snapshot{
224+
{ID: "test1", CreatedAt: dateTime, DataStateAt: dateTime},
225+
{ID: "test2", CreatedAt: dateTime.Add(time.Hour), DataStateAt: dateTime.Add(time.Hour)},
226+
{ID: "test3", CreatedAt: dateTime.Add(-time.Hour), DataStateAt: dateTime.Add(2 * time.Hour)},
227+
},
228+
expectedID: "test3",
229+
},
230+
}
231+
232+
for _, tc := range testCases {
233+
latest, err := getLatestSnapshot(tc.snapshots)
234+
if err != nil {
235+
assert.EqualError(t, err, tc.err.Error())
236+
continue
237+
}
238+
239+
assert.Equal(t, tc.expectedID, latest.ID)
240+
}
241+
})
242+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /