Skip to content

Commit

Permalink
Merge pull request #57 from vshn/refactoring
Browse files Browse the repository at this point in the history
refactoring postgresql controller
  • Loading branch information
mweibel authored Nov 14, 2022
2 parents 670d55a + 4941e50 commit 00d4060
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 531 deletions.
7 changes: 6 additions & 1 deletion apis/exoscale/v1/dbaas_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
exoscaleoapi "github.com/exoscale/egoscale/v2/oapi"
Expand Down Expand Up @@ -50,6 +51,10 @@ type BackupSpec struct {
TimeOfDay TimeOfDay `json:"timeOfDay,omitempty"`
}

func (in *BackupSpec) Equals(other BackupSpec) bool {
return in.TimeOfDay.String() == other.TimeOfDay.String()
}

// MaintenanceSpec contains settings to control the maintenance of an instance.
type MaintenanceSpec struct {
// +kubebuilder:validation:Enum=monday;tuesday;wednesday;thursday;friday;saturday;sunday;never
Expand Down Expand Up @@ -107,7 +112,7 @@ type SizeSpec struct {
}

func (s SizeSpec) Equals(other SizeSpec) bool {
return s.Plan == other.Plan
return strings.EqualFold(s.Plan, other.Plan)
}

// IPFilter is a list of allowed IPv4 CIDR ranges that can access the service.
Expand Down
33 changes: 33 additions & 0 deletions apis/exoscale/v1/dbaas_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,43 @@ func TestSizeSpec_Equals(t *testing.T) {
},
want: false,
},
"plan ignores case": {
s: SizeSpec{
Plan: "a",
},
other: SizeSpec{
Plan: "A",
},
want: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.s.Equals(tt.other), "Equals(%v)", tt.other)
})
}
}

func TestBackupSpec_Equals(t *testing.T) {
tests := map[string]struct {
givenSpec BackupSpec
observedSpec BackupSpec
expected bool
}{
"Same": {
givenSpec: BackupSpec{TimeOfDay: "12:00:00"},
observedSpec: BackupSpec{TimeOfDay: "12:00:00"},
expected: true,
},
"Different": {
givenSpec: BackupSpec{TimeOfDay: "13:00:00"},
observedSpec: BackupSpec{TimeOfDay: "12:00:00"},
expected: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.givenSpec.Equals(tc.observedSpec))
})
}
}
34 changes: 1 addition & 33 deletions operator/mapper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ type BackupSchedule = struct {
BackupMinute *int64 `json:"backup-minute,omitempty"`
}

// MaintenanceScheduleCreateRequest is a type alias for the embedded struct in opai.CreateDbaasServicePgJSONRequestBody.
type MaintenanceScheduleCreateRequest = struct {
// Day of week for installing updates
Dow oapi.CreateDbaasServicePgJSONBodyMaintenanceDow `json:"dow"`

// Time for installing updates, UTC
Time string `json:"time"`
}

// MaintenanceScheduleUpdateRequest is a type alias for the embedded struct in opai.UpdateDbaasServicePgJSONRequestBody.
type MaintenanceScheduleUpdateRequest = struct {
// Day of week for installing updates
Dow oapi.UpdateDbaasServicePgJSONBodyMaintenanceDow `json:"dow"`

// Time for installing updates, UTC
Time string `json:"time"`
}

func ToBackupSchedule(day exoscalev1.TimeOfDay) (BackupSchedule, error) {
backupHour, backupMin, _, err := day.Parse()
return BackupSchedule{
Expand All @@ -42,21 +24,7 @@ func ToBackupSchedule(day exoscalev1.TimeOfDay) (BackupSchedule, error) {
}, err
}

func toMaintenanceScheduleCreateRequest(spec exoscalev1.MaintenanceSpec) MaintenanceScheduleCreateRequest {
return MaintenanceScheduleCreateRequest{
Dow: oapi.CreateDbaasServicePgJSONBodyMaintenanceDow(spec.DayOfWeek),
Time: spec.TimeOfDay.String(),
}
}

func toMaintenanceScheduleUpdateRequest(spec exoscalev1.MaintenanceSpec) MaintenanceScheduleUpdateRequest {
return MaintenanceScheduleUpdateRequest{
Dow: oapi.UpdateDbaasServicePgJSONBodyMaintenanceDow(spec.DayOfWeek),
Time: spec.TimeOfDay.String(),
}
}

func toSlicePtr(arr []string) *[]string {
func ToSlicePtr(arr []string) *[]string {
return &arr
}

Expand Down
30 changes: 2 additions & 28 deletions operator/mapper/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"k8s.io/utils/pointer"
)

func Test_toBackupSpec(t *testing.T) {
func TestToBackupSpec(t *testing.T) {
tests := map[string]struct {
givenSchedule *BackupSchedule
expectedSpec exoscalev1.BackupSpec
Expand Down Expand Up @@ -38,7 +38,7 @@ func Test_toBackupSpec(t *testing.T) {
}
}

func Test_toBackupSchedule(t *testing.T) {
func TestToBackupSchedule(t *testing.T) {
tests := map[string]struct {
givenTime exoscalev1.TimeOfDay
expectedSchedule BackupSchedule
Expand All @@ -60,29 +60,3 @@ func Test_toBackupSchedule(t *testing.T) {
})
}
}

func Test_toMaintenanceSchedule(t *testing.T) {
tests := map[string]struct {
givenSpec exoscalev1.MaintenanceSpec
expectedResult MaintenanceScheduleCreateRequest
}{
"Disabled": {
givenSpec: exoscalev1.MaintenanceSpec{TimeOfDay: "0:00:00", DayOfWeek: "never"},
expectedResult: MaintenanceScheduleCreateRequest{Time: "0:00:00", Dow: "never"},
},
"SameWeekDay": {
givenSpec: exoscalev1.MaintenanceSpec{TimeOfDay: "0:00:00", DayOfWeek: "monday"},
expectedResult: MaintenanceScheduleCreateRequest{Time: "0:00:00", Dow: "monday"},
},
"SameTime": {
givenSpec: exoscalev1.MaintenanceSpec{TimeOfDay: "12:34:56", DayOfWeek: "monday"},
expectedResult: MaintenanceScheduleCreateRequest{Time: "12:34:56", Dow: "monday"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result := toMaintenanceScheduleCreateRequest(tc.givenSpec)
assert.Equal(t, tc.expectedResult, result)
})
}
}
91 changes: 90 additions & 1 deletion operator/mapper/dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,65 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
)

func Test_CompareMajorVersion(t *testing.T) {
func TestIsSameStringSet(t *testing.T) {
tests := map[string]struct {
given []string
arg []string
expected bool
}{
"EmptyFilter_EmptyArg": {
given: []string{},
arg: []string{},
expected: true,
},
"EmptyFilter_GivenArg": {
given: []string{},
arg: []string{"arg1"},
expected: false,
},
"GivenFilter_EmptyArg": {
given: []string{"filter1"},
arg: []string{},
expected: false,
},
"SingleValue_Same": {
given: []string{"1"},
arg: []string{"1"},
expected: true,
},
"SingleValue_Different": {
given: []string{"1"},
arg: []string{"2"},
expected: false,
},
"MultipleValues_Unordered": {
given: []string{"1", "2"},
arg: []string{"2", "1"},
expected: true,
},
"MultipleValues_Difference": {
given: []string{"1", "2"},
arg: []string{"3", "1"},
expected: false,
},
"MultipleValues_Duplicates": {
given: []string{"1", "2"},
arg: []string{"2", "1", "1"},
expected: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result := IsSameStringSet(tc.given, &tc.arg)
assert.Equal(t, tc.expected, result)
})
}
}

func TestCompareMajorVersion(t *testing.T) {
tests := map[string]struct {
versionA string
versionB string
Expand Down Expand Up @@ -68,3 +124,36 @@ func Test_CompareMajorVersion(t *testing.T) {
})
}
}

func TestCompareSettings(t *testing.T) {
tests := map[string]struct {
givenSpec string
observedSpec map[string]interface{}
expected bool
}{
"BothEmpty": {givenSpec: "", observedSpec: nil, expected: true},
"Null": {givenSpec: "null", observedSpec: nil, expected: true},
"EmptyObserved": {givenSpec: `{"key":"value"}`, observedSpec: map[string]interface{}{}, expected: false},
"EmptySpec": {givenSpec: ``, observedSpec: map[string]interface{}{"key": "value"}, expected: false},
"EmptySpecObject": {givenSpec: `{}`, observedSpec: nil, expected: true},
"SameString": {givenSpec: `{"string":"value"}`, observedSpec: map[string]interface{}{"string": "value"}, expected: true},
"SameNumber": {givenSpec: `{"number":0.5}`, observedSpec: map[string]interface{}{"number": 0.5}, expected: true},
"SameBoolean": {givenSpec: `{"bool":true}`, observedSpec: map[string]interface{}{"bool": true}, expected: true},
"NestedNull": {givenSpec: `{"null":null}`, observedSpec: map[string]interface{}{"null": nil}, expected: true},
"MultipleValues": {
givenSpec: `{"bool":true,"number":0.01, "string": ""}`,
observedSpec: map[string]interface{}{"bool": true, "number": 0.01, "string": ""},
expected: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
given := runtime.RawExtension{Raw: []byte(tc.givenSpec)}
exo, err := ToRawExtension(&tc.observedSpec)
assert.NoError(t, err)

result := CompareSettings(given, exo)
assert.Equal(t, tc.expected, result)
})
}
}
10 changes: 0 additions & 10 deletions operator/mapper/error.go

This file was deleted.

Loading

0 comments on commit 00d4060

Please sign in to comment.