Skip to content
21 changes: 21 additions & 0 deletions docs/en/latest/internal/testing-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ GET /index.html
no valid upstream node
```

## Custom Lua Paths

To test custom plugins in custom directories, you can specify custom Lua paths:

**Using block definitions:**

```
--- extra_lua_path: /custom/path/?.lua
--- extra_lua_cpath: /custom/path/?.so
```

**Using YAML config:**

```
--- extra_yaml_config
apisix:
extra_lua_path: "/custom/path/?.lua"
```

Block definitions take precedence over YAML config when both are provided.

## Preparing the upstream

To test the code, we need to provide a mock upstream.
Expand Down
48 changes: 46 additions & 2 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,53 @@ deployment:
_EOC_
}

my $extra_lua_path = "";
my $extra_lua_cpath = "";

# Support for extra_lua_path / extra_lua_cpath:
# These allow tests to load custom plugins from non-standard directories.
#
# Method 1 - Block definition (preferred, takes precedence):
# --- extra_lua_path
# /path/to/custom/plugins/?.lua;
#
# Method 2 - Via extra_yaml_config:
# --- extra_yaml_config
# apisix:
# extra_lua_path: "/path/to/custom/plugins/?.lua;"
#
# The extracted paths are prepended to lua_package_path / lua_package_cpath,
# consistent with the runtime behavior in apisix/cli/ops.lua.

# Method 1: Block definition (preferred)
if ($block->extra_lua_path) {
$extra_lua_path = $block->extra_lua_path;
$extra_lua_path .= ";" unless $extra_lua_path =~ /;$/;
}
if ($block->extra_lua_cpath) {
$extra_lua_cpath = $block->extra_lua_cpath;
$extra_lua_cpath .= ";" unless $extra_lua_cpath =~ /;$/;
}

# Method 2: Extract from extra_yaml_config if block definition not provided
if (!$extra_lua_path && $block->extra_yaml_config) {
my $extra_yaml = $block->extra_yaml_config;
if ($extra_yaml =~ m/^\s*extra_lua_path:\s*["\']?([^"\'\n]+)["\']?/m) {
$extra_lua_path = $1;
$extra_lua_path .= ";" unless $extra_lua_path =~ /;$/;
}
}
if (!$extra_lua_cpath && $block->extra_yaml_config) {
my $extra_yaml = $block->extra_yaml_config;
if ($extra_yaml =~ m/^\s*extra_lua_cpath:\s*["\']?([^"\'\n]+)["\']?/m) {
$extra_lua_cpath = $1;
$extra_lua_cpath .= ";" unless $extra_lua_cpath =~ /;$/;
}
}

my $lua_deps_path = $block->lua_deps_path // <<_EOC_;
lua_package_path "$apisix_home/?.lua;$apisix_home/?/init.lua;$apisix_home/deps/share/lua/5.1/?/init.lua;$apisix_home/deps/share/lua/5.1/?.lua;$apisix_home/apisix/?.lua;$apisix_home/t/?.lua;$apisix_home/t/xrpc/?.lua;$apisix_home/t/xrpc/?/init.lua;;";
lua_package_cpath "$apisix_home/?.so;$apisix_home/deps/lib/lua/5.1/?.so;$apisix_home/deps/lib64/lua/5.1/?.so;;";
lua_package_path "$extra_lua_path$apisix_home/?.lua;$apisix_home/?/init.lua;$apisix_home/deps/share/lua/5.1/?/init.lua;$apisix_home/deps/share/lua/5.1/?.lua;$apisix_home/apisix/?.lua;$apisix_home/t/?.lua;$apisix_home/t/xrpc/?.lua;$apisix_home/t/xrpc/?/init.lua;;";
lua_package_cpath "$extra_lua_cpath$apisix_home/?.so;$apisix_home/deps/lib/lua/5.1/?.so;$apisix_home/deps/lib64/lua/5.1/?.so;;";
_EOC_

my $main_config = $block->main_config // <<_EOC_;
Expand Down
Loading
Loading