Skip to content

Commit

Permalink
Merge pull request #1550 from aledbf/e2e-4
Browse files Browse the repository at this point in the history
Add e2e tests - default backend
  • Loading branch information
aledbf authored Oct 20, 2017
2 parents 011ec65 + 601fb7d commit 4d6fa3e
Show file tree
Hide file tree
Showing 1,163 changed files with 289,196 additions and 14,174 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Session.vim
# coverage artifacts
.coverprofile
/gover.coverprofile

e2e-tests
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ go:

go_import_path: k8s.io/ingress-nginx

env:
global:
- CHANGE_MINIKUBE_NONE_USER=true
- KUBERNETES_VERSION=v1.7.5
- DOCKER=docker

jobs:
include:
- stage: Static Check
Expand All @@ -25,3 +31,9 @@ jobs:
- go get github.com/modocache/gover
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
- make cover
- stage: e2e
before_script:
- make e2e-image
- test/e2e/up.sh
script:
- make e2e-test
134 changes: 89 additions & 45 deletions Gopkg.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

[[constraint]]
name = "github.com/imdario/mergo"
version = "0.2.2"
version = "0.2.4"

[[constraint]]
branch = "master"
Expand Down Expand Up @@ -91,20 +91,20 @@

[[constraint]]
branch = "master"
name = "k8s.io/api"
name = "k8s.io/apiserver"

[[constraint]]
branch = "master"
name = "k8s.io/apimachinery"
name = "k8s.io/api"
revision = "409c3b2393cd3359f275bd8b883b4d9c5aec41f6"

[[constraint]]
branch = "master"
name = "k8s.io/apiserver"
name = "k8s.io/apimachinery"
revision = "e9a29eff7d472df0f7da9ead5ab59b74e74a07ec"

[[constraint]]
branch = "master"
name = "k8s.io/client-go"
revision = "076e344c86e52f088b78615f815b245f6d613537"

[[constraint]]
name = "k8s.io/kubernetes"
version = "1.8.0"
revision = "88975e98d6f4a84929a243abeb772de16399a4e9"
34 changes: 28 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

all: push

BUILDTAGS=
Expand Down Expand Up @@ -116,15 +130,23 @@ fmt:

lint:
@echo "+ $@"
@go list -f '{{if len .TestGoFiles}}"golint {{.Dir}}/..."{{end}}' $(shell go list ${PKG}/... | grep -v vendor) | xargs -L 1 sh -c
@go list -f '{{if len .TestGoFiles}}"golint {{.Dir}}/..."{{end}}' $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e') | xargs -L 1 sh -c

test: fmt lint vet
@echo "+ $@"
@go test -v -race -tags "$(BUILDTAGS) cgo" $(shell go list ${PKG}/... | grep -v vendor)
@go test -v -race -tags "$(BUILDTAGS) cgo" $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e')

e2e-image: sub-container-amd64
TAG=$(TAG) IMAGE=$(MULTI_ARCH_IMG) docker tag $(IMAGE):$(TAG) $(IMAGE):test
docker images

e2e-test:
@go test -o e2e-tests -c ./test/e2e
@KUBECONFIG=${HOME}/.kube/config INGRESSNGINXCONFIG=${HOME}/.kube/config ./e2e-tests

cover:
@echo "+ $@"
@go list -f '{{if len .TestGoFiles}}"go test -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' $(shell go list ${PKG}/... | grep -v vendor) | xargs -L 1 sh -c
@go list -f '{{if len .TestGoFiles}}"go test -coverprofile={{.Dir}}/.coverprofile {{.ImportPath}}"{{end}}' $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e') | xargs -L 1 sh -c
gover
goveralls -coverprofile=gover.coverprofile -service travis-ci -repotoken ${COVERALLS_TOKEN}

Expand All @@ -135,8 +157,8 @@ vet:
release: all-container all-push
echo "done"

.PHONY: docker-build
.PHONY: docker-build
docker-build: all-container

.PHONY: docker-push
docker-push: all-push
.PHONY: docker-push
docker-push: all-push
6 changes: 3 additions & 3 deletions pkg/ingress/type_equals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ import (
)

