Skip to content

Commit 9cef9cf

Browse files
draychevakshaysngupta
authored andcommitted
health_probes: Adding unit tests (#162)
1 parent 0101cf8 commit 9cef9cf

File tree

3 files changed

+356
-95
lines changed

3 files changed

+356
-95
lines changed

pkg/appgw/appgw_test.go

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -154,45 +154,7 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
154154
},
155155
}
156156

157-
pod := &v1.Pod{
158-
ObjectMeta: metav1.ObjectMeta{
159-
Name: serviceName,
160-
Namespace: ingressNS,
161-
Labels: map[string]string{
162-
"app": "frontend",
163-
},
164-
},
165-
Spec: v1.PodSpec{
166-
Containers: []v1.Container{
167-
{
168-
Name: serviceName,
169-
Image: "image",
170-
Ports: []v1.ContainerPort{
171-
{
172-
Name: backendName,
173-
ContainerPort: backendPort,
174-
},
175-
},
176-
ReadinessProbe: &v1.Probe{
177-
TimeoutSeconds: 5,
178-
FailureThreshold: 3,
179-
PeriodSeconds: 20,
180-
Handler: v1.Handler{
181-
HTTPGet: &v1.HTTPGetAction{
182-
Host: "bye.com",
183-
Path: "/healthz",
184-
Port: intstr.IntOrString{
185-
Type: intstr.String,
186-
StrVal: backendName,
187-
},
188-
Scheme: v1.URISchemeHTTP,
189-
},
190-
},
191-
},
192-
},
193-
},
194-
},
195-
}
157+
pod := makePodFixture(serviceName, ingressNS, backendName, backendPort)
196158

197159
go_flag.Lookup("logtostderr").Value.Set("true")
198160
go_flag.Set("v", "3")
@@ -216,18 +178,21 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
216178
Name: &probeName,
217179
ApplicationGatewayProbePropertiesFormat: &network.ApplicationGatewayProbePropertiesFormat{
218180
Protocol: network.HTTP,
219-
Host: to.StringPtr("bye.com"),
220-
Path: to.StringPtr("/healthz"),
181+
Host: to.StringPtr(testFixturesHost),
182+
Path: to.StringPtr(testFixturesURLPath),
221183
Interval: to.Int32Ptr(20),
222184
UnhealthyThreshold: to.Int32Ptr(3),
223185
Timeout: to.Int32Ptr(5),
224186
},
225187
}
226188

189+
probes := *appGW.Probes
190+
Expect(len(probes)).To(Equal(2))
191+
227192
// Test the default health probe.
228-
Expect((*appGW.Probes)[0]).To(Equal(defaultProbe()))
193+
Expect(probes).To(ContainElement(defaultProbe()))
229194
// Test the ingress health probe that we installed.
230-
Expect((*appGW.Probes)[1]).To(Equal(*probe))
195+
Expect(probes).To(ContainElement(*probe))
231196
}
232197

