Skip to content

Commit

Permalink
feat: Finish new CLI + documentation + fix tests
Browse files Browse the repository at this point in the history
The new Lua CLI is still at an MVP stage, but implements all the
features previously supported by the bash script + the Makefile is
converted to use it + tests are now passing.
  • Loading branch information
thibaultcha committed Mar 24, 2015
1 parent 7c67b1a commit dc2c7fb
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 169 deletions.
4 changes: 2 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ If you whish to run Cassandra locally, install it following the [Datastax instru
Finally, install Kong. Download [the latest release][kong-latest-url] and execute:

```
sudo make install
[sudo] make install
```

Now follow the "Usage" section of the README to start Kong.
Expand Down Expand Up @@ -108,7 +108,7 @@ brew install cassandra
Finally, install Kong. Download [the latest release][kong-latest-url] and execute:

```
sudo make install
[sudo] make install
```

Now follow the "Usage" section of the README to start Kong.
Expand Down
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
TESTING_CONF = kong_TEST.yml
DEVELOPMENT_CONF = kong_DEVELOPMENT.yml
DEV_ROCKS=busted luacov luacov-coveralls luacheck

.PHONY: install dev clean run seed drop lint test coverage test-api test-proxy test-server test-all

Expand All @@ -13,18 +14,17 @@ install:
fi

dev:
@NEEDED_ROCKS="busted luacov luacov-coveralls luacheck"
@for rock in ${NEEDED_ROCKS} ; do \
if ! command -v ${rock} &> /dev/null ; then \
echo ${rock} not found, installing via luarocks... ; \
luarocks install ${rock} ; \
else \
@echo "${rock} already installed, skipping" ; \
fi \
@for rock in $(DEV_ROCKS) ; do \
if ! command -v $$rock &> /dev/null ; then \
echo $$rock not found, installing via luarocks... ; \
luarocks install $$rock ; \
else \
echo "$$rock already installed, skipping" ; \
fi \
done;
@bin/kong config -e TEST
@bin/kong config -e DEVELOPMENT
@bin/kong db -c $(DEVELOPMENT_CONF) migrations:up
bin/kong config -e TEST
bin/kong config -e DEVELOPMENT
bin/kong db -c $(DEVELOPMENT_CONF) migrations:up

clean:
@rm -f luacov.*
Expand Down
71 changes: 20 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# KONG - The API layer

[![Build Status][travis-badge]][travis-url] [![Coverage Status][coveralls-badge]][coveralls-url] [![Gitter][gitter-badge]][gitter-url]
[![Build Status][travis-badge]][travis-url]
[![Coverage Status][coveralls-badge]][coveralls-url]
[![Gitter][gitter-badge]][gitter-url]

Kong is a scalable and customizable API Management Layer built on top of nginx.
Kong is a scalable and customizable API Management Layer built on top of Nginx.

> **Note**: getkong.org is still a work in progress, in the meanwhile, please follow instructions in this README instead.
* **[Installation](#installation)**
* **[Documentation](#documentation)**
Expand All @@ -11,50 +15,47 @@ Kong is a scalable and customizable API Management Layer built on top of nginx.

## Installation

Follow instructions at [getkong.org/download](http://getkong.org/download) for a production installation. **(coming soon)**

See [INSTALL.md](INSTALL.md) for installation instructions on your platform.

## Documentation

A complete documentation can be found at: [getkong.org/docs](http://getkong.org/docs)
A complete documentation on how to configure and use Kong can be found at: [getkong.org/docs](http://getkong.org/docs). **(coming soon)**

## Usage

Use Kong through the `bin/kong` executable. Make sure your Cassandra instance is running.

The first time ever you're running Kong, you need to make sure to setup Cassandra by executing:

```bash
bin/kong migrate
Use Kong through the `kong` executable. If you installed Kong via luarocks (as previously instructed) then `kong` should be in your `$PATH`.
```
$ kong --help
```

To start Kong:
To start Kong (make sure your Cassandra instance is running):

```bash
bin/kong start
```

See all the available options, with `bin/kong -h`.
$ kong start
```

## Development

Running Kong for development requires you to run:
To develop for Kong, simply run `[sudo] make install` in a clone of this repo. Then run:

```
make dev
$ make dev
```

This will install development dependencies and create your environment configuration files (`kong_TESTS.yml` and `kong_DEVELOPMENT.yml`).

- Run the tests:

```
make test-all
$ make test-all
```

- Run it:
- Run Kong with the development configuration:

```
bin/kong -c kong.yml start
$ kong start -c kong_DEVELOPMENT.yml
```

#### Makefile
Expand All @@ -76,38 +77,6 @@ When developing, use the `Makefile` for doing the following operations:
| `test-api` | Run the api integration tests |
| `test-all` | Run all unit + integration tests at once |

#### Scripts

Those scripts provide handy features while developing Kong:

##### db.lua

This script handles schema migrations, seeding and dropping of the database.

```bash
# Complete usage
scripts/db.lua --help

# Migrate up
scripts/db.lua [-c configuration_file] migrate # for all commands, the default configuration_file is kong.yml

# Revert latest migration
scripts/db.lua rollback

# Revert all migrations (danger! this will delete your data)
scripts/db.lua reset

# Seed DB (danger! this will delete your data)
scripts/db.lua seed

# Drop DB (danger! this will delete your data)
scripts/db.lua drop
```

##### config.lua

This script handles Kong's configuration files. It is better not to directly use it, as it is mainly used through `bin/kong` and the Makefile.

[travis-url]: https://travis-ci.org/Mashape/kong
[travis-badge]: https://img.shields.io/travis/Mashape/kong.svg?style=flat
[coveralls-url]: https://coveralls.io/r/Mashape/kong?branch=master
Expand Down
6 changes: 3 additions & 3 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ local cmd = arg[1]
if not cmd then
print("Missing <command>\n\n"..help_message)
os.exit(1)
elseif not commands[cmd] then
print("Invalid <command>: "..cmd.."\n\n"..help_message)
os.exit(1)
elseif cmd == "-h" or cmd == "--help" then
print(help_message)
os.exit(0)
elseif not commands[cmd] then
print("Invalid <command>: "..cmd.."\n\n"..help_message)
os.exit(1)
end

require(commands[cmd])
53 changes: 23 additions & 30 deletions spec/integration/server/server_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,18 @@ describe("Server", function()
describe("CLI", function()

it("should return the right version", function()
local result, exit_code = spec_helper.os_execute(spec_helper.KONG_BIN.." -v")
assert.are.same("Version: "..constants.VERSION, stringy.strip(result))
local result, exit_code = spec_helper.os_execute(spec_helper.KONG_BIN.." version")
assert.are.same("Kong version: "..constants.VERSION, stringy.strip(result))
end)

end)

describe("Startup migration", function()
it("should start with the default configuration", function()
assert.has_no.errors(function()
spec_helper.start_kong(TEST_CONF, true)
end)

setup(function()
local databases_available = yaml.load(utils.read_file(TEST_CONF)).databases_available
databases_available.cassandra.properties.keyspace = "kong_tests_server_migrations"
replace_conf_property("databases_available", databases_available)
spec_helper.add_env(SERVER_CONF)
end)

teardown(function()
spec_helper.stop_kong(SERVER_CONF)
spec_helper.reset_db(SERVER_CONF)
os.remove(SERVER_CONF)
spec_helper.remove_env(SERVER_CONF)
end)

it("should migrate when starting for the first time on a new keyspace", function()
spec_helper.start_kong(SERVER_CONF)
local env = spec_helper.get_env(SERVER_CONF)
local migrations = env.dao_factory:get_migrations()
assert.True(#migrations > 0)
finally(function()
pcall(spec_helper.stop_kong, TEST_CONF)
end)
end)

end)
Expand All @@ -56,21 +41,24 @@ describe("Server", function()

setup(function()
os.execute("cp "..TEST_CONF.." "..SERVER_CONF)
spec_helper.prepare_db()
spec_helper.drop_db() -- remove the seed from prepare_db()
spec_helper.add_env(SERVER_CONF)
spec_helper.prepare_db(SERVER_CONF)
spec_helper.drop_db(SERVER_CONF) -- remove the seed from prepare_db()
end)

teardown(function()
os.remove(SERVER_CONF)
spec_helper.remove_env(SERVER_CONF)
end)

after_each(function()
spec_helper.stop_kong(SERVER_CONF)
spec_helper.reset_db()
pcall(spec_helper.stop_kong, SERVER_CONF)
spec_helper.reset_db(SERVER_CONF)
end)

it("should work when no plugins are enabled and the DB is empty", function()
replace_conf_property("plugins_available", {})

local result, exit_code = spec_helper.start_kong(SERVER_CONF, true)
assert.are.same(0, exit_code)
end)
Expand All @@ -92,22 +80,27 @@ describe("Server", function()

it("should not work when an unexisting plugin is being enabled along with an existing one", function()
replace_conf_property("plugins_available", {"queryauth", "wot-wat"})

assert.has_error(function()
spec_helper.start_kong(SERVER_CONF, true)
end, "The following plugin has been enabled in the configuration but is not installed on the system: wot-wat")
end)

it("should not work when a plugin is being used in the DB but it's not in the configuration", function()
replace_conf_property("plugins_available", {"queryauth", "basicauth", "headerauth", "tcplog", "udplog", "filelog"})
spec_helper.prepare_db()

spec_helper.prepare_db(SERVER_CONF)

assert.has_error(function()
spec_helper.start_kong(SERVER_CONF, true)
end, "You are using a plugin that has not been enabled in the configuration: ratelimiting")
end)

it("should work the used plugins are enabled", function()
replace_conf_property("plugins_available", {"ratelimiting", "queryauth", "headerauth", "basicauth", "tcplog", "udplog", "filelog"})
spec_helper.prepare_db()

spec_helper.prepare_db(SERVER_CONF)

local result, exit_code = spec_helper.start_kong(SERVER_CONF, true)
assert.are.same(0, exit_code)
end)
Expand Down
21 changes: 12 additions & 9 deletions spec/spec_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _M.envs = {}
function _M.add_env(conf_file)
local env_configuration, env_factory = utils.load_configuration_and_dao(conf_file)
_M.envs[conf_file] = {
conf_file = conf_file,
configuration = env_configuration,
migrations = Migrations(env_factory),
faker = Faker(env_factory),
Expand Down Expand Up @@ -56,29 +57,31 @@ function _M.os_execute(command)
end

function _M.start_kong(conf_file, skip_wait)
local conf_file = conf_file and conf_file or TEST_CONF_FILE
local result, exit_code = _M.os_execute(KONG_BIN.." -c "..conf_file.." start")
local env = _M.get_env(conf_file)
local result, exit_code = _M.os_execute(KONG_BIN.." start -c "..env.conf_file)

if exit_code ~= 0 then
error("spec_helper cannot start kong: "..result)
end

if not skip_wait then
os.execute("while ! [ `pgrep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done")
os.execute("while ! [ -f "..env.configuration.pid_file.." ]; do sleep 1; done")
end

return result, exit_code
end

function _M.stop_kong(conf_file)
local conf_file = conf_file and conf_file or TEST_CONF_FILE
local result, exit_code = _M.os_execute(KONG_BIN.." -c "..conf_file.." stop")
local env = _M.get_env(conf_file)
local result, exit_code = _M.os_execute(KONG_BIN.." stop -c "..env.conf_file)

if exit_code ~= 0 then
error("spec_helper cannot stop kong: "..result)
end

os.execute("while [ `pgrep nginx | grep -c -v grep` -gt 0 ]; do sleep 1; done")
os.execute("while [ -f "..env.configuration.pid_file.." ]; do sleep 1; done")

return result, exit_code
end

--
Expand All @@ -104,20 +107,20 @@ function _M.prepare_db(conf_file)
env.faker:seed()
end

function _M.drop_db()
function _M.drop_db(conf_file)
local env = _M.get_env(conf_file)
local err = env.dao_factory:drop()
if err then
error(err)
end
end

function _M.seed_db(random_amount)
function _M.seed_db(conf_file, random_amount)
local env = _M.get_env(conf_file)
env.faker:seed(random_amount)
end

function _M.reset_db()
function _M.reset_db(conf_file)
local env = _M.get_env(conf_file)
env.migrations:reset(function(_, err)
if err then
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/dao/cassandra_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ describe("Cassandra DAO #dao #cassandra", function()

setup(function()
spec_helper.drop_db()
spec_helper.seed_db(100)
spec_helper.seed_db(nil, 100)
end)

describe_all_collections(function(type, collection)
Expand Down Expand Up @@ -522,7 +522,7 @@ describe("Cassandra DAO #dao #cassandra", function()

setup(function()
spec_helper.drop_db()
spec_helper.seed_db(100)
spec_helper.seed_db(nil, 100)
end)

describe_all_collections(function(type, collection)
Expand Down Expand Up @@ -799,7 +799,7 @@ describe("Cassandra DAO #dao #cassandra", function()

setup(function()
spec_helper.drop_db()
spec_helper.seed_db(100)
spec_helper.seed_db(nil, 100)
end)

it("should find distinct plugins", function()
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/migrations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("Migrations #tools", function()

setup(function()
utils.write_to_file(fixtures_path, fixture_migration)
local mig_files = utils.retrieve_files(migrations_path, '.lua')
local mig_files = utils.retrieve_files(migrations_path, { file_pattern = ".lua" })
for _, mig in ipairs(mig_files) do
table.insert(migrations_names, mig:match("[^/]*$"))
end
Expand Down
Loading

0 comments on commit dc2c7fb

Please sign in to comment.