Skip to content

Commit

Permalink
Merge pull request #24 from appscode-cloud/lka4
Browse files Browse the repository at this point in the history
 Add Test for LoadBalancer Service with 2 Ports
  • Loading branch information
asauber authored Apr 16, 2019
2 parents 3188c64 + 464896c commit 259693b
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 72 deletions.
246 changes: 200 additions & 46 deletions e2e/test/ccm_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package test

import (
"e2e_test/test/framework"
"fmt"
"log"
"strings"

"github.com/appscode/go/wait"
"github.com/codeskyblue/go-sh"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"net/url"
"strings"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

var _ = Describe("CloudControllerManager", func() {
Expand All @@ -30,9 +32,12 @@ var _ = Describe("CloudControllerManager", func() {
Expect(len(workers)).Should(BeNumerically(">=", 2))
})

var createPodWithLabel = func(pods []string, labels map[string]string) {
var createPodWithLabel = func(pods []string, ports []core.ContainerPort, image string, labels map[string]string, selectNode bool) {
for i, pod := range pods {
p := f.LoadBalancer.GetPodObject(pod, workers[i], labels)
p := f.LoadBalancer.GetPodObject(pod, image, ports, labels)
if selectNode {
p = f.LoadBalancer.SetNodeSelector(p, workers[i])
}
err = f.LoadBalancer.CreatePod(p)
Expect(err).NotTo(HaveOccurred())
}
Expand All @@ -50,18 +55,18 @@ var _ = Describe("CloudControllerManager", func() {
Expect(err).NotTo(HaveOccurred())
}

var deleteSecret = func() {
err = f.LoadBalancer.DeleteSecret()
var deleteSecret = func(name string) {
err = f.LoadBalancer.DeleteSecret(name)
Expect(err).NotTo(HaveOccurred())
}

var createServiceWithSelector = func(selector map[string]string) {
err = f.LoadBalancer.CreateService(selector, nil)
var createServiceWithSelector = func(selector map[string]string, ports []core.ServicePort) {
err = f.LoadBalancer.CreateService(selector, nil, ports)
Expect(err).NotTo(HaveOccurred())
}

var createServiceWithAnnotations = func(labels map[string]string, annotations map[string]string) {
err = f.LoadBalancer.CreateService(labels, annotations)
var createServiceWithAnnotations = func(labels map[string]string, annotations map[string]string, ports []core.ServicePort) {
err = f.LoadBalancer.CreateService(labels, annotations, ports)
Expect(err).NotTo(HaveOccurred())
}

Expand All @@ -75,15 +80,29 @@ var _ = Describe("CloudControllerManager", func() {

BeforeEach(func() {
pods = []string{"test-pod-1", "test-pod-2"}
ports := []core.ContainerPort{
{
Name: "http-1",
ContainerPort: 8080,
},
}
servicePorts := []core.ServicePort{
{
Name: "http-1",
Port: 80,
TargetPort: intstr.FromInt(8080),
Protocol: "TCP",
},
}
labels = map[string]string{
"app": "test-loadbalancer",
}

By("Creating Pods")
createPodWithLabel(pods, labels)
createPodWithLabel(pods, ports, framework.TestServerImage, labels, true)

By("Creating Service")
createServiceWithSelector(labels)
createServiceWithSelector(labels, servicePorts)
})

AfterEach(func() {
Expand All @@ -110,10 +129,10 @@ var _ = Describe("CloudControllerManager", func() {
}
stringResp := string(resp)
if strings.Contains(stringResp, pods[0]) {
fmt.Println("Got response from " + pods[0])
log.Println("Got response from " + pods[0])
counter1++
} else if strings.Contains(stringResp, pods[1]) {
fmt.Println("Got response from " + pods[1])
log.Println("Got response from " + pods[1])
counter2++
}

Expand All @@ -139,7 +158,21 @@ var _ = Describe("CloudControllerManager", func() {
secretName string
)
BeforeEach(func() {
pods = []string{"test-pod"}
pods = []string{"test-single-port-pod"}
ports := []core.ContainerPort{
{
Name: "https",
ContainerPort: 8080,
},
}
servicePorts := []core.ServicePort{
{
Name: "https",
Port: 80,
TargetPort: intstr.FromInt(8080),
Protocol: "TCP",
},
}
secretName = "tls-secret"
labels = map[string]string{
"app": "test-loadbalancer",
Expand All @@ -150,14 +183,14 @@ var _ = Describe("CloudControllerManager", func() {
}

By("Creating Pod")
createPodWithLabel(pods, labels)
createPodWithLabel(pods, ports, framework.TestServerImage, labels, false)

By("Creating Secret")
err = f.LoadBalancer.CreateTLSSecret("tls-secret", "linode.test")
err = f.LoadBalancer.CreateTLSSecret("tls-secret")
Expect(err).NotTo(HaveOccurred())

By("Creating Service")
createServiceWithAnnotations(labels, annotations)
createServiceWithAnnotations(labels, annotations, servicePorts)
})

AfterEach(func() {
Expand All @@ -168,7 +201,7 @@ var _ = Describe("CloudControllerManager", func() {
deleteService()

By("Deleting the Secret")
deleteSecret()
deleteSecret(secretName)
})

It("should reach the pod via tls", func() {
Expand All @@ -177,38 +210,159 @@ var _ = Describe("CloudControllerManager", func() {
Expect(err).NotTo(HaveOccurred())
Expect(len(eps)).Should(BeNumerically(">=", 1))

var counter int

By("Waiting for Response from the LoadBalancer url: " + eps[0])
err = wait.PollImmediate(framework.RetryInterval, framework.RetryTimout, func() (bool, error) {
u, err := url.Parse(eps[0])
if err != nil {
return false, nil
}
ipPort := strings.Split(u.Host, ":")
err = framework.WaitForHTTPSResponse(eps[0], pods[0])
Expect(err).NotTo(HaveOccurred())
})
})

session := sh.NewSession()
session.ShowCMD = true
resp, err := session.Command("curl", "--max-time", "5", "--resolve", "linode.test:"+ipPort[1]+":"+ipPort[0]+"", "--cacert", "certificates/ca.crt", "https://linode.test:80", "-s").Output()
stringResp := string(resp)
if err != nil {
return false, nil
}
Context("With Multiple TLS Ports", func() {
var (
pods []string
labels map[string]string
annotations map[string]string
secretName1 string
secretName2 string
)
BeforeEach(func() {
pods = []string{"tls-multi-port-pod"}
secretName1 = "tls-secret-1"
secretName2 = "tls-secret-2"
labels = map[string]string{
"app": "test-loadbalancer",
}
annotations = map[string]string{
annLinodeLoadBalancerTLS: `[ { "tls-secret-name": "` + secretName1 + `", "port": 80}, {"tls-secret-name": "` + secretName2 + `", "port": 443}]`,
annLinodeProtocol: "https",
}
ports := []core.ContainerPort{
{
Name: "https1",
ContainerPort: 8080,
},
{
Name: "https2",
ContainerPort: 8989,
},
}
servicePorts := []core.ServicePort{
{
Name: "https-1",
Port: 80,
TargetPort: intstr.FromInt(8080),
Protocol: "TCP",
},
{
Name: "https-2",
Port: 443,
TargetPort: intstr.FromInt(8989),
Protocol: "TCP",
},
}

if strings.Contains(stringResp, pods[0]) {
fmt.Println("Got response from " + pods[0] + " using url " + eps[0])
counter++
}
By("Creating Pod")
createPodWithLabel(pods, ports, framework.TestServerImage, labels, false)

if counter > 0 {
return true, nil
}
return false, nil
})
Expect(counter).Should(BeNumerically(">", 0))
By("Creating Secret")
err = f.LoadBalancer.CreateTLSSecret(secretName1)
Expect(err).NotTo(HaveOccurred())
err = f.LoadBalancer.CreateTLSSecret(secretName2)
Expect(err).NotTo(HaveOccurred())

By("Creating Service")
createServiceWithAnnotations(labels, annotations, servicePorts)
})

AfterEach(func() {
By("Deleting the Secrets")
deletePods(pods)

By("Deleting the Service")
deleteService()

By("Deleting the Secret")
deleteSecret(secretName1)
deleteSecret(secretName2)
})

It("should reach the pod via tls", func() {
By("Checking TCP Response")
eps, err := f.LoadBalancer.GetHTTPEndpoints()
Expect(err).NotTo(HaveOccurred())
Expect(len(eps)).Should(BeNumerically(">=", 1))

By("Waiting for Response from the LoadBalancer urls: " + eps[0] + ", " + eps[1])
for _, ep := range eps {
err = framework.WaitForHTTPSResponse(ep, pods[0])
Expect(err).NotTo(HaveOccurred())
}
})
})

Context("With Multiple HTTP Ports", func() {
var (
pods []string
labels map[string]string
)

BeforeEach(func() {
pods = []string{"test-pod"}
ports := []core.ContainerPort{
{
Name: "http-1",
ContainerPort: 8080,
},
{
Name: "http-2",
ContainerPort: 8989,
},
}
servicePorts := []core.ServicePort{
{
Name: "http-1",
Port: 80,
TargetPort: intstr.FromInt(8080),
Protocol: "TCP",
},
{
Name: "http-2",
Port: 8888,
TargetPort: intstr.FromInt(8989),
Protocol: "TCP",
},
}
labels = map[string]string{
"app": "test-loadbalancer",
}

By("Creating Pods")
createPodWithLabel(pods, ports, framework.TestServerImage, labels, true)

By("Creating Service")
createServiceWithSelector(labels, servicePorts)
})

AfterEach(func() {
By("Deleting the Pods")
deletePods(pods)

By("Deleting the Service")
deleteService()
})

It("should reach all pods", func() {
By("Checking TCP Response")
eps, err := f.LoadBalancer.GetHTTPEndpoints()
Expect(err).NotTo(HaveOccurred())
Expect(len(eps)).Should(BeNumerically(">=", 1))

By("Waiting for Response from the LoadBalancer url: " + eps[0] + " " + eps[1])
for _, ep := range eps {
err = framework.WaitForHTTPResponse(ep, pods[0])
Expect(err).NotTo(HaveOccurred())
}
})
})
})
})

})
2 changes: 1 addition & 1 deletion e2e/test/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (

const (
MaxRetry = 100
testServerImage = "appscode/test-server:2.3"
TestServerImage = "appscode/test-server:2.3"
)

type Framework struct {
Expand Down
21 changes: 10 additions & 11 deletions e2e/test/framework/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
)

func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]string) *core.Pod {
func (i *lbInvocation) GetPodObject(podName, image string, ports []core.ContainerPort, labels map[string]string) *core.Pod {
return &core.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Expand All @@ -18,7 +18,7 @@ func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]
Containers: []core.Container{
{
Name: "server",
Image: testServerImage,
Image: image,
Env: []core.EnvVar{
{
Name: "POD_NAME",
Expand All @@ -29,21 +29,20 @@ func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]
},
},
},
Ports: []core.ContainerPort{
{
Name: "http-1",
ContainerPort: 8080,
},
},
Ports: ports,
},
},
NodeSelector: map[string]string{
"kubernetes.io/hostname": nodeName,
},
},
}
}

func (i *lbInvocation) SetNodeSelector(pod *core.Pod, nodeName string) *core.Pod {
pod.Spec.NodeSelector = map[string]string{
"kubernetes.io/hostname": nodeName,
}
return pod
}

func (i *lbInvocation) CreatePod(pod *core.Pod) error {
pod, err := i.kubeClient.CoreV1().Pods(i.Namespace()).Create(pod)
if err != nil {
Expand Down
Loading

0 comments on commit 259693b

Please sign in to comment.