Skip to content

Commit

Permalink
Configuration in API - force-leave in API - additional check for luajit
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jan 22, 2016
1 parent e7ce5d6 commit d6633e9
Show file tree
Hide file tree
Showing 28 changed files with 111 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .ci/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

CMD="busted -v -o gtest --exclude-tags=ci"
CMD="busted -v -o gtest --exclude-tags=ci --repeat=3"

if [ "$TEST_SUITE" == "unit" ]; then
CMD="$CMD --coverage spec/unit && luacov-coveralls -i kong"
Expand Down
15 changes: 14 additions & 1 deletion kong/api/routes/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ return {
return responses.send_HTTP_OK(result)
end,

DELETE = function(self, dao_factory)
if not self.params.name then
return responses.send_HTTP_BAD_REQUEST("Missing node \"name\"")
end

local _, err = Serf(configuration):invoke_signal("force-leave", {self.params.name})
if err then
return responses.send_HTTP_BAD_REQUEST(err)
end

return responses.send_HTTP_OK()
end,

POST = function(self, dao_factory)
if not self.params.address then
return responses.send_HTTP_BAD_REQUEST("Missing \"address\"")
return responses.send_HTTP_BAD_REQUEST("Missing node \"address\"")
end

local _, err = Serf(configuration):invoke_signal("join", {self.params.address})
Expand Down
3 changes: 2 additions & 1 deletion kong/api/routes/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ return {
available_on_server = configuration.plugins,
enabled_in_cluster = db_plugins
},
lua_version = jit and jit.version or _VERSION
lua_version = jit and jit.version or _VERSION,
configuration = configuration
})
end
},
Expand Down
2 changes: 1 addition & 1 deletion kong/cli/cmds/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ local ok, err = IO.write_to_file(IO.path:join(args.output, CONFIG_FILENAME), new
if not ok then
logger:error(err)
os.exit(1)
end
end
2 changes: 1 addition & 1 deletion kong/cli/cmds/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local configuration, configuration_path = config_loader.load_default(args.config

local status = services.check_status(configuration, configuration_path)
if status == services.STATUSES.SOME_RUNNING then
logger:error("Some services required by Kong are not running. Please execute \"kong restart\"!")
logger:error("Some services required by Kong are already running. Please execute \"kong restart\"!")
os.exit(1)
elseif status == services.STATUSES.ALL_RUNNING then
logger:error("Kong is currently running")
Expand Down
1 change: 1 addition & 0 deletions kong/cli/services/base_service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function BaseService.find_cmd(app_name, additional_paths, check_path_func)
-- These are some default locations we are always looking into
local search_dirs = utils.table_merge({
"/usr/local/sbin",
"/usr/local/bin",
"/usr/sbin",
"/usr/bin",
"/bin"
Expand Down
5 changes: 4 additions & 1 deletion kong/cli/services/nginx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ function Nginx:stop()
return nil, err
end

return self:_invoke_signal(cmd, STOP)
local _, err = self:_invoke_signal(cmd, STOP)
if not err then
os.execute("while [ -f "..self._pid_file_path.." ]; do sleep 0.5; done")
end
end

function Nginx:reload()
Expand Down
6 changes: 4 additions & 2 deletions kong/cli/services/serf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function Serf:prepare()

-- Create serf event handler
local luajit_path = BaseService.find_cmd("luajit")
if not luajit_path then
return nil, "Can't find luajit"
end

local script = [[
#!/bin/sh
Expand Down Expand Up @@ -75,7 +78,6 @@ echo $COMMAND | ]]..luajit_path..[[
return true
end


function Serf:_join_node(address)
local _, err = self:invoke_signal("join", {address})
if err then
Expand Down Expand Up @@ -226,7 +228,7 @@ function Serf:stop()
})

-- Finally stop Serf
Serf.super.stop(self)
Serf.super.stop(self, true)
end
end

Expand Down
3 changes: 2 additions & 1 deletion kong/cli/utils/services.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ end
function _M.check_status(configuration, configuration_path)
local running, not_running

for _, service in ipairs(services) do
for index, service in ipairs(services) do
if service(configuration, configuration_path):is_running() then
running = true
print("INDEX "..index.." IS RUNNING")
else
not_running = true
end
Expand Down
2 changes: 1 addition & 1 deletion kong/dao/cassandra/base_dao.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function BaseDao:update(t, full, where_t)
end

-- Extract primary key from the entity
local t_primary_key, t_no_primary_key = extract_primary_key(old_entity, self._primary_key, self._clustering_key)
local t_primary_key, t_no_primary_key = extract_primary_key(t, self._primary_key, self._clustering_key)

-- If full, add CQL `null` to the SET part of the query for nil columns
if full then
Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/http-log/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ end
-- @param `conf` Configuration table, holds http endpoint details
-- @param `message` Message to be logged
local function log(premature, conf, message)
if premature then return end

local ok, err
local parsed_url = parse_url(conf.http_endpoint)
local host = parsed_url.host
Expand Down
6 changes: 1 addition & 5 deletions kong/plugins/jwt/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ local function retrieve_token(request, conf)
end
end

local function jwt_secret_cache_key(consumer_id)
return "jwt_secret/"..consumer_id
end

function JwtHandler:new()
JwtHandler.super.new(self, "jwt")
end
Expand Down Expand Up @@ -78,7 +74,7 @@ function JwtHandler:access(conf)
end

-- Retrieve the secret
local jwt_secret = cache.get_or_set(jwt_secret_cache_key(jwt_secret_key), function()
local jwt_secret = cache.get_or_set(cache.jwtauth_credential_key(jwt_secret_key), function()
local rows, err = dao.jwt_secrets:find_by_keys {key = jwt_secret_key}
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/loggly/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ local function decide_severity(conf, severity, message)
end

local function log(premature, conf, message)
if premature then return end

if message.response.status >= 500 then
return decide_severity(conf.log_level, conf.server_errors_severity, message)
elseif message.response.status >= 400 then
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,4 @@ function _M.execute(conf)
transform_querystrings(conf)
end

return _M
return _M
2 changes: 1 addition & 1 deletion kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ return {
}
}
}
}
}
2 changes: 2 additions & 0 deletions kong/plugins/syslog/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ local function send_to_syslog(log_level, severity, message)
end

local function log(premature, conf, message)
if premature then return end

if message.response.status >= 500 then
send_to_syslog(conf.log_level, conf.server_errors_severity, message)
elseif message.response.status >= 400 then
Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/tcp-log/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local cjson = require "cjson"
local TcpLogHandler = BasePlugin:extend()

local function log(premature, conf, message)
if premature then return end

local ok, err
local host = conf.host
local port = conf.port
Expand Down
2 changes: 2 additions & 0 deletions kong/plugins/udp-log/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local UdpLogHandler = BasePlugin:extend()
UdpLogHandler.PRIORITY = 1

local function log(premature, conf, message)
if premature then return end

local host = conf.host
local port = conf.port
local timeout = conf.timeout
Expand Down
2 changes: 1 addition & 1 deletion kong/tools/config_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ return {
["send_anonymous_reports"] = {type = "boolean", default = true},
["memory_cache_size"] = {type = "number", default = 128, min = 32},
["nginx"] = {type = "string", nullable = true}
}
}
2 changes: 1 addition & 1 deletion spec/integration/admin_api/apis_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,4 @@ describe("Admin API", function()
end)
end)
end)
end)
end)
60 changes: 51 additions & 9 deletions spec/integration/admin_api/cluster_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ describe("Admin API", function()
local BASE_URL = spec_helper.API_URL.."/cluster/events"

describe("POST", function()

it("[SUCCESS] should post a new event", function()
local _, status = http_client.post(BASE_URL, {}, {})
assert.equal(200, status)
end)

end)

end)

