Skip to content

Commit 259693b

Browse files
authored
Merge pull request #24 from appscode-cloud/lka4
Add Test for LoadBalancer Service with 2 Ports
2 parents 3188c64 + 464896c commit 259693b

File tree

6 files changed

+359
-72
lines changed

6 files changed

+359
-72
lines changed

e2e/test/ccm_e2e_test.go

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

33
import (
44
"e2e_test/test/framework"
5-
"fmt"
5+
"log"
6+
"strings"
7+
68
"github.com/appscode/go/wait"
79
"github.com/codeskyblue/go-sh"
810
. "github.com/onsi/ginkgo"
911
. "github.com/onsi/gomega"
10-
"net/url"
11-
"strings"
12+
core "k8s.io/api/core/v1"
13+
"k8s.io/apimachinery/pkg/util/intstr"
1214
)
1315

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

33-
var createPodWithLabel = func(pods []string, labels map[string]string) {
35+
var createPodWithLabel = func(pods []string, ports []core.ContainerPort, image string, labels map[string]string, selectNode bool) {
3436
for i, pod := range pods {
35-
p := f.LoadBalancer.GetPodObject(pod, workers[i], labels)
37+
p := f.LoadBalancer.GetPodObject(pod, image, ports, labels)
38+
if selectNode {
39+
p = f.LoadBalancer.SetNodeSelector(p, workers[i])
40+
}
3641
err = f.LoadBalancer.CreatePod(p)
3742
Expect(err).NotTo(HaveOccurred())
3843
}
@@ -50,18 +55,18 @@ var _ = Describe("CloudControllerManager", func() {
5055
Expect(err).NotTo(HaveOccurred())
5156
}
5257

53-
var deleteSecret = func() {
54-
err = f.LoadBalancer.DeleteSecret()
58+
var deleteSecret = func(name string) {
59+
err = f.LoadBalancer.DeleteSecret(name)
5560
Expect(err).NotTo(HaveOccurred())
5661
}
5762

58-
var createServiceWithSelector = func(selector map[string]string) {
59-
err = f.LoadBalancer.CreateService(selector, nil)
63+
var createServiceWithSelector = func(selector map[string]string, ports []core.ServicePort) {
64+
err = f.LoadBalancer.CreateService(selector, nil, ports)
6065
Expect(err).NotTo(HaveOccurred())
6166
}
6267

63-
var createServiceWithAnnotations = func(labels map[string]string, annotations map[string]string) {
64-
err = f.LoadBalancer.CreateService(labels, annotations)
68+
var createServiceWithAnnotations = func(labels map[string]string, annotations map[string]string, ports []core.ServicePort) {
69+
err = f.LoadBalancer.CreateService(labels, annotations, ports)
6570
Expect(err).NotTo(HaveOccurred())
6671
}
6772

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

7681
BeforeEach(func() {
7782
pods = []string{"test-pod-1", "test-pod-2"}
83+
ports := []core.ContainerPort{
84+
{
85+
Name: "http-1",
86+
ContainerPort: 8080,
87+
},
88+
}
89+
servicePorts := []core.ServicePort{
90+
{
91+
Name: "http-1",
92+
Port: 80,
93+
TargetPort: intstr.FromInt(8080),
94+
Protocol: "TCP",
95+
},
96+
}
7897
labels = map[string]string{
7998
"app": "test-loadbalancer",
8099
}
81100

82101
By("Creating Pods")
83-
createPodWithLabel(pods, labels)
102+
createPodWithLabel(pods, ports, framework.TestServerImage, labels, true)
84103

85104
By("Creating Service")
86-
createServiceWithSelector(labels)
105+
createServiceWithSelector(labels, servicePorts)
87106
})
88107

89108
AfterEach(func() {
@@ -110,10 +129,10 @@ var _ = Describe("CloudControllerManager", func() {
110129
}
111130
stringResp := string(resp)
112131
if strings.Contains(stringResp, pods[0]) {
113-
fmt.Println("Got response from " + pods[0])
132+
log.Println("Got response from " + pods[0])
114133
counter1++
115134
} else if strings.Contains(stringResp, pods[1]) {
116-
fmt.Println("Got response from " + pods[1])
135+
log.Println("Got response from " + pods[1])
117136
counter2++
118137
}
119138

@@ -139,7 +158,21 @@ var _ = Describe("CloudControllerManager", func() {
139158
secretName string
140159
)
141160
BeforeEach(func() {
142-
pods = []string{"test-pod"}
161+
pods = []string{"test-single-port-pod"}
162+
ports := []core.ContainerPort{
163+
{
164+
Name: "https",
165+
ContainerPort: 8080,
166+
},
167+
}
168+
servicePorts := []core.ServicePort{
169+
{
170+
Name: "https",
171+
Port: 80,
172+
TargetPort: intstr.FromInt(8080),
173+
Protocol: "TCP",
174+
},
175+
}
143176
secretName = "tls-secret"
144177
labels = map[string]string{
145178
"app": "test-loadbalancer",
@@ -150,14 +183,14 @@ var _ = Describe("CloudControllerManager", func() {
150183
}
151184

152185
By("Creating Pod")
153-
createPodWithLabel(pods, labels)
186+
createPodWithLabel(pods, ports, framework.TestServerImage, labels, false)
154187

155188
By("Creating Secret")
156-
err = f.LoadBalancer.CreateTLSSecret("tls-secret", "linode.test")
189+
err = f.LoadBalancer.CreateTLSSecret("tls-secret")
157190
Expect(err).NotTo(HaveOccurred())
158191

159192
By("Creating Service")
160-
createServiceWithAnnotations(labels, annotations)
193+
createServiceWithAnnotations(labels, annotations, servicePorts)
161194
})
162195

163196
AfterEach(func() {
@@ -168,7 +201,7 @@ var _ = Describe("CloudControllerManager", func() {
168201
deleteService()
169202

170203
By("Deleting the Secret")
171-
deleteSecret()
204+
deleteSecret(secretName)
172205
})
173206

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

180-
var counter int
181-
182213
By("Waiting for Response from the LoadBalancer url: " + eps[0])
183-
err = wait.PollImmediate(framework.RetryInterval, framework.RetryTimout, func() (bool, error) {
184-
u, err := url.Parse(eps[0])
185-
if err != nil {
186-
return false, nil
187-
}
188-
ipPort := strings.Split(u.Host, ":")
214+
err = framework.WaitForHTTPSResponse(eps[0], pods[0])
215+
Expect(err).NotTo(HaveOccurred())
216+
})
217+
})
189218

190-
session := sh.NewSession()
191-
session.ShowCMD = true
192-
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()
193-
stringResp := string(resp)
194-
if err != nil {
195-
return false, nil
196-
}
219+
Context("With Multiple TLS Ports", func() {
220+
var (
221+
pods []string
222+
labels map[string]string
223+
annotations map[string]string
224+
secretName1 string
225+
secretName2 string
226+
)
227+
BeforeEach(func() {
228+
pods = []string{"tls-multi-port-pod"}
229+
secretName1 = "tls-secret-1"
230+
secretName2 = "tls-secret-2"
231+
labels = map[string]string{
232+
"app": "test-loadbalancer",
233+
}
234+
annotations = map[string]string{
235+
annLinodeLoadBalancerTLS: `[ { "tls-secret-name": "` + secretName1 + `", "port": 80}, {"tls-secret-name": "` + secretName2 + `", "port": 443}]`,
236+
annLinodeProtocol: "https",
237+
}
238+
ports := []core.ContainerPort{
239+
{
240+
Name: "https1",
241+
ContainerPort: 8080,
242+
},
243+
{
244+
Name: "https2",
245+
ContainerPort: 8989,
246+
},
247+
}
248+
servicePorts := []core.ServicePort{
249+
{
250+
Name: "https-1",
251+
Port: 80,
252+
TargetPort: intstr.FromInt(8080),
253+
Protocol: "TCP",
254+
},
255+
{
256+
Name: "https-2",
257+
Port: 443,
258+
TargetPort: intstr.FromInt(8989),
259+
Protocol: "TCP",
260+
},
261+
}
197262

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

203-
if counter > 0 {
204-
return true, nil
205-
}
206-
return false, nil
207-
})
208-
Expect(counter).Should(BeNumerically(">", 0))
266+
By("Creating Secret")
267+
err = f.LoadBalancer.CreateTLSSecret(secretName1)
268+
Expect(err).NotTo(HaveOccurred())
269+
err = f.LoadBalancer.CreateTLSSecret(secretName2)
270+
Expect(err).NotTo(HaveOccurred())
271+
272+
By("Creating Service")
273+
createServiceWithAnnotations(labels, annotations, servicePorts)
274+
})
275+
276+
AfterEach(func() {
277+
By("Deleting the Secrets")
278+
deletePods(pods)
279+
280+
By("Deleting the Service")
281+
deleteService()
282+
283+
By("Deleting the Secret")
284+
deleteSecret(secretName1)
285+
deleteSecret(secretName2)
286+
})
287+
288+
It("should reach the pod via tls", func() {
289+
By("Checking TCP Response")
290+
eps, err := f.LoadBalancer.GetHTTPEndpoints()
291+
Expect(err).NotTo(HaveOccurred())
292+
Expect(len(eps)).Should(BeNumerically(">=", 1))
293+
294+
By("Waiting for Response from the LoadBalancer urls: " + eps[0] + ", " + eps[1])
295+
for _, ep := range eps {
296+
err = framework.WaitForHTTPSResponse(ep, pods[0])
297+
Expect(err).NotTo(HaveOccurred())
298+
}
299+
})
300+
})
301+
302+
Context("With Multiple HTTP Ports", func() {
303+
var (
304+
pods []string
305+
labels map[string]string
306+
)
307+
308+
BeforeEach(func() {
309+
pods = []string{"test-pod"}
310+
ports := []core.ContainerPort{
311+
{
312+
Name: "http-1",
313+
ContainerPort: 8080,
314+
},
315+
{
316+
Name: "http-2",
317+
ContainerPort: 8989,
318+
},
319+
}
320+
servicePorts := []core.ServicePort{
321+
{
322+
Name: "http-1",
323+
Port: 80,
324+
TargetPort: intstr.FromInt(8080),
325+
Protocol: "TCP",
326+
},
327+
{
328+
Name: "http-2",
329+
Port: 8888,
330+
TargetPort: intstr.FromInt(8989),
331+
Protocol: "TCP",
332+
},
333+
}
334+
labels = map[string]string{
335+
"app": "test-loadbalancer",
336+
}
337+
338+
By("Creating Pods")
339+
createPodWithLabel(pods, ports, framework.TestServerImage, labels, true)
340+
341+
By("Creating Service")
342+
createServiceWithSelector(labels, servicePorts)
343+
})
344+
345+
AfterEach(func() {
346+
By("Deleting the Pods")
347+
deletePods(pods)
348+
349+
By("Deleting the Service")
350+
deleteService()
351+
})
352+
353+
It("should reach all pods", func() {
354+
By("Checking TCP Response")
355+
eps, err := f.LoadBalancer.GetHTTPEndpoints()
356+
Expect(err).NotTo(HaveOccurred())
357+
Expect(len(eps)).Should(BeNumerically(">=", 1))
358+
359+
By("Waiting for Response from the LoadBalancer url: " + eps[0] + " " + eps[1])
360+
for _, ep := range eps {
361+
err = framework.WaitForHTTPResponse(ep, pods[0])
362+
Expect(err).NotTo(HaveOccurred())
363+
}
209364
})
210365
})
211366
})
212367
})
213-
214368
})

e2e/test/framework/framework.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
const (
1818
MaxRetry = 100
19-
testServerImage = "appscode/test-server:2.3"
19+
TestServerImage = "appscode/test-server:2.3"
2020
)
2121

2222
type Framework struct {

e2e/test/framework/pod.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"k8s.io/apimachinery/pkg/util/wait"
88
)
99

10-
func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]string) *core.Pod {
10+
func (i *lbInvocation) GetPodObject(podName, image string, ports []core.ContainerPort, labels map[string]string) *core.Pod {
1111
return &core.Pod{
1212
ObjectMeta: metav1.ObjectMeta{
1313
Name: podName,
@@ -18,7 +18,7 @@ func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]
1818
Containers: []core.Container{
1919
{
2020
Name: "server",
21-
Image: testServerImage,
21+
Image: image,
2222
Env: []core.EnvVar{
2323
{
2424
Name: "POD_NAME",
@@ -29,21 +29,20 @@ func (i *lbInvocation) GetPodObject(podName, nodeName string, labels map[string]
2929
},
3030
},
3131
},
32-
Ports: []core.ContainerPort{
33-
{
34-
Name: "http-1",
35-
ContainerPort: 8080,
36-
},
37-
},
32+
Ports: ports,
3833
},
3934
},
40-
NodeSelector: map[string]string{
41-
"kubernetes.io/hostname": nodeName,
42-
},
4335
},
4436
}
4537
}
4638

39+
func (i *lbInvocation) SetNodeSelector(pod *core.Pod, nodeName string) *core.Pod {
40+
pod.Spec.NodeSelector = map[string]string{
41+
"kubernetes.io/hostname": nodeName,
42+
}
43+
return pod
44+
}
45+
4746
func (i *lbInvocation) CreatePod(pod *core.Pod) error {
4847
pod, err := i.kubeClient.CoreV1().Pods(i.Namespace()).Create(pod)
4948
if err != nil {

0 commit comments

Comments
 (0)