Skip to content

Commit

Permalink
Merge branch 'fork_master' into feat_service_test
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz committed Feb 5, 2021
2 parents e98eb2d + 9824b5f commit f8239e2
Show file tree
Hide file tree
Showing 37 changed files with 747 additions and 421 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/backend-e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ jobs:
with:
go-version: '1.13'

- name: Modify conf.yaml Configure for use by the manage-api cluster
run: |
sed -i 's/127.0.0.1:2379/172.16.238.10:2379/' ./api/conf/conf.yaml
sed -i 's/127.0.0.1/0.0.0.0/' ./api/conf/conf.yaml
sed -i '/172.16.238.10:2379/a\ - 172.16.238.11:2379' ./api/conf/conf.yaml
sed -i '/172.16.238.10:2379/a\ - 172.16.238.12:2379' ./api/conf/conf.yaml
- name: download file Dockerfile-apisix
working-directory: ./api/test/docker
run: |
curl -o Dockerfile-apisix https://raw.githubusercontent.com/apache/apisix-docker/master/alpine/Dockerfile
- name: run docker compose
working-directory: ./api/test/docker
run: |
Expand Down
2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set -e
VERSION=$(cat ./api/VERSION)
GITHASH=$(cat ./.githash 2> /dev/null || HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat ".git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7})

GOLDFLAGS="-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}"
GOLDFLAGS="-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}"

# Enter dry-run mode
if [ "$1" == "--dry-run" ]; then
Expand Down
5 changes: 4 additions & 1 deletion api/cmd/managerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func NewManagerAPICommand() *cobra.Command {
return nil
})

GitHash, Version = utils.GetHashAndVersion()

droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware {
var newMws []droplet.Middleware
// default middleware order: resp_reshape, auto_input, traffic_log
Expand All @@ -90,6 +92,7 @@ func NewManagerAPICommand() *cobra.Command {
log.Errorf("init stores fail: %w", err)
panic(err)
}

// routes
r := internal.SetUpRouter()
addr := fmt.Sprintf("%s:%d", conf.ServerHost, conf.ServerPort)
Expand Down Expand Up @@ -146,7 +149,7 @@ func newStopCommand() *cobra.Command {
if syscall.ENOENT.Error() != err.Error() {
fmt.Fprintf(os.Stderr, "failed to get manager-api pid: %s\n", err)
} else {
fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath)
fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath)
}
return
}
Expand Down
50 changes: 50 additions & 0 deletions api/internal/handler/tool/tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 tool

import (
"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
wgin "github.com/shiningrush/droplet/wrapper/gin"

"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/utils"
)

type Handler struct {
}

type InfoOutput struct {
Hash string `json:"commit_hash"`
Version string `json:"version"`
}

func NewHandler() (handler.RouteRegister, error) {
return &Handler{}, nil
}

func (h *Handler) ApplyRoute(r *gin.Engine) {
r.GET("/version", wgin.Wraps(h.Version))
}

func (h *Handler) Version(_ droplet.Context) (interface{}, error) {
hash, version := utils.GetHashAndVersion()
return &InfoOutput{
Hash: hash,
Version: version,
}, nil
}
40 changes: 40 additions & 0 deletions api/internal/handler/tool/tool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 tool

import (
"testing"

"github.com/shiningrush/droplet"
"github.com/stretchr/testify/assert"

"github.com/apisix/manager-api/internal/utils"
)

func TestInfo_Get(t *testing.T) {
h := Handler{}
ctx := droplet.NewContext()

hash, version := utils.GetHashAndVersion()

ret, err := h.Version(ctx)
assert.Nil(t, err)
assert.Equal(t, &InfoOutput{
Hash: hash,
Version: version,
}, ret)
}
2 changes: 2 additions & 0 deletions api/internal/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/apisix/manager-api/internal/handler/server_info"
"github.com/apisix/manager-api/internal/handler/service"
"github.com/apisix/manager-api/internal/handler/ssl"
"github.com/apisix/manager-api/internal/handler/tool"
"github.com/apisix/manager-api/internal/handler/upstream"
"github.com/apisix/manager-api/internal/log"
)
Expand Down Expand Up @@ -76,6 +77,7 @@ func SetUpRouter() *gin.Engine {
label.NewHandler,
data_loader.NewHandler,
data_loader.NewImportHandler,
tool.NewHandler,
}

for i := range factories {
Expand Down
27 changes: 27 additions & 0 deletions api/internal/utils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 utils

var (
gitHash string
version string
)

// get the hash and version
func GetHashAndVersion() (string, string) {
return gitHash, version
}
58 changes: 0 additions & 58 deletions api/test/docker/Dockerfile-apisix

This file was deleted.

2 changes: 1 addition & 1 deletion api/test/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ services:
dockerfile: test/docker/Dockerfile
restart: always
volumes:
- ./manager-api-conf.yaml:/go/manager-api/conf/conf.yaml:ro
- ../../conf/conf.yaml:/go/manager-api/conf/conf.yaml:ro
- ../testdata:/go/manager-api/testdata
depends_on:
- node1
Expand Down
39 changes: 39 additions & 0 deletions api/test/e2e/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 e2e

import (
"net/http"
"testing"
)

func TestInfo(t *testing.T) {
tests := []HttpTestCase{
{
Desc: "get info",
Object: ManagerApiExpect(t),
Method: http.MethodGet,
Path: "/version",
ExpectStatus: http.StatusOK,
ExpectBody: []string{"commit_hash", "\"version\""},
},
}

for _, tc := range tests {
testCaseCheck(tc, t)
}
}
2 changes: 1 addition & 1 deletion api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ clean_logfile() {
trap clean_up EXIT

export GO111MODULE=on
go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" ./cmd/manager
go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}" ./cmd/manager

# default level: warn, path: logs/error.log

Expand Down
13 changes: 13 additions & 0 deletions docs/back-end-e2e.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ This document describes how to use E2E test locally.

1. [install docker-compose](https://docs.docker.com/compose/install/)

**NOTE:** In order to run docker compose locally, please change the values of `listen.host` and `etcd.endpoints` within `./api/conf/conf.yaml` as follows:

```sh
listen:
host: 0.0.0.0
port: 9000
etcd:
endpoints:
- 172.16.238.10:2379
- 172.16.238.11:2379
- 172.16.238.12:2379
```

2. Use `docker-compose` to run services such as `manager-api`, `apisix`, `etcd` and `upstream-node`, run the command.

```sh
Expand Down
Loading

0 comments on commit f8239e2

Please sign in to comment.