Skip to content

Commit 747a708

Browse files
committed
test(storage): test prune backups string representation
Signed-off-by: James Neill <[email protected]>
1 parent ebd353a commit 747a708

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

proxmox/storage/storage_types_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package storage
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func intPtr(i int) *int {
13+
return &i
14+
}
15+
16+
func customInt64Ptr(i int64) *types.CustomInt64 {
17+
c := types.CustomInt64(i)
18+
return &c
19+
}
20+
21+
// TestDataStoreWithBackups_String tests backup settings are encoded correctly into a string.
22+
func TestDataStoreWithBackups_String(t *testing.T) {
23+
testCases := []struct {
24+
name string
25+
input DataStoreWithBackups
26+
expected string
27+
}{
28+
{
29+
name: "Empty struct",
30+
input: DataStoreWithBackups{},
31+
expected: "",
32+
},
33+
{
34+
name: "KeepLast only",
35+
input: DataStoreWithBackups{KeepLast: intPtr(5)},
36+
expected: "keep-last=5",
37+
},
38+
{
39+
name: "KeepHourly only",
40+
input: DataStoreWithBackups{KeepHourly: intPtr(24)},
41+
expected: "keep-hourly=24",
42+
},
43+
{
44+
name: "KeepDaily only",
45+
input: DataStoreWithBackups{KeepDaily: intPtr(7)},
46+
expected: "keep-daily=7",
47+
},
48+
{
49+
name: "KeepWeekly only",
50+
input: DataStoreWithBackups{KeepWeekly: intPtr(4)},
51+
expected: "keep-weekly=4",
52+
},
53+
{
54+
name: "KeepMonthly only",
55+
input: DataStoreWithBackups{KeepMonthly: intPtr(12)},
56+
expected: "keep-monthly=12",
57+
},
58+
{
59+
name: "KeepYearly only",
60+
input: DataStoreWithBackups{KeepYearly: intPtr(3)},
61+
expected: "keep-yearly=3",
62+
},
63+
{
64+
name: "Multiple values",
65+
input: DataStoreWithBackups{
66+
KeepDaily: intPtr(30),
67+
KeepWeekly: intPtr(8),
68+
KeepYearly: intPtr(10),
69+
},
70+
expected: "keep-daily=30,keep-weekly=8,keep-yearly=10",
71+
},
72+
{
73+
name: "All values set",
74+
input: DataStoreWithBackups{
75+
KeepLast: intPtr(1),
76+
KeepHourly: intPtr(2),
77+
KeepDaily: intPtr(3),
78+
KeepWeekly: intPtr(4),
79+
KeepMonthly: intPtr(5),
80+
KeepYearly: intPtr(6),
81+
},
82+
expected: "keep-last=1,keep-hourly=2,keep-daily=3,keep-weekly=4,keep-monthly=5,keep-yearly=6",
83+
},
84+
{
85+
name: "MaxProtectedBackups should be ignored",
86+
input: DataStoreWithBackups{MaxProtectedBackups: customInt64Ptr(10)},
87+
expected: "",
88+
},
89+
}
90+
91+
for _, tc := range testCases {
92+
t.Run(tc.name, func(t *testing.T) {
93+
result := tc.input.String()
94+
assert.Equal(t, tc.expected, result)
95+
})
96+
}
97+
}
98+
99+
// TestDataStoreWithBackups_MarshalJSON tests the MarshalJSON() method.
100+
func TestDataStoreWithBackups_MarshalJSON(t *testing.T) {
101+
testCases := []struct {
102+
name string
103+
input DataStoreWithBackups
104+
expected string
105+
}{
106+
{
107+
name: "Empty struct",
108+
input: DataStoreWithBackups{},
109+
expected: `{}`,
110+
},
111+
{
112+
name: "Only MaxProtectedBackups",
113+
input: DataStoreWithBackups{MaxProtectedBackups: customInt64Ptr(10)},
114+
expected: `{"max-protected-backups":10}`,
115+
},
116+
{
117+
name: "Only prune-backups (single)",
118+
input: DataStoreWithBackups{KeepDaily: intPtr(7)},
119+
expected: `{"prune-backups":"keep-daily=7"}`,
120+
},
121+
{
122+
name: "Only prune-backups (multiple)",
123+
input: DataStoreWithBackups{
124+
KeepWeekly: intPtr(4),
125+
KeepMonthly: intPtr(12),
126+
},
127+
expected: `{"prune-backups":"keep-weekly=4,keep-monthly=12"}`,
128+
},
129+
{
130+
name: "Both MaxProtectedBackups and prune-backups",
131+
input: DataStoreWithBackups{
132+
MaxProtectedBackups: customInt64Ptr(5),
133+
KeepLast: intPtr(3),
134+
KeepYearly: intPtr(1),
135+
},
136+
expected: `{"max-protected-backups":5,"prune-backups":"keep-last=3,keep-yearly=1"}`,
137+
},
138+
}
139+
140+
for _, tc := range testCases {
141+
t.Run(tc.name, func(t *testing.T) {
142+
result, err := json.Marshal(tc.input)
143+
require.NoError(t, err)
144+
assert.JSONEq(t, tc.expected, string(result))
145+
})
146+
}
147+
}

0 commit comments

Comments
 (0)