From 25d5ea22611b8aa13f5f1d73c66723f0d3c177fe Mon Sep 17 00:00:00 2001 From: Alex Zhang Date: Fri, 8 Jan 2021 19:18:39 +0800 Subject: [PATCH] test: add e2e case to cover the namespacing filtering feature --- test/e2e/ingress/namespace.go | 77 +++++++++++++++++++++++++++++ test/e2e/ingress/resourcepushing.go | 28 +++++------ test/e2e/scaffold/crd.go | 29 +++++++++-- test/e2e/scaffold/scaffold.go | 13 ++++- 4 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 test/e2e/ingress/namespace.go diff --git a/test/e2e/ingress/namespace.go b/test/e2e/ingress/namespace.go new file mode 100644 index 0000000000..23601ffa56 --- /dev/null +++ b/test/e2e/ingress/namespace.go @@ -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) + }) +}) diff --git a/test/e2e/ingress/resourcepushing.go b/test/e2e/ingress/resourcepushing.go index 572e342502..44ddabd2c9 100644 --- a/test/e2e/ingress/resourcepushing.go +++ b/test/e2e/ingress/resourcepushing.go @@ -15,6 +15,7 @@ package ingress import ( + "fmt" "time" "github.com/onsi/ginkgo" @@ -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: @@ -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") @@ -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: @@ -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") }) diff --git a/test/e2e/scaffold/crd.go b/test/e2e/scaffold/crd.go index 29338aed87..b955270b29 100644 --- a/test/e2e/scaffold/crd.go +++ b/test/e2e/scaffold/crd.go @@ -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 { diff --git a/test/e2e/scaffold/scaffold.go b/test/e2e/scaffold/scaffold.go index 64e12b9603..fdd19981d0 100644 --- a/test/e2e/scaffold/scaffold.go +++ b/test/e2e/scaffold/scaffold.go @@ -53,6 +53,7 @@ type Scaffold struct { apisixService *corev1.Service httpbinDeployment *appsv1.Deployment httpbinService *corev1.Service + finializers []func() // Used for template rendering. EtcdServiceFQDN string @@ -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) {