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

@ -22,11 +22,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"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/ingress-nginx/test/e2e/framework"
@ -35,8 +32,8 @@ import (
var _ = framework.IngressNginxDescribe("[Default Backend] custom service", func() {
f := framework.NewDefaultFramework("custom-default-backend")
BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
ginkgo.It("uses custom default backend that returns 200 as status code", func() {
f.NewEchoDeployment()
err := framework.UpdateDeployment(f.KubeClientSet, f.Namespace, "nginx-ingress-controller", 1,
func(deployment *appsv1.Deployment) error {
@ -44,21 +41,21 @@ var _ = framework.IngressNginxDescribe("[Default Backend] custom service", func(
args = append(args, fmt.Sprintf("--default-backend-service=%v/%v", f.Namespace, framework.EchoService))
deployment.Spec.Template.Spec.Containers[0].Args = args
_, err := f.KubeClientSet.AppsV1().Deployments(f.Namespace).Update(deployment)
time.Sleep(5 * time.Second)
return err
})
Expect(err).NotTo(HaveOccurred())
assert.Nil(ginkgo.GinkgoT(), err, "updating deployment")
time.Sleep(5 * time.Second)
f.WaitForNginxServer("_",
func(server string) bool {
return strings.Contains(server, `set $proxy_upstream_name "upstream-default-backend"`)
})
})
It("uses custom default backend", func() {
resp, _, errs := gorequest.New().Get(f.GetURL(framework.HTTP)).End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
f.HTTPTestClient().
GET("/").
Expect().
Status(http.StatusOK)
})
})

View file

@ -17,12 +17,11 @@ limitations under the License.
package defaultbackend
import (
"crypto/tls"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/parnurzeal/gorequest"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
"gopkg.in/gavv/httpexpect.v2"
"k8s.io/ingress-nginx/test/e2e/framework"
)
@ -30,7 +29,7 @@ import (
var _ = framework.IngressNginxDescribe("[Default Backend]", func() {
f := framework.NewDefaultFramework("default-backend")
It("should return 404 sending requests when only a default backend is running", func() {
ginkgo.It("should return 404 sending requests when only a default backend is running", func() {
testCases := []struct {
Name string
Host string
@ -61,65 +60,55 @@ var _ = framework.IngressNginxDescribe("[Default Backend]", func() {
}
for _, test := range testCases {
By(test.Name)
ginkgo.By(test.Name)
request := gorequest.New()
var cm *gorequest.SuperAgent
var req *httpexpect.Request
switch test.Scheme {
case framework.HTTP:
cm = request.CustomMethod(test.Method, f.GetURL(framework.HTTP))
req = f.HTTPTestClient().Request(test.Method, test.Path)
req.WithURL(f.GetURL(framework.HTTP) + test.Path)
case framework.HTTPS:
cm = request.CustomMethod(test.Method, f.GetURL(framework.HTTPS))
// the default backend uses a self generated certificate
cm.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
req = f.HTTPTestClient().Request(test.Method, test.Path)
req.WithURL(f.GetURL(framework.HTTPS) + test.Path)
default:
Fail("Unexpected request scheme")
ginkgo.Fail("Unexpected request scheme")
}
if test.Host != "" {
cm.Set("Host", test.Host)
req.WithHeader("Host", test.Host)
}
resp, _, errs := cm.End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(test.Status))
req.Expect().
Status(test.Status)
}
})
It("enables access logging for default backend", func() {
ginkgo.It("enables access logging for default backend", func() {
f.UpdateNginxConfigMapData("enable-access-log-for-default-backend", "true")
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)+"/somethingOne").
Set("Host", "foo").
End()
Expect(len(errs)).Should(Equal(0))
Expect(resp.StatusCode).Should(Equal(http.StatusNotFound))
f.HTTPTestClient().
GET("/somethingOne").
WithHeader("Host", "foo").
Expect().
Status(http.StatusNotFound)
logs, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(logs).To(ContainSubstring("/somethingOne"))
assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs")
assert.Contains(ginkgo.GinkgoT(), logs, "/somethingOne")
})
It("disables access logging for default backend", func() {
ginkgo.It("disables access logging for default backend", func() {
f.UpdateNginxConfigMapData("enable-access-log-for-default-backend", "false")
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)+"/somethingTwo").
Set("Host", "bar").
End()
Expect(len(errs)).Should(Equal(0))
Expect(resp.StatusCode).Should(Equal(http.StatusNotFound))
f.HTTPTestClient().
GET("/somethingTwo").
WithHeader("Host", "bar").
Expect().
Status(http.StatusNotFound)
logs, err := f.NginxLogs()
Expect(err).ToNot(HaveOccurred())
Expect(logs).ToNot(ContainSubstring("/somethingTwo"))
assert.Nil(ginkgo.GinkgoT(), err, "obtaining nginx logs")
assert.NotContains(ginkgo.GinkgoT(), logs, "/somethingTwo")
})
})

View file

@ -17,11 +17,8 @@ limitations under the License.
package defaultbackend
import (
"crypto/tls"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/parnurzeal/gorequest"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
"k8s.io/ingress-nginx/test/e2e/framework"
)
@ -29,35 +26,31 @@ import (
var _ = framework.IngressNginxDescribe("[Default Backend] SSL", func() {
f := framework.NewDefaultFramework("default-backend")
It("should return a self generated SSL certificate", func() {
By("checking SSL Certificate using the NGINX IP address")
resp, _, errs := gorequest.New().
Post(f.GetURL(framework.HTTPS)).
TLSClientConfig(&tls.Config{
// the default backend uses a self generated certificate
InsecureSkipVerify: true,
}).End()
ginkgo.It("should return a self generated SSL certificate", func() {
ginkgo.By("checking SSL Certificate using the NGINX IP address")
resp := f.HTTPTestClient().
GET("/").
WithURL(f.GetURL(framework.HTTPS)).
Expect().
Raw()
Expect(errs).Should(BeEmpty())
Expect(len(resp.TLS.PeerCertificates)).Should(BeNumerically("==", 1))
assert.Equal(ginkgo.GinkgoT(), len(resp.TLS.PeerCertificates), 1)
for _, pc := range resp.TLS.PeerCertificates {
Expect(pc.Issuer.CommonName).Should(Equal("Kubernetes Ingress Controller Fake Certificate"))
assert.Equal(ginkgo.GinkgoT(), pc.Issuer.CommonName, "Kubernetes Ingress Controller Fake Certificate")
}
By("checking SSL Certificate using the NGINX catch all server")
resp, _, errs = gorequest.New().
Post(f.GetURL(framework.HTTPS)).
TLSClientConfig(&tls.Config{
// the default backend uses a self generated certificate
InsecureSkipVerify: true,
}).
Set("Host", "foo.bar.com").End()
ginkgo.By("checking SSL Certificate using the NGINX catch all server")
resp = f.HTTPTestClient().
GET("/").
WithURL(f.GetURL(framework.HTTPS)).
WithHeader("Host", "foo.bar.com").
Expect().
Raw()
Expect(errs).Should(BeEmpty())
Expect(len(resp.TLS.PeerCertificates)).Should(BeNumerically("==", 1))
assert.Equal(ginkgo.GinkgoT(), len(resp.TLS.PeerCertificates), 1)
for _, pc := range resp.TLS.PeerCertificates {
Expect(pc.Issuer.CommonName).Should(Equal("Kubernetes Ingress Controller Fake Certificate"))
assert.Equal(ginkgo.GinkgoT(), pc.Issuer.CommonName, "Kubernetes Ingress Controller Fake Certificate")
}
})
})

View file

@ -17,13 +17,11 @@ limitations under the License.
package defaultbackend
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"net/http"
"strings"
"github.com/parnurzeal/gorequest"
"github.com/onsi/ginkgo"
networking "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
@ -34,11 +32,11 @@ var _ = framework.IngressNginxDescribe("[Default Backend] change default setting
f := framework.NewDefaultFramework("default-backend-hosts")
host := "foo.com"
BeforeEach(func() {
f.NewEchoDeploymentWithReplicas(1)
ginkgo.BeforeEach(func() {
f.NewEchoDeployment()
})
It("should apply the annotation to the default backend", func() {
ginkgo.It("should apply the annotation to the default backend", func() {
annotations := map[string]string{
"nginx.ingress.kubernetes.io/proxy-buffer-size": "8k",
}
@ -69,13 +67,10 @@ var _ = framework.IngressNginxDescribe("[Default Backend] change default setting
return strings.Contains(server, "proxy_buffer_size 8k;")
})
resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)).
Set("Host", "foo.com").
End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
f.HTTPTestClient().
GET("/").
WithHeader("Host", "foo.com").
Expect().
Status(http.StatusOK)
})
})