Skip to content

Commit

Permalink
tests(dao) do not run migrations in require()
Browse files Browse the repository at this point in the history
Expose a run_migrations helper function that allows individual tests
to ensure that schema/keyspaces are in place. This also removes the
bad-citizen behavior of running migrations inside a 'require' context.

For convenience, the helpers run_migrations function is now explicitly
called from within a number of test setup() functions, to facilitate
executing individual tests.
  • Loading branch information
p0pr0ck5 authored and thibaultcha committed Jul 19, 2017
1 parent a0bcfc0 commit aa11eca
Show file tree
Hide file tree
Showing 43 changed files with 97 additions and 9 deletions.
2 changes: 2 additions & 0 deletions spec/03-plugins/01-tcp-log/01-tcp-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe("Plugin: tcp-log (log)", function()
local client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "tcp_logging.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/02-udp-log/01-udp-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe("Plugin: udp-log (log)", function()
local client

setup(function()
helpers.run_migrations()

local api2 = assert(helpers.dao.apis:insert {
name = "tests-udp-logging2",
hosts = { "udp_logging2.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/03-http-log/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pending("Plugin: http-log (log)", function()
-- seems to be broken.
local client
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "http_logging.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/04-file-log/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local FILE_LOG_PATH = os.tmpname()
describe("Plugin: file-log (log)", function()
local client
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "file_logging.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/05-syslog/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local pl_stringx = require "pl.stringx"
describe("#ci Plugin: syslog (log)", function()
local client, platform
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "logging.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/06-statsd/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local UDP_PORT = 20000
describe("Plugin: statsd (log)", function()
local client
setup(function()
helpers.run_migrations()

local consumer1 = assert(helpers.dao.consumers:insert {
username = "bob",
custom_id = "robert"
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/07-loggly/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local UDP_PORT = 20000
describe("Plugin: loggly (log)", function()
local client
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "logging.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/08-datadog/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local threads = require "llthreads2.ex"
describe("Plugin: datadog (log)", function()
local client
setup(function()
helpers.run_migrations()

local consumer1 = assert(helpers.dao.consumers:insert {
username = "foo",
custom_id = "bar"
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/10-key-auth/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: key-auth (API)", function()
local consumer
local admin_client
setup(function()
helpers.run_migrations()

assert(helpers.dao.apis:insert {
name = "keyauth1",
upstream_url = "http://mockbin.com",
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/10-key-auth/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local utils = require "kong.tools.utils"
describe("Plugin: key-auth (access)", function()
local client
setup(function()
helpers.run_migrations()

local anonymous_user = assert(helpers.dao.consumers:insert {
username = "no-body"
})
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/10-key-auth/03-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: key-auth (invalidations)", function()
local admin_client, proxy_client

before_each(function()
helpers.run_migrations()

helpers.dao:truncate_tables()
local api = assert(helpers.dao.apis:insert {
name = "api-1",
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/11-basic-auth/02-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local helpers = require "spec.helpers"
describe("Plugin: basic-auth (API)", function()
local consumer, admin_client
setup(function()
helpers.run_migrations()

assert(helpers.start_kong())
admin_client = helpers.admin_client()
end)
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/11-basic-auth/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe("Plugin: basic-auth (access)", function()
local client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "basic-auth1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/11-basic-auth/04-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: basic-auth (invalidations)", function()
local admin_client, proxy_client

before_each(function()
helpers.run_migrations()

helpers.dao:truncate_tables()
local api = assert(helpers.dao.apis:insert {
name = "api-1",
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/12-correlation-id/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local TRACKER_PATTERN = "%d+%.%d+%.%d+%.%d+%-%d+%-%d+%-%d+%-%d+%-%d%d%d%d%d%d%d%
describe("Plugin: correlation-id (access)", function()
local client
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "correlation1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/13-request-size-limiting/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe("Plugin: request-size-limiting (access)", function()
local client

setup(function()
helpers.run_migrations()

local api = assert(helpers.dao.apis:insert {
name = "limit.com",
hosts = { "limit.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/14-cors/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: cors (access)", function()
local client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "cors1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/15-request-transformer/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe("Plugin: request-transformer (access)", function()
local client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert { name = "api-1", hosts = { "test1.com" }, upstream_url = "http://mockbin.com"})
local api2 = assert(helpers.dao.apis:insert { name = "api-2", hosts = { "test2.com" }, upstream_url = "http://httpbin.org"})
local api3 = assert(helpers.dao.apis:insert { name = "api-3", hosts = { "test3.com" }, upstream_url = "http://mockbin.com"})
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/15-request-transformer/03-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("Plugin: request-transformer (API)", function()

describe("POST", function()
setup(function()
helpers.run_migrations()

assert(helpers.dao.apis:insert {
name = "test",
hosts = { "test1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/16-response-transformer/03-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("Plugin: response-transformer (API)", function()

describe("POST", function()
setup(function()
helpers.run_migrations()

assert(helpers.dao.apis:insert {
name = "test",
hosts = { "test1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/16-response-transformer/04-filter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe("Plugin: response-transformer (filter)", function()
local client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "tests-response-transformer",
hosts = { "response.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/17-jwt/02-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local fixtures = require "spec.03-plugins.17-jwt.fixtures"
describe("Plugin: jwt (API)", function()
local admin_client, consumer, jwt_secret
setup(function()
helpers.run_migrations()

assert(helpers.start_kong())
admin_client = helpers.admin_client()
end)
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/17-jwt/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe("Plugin: jwt (access)", function()
local proxy_client, admin_client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {name = "tests-jwt1", hosts = { "jwt.com" }, upstream_url = "http://mockbin.com"})
local api2 = assert(helpers.dao.apis:insert {name = "tests-jwt2", hosts = { "jwt2.com" }, upstream_url = "http://mockbin.com"})
local api3 = assert(helpers.dao.apis:insert {name = "tests-jwt3", hosts = { "jwt3.com" }, upstream_url = "http://mockbin.com"})
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/17-jwt/04-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe("Plugin: jwt (invalidations)", function()
local admin_client, proxy_client, consumer1, api1

before_each(function()
helpers.run_migrations()

helpers.dao:truncate_tables()

api1 = assert(helpers.dao.apis:insert {
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/18-ip-restriction/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe("Plugin: ip-restriction (access)", function()
local client, admin_client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "ip-restriction1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/19-acl/01-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local helpers = require "spec.helpers"
describe("Plugin: acl (API)", function()
local consumer, admin_client
setup(function()
helpers.run_migrations()

assert(helpers.start_kong())
admin_client = helpers.admin_client()
end)
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/19-acl/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: ACL (access)", function()
local client, api_client

setup(function()
helpers.run_migrations()

local consumer1 = assert(helpers.dao.consumers:insert {
username = "consumer1"
})
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/19-acl/03-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe("Plugin: ACL (invalidations)", function()
local consumer1, acl1

before_each(function()
helpers.run_migrations()

helpers.dao:truncate_tables()

consumer1 = assert(helpers.dao.consumers:insert {
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/20-hmac-auth/02-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ local cjson = require "cjson"
describe("Plugin: hmac-auth (API)", function()
local client, credential, consumer
setup(function()
helpers.run_migrations()

helpers.prepare_prefix()
assert(helpers.start_kong())
client = helpers.admin_client()
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/20-hmac-auth/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ describe("Plugin: hmac-auth (access)", function()
local client, consumer, credential

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "hmacauth.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/20-hmac-auth/04-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe("Plugin: hmac-auth (invalidations)", function()
local client_proxy, client_admin, consumer, credential

setup(function()
helpers.run_migrations()

local api = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "hmacauth.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/21-ldap-auth/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ local ldap_host_aws = "ec2-54-172-82-117.compute-1.amazonaws.com"
describe("Plugin: ldap-auth (access)", function()
local client, client_admin, api2, plugin2
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "test-ldap",
hosts = { "ldap.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/22-bot-detection/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local FACEBOOK = "facebookexternalhit/1.1" -- matches a known bot in `rules.lua
describe("Plugin: bot-detection (access)", function()
local client
setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "bot.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/22-bot-detection/02-invalidations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe("Plugin: bot-detection (hooks)", function()
local plugin, proxy_client, admin_client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "bot.com",
hosts = { "bot.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/22-bot-detection/03-api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe("Plugin: bot-detection (API)", function()
local client

setup(function()
helpers.run_migrations()

assert(helpers.dao.apis:insert {
name = "bot1.com",
hosts = { "bot1.com" },
Expand Down
2 changes: 2 additions & 0 deletions spec/03-plugins/23-aws-lambda/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe("Plugin: AWS Lambda (access)", function()
local client, api_client

setup(function()
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "lambda.com",
hosts = { "lambda.com" } ,
Expand Down
8 changes: 4 additions & 4 deletions spec/03-plugins/24-rate-limiting/04-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
helpers.kill_all()
flush_redis()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()

local consumer1 = assert(helpers.dao.consumers:insert {
custom_id = "provider_123"
Expand Down Expand Up @@ -407,7 +407,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
before_each(function()
helpers.kill_all()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "failtest1_com",
Expand Down Expand Up @@ -437,7 +437,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
teardown(function()
helpers.kill_all()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()
end)

it("does not work if an error occurs", function()
Expand Down Expand Up @@ -561,7 +561,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
setup(function()
helpers.stop_kong()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()

api = assert(helpers.dao.apis:insert {
name = "expire1_com",
Expand Down
8 changes: 4 additions & 4 deletions spec/03-plugins/25-response-rate-limiting/04-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
setup(function()
flush_redis()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()

local consumer1 = assert(helpers.dao.consumers:insert {custom_id = "provider_123"})
assert(helpers.dao.keyauth_credentials:insert {
Expand Down Expand Up @@ -520,7 +520,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
before_each(function()
helpers.kill_all()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "failtest1_com",
Expand Down Expand Up @@ -564,7 +564,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
teardown(function()
helpers.kill_all()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()
end)

it("does not work if an error occurs", function()
Expand Down Expand Up @@ -695,7 +695,7 @@ for i, policy in ipairs({"local", "cluster", "redis"}) do
setup(function()
helpers.stop_kong()
helpers.dao:drop_schema()
assert(helpers.dao:run_migrations())
helpers.run_migrations()
assert(helpers.start_kong())

api = assert(helpers.dao.apis:insert {
Expand Down
Loading

0 comments on commit aa11eca

Please sign in to comment.