Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Gabriel Saratura committed Jan 8, 2025

Verified

This commit was signed with the committer’s verified signature.
renovate-bot Mend Renovate
1 parent 488571a commit e60b671
Showing 2 changed files with 120 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/controller/webhooks/postgresql.go
Original file line number Diff line number Diff line change
@@ -101,15 +101,14 @@ func (p *PostgreSQLWebhookHandler) validatePostgreSQL(ctx context.Context, newOb
}

// Validate major upgrades
allErrs = append(allErrs, validateMajorVersionUpgrade(newPg, oldPg))
if err := validateMajorVersionUpgrade(newPg, oldPg); err != nil {
allErrs = append(allErrs, err)
}
}

// Validate Vacuum and Repack settings
if err := validateVacuumRepack(newPg.Spec.Parameters.Service.VacuumEnabled, newPg.Spec.Parameters.Service.RepackEnabled); err != nil {
allErrs = append(allErrs, field.Forbidden(
field.NewPath("spec.parameters.service"),
fmt.Sprintf("pg.Spec.Parameters.Service.VacuumEnabled and pg.Spec.Parameters.Service.RepackEnabled settings can't be both disabled: %s", err.Error()),
))
allErrs = append(allErrs, err)
}

// Validate quotas if enabled
@@ -244,9 +243,12 @@ func (p *PostgreSQLWebhookHandler) checkGuaranteedAvailability(pg *vshnv1.VSHNPo
return allErrs
}

func validateVacuumRepack(vacuum, repack bool) error {
func validateVacuumRepack(vacuum, repack bool) *field.Error {
if !vacuum && !repack {
return fmt.Errorf("repack cannot be enabled without vacuum")
return field.Forbidden(
field.NewPath("spec.parameters.service"),
"pg.Spec.Parameters.Service.VacuumEnabled and pg.Spec.Parameters.Service.RepackEnabled settings can't be both disabled",
)
}
return nil
}
111 changes: 111 additions & 0 deletions pkg/controller/webhooks/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package webhooks

import (
"context"
"k8s.io/apimachinery/pkg/util/validation/field"
"testing"

"github.com/go-logr/logr"
@@ -492,3 +493,113 @@ func TestPostgreSQLWebhookHandler_ValidateDelete(t *testing.T) {
assert.NoError(t, err)

}

func TestPostgreSQLWebhookHandler_ValidateMajorVersionUpgrade(t *testing.T) {
tests := []struct {
name string
new *vshnv1.VSHNPostgreSQL
old *vshnv1.VSHNPostgreSQL
expectErr *field.Error
}{
{
name: "GivenSameMajorVersion_ThenNoError",
new: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "15",
},
},
},
},
old: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "15",
},
},
},
},
expectErr: nil,
},
{
name: "GivenOneMajorVersionUpdate_ThenNoError",
new: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "16",
},
},
},
},
old: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "15",
},
},
},
},
expectErr: nil,
},
{
name: "GivenTwoMajorVersionsUpdate_ThenError",
new: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "17",
},
},
},
},
old: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "15",
},
},
},
},
expectErr: field.Forbidden(
field.NewPath("spec.parameters.service.majorVersion"),
"only one major version upgrade at a time is allowed",
),
},
{
name: "GivenOneMajorVersionsBehind_ThenError",
new: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "14",
},
},
},
},
old: &vshnv1.VSHNPostgreSQL{
Spec: vshnv1.VSHNPostgreSQLSpec{
Parameters: vshnv1.VSHNPostgreSQLParameters{
Service: vshnv1.VSHNPostgreSQLServiceSpec{
MajorVersion: "15",
},
},
},
},
expectErr: field.Forbidden(
field.NewPath("spec.parameters.service.majorVersion"),
"only one major version upgrade at a time is allowed",
),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateMajorVersionUpgrade(tt.new, tt.old)
assert.Equal(t, tt.expectErr, err)
})
}
}

0 comments on commit e60b671

Please sign in to comment.