Skip to content

Commit

Permalink
Merge pull request #2455 from Mashape/test/test-plugins
Browse files Browse the repository at this point in the history
test(*) include plugin fixture Lua path
  • Loading branch information
thibaultcha authored Apr 25, 2017
2 parents 9e79ea7 + eacba3a commit 2fca928
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 103 deletions.
160 changes: 74 additions & 86 deletions spec/02-integration/03-admin_api/05-cache_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ local function do_it(desc, func)
end
end

describe("Admin API", function()
describe("Admin API /cache/{key}", function()
local api_client, proxy_client
setup(function()
assert(helpers.dao.apis:insert {
local api = assert(helpers.dao.apis:insert {
name = "api-cache",
hosts = { "cache.com" },
upstream_url = "http://mockbin.com"
})
assert(helpers.dao.plugins:insert {
api_id = api.id,
name = "first-request",
})

assert(helpers.start_kong({
custom_plugins = "first-request",
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua"
}))
api_client = helpers.admin_client()
proxy_client = helpers.proxy_client(2000)
Expand All @@ -36,58 +40,79 @@ describe("Admin API", function()
helpers.stop_kong()
end)

describe("/cache/{key}", function()
setup(function()
describe("GET", function()
do_it("returns 404 if not found", function()
local res = assert(api_client:send {
method = "POST",
path = "/apis/api-cache/plugins/",
headers = {
["Content-Type"] = "application/json"
},
body = {
name = "first-request"
}
method = "GET",
path = "/cache/_inexistent_",
query = { cache = current_cache },
})
assert.res_status(201, res)
assert.response(res).has.status(404)
end)
it("retrieves a cached entity", function()
-- populate cache
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {host = "cache.com"},
query = { cache = current_cache },
})
assert.response(res).has.status(200)

describe("GET", function()
do_it("returns 404 if not found", function()
local res = assert(api_client:send {
method = "GET",
path = "/cache/_inexistent_",
query = { cache = current_cache },
})
assert.response(res).has.status(404)
end)
it("retrieves a cached entity", function()
-- populate cache
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {host = "cache.com"},
query = { cache = current_cache },
})
assert.response(res).has.status(200)
res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
if current_cache == "shm" then
-- in this case the entry is jsonified (string type) and hence send as a "message" entry
json = cjson.decode(json.message)
end
assert.True(json.requested)
end)
end)

res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(200)
local json = assert.response(res).has.jsonbody()
if current_cache == "shm" then
-- in this case the entry is jsonified (string type) and hence send as a "message" entry
json = cjson.decode(json.message)
end
assert.True(json.requested)
end)
describe("DELETE", function()
it("purges cached entity", function()
-- populate cache
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {host = "cache.com"},
query = { cache = current_cache },
})
assert.response(res).has.status(200)

res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(200)

-- delete cache
res = assert(api_client:send {
method = "DELETE",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(204)

res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(404)
end)
end)

describe("/cache/", function()
describe("DELETE", function()
it("purges cached entity", function()
-- populate cache
it("purges all entities", function()
-- populate cache
local res = assert(proxy_client:send {
method = "GET",
path = "/",
Expand All @@ -103,10 +128,10 @@ describe("Admin API", function()
})
assert.response(res).has.status(200)

-- delete cache
-- delete cache
res = assert(api_client:send {
method = "DELETE",
path = "/cache/requested",
path = "/cache",
query = { cache = current_cache },
})
assert.response(res).has.status(204)
Expand All @@ -119,42 +144,5 @@ describe("Admin API", function()
assert.response(res).has.status(404)
end)
end)

describe("/cache/", function()
describe("DELETE", function()
it("purges all entities", function()
-- populate cache
local res = assert(proxy_client:send {
method = "GET",
path = "/",
headers = {host = "cache.com"},
query = { cache = current_cache },
})
assert.response(res).has.status(200)

res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(200)

-- delete cache
res = assert(api_client:send {
method = "DELETE",
path = "/cache",
query = { cache = current_cache },
})
assert.response(res).has.status(204)

res = assert(api_client:send {
method = "GET",
path = "/cache/requested",
query = { cache = current_cache },
})
assert.response(res).has.status(404)
end)
end)
end)
end)
end)
3 changes: 0 additions & 3 deletions spec/02-integration/06-cluster/01-cluster_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local NODES = {
cluster_listen_rpc = "0.0.0.0:9373",
cluster_profile = "local",
custom_plugins = "first-request",
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua"
},
servroot2 = {
prefix = "servroot2",
Expand All @@ -29,7 +28,6 @@ local NODES = {
cluster_listen_rpc = "0.0.0.0:10373",
cluster_profile = "local",
custom_plugins = "first-request",
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua"
},
servroot3 = {
prefix = "servroot3",
Expand All @@ -41,7 +39,6 @@ local NODES = {
cluster_listen_rpc = "0.0.0.0:20373",
cluster_profile = "local",
custom_plugins = "first-request",
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua"
}
}

Expand Down
19 changes: 5 additions & 14 deletions spec/02-integration/07-cache/01-database_cache_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ describe("Resolver", function()
local admin_client

setup(function()
assert(helpers.dao.apis:insert {
local api = assert(helpers.dao.apis:insert {
name = "mockbin",
hosts = { "mockbin.com" },
upstream_url = "http://mockbin.com"
})
assert(helpers.dao.plugins:insert {
api_id = api.id,
name = "database-cache",
})

assert(helpers.start_kong({
custom_plugins = "database-cache",
lua_package_path = "?/init.lua;./kong/?.lua;./spec/fixtures/?.lua"
}))
admin_client = helpers.admin_client()

local res = assert(admin_client:send {
method = "POST",
path = "/apis/mockbin/plugins/",
body = {
name = "database-cache"
},
headers = {
["Content-Type"] = "application/json"
}
})
assert.res_status(201, res)
end)
teardown(function()
if admin_client then admin_client:close() end
Expand Down
14 changes: 14 additions & 0 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

local BIN_PATH = "bin/kong"
local TEST_CONF_PATH = "spec/kong_tests.conf"
local CUSTOM_PLUGIN_PATH = "./spec/fixtures/custom_plugins/?.lua"

local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
Expand All @@ -21,6 +22,10 @@ local log = require "kong.cmd.utils.log"

log.set_lvl(log.levels.quiet) -- disable stdout logs in tests

-- Add to package path so dao helpers can insert custom plugins
-- (while running from the busted environment)
package.path = CUSTOM_PLUGIN_PATH .. ";" .. package.path

---------------
-- Conf and DAO
---------------
Expand Down Expand Up @@ -763,6 +768,15 @@ local function kong_exec(cmd, env)
cmd = cmd or ""
env = env or {}

-- Insert the Lua path to the custom-plugin fixtures
if not env.lua_package_path then
env.lua_package_path = CUSTOM_PLUGIN_PATH

else
env.lua_package_path = CUSTOM_PLUGIN_PATH .. ";" .. env.lua_package_path
end

-- build Kong environment variables
local env_vars = ""
for k, v in pairs(env) do
env_vars = string.format("%s KONG_%s='%s'", env_vars, k:upper(), v)
Expand Down

0 comments on commit 2fca928

Please sign in to comment.