Skip to content

Commit 589c041

Browse files
Fahim Abrarasauber
authored andcommitted
Add test for LoadBalancer Service with 2 ports
1 parent 5ef2dde commit 589c041

File tree

2 files changed

+104
-11
lines changed

2 files changed

+104
-11
lines changed

e2e/test/ccm_e2e_test.go

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package test
22

33
import (
44
"e2e_test/test/framework"
5-
"fmt"
65
"github.com/appscode/go/wait"
76
"github.com/codeskyblue/go-sh"
87
. "github.com/onsi/ginkgo"
98
. "github.com/onsi/gomega"
109
core "k8s.io/api/core/v1"
1110
"k8s.io/apimachinery/pkg/util/intstr"
11+
"log"
1212
"strings"
1313
)
1414

@@ -69,7 +69,7 @@ var _ = Describe("CloudControllerManager", func() {
6969
Expect(err).NotTo(HaveOccurred())
7070
}
7171

72-
PDescribe("Test", func() {
72+
Describe("Test", func() {
7373
Context("Simple", func() {
7474
Context("Load Balancer", func() {
7575
var (
@@ -128,10 +128,10 @@ var _ = Describe("CloudControllerManager", func() {
128128
}
129129
stringResp := string(resp)
130130
if strings.Contains(stringResp, pods[0]) {
131-
fmt.Println("Got response from " + pods[0])
131+
log.Println("Got response from " + pods[0])
132132
counter1++
133133
} else if strings.Contains(stringResp, pods[1]) {
134-
fmt.Println("Got response from " + pods[1])
134+
log.Println("Got response from " + pods[1])
135135
counter2++
136136
}
137137

@@ -215,7 +215,7 @@ var _ = Describe("CloudControllerManager", func() {
215215
})
216216
})
217217

218-
Context("With Multiple TLS Port", func() {
218+
Context("With Multiple TLS Ports", func() {
219219
var (
220220
pods []string
221221
labels map[string]string
@@ -297,7 +297,71 @@ var _ = Describe("CloudControllerManager", func() {
297297
}
298298
})
299299
})
300+
301+
Context("With Multiple HTTP Ports", func() {
302+
var (
303+
pods []string
304+
labels map[string]string
305+
)
306+
307+
BeforeEach(func() {
308+
pods = []string{"test-pod"}
309+
ports := []core.ContainerPort{
310+
{
311+
Name: "http-1",
312+
ContainerPort: 8080,
313+
},
314+
{
315+
Name: "http-2",
316+
ContainerPort: 8989,
317+
},
318+
}
319+
servicePorts := []core.ServicePort{
320+
{
321+
Name: "http-1",
322+
Port: 80,
323+
TargetPort: intstr.FromInt(8080),
324+
Protocol: "TCP",
325+
},
326+
{
327+
Name: "http-2",
328+
Port: 8888,
329+
TargetPort: intstr.FromInt(8989),
330+
Protocol: "TCP",
331+
},
332+
}
333+
labels = map[string]string{
334+
"app": "test-loadbalancer",
335+
}
336+
337+
By("Creating Pods")
338+
createPodWithLabel(pods, ports, framework.TestServerImage, labels, true)
339+
340+
By("Creating Service")
341+
createServiceWithSelector(labels, servicePorts)
342+
})
343+
344+
AfterEach(func() {
345+
By("Deleting the Pods")
346+
deletePods(pods)
347+
348+
By("Deleting the Service")
349+
deleteService()
350+
})
351+
352+
It("should reach all pods", func() {
353+
By("Checking TCP Response")
354+
eps, err := f.LoadBalancer.GetHTTPEndpoints()
355+
Expect(err).NotTo(HaveOccurred())
356+
Expect(len(eps)).Should(BeNumerically(">=", 1))
357+
358+
By("Waiting for Response from the LoadBalancer url: " + eps[0] + " " + eps[1])
359+
for _, ep := range eps {
360+
err = framework.WaitForHTTPResponse(ep, pods[0])
361+
Expect(err).NotTo(HaveOccurred())
362+
}
363+
})
364+
})
300365
})
301366
})
302-
303367
})

e2e/test/framework/util.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7-
"fmt"
87
"github.com/appscode/go/wait"
98
"github.com/golang/glog"
109
"io/ioutil"
@@ -115,7 +114,7 @@ func getHTTPSResponse(domain, ip, port string) (string, error) {
115114
}
116115
client := &http.Client{Transport: tr}
117116

118-
fmt.Println("Waiting for response from https://" + ip + ":" + port)
117+
log.Println("Waiting for response from https://" + ip + ":" + port)
119118
u := "https://" + domain + ":" + port
120119
req, err := http.NewRequest(http.MethodGet, u, nil)
121120
if err != nil {
@@ -136,9 +135,9 @@ func getHTTPSResponse(domain, ip, port string) (string, error) {
136135
return bodyString, nil
137136
}
138137

139-
func WaitForHTTPSResponse(ep string, podName string) error {
138+
func WaitForHTTPSResponse(link string, podName string) error {
140139
return wait.PollImmediate(RetryInterval, RetryTimout, func() (bool, error) {
141-
u, err := url.Parse(ep)
140+
u, err := url.Parse(link)
142141
if err != nil {
143142
return false, nil
144143
}
@@ -150,7 +149,37 @@ func WaitForHTTPSResponse(ep string, podName string) error {
150149
}
151150

152151
if strings.Contains(resp, podName) {
153-
fmt.Println("Got response from " + podName + " using url " + ep)
152+
log.Println("Got response from " + podName + " using url " + link)
153+
return true, nil
154+
}
155+
156+
return false, nil
157+
})
158+
}
159+
160+
func getHTTPResponse(link string) (bool, string, error) {
161+
resp, err := http.Get(link)
162+
if err != nil {
163+
return false, "", err
164+
}
165+
defer resp.Body.Close()
166+
167+
bodyBytes, err := ioutil.ReadAll(resp.Body)
168+
if err != nil {
169+
return false, "", err
170+
}
171+
172+
return resp.StatusCode == 200, string(bodyBytes), nil
173+
}
174+
175+
func WaitForHTTPResponse(link string, podName string) error {
176+
return wait.PollImmediate(RetryInterval, RetryTimout, func() (bool, error) {
177+
ok, _, err := getHTTPResponse(link)
178+
if err != nil {
179+
return false, nil
180+
}
181+
if ok {
182+
log.Println("Got response from " + podName + " using url " + link)
154183
return true, nil
155184
}
156185

0 commit comments

Comments
 (0)