Skip to content

Commit

Permalink
test: add e2e case to cover the namespacing filtering feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers committed Jan 11, 2021
1 parent be8dc6d commit 25d5ea2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 20 deletions.
77 changes: 77 additions & 0 deletions test/e2e/ingress/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 ingress

import (
"encoding/json"
"fmt"
"net/http"

"github.com/api7/ingress-controller/test/e2e/scaffold"
"github.com/onsi/ginkgo"
"github.com/stretchr/testify/assert"
)

var _ = ginkgo.Describe("namespacing filtering", func() {
s := scaffold.NewDefaultScaffold()
ginkgo.It("resources in other namespaces should be ignored", func() {
backendSvc, backendSvcPort := s.DefaultHTTPBackend()
route := fmt.Sprintf(`
apiVersion: apisix.apache.org/v1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
rules:
- host: httpbin.com
http:
paths:
- backend:
serviceName: %s
servicePort: %d
path: /ip
`, backendSvc, backendSvcPort[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(route), "creating ApisixRoute")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixRoutesCreated(1), "checking number of routes")
assert.Nil(ginkgo.GinkgoT(), s.EnsureNumApisixUpstreamsCreated(1), "checking number of upstreams")

body := s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.com").Expect().Status(http.StatusOK).Body().Raw()
var placeholder ip
err := json.Unmarshal([]byte(body), &placeholder)
assert.Nil(ginkgo.GinkgoT(), err, "unmarshalling IP")

// Now create another ApisixRoute in default namespace.
route = fmt.Sprintf(`
apiVersion: apisix.apache.org/v1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
rules:
- host: httpbin.com
http:
paths:
- backend:
serviceName: %s
servicePort: %d
path: /headers
`, backendSvc, backendSvcPort[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromStringWithNamespace(route, "default"), "creating ApisixRoute")
_ = s.NewAPISIXClient().GET("/headers").WithHeader("Host", "httpbin.com").Expect().Status(http.StatusNotFound)
})
})
28 changes: 14 additions & 14 deletions test/e2e/ingress/resourcepushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ingress

import (
"fmt"
"time"

"github.com/onsi/ginkgo"
Expand All @@ -26,7 +27,8 @@ import (
var _ = ginkgo.Describe("ApisixRoute Testing", func() {
s := scaffold.NewDefaultScaffold()
ginkgo.It("create and then scale upstream pods to 2 ", func() {
apisixRoute := `
backendSvc, backendSvcPort := s.DefaultHTTPBackend()
apisixRoute := fmt.Sprintf(`
apiVersion: apisix.apache.org/v1
kind: ApisixRoute
metadata:
Expand All @@ -40,8 +42,8 @@ spec:
serviceName: httpbin-service-e2e-test
servicePort: 80
path: /ip
`
s.CreateApisixRouteByString(apisixRoute)
`, backendSvc, backendSvcPort[0])
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(apisixRoute))

err := s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")
Expand All @@ -56,8 +58,9 @@ spec:
assert.Len(ginkgo.GinkgoT(), ups[0].Nodes, 2, "upstreams nodes not expect")
})

ginkgo.It("create and then remove ", func() {
apisixRoute := `
ginkgo.It("create and then remove", func() {
backendSvc, backendSvcPort := s.DefaultHTTPBackend()
apisixRoute := fmt.Sprintf(`
apiVersion: apisix.apache.org/v1
kind: ApisixRoute
metadata:
Expand All @@ -68,24 +71,21 @@ spec:
http:
paths:
- backend:
serviceName: httpbin-service-e2e-test
servicePort: 80
serviceName: %s
servicePort: %d
path: /ip
`
s.CreateApisixRouteByString(apisixRoute)
`, backendSvc, backendSvcPort[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(apisixRoute), "creating ApisixRoute")
err := s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")
err = s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err, "list upstreams error")
assert.Len(ginkgo.GinkgoT(), ups[0].Nodes, 1, "upstreams nodes not expect")

// remove
s.CreateApisixRouteByString(apisixRoute)
assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(apisixRoute))
time.Sleep(5 * time.Second) // wait for ingress to sync
ups, err = s.ListApisixUpstreams()
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err, "list upstreams error")
assert.Len(ginkgo.GinkgoT(), len(ups), 0, "upstreams nodes not expect")
})
Expand Down
29 changes: 25 additions & 4 deletions test/e2e/scaffold/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,33 @@ func (s *Scaffold) CreateApisixRoute(name string, rules []ApisixRouteRule) {
k8s.KubectlApplyFromString(s.t, s.kubectlOptions, string(data))
}

func (s *Scaffold) CreateApisixRouteByString(yaml string) {
k8s.KubectlApplyFromString(s.t, s.kubectlOptions, yaml)
// CreateResourceFromString creates resource from a loaded yaml string.
func (s *Scaffold) CreateResourceFromString(yaml string) error {
return k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml)
}

// RemoveResourceByString remove resource from a loaded yaml string.
func (s *Scaffold) RemoveResourceByString(yaml string) error {
return k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, yaml)
}
func (s *Scaffold) RemoveApisixRouteByString(yaml string) {
k8s.KubectlDeleteFromString(s.t, s.kubectlOptions, yaml)

// CreateResourceFromStringWithNamespace creates resource from a loaded yaml string
// and sets its namespace to the sepcified one.
func (s *Scaffold) CreateResourceFromStringWithNamespace(yaml, namespace string) error {
originalNamespace := s.kubectlOptions.Namespace
s.kubectlOptions.Namespace = namespace
defer func() {
s.kubectlOptions.Namespace = originalNamespace
}()
s.addFinializer(func() {
originalNamespace := s.kubectlOptions.Namespace
s.kubectlOptions.Namespace = namespace
defer func() {
s.kubectlOptions.Namespace = originalNamespace
}()
assert.Nil(s.t, k8s.KubectlDeleteFromStringE(s.t, s.kubectlOptions, yaml))
})
return k8s.KubectlApplyFromStringE(s.t, s.kubectlOptions, yaml)
}

func ensureNumApisixCRDsCreated(url string, desired int) error {
Expand Down
13 changes: 11 additions & 2 deletions test/e2e/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Scaffold struct {
apisixService *corev1.Service
httpbinDeployment *appsv1.Deployment
httpbinService *corev1.Service
finializers []func()

// Used for template rendering.
EtcdServiceFQDN string
Expand Down Expand Up @@ -173,8 +174,16 @@ func (s *Scaffold) beforeEach() {

func (s *Scaffold) afterEach() {
defer ginkgo.GinkgoRecover()
//err := k8s.DeleteNamespaceE(s.t, s.kubectlOptions, s.namespace)
//assert.Nilf(ginkgo.GinkgoT(), err, "deleting namespace %s", s.namespace)
err := k8s.DeleteNamespaceE(s.t, s.kubectlOptions, s.namespace)
assert.Nilf(ginkgo.GinkgoT(), err, "deleting namespace %s", s.namespace)

for _, f := range s.finializers {
f()
}
}

func (s *Scaffold) addFinializer(f func()) {
s.finializers = append(s.finializers, f)
}

func (s *Scaffold) renderConfig(path string) (string, error) {
Expand Down

0 comments on commit 25d5ea2

Please sign in to comment.