Skip to content

2. Running Tests

Gary Archer edited this page Sep 6, 2024 · 2 revisions

1. Test Setup

Run the following command to install the Test::Nginx Perl module:

cpan Test::Nginx

2. Run Tests

Then run this command from the root folder to execute all NGINX tests developed in the t folder.
This spins up a Docker based instance of the Curity Identity Server, so a license file is needed.

export LICENSE_FILE_PATH=~/Desktop/license.json
make test

3. Test Execution

The above command will run prove, passing in the test or test directory (t). NGINX must be in the system path; the tests will run the first nginx command that's found or bail if none is located. Also, the tests assume that the module is statically linked with NGINX. Before running them, be sure that the module is linked into the NGINX binary. Also, debug logging must be compiled into NGINX for some tests in config.t to pass. (This is the case if nginx -V includes --with-debug in the output.

4. Nginx Test Framework

See the Test::Nginx and OpenResty Testing docs to understand syntax.

5. Test Behavior

Tests in the curity.t module call a dummy upstream API at http://localhost:8080/anything.
An opaque access token is retrieved from the Curity Identity Server at the start of the test:

my $response = $ua->post("http://localhost:8443/oauth/v2/oauth-token", {
    "client_id" => "test-client",
    "client_secret" => "secret1",
    "grant_type" => "client_credentials"
});

Tests that expect a successful response, assert that a JWT was forwarded:

my ($response) = @_;
if ($response =~ /Authorization": "[Bb]earer ey/) {
    return "GOOD"; # A JWT (which starts with "ey") was forwarded to the back-end
}

6. Troubleshoot Failed Tests

If one test out of many is failing, then edit the testing/test/run.sh file to run only a single file:

PATH=$(NGINX_SRC_DIR)/objs:$$PATH prove -v -f t/curity.t

Then add the ONLY directive to limit test execution to the single test that is failing:

--- config
location /t {
    ...
}

--- request
GET /t

--- ONLY

View the t/servroot/conf/nginx.conf file to see the deployed configuration for a test.
If required, add ngx_log_error statements to C code, then look at test logs at t/servroot/logs/error.log.
If you get cryptic permission errors or locked files, delete the t/servroot folder.