2019-10-31 09:27:28 +08:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
2019-09-10 09:42:55 +08:00
|
|
|
package t::APISIX;
|
2019-04-11 16:53:21 +08:00
|
|
|
|
|
|
|
use lib 'lib';
|
|
|
|
use Cwd qw(cwd);
|
|
|
|
use Test::Nginx::Socket::Lua::Stream -Base;
|
|
|
|
|
2019-06-19 15:51:28 +08:00
|
|
|
repeat_each(1);
|
2019-06-17 23:17:41 +08:00
|
|
|
log_level('info');
|
|
|
|
no_long_string();
|
|
|
|
no_shuffle();
|
2019-08-21 23:10:34 +08:00
|
|
|
worker_connections(128);
|
2019-06-17 23:17:41 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $pwd = cwd();
|
|
|
|
|
|
|
|
sub read_file($) {
|
|
|
|
my $infile = shift;
|
|
|
|
open my $in, $infile
|
|
|
|
or die "cannot open $infile for reading: $!";
|
|
|
|
my $cert = do { local $/; <$in> };
|
|
|
|
close $in;
|
|
|
|
$cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $yaml_config = read_file("conf/config.yaml");
|
2019-06-22 10:44:07 +08:00
|
|
|
my $ssl_crt = read_file("conf/cert/apisix.crt");
|
|
|
|
my $ssl_key = read_file("conf/cert/apisix.key");
|
2019-06-22 08:24:02 +08:00
|
|
|
$yaml_config =~ s/node_listen: 9080/node_listen: 1984/;
|
2019-06-22 10:44:07 +08:00
|
|
|
$yaml_config =~ s/enable_heartbeat: true/enable_heartbeat: false/;
|
2019-06-22 08:24:02 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
|
|
|
|
add_block_preprocessor(sub {
|
|
|
|
my ($block) = @_;
|
2019-09-12 13:27:18 +08:00
|
|
|
my $wait_etcd_sync = $block->wait_etcd_sync // 0.1;
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2019-08-19 14:02:33 +08:00
|
|
|
my $main_config = $block->main_config // <<_EOC_;
|
|
|
|
worker_rlimit_core 500M;
|
|
|
|
working_directory $pwd;
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("main_config", $main_config);
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
my $stream_enable = $block->stream_enable;
|
|
|
|
my $stream_config = $block->stream_config // <<_EOC_;
|
|
|
|
lua_package_path "$pwd/deps/share/lua/5.1/?.lua;$pwd/lua/?.lua;$pwd/t/?.lua;/usr/share/lua/5.1/?.lua;;";
|
|
|
|
lua_package_cpath "$pwd/deps/lib/lua/5.1/?.so;$pwd/deps/lib64/lua/5.1/?.so;/usr/lib64/lua/5.1/?.so;;";
|
|
|
|
|
|
|
|
lua_socket_log_errors off;
|
|
|
|
|
|
|
|
upstream apisix_backend {
|
|
|
|
server 127.0.0.1:1900;
|
|
|
|
balancer_by_lua_block {
|
|
|
|
apisix.stream_balancer_phase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init_by_lua_block {
|
2019-09-16 10:58:27 +08:00
|
|
|
-- if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
|
|
|
|
-- require("luacov.runner")("t/apisix.luacov")
|
|
|
|
-- jit.off()
|
|
|
|
-- end
|
|
|
|
|
2019-09-12 13:27:18 +08:00
|
|
|
require "resty.core"
|
|
|
|
|
|
|
|
apisix = require("apisix")
|
|
|
|
apisix.stream_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init_worker_by_lua_block {
|
|
|
|
apisix.stream_init_worker()
|
|
|
|
}
|
|
|
|
|
|
|
|
# fake server, only for test
|
|
|
|
server {
|
|
|
|
listen 1995;
|
|
|
|
|
|
|
|
content_by_lua_block {
|
2019-09-16 10:58:27 +08:00
|
|
|
local sock = ngx.req.socket()
|
|
|
|
local data = sock:receive("1")
|
2019-09-12 13:27:18 +08:00
|
|
|
ngx.say("hello world")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
if (defined $stream_enable) {
|
|
|
|
$block->set_value("stream_config", $stream_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $stream_server_config = $block->stream_server_config // <<_EOC_;
|
|
|
|
preread_by_lua_block {
|
|
|
|
-- wait for etcd sync
|
|
|
|
ngx.sleep($wait_etcd_sync)
|
|
|
|
apisix.stream_preread_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy_pass apisix_backend;
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.stream_log_phase()
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
if (defined $stream_enable) {
|
|
|
|
$block->set_value("stream_server_config", $stream_server_config);
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:20:57 +08:00
|
|
|
my $init_by_lua_block = $block->init_by_lua_block // <<_EOC_;
|
2019-07-03 15:44:24 +08:00
|
|
|
if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
|
|
|
|
require("luacov.runner")("t/apisix.luacov")
|
|
|
|
jit.off()
|
|
|
|
end
|
|
|
|
|
2019-09-16 10:58:27 +08:00
|
|
|
require "resty.core"
|
|
|
|
|
2019-06-11 12:20:57 +08:00
|
|
|
apisix = require("apisix")
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_init()
|
2019-06-11 12:20:57 +08:00
|
|
|
_EOC_
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $http_config = $block->http_config // '';
|
|
|
|
$http_config .= <<_EOC_;
|
2019-06-20 16:43:01 +08:00
|
|
|
lua_package_path "$pwd/deps/share/lua/5.1/?.lua;$pwd/lua/?.lua;$pwd/t/?.lua;/usr/share/lua/5.1/?.lua;;";
|
2019-07-15 08:24:35 +08:00
|
|
|
lua_package_cpath "$pwd/deps/lib/lua/5.1/?.so;$pwd/deps/lib64/lua/5.1/?.so;/usr/lib64/lua/5.1/?.so;;";
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2019-07-17 23:52:15 +08:00
|
|
|
lua_shared_dict plugin-limit-req 10m;
|
|
|
|
lua_shared_dict plugin-limit-count 10m;
|
|
|
|
lua_shared_dict plugin-limit-conn 10m;
|
|
|
|
lua_shared_dict prometheus-metrics 10m;
|
|
|
|
lua_shared_dict upstream-healthcheck 32m;
|
|
|
|
lua_shared_dict worker-events 10m;
|
2019-06-10 14:20:24 +08:00
|
|
|
|
2019-11-02 09:05:38 +08:00
|
|
|
resolver 8.8.8.8 114.114.114.114 ipv6=off;
|
2019-06-10 14:57:11 +08:00
|
|
|
resolver_timeout 5;
|
|
|
|
|
2019-06-11 23:47:00 +08:00
|
|
|
lua_socket_log_errors off;
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
upstream apisix_backend {
|
|
|
|
server 0.0.0.1;
|
|
|
|
balancer_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_balancer_phase()
|
2019-06-14 10:27:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
keepalive 32;
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
init_by_lua_block {
|
2019-06-11 12:20:57 +08:00
|
|
|
$init_by_lua_block
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
init_worker_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
require("apisix").http_init_worker()
|
2019-04-11 16:53:21 +08:00
|
|
|
}
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-07-25 16:06:07 +08:00
|
|
|
# fake server, only for test
|
2019-06-14 10:27:45 +08:00
|
|
|
server {
|
|
|
|
listen 1980;
|
2019-06-17 23:17:41 +08:00
|
|
|
listen 1981;
|
|
|
|
listen 1982;
|
2019-09-27 15:57:04 +08:00
|
|
|
|
|
|
|
server_tokens off;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
|
|
|
require("lib.server").go()
|
|
|
|
}
|
|
|
|
|
|
|
|
more_clear_headers Date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
listen 1983 ssl;
|
|
|
|
ssl_certificate cert/apisix.crt;
|
|
|
|
ssl_certificate_key cert/apisix.key;
|
|
|
|
lua_ssl_trusted_certificate cert/apisix.crt;
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-06-22 10:44:07 +08:00
|
|
|
server_tokens off;
|
|
|
|
|
2019-06-17 23:17:41 +08:00
|
|
|
location / {
|
|
|
|
content_by_lua_block {
|
|
|
|
require("lib.server").go()
|
|
|
|
}
|
2019-06-22 10:44:07 +08:00
|
|
|
|
|
|
|
more_clear_headers Date;
|
2019-06-14 10:27:45 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:17:41 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("http_config", $http_config);
|
|
|
|
|
2019-06-22 10:44:07 +08:00
|
|
|
my $TEST_NGINX_HTML_DIR = $ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
|
2019-08-24 09:10:11 +08:00
|
|
|
my $ipv6_listen_conf = '';
|
|
|
|
if (defined $block->listen_ipv6) {
|
|
|
|
$ipv6_listen_conf = "listen \[::1\]:12345;"
|
|
|
|
}
|
2019-06-22 10:44:07 +08:00
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
my $config = $block->config // '';
|
2019-06-15 22:34:26 +08:00
|
|
|
$config .= <<_EOC_;
|
2019-08-24 09:10:11 +08:00
|
|
|
$ipv6_listen_conf
|
2019-06-22 10:44:07 +08:00
|
|
|
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
|
|
|
|
|
|
|
|
ssl_certificate cert/apisix.crt;
|
|
|
|
ssl_certificate_key cert/apisix.key;
|
|
|
|
lua_ssl_trusted_certificate cert/apisix.crt;
|
|
|
|
|
|
|
|
ssl_certificate_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_ssl_phase()
|
2019-06-22 10:44:07 +08:00
|
|
|
}
|
|
|
|
|
2019-06-17 21:08:17 +08:00
|
|
|
location = /apisix/nginx_status {
|
2019-06-22 08:24:02 +08:00
|
|
|
allow 127.0.0.0/24;
|
2019-06-17 21:08:17 +08:00
|
|
|
access_log off;
|
|
|
|
stub_status;
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
location /apisix/admin {
|
|
|
|
content_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_admin()
|
2019-06-13 12:01:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
location / {
|
2019-06-15 22:34:26 +08:00
|
|
|
set \$upstream_scheme 'http';
|
|
|
|
set \$upstream_host \$host;
|
|
|
|
set \$upstream_upgrade '';
|
|
|
|
set \$upstream_connection '';
|
|
|
|
set \$upstream_uri '';
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
access_by_lua_block {
|
2019-06-14 10:27:45 +08:00
|
|
|
-- wait for etcd sync
|
2019-06-15 22:34:26 +08:00
|
|
|
ngx.sleep($wait_etcd_sync)
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_access_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_http_version 1.1;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_set_header Host \$upstream_host;
|
|
|
|
proxy_set_header Upgrade \$upstream_upgrade;
|
|
|
|
proxy_set_header Connection \$upstream_connection;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_pass_header Server;
|
|
|
|
proxy_pass_header Date;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_pass \$upstream_scheme://apisix_backend\$upstream_uri;
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
header_filter_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_header_filter_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 23:10:34 +08:00
|
|
|
body_filter_by_lua_block {
|
|
|
|
apisix.http_body_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
|
|
|
apisix.http_log_phase()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
location \@grpc_pass {
|
|
|
|
access_by_lua_block {
|
|
|
|
apisix.grpc_access_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
grpc_set_header Content-Type application/grpc;
|
|
|
|
grpc_socket_keepalive on;
|
|
|
|
grpc_pass grpc://apisix_backend;
|
|
|
|
|
|
|
|
header_filter_by_lua_block {
|
|
|
|
apisix.http_header_filter_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
body_filter_by_lua_block {
|
|
|
|
apisix.http_body_filter_phase()
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
log_by_lua_block {
|
2019-07-01 17:24:38 +08:00
|
|
|
apisix.http_log_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("config", $config);
|
|
|
|
|
2019-09-09 17:42:20 +08:00
|
|
|
my $user_apisix_yaml = $block->apisix_yaml // "";
|
|
|
|
if ($user_apisix_yaml) {
|
|
|
|
$user_apisix_yaml = <<_EOC_;
|
|
|
|
>>> ../conf/apisix.yaml
|
|
|
|
$user_apisix_yaml
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
my $user_yaml_config = $block->yaml_config // $yaml_config;
|
2019-10-09 11:26:54 +08:00
|
|
|
my $user_debug_config = $block->debug_config // "";
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $user_files = $block->user_files;
|
|
|
|
$user_files .= <<_EOC_;
|
2019-10-09 11:26:54 +08:00
|
|
|
>>> ../conf/debug.yaml
|
|
|
|
$user_debug_config
|
2019-04-11 16:53:21 +08:00
|
|
|
>>> ../conf/config.yaml
|
2019-06-16 07:32:25 +08:00
|
|
|
$user_yaml_config
|
2019-06-22 10:44:07 +08:00
|
|
|
>>> ../conf/cert/apisix.crt
|
|
|
|
$ssl_crt
|
|
|
|
>>> ../conf/cert/apisix.key
|
|
|
|
$ssl_key
|
2019-09-09 17:42:20 +08:00
|
|
|
$user_apisix_yaml
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("user_files", $user_files);
|
|
|
|
|
|
|
|
$block;
|
|
|
|
});
|
|
|
|
|
|
|
|
1;
|