Fix tests

This commit is contained in:
erthalion 2018-07-09 11:24:08 +02:00
parent f9c10b3d00
commit 332db3bbda
3 changed files with 33 additions and 36 deletions

View File

@ -3,6 +3,7 @@ package cluster
import ( import (
"reflect" "reflect"
u "github.com/zalando-incubator/postgres-operator/pkg/util"
"k8s.io/client-go/pkg/api/v1" "k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/apps/v1beta1" "k8s.io/client-go/pkg/apis/apps/v1beta1"
) )
@ -19,11 +20,6 @@ type ResourceCheck struct {
reason string reason string
} }
func True() *bool {
b := true
return &b
}
type Result struct { type Result struct {
needUpdate *bool needUpdate *bool
needsRollUpdate *bool needsRollUpdate *bool
@ -60,51 +56,51 @@ func (c *Cluster) getStatefulSetChecks() []ResourceCheck {
return []ResourceCheck{ return []ResourceCheck{
c.NewCheck("new statefulset's number of replicas doesn't match the current one", c.NewCheck("new statefulset's number of replicas doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { return a.Spec.Replicas != b.Spec.Replicas }, func(a, b *v1beta1.StatefulSet) bool { return a.Spec.Replicas != b.Spec.Replicas },
Result{needUpdate: True()}), Result{needUpdate: u.True()}),
c.NewCheck("new statefulset's annotations doesn't match the current one", c.NewCheck("new statefulset's annotations doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { return !reflect.DeepEqual(a.Annotations, b.Annotations) }, func(a, b *v1beta1.StatefulSet) bool { return !reflect.DeepEqual(a.Annotations, b.Annotations) },
Result{needUpdate: True()}), Result{needUpdate: u.True()}),
c.NewCheck("new statefulset's serviceAccountName service asccount name doesn't match the current one", c.NewCheck("new statefulset's serviceAccountName service asccount name doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return len(a.Spec.Template.Spec.Containers) != len(b.Spec.Template.Spec.Containers) return len(a.Spec.Template.Spec.Containers) != len(b.Spec.Template.Spec.Containers)
}, Result{needsRollUpdate: True()}), }, Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's serviceAccountName service asccount name doesn't match the current one", c.NewCheck("new statefulset's serviceAccountName service asccount name doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return a.Spec.Template.Spec.ServiceAccountName != return a.Spec.Template.Spec.ServiceAccountName !=
b.Spec.Template.Spec.ServiceAccountName b.Spec.Template.Spec.ServiceAccountName
}, Result{needsRollUpdate: True(), needsReplace: True()}), }, Result{needsRollUpdate: u.True(), needsReplace: u.True()}),
c.NewCheck("new statefulset's terminationGracePeriodSeconds doesn't match the current one", c.NewCheck("new statefulset's terminationGracePeriodSeconds doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return a.Spec.Template.Spec.TerminationGracePeriodSeconds != return a.Spec.Template.Spec.TerminationGracePeriodSeconds !=
b.Spec.Template.Spec.TerminationGracePeriodSeconds b.Spec.Template.Spec.TerminationGracePeriodSeconds
}, Result{needsRollUpdate: True(), needsReplace: True()}), }, Result{needsRollUpdate: u.True(), needsReplace: u.True()}),
c.NewCheck("new statefulset's pod affinity doesn't match the current one", c.NewCheck("new statefulset's pod affinity doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return !reflect.DeepEqual(a.Spec.Template.Spec.Affinity, return !reflect.DeepEqual(a.Spec.Template.Spec.Affinity,
b.Spec.Template.Spec.Affinity) b.Spec.Template.Spec.Affinity)
}, Result{needsRollUpdate: True(), needsReplace: True()}), }, Result{needsRollUpdate: u.True(), needsReplace: u.True()}),
// Some generated fields like creationTimestamp make it not possible to // Some generated fields like creationTimestamp make it not possible to
// use DeepCompare on Spec.Template.ObjectMeta // use DeepCompare on Spec.Template.ObjectMeta
c.NewCheck("new statefulset's metadata labels doesn't match the current one", c.NewCheck("new statefulset's metadata labels doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return !reflect.DeepEqual(a.Spec.Template.Labels, b.Spec.Template.Labels) return !reflect.DeepEqual(a.Spec.Template.Labels, b.Spec.Template.Labels)
}, Result{needsRollUpdate: True(), needsReplace: True()}), }, Result{needsRollUpdate: u.True(), needsReplace: u.True()}),
c.NewCheck("new statefulset's pod template metadata annotations doesn't match the current one", c.NewCheck("new statefulset's pod template metadata annotations doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return !reflect.DeepEqual(a.Spec.Template.Annotations, b.Spec.Template.Annotations) return !reflect.DeepEqual(a.Spec.Template.Annotations, b.Spec.Template.Annotations)
}, Result{needUpdate: True(), needsRollUpdate: True(), needsReplace: True()}), }, Result{needUpdate: u.True(), needsRollUpdate: u.True(), needsReplace: u.True()}),
c.NewCheck("new statefulset's volumeClaimTemplates contains different number of volumes to the old one", c.NewCheck("new statefulset's volumeClaimTemplates contains different number of volumes to the old one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
return len(a.Spec.VolumeClaimTemplates) != len(b.Spec.VolumeClaimTemplates) return len(a.Spec.VolumeClaimTemplates) != len(b.Spec.VolumeClaimTemplates)
}, Result{needsReplace: True()}), }, Result{needsReplace: u.True()}),
c.NewCheck("new statefulset's selector doesn't match the current one", c.NewCheck("new statefulset's selector doesn't match the current one",
func(a, b *v1beta1.StatefulSet) bool { func(a, b *v1beta1.StatefulSet) bool {
@ -112,7 +108,7 @@ func (c *Cluster) getStatefulSetChecks() []ResourceCheck {
return false return false
} }
return !reflect.DeepEqual(a.Spec.Selector.MatchLabels, b.Spec.Selector.MatchLabels) return !reflect.DeepEqual(a.Spec.Selector.MatchLabels, b.Spec.Selector.MatchLabels)
}, Result{needsReplace: True()}), }, Result{needsReplace: u.True()}),
} }
} }
@ -120,27 +116,27 @@ func (c *Cluster) getContainerChecks() []ResourceCheck {
return []ResourceCheck{ return []ResourceCheck{
c.NewCheck("new statefulset's container %d name doesn't match the current one", c.NewCheck("new statefulset's container %d name doesn't match the current one",
func(a, b *v1.Container) bool { return a.Name != b.Name }, func(a, b *v1.Container) bool { return a.Name != b.Name },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's container %d image doesn't match the current one", c.NewCheck("new statefulset's container %d image doesn't match the current one",
func(a, b *v1.Container) bool { return a.Image != b.Image }, func(a, b *v1.Container) bool { return a.Image != b.Image },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's container %d ports don't match the current one", c.NewCheck("new statefulset's container %d ports don't match the current one",
func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.Ports, b.Ports) }, func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.Ports, b.Ports) },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's container %d resources don't match the current ones", c.NewCheck("new statefulset's container %d resources don't match the current ones",
func(a, b *v1.Container) bool { return !compareResources(&a.Resources, &b.Resources) }, func(a, b *v1.Container) bool { return !compareResources(&a.Resources, &b.Resources) },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's container %d environment doesn't match the current one", c.NewCheck("new statefulset's container %d environment doesn't match the current one",
func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.Env, b.Env) }, func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.Env, b.Env) },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
c.NewCheck("new statefulset's container %d environment sources don't match the current one", c.NewCheck("new statefulset's container %d environment sources don't match the current one",
func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.EnvFrom, b.EnvFrom) }, func(a, b *v1.Container) bool { return !reflect.DeepEqual(a.EnvFrom, b.EnvFrom) },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
} }
} }
@ -148,16 +144,16 @@ func (c *Cluster) getVolumeClaimChecks() []ResourceCheck {
return []ResourceCheck{ return []ResourceCheck{
c.NewCheck("new statefulset's name for volume %d doesn't match the current one", c.NewCheck("new statefulset's name for volume %d doesn't match the current one",
func(a, b *v1.PersistentVolumeClaim) bool { return a.Name != b.Name }, func(a, b *v1.PersistentVolumeClaim) bool { return a.Name != b.Name },
Result{needsReplace: True()}), Result{needsReplace: u.True()}),
c.NewCheck("new statefulset's annotations for volume %q doesn't match the current one", c.NewCheck("new statefulset's annotations for volume %q doesn't match the current one",
func(a, b *v1.PersistentVolumeClaim) bool { func(a, b *v1.PersistentVolumeClaim) bool {
return !reflect.DeepEqual(a.Annotations, b.Annotations) return !reflect.DeepEqual(a.Annotations, b.Annotations)
}, },
Result{needsReplace: True()}), Result{needsReplace: u.True()}),
c.NewCheck("new statefulset's volumeClaimTemplates specification for volume %q doesn't match the current one", c.NewCheck("new statefulset's volumeClaimTemplates specification for volume %q doesn't match the current one",
func(a, b *v1.PersistentVolumeClaim) bool { return !reflect.DeepEqual(a.Spec, b.Spec) }, func(a, b *v1.PersistentVolumeClaim) bool { return !reflect.DeepEqual(a.Spec, b.Spec) },
Result{needsRollUpdate: True()}), Result{needsRollUpdate: u.True()}),
} }
} }

