Refactor e2e tests to use testify y httpexpect

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-02-19 00:08:56 -03:00
parent 046e2d959d
commit f9624cbe46
80 changed files with 2280 additions and 2631 deletions

View file

@ -23,10 +23,8 @@ import (
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/parnurzeal/gorequest"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -40,18 +38,19 @@ const (
logRequireBackendReload = "Configuration changes detected, backend reload required"
logBackendReloadSuccess = "Backend successfully reloaded"
logInitialConfigSync = "Initial synchronization of the NGINX configuration"
waitForLuaSync = 5 * time.Second
waitForLuaSync = 5 * time.Second
)
var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
f := framework.NewDefaultFramework("dynamic-configuration")
BeforeEach(func() {
ginkgo.BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
ensureIngress(f, "foo.com", framework.EchoService)
})
It("configures balancer Lua middleware correctly", func() {
ginkgo.It("configures balancer Lua middleware correctly", func() {
f.WaitForNginxConfiguration(func(cfg string) bool {
return strings.Contains(cfg, "balancer.init_worker()") && strings.Contains(cfg, "balancer.balance()")
})
@ -62,8 +61,8 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
})
})
Context("when only backends change", func() {
It("handles endpoints only changes", func() {
ginkgo.Context("when only backends change", func() {
ginkgo.It("handles endpoints only changes", func() {
var nginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
nginxConfig = cfg
@ -72,19 +71,23 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
replicas := 2
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, framework.EchoService, replicas, nil)
Expect(err).NotTo(HaveOccurred())
assert.Nil(ginkgo.GinkgoT(), err)
ensureRequest(f, "foo.com")
f.HTTPTestClient().
GET("/").
WithHeader("Host", "foo.com").
Expect().
Status(http.StatusOK)
var newNginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
newNginxConfig = cfg
return true
})
Expect(nginxConfig).Should(Equal(newNginxConfig))
assert.Equal(ginkgo.GinkgoT(), nginxConfig, newNginxConfig)
})
It("handles endpoints only changes (down scaling of replicas)", func() {
ginkgo.It("handles endpoints only changes (down scaling of replicas)", func() {
var nginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
nginxConfig = cfg
@ -93,52 +96,79 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
replicas := 2
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, framework.EchoService, replicas, nil)
Expect(err).NotTo(HaveOccurred())
time.Sleep(waitForLuaSync * 2)
assert.Nil(ginkgo.GinkgoT(), err)
ensureRequest(f, "foo.com")
time.Sleep(waitForLuaSync)
f.HTTPTestClient().
GET("/").
WithHeader("Host", "foo.com").
Expect().
Status(http.StatusOK)
var newNginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
newNginxConfig = cfg
return true
})
Expect(nginxConfig).Should(Equal(newNginxConfig))
assert.Equal(ginkgo.GinkgoT(), nginxConfig, newNginxConfig)
err = framework.UpdateDeployment(f.KubeClientSet, f.Namespace, framework.EchoService, 0, nil)
assert.Nil(ginkgo.GinkgoT(), err)
Expect(err).NotTo(HaveOccurred())
time.Sleep(waitForLuaSync * 2)
time.Sleep(waitForLuaSync)
ensureRequestWithStatus(f, "foo.com", 503)
f.HTTPTestClient().
GET("/").
WithHeader("Host", "foo.com").
Expect().
Status(503)
})
It("handles endpoints only changes consistently (down scaling of replicas vs. empty service)", func() {
ginkgo.It("handles endpoints only changes consistently (down scaling of replicas vs. empty service)", func() {
deploymentName := "scalingecho"
f.NewEchoDeploymentWithNameAndReplicas(deploymentName, 0)
createIngress(f, "scaling.foo.com", deploymentName)
originalResponseCode := runRequest(f, "scaling.foo.com")
resp := f.HTTPTestClient().
GET("/").
WithHeader("Host", "scaling.foo.com").
Expect().Raw()
originalResponseCode := resp.StatusCode
replicas := 2
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, deploymentName, replicas, nil)
Expect(err).NotTo(HaveOccurred())
time.Sleep(waitForLuaSync * 2)
assert.Nil(ginkgo.GinkgoT(), err)
expectedSuccessResponseCode := runRequest(f, "scaling.foo.com")
time.Sleep(waitForLuaSync)
resp = f.HTTPTestClient().
GET("/").
WithHeader("Host", "scaling.foo.com").
Expect().Raw()
expectedSuccessResponseCode := resp.StatusCode
replicas = 0
err = framework.UpdateDeployment(f.KubeClientSet, f.Namespace, deploymentName, replicas, nil)
Expect(err).NotTo(HaveOccurred())
time.Sleep(waitForLuaSync * 2)
assert.Nil(ginkgo.GinkgoT(), err)
expectedFailureResponseCode := runRequest(f, "scaling.foo.com")
time.Sleep(waitForLuaSync)
Expect(originalResponseCode).To(Equal(503), "Expected empty service to return 503 response.")
Expect(expectedFailureResponseCode).To(Equal(503), "Expected downscaled replicaset to return 503 response.")
Expect(expectedSuccessResponseCode).To(Equal(200), "Expected intermediate scaled replicaset to return a 200 response.")
resp = f.HTTPTestClient().
GET("/").
WithHeader("Host", "scaling.foo.com").
Expect().Raw()
expectedFailureResponseCode := resp.StatusCode
assert.Equal(ginkgo.GinkgoT(), originalResponseCode, 503, "Expected empty service to return 503 response.")
assert.Equal(ginkgo.GinkgoT(), expectedFailureResponseCode, 503, "Expected downscaled replicaset to return 503 response.")
assert.Equal(ginkgo.GinkgoT(), expectedSuccessResponseCode, 200, "Expected intermediate scaled replicaset to return a 200 response.")
})
It("handles an annotation change", func() {
ginkgo.It("handles an annotation change", func() {
var nginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
nginxConfig = cfg
@ -146,13 +176,17 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
})
ingress, err := f.KubeClientSet.NetworkingV1beta1().Ingresses(f.Namespace).Get("foo.com", metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
assert.Nil(ginkgo.GinkgoT(), err)
ingress.ObjectMeta.Annotations["nginx.ingress.kubernetes.io/load-balance"] = "round_robin"
_, err = f.KubeClientSet.NetworkingV1beta1().Ingresses(f.Namespace).Update(ingress)
Expect(err).ToNot(HaveOccurred())
assert.Nil(ginkgo.GinkgoT(), err)
ensureRequest(f, "foo.com")
f.HTTPTestClient().
GET("/").
WithHeader("Host", "foo.com").
Expect().
Status(http.StatusOK)
var newNginxConfig string
f.WaitForNginxConfiguration(func(cfg string) bool {
@ -160,31 +194,35 @@ var _ = framework.IngressNginxDescribe("[Lua] dynamic configuration", func() {
return true
})
Expect(nginxConfig).Should(Equal(newNginxConfig))
assert.Equal(ginkgo.GinkgoT(), nginxConfig, newNginxConfig)
})
})
It("sets controllerPodsCount in Lua general configuration", func() {
ginkgo.It("sets controllerPodsCount in Lua general configuration", func() {
// https://github.com/curl/curl/issues/936
curlCmd := fmt.Sprintf("curl --fail --silent http://localhost:%v/configuration/general", nginx.StatusPort)
output, err := f.ExecIngressPod(curlCmd)
Expect(err).ToNot(HaveOccurred())
Expect(output).Should(Equal(`{"controllerPodsCount":1}`))
assert.Nil(ginkgo.GinkgoT(), err)
assert.Equal(ginkgo.GinkgoT(), output, `{"controllerPodsCount":1}`)
err = framework.UpdateDeployment(f.KubeClientSet, f.Namespace, "nginx-ingress-controller", 3, nil)
Expect(err).ToNot(HaveOccurred())
assert.Nil(ginkgo.GinkgoT(), err)
output, err = f.ExecIngressPod(curlCmd)
Expect(err).ToNot(HaveOccurred())
Expect(output).Should(Equal(`{"controllerPodsCount":3}`))
assert.Nil(ginkgo.GinkgoT(), err)
assert.Equal(ginkgo.GinkgoT(), output, `{"controllerPodsCount":3}`)
})
})
func ensureIngress(f *framework.Framework, host string, deploymentName string) *networking.Ingress {
ing := createIngress(f, host, deploymentName)
ensureRequest(f, host)
f.HTTPTestClient().
GET("/").
WithHeader("Host", host).
Expect().
Status(http.StatusOK)
return ing
}
@ -205,44 +243,18 @@ func createIngress(f *framework.Framework, host string, deploymentName string) *
return ing
}
func ensureRequest(f *framework.Framework, host string) {
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)).
Set("Host", host).
End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
}
func ensureHTTPSRequest(f *framework.Framework, url string, host string, expectedDNSName string) {
resp := f.HTTPTestClientWithTLSConfig(&tls.Config{
ServerName: host,
InsecureSkipVerify: true,
}).
GET("/").
WithURL(url).
WithHeader("Host", host).
Expect().
Raw()
func ensureRequestWithStatus(f *framework.Framework, host string, statusCode int) {
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)).
Set("Host", host).
End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(statusCode))
}
func runRequest(f *framework.Framework, host string) int {
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)).
Set("Host", host).
End()
Expect(errs).Should(BeEmpty())
return resp.StatusCode
}
func ensureHTTPSRequest(url string, host string, expectedDNSName string) {
resp, _, errs := gorequest.New().
Get(url).
Set("Host", host).
TLSClientConfig(&tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}).
End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
Expect(len(resp.TLS.PeerCertificates)).Should(BeNumerically("==", 1))
Expect(resp.TLS.PeerCertificates[0].DNSNames[0]).Should(Equal(expectedDNSName))
assert.Equal(ginkgo.GinkgoT(), resp.StatusCode, http.StatusOK)
assert.Equal(ginkgo.GinkgoT(), len(resp.TLS.PeerCertificates), 1)
assert.Equal(ginkgo.GinkgoT(), resp.TLS.PeerCertificates[0].DNSNames[0], expectedDNSName)
}