Skip to content

Commit

Permalink
test: integration tests for example modules
Browse files Browse the repository at this point in the history
  • Loading branch information
bavshin-f5 committed Oct 30, 2024
1 parent a4150bf commit b79cffd
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 1 deletion.
96 changes: 96 additions & 0 deletions .github/workflows/nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: NGINX

on:
push:
branches:
- master
pull_request:

env:
CARGO_TERM_COLOR: 'always'
RUST_BACKTRACE: '1'
NGX_CONFIGURE: |
auto/configure
--with-compat
--with-debug
--with-http_realip_module
--with-http_ssl_module
--with-http_v2_module
--with-http_v3_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-threads
jobs:
test:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
module:
# static
- dynamic
nginx-ref:
# master
- stable-1.26

steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
ref: ${{ matrix.nginx-ref }}
repository: 'nginx/nginx'
path: 'nginx'
- uses: actions/checkout@v4
with:
repository: 'nginx/nginx-tests'
path: 'nginx/tests'
sparse-checkout: |
lib
- uses: dtolnay/rust-toolchain@stable

- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
nginx/objs/ngx_rust_examples
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Configure nginx with static modules
if: matrix.module == 'static'
working-directory: nginx
run: |
${NGX_CONFIGURE} \
--add-module=${{ github.workspace }}/examples
- name: Configure nginx with dynamic modules
if: matrix.module != 'static'
working-directory: nginx
env:
TEST_NGINX_GLOBALS: |
load_module ${{ github.workspace }}/nginx/objs/ngx_http_awssigv4_module.so;
load_module ${{ github.workspace }}/nginx/objs/ngx_http_curl_module.so;
load_module ${{ github.workspace }}/nginx/objs/ngx_http_upstream_custom_module.so;
run: |
${NGX_CONFIGURE} \
--add-dynamic-module=${{ github.workspace }}/examples
echo "TEST_NGINX_GLOBALS=$TEST_NGINX_GLOBALS" >> $GITHUB_ENV
- name: Build nginx
working-directory: nginx
run: make -j$(nproc)

- name: Run tests
env:
TEST_NGINX_BINARY: ${{ github.workspace }}/nginx/objs/nginx
TEST_NGINX_MODULES: ${{ github.workspace }}/nginx/objs
run: |
prove -v -j$(nproc) -Inginx/tests/lib --state=save examples/t \
|| prove -v -Inginx/tests/lib --state=failed
2 changes: 1 addition & 1 deletion examples/config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if [ $HTTP = YES ]; then

ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
ngx_module_deps=$ngx_module_lib
ngx_module_libs=$ngx_module_lib
ngx_module_libs="$ngx_module_lib -lm"

# Module deps are usually added to the object file targets, but we don't have any
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
Expand Down
73 changes: 73 additions & 0 deletions examples/t/awssig.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/perl

# (C) Nginx, Inc

# Tests for ngx-rust example modules.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(1)
->write_file_expand('nginx.conf', <<"EOF");
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
awssigv4_access_key my-access-key;
awssigv4_secret_key my-secret-key;
awssigv4_s3_bucket my-bucket;
awssigv4_s3_endpoint s3.example.com;
location / {
awssigv4 on;
proxy_pass http://127.0.0.1:8081;
}
}
server {
listen 127.0.0.1:8081;
server_name localhost;
add_header x-amz-date \$http_x_amz_date;
add_header x-authorization \$http_authorization;
location / { }
}
}
EOF

$t->write_file('index.html', '');
$t->run();

###############################################################################

like(http_get('/'), qr/x-authorization: AWS4.*Credential=my-access-key/i,
'awssig header');

###############################################################################
70 changes: 70 additions & 0 deletions examples/t/curl.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/perl

# (C) Nginx, Inc

# Tests for ngx-rust example modules.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http/)->plan(2)
->write_file_expand('nginx.conf', <<"EOF");
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
location / {
curl on;
}
}
}
EOF

$t->write_file('index.html', '');
$t->run();

###############################################################################

like(get('/', 'curl/1.2.3'), qr/403 Forbidden/, 'curl UA forbidden');
like(get('/', 'MSIE 6.0'), qr/200 OK/, 'other UA allowed');

###############################################################################

sub get {
my ($url, $ua, $extra) = @_;
return http(<<EOF);
GET $url HTTP/1.1
Host: localhost
Connection: close
User-Agent: $ua
EOF
}

###############################################################################
79 changes: 79 additions & 0 deletions examples/t/upstream.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/perl

# (C) Nginx, Inc

# Tests for ngx-rust example modules.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http proxy/)->plan(2)
->write_file_expand('nginx.conf', <<"EOF");
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
upstream u {
server 127.0.0.1:8081;
custom 32;
}
server {
listen 127.0.0.1:8080;
server_name localhost;
error_log %%TESTDIR%%/e_debug.log debug;
location / {
proxy_pass http://u;
}
}
server {
listen 127.0.0.1:8081;
server_name localhost;
location / { }
}
}
EOF

$t->write_file('index.html', '');
$t->run();

###############################################################################

like(http_get('/'), qr/200 OK/, 'custom upstream');

$t->stop();

SKIP: {
skip "no --with-debug", 1 unless $t->has_module('--with-debug');

like($t->read_file('e_debug.log'), qr/CUSTOM UPSTREAM request/,
'log - custom upstream');
}

###############################################################################

0 comments on commit b79cffd

Please sign in to comment.