Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/pgbackrest/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,21 @@ var _ = Describe("Pgbackrest credentials", func() {
}.ArePopulated()).To(BeTrue())
})
})

var _ = Describe("Pgbackrest retention", func() {
var history int32 = 9

It("can check when policy is partially populated", func() {
policy1 := PgbackrestRetention{
Archive: 7,
}
policy2 := PgbackrestRetention{
History: &history,
}

Expect(policy1.History == nil).To(BeTrue())

Expect(policy2.History).To(Not(BeNil()))
Expect(*policy2.History).To(Equal(history))
})
})
41 changes: 40 additions & 1 deletion internal/pgbackrest/backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var _ = Describe("GetPgbackrestBackupOptions", func() {
To(ContainSubstring(" --annotation foo=bar "))
})

It("should include backup retention", func(ctx SpecContext) {
It("should include Full backup retention", func(ctx SpecContext) {
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
retention := pgbackrestApi.PgbackrestRetention{
Full: 28,
Expand All @@ -105,4 +105,43 @@ var _ = Describe("GetPgbackrestBackupOptions", func() {
),
)
})

It("should include Archive backup retention", func(ctx SpecContext) {
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
retention := pgbackrestApi.PgbackrestRetention{
Archive: 10,
ArchiveType: "time",
}
pluginConfig.Repositories[0].Retention = &retention
command := NewBackupCommand(pluginConfig, &backupConfig, pgDataDir)

options, err := command.GetPgbackrestBackupOptions(ctx, backupName, stanza)

Expect(err).ToNot(HaveOccurred())
Expect(strings.Join(options, " ")).
To(
And(
ContainSubstring(" --repo1-retention-archive 10 "),
ContainSubstring(" --repo1-retention-archive-type time "),
),
)
})

It("should include History retention", func(ctx SpecContext) {
backupConfig := cnpgApiV1.BackupPluginConfiguration{Name: metadata.PluginName, Parameters: map[string]string{"type": "full"}}
var hist int32 = 9
retention := pgbackrestApi.PgbackrestRetention{
History: &hist,
}
pluginConfig.Repositories[0].Retention = &retention
command := NewBackupCommand(pluginConfig, &backupConfig, pgDataDir)

options, err := command.GetPgbackrestBackupOptions(ctx, backupName, stanza)

Expect(err).ToNot(HaveOccurred())
Expect(strings.Join(options, " ")).
To(
ContainSubstring(" --repo1-retention-history 9"),
)
})
})
6 changes: 5 additions & 1 deletion internal/pgbackrest/command/commandbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package command
import (
"context"
"fmt"
"strconv"

pgbackrestApi "github.com/operasoftware/cnpg-plugin-pgbackrest/internal/pgbackrest/api"
"github.com/operasoftware/cnpg-plugin-pgbackrest/internal/pgbackrest/utils"
Expand Down Expand Up @@ -140,7 +141,10 @@ func appendRetentionOptions(
options = append(
options,
utils.FormatRepoFlag(repoIndex, "retention-history"),
string(*retention.History))

// Simple string(*retention.History)) won't work below
// Keep in mind it's a pointer. Explicit cast and format calls are necessary
strconv.FormatInt(int64(*retention.History), 10))
Comment thread
Agalin marked this conversation as resolved.
}

return options, nil
Expand Down
38 changes: 38 additions & 0 deletions internal/pgbackrest/command/commandbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,41 @@ var _ = Describe("pgbackrestWalRestoreOptions", func() {
))
})
})

var _ = Describe("PgbackrestRetention", func() {
var config *pgbackrestApi.PgbackrestConfiguration
var history int32 = 8
retention := pgbackrestApi.PgbackrestRetention{
Archive: 5,
ArchiveType: "full",
Full: 6,
FullType: "count",
Diff: 7,
History: &history,
}
BeforeEach(func() {
config = &pgbackrestApi.PgbackrestConfiguration{
Repositories: []pgbackrestApi.PgbackrestRepository{
{
Retention: &retention,
},
},
}
})

It("should generate correct argument list for retention policy", func(ctx SpecContext) {
var empty []string
options, err := AppendRetentionOptionsFromConfiguration(ctx, empty, config)
Expect(err).ToNot(HaveOccurred())
Expect(strings.Join(options, " ")).
To(
And(
ContainSubstring("--repo1-retention-archive-type full"),
ContainSubstring("--repo1-retention-full-type count"),
ContainSubstring("--repo1-retention-archive 5"),
ContainSubstring("--repo1-retention-full 6"),
ContainSubstring("--repo1-retention-diff 7"),
ContainSubstring("--repo1-retention-history 8"),
))
})
})