Skip to content

Commit

Permalink
test: add e2e test cases for server-info plugin (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
fhuzero committed May 12, 2021
1 parent b0a6f3e commit f199cdb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
44 changes: 44 additions & 0 deletions test/e2e/plugins/server-info.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
4 changes: 4 additions & 0 deletions test/e2e/scaffold/apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ spec:
port: 9100
protocol: TCP
targetPort: 9100
- name: http-control
port: 9090
protocol: TCP
targetPort: 9090
type: NodePort
`
)
Expand Down
52 changes: 44 additions & 8 deletions test/e2e/scaffold/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions test/e2e/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/testdata/apisix-gw-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -136,6 +137,7 @@ plugins: # plugin list
- ip-restriction
- referer-restriction
- grpc-transcode
- server-info
- serverless-pre-function
- serverless-post-function
- openid-connect
Expand Down

0 comments on commit f199cdb

Please sign in to comment.