233198
defaultBackendHTTPSettingsChecker := func(appGW *network.ApplicationGatewayPropertiesFormat) {
@@ -700,14 +665,14 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
700665
Context("Tests Ingress Controller Annotations", func() {
701666
It("Should be able to create Application Gateway Configuration from Ingress with backend prefix.", func() {
702667
ingress, err := k8sClient.Extensions().Ingresses(ingressNS).Get(ingressName, metav1.GetOptions{})
703-
Expect(err).Should(BeNil(), "Unabled to create ingress resource due to: %v", err)
668+
Expect(err).Should(BeNil(), "Unable to create ingress resource due to: %v", err)
704669

705670
// Set the ingress annotation for this ingress.
706671
ingress.Annotations[annotations.BackendPathPrefixKey] = "/test"
707672

708673
// Update the ingress.
709674
_, err = k8sClient.Extensions().Ingresses(ingressNS).Update(ingress)
710-
Expect(err).Should(BeNil(), "Unabled to update ingress resource due to: %v", err)
675+
Expect(err).Should(BeNil(), "Unable to update ingress resource due to: %v", err)
711676

712677
// Start the informers. This will sync the cache with the latest ingress.
713678
ctxt.Run()
@@ -746,10 +711,15 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
746711
},
747712
}
748713

714+
backendSettings := *appGW.BackendHTTPSettingsCollection
715+
716+
defaultProbe := defaultBackendHTTPSettings(appGwIdentifier.probeID(defaultProbeName))
717+
718+
Expect(len(backendSettings)).To(Equal(2))
749719
// Test the default backend HTTP settings.
750-
Expect((*appGW.BackendHTTPSettingsCollection)[0]).To(Equal(defaultBackendHTTPSettings(appGwIdentifier.probeID(defaultProbeName))))
720+
Expect(backendSettings).To(ContainElement(defaultProbe))
751721
// Test the ingress backend HTTP setting that we installed.
752-
Expect((*appGW.BackendHTTPSettingsCollection)[1]).To(Equal(*httpSettings))
722+
Expect(backendSettings).To(ContainElement(*httpSettings))
753723
}
754724

755725
testAGConfig(ingressList, appGwConfigSettings{

pkg/appgw/health_probes_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// -------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// --------------------------------------------------------------------------------------------
5+
6+
package appgw
7+
8+
import (
9+
"testing"
10+
11+
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
12+
"github.com/Azure/go-autorest/autorest/to"
13+
. "github.com/onsi/ginkgo"
14+
. "github.com/onsi/gomega"
15+
"k8s.io/api/extensions/v1beta1"
16+
)
17+
18+
func TestHealthProbes(t *testing.T) {
19+
RegisterFailHandler(Fail)
20+
RunSpecs(t, "Test setting up App Gateway health probes")
21+
}
22+
23+
var _ = Describe("configure App Gateway health probes", func() {
24+
Context("create probes", func() {
25+
cb := makeConfigBuilderTestFixture(nil)
26+
27+
endpoints := makeEndpointsFixture()
28+
_ = cb.k8sContext.Caches.Endpoints.Add(endpoints)
29+
30+
service := makeServiceFixture(*makeServicePorts()...)
31+
_ = cb.k8sContext.Caches.Service.Add(service)
32+
33+
pod := makePodFixture(testFixturesServiceName, testFixturesNamespace, testFixturesContainerName, testFixturesContainerPort)
34+
_ = cb.k8sContext.Caches.Pods.Add(pod)
35+
36+
ingressList := []*v1beta1.Ingress{
37+
makeIngressFixture(),
38+
}
39+
40+
// !! Action !!
41+
_, _ = cb.HealthProbesCollection(ingressList)
42+
actual := cb.appGwConfig.Probes
43+
44+
// We expect our health probe configurator to have arrived at this final setup
45+
defaultProbe := network.ApplicationGatewayProbe{
46+
47+
ApplicationGatewayProbePropertiesFormat: &network.ApplicationGatewayProbePropertiesFormat{
48+
Protocol: network.HTTP,
49+
Host: to.StringPtr("localhost"),
50+
Path: to.StringPtr("/"),
51+
Interval: to.Int32Ptr(30),
52+
Timeout: to.Int32Ptr(30),
53+
UnhealthyThreshold: to.Int32Ptr(3),
54+
PickHostNameFromBackendHTTPSettings: nil,
55+
MinServers: nil,
56+
Match: nil,
57+
ProvisioningState: nil,
58+
},
59+
Name: to.StringPtr("k8s-ag-ingress-defaultprobe"),
60+
Etag: nil,
61+
Type: nil,
62+
ID: nil,
63+
}
64+
probeForHost := network.ApplicationGatewayProbe{
65+
ApplicationGatewayProbePropertiesFormat: &network.ApplicationGatewayProbePropertiesFormat{
66+
Protocol: network.HTTP,
67+
Host: to.StringPtr(testFixturesHost),
68+
Path: to.StringPtr(testFixturesURLPath),
69+
Interval: to.Int32Ptr(30),
70+
Timeout: to.Int32Ptr(30),
71+
UnhealthyThreshold: to.Int32Ptr(3),
72+
PickHostNameFromBackendHTTPSettings: nil,
73+
MinServers: nil,
74+
Match: nil,
75+
ProvisioningState: nil,
76+
},
77+
Name: to.StringPtr("k8s-ag-ingress---service-name---443-pb---name--"),
78+
Etag: nil,
79+
Type: nil,
80+
ID: nil,
81+
}
82+
83+
probeForOtherHost := network.ApplicationGatewayProbe{
84+
ApplicationGatewayProbePropertiesFormat: &network.ApplicationGatewayProbePropertiesFormat{
85+
Protocol: network.HTTP,
86+
Host: to.StringPtr(testFixturesHost),
87+
Path: to.StringPtr(testFixturesURLPath),
88+
Interval: to.Int32Ptr(20),
89+
Timeout: to.Int32Ptr(5),
90+
UnhealthyThreshold: to.Int32Ptr(3),
91+
PickHostNameFromBackendHTTPSettings: nil,
92+
MinServers: nil,
93+
Match: nil,
94+
ProvisioningState: nil,
95+
},
96+
Name: to.StringPtr("k8s-ag-ingress---service-name---80-pb---name--"),
97+
Etag: nil,
98+
Type: nil,
99+
ID: nil,
100+
}
101+
102+
It("should have exactly 3 records", func() {
103+
Expect(len(*actual)).To(Equal(3))
104+
})
105+
106+
It("should have created 1 default probe", func() {
107+
Expect(*actual).To(ContainElement(defaultProbe))
108+
})
109+
110+
It("should have created 1 probe for Host", func() {
111+
Expect(*actual).To(ContainElement(probeForHost))
112+
})
113+
114+
It("should have created 1 probe for OtherHost", func() {
115+
Expect(*actual).To(ContainElement(probeForOtherHost))
116+
})
117+
})
118+
})

0 commit comments

Comments
 (0)