Skip to content

Commit 5dd340d

Browse files
Boris MironovAgalin
authored andcommitted
fix: parsing PgbackrestRetention.History (#43)
Signed-off-by: Boris Mironov <[email protected]>
1 parent 78727e0 commit 5dd340d

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

internal/pgbackrest/api/config_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,21 @@ var _ = Describe("Pgbackrest credentials", func() {
158158
}.ArePopulated()).To(BeTrue())
159159
})
160160
})
161+
162+
var _ = Describe("Pgbackrest retention", func() {
163+
var history int32 = 9
164+
165+
It("can check when policy is partially populated", func() {
166+
policy1 := PgbackrestRetention{
167+
Archive: 7,
168+
}
169+
policy2 := PgbackrestRetention{
170+
History: &history,
171+
}
172+
173+
Expect(policy1.History == nil).To(BeTrue())
174+
175+
Expect(policy2.History).To(Not(BeNil()))
176+
Expect(*policy2.History).To(Equal(history))
177+
})
178+
})

internal/pgbackrest/backup/backup_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var _ = Describe("GetPgbackrestBackupOptions", func() {
8585
To(ContainSubstring(" --annotation foo=bar "))
8686
})
8787

88-
It("should include backup retention", func(ctx SpecContext) {
88+
It("should include Full backup retention", func(ctx SpecContext) {
8989
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
9090
retention := pgbackrestApi.PgbackrestRetention{
9191
Full: 28,
@@ -105,4 +105,43 @@ var _ = Describe("GetPgbackrestBackupOptions", func() {
105105
),
106106
)
107107
})
108+
109+
It("should include Archive backup retention", func(ctx SpecContext) {
110+
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
111+
retention := pgbackrestApi.PgbackrestRetention{
112+
Archive: 10,
113+
ArchiveType: "time",
114+
}
115+
pluginConfig.Repositories[0].Retention = &retention
116+
command := NewBackupCommand(pluginConfig, &backupConfig, pgDataDir)
117+
118+
options, err := command.GetPgbackrestBackupOptions(ctx, backupName, stanza)
119+
120+
Expect(err).ToNot(HaveOccurred())
121+
Expect(strings.Join(options, " ")).
122+
To(
123+
And(
124+
ContainSubstring(" --repo1-retention-archive 10 "),
125+
ContainSubstring(" --repo1-retention-archive-type time "),
126+
),
127+
)
128+
})
129+
130+
It("should include History retention", func(ctx SpecContext) {
131+
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
132+
var hist int32 = 9
133+
retention := pgbackrestApi.PgbackrestRetention{
134+
History: &hist,
135+
}
136+
pluginConfig.Repositories[0].Retention = &retention
137+
command := NewBackupCommand(pluginConfig, &backupConfig, pgDataDir)
138+
139+
options, err := command.GetPgbackrestBackupOptions(ctx, backupName, stanza)
140+
141+
Expect(err).ToNot(HaveOccurred())
142+
Expect(strings.Join(options, " ")).
143+
To(
144+
ContainSubstring(" --repo1-retention-history 9"),
145+
)
146+
})
108147
})

internal/pgbackrest/command/commandbuilder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package command
2020
import (
2121
"context"
2222
"fmt"
23+
"strconv"
2324

2425
pgbackrestApi "github.com/operasoftware/cnpg-plugin-pgbackrest/internal/pgbackrest/api"
2526
"github.com/operasoftware/cnpg-plugin-pgbackrest/internal/pgbackrest/utils"
@@ -140,7 +141,10 @@ func appendRetentionOptions(
140141
options = append(
141142
options,
142143
utils.FormatRepoFlag(repoIndex, "retention-history"),
143-
string(*retention.History))
144+
145+
// Simple string(*retention.History)) won't work below
146+
// Keep in mind it's a pointer. Explicit cast and format calls are necessary
147+
strconv.FormatInt(int64(*retention.History), 10))
144148
}
145149

146150
return options, nil

internal/pgbackrest/command/commandbuilder_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,41 @@ var _ = Describe("pgbackrestWalRestoreOptions", func() {
6363
))
6464
})
6565
})
66+
67+
var _ = Describe("PgbackrestRetention", func() {
68+
var config *pgbackrestApi.PgbackrestConfiguration
69+
var history int32 = 8
70+
retention := pgbackrestApi.PgbackrestRetention{
71+
Archive: 5,
72+
ArchiveType: "full",
73+
Full: 6,
74+
FullType: "count",
75+
Diff: 7,
76+
History: &history,
77+
}
78+
BeforeEach(func() {
79+
config = &pgbackrestApi.PgbackrestConfiguration{
80+
Repositories: []pgbackrestApi.PgbackrestRepository{
81+
{
82+
Retention: &retention,
83+
},
84+
},
85+
}
86+
})
87+
88+
It("should generate correct argument list for retention policy", func(ctx SpecContext) {
89+
var empty []string
90+
options, err := AppendRetentionOptionsFromConfiguration(ctx, empty, config)
91+
Expect(err).ToNot(HaveOccurred())
92+
Expect(strings.Join(options, " ")).
93+
To(
94+
And(
95+
ContainSubstring("--repo1-retention-archive-type full"),
96+
ContainSubstring("--repo1-retention-full-type count"),
97+
ContainSubstring("--repo1-retention-archive 5"),
98+
ContainSubstring("--repo1-retention-full 6"),
99+
ContainSubstring("--repo1-retention-diff 7"),
100+
ContainSubstring("--repo1-retention-history 8"),
101+
))
102+
})
103+
})

0 commit comments

Comments
 (0)