Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PTOCP-1756 check activedb.dbversion is respected #373

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Changes from 1 commit
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
128 changes: 88 additions & 40 deletions pkg/hostparams/hostparams_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hostparams

import (
"bytes"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -165,7 +164,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "no-change-with-default", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{}},
activeHostParams: &HostParams{Type: "postgres",
Shape: "db.t4g.medium",
Expand All @@ -177,7 +176,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "shape-different", want: true,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
Shape: "db.t4g.medium",
Expand All @@ -195,7 +194,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "shape-different-default-shape", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
MinStorageGB: 20,
Expand All @@ -212,7 +211,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "version-different", want: true,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
Shape: "db.t4g.medium",
Expand All @@ -230,7 +229,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "version-different-default-version", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{}},
activeHostParams: &HostParams{Type: "postgres",
Shape: "db.t4g.medium",
Expand All @@ -243,7 +242,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "engine-different", want: true,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
Shape: "db.t4g.different",
Expand All @@ -259,7 +258,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "storage-different-postgres", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
Shape: "db.t4g.medium",
Expand All @@ -276,7 +275,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "storage-different-aurora", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
Shape: "db.t4g.medium",
Expand All @@ -293,7 +292,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "storage-different-aurora-with-io-shape", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
Shape: "db.t4g.medium!io1",
Expand All @@ -310,7 +309,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "dbversion-empty-must-read-status.dbversion", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{
Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
Expand All @@ -334,7 +333,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
{
name: "dbversion-empty-must-read-status.dbversion", want: false,
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{
Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
Expand Down Expand Up @@ -457,15 +456,6 @@ func TestGetActiveHostParams(t *testing.T) {
}
}

var testConfig = []byte(`defaultMasterPort: 5432
defaultMasterUsername: root
defaultSslMode: require
defaultMinStorageGB: 42
sample-connection:
storageType: gp3
defaultDeletionPolicy: orphan
`)

func TestNew(t *testing.T) {
type args struct {
config *viper.Viper
Expand All @@ -480,7 +470,7 @@ func TestNew(t *testing.T) {
{
name: "use_no_default_ok",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
DBVersion: "12.11",
Expand All @@ -500,7 +490,7 @@ func TestNew(t *testing.T) {
{
name: "aurora_with_io_ok",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
DBVersion: "12.11",
Expand All @@ -520,7 +510,7 @@ func TestNew(t *testing.T) {
{
name: "postgres_ok",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand All @@ -540,7 +530,7 @@ func TestNew(t *testing.T) {
{
name: "aurora_with_io_not_ok",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
DBVersion: "12.11",
Expand All @@ -555,7 +545,7 @@ func TestNew(t *testing.T) {
{
name: "use_default_ok",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{}},
},
want: &HostParams{Type: "postgres",
Expand All @@ -569,7 +559,7 @@ func TestNew(t *testing.T) {
{
name: "maxStorage-less",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand All @@ -583,7 +573,7 @@ func TestNew(t *testing.T) {
}, {
name: "maxStorage-reduced",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand All @@ -603,7 +593,7 @@ func TestNew(t *testing.T) {
}, {
name: "maxStorage_increased",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand All @@ -629,7 +619,7 @@ func TestNew(t *testing.T) {
}, {
name: "maxStorage-equel-minStorage",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand All @@ -650,7 +640,7 @@ func TestNew(t *testing.T) {
{
name: "maxStorage-not-specified-freash-dbc",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand Down Expand Up @@ -678,7 +668,7 @@ func TestNew(t *testing.T) {
{
name: "maxStorage-not-specified-after-enabled",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand Down Expand Up @@ -722,7 +712,7 @@ func TestDeletionPolicy(t *testing.T) {
{
name: "test_default_deletion_policy",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
DBVersion: "12.11",
Expand All @@ -735,7 +725,7 @@ func TestDeletionPolicy(t *testing.T) {
{
name: "test_delete_deletion_policy",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "aurora-postgresql",
DBVersion: "12.11",
Expand All @@ -749,7 +739,7 @@ func TestDeletionPolicy(t *testing.T) {
{
name: "test_orphan_deletion_policy",
args: args{
config: NewConfig(testConfig),
config: testViper,
dbClaim: &persistancev1.DatabaseClaim{Spec: persistancev1.DatabaseClaimSpec{
Type: "postgres",
DBVersion: "12.11",
Expand Down Expand Up @@ -792,10 +782,68 @@ func TestDBVersions(t *testing.T) {

}

func NewConfig(in []byte) *viper.Viper {
c := viper.NewWithOptions(viper.KeyDelimiter(":"))
c.SetConfigType("yaml")
c.ReadConfig(bytes.NewBuffer(in))
var testViper *viper.Viper

func init() {
testViper = viper.NewWithOptions(viper.KeyDelimiter(":"))
testViper.SetConfigType("yaml")
testViper.ReadConfig(strings.NewReader(`
defaultMasterPort: 5432
defaultMasterUsername: root
defaultSslMode: require
defaultMinStorageGB: 42
sample-connection:
storageType: gp3
defaultDeletionPolicy: orphan
`))

}

func TestNew_dbversion(t *testing.T) {

var tests = []struct {
name string
claim *persistancev1.DatabaseClaim
want *HostParams
wantErr bool
}{
{
name: "ctrl_default",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both test cases have the same name

claim: &persistancev1.DatabaseClaim{
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{},
},
},
want: &HostParams{
DBVersion: defaultMajorVersion,
},
},
{
name: "ctrl_default",
claim: &persistancev1.DatabaseClaim{
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{
DBVersion: "12.11",
},
},
},
want: &HostParams{
DBVersion: "12.11",
},
},
}

for _, tt := range tests {

t.Run(tt.name, func(t *testing.T) {
got, err := New(testViper, tt.claim)
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected err: %v", err)
}
if got.DBVersion != tt.want.DBVersion {
t.Errorf("DBVersion = %v, want %v", got.DBVersion, tt.want.DBVersion)
}
})
}

return c
}
Loading