spacewander commented on a change in pull request #5171:
URL: https://github.com/apache/apisix/pull/5171#discussion_r720998258
##########
File path: apisix/cli/ops.lua
##########
@@ -932,6 +936,23 @@ local function reload(env)
end
+local function test(env)
+ -- reinit nginx.conf
+ init(env)
+
+ local test_cmd = env.openresty_args .. [[ -t -q ]]
+ -- When success,
+ -- On linux, os.execute returns 0,
+ -- On macos, os.execute returns 3 values: true, exit, 0, and we need the
first.
+ local test_ret = execute((test_cmd))
+ if (test_ret == 0 or test_ret == true) then
+ print("configuration test is successful")
+ return
+ end
+
+ print("configuration test failed")
Review comment:
Need to use `util.die`
##########
File path: t/cli/test_validate_config.sh
##########
@@ -89,3 +89,60 @@ fi
git checkout conf/config-default.yaml
echo "passed: allow configuring node_listen as a number in the default config"
+
+# apisix test
+git checkout conf/config.yaml
+
+out=$(./bin/apisix test 2>&1 || true)
+if ! echo "$out" | grep "configuration test is successful"; then
+ echo "failed: configuration test should be successful"
+ exit 1
+fi
+
+echo "pass: apisix test"
+
+echo '
+nginx_config:
+ main_configuration_snippet: |
+ notexist on;
+' > conf/config.yaml
+
+out=$(./bin/apisix test 2>&1 || true)
+if ! echo "$out" | grep "configuration test failed"; then
+ echo "failed: should test failed when configuration invalid"
+ exit 1
+fi
+
+echo "passed: apisix test(failure scenario)"
+
+# apisix stop and restart
+git checkout conf/config.yaml
+
+./bin/apisix start
+sleep 1 # wait for apisix starts
+
+echo '
+nginx_config:
+ main_configuration_snippet: |
+ notexist on;
+' > conf/config.yaml
+
+out=$(./bin/apisix stop 2>&1 || true)
+if ! echo "$out" | grep "\[emerg\] unknown directive \"notexist\""; then
+ echo "failed: should stop failed when configuration invalid"
+ exit 1
+fi
+
+echo "passed: apisix stop"
+
+
+out=$(./bin/apisix stop 2>&1 || true)
+if ! (echo "$out" | grep "\[emerg\] unknown directive \"notexist\"") && !
(echo "$out" | grep "APISIX is running"); then
+ echo "failed: should restart failed when configuration invalid"
+ exit 1
+fi
+
+echo "passed: apisix restart"
+
+git checkout conf/config.yaml
Review comment:
The configuration will be checked out after the test exits. We don't
need to do it ourselves.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]