add_block_preprocessor()

in t/kubernetes/discovery/kubernetes.t [28:116]


add_block_preprocessor(sub {
    my ($block) = @_;

    my $apisix_yaml = $block->apisix_yaml // <<_EOC_;
routes: []
#END
_EOC_

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

    my $main_config = $block->main_config // <<_EOC_;
env MyPort=6443;
env KUBERNETES_SERVICE_HOST=127.0.0.1;
env KUBERNETES_SERVICE_PORT=6443;
env KUBERNETES_CLIENT_TOKEN=$::token_value;
env KUBERNETES_CLIENT_TOKEN_FILE=$::token_file;
_EOC_

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

    my $config = $block->config // <<_EOC_;

        location /compare {
            content_by_lua_block {
                local http = require("resty.http")
                local core = require("apisix.core")
                local local_conf = require("apisix.core.config_local").local_conf()

                local function deep_compare(tbl1, tbl2)
                    if tbl1 == tbl2 then
                        return true
                    elseif type(tbl1) == "table" and type(tbl2) == "table" then
                        for key1, value1 in pairs(tbl1) do
                            local value2 = tbl2[key1]
                            if value2 == nil then
                                -- avoid the type call for missing keys in tbl2 by directly comparing with nil
                                return false
                            elseif value1 ~= value2 then
                                if type(value1) == "table" and type(value2) == "table" then
                                    if not deep_compare(value1, value2) then
                                        return false
                                    end
                                else
                                    return false
                                end
                            end
                        end
                        for key2, _ in pairs(tbl2) do
                            if tbl1[key2] == nil then
                                return false
                            end
                        end
                        return true
                    end

                    return false
                end

                ngx.req.read_body()
                local request_body = ngx.req.get_body_data()
                local expect = core.json.decode(request_body)
                local current = local_conf.discovery.kubernetes
                if deep_compare(expect,current) then
                  ngx.say("true")
                else
                  ngx.say("false, current is ",core.json.encode(current,true))
                end
            }
        }

        location /update_token {
            content_by_lua_block {
                local token_file = "$::token_file"
                local file = io.open(token_file, "w")
                file:write("invalid_token_value")
                file:close()
                ngx.sleep(3)
                file = io.open(token_file, "w")
                local token_value = [[$::token_value]]
                file:write(token_value)
                file:close()
            }
        }

_EOC_

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

});