| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package discover |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/vmware/govmomi/vim25/types" |
| 11 | ) |
| 12 | |
| 13 | func TestSummarizeSnapshotInfo(t *testing.T) { |
| 14 | now := time.Now().UTC() |
| 15 | older := now.Add(-48 * time.Hour) |
| 16 | newer := now.Add(-2 * time.Hour) |
| 17 | oldest := now.Add(-72 * time.Hour) |
| 18 | |
| 19 | tests := map[string]struct { |
| 20 | info *types.VirtualMachineSnapshotInfo |
| 21 | want snapshotSummary |
| 22 | }{ |
| 23 | "nil snapshot info": {}, |
| 24 | "empty root list": { |
| 25 | info: &types.VirtualMachineSnapshotInfo{}, |
| 26 | }, |
| 27 | "single snapshot": { |
| 28 | info: &types.VirtualMachineSnapshotInfo{ |
| 29 | RootSnapshotList: []types.VirtualMachineSnapshotTree{ |
| 30 | {CreateTime: older}, |
| 31 | }, |
| 32 | }, |
| 33 | want: snapshotSummary{ |
| 34 | count: 1, |
| 35 | maxChainDepth: 1, |
| 36 | oldestCreateTime: older, |
| 37 | }, |
| 38 | }, |
| 39 | "siblings and nested chain": { |
| 40 | info: &types.VirtualMachineSnapshotInfo{ |
| 41 | RootSnapshotList: []types.VirtualMachineSnapshotTree{ |
| 42 | { |
| 43 | CreateTime: newer, |
| 44 | ChildSnapshotList: []types.VirtualMachineSnapshotTree{ |
| 45 | { |
| 46 | CreateTime: older, |
| 47 | ChildSnapshotList: []types.VirtualMachineSnapshotTree{ |
| 48 | {CreateTime: oldest}, |
| 49 | }, |
| 50 | }, |
| 51 | }, |
| 52 | }, |
| 53 | {CreateTime: now}, |
| 54 | }, |
| 55 | }, |
| 56 | want: snapshotSummary{ |
| 57 | count: 4, |
| 58 | maxChainDepth: 3, |
| 59 | oldestCreateTime: oldest, |
| 60 | }, |
| 61 | }, |
| 62 | "zero create time still counts": { |
| 63 | info: &types.VirtualMachineSnapshotInfo{ |
| 64 | RootSnapshotList: []types.VirtualMachineSnapshotTree{ |
| 65 | {}, |
| 66 | }, |
| 67 | }, |
| 68 | want: snapshotSummary{ |
| 69 | count: 1, |
| 70 | maxChainDepth: 1, |
| 71 | }, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | for name, tc := range tests { |
| 76 | t.Run(name, func(t *testing.T) { |
| 77 | assert.Equal(t, tc.want, summarizeSnapshotInfo(tc.info)) |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestSummarizeSnapshotInfoCapsTraversalDepth(t *testing.T) { |
| 83 | root := types.VirtualMachineSnapshotTree{} |
| 84 | node := &root |
| 85 | for i := int64(1); i < maxSnapshotTreeDepth+10; i++ { |
| 86 | node.ChildSnapshotList = []types.VirtualMachineSnapshotTree{{}} |
| 87 | node = &node.ChildSnapshotList[0] |
| 88 | } |
| 89 | |
| 90 | summary := summarizeSnapshotInfo(&types.VirtualMachineSnapshotInfo{ |
| 91 | RootSnapshotList: []types.VirtualMachineSnapshotTree{root}, |
| 92 | }) |
| 93 | |
| 94 | assert.EqualValues(t, maxSnapshotTreeDepth, summary.count) |
| 95 | assert.EqualValues(t, maxSnapshotTreeDepth, summary.maxChainDepth) |
| 96 | } |