describe("/cluster/", function()
local BASE_URL = spec_helper.API_URL.."/cluster/"

local BASE_URL = spec_helper.API_URL.."/cluster/"

describe("GET", function()
it("[SUCCESS] should get the list of members", function()
os.execute("sleep 2") -- Let's wait for serf to register the node
Expand All @@ -50,15 +49,58 @@ describe("Admin API", function()

assert.equal("alive", member.status)
end)
it("[FAILURE] should fail when serf is not running anymore", function()
os.execute("pkill -9 serf")
end)

describe("DELETE", function()

setup(function()
os.execute([[nohup serf agent -rpc-addr=127.0.0.1:20000 -bind=127.0.0.1:20001 -node=helloworld > serf.log 2>&1 & echo $! > serf.pid]])
-- Wait for agent to start
while (os.execute("cat serf.log | grep running > /dev/null") / 256 == 1) do
-- Wait
end
end)

teardown(function()
os.execute("kill -9 $(cat serf.pid) && rm serf.pid && rm serf.log")
end)

it("[SUCCESS] should force-leave a node", function()
-- Join node
os.execute("serf join -rpc-addr=127.0.0.1:9101 127.0.0.1:20001 > /dev/null")

os.execute("sleep 2") -- Let's wait for serf to register the node

local _, status = http_client.get(BASE_URL, {}, {})
assert.equal(500, status)
local response, status = http_client.get(BASE_URL, {}, {})
assert.equal(200, status)
local body = json.decode(response)
assert.truthy(body)
assert.equal(2, #body.data)
assert.equal(2, body.total)
for _, v in ipairs(body.data) do
assert.equal("alive", v.status)
end

local _, status = http_client.delete(BASE_URL, {name="helloworld"}, {})
assert.equal(200, status)
os.execute("sleep 2") -- Let's wait for serf to propagate the event

response, status = http_client.get(BASE_URL, {}, {})
assert.equal(200, status)
local body = json.decode(response)
assert.truthy(body)
assert.equal(2, #body.data)
assert.equal(2, body.total)
local not_alive
for _, v in ipairs(body.data) do
if v.name == "helloworld" then
assert.equal("leaving", v.status)
not_alive = true
end
end
assert.truthy(not_alive)
end)
end)

end)


end)
6 changes: 6 additions & 0 deletions spec/integration/cli/cmds/restart_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ describe("CLI", function()
assert.are.same(0, code)
end)

it("should restart when a service has crashed", function()
os.execute("pkill -9 serf")
local _, code = spec_helper.restart_kong()
assert.are.same(0, code)
end)

end)
2 changes: 1 addition & 1 deletion spec/integration/cli/cmds/start_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@ describe("CLI", function()

end)

end)
end)
14 changes: 3 additions & 11 deletions spec/plugins/oauth2/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ end

