Skip to content

Commit

Permalink
Introducing spec_helpers for self-sustainable busted tests
Browse files Browse the repository at this point in the history
The spec_helper will be able to migrate and reset the db, start and stop
Kong, to ideally not depend on the Makefile anymore for those
operations.
  • Loading branch information
thibaultcha committed Mar 5, 2015
1 parent 3f8244f commit b036a32
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 64 deletions.
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,17 @@ lint:
@luacheck kong*.rockspec

run-integration-tests:
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) migrate
@bin/kong -c $(TESTS_CONF) start
@while ! [ `ps aux | grep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done # Wait until nginx starts
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) seed
@busted $(COVERAGE_FLAG) $(FOLDER) || (bin/kong stop; scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset; exit 1)
@bin/kong stop
@scripts/db.lua -c $(TESTS_CONF) $(SILENT_FLAG) reset

test-web:
@$(MAKE) run-integration-tests FOLDER=spec/other/web SILENT_FLAG=-s
@$(MAKE) run-integration-tests FOLDER=spec/web SILENT_FLAG=-s

test-proxy:
@$(MAKE) run-integration-tests FOLDER=spec/other/proxy SILENT_FLAG=-s
@$(MAKE) run-integration-tests FOLDER=spec/proxy SILENT_FLAG=-s

test-server:
@busted $(COVERAGE_FLAG) spec/server

test-all:
@$(MAKE) run-integration-tests FOLDER=spec/other SILENT_FLAG=-s
@$(MAKE) run-integration-tests FOLDER=spec SILENT_FLAG=-s
@busted $(COVERAGE_FLAG) spec/server
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 0 additions & 26 deletions spec/server/server_spec.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
local yaml = require "yaml"
local stringy = require "stringy"

local Faker = require "kong.tools.faker"
local CassandraFactory = require "kong.dao.cassandra.factory"
local utils = require "kong.tools.utils"

local configuration, dao_factory = utils.load_configuration_and_dao("kong_TEST.yml")

local TEST_CONF = "kong_TEST.yml"
local SERVER_CONF = "kong_TEST_SERVER.yml"
local KONG_BIN = "bin/kong"
local DB_BIN = "scripts/db.lua"

local function execute(command)
n = os.tmpname() -- get a temporary file name to store output
local exit_code = os.execute (command.." &> " .. n)
local result = utils.read_file(n)
os.remove (n)

return result, exit_code / 256
end

local function start_server()
execute(KONG_BIN.." -c "..SERVER_CONF.." migrate")
return execute(KONG_BIN.." -c "..SERVER_CONF.." start")
end

local function stop_server()
return execute(KONG_BIN.." -c "..SERVER_CONF.." stop")
end

local function replace_conf_property(name, value)
local yaml_value = yaml.load(utils.read_file(TEST_CONF))
Expand Down
64 changes: 64 additions & 0 deletions spec/spec_helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local utils = require "kong.tools.utils"
local Faker = require "kong.tools.faker"
local Migrations = require "kong.tools.migrations"

-- Constants
local KONG_BIN = "bin/kong"
local TEST_CONF_FILE = "kong_TEST.yml"

-- DB objects
local configuration, dao_factory = utils.load_configuration_and_dao(TEST_CONF_FILE)
local migrations = Migrations(dao_factory)
local faker = Faker(dao_factory)

local _M = {}

_M.CONF_FILE = TEST_CONF_FILE
_M.configuration = configuration
_M.dao_factory = dao_factory
_M.faker = faker

function _M.os_execute(command)
local n = os.tmpname() -- get a temporary file name to store output
local exit_code = os.execute (command.." &> " .. n)
local result = utils.read_file(n)
os.remove(n)

return result, exit_code / 256
end

function _M.start_kong()
return _M.os_execute(KONG_BIN.." -c "..TEST_CONF_FILE.." start")
end

function _M.stop_kong()
return _M.os_execute(KONG_BIN.." -c "..TEST_CONF_FILE.." stop")
end

function _M.prepare_db()
-- 1. Migrate our keyspace
migrations:migrate(function(_, err)
if err then
error(err)
end
end)

-- 2. Prepare statements
local err = dao_factory:prepare()
if err then
error(err)
end

-- 3. Seed DB with our default data. This will throw any necessary error
faker:seed()
end

function _M.reset_db()
migrations:reset(function(_, err)
if err then
error(err)
end
end)
end

return _M
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
-- dependencies
local CassandraFactory = require "kong.dao.cassandra.factory"
local spec_helper = require "spec.spec_helpers"
local cassandra = require "cassandra"
local constants = require "kong.constants"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local uuid = require "uuid"

-- Kong modules
local Faker = require "kong.tools.faker"
local Migrations = require "kong.tools.migrations"
local CassandraFactory = require "kong.dao.cassandra.factory"

-- Start instances
local configuration, dao_factory = utils.load_configuration_and_dao("kong_TEST.yml")
-- Raw session for check purposes
local session
-- Load everything we need from the spec_helper
local faker = spec_helper.faker
local dao_factory = spec_helper.dao_factory
local configuration = spec_helper.configuration
configuration.cassandra = configuration.databases_available[configuration.database].properties

local migrations = Migrations(dao_factory)
local faker = Faker(dao_factory)

-- An utility function to apply tests on each collection
local function describe_all_collections(tests_cb)
for type, dao in pairs({ api = dao_factory.apis,
Expand All @@ -34,36 +31,25 @@ end
describe("Cassandra DAO #dao #cassandra", function()

setup(function()
-- Migrate our schema
migrations:migrate(function(_, err)
assert.falsy(err)
end)

-- Prepare dao statements
local err = dao_factory:prepare()
assert.falsy(err)

-- Seed DB with dummy entities
faker:seed()
spec_helper.prepare_db()

-- Create a session to verify the dao's behaviour
session = cassandra.new()
session:set_timeout(configuration.cassandra.timeout)

local connected, err = session:connect(configuration.cassandra.hosts, configuration.cassandra.port)
local _, err = session:connect(configuration.cassandra.hosts, configuration.cassandra.port)
assert.falsy(err)

local ok, err = session:set_keyspace(configuration.cassandra.keyspace)
local _, err = session:set_keyspace(configuration.cassandra.keyspace)
assert.falsy(err)
end)

teardown(function()
if session then
session:close()
end
migrations:reset(function(_, err)
local _, err = session:close()
assert.falsy(err)
end)
end
spec_helper.reset_db()
end)

describe("Factory", function()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe("Validation #schema", function()
describe("#validate()", function()
-- Ok kids, today we're gonna test a custom validation schema,
-- grab a pair of glasses, this stuff can literally explode.
local collection = "custom_object"
local schema = {
string = { required = true, immutable = true },
table = { type = "table" },
Expand Down
File renamed without changes.

0 comments on commit b036a32

Please sign in to comment.