Skip to content

Commit

Permalink
test: add basic headless service e2e test (#466)
Browse files Browse the repository at this point in the history
Signed-off-by: Ling Samuel <lingsamuelgrace@gmail.com>
  • Loading branch information
lingsamuel committed May 20, 2021
1 parent 1ffa862 commit 269cf07
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
108 changes: 107 additions & 1 deletion test/e2e/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"github.com/stretchr/testify/assert"

"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
"github.com/onsi/ginkgo"
ginkgo "github.com/onsi/ginkgo"
corev1 "k8s.io/api/core/v1"
)

var _ = ginkgo.Describe("support ingress.networking/v1", func() {
Expand Down Expand Up @@ -226,3 +227,108 @@ spec:
_ = s.NewAPISIXClient().GET("/status/200").WithHeader("Host", "a.httpbin.org").Expect().Status(http.StatusNotFound)
})
})

var _ = ginkgo.Describe("support ingress.networking/v1 with headless service backend", func() {
s := scaffold.NewDefaultScaffold()

const _httpHeadlessService = `
apiVersion: v1
kind: Service
metadata:
name: httpbin-headless-service-e2e-test
spec:
selector:
app: httpbin-deployment-e2e-test
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
type: ClusterIP
clusterIP: None
`

var (
backendSvc string
backendPort []int32
)
ginkgo.BeforeEach(func() {
err := s.CreateResourceFromString(_httpHeadlessService)
assert.Nil(ginkgo.GinkgoT(), err, "creating headless service")
svc, err := s.GetServiceByName("httpbin-headless-service-e2e-test")
assert.Nil(ginkgo.GinkgoT(), err, "get headless service")
getSvcNameAndPorts := func(svc *corev1.Service) (string, []int32) {
var ports []int32
for _, p := range svc.Spec.Ports {
ports = append(ports, p.Port)
}
return svc.Name, ports
}

backendSvc, backendPort = getSvcNameAndPorts(svc)
})

ginkgo.It("path exact match", func() {
ing := fmt.Sprintf(`
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: apisix
name: ingress-v1
spec:
rules:
- host: httpbin.org
http:
paths:
- path: /ip
pathType: Exact
backend:
service:
name: %s
port:
number: %d
`, backendSvc, backendPort[0])
err := s.CreateResourceFromString(ing)
assert.Nil(ginkgo.GinkgoT(), err, "creating ingress")
time.Sleep(5 * time.Second)

_ = s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect().Status(http.StatusOK)
// Exact path, doesn't match /ip/aha
_ = s.NewAPISIXClient().GET("/ip/aha").WithHeader("Host", "httpbin.org").Expect().Status(http.StatusNotFound)
// Mismatched host
_ = s.NewAPISIXClient().GET("/ip/aha").WithHeader("Host", "a.httpbin.org").Expect().Status(http.StatusNotFound)
})

ginkgo.It("path prefix match", func() {
ing := fmt.Sprintf(`
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: apisix
name: ingress-v1
spec:
rules:
- host: httpbin.org
http:
paths:
- path: /status
pathType: Prefix
backend:
service:
name: %s
port:
number: %d
`, backendSvc, backendPort[0])
err := s.CreateResourceFromString(ing)
assert.Nil(ginkgo.GinkgoT(), err, "creating ingress")
time.Sleep(5 * time.Second)

_ = s.NewAPISIXClient().GET("/status/500").WithHeader("Host", "httpbin.org").Expect().Status(http.StatusInternalServerError)
_ = s.NewAPISIXClient().GET("/status/504").WithHeader("Host", "httpbin.org").Expect().Status(http.StatusGatewayTimeout)
_ = s.NewAPISIXClient().GET("/statusaaa").WithHeader("Host", "httpbin.org").Expect().Status(http.StatusNotFound).Body().Contains("404 Route Not Found")
// Mismatched host
_ = s.NewAPISIXClient().GET("/status/200").WithHeader("Host", "a.httpbin.org").Expect().Status(http.StatusNotFound)
})
})
5 changes: 5 additions & 0 deletions test/e2e/scaffold/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
Expand Down Expand Up @@ -101,6 +102,10 @@ func (s *Scaffold) RemoveResourceByString(yaml string) error {
return k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, yaml)
}

func (s *Scaffold) GetServiceByName(name string) (*corev1.Service, error) {
return k8s.GetServiceE(s.t, s.kubectlOptions, name)
}

// CreateResourceFromStringWithNamespace creates resource from a loaded yaml string
// and sets its namespace to the specified one.
func (s *Scaffold) CreateResourceFromStringWithNamespace(yaml, namespace string) error {
Expand Down

0 comments on commit 269cf07

Please sign in to comment.