describe("Authentication Plugin", function()

local function prepare()
spec_helper.drop_db()
setup(function()
spec_helper.prepare_db()
spec_helper.insert_fixtures {
api = {
{ name = "tests-oauth2", request_host = "oauth2.com", upstream_url = "http://mockbin.com" },
Expand All @@ -60,21 +60,13 @@ describe("Authentication Plugin", function()
{ client_id = "clientid123", client_secret = "secret123", redirect_uri = "http://google.com/kong", name="testapp", __consumer = 1 }
}
}
end

setup(function()
spec_helper.prepare_db()
spec_helper.start_kong()
end)

teardown(function()
spec_helper.stop_kong()
end)

before_each(function()
spec_helper.restart_kong() -- Required because the uuid function doesn't seed itself every millisecond, but every second
prepare()
end)

describe("OAuth2 Authorization", function()

describe("Code Grant", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/plugins/oauth2/hooks_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local API_URL = spec_helper.API_URL
local env = spec_helper.get_env() -- test environment
local dao_factory = env.dao_factory
local configuration = env.configuration
configuration.cassandra = configuration.databases_available[configuration.database].properties
configuration.cassandra = configuration[configuration.database].properties

describe("OAuth2 Authentication Hooks", function()

Expand Down
2 changes: 1 addition & 1 deletion spec/plugins/request-transformer/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,4 @@ describe("Request Transformer", function()
assert.equal("20", body.queryString["q1"])
end)
end)
end)
end)
2 changes: 1 addition & 1 deletion spec/unit/dao/entities_schemas_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,4 @@ describe("Entities Schemas", function()
}, errors)
end)
end)
end)
end)
1 change: 1 addition & 0 deletions spec/unit/tools/config_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ describe("Configuration validation", function()
assert.falsy(errors)
end)
end)

0 comments on commit d6633e9

Please sign in to comment.