View File

@ -2,21 +2,12 @@ package cluster
import ( import (
"github.com/zalando-incubator/postgres-operator/pkg/spec" "github.com/zalando-incubator/postgres-operator/pkg/spec"
u "github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/config" "github.com/zalando-incubator/postgres-operator/pkg/util/config"
"github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil" "github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil"
"testing" "testing"
) )
func True() *bool {
b := true
return &b
}
func False() *bool {
b := false
return &b
}
func TestCreateLoadBalancerLogic(t *testing.T) { func TestCreateLoadBalancerLogic(t *testing.T) {
var cluster = New( var cluster = New(
Config{ Config{
@ -40,14 +31,14 @@ func TestCreateLoadBalancerLogic(t *testing.T) {
{ {
subtest: "new format, load balancer is enabled for replica", subtest: "new format, load balancer is enabled for replica",
role: Replica, role: Replica,
spec: &spec.PostgresSpec{EnableReplicaLoadBalancer: True()}, spec: &spec.PostgresSpec{EnableReplicaLoadBalancer: u.True()},
opConfig: config.Config{}, opConfig: config.Config{},
result: true, result: true,
}, },
{ {
subtest: "new format, load balancer is disabled for replica", subtest: "new format, load balancer is disabled for replica",
role: Replica, role: Replica,
spec: &spec.PostgresSpec{EnableReplicaLoadBalancer: False()}, spec: &spec.PostgresSpec{EnableReplicaLoadBalancer: u.False()},
opConfig: config.Config{}, opConfig: config.Config{},
result: false, result: false,
}, },

View File

@ -127,6 +127,16 @@ func Coalesce(val, defaultVal string) string {
return val return val
} }
func True() *bool {
b := true
return &b
}
func False() *bool {
b := false
return &b
}
func IsTrue(value *bool) bool { func IsTrue(value *bool) bool {
return value != nil && *value return value != nil && *value
} }