|
| 1 | +#!/usr/bin/env resty |
| 2 | + |
| 3 | +setmetatable(_G, nil) |
| 4 | + |
| 5 | +local pl_path = require("pl.path") |
| 6 | +local pl_file = require("pl.file") |
| 7 | + |
| 8 | +local tools_system = require("kong.tools.system") |
| 9 | + |
| 10 | +local emmy_debugger = require("kong.tools.emmy_debugger") |
| 11 | + |
| 12 | +local cert_path do |
| 13 | + local busted_cert_file = pl_path.tmpname() |
| 14 | + local busted_cert_content = pl_file.read("spec/fixtures/kong_spec.crt") |
| 15 | + |
| 16 | + local system_cert_path, err = tools_system.get_system_trusted_certs_filepath() |
| 17 | + if system_cert_path then |
| 18 | + busted_cert_content = busted_cert_content .. "\n" .. pl_file.read(system_cert_path) |
| 19 | + end |
| 20 | + |
| 21 | + pl_file.write(busted_cert_file, busted_cert_content) |
| 22 | + cert_path = busted_cert_file |
| 23 | +end |
| 24 | + |
| 25 | +local DEFAULT_RESTY_FLAGS=string.format(" -c 4096 --http-conf 'lua_ssl_trusted_certificate %s;' ", cert_path) |
| 26 | + |
| 27 | +if not os.getenv("KONG_BUSTED_RESPAWNED") then |
| 28 | + -- initial run, so go update the environment |
| 29 | + local script = {} |
| 30 | + for line in io.popen("set"):lines() do |
| 31 | + local ktvar, val = line:match("^KONG_TEST_([^=]*)=(.*)") |
| 32 | + if ktvar then |
| 33 | + -- reinserted KONG_TEST_xxx as KONG_xxx; append |
| 34 | + table.insert(script, "export KONG_" .. ktvar .. "=" ..val) |
| 35 | + end |
| 36 | + |
| 37 | + local var = line:match("^(KONG_[^=]*)") |
| 38 | + local var_for_spec = line:match("^(KONG_SPEC_[^=]*)") |
| 39 | + if var and not var_for_spec then |
| 40 | + -- remove existing KONG_xxx and KONG_TEST_xxx variables; prepend |
| 41 | + table.insert(script, 1, "unset " .. var) |
| 42 | + end |
| 43 | + end |
| 44 | + -- add cli recursion detection |
| 45 | + table.insert(script, "export KONG_BUSTED_RESPAWNED=1") |
| 46 | + |
| 47 | + -- XXX EE |
| 48 | + table.insert(script, "export KONG_IS_TESTING=1") |
| 49 | + |
| 50 | + -- rebuild the invoked commandline, while inserting extra resty-flags |
| 51 | + local resty_flags = DEFAULT_RESTY_FLAGS |
| 52 | + local cmd = { "exec", "/usr/bin/env", "resty" } |
| 53 | + local cmd_prefix_count = #cmd |
| 54 | + for i = 0, #arg do |
| 55 | + if arg[i]:sub(1, 12) == "RESTY_FLAGS=" then |
| 56 | + resty_flags = arg[i]:sub(13, -1) |
| 57 | + |
| 58 | + else |
| 59 | + table.insert(cmd, "'" .. arg[i] .. "'") |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + -- create shared dict |
| 64 | + resty_flags = resty_flags .. require("spec.fixtures.shared_dict") |
| 65 | + |
| 66 | + if resty_flags then |
| 67 | + table.insert(cmd, cmd_prefix_count+1, resty_flags) |
| 68 | + end |
| 69 | + |
| 70 | + table.insert(script, table.concat(cmd, " ")) |
| 71 | + |
| 72 | + -- recurse cli command, with proper variables (un)set for clean testing |
| 73 | + local _, _, rc = os.execute(table.concat(script, "; ")) |
| 74 | + os.exit(rc) |
| 75 | +end |
| 76 | + |
| 77 | +pcall(require, "luarocks.loader") |
| 78 | + |
| 79 | +if os.getenv("BUSTED_EMMY_DEBUGGER") then |
| 80 | + emmy_debugger.init({ |
| 81 | + debugger = os.getenv("BUSTED_EMMY_DEBUGGER"), |
| 82 | + host = os.getenv("BUSTED_EMMY_DEBUGGER_HOST"), |
| 83 | + port = os.getenv("BUSTED_EMMY_DEBUGGER_PORT"), |
| 84 | + wait = true, |
| 85 | + source_path = os.getenv("BUSTED_EMMY_DEBUGGER_SOURCE_PATH"), |
| 86 | + source_path_mapping = os.getenv("BUSTED_EMMY_DEBUGGER_SOURCE_PATH_MAPPING"), |
| 87 | + }) |
| 88 | +end |
| 89 | + |
| 90 | +require("kong.globalpatches")({ |
| 91 | + cli = true, |
| 92 | + rbusted = true |
| 93 | +}) |
| 94 | + |
| 95 | +-- some libraries used in test like spec/helpers |
| 96 | +-- calls cosocket in module level, and as LuaJIT's |
| 97 | +-- `require` is implemented in C, this throws |
| 98 | +-- "attempt to yield across C-call boundary" error |
| 99 | +-- the following pure-lua implementation is to bypass |
| 100 | +-- this limitation, without need to modify all tests |
| 101 | +_G.require = require "spec.require".require |
| 102 | + |
| 103 | +-- Busted command-line runner |
| 104 | +require 'busted.runner'({ standalone = false }) |
| 105 | + |
| 106 | + |
| 107 | +-- vim: set ft=lua ts=2 sw=2 sts=2 et : |
0 commit comments