move setStatus test to controller package and make function public

This commit is contained in:
Felix Kunde 2019-04-18 15:18:31 +02:00
parent f0888b6017
commit 2ad951e172
5 changed files with 60 additions and 48 deletions

View File

@ -102,5 +102,5 @@ func (postgresStatus PostgresStatus) Creating() bool {
}
func (postgresStatus PostgresStatus) String() string {
return fmt.Sprintf(`status=%s`, postgresStatus.PostgresClusterStatus)
return fmt.Sprintf(`"status":{"PostgresClusterStatus": %s}`, postgresStatus.PostgresClusterStatus)
}

View File

@ -4,6 +4,7 @@ package cluster
import (
"database/sql"
"encoding/json"
"fmt"
"reflect"
"regexp"
@ -19,8 +20,6 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"encoding/json"
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
"github.com/zalando/postgres-operator/pkg/spec"
"github.com/zalando/postgres-operator/pkg/util"
@ -149,9 +148,9 @@ func (c *Cluster) setProcessName(procName string, args ...interface{}) {
}
}
func (c *Cluster) setStatus(status string) {
func (c *Cluster) SetStatus(status string) {
// TODO: eventually switch to updateStatus() for kubernetes 1.11 and above
patch, err := json.Marshal(&acidv1.PostgresStatus{PostgresClusterStatus: status})
patch, err := json.Marshal(acidv1.PostgresStatus{PostgresClusterStatus: status})
if err != nil {
c.logger.Errorf("could not marshal status: %v", err)
}
@ -210,13 +209,13 @@ func (c *Cluster) Create() error {
defer func() {
if err == nil {
c.setStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running?
c.SetStatus(acidv1.ClusterStatusRunning) //TODO: are you sure it's running?
} else {
c.setStatus(acidv1.ClusterStatusAddFailed)
c.SetStatus(acidv1.ClusterStatusAddFailed)
}
}()
c.setStatus(acidv1.ClusterStatusCreating)
c.SetStatus(acidv1.ClusterStatusCreating)
for _, role := range []PostgresRole{Master, Replica} {
@ -483,14 +482,14 @@ func (c *Cluster) Update(oldSpec, newSpec *acidv1.Postgresql) error {
c.mu.Lock()
defer c.mu.Unlock()
c.setStatus(acidv1.ClusterStatusUpdating)
c.SetStatus(acidv1.ClusterStatusUpdating)
c.setSpec(newSpec)
defer func() {
if updateFailed {
c.setStatus(acidv1.ClusterStatusUpdateFailed)
c.SetStatus(acidv1.ClusterStatusUpdateFailed)
} else {
c.setStatus(acidv1.ClusterStatusRunning)
c.SetStatus(acidv1.ClusterStatusRunning)
}
}()

View File

@ -20,10 +20,20 @@ const (
)
var logger = logrus.New().WithField("test", "cluster")
var cl = New(Config{OpConfig: config.Config{ProtectedRoles: []string{"admin"},
Auth: config.Auth{SuperUsername: superUserName,
ReplicationUsername: replicationUserName}}},
k8sutil.KubernetesClient{}, acidv1.Postgresql{}, logger)
var cl = New(
Config{
OpConfig: config.Config{
ProtectedRoles: []string{"admin"},
Auth: config.Auth{
SuperUsername: superUserName,
ReplicationUsername: replicationUserName,
},
},
},
k8sutil.KubernetesClient{},
acidv1.Postgresql{},
logger,
)
func TestInitRobotUsers(t *testing.T) {
testName := "TestInitRobotUsers"
@ -318,31 +328,3 @@ func TestShouldDeleteSecret(t *testing.T) {
}
}
}
func TestSetStatus(t *testing.T) {
tests := []struct {
status acidv1.PostgresStatus
outcome bool
}{
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating},
outcome: cl.Status.Creating(),
},
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning},
outcome: cl.Status.Running(),
},
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusSyncFailed},
outcome: !cl.Status.Success(),
},
}
for _, tt := range tests {
cl.setStatus(tt.status.PostgresClusterStatus)
if tt.outcome {
t.Errorf("Wrong status: %s", cl.Status.String())
}
}
}

View File

@ -27,9 +27,9 @@ func (c *Cluster) Sync(newSpec *acidv1.Postgresql) error {
defer func() {
if err != nil {
c.logger.Warningf("error while syncing cluster state: %v", err)
c.setStatus(acidv1.ClusterStatusSyncFailed)
c.SetStatus(acidv1.ClusterStatusSyncFailed)
} else if !c.Status.Running() {
c.setStatus(acidv1.ClusterStatusRunning)
c.SetStatus(acidv1.ClusterStatusRunning)
}
}()

View File

@ -6,12 +6,13 @@ import (
"testing"
b64 "encoding/base64"
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
"github.com/zalando/postgres-operator/pkg/spec"
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"github.com/zalando/postgres-operator/pkg/spec"
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
)
const (
@ -186,3 +187,33 @@ func TestGetInfrastructureRoles(t *testing.T) {
}
}
}
func TestSetStatus(t *testing.T) {
for _, cl := range c.clusters {
tests := []struct {
status acidv1.PostgresStatus
outcome bool
}{
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusCreating},
outcome: cl.Status.Creating(),
},
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusRunning},
outcome: cl.Status.Running(),
},
{
status: acidv1.PostgresStatus{PostgresClusterStatus: acidv1.ClusterStatusInvalid},
outcome: !cl.Status.Success(),
},
}
for _, tt := range tests {
cl.SetStatus(tt.status.PostgresClusterStatus)
if tt.outcome {
t.Errorf("Wrong status: %s", cl.Status.String())
}
}
}
}