This is an automated email from the ASF dual-hosted git repository.
chenjunxu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 3d30aa0 fix(cli): support running manager-api cmd in non-default
diretory (#1184)
3d30aa0 is described below
commit 3d30aa0e5677936ab31ebce4a76cbec6888bd1a3
Author: Joey <[email protected]>
AuthorDate: Mon Jan 4 19:53:56 2021 +0800
fix(cli): support running manager-api cmd in non-default diretory (#1184)
---
Makefile | 2 +-
api/cmd/managerapi.go | 1 +
api/internal/conf/conf.go | 8 ++++++--
api/internal/log/zap.go | 9 +++++++--
api/test/shell/cli_test.sh | 21 +++++++++++++++++++++
5 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
index b4411a7..666deb5 100644
--- a/Makefile
+++ b/Makefile
@@ -58,7 +58,7 @@ endif
### api-test: Run the tests of manager-api
.PHONY: api-test
api-test: api-default
- cd api/ && APISIX_API_WORKDIR=$$PWD go test -v -race -cover
-coverprofile=coverage.txt -covermode=atomic ./...
+ cd api/ && APISIX_API_WORKDIR=$$PWD ENV=test go test -v -race -cover
-coverprofile=coverage.txt -covermode=atomic ./...
### api-run: Run the manager-api
diff --git a/api/cmd/managerapi.go b/api/cmd/managerapi.go
index d60e73f..858ff36 100644
--- a/api/cmd/managerapi.go
+++ b/api/cmd/managerapi.go
@@ -57,6 +57,7 @@ func NewManagerAPICommand() *cobra.Command {
Short: "APISIX Manager API",
RunE: func(cmd *cobra.Command, args []string) error {
conf.InitConf()
+ log.InitLogger()
droplet.Option.Orchestrator = func(mws
[]droplet.Middleware) []droplet.Middleware {
var newMws []droplet.Middleware
// default middleware order: resp_reshape,
auto_input, traffic_log
diff --git a/api/internal/conf/conf.go b/api/internal/conf/conf.go
index 76230d3..e01ef41 100644
--- a/api/internal/conf/conf.go
+++ b/api/internal/conf/conf.go
@@ -34,6 +34,7 @@ const (
EnvBETA = "beta"
EnvDEV = "dev"
EnvLOCAL = "local"
+ EnvTEST = "test"
WebDir = "html/"
)
@@ -100,9 +101,12 @@ type Config struct {
Authentication Authentication
}
-// TODO: it is just for integration tests, we should call "InitLog" explicitly
when remove all handler's integration tests
+// TODO: we should no longer use init() function after remove all handler's
integration tests
+// ENV=test is for integration tests only, other ENV should call "InitConf"
explicitly
func init() {
- InitConf()
+ if env := os.Getenv("ENV"); env == EnvTEST {
+ InitConf()
+ }
}
func InitConf() {
diff --git a/api/internal/log/zap.go b/api/internal/log/zap.go
index d0d6720..cbcd160 100644
--- a/api/internal/log/zap.go
+++ b/api/internal/log/zap.go
@@ -27,13 +27,18 @@ import (
var logger *zap.SugaredLogger
-// TODO: it is just for integration tests, we should call "InitLog" explicitly
when remove all handler's integration tests
+// TODO: we should no longer use init() function after remove all handler's
integration tests
+// ENV=test is for integration tests only, other ENV should call "InitLogger"
explicitly
func init() {
- InitLogger()
+ if env := os.Getenv("ENV"); env == conf.EnvTEST {
+ InitLogger()
+ }
}
+
func InitLogger() {
logger = GetLogger(ErrorLog)
}
+
func GetLogger(logType Type) *zap.SugaredLogger {
writeSyncer := fileWriter(logType)
encoder := getEncoder(logType)
diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh
index 73aef84..f73e08c 100755
--- a/api/test/shell/cli_test.sh
+++ b/api/test/shell/cli_test.sh
@@ -126,6 +126,27 @@ if [[ $res != "hi~" ]]; then
fi
clean_up
+# run with -p flag out of the default directory
+workDir=$(pwd)
+distDir=/tmp/manager-api
+cp -r $workDir $distDir
+cd $distDir
+rm -fr bin && mkdir bin && mv ./manager-api ./bin/
+rm -rf html && mkdir html && echo "hi~" >> html/index.html
+cd bin && ./manager-api -p $distDir &
+sleep 5
+
+res=$(curl http://127.0.0.1:9000)
+pkill -f manager-api
+rm -fr $distDir
+
+if [[ $res != "hi~" ]]; then
+ echo "failed: manager-api can't run with -p flag out of the default
directory"
+ exit 1
+fi
+cd $workDir
+clean_up
+
# test start info
LOGLEVEL=$(cat conf/conf.yaml | awk '$1=="level:"{print $2}')