This is an automated email from the ASF dual-hosted git repository.

tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new f199cdb  test: add e2e test cases for server-info plugin (#406)
f199cdb is described below

commit f199cdb5f5bfe3cb5acb19dc1903b1f5f426a353
Author: Fang <[email protected]>
AuthorDate: Wed May 12 18:53:51 2021 +0800

    test: add e2e test cases for server-info plugin (#406)
---
 test/e2e/plugins/server-info.go         | 44 ++++++++++++++++++++++++++++
 test/e2e/scaffold/apisix.go             |  4 +++
 test/e2e/scaffold/k8s.go                | 52 ++++++++++++++++++++++++++++-----
 test/e2e/scaffold/scaffold.go           |  9 +++---
 test/e2e/testdata/apisix-gw-config.yaml |  2 ++
 5 files changed, 99 insertions(+), 12 deletions(-)

diff --git a/test/e2e/plugins/server-info.go b/test/e2e/plugins/server-info.go
new file mode 100644
index 0000000..1598910
--- /dev/null
+++ b/test/e2e/plugins/server-info.go
@@ -0,0 +1,44 @@
+// 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 plugins
+
+import (
+       "github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
+       "github.com/onsi/ginkgo"
+       "github.com/stretchr/testify/assert"
+)
+
+var _ = ginkgo.Describe("server-info plugin", func() {
+       opts := &scaffold.Options{
+               Name:                    "default",
+               Kubeconfig:              scaffold.GetKubeconfig(),
+               APISIXConfigPath:        "testdata/apisix-gw-config.yaml",
+               APISIXDefaultConfigPath: 
"testdata/apisix-gw-config-default.yaml",
+               IngressAPISIXReplicas:   1,
+               HTTPBinServicePort:      80,
+               APISIXRouteVersion:      "apisix.apache.org/v2alpha1",
+       }
+       s := scaffold.NewScaffold(opts)
+
+       ginkgo.It("enable server-info plugin", func() {
+               serverInfoKey := [...]string{"etcd_version", "up_time", 
"last_report_time", "id", "hostname", "version", "boot_time"}
+               serverInfo, err := s.GetServerInfo()
+               assert.Nil(ginkgo.GinkgoT(), err)
+               for _, key := range serverInfoKey {
+                       _, ok := serverInfo[key]
+                       assert.True(ginkgo.GinkgoT(), ok)
+               }
+       })
+})
diff --git a/test/e2e/scaffold/apisix.go b/test/e2e/scaffold/apisix.go
index 84bed8e..2bab32e 100644
--- a/test/e2e/scaffold/apisix.go
+++ b/test/e2e/scaffold/apisix.go
@@ -124,6 +124,10 @@ spec:
       port: 9100
       protocol: TCP
       targetPort: 9100
+    - name: http-control
+      port: 9090
+      protocol: TCP
+      targetPort: 9090
   type: NodePort
 `
 )
diff --git a/test/e2e/scaffold/k8s.go b/test/e2e/scaffold/k8s.go
index 6fcc05b..1c80f55 100644
--- a/test/e2e/scaffold/k8s.go
+++ b/test/e2e/scaffold/k8s.go
@@ -184,6 +184,31 @@ func (s *Scaffold) EnsureNumApisixUpstreamsCreated(desired 
int) error {
        return ensureNumApisixCRDsCreated(u.String(), desired)
 }
 
+// GetServerInfo collect server info from "/v1/server_info" (Control API) 
exposed by server-info plugin
+func (s *Scaffold) GetServerInfo() (map[string]interface{}, error) {
+       u := url.URL{
+               Scheme: "http",
+               Host:   s.apisixControlTunnel.Endpoint(),
+               Path:   "/v1/server_info",
+       }
+       resp, err := http.Get(u.String())
+       if err != nil {
+               ginkgo.GinkgoT().Logf("failed to get response from Control API: 
%s", err.Error())
+               return nil, err
+       }
+       defer resp.Body.Close()
+       if resp.StatusCode != http.StatusOK {
+               ginkgo.GinkgoT().Logf("got status code %d from Control API", 
resp.StatusCode)
+               return nil, err
+       }
+       var ret map[string]interface{}
+       dec := json.NewDecoder(resp.Body)
+       if err := dec.Decode(&ret); err != nil {
+               return nil, err
+       }
+       return ret, nil
+}
+
 // ListApisixUpstreams list all upstreams from APISIX
 func (s *Scaffold) ListApisixUpstreams() ([]*v1.Upstream, error) {
        u := url.URL{
@@ -286,14 +311,16 @@ func (s *Scaffold) ListApisixTls() ([]*v1.Ssl, error) {
 
 func (s *Scaffold) newAPISIXTunnels() error {
        var (
-               adminNodePort int
-               httpNodePort  int
-               httpsNodePort int
-               adminPort     int
-               httpPort      int
-               httpsPort     int
-               tcpPort       int
-               tcpNodePort   int
+               adminNodePort   int
+               httpNodePort    int
+               httpsNodePort   int
+               tcpNodePort     int
+               controlNodePort int
+               adminPort       int
+               httpPort        int
+               httpsPort       int
+               tcpPort         int
+               controlPort     int
        )
        for _, port := range s.apisixService.Spec.Ports {
                if port.Name == "http" {
@@ -308,6 +335,9 @@ func (s *Scaffold) newAPISIXTunnels() error {
                } else if port.Name == "tcp" {
                        tcpNodePort = int(port.NodePort)
                        tcpPort = int(port.Port)
+               } else if port.Name == "http-control" {
+                       controlNodePort = int(port.NodePort)
+                       controlPort = int(port.Port)
                }
        }
 
@@ -319,6 +349,8 @@ func (s *Scaffold) newAPISIXTunnels() error {
                httpsNodePort, httpsPort)
        s.apisixTCPTunnel = k8s.NewTunnel(s.kubectlOptions, 
k8s.ResourceTypeService, "apisix-service-e2e-test",
                tcpNodePort, tcpPort)
+       s.apisixControlTunnel = k8s.NewTunnel(s.kubectlOptions, 
k8s.ResourceTypeService, "apisix-service-e2e-test",
+               controlNodePort, controlPort)
 
        if err := s.apisixAdminTunnel.ForwardPortE(s.t); err != nil {
                return err
@@ -336,6 +368,10 @@ func (s *Scaffold) newAPISIXTunnels() error {
                return err
        }
        s.addFinalizers(s.apisixTCPTunnel.Close)
+       if err := s.apisixControlTunnel.ForwardPortE(s.t); err != nil {
+               return err
+       }
+       s.addFinalizers(s.apisixControlTunnel.Close)
        return nil
 }
 
diff --git a/test/e2e/scaffold/scaffold.go b/test/e2e/scaffold/scaffold.go
index ab63cd2..7c778b7 100644
--- a/test/e2e/scaffold/scaffold.go
+++ b/test/e2e/scaffold/scaffold.go
@@ -62,10 +62,11 @@ type Scaffold struct {
        httpbinService    *corev1.Service
        finializers       []func()
 
-       apisixAdminTunnel *k8s.Tunnel
-       apisixHttpTunnel  *k8s.Tunnel
-       apisixHttpsTunnel *k8s.Tunnel
-       apisixTCPTunnel   *k8s.Tunnel
+       apisixAdminTunnel   *k8s.Tunnel
+       apisixHttpTunnel    *k8s.Tunnel
+       apisixHttpsTunnel   *k8s.Tunnel
+       apisixTCPTunnel     *k8s.Tunnel
+       apisixControlTunnel *k8s.Tunnel
 
        // Used for template rendering.
        EtcdServiceFQDN string
diff --git a/test/e2e/testdata/apisix-gw-config.yaml 
b/test/e2e/testdata/apisix-gw-config.yaml
index 4225a3a..f9af36e 100644
--- a/test/e2e/testdata/apisix-gw-config.yaml
+++ b/test/e2e/testdata/apisix-gw-config.yaml
@@ -23,6 +23,7 @@ apisix:
   node_listen: 9080              # APISIX listening port
   enable_admin: true
   enable_admin_cors: true         # Admin API support CORS response headers.
+  enable_control: true
   enable_debug: false
   enable_dev_mode: false          # Sets nginx worker_processes to 1 if set to 
true
   enable_reuseport: true          # Enable nginx SO_REUSEPORT switch if set to 
true.
@@ -136,6 +137,7 @@ plugins:                          # plugin list
   - ip-restriction
   - referer-restriction
   - grpc-transcode
+  - server-info
   - serverless-pre-function
   - serverless-post-function
   - openid-connect

Reply via email to