add_block_preprocessor()

in t/APISIX.pm [241:626]


add_block_preprocessor(sub {
    my ($block) = @_;
    my $wait_etcd_sync = $block->wait_etcd_sync // 0.1;

    if ($block->apisix_yaml && (!defined $block->yaml_config)) {
        $user_yaml_config = <<_EOC_;
apisix:
    node_listen: 1984
    enable_admin: false
deployment:
    role: data_plane
    role_data_plane:
        config_provider: yaml
_EOC_
    }

    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;;";
_EOC_

    my $main_config = $block->main_config // <<_EOC_;
worker_rlimit_core  500M;
env ENABLE_ETCD_AUTH;
env APISIX_PROFILE;
env PATH; # for searching external plugin runner's binary
env TEST_NGINX_HTML_DIR;
env OPENSSL_BIN;
_EOC_


    if ($version =~ m/\/apisix-nginx-module/) {
        $main_config .= <<_EOC_;
thread_pool grpc-client-nginx-module threads=1;

lua {
    lua_shared_dict prometheus-metrics 15m;
}
_EOC_
    }

    # set default `timeout` to 5sec
    my $timeout = $block->timeout // 5;
    $block->set_value("timeout", $timeout);

    my $stream_tls_request = $block->stream_tls_request;
    if ($stream_tls_request) {
        # generate a springboard to send tls stream request
        $block->set_value("stream_conf_enable", 1);
        # avoid conflict with stream_enable
        $block->set_value("stream_enable");
        $block->set_value("request", "GET /stream_tls_request");

        my $sni = "nil";
        if ($block->stream_sni) {
            $sni = '"' . $block->stream_sni . '"';
        }
        chomp $stream_tls_request;

        my $repeat = "1";
        if (defined $block->stream_session_reuse) {
            $repeat = "2";
        }

        my $config = <<_EOC_;
            location /stream_tls_request {
                content_by_lua_block {
                    local sess
                    for _ = 1, $repeat do
                        local sock = ngx.socket.tcp()
                        local ok, err = sock:connect("127.0.0.1", 2005)
                        if not ok then
                            ngx.say("failed to connect: ", err)
                            return
                        end

                        sess, err = sock:sslhandshake(sess, $sni, false)
                        if not sess then
                            ngx.say("failed to do SSL handshake: ", err)
                            return
                        end

                        local bytes, err = sock:send("$stream_tls_request")
                        if not bytes then
                            ngx.say("send stream request error: ", err)
                            return
                        end
                        local data, err = sock:receive("*a")
                        if not data then
                            sock:close()
                            ngx.say("receive stream response error: ", err)
                            return
                        end
                        ngx.print(data)
                        sock:close()
                    end
                }
            }
_EOC_
        $block->set_value("config", $config)
    }

    # handling shell exec in test Nginx
    my $exec_snippet = $block->exec;
    if ($exec_snippet) {
        # capture the stdin & max response size
        my $stdin = "nil";
        if ($block->stdin) {
            $stdin = '"' . $block->stdin . '"';
        }
        chomp  $exec_snippet;
        chomp $stdin;

        my $max_size = $block->max_size // 8096;
        $block->set_value("request", "GET /exec_request");

        my $config = $block->config // '';
        $config .= <<_EOC_;
            location /exec_request {
                content_by_lua_block {
                    local shell = require("resty.shell")
                    local ok, stdout, stderr, reason, status = shell.run([[ $exec_snippet ]], $stdin, @{[$timeout*1000]}, $max_size)
                    if not ok then
                        ngx.log(ngx.WARN, "failed to execute the script with status: " .. status .. ", reason: " .. reason .. ", stderr: " .. stderr)
                        return
                    end
                    ngx.print(stdout)
                }
            }
_EOC_

        $block->set_value("config", $config)
    }

    my $stream_enable = $block->stream_enable;
    if ($block->stream_request) {
        # Like stream_tls_request, if stream_request is given, automatically enable stream
        $stream_enable = 1;
    }

    my $stream_conf_enable = $block->stream_conf_enable;
    my $extra_stream_config = $block->extra_stream_config // '';
    my $stream_upstream_code = $block->stream_upstream_code // <<_EOC_;
            local sock = ngx.req.socket()
            local data = sock:receive("1")
            ngx.say("hello world")
_EOC_

    my $stream_config = $block->stream_config // <<_EOC_;
    $lua_deps_path
    lua_socket_log_errors off;

    lua_shared_dict lrucache-lock-stream 10m;
    lua_shared_dict plugin-limit-conn-stream 10m;
    lua_shared_dict etcd-cluster-health-check-stream 10m;
    lua_shared_dict worker-events-stream 10m;

    lua_shared_dict kubernetes-stream 1m;
    lua_shared_dict kubernetes-first-stream 1m;
    lua_shared_dict kubernetes-second-stream 1m;
    lua_shared_dict tars-stream 1m;

    upstream apisix_backend {
        server 127.0.0.1:1900;
        balancer_by_lua_block {
            apisix.stream_balancer_phase()
        }
    }
_EOC_

    my $stream_extra_init_by_lua_start = $block->stream_extra_init_by_lua_start // "";

    my $stream_init_by_lua_block = $block->stream_init_by_lua_block // <<_EOC_;
        if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
            require("luacov.runner")("t/apisix.luacov")
            jit.off()
        end

        require "resty.core"

        $stream_extra_init_by_lua_start

        apisix = require("apisix")
        local args = {
            dns_resolver = $dns_addrs_tbl_str,
        }
        apisix.stream_init(args)
_EOC_

    my $stream_extra_init_by_lua = $block->stream_extra_init_by_lua // "";
    my $stream_extra_init_worker_by_lua = $block->stream_extra_init_worker_by_lua // "";

    $stream_config .= <<_EOC_;
    init_by_lua_block {
        $test_default_config
        $stream_init_by_lua_block
        $stream_extra_init_by_lua
    }
    init_worker_by_lua_block {
        apisix.stream_init_worker()
        $stream_extra_init_worker_by_lua
    }

    $extra_stream_config

    server {
        listen unix:$apisix_home/t/servroot/logs/stream_worker_events.sock;
        access_log off;
        content_by_lua_block {
            require("resty.events.compat").run()
        }
    }

    # fake server, only for test
    server {
        listen 1995;

        content_by_lua_block {
            $stream_upstream_code
        }
    }
_EOC_

    if (defined $stream_enable) {
        $block->set_value("stream_config", $stream_config);
    }

    my $custom_trusted_cert = $block->custom_trusted_cert // 'cert/apisix.crt';

    my $stream_server_config = $block->stream_server_config // <<_EOC_;
    listen 2005 ssl;
    ssl_certificate             cert/apisix.crt;
    ssl_certificate_key         cert/apisix.key;
    lua_ssl_trusted_certificate cert/apisix.crt;

    ssl_certificate_by_lua_block {
        apisix.stream_ssl_phase()
    }

    preread_by_lua_block {
        -- wait for etcd sync
        ngx.sleep($wait_etcd_sync)
        apisix.stream_preread_phase()
    }

    proxy_pass apisix_backend;
_EOC_

    if ($version =~ m/\/apisix-nginx-module/) {
        $stream_server_config .= <<_EOC_;
    proxy_ssl_server_name on;
    proxy_ssl_name \$upstream_sni;
    set \$upstream_sni "apisix_backend";
_EOC_
    }

    $stream_server_config .= <<_EOC_;
    log_by_lua_block {
        apisix.stream_log_phase()
    }
_EOC_

    if (defined $stream_enable) {
        $block->set_value("stream_server_config", $stream_server_config);
    }

    if (defined $stream_conf_enable) {
        $main_config .= <<_EOC_;
stream {
$stream_config
    server {
        listen 1985;
        $stream_server_config
    }
}
_EOC_
    }

    $block->set_value("main_config", $main_config);

    # The new directive is introduced here to modify the schema
    # before apisix validate in require("apisix")
    # Todo: merge extra_init_by_lua_start and extra_init_by_lua
    my $extra_init_by_lua_start = $block->extra_init_by_lua_start // "";

    my $extra_init_by_lua = $block->extra_init_by_lua // "";
    my $init_by_lua_block = $block->init_by_lua_block // <<_EOC_;
    if os.getenv("APISIX_ENABLE_LUACOV") == "1" then
        require("luacov.runner")("t/apisix.luacov")
        jit.off()
    end

    require "resty.core"

    $extra_init_by_lua_start

    apisix = require("apisix")
    local args = {
        dns_resolver = $dns_addrs_tbl_str,
    }
    apisix.http_init(args)

    -- set apisix_lua_home into constants module
    -- it may be used by plugins to determine the work path of apisix
    local constants = require("apisix.constants")
    constants.apisix_lua_home = "$apisix_home"

    $extra_init_by_lua
_EOC_

    my $extra_init_worker_by_lua = $block->extra_init_worker_by_lua // "";

    my $http_config = $block->http_config // '';
    $http_config .= <<_EOC_;
    $lua_deps_path

    lua_shared_dict plugin-limit-req 10m;
    lua_shared_dict plugin-limit-count 10m;
    lua_shared_dict plugin-limit-count-reset-header 10m;
    lua_shared_dict plugin-limit-conn 10m;
    lua_shared_dict plugin-ai-rate-limiting 10m;
    lua_shared_dict plugin-ai-rate-limiting-reset-header 10m;
    lua_shared_dict internal-status 10m;
    lua_shared_dict upstream-healthcheck 32m;
    lua_shared_dict worker-events 10m;
    lua_shared_dict lrucache-lock 10m;
    lua_shared_dict balancer-ewma 1m;
    lua_shared_dict balancer-ewma-locks 1m;
    lua_shared_dict balancer-ewma-last-touched-at 1m;
    lua_shared_dict plugin-limit-req-redis-cluster-slot-lock 1m;
    lua_shared_dict plugin-limit-count-redis-cluster-slot-lock 1m;
    lua_shared_dict plugin-limit-conn-redis-cluster-slot-lock 1m;
    lua_shared_dict tracing_buffer 10m;    # plugin skywalking
    lua_shared_dict access-tokens 1m;    # plugin authz-keycloak
    lua_shared_dict discovery 1m;    # plugin authz-keycloak
    lua_shared_dict plugin-api-breaker 10m;
    lua_capture_error_log 1m;    # plugin error-log-logger
    lua_shared_dict etcd-cluster-health-check 10m; # etcd health check
    lua_shared_dict ext-plugin 1m;
    lua_shared_dict kubernetes 1m;
    lua_shared_dict kubernetes-first 1m;
    lua_shared_dict kubernetes-second 1m;
    lua_shared_dict tars 1m;
    lua_shared_dict ocsp-stapling 10m;
    lua_shared_dict mcp-session 10m;
    lua_shared_dict xds-config 1m;
    lua_shared_dict xds-config-version 1m;
    lua_shared_dict cas_sessions 10m;

    proxy_ssl_name \$upstream_host;
    proxy_ssl_server_name on;

    resolver $dns_addrs_str;
    resolver_timeout 5;

    underscores_in_headers on;
    lua_socket_log_errors off;
    client_body_buffer_size 8k;

    variables_hash_bucket_size 128;

    upstream apisix_backend {
        server 0.0.0.1;
_EOC_

    if ($version =~ m/\/apisix-nginx-module/) {
    $http_config .= <<_EOC_;
        keepalive 32;

        balancer_by_lua_block {
            apisix.http_balancer_phase()
        }
    }
_EOC_
    } else {
    $http_config .= <<_EOC_;
        balancer_by_lua_block {
            apisix.http_balancer_phase()
        }

        keepalive 32;
    }

    lua_shared_dict prometheus-metrics 10m;
_EOC_
    }