add unit test
This commit is contained in:
parent
9390ae3630
commit
03f7fc0a92
|
|
@ -77,7 +77,7 @@ func main() {
|
||||||
log.Fatalf("couldn't get REST config: %v", err)
|
log.Fatalf("couldn't get REST config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c := controller.NewController(&config)
|
c := controller.NewController(&config, "")
|
||||||
|
|
||||||
c.Run(stop, wg)
|
c.Run(stop, wg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,14 +63,14 @@ type Controller struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewController creates a new controller
|
// NewController creates a new controller
|
||||||
func NewController(controllerConfig *spec.ControllerConfig) *Controller {
|
func NewController(controllerConfig *spec.ControllerConfig, controllerId string) *Controller {
|
||||||
logger := logrus.New()
|
logger := logrus.New()
|
||||||
|
|
||||||
c := &Controller{
|
c := &Controller{
|
||||||
config: *controllerConfig,
|
config: *controllerConfig,
|
||||||
opConfig: &config.Config{},
|
opConfig: &config.Config{},
|
||||||
logger: logger.WithField("pkg", "controller"),
|
logger: logger.WithField("pkg", "controller"),
|
||||||
controllerID: os.Getenv("CONTROLLER_ID"),
|
controllerID: controllerId,
|
||||||
curWorkerCluster: sync.Map{},
|
curWorkerCluster: sync.Map{},
|
||||||
clusterWorkers: make(map[spec.NamespacedName]uint32),
|
clusterWorkers: make(map[spec.NamespacedName]uint32),
|
||||||
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
clusters: make(map[spec.NamespacedName]*cluster.Cluster),
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/zalando/postgres-operator/pkg/spec"
|
"github.com/zalando/postgres-operator/pkg/spec"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,10 +13,10 @@ const (
|
||||||
readyValue = "ready"
|
readyValue = "ready"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initializeController() *Controller {
|
func newNodeTestController() *Controller {
|
||||||
var c = NewController(&spec.ControllerConfig{})
|
var controller = NewController(&spec.ControllerConfig{}, "node-test")
|
||||||
c.opConfig.NodeReadinessLabel = map[string]string{readyLabel: readyValue}
|
controller.opConfig.NodeReadinessLabel = map[string]string{readyLabel: readyValue}
|
||||||
return c
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeNode(labels map[string]string, isSchedulable bool) *v1.Node {
|
func makeNode(labels map[string]string, isSchedulable bool) *v1.Node {
|
||||||
|
|
@ -31,7 +31,7 @@ func makeNode(labels map[string]string, isSchedulable bool) *v1.Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var c = initializeController()
|
var nodeTestController = newNodeTestController()
|
||||||
|
|
||||||
func TestNodeIsReady(t *testing.T) {
|
func TestNodeIsReady(t *testing.T) {
|
||||||
testName := "TestNodeIsReady"
|
testName := "TestNodeIsReady"
|
||||||
|
|
@ -57,7 +57,7 @@ func TestNodeIsReady(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range testTable {
|
for _, tt := range testTable {
|
||||||
if isReady := c.nodeIsReady(tt.in); isReady != tt.out {
|
if isReady := nodeTestController.nodeIsReady(tt.in); isReady != tt.out {
|
||||||
t.Errorf("%s: expected response %t doesn't match the actual %t for the node %#v",
|
t.Errorf("%s: expected response %t doesn't match the actual %t for the node %#v",
|
||||||
testName, tt.out, isReady, tt.in)
|
testName, tt.out, isReady, tt.in)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
|
||||||
"github.com/zalando/postgres-operator/pkg/spec"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
|
||||||
|
"github.com/zalando/postgres-operator/pkg/spec"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -12,9 +14,55 @@ var (
|
||||||
False = false
|
False = false
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMergeDeprecatedPostgreSQLSpecParameters(t *testing.T) {
|
func newPostgresqlTestController() *Controller {
|
||||||
c := NewController(&spec.ControllerConfig{})
|
controller := NewController(&spec.ControllerConfig{}, "postgresql-test")
|
||||||
|
return controller
|
||||||
|
}
|
||||||
|
|
||||||
|
var postgresqlTestController = newPostgresqlTestController()
|
||||||
|
|
||||||
|
func TestControllerOwnershipOnPostgresql(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
pg *acidv1.Postgresql
|
||||||
|
owned bool
|
||||||
|
error string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"Postgres cluster with defined ownership of mocked controller",
|
||||||
|
&acidv1.Postgresql{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Annotations: map[string]string{"acid.zalan.do/controller": "postgresql-test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
True,
|
||||||
|
"Postgres cluster should be owned by operator, but controller says no",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Postgres cluster with defined ownership of another controller",
|
||||||
|
&acidv1.Postgresql{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Annotations: map[string]string{"acid.zalan.do/controller": "stups-test"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
False,
|
||||||
|
"Postgres cluster should be owned by another operator, but controller say yes",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Test Postgres cluster without defined ownership",
|
||||||
|
&acidv1.Postgresql{},
|
||||||
|
False,
|
||||||
|
"Postgres cluster should be owned by operator with empty controller ID, but controller says yes",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
if postgresqlTestController.hasOwnership(tt.pg) != tt.owned {
|
||||||
|
t.Errorf("%s: %v", tt.name, tt.error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergeDeprecatedPostgreSQLSpecParameters(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
in *acidv1.PostgresSpec
|
in *acidv1.PostgresSpec
|
||||||
|
|
@ -36,7 +84,7 @@ func TestMergeDeprecatedPostgreSQLSpecParameters(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
result := c.mergeDeprecatedPostgreSQLSpecParameters(tt.in)
|
result := postgresqlTestController.mergeDeprecatedPostgreSQLSpecParameters(tt.in)
|
||||||
if !reflect.DeepEqual(result, tt.out) {
|
if !reflect.DeepEqual(result, tt.out) {
|
||||||
t.Errorf("%s: %v", tt.name, tt.error)
|
t.Errorf("%s: %v", tt.name, tt.error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ const (
|
||||||
testInfrastructureRolesSecretName = "infrastructureroles-test"
|
testInfrastructureRolesSecretName = "infrastructureroles-test"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newMockController() *Controller {
|
func newUtilTestController() *Controller {
|
||||||
controller := NewController(&spec.ControllerConfig{})
|
controller := NewController(&spec.ControllerConfig{}, "util-test")
|
||||||
controller.opConfig.ClusterNameLabel = "cluster-name"
|
controller.opConfig.ClusterNameLabel = "cluster-name"
|
||||||
controller.opConfig.InfrastructureRolesSecretName =
|
controller.opConfig.InfrastructureRolesSecretName =
|
||||||
spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: testInfrastructureRolesSecretName}
|
spec.NamespacedName{Namespace: v1.NamespaceDefault, Name: testInfrastructureRolesSecretName}
|
||||||
|
|
@ -27,7 +27,7 @@ func newMockController() *Controller {
|
||||||
return controller
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
var mockController = newMockController()
|
var utilTestController = newUtilTestController()
|
||||||
|
|
||||||
func TestPodClusterName(t *testing.T) {
|
func TestPodClusterName(t *testing.T) {
|
||||||
var testTable = []struct {
|
var testTable = []struct {
|
||||||
|
|
@ -43,7 +43,7 @@ func TestPodClusterName(t *testing.T) {
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Namespace: v1.NamespaceDefault,
|
Namespace: v1.NamespaceDefault,
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
mockController.opConfig.ClusterNameLabel: "testcluster",
|
utilTestController.opConfig.ClusterNameLabel: "testcluster",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -51,7 +51,7 @@ func TestPodClusterName(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testTable {
|
for _, test := range testTable {
|
||||||
resp := mockController.podClusterName(test.in)
|
resp := utilTestController.podClusterName(test.in)
|
||||||
if resp != test.expected {
|
if resp != test.expected {
|
||||||
t.Errorf("expected response %v does not match the actual %v", test.expected, resp)
|
t.Errorf("expected response %v does not match the actual %v", test.expected, resp)
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +73,7 @@ func TestClusterWorkerID(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testTable {
|
for _, test := range testTable {
|
||||||
resp := mockController.clusterWorkerID(test.in)
|
resp := utilTestController.clusterWorkerID(test.in)
|
||||||
if resp != test.expected {
|
if resp != test.expected {
|
||||||
t.Errorf("expected response %v does not match the actual %v", test.expected, resp)
|
t.Errorf("expected response %v does not match the actual %v", test.expected, resp)
|
||||||
}
|
}
|
||||||
|
|
@ -116,7 +116,7 @@ func TestGetInfrastructureRoles(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testTable {
|
for _, test := range testTable {
|
||||||
roles, err := mockController.getInfrastructureRoles(&test.secretName)
|
roles, err := utilTestController.getInfrastructureRoles(&test.secretName)
|
||||||
if err != test.expectedError {
|
if err != test.expectedError {
|
||||||
if err != nil && test.expectedError != nil && err.Error() == test.expectedError.Error() {
|
if err != nil && test.expectedError != nil && err.Error() == test.expectedError.Error() {
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue