From 0ab3e53cb9ac246400c396713f20354ca12dbfdb Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Mon, 10 Jun 2019 17:18:04 +0800 Subject: [PATCH] test: added test case about `core.id` and `core.utils` library. --- lua/apisix/core/id.lua | 7 ++++--- lua/apisix/core/utils.lua | 1 - t/core-id.t | 26 ++++++++++++++++++++++++++ t/core-utils.t | 25 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 t/core-id.t create mode 100644 t/core-utils.t diff --git a/lua/apisix/core/id.lua b/lua/apisix/core/id.lua index ac3e368e..27042733 100644 --- a/lua/apisix/core/id.lua +++ b/lua/apisix/core/id.lua @@ -1,6 +1,7 @@ local log = require("apisix.core.log") local uuid = require('resty.jit-uuid') local smatch = string.match +local open = io.open local prefix = ngx.config.prefix() @@ -15,7 +16,7 @@ end local function read_file(path) - local file = io.open(path, "rb") -- r read mode and b binary mode + local file = open(path, "rb") -- r read mode and b binary mode if not file then return nil end @@ -27,7 +28,7 @@ end local function write_file(path, data) - local file = io.open(path ,"w+") + local file = open(path ,"w+") if not file then return nil, "failed to open file[" .. path .. "] for writing" end @@ -46,7 +47,7 @@ function _M.init() end apisix_uid = uuid.generate_v4() - log.warn("not found apisix uid, generate a new one: ", apisix_uid) + log.notice("not found apisix uid, generate a new one: ", apisix_uid) local ok, err = write_file(uid_file_path, apisix_uid) if not ok then diff --git a/lua/apisix/core/utils.lua b/lua/apisix/core/utils.lua index 2d9bc500..2e243f77 100644 --- a/lua/apisix/core/utils.lua +++ b/lua/apisix/core/utils.lua @@ -1,4 +1,3 @@ -local log = require("apisix.core.log") local open = io.open diff --git a/t/core-id.t b/t/core-id.t new file mode 100644 index 00000000..576ccb87 --- /dev/null +++ b/t/core-id.t @@ -0,0 +1,26 @@ +use t::APISix 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +log_level("info"); + +run_tests; + +__DATA__ + +=== TEST 1: sanity +--- config + location /t { + content_by_lua_block { + local core = require("apisix.core") + + ngx.say("uid: ", core.id.get()) + } + } +--- request +GET /t +--- response_body_like eval +qr/uid: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ +--- error_log +not found apisix uid, generate a new one diff --git a/t/core-utils.t b/t/core-utils.t new file mode 100644 index 00000000..de570b25 --- /dev/null +++ b/t/core-utils.t @@ -0,0 +1,25 @@ +use t::APISix 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); +log_level("info"); + +run_tests; + +__DATA__ + +=== TEST 1: sanity +--- config + location /t { + content_by_lua_block { + local get_seed = require("apisix.core.utils").get_seed_from_urandom + + ngx.say("random seed ", get_seed()) + ngx.say("twice: ", get_seed() == get_seed()) + } + } +--- request +GET /t +--- response_body_like eval +qr/random seed \d+\ntwice: false/