Add dockerfile and framework for unit testing
diff --git a/Dockerfile-test b/Dockerfile-test
new file mode 100644
index 0000000..1b321f8
--- /dev/null
+++ b/Dockerfile-test
@@ -0,0 +1,32 @@
+# Dockerfile for creating apigateway image for running unit tests
+
+FROM apicgw/apigateway:latest
+
+RUN echo "... Installing test dependencies ..." \
+ && apk add --update gcc readline-dev git unzip ncurses-dev \
+ && LUA_VERSION=5.1.5 && LUAROCKS_VERSION=2.4.1 \
+ && curl -R -O https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz \
+ && tar zxf lua-${LUA_VERSION}.tar.gz \
+ && cd lua-${LUA_VERSION} \
+ && make linux install \
+ && wget http://keplerproject.github.io/luarocks/releases/luarocks-${LUAROCKS_VERSION}.tar.gz \
+ && tar -xvzf ./luarocks-${LUAROCKS_VERSION}.tar.gz \
+ && cd luarocks-${LUAROCKS_VERSION} \
+ && ./configure \
+ && make build \
+ && make install \
+ && cd /etc/api-gateway/tests \
+ && curl -R -O https://raw.githubusercontent.com/bsm/fakengx/master/fakengx.lua
+
+RUN echo "... Installing lua rocks ..." \
+ && luarocks install busted \
+ && luarocks install luabitop \
+ && luarocks install luasocket \
+ && luarocks install sha1 \
+ && luarocks install md5
+
+COPY init-test.sh /etc/init-container-test.sh
+ONBUILD COPY init-test.sh /etc/init-container-test.sh
+RUN chmod +x /etc/init-container-test.sh
+
+ENTRYPOINT ["/etc/init-container-test.sh"]
\ No newline at end of file
diff --git a/Makefile b/Makefile
index c1940d6..a4ec8a1 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,15 @@
docker:
docker build -t apicgw/apigateway .
+.PHONY: docker-test
+docker-test:
+ docker build -f ./Dockerfile-test -t apicgw/apigateway-test .
+ docker run --rm --name="apigateway-test" apicgw/apigateway-test:latest
+
+.PHONY: docker-test-run
+docker-test-run:
+ docker run --rm --name="apigateway-test" apicgw/apigateway-test:latest
+
.PHONY: docker-ssh
docker-ssh:
docker run -ti --entrypoint='bash' apicgw/apigateway:latest
diff --git a/api-gateway-config/conf.d/management_apis.conf b/api-gateway-config/conf.d/management_apis.conf
index 3893b80..dc8c714 100644
--- a/api-gateway-config/conf.d/management_apis.conf
+++ b/api-gateway-config/conf.d/management_apis.conf
@@ -35,8 +35,8 @@
location /resources {
access_by_lua_block {
- mgmt = require("management")
- requestMethod = ngx.req.get_method()
+ local mgmt = require("management")
+ local requestMethod = ngx.req.get_method()
if requestMethod == "GET" then
mgmt.getResource()
elseif requestMethod == "PUT" then
@@ -55,8 +55,8 @@
location /subscriptions {
access_by_lua_block {
- mgmt = require("management")
- requestMethod = ngx.req.get_method()
+ local mgmt = require("management")
+ local requestMethod = ngx.req.get_method()
if requestMethod == "PUT" then
mgmt.addSubscription()
elseif requestMethod == "DELETE" then
@@ -71,14 +71,14 @@
location /subscribe {
access_by_lua_block {
- mgmt = require("management")
+ local mgmt = require("management")
mgmt.subscribe()
}
}
location /unsubscribe {
access_by_lua_block {
- mgmt = require("management")
+ local mgmt = require("management")
mgmt.unsubscribe()
}
}
diff --git a/api-gateway-config/scripts/lua/lib/request.lua b/api-gateway-config/scripts/lua/lib/request.lua
index 6a3d936..55de64b 100644
--- a/api-gateway-config/scripts/lua/lib/request.lua
+++ b/api-gateway-config/scripts/lua/lib/request.lua
@@ -31,7 +31,7 @@
-- @param msg error message
function err(code, msg)
ngx.status = code
- ngx.say(utils.concatStrings({"Error: ", msg}))
+ ngx.print(utils.concatStrings({"Error: ", msg}))
ngx.exit(ngx.status)
end
@@ -40,7 +40,7 @@
-- @param obj object to return
function success(code, obj)
ngx.status = code
- ngx.say(obj)
+ ngx.print(obj)
ngx.exit(ngx.status)
end
diff --git a/api-gateway-config/scripts/lua/management.lua b/api-gateway-config/scripts/lua/management.lua
index 076785f..ef7aad6 100644
--- a/api-gateway-config/scripts/lua/management.lua
+++ b/api-gateway-config/scripts/lua/management.lua
@@ -61,7 +61,6 @@
end
-- Convert json into Lua table
local decoded = utils.convertJSONBody(args)
-
-- Error handling for required fields in the request body
local gatewayMethod = decoded.gatewayMethod
if not gatewayMethod then
diff --git a/api-gateway-config/tests/test.lua b/api-gateway-config/tests/test.lua
new file mode 100644
index 0000000..e586fc7
--- /dev/null
+++ b/api-gateway-config/tests/test.lua
@@ -0,0 +1,50 @@
+-- Copyright (c) 2016 IBM. All rights reserved.
+--
+-- Permission is hereby granted, free of charge, to any person obtaining a
+-- copy of this software and associated documentation files (the "Software"),
+-- to deal in the Software without restriction, including without limitation
+-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
+-- and/or sell copies of the Software, and to permit persons to whom the
+-- Software is furnished to do so, subject to the following conditions:
+--
+-- The above copyright notice and this permission notice shall be included in
+-- all copies or substantial portions of the Software.
+--
+-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+-- DEALINGS IN THE SOFTWARE.
+
+-- Unit tests for the apigateway using the busted framework.
+-- @author Alex Song (songs)
+
+package.path = package.path .. ';/usr/local/share/lua/5.2/?.lua' ..
+ ';/usr/local/api-gateway/lualib/resty/?.lua;/etc/api-gateway/scripts/lua/?.lua'
+package.cpath = package.cpath .. ';/usr/local/lib/lua/5.2/?.so;/usr/local/api-gateway/lualib/?.so'
+
+local fakengx = require 'fakengx'
+local request = require 'lib/request'
+
+describe('Testing Request module', function()
+ before_each(function()
+ -- Mock ngx object for each test
+ _G.ngx = fakengx.new()
+ end)
+ it('should return correct error response', function()
+ local code = 500
+ local msg = 'Internal server error\n'
+ request.err(code, msg)
+ assert.are.equal(ngx._body, 'Error: ' .. msg)
+ assert.are.equal(ngx._exit, code)
+ end)
+ it('should return correct success response', function()
+ local code = 200
+ local msg ='Success!\n'
+ request.success(code, msg)
+ assert.are.equal(ngx._body, msg)
+ assert.are.equal(ngx._exit, code)
+ end)
+end)
diff --git a/init-test.sh b/init-test.sh
new file mode 100644
index 0000000..407f042
--- /dev/null
+++ b/init-test.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+cd /etc/api-gateway/tests
+busted test.lua
\ No newline at end of file