Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davissp14 committed Jul 2, 2024
1 parent 644c0d1 commit d19644c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/api/handle_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func handleViewRepmgrSettings(w http.ResponseWriter, r *http.Request) {
renderJSON(w, resp, http.StatusOK)
}

func handleViewBarmanSettings(w http.ResponseWriter, r *http.Request) {
func handleViewBarmanSettings(w http.ResponseWriter, _ *http.Request) {
if os.Getenv("BARMAN_ENABLED") == "" {
renderErr(w, fmt.Errorf("barman is not enabled"))
return
Expand Down
29 changes: 16 additions & 13 deletions internal/flypg/barman_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -118,32 +119,34 @@ func (c *BarmanConfig) Validate(requestedChanges map[string]interface{}) error {
}
}

// Verify that the values provided are valid
for k, v := range requestedChanges {
switch k {
case "archive_timeout":
if _, ok := v.(string); !ok {
// Ensure that the value is a valid duration
if _, err := time.ParseDuration(v.(string)); err != nil {
return fmt.Errorf("invalid value for archive_timeout: %v", v)
}
// TODO - validate

case "recovery_window":
if _, ok := v.(string); !ok {
// Ensure that the value is a valid duration
re := regexp.MustCompile(`^(\d+)([dwy])$`)
matches := re.FindStringSubmatch(v.(string))
if len(matches) != 3 {
return fmt.Errorf("invalid value for recovery_window: %v", v)
}
// TODO - validate

fmt.Println(matches)
log.Printf("matches: %v\n", matches)

_, err := strconv.Atoi(matches[1])
if err != nil {
return fmt.Errorf("failed to parse recovery_window: %w", err)
}

case "full_backup_frequency":
if _, ok := v.(string); !ok {
if _, err := time.ParseDuration(v.(string)); err != nil {
return fmt.Errorf("invalid value for full_backup_frequency: %v", v)
}
// TODO - validate
case "minimum_redundancy":
if _, ok := v.(string); !ok {
return fmt.Errorf("invalid value for minimum_redundancy: %v", v)
}

// Ensure that the value is a non-negative integer
val, err := strconv.Atoi(v.(string))
if err != nil {
return fmt.Errorf("invalid value for minimum_redundancy: %v", v)
Expand Down
80 changes: 80 additions & 0 deletions internal/flypg/barman_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package flypg

import (
"testing"

"github.com/fly-apps/postgres-flex/internal/flypg/state"
)

const (
barmanConfigTestDir = "./test_results/barman"
)

func TestValidateBarmanConfig(t *testing.T) {
if err := setup(t); err != nil {
t.Fatal(err)
}
defer cleanup()

store, _ := state.NewStore()

b, err := NewBarmanConfig(store, barmanConfigTestDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

t.Run("valid-config", func(t *testing.T) {

conf := ConfigMap{
"archive_timeout": "120s",
"recovery_window": "7d",
"full_backup_frequency": "24h",
"minimum_redundancy": "3",
}

if err := b.Validate(conf); err != nil {
t.Fatalf("unexpected error: %v", err)
}
})

t.Run("invalid-archive-timeout", func(t *testing.T) {
conf := ConfigMap{
"archive_timeout": "120seconds",
}

if err := b.Validate(conf); err == nil {
t.Fatalf("expected error, got nil")
}
})

t.Run("invalid-recovery-window", func(t *testing.T) {
conf := ConfigMap{
"recovery_window": "10seconds",
}

if err := b.Validate(conf); err == nil {
t.Fatalf("expected error, got nil")
}
})

t.Run("invalid-full-backup-frequency", func(t *testing.T) {
conf := ConfigMap{
"full_backup_frequency": "10seconds",
}

if err := b.Validate(conf); err == nil {
t.Fatalf("expected error, got nil")
}
})

t.Run("invalid-minimum-redundancy", func(t *testing.T) {
conf := ConfigMap{
"minimum_redundancy": "-1",
}

if err := b.Validate(conf); err == nil {
t.Fatalf("expected error, got nil")
}
})

}
2 changes: 1 addition & 1 deletion internal/flypg/barman_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewBarmanRestore(configURL string) (*BarmanRestore, error) {
return restore, nil
}

func (b *BarmanRestore) walReplayAndReset(ctx context.Context, node *Node) error {
func (*BarmanRestore) walReplayAndReset(ctx context.Context, node *Node) error {
// create a copy of the pg_hba.conf file so we can revert back to it when needed.
if err := backupHBAFile(); err != nil {
if os.IsNotExist(err) {
Expand Down

0 comments on commit d19644c

Please sign in to comment.