Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: restart and health #1366

Merged
merged 17 commits into from
Jul 12, 2016
Merged
1 change: 1 addition & 0 deletions kong-0.8.2-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ build = {
["kong.cmd.roar"] = "kong/cmd/roar.lua",
["kong.cmd.init"] = "kong/cmd/init.lua",
["kong.cmd.stop"] = "kong/cmd/stop.lua",
["kong.cmd.quit"] = "kong/cmd/quit.lua",
["kong.cmd.start"] = "kong/cmd/start.lua",
["kong.cmd.check"] = "kong/cmd/check.lua",
["kong.cmd.reload"] = "kong/cmd/reload.lua",
Expand Down
1 change: 1 addition & 0 deletions kong/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
local cmds = {
start = "start",
stop = "stop",
quit = "quit",
restart = "restart",
reload = "reload",
check = "check",
Expand Down
37 changes: 37 additions & 0 deletions kong/cmd/quit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local dnsmasq_signals = require "kong.cmd.utils.dnsmasq_signals"
local nginx_signals = require "kong.cmd.utils.nginx_signals"
local serf_signals = require "kong.cmd.utils.serf_signals"
local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
local pl_path = require "pl.path"
local log = require "kong.cmd.utils.log"

local function execute(args)
-- retrieve prefix or use given one
local default_conf = assert(conf_loader(nil, {
prefix = args.prefix
}))
assert(pl_path.exists(default_conf.prefix),
"no such prefix: "..default_conf.prefix)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
assert(nginx_signals.stop(conf, true))
assert(serf_signals.stop(conf, DAOFactory(conf)))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end
log("Stopped gracefully")
end

local lapp = [[
Usage: kong quit [OPTIONS]

Options:
--prefix (optional string) prefix Kong is running at
]]

return {
lapp = lapp,
execute = execute
}
5 changes: 2 additions & 3 deletions kong/cmd/stop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ local function execute(args)

-- load <PREFIX>/kong.conf containing running node's config
local conf = assert(conf_loader(default_conf.kong_conf))
assert(nginx_signals.stop(conf, args.graceful))
assert(nginx_signals.stop(conf))
assert(serf_signals.stop(conf, DAOFactory(conf)))
if conf.dnsmasq then
assert(dnsmasq_signals.stop(conf))
end
log("Stopped%s", args.graceful and " gracefully" or "")
log("Stopped")
end

local lapp = [[
Usage: kong stop [OPTIONS]

Options:
--prefix (optional string) prefix Kong is running at
--graceful (optional boolean) graceful shutdown
]]

return {
Expand Down
4 changes: 0 additions & 4 deletions spec/02-integration/01-cmd/02-start_stop_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ describe("kong start/stop", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert(helpers.kong_exec("stop --prefix "..helpers.test_conf.prefix))
end)
it("start/stop gracefully", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert(helpers.kong_exec("stop --graceful --prefix "..helpers.test_conf.prefix))
end)
it("start with inexistent prefix", function()
finally(function()
pcall(helpers.dir.rmtree, "foobar")
Expand Down
50 changes: 48 additions & 2 deletions spec/02-integration/01-cmd/07-cluster_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
local helpers = require "spec.helpers"

describe("kong cluster", function()
it("keygen", function()
local _, stderr, stdout = helpers.kong_exec "cluster keygen"
before_each(function()
helpers.kill_all()
end)
teardown(function()
helpers.kill_all()
helpers.clean_prefix()
end)


it("cluster help", function()
local _, stderr = helpers.kong_exec "cluster --help"
assert.not_equal("", stderr)
end)
it("generates a key", function()
local _, stderr, stdout = assert(helpers.kong_exec("cluster keygen"))
assert.equal("", stderr)
assert.equal(26, stdout:len()) -- 24 + \r\n
end)
it("shows members", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
local _, _, stdout = assert(helpers.kong_exec("cluster members --conf "..helpers.test_conf_path))
assert.matches("alive", stdout)
end)
it("shows rechability", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
local _, _, stdout = assert(helpers.kong_exec("cluster reachability --conf "..helpers.test_conf_path))
assert.matches("Successfully contacted all live nodes", stdout)
end)
it("force-leaves a node", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
local _, _, stdout = assert(helpers.kong_exec("cluster force-leave 127.0.0.1 --conf "..helpers.test_conf_path))
assert.matches("left node 127.0.0.1", stdout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be

assert.matches("left node 127.0.0.1", stdout, nil, true)

Because arg #1 will be interpreted as a pattern otherwise, which means . can be any character. Better to have sane examples to not lead to errors in the future.

end)

describe("errors", function()
it("fails to show members when Kong is not running", function()
local ok, stderr = helpers.kong_exec("cluster members --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Error connecting to Serf agent", stderr)
end)
it("fails to show reachability when Kong is not running", function()
local ok, stderr = helpers.kong_exec("cluster reachability --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("Error connecting to Serf agent", stderr)
end)
it("fails to force-leave when a node is not specified", function()
local ok, stderr = helpers.kong_exec("cluster force-leave --conf "..helpers.test_conf_path)
assert.False(ok)
assert.matches("must specify the name of the node to leave", stderr)
end)
end)
end)
4 changes: 4 additions & 0 deletions spec/02-integration/01-cmd/08-restart_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe("kong restart", function()
helpers.clean_prefix()
end)

it("restart help", function()
local _, stderr = helpers.kong_exec "health --help"
assert.not_equal("", stderr)
end)
it("restarts if not running", function()
assert(helpers.kong_exec("restart --conf "..helpers.test_conf_path))
end)
Expand Down
4 changes: 4 additions & 0 deletions spec/02-integration/01-cmd/09-health_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe("kong restart", function()
helpers.clean_prefix()
end)

it("health help", function()
local _, stderr = helpers.kong_exec "health --help"
assert.not_equal("", stderr)
end)
it("succeeds when Kong is running", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path, {dnsmasq = true, dns_resolver = ""}))
assert(helpers.kong_exec("health --conf "..helpers.test_conf_path))
Expand Down
21 changes: 21 additions & 0 deletions spec/02-integration/01-cmd/10-quit_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local helpers = require "spec.helpers"

describe("kong start/stop", function()
teardown(function()
helpers.kill_all()
helpers.clean_prefix()
end)
before_each(function()
helpers.kill_all()
end)

it("quit help", function()
local _, stderr = helpers.kong_exec "quit --help"
assert.not_equal("", stderr)
end)
it("quits gracefully", function()
assert(helpers.kong_exec("start --conf "..helpers.test_conf_path))
assert(helpers.kong_exec("quit --prefix "..helpers.test_conf.prefix))
end)

end)