Skip to content

Commit

Permalink
test: make sure we test port correctness for services (cloudnative-pg…
Browse files Browse the repository at this point in the history
…#4934)

Signed-off-by: Abhishek Chanda <[email protected]>
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Co-authored-by: Marco Nenciarini <[email protected]>
Co-authored-by: Armando Ruocco <[email protected]>
(cherry picked from commit fd62a1c)
  • Loading branch information
achanda authored and mnencia committed Dec 7, 2024
1 parent 2eef24b commit afcfaf3
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/specs/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package specs

import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -32,13 +35,21 @@ var _ = Describe("Services specification", func() {
Name: "clustername",
},
}
expectedPort := corev1.ServicePort{
Name: PostgresContainerName,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt32(postgres.ServerPort),
Port: postgres.ServerPort,
}

It("create a configured -any service", func() {
service := CreateClusterAnyService(postgresql)
Expect(service.Name).To(Equal("clustername-any"))
Expect(service.Spec.PublishNotReadyAddresses).To(BeTrue())
Expect(service.Spec.Selector[utils.ClusterLabelName]).To(Equal("clustername"))
Expect(service.Spec.Selector[utils.PodRoleLabelName]).To(Equal(string(utils.PodRoleInstance)))
Expect(service.Spec.Ports).To(HaveLen(1))
Expect(service.Spec.Ports).To(ContainElement(expectedPort))
})

It("create a configured -r service", func() {
Expand All @@ -47,6 +58,8 @@ var _ = Describe("Services specification", func() {
Expect(service.Spec.PublishNotReadyAddresses).To(BeFalse())
Expect(service.Spec.Selector[utils.ClusterLabelName]).To(Equal("clustername"))
Expect(service.Spec.Selector[utils.PodRoleLabelName]).To(Equal(string(utils.PodRoleInstance)))
Expect(service.Spec.Ports).To(HaveLen(1))
Expect(service.Spec.Ports).To(ContainElement(expectedPort))
})

It("create a configured -ro service", func() {
Expand All @@ -55,6 +68,8 @@ var _ = Describe("Services specification", func() {
Expect(service.Spec.PublishNotReadyAddresses).To(BeFalse())
Expect(service.Spec.Selector[utils.ClusterLabelName]).To(Equal("clustername"))
Expect(service.Spec.Selector[utils.ClusterRoleLabelName]).To(Equal(ClusterRoleLabelReplica))
Expect(service.Spec.Ports).To(HaveLen(1))
Expect(service.Spec.Ports).To(ContainElement(expectedPort))
})

It("create a configured -rw service", func() {
Expand All @@ -63,5 +78,7 @@ var _ = Describe("Services specification", func() {
Expect(service.Spec.PublishNotReadyAddresses).To(BeFalse())
Expect(service.Spec.Selector[utils.ClusterLabelName]).To(Equal("clustername"))
Expect(service.Spec.Selector[utils.ClusterRoleLabelName]).To(Equal(ClusterRoleLabelPrimary))
Expect(service.Spec.Ports).To(HaveLen(1))
Expect(service.Spec.Ports).To(ContainElement(expectedPort))
})
})

0 comments on commit afcfaf3

Please sign in to comment.