func TestEqualConfiguration(t *testing.T) {
ap, _ := filepath.Abs("../../tests/manifests/configuration-a.json")
ap, _ := filepath.Abs("../../test/manifests/configuration-a.json")
a, err := readJSON(ap)
if err != nil {
t.Errorf("unexpected error reading JSON file: %v", err)
}

bp, _ := filepath.Abs("../../tests/manifests/configuration-b.json")
bp, _ := filepath.Abs("../../test/manifests/configuration-b.json")
b, err := readJSON(bp)
if err != nil {
t.Errorf("unexpected error reading JSON file: %v", err)
}

cp, _ := filepath.Abs("../../tests/manifests/configuration-c.json")
cp, _ := filepath.Abs("../../test/manifests/configuration-c.json")
c, err := readJSON(cp)
if err != nil {
t.Errorf("unexpected error reading JSON file: %v", err)
Expand Down
58 changes: 58 additions & 0 deletions pkg/k8s/ensure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package k8s

import (
api "k8s.io/api/core/v1"
core "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
)

func EnsureSecret(cl kubernetes.Interface, secret *api.Secret) (*api.Secret, error) {
s, err := cl.CoreV1().Secrets(secret.Namespace).Create(secret)
if err != nil {
if k8sErrors.IsAlreadyExists(err) {
return cl.CoreV1().Secrets(secret.Namespace).Update(secret)
}
return nil, err
}
return s, nil
}

func EnsureIngress(cl kubernetes.Interface, ingress *extensions.Ingress) (*extensions.Ingress, error) {
s, err := cl.ExtensionsV1beta1().Ingresses(ingress.Namespace).Update(ingress)
if err != nil {
if k8sErrors.IsNotFound(err) {
return cl.ExtensionsV1beta1().Ingresses(ingress.Namespace).Create(ingress)
}
return nil, err
}
return s, nil
}

func EnsureService(cl kubernetes.Interface, service *core.Service) (*core.Service, error) {
s, err := cl.CoreV1().Services(service.Namespace).Update(service)
if err != nil {
if k8sErrors.IsNotFound(err) {
return cl.CoreV1().Services(service.Namespace).Create(service)
}
return nil, err
}
return s, nil
}
2 changes: 1 addition & 1 deletion pkg/nginx/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestBuildAuthResponseHeaders(t *testing.T) {

func TestTemplateWithData(t *testing.T) {
pwd, _ := os.Getwd()
f, err := os.Open(path.Join(pwd, "../../../tests/data/config.json"))
f, err := os.Open(path.Join(pwd, "../../../test/data/config.json"))
if err != nil {
t.Errorf("unexpected error reading json file: %v", err)
}
Expand Down
14 changes: 14 additions & 0 deletions rootfs/ingress-controller/clean-nginx-conf.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script removes consecutive empty lines in nginx.conf
# Using sed is more simple than using a go regex

Expand Down
File renamed without changes.
110 changes: 110 additions & 0 deletions test/e2e/defaultbackend/default_backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package defaultbackend

import (
"crypto/tls"
"net/http"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/parnurzeal/gorequest"

"k8s.io/ingress-nginx/test/e2e/framework"
)

const defaultBackend = "default backend - 404"

var _ = framework.IngressNginxDescribe("Default backend", func() {
f := framework.NewDefaultFramework("default-backend")

BeforeEach(func() {
})

AfterEach(func() {
})

It("should return 404 sending requests when only a default backend is running", func() {
httpURL, err := f.GetNginxURL(framework.HTTP)
Expect(err).NotTo(HaveOccurred())

httpsURL, err := f.GetNginxURL(framework.HTTPS)
Expect(err).NotTo(HaveOccurred())

request := gorequest.New()

testCases := []struct {
Name string
Host string
Scheme framework.RequestScheme
Method string
Path string
Status int
}{
{"basic HTTP GET request without host to path / should return 404", "", framework.HTTP, "GET", "/", 404},
{"basic HTTP GET request without host to path /demo should return 404", "", framework.HTTP, "GET", "/demo", 404},
{"basic HTTPS GET request without host to path / should return 404", "", framework.HTTPS, "GET", "/", 404},
{"basic HTTPS GET request without host to path /demo should return 404", "", framework.HTTPS, "GET", "/demo", 404},

{"basic HTTP POST request without host to path / should return 404", "", framework.HTTP, "POST", "/", 404},
{"basic HTTP POST request without host to path /demo should return 404", "", framework.HTTP, "POST", "/demo", 404},
{"basic HTTPS POST request without host to path / should return 404", "", framework.HTTPS, "POST", "/", 404},
{"basic HTTPS POST request without host to path /demo should return 404", "", framework.HTTPS, "POST", "/demo", 404},

{"basic HTTP GET request to host foo.bar.com and path / should return 404", " foo.bar.com", framework.HTTP, "GET", "/", 404},
{"basic HTTP GET request to host foo.bar.com and path /demo should return 404", " foo.bar.com", framework.HTTP, "GET", "/demo", 404},
{"basic HTTPS GET request to host foo.bar.com and path / should return 404", " foo.bar.com", framework.HTTPS, "GET", "/", 404},
{"basic HTTPS GET request to host foo.bar.com and path /demo should return 404", " foo.bar.com", framework.HTTPS, "GET", "/demo", 404},

{"basic HTTP POST request to host foo.bar.com and path / should return 404", " foo.bar.com", framework.HTTP, "POST", "/", 404},
{"basic HTTP POST request to host foo.bar.com and path /demo should return 404", " foo.bar.com", framework.HTTP, "POST", "/demo", 404},
{"basic HTTPS POST request to host foo.bar.com and path / should return 404", " foo.bar.com", framework.HTTPS, "POST", "/", 404},
{"basic HTTPS POST request to host foo.bar.com and path /demo should return 404", " foo.bar.com", framework.HTTPS, "POST", "/demo", 404},
}

for _, test := range testCases {
By(test.Name)
var errs []error
var cm *gorequest.SuperAgent

switch test.Scheme {
case framework.HTTP:
cm = request.CustomMethod(test.Method, httpURL)
break
case framework.HTTPS:
cm = request.CustomMethod(test.Method, httpsURL)
// the default backend uses a self generated certificate
cm.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
break
default:
Fail("Unexpected request scheme")
}

if test.Host != "" {
cm.Set("Host", test.Host)
}

resp, _, errs := cm.End()
Expect(len(errs)).Should(BeNumerically("==", 0))
Expect(resp.StatusCode).Should(Equal(test.Status))
}
})
})
Loading

0 comments on commit 4d6fa3e

Please sign in to comment.