Skip to content

Commit

Permalink
Merge pull request #458 from 3scale/test-base-class
Browse files Browse the repository at this point in the history
[t] use one base class
  • Loading branch information
mikz authored Oct 19, 2017
2 parents 040d8cb + f86d015 commit fc0599e
Show file tree
Hide file tree
Showing 27 changed files with 213 additions and 325 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
working_directory: /opt/app-root/apicast
steps:
- checkout
- run: rm -rf lua_modules
- restore_cache:
keys:
- apicast-rocks-{{ arch }}-{{ checksum "Roverfile.lock" }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- URI params in POST requests are now taken into account when matching mapping rules [PR #437](https://github.com/3scale/apicast/pull/437)
- Increased number of background timers and connections in the cosocket pool [PR #290](https://github.com/3scale/apicast/pull/290)
- Make OAuth tokens TTL configurable [PR #448](https://github.com/3scale/apicast/pull/448)
- Detect when being executed in Test::Nginx and use default backend accordingly [PR #458](https://github.com/3scale/apicast/pull/458)

### Fixed

Expand Down
37 changes: 28 additions & 9 deletions apicast/src/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local len = string.len
local format = string.format
local pairs = pairs
local type = type
local unpack = unpack
local error = error
local tostring = tostring
local next = next
Expand Down Expand Up @@ -129,12 +128,35 @@ end

local Service = require 'configuration.service'

local noop = function() end
local function readonly_table(table)
return setmetatable({}, { __newindex = noop, __index = table })
end

local empty_t = readonly_table()
local fake_backend = readonly_table({ endpoint = 'http://127.0.0.1:8081' })

local function backend_endpoint(proxy)
local backend_endpoint_override = resty_url.parse(env.get("BACKEND_ENDPOINT_OVERRIDE"))
local backend = backend_endpoint_override or proxy.backend or empty_t

if backend == empty_t then
local test_nginx_server_port = env.get('TEST_NGINX_SERVER_PORT')
if test_nginx_server_port then
return { endpoint = 'http://127.0.0.1:' .. test_nginx_server_port }
else
return fake_backend
end
else
return { endpoint = backend.endpoint or tostring(backend), host = backend.host }
end
end

function _M.parse_service(service)
local backend_version = tostring(service.backend_version)
local proxy = service.proxy or {}
local backend = proxy.backend or {}
local backend_endpoint_override = env.get("BACKEND_ENDPOINT_OVERRIDE")
local _, _, _, backend_host_override = unpack(resty_url.split(backend_endpoint_override) or {})
local proxy = service.proxy or empty_t
local backend = backend_endpoint(proxy)


return Service.new({
id = tostring(service.id or 'default'),
Expand All @@ -161,10 +183,7 @@ function _M.parse_service(service)
type = service.backend_authentication_type,
value = service.backend_authentication_value
},
backend = {
endpoint = backend_endpoint_override or backend.endpoint,
host = backend_host_override or backend.host
},
backend = backend,
oidc = {
issuer_endpoint = proxy.oidc_issuer_endpoint ~= ngx.null and proxy.oidc_issuer_endpoint
},
Expand Down
43 changes: 33 additions & 10 deletions spec/configuration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ describe('Configuration object', function()
assert.same('example.com', config.hostname_rewrite)
end)

it('overrides backend endpoint from ENV', function()
env.set('BACKEND_ENDPOINT_OVERRIDE', 'https://backend.example.com')

local config = configuration.parse_service({ proxy = {
backend = { endpoint = 'http://example.com', host = 'foo.example.com' }
}})

assert.same('https://backend.example.com', config.backend.endpoint)
assert.same('backend.example.com', config.backend.host)
end)

it('has a default message, content-type, and status for the auth failed error', function()
local config = configuration.parse_service({})
Expand Down Expand Up @@ -67,6 +57,39 @@ describe('Configuration object', function()
assert.same('text/plain; charset=utf-8', config.limits_exceeded_headers)
assert.equals(429, config.limits_exceeded_status)
end)

describe('backend', function()
it('defaults to fake backend', function()
local config = configuration.parse_service({ proxy = {
backend = nil
}})

assert.same('http://127.0.0.1:8081', config.backend.endpoint)
assert.falsy(config.backend.host)
end)

it('is overriden from ENV', function()
env.set('BACKEND_ENDPOINT_OVERRIDE', 'https://backend.example.com')

local config = configuration.parse_service({ proxy = {
backend = { endpoint = 'http://example.com', host = 'foo.example.com' }
}})

assert.same('https://backend.example.com', config.backend.endpoint)
assert.same('backend.example.com', config.backend.host)
end)

it('detects TEST_NGINX_SERVER_PORT', function()
env.set('TEST_NGINX_SERVER_PORT', '1954')

local config = configuration.parse_service({ proxy = {
backend = nil
}})

assert.same('http://127.0.0.1:1954', config.backend.endpoint)
assert.falsy(config.backend.host)
end)
end)
end)

describe('.filter_services', function()
Expand Down
22 changes: 6 additions & 16 deletions t/001-management.t
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";
use TestAPIcast 'no_plan';

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_MANAGEMENT_CONFIG} = "$apicast/conf.d/management.conf";

require("$pwd/t/dns.pl");

log_level('debug');
repeat_each(2);
no_root_location();
run_tests();

__DATA__
Expand Down Expand Up @@ -177,8 +167,8 @@ POST /boot
{"status":"ok","config":{"services":[{"id":42}]}}
--- error_code: 200
--- udp_listen: 1953
--- udp_reply eval
$::dns->("localhost.local", "127.0.0.1", 60)
--- udp_reply dns
[ "localhost.local", "127.0.0.1", 60 ]
--- no_error_log
[error]
Expand Down Expand Up @@ -206,8 +196,8 @@ POST /test
{"status":"ok","config":{"services":[{"id":42}]}}
--- error_code: 200
--- udp_listen: 1953
--- udp_reply eval
$::dns->("localhost.local", "127.0.0.1", 60)
--- udp_reply dns
[ "localhost.local", "127.0.0.1", 60 ]
--- no_error_log
[error]
Expand Down
17 changes: 4 additions & 13 deletions t/002-backend.t
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

our $HttpConfig = qq{
lua_package_path "$apicast/src/?.lua;;";
};
our $backendConfig = "$apicast/conf.d/backend.conf";

repeat_each(2);
run_tests();

__DATA__
=== TEST 1: backend
This is just a simple demonstration of the
echo directive provided by ngx_http_echo_module.
--- http_config eval: $::HttpConfig
--- config eval: "include $::backendConfig;"
--- config
include $TEST_NGINX_BACKEND_CONFIG;
--- request
GET /transactions/authrep.xml
--- response_body
Expand Down
21 changes: 4 additions & 17 deletions t/003-apicast.t
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$apicast/http.d/upstream.conf";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";

require("$pwd/t/dns.pl");

log_level('debug');
repeat_each(2);
no_root_location();
run_tests();

__DATA__
Expand Down Expand Up @@ -634,8 +621,8 @@ GET /t?user_key=val
all ok
--- error_code: 200
--- udp_listen: 1953
--- udp_reply eval
$::dns->("localhost.example.com", "127.0.0.1", 3600)
--- udp_reply dns
[ "localhost.example.com", "127.0.0.1", 3600 ]
--- no_error_log
[error]

Expand Down
13 changes: 2 additions & 11 deletions t/004-apicast-path-routing.t
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$apicast/http.d/upstream.conf";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";
$ENV{APICAST_PATH_ROUTING_ENABLED} = '1';

log_level('debug');
repeat_each(1); # Can't be 2 as the second run would hit the cache
no_root_location();
run_tests();

__DATA__
Expand Down
13 changes: 2 additions & 11 deletions t/004-custom-config.t
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";

log_level('debug');
repeat_each(2);
run_tests();

__DATA__
Expand Down
18 changes: 5 additions & 13 deletions t/005-apicast-oauth.t
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$apicast/http.d/upstream.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";
use lib 't';
use TestAPIcast 'no_plan';

$ENV{TEST_NGINX_REDIS_HOST} ||= $ENV{REDIS_HOST} || "127.0.0.1";
$ENV{TEST_NGINX_RESOLVER} ||= `grep nameserver /etc/resolv.conf | awk '{print \$2}' | head -1 | tr '\n' ' '`;
$ENV{BACKEND_ENDPOINT_OVERRIDE} ||= "http://127.0.0.1:$Test::Nginx::Util::ServerPortForClient/backend";

env_to_nginx('BACKEND_ENDPOINT_OVERRIDE');

log_level('debug');
repeat_each(2);
no_root_location();
run_tests();

__DATA__
Expand Down
15 changes: 2 additions & 13 deletions t/006-apicast-subset-of-services.t
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$apicast/http.d/upstream.conf";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";

log_level('debug');
repeat_each(2);
no_root_location();
run_tests();

__DATA__
Expand Down
22 changes: 6 additions & 16 deletions t/007-apicast-upstream-balancer.t
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use lib 't';
use TestAPIcast 'no_plan';

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";
$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";

require("$pwd/t/dns.pl");

log_level('debug');
repeat_each(2);
no_root_location();
run_tests();


__DATA__

=== TEST 1: resolver
Expand Down Expand Up @@ -42,8 +32,8 @@ location /t {
}
}
--- udp_listen: 1953
--- udp_reply eval
$::dns->("localhost", "127.0.0.1")
--- udp_reply dns
[ "localhost", "127.0.0.1" ]
--- request
GET /t
--- response_body
Expand Down Expand Up @@ -135,8 +125,8 @@ location /t {
proxy_pass http://upstream/api;
}
--- udp_listen: 1953
--- udp_reply eval
$::dns->("localhost", "127.0.0.1")
--- udp_reply dns
[ "localhost", "127.0.0.1" ]
--- request
GET /t
--- response_body
Expand Down
19 changes: 3 additions & 16 deletions t/008-apicast-request-logs.t
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);
use Sys::Hostname;

my $pwd = cwd();
my $apicast = $ENV{TEST_NGINX_APICAST_PATH} || "$pwd/apicast";

our $host = hostname;
$ENV{TEST_NGINX_LUA_PATH} = "$apicast/src/?.lua;;";
$ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$apicast/http.d/upstream.conf";
$ENV{TEST_NGINX_BACKEND_CONFIG} = "$apicast/conf.d/backend.conf";
$ENV{TEST_NGINX_APICAST_CONFIG} = "$apicast/conf.d/apicast.conf";

log_level('debug');
repeat_each(2);
no_root_location();
use lib 't';
use TestAPIcast 'no_plan';

run_tests();

__DATA__
Expand Down
Loading

0 comments on commit fc0599e

Please sign in to comment.