postgresql tests

This commit is contained in:
Murat Kabilov 2017-05-26 12:44:44 +02:00
parent 0bfb81eb4f
commit 1708f7b8e0
1 changed files with 123 additions and 74 deletions

View File

@ -1,8 +1,8 @@
package spec package spec
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@ -104,7 +104,31 @@ var wrongMaintenanceWindows = [][]byte{
[]byte(`"Mon:00:00"`), []byte(`"Mon:00:00"`),
} }
var cl = []byte(`{ var unmarshalCluster = []struct {
in []byte
out Postgresql
}{{
[]byte(`{
"kind": "Postgresql","apiVersion": "acid.zalan.do/v1",
"metadata": {"name": "acid-testcluster1"}, "spec": {"teamId": 100}}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
Name: "acid-testcluster1",
},
Status: ClusterStatusInvalid,
Error: &json.UnmarshalTypeError{
Value: "number",
Type: reflect.TypeOf(""),
Offset: 126,
Struct: "PostgresSpec",
Field: "teamId",
},
}},
{[]byte(`{
"kind": "Postgresql", "kind": "Postgresql",
"apiVersion": "acid.zalan.do/v1", "apiVersion": "acid.zalan.do/v1",
"metadata": { "metadata": {
@ -164,68 +188,88 @@ var cl = []byte(`{
"Sat:00:00-Sat:04:00" "Sat:00:00-Sat:04:00"
] ]
} }
}`) }`),
Postgresql{
var clExp = Postgresql{ TypeMeta: unversioned.TypeMeta{
TypeMeta: unversioned.TypeMeta{ Kind: "Postgresql",
Kind: "Postgresql", APIVersion: "acid.zalan.do/v1",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
Name: "acid-testcluster1",
},
Spec: PostgresSpec{
PostgresqlParam: PostgresqlParam{
PgVersion: "9.6",
Parameters: map[string]string{
"shared_buffers": "32MB",
"max_connections": "10",
"log_statement": "all",
}, },
}, Metadata: v1.ObjectMeta{
Volume: Volume{ Name: "acid-testcluster1",
Size: "5Gi",
StorageClass: "SSD",
},
Patroni: Patroni{
InitDB: map[string]string{
"encoding": "UTF8",
"locale": "en_US.UTF-8",
"data-checksums": "true",
}, },
PgHba: []string{"hostssl all all 0.0.0.0/0 md5", "host all all 0.0.0.0/0 md5"}, Spec: PostgresSpec{
TTL: 30, PostgresqlParam: PostgresqlParam{
LoopWait: 10, PgVersion: "9.6",
RetryTimeout: 10, Parameters: map[string]string{
MaximumLagOnFailover: 33554432, "shared_buffers": "32MB",
}, "max_connections": "10",
Resources: Resources{ "log_statement": "all",
ResourceRequest: ResourceDescription{CPU: "10m", Memory: "50Mi"}, },
ResourceLimits: ResourceDescription{CPU: "300m", Memory: "3000Mi"}, },
}, Volume: Volume{
TeamID: "ACID", Size: "5Gi",
AllowedSourceRanges: []string{"127.0.0.1/32"}, StorageClass: "SSD",
NumberOfInstances: 2, },
Users: map[string]UserFlags{"zalando": {"superuser", "createdb"}}, Patroni: Patroni{
MaintenanceWindows: []MaintenanceWindow{{ InitDB: map[string]string{
StartWeekday: time.Monday, "encoding": "UTF8",
StartTime: mustParseTime("01:00"), "locale": "en_US.UTF-8",
EndTime: mustParseTime("06:00"), "data-checksums": "true",
EndWeekday: time.Sunday, },
}, PgHba: []string{"hostssl all all 0.0.0.0/0 md5", "host all all 0.0.0.0/0 md5"},
{ TTL: 30,
StartWeekday: time.Saturday, LoopWait: 10,
StartTime: mustParseTime("00:00"), RetryTimeout: 10,
EndTime: mustParseTime("04:00"), MaximumLagOnFailover: 33554432,
EndWeekday: time.Saturday, },
Resources: Resources{
ResourceRequest: ResourceDescription{CPU: "10m", Memory: "50Mi"},
ResourceLimits: ResourceDescription{CPU: "300m", Memory: "3000Mi"},
},
TeamID: "ACID",
AllowedSourceRanges: []string{"127.0.0.1/32"},
NumberOfInstances: 2,
Users: map[string]UserFlags{"zalando": {"superuser", "createdb"}},
MaintenanceWindows: []MaintenanceWindow{{
StartWeekday: time.Monday,
StartTime: mustParseTime("01:00"),
EndTime: mustParseTime("06:00"),
EndWeekday: time.Sunday,
},
{
StartWeekday: time.Saturday,
StartTime: mustParseTime("00:00"),
EndTime: mustParseTime("04:00"),
EndWeekday: time.Saturday,
},
},
ClusterName: "testcluster1",
}, },
}, Error: nil,
ClusterName: "testcluster1", }},
}, {
Status: "", []byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "teapot-testcluster1"}, "spec": {"teamId": "acid"}}`),
Error: nil, Postgresql{
TypeMeta: unversioned.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
Name: "teapot-testcluster1",
},
Spec: PostgresSpec{TeamID: "acid"},
Status: ClusterStatusInvalid,
Error: errors.New("name must match {TEAM}-{NAME} format"),
}},
} }
var invalidClusterSpec = []struct {
in []byte
err error
}{{[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1"`),
errors.New("unexpected end of JSON input"),
}}
func mustParseTime(s string) time.Time { func mustParseTime(s string) time.Time {
v, err := time.Parse("15:04", s) v, err := time.Parse("15:04", s)
if err != nil { if err != nil {
@ -323,25 +367,30 @@ func TestUnmarshalMaintWindowsErrs(t *testing.T) {
} }
} }
func TestPostgresqlUnmarshal(t *testing.T) { func TestPostgresUnmarshal(t *testing.T) {
var cluster Postgresql for _, tt := range unmarshalCluster {
err := cluster.UnmarshalJSON(cl) var cluster Postgresql
if err != nil { err := cluster.UnmarshalJSON(tt.in)
t.Errorf("Unmarshal Error: %v", err) if err != nil {
} t.Errorf("Unmarshal Error: %v", err)
}
if !reflect.DeepEqual(cluster, clExp) { if !reflect.DeepEqual(cluster, tt.out) {
t.Errorf("Expected Postgresql: %#v, got %#v", clExp, cluster) t.Errorf("Expected Postgresql: %#v, got %#v", tt.out, cluster)
}
} }
} }
func TestPostgresqlMarshal(t *testing.T) { func TestInvalidPostgresUnmarshal(t *testing.T) {
m, err := json.Marshal(clExp) for _, tt := range invalidClusterSpec {
if err != nil { var cluster Postgresql
t.Errorf("Marshal Error: %v", err) err := cluster.UnmarshalJSON(tt.in)
} if err == nil {
t.Errorf("Error expected for %s", string(tt.in))
}
if bytes.Compare(m, cl) != 0 { if err.Error() != tt.err.Error() {
t.Errorf("Expected postgresql marshal: %s, got %s", string(cl), string(m)) t.Errorf("Unmarshal error expected: %v, got: %v", tt.err, err)
}
} }
} }