@@ -864,6 +864,136 @@ func TestGenerateSpiloPodEnvVars(t *testing.T) {
864864 }
865865}
866866
867+ func TestGetNumberOfInstances (t * testing.T ) {
868+ testName := "TestGetNumberOfInstances"
869+ tests := []struct {
870+ subTest string
871+ config config.Config
872+ annotationKey string
873+ annotationValue string
874+ desired int32
875+ provided int32
876+ }{
877+ {
878+ subTest : "no constraints" ,
879+ config : config.Config {
880+ Resources : config.Resources {
881+ MinInstances : - 1 ,
882+ MaxInstances : - 1 ,
883+ IgnoreInstanceLimitsAnnotationKey : "" ,
884+ },
885+ },
886+ annotationKey : "" ,
887+ annotationValue : "" ,
888+ desired : 2 ,
889+ provided : 2 ,
890+ },
891+ {
892+ subTest : "minInstances defined" ,
893+ config : config.Config {
894+ Resources : config.Resources {
895+ MinInstances : 2 ,
896+ MaxInstances : - 1 ,
897+ IgnoreInstanceLimitsAnnotationKey : "" ,
898+ },
899+ },
900+ annotationKey : "" ,
901+ annotationValue : "" ,
902+ desired : 1 ,
903+ provided : 2 ,
904+ },
905+ {
906+ subTest : "maxInstances defined" ,
907+ config : config.Config {
908+ Resources : config.Resources {
909+ MinInstances : - 1 ,
910+ MaxInstances : 5 ,
911+ IgnoreInstanceLimitsAnnotationKey : "" ,
912+ },
913+ },
914+ annotationKey : "" ,
915+ annotationValue : "" ,
916+ desired : 10 ,
917+ provided : 5 ,
918+ },
919+ {
920+ subTest : "ignore minInstances" ,
921+ config : config.Config {
922+ Resources : config.Resources {
923+ MinInstances : 2 ,
924+ MaxInstances : - 1 ,
925+ IgnoreInstanceLimitsAnnotationKey : "ignore-instance-limits" ,
926+ },
927+ },
928+ annotationKey : "ignore-instance-limits" ,
929+ annotationValue : "true" ,
930+ desired : 1 ,
931+ provided : 1 ,
932+ },
933+ {
934+ subTest : "want to ignore minInstances but wrong key" ,
935+ config : config.Config {
936+ Resources : config.Resources {
937+ MinInstances : 2 ,
938+ MaxInstances : - 1 ,
939+ IgnoreInstanceLimitsAnnotationKey : "ignore-instance-limits" ,
940+ },
941+ },
942+ annotationKey : "ignoring-instance-limits" ,
943+ annotationValue : "true" ,
944+ desired : 1 ,
945+ provided : 2 ,
946+ },
947+ {
948+ subTest : "want to ignore minInstances but wrong value" ,
949+ config : config.Config {
950+ Resources : config.Resources {
951+ MinInstances : 2 ,
952+ MaxInstances : - 1 ,
953+ IgnoreInstanceLimitsAnnotationKey : "ignore-instance-limits" ,
954+ },
955+ },
956+ annotationKey : "ignore-instance-limits" ,
957+ annotationValue : "active" ,
958+ desired : 1 ,
959+ provided : 2 ,
960+ },
961+ {
962+ subTest : "annotation set but no constraints to ignore" ,
963+ config : config.Config {
964+ Resources : config.Resources {
965+ MinInstances : - 1 ,
966+ MaxInstances : - 1 ,
967+ IgnoreInstanceLimitsAnnotationKey : "ignore-instance-limits" ,
968+ },
969+ },
970+ annotationKey : "ignore-instance-limits" ,
971+ annotationValue : "true" ,
972+ desired : 1 ,
973+ provided : 1 ,
974+ },
975+ }
976+
977+ for _ , tt := range tests {
978+ var cluster = New (
979+ Config {
980+ OpConfig : tt .config ,
981+ }, k8sutil.KubernetesClient {}, acidv1.Postgresql {}, logger , eventRecorder )
982+
983+ cluster .Spec .NumberOfInstances = tt .desired
984+ if tt .annotationKey != "" {
985+ cluster .ObjectMeta .Annotations = make (map [string ]string )
986+ cluster .ObjectMeta .Annotations [tt .annotationKey ] = tt .annotationValue
987+ }
988+ numInstances := cluster .getNumberOfInstances (& cluster .Spec )
989+
990+ if numInstances != tt .provided {
991+ t .Errorf ("%s %s: Expected to get %d instances, have %d instead" ,
992+ testName , tt .subTest , tt .provided , numInstances )
993+ }
994+ }
995+ }
996+
867997func TestCloneEnv (t * testing.T ) {
868998 testName := "TestCloneEnv"
869999 tests := []struct {
0 commit comments