Skip to content

fix: support extra_lua_path in test framework#12759

Open
grapestore wants to merge 11 commits intoapache:masterfrom
grapestore:fix/extra-lua-path-in-tests
Open

fix: support extra_lua_path in test framework#12759
grapestore wants to merge 11 commits intoapache:masterfrom
grapestore:fix/extra-lua-path-in-tests

Conversation

@grapestore
Copy link
Copy Markdown
Contributor

Description

This PR adds support for setting custom Lua paths (extra_lua_path and extra_lua_cpath) in test files, aligning the test framework behavior with APISIX runtime configuration.

Previously, while APISIX runtime supported apisix.extra_lua_path in config.yaml to load custom plugins from specified directories, the test framework (t/APISIX.pm) did not respect this configuration. This made it impossible to write tests for custom plugins located in custom directories.

This PR implements two complementary methods for setting custom Lua paths in tests:

Method 1: Block definitions (preferred)
--- extra_lua_path: /custom/path/?.lua
--- extra_lua_cpath: /custom/path/?.soMethod 2: Automatic parsing from extra_yaml_config
--- extra_yaml_config
apisix:
extra_lua_path: "/custom/path/?.lua"
extra_lua_cpath: "/custom/path/?.so"The implementation:

  • Prepends custom paths to lua_package_path and lua_package_cpath (matching runtime behavior)
  • Block definitions take precedence when both methods are used
  • Enables testing custom plugins without modifying core APISIX paths

Which issue(s) this PR fixes:

Fixes #12389

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Test Coverage:
Added comprehensive test suite in t/admin/extra-lua-path.t covering:

  • Path addition via block definitions
  • YAML configuration parsing
  • Simultaneous lua_path and lua_cpath configuration
  • Correct path prepending behavior
  • Precedence rules between configuration methods

Backward Compatibility:
This change is fully backward compatible. It only adds new optional block definitions (extra_lua_path, extra_lua_cpath) and parsing logic for existing extra_yaml_config. All existing tests continue to work without modification.

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 19, 2025
Copy link
Copy Markdown
Contributor

@Baoyuantop Baoyuantop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codes LGTM, please fix the failed CI

@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @grapestore, you can merge the latest main branch to make CI pass.

@grapestore grapestore force-pushed the fix/extra-lua-path-in-tests branch from 2b64c82 to 9c80b2c Compare November 20, 2025 10:17
@grapestore
Copy link
Copy Markdown
Contributor Author

I’ve updated my branch to follow the latest master branch

@grapestore
Copy link
Copy Markdown
Contributor Author

Could you please take a look?

Copy link
Copy Markdown
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a real case to cover this case

@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Dec 10, 2025
@grapestore
Copy link
Copy Markdown
Contributor Author

@membphis

Here is a real scenario:

We use the official APISIX Docker image while developing custom plugins.
The custom plugin directory is mounted into the container, and in tests we add that directory to extra_lua_path so the plugin can be loaded.

Example:

Mount local custom plugins to /usr/local/apisix/custom/plugins
In the test:
apisix.extra_lua_path: "/usr/local/apisix/custom/plugins/?.lua"
The issue is that the test framework does not prepend this extra_lua_path to lua_package_path.
As a result, during tests the custom plugin path is not included in package.path, and require fails to load the plugin.
This works correctly in a real APISIX runtime, but the test environment cannot see the plugin, causing inconsistent behavior.

This scenario clearly shows the problem where extra_lua_path is not applied in the test framework.

@membphis
Copy link
Copy Markdown
Member

I mean:

you should add a test case to cove the case you show me right now

@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @grapestore, following up on the previous review comments. Please let us know if you have any updates. Thank you.

@grapestore
Copy link
Copy Markdown
Contributor Author

I added it @Baoyuantop @membphis

@grapestore grapestore requested a review from Baoyuantop January 3, 2026 04:21
@Baoyuantop Baoyuantop added awaiting review and removed wait for update wait for the author's response in this issue/PR user responded labels Jan 4, 2026
@grapestore grapestore force-pushed the fix/extra-lua-path-in-tests branch from 0518a14 to 10e81dd Compare January 8, 2026 04:37
@grapestore
Copy link
Copy Markdown
Contributor Author

update rebase

@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @grapestore, the newly added test execution failed.

@Baoyuantop Baoyuantop added wait for update wait for the author's response in this issue/PR and removed awaiting review labels Feb 2, 2026
@grapestore
Copy link
Copy Markdown
Contributor Author

check plz

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for custom Lua paths in the APISIX test framework, addressing a gap where the test framework didn't respect extra_lua_path and extra_lua_cpath configurations that are available in APISIX runtime. This enables developers to write tests for custom plugins located in custom directories.

Changes:

  • Extended the test framework preprocessor in t/APISIX.pm to extract and prepend custom Lua paths from block definitions or YAML configuration
  • Added comprehensive test coverage in t/admin/extra-lua-path.t to verify all configuration methods and edge cases
  • Created a test plugin test-extra-path to demonstrate real-world usage

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
t/APISIX.pm Core implementation that extracts extra_lua_path and extra_lua_cpath from block definitions or extra_yaml_config and prepends them to lua_package_path and lua_package_cpath
t/admin/extra-lua-path.t Comprehensive test suite covering block definitions, YAML config parsing, precedence rules, path prepending, and real plugin loading
t/plugin/custom-plugins/apisix/plugins/test-extra-path.lua Simple test plugin that sets custom headers to verify custom plugin loading works correctly
docs/en/latest/internal/testing-framework.md Documentation update explaining both configuration methods and precedence rules

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread t/APISIX.pm Outdated

# Method 1: Block definition (preferred)
if ($block->extra_lua_path) {
$extra_lua_path = $block->extra_lua_path . ";";
Copy link

Copilot AI Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code unconditionally appends a semicolon to the extra_lua_path value. If a user provides a path that already ends with a semicolon (e.g., /custom/path/?.lua;), this will result in a double semicolon (/custom/path/?.lua;;). While this is not harmful in practice (double semicolons in Lua package paths just add the default path), it's inconsistent with the runtime behavior in apisix/cli/ops.lua which checks if the path already ends with a semicolon before appending one.

Consider checking if the path already ends with a semicolon before appending, similar to the runtime implementation. The same applies to extra_lua_cpath on line 282.

Copilot uses AI. Check for mistakes.
@Baoyuantop Baoyuantop removed wait for update wait for the author's response in this issue/PR user responded labels Feb 24, 2026
@Baoyuantop Baoyuantop added the wait for update wait for the author's response in this issue/PR label Mar 11, 2026
@Baoyuantop
Copy link
Copy Markdown
Contributor

Hi @grapestore, following up on the previous review comments. Please let us know if you have any updates. Thank you.

Copy link
Copy Markdown
Member

@moonming moonming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @grapestore, thank you for adding extra_lua_path support to the test framework!

This improves the developer experience for plugin developers who need custom Lua paths in their tests.

Please:

  1. Rebase on the latest master
  2. Add a brief description of how to use this feature in the test framework (a comment in t/APISIX.pm or the PR description)

Thank you!

@grapestore grapestore force-pushed the fix/extra-lua-path-in-tests branch from 3ed05ea to 7adf5bf Compare March 18, 2026 01:45
inkyu.bae added 11 commits March 31, 2026 22:28
Add support for setting custom Lua paths in test files through
two complementary methods:

1. Block definitions (preferred):
   --- extra_lua_path: /custom/path/?.lua
   --- extra_lua_cpath: /custom/path/?.so

2. YAML configuration parsing:
   --- extra_yaml_config
   apisix:
     extra_lua_path: "/custom/path/?.lua"

The implementation prepends custom paths to lua_package_path and
lua_package_cpath, aligning test behavior with APISIX runtime where
extra_lua_path allows loading custom plugins from specified directories.

Block definitions take precedence when both methods are used, ensuring
explicit test configuration overrides YAML settings.

Added comprehensive test suite (t/admin/extra-lua-path.t) covering:
- Path addition via block definitions
- YAML configuration parsing
- Simultaneous lua_path and lua_cpath configuration
- Correct path prepending behavior
- Precedence rules between methods

This enables testing custom plugins in custom directories without
modifying core APISIX paths.

Fixes apache#12389
@grapestore grapestore force-pushed the fix/extra-lua-path-in-tests branch from 7adf5bf to 0a0fecb Compare March 31, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. wait for update wait for the author's response in this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: cannot set apisix.extra_lua_path in tests via yaml_config or extra_yaml_config

5 participants