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 8, 2021
1 parent cbb86cb commit 5a4be8f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 9 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)
})
})
17 changes: 10 additions & 7 deletions test/e2e/ingress/route.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("upstream expansion", func() {
s := scaffold.NewDefaultScaffold()
ginkgo.It("create and then scale to 2 ", func() {
apisixRoute := `
backendSvc, backendSvcPort := s.DefaultHTTPBackend()
apisixRoute := fmt.Sprintf(`
apiVersion: apisix.apache.org/v1
kind: ApisixRoute
metadata:
Expand All @@ -37,19 +39,20 @@ 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")
scale := 2
s.ScaleHTTPBIN(scale)
s.WaitUntilNumPodsCreatedE(s.Selector("app=httpbin-deployment-e2e-test"), scale, 5, 5*time.Second)
assert.Nil(ginkgo.GinkgoT(), s.ScaleHTTPBIN(scale))
err = s.WaitUntilNumPodsCreatedE(s.Selector("app=httpbin-deployment-e2e-test"), scale, 5, 5*time.Second)
assert.Nil(ginkgo.GinkgoT(), err, "waiting pods number to desired number")
time.Sleep(10 * time.Second) // wait for ingress to sync
ups, err := s.ListApisixUpstreams()
assert.Nil(ginkgo.GinkgoT(), err, "list upstreams error")
Expand Down
24 changes: 22 additions & 2 deletions test/e2e/scaffold/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,28 @@ 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)
}

// 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
9 changes: 9 additions & 0 deletions test/e2e/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -176,6 +177,14 @@ 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)

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 5a4be8f

Please sign in to comment.