Skip to content

Commit

Permalink
add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Akayeshmantha committed Apr 27, 2020
1 parent f3c1234 commit c67eba5
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 3 deletions.
6 changes: 3 additions & 3 deletions apisix/plugins/syslog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local schema = {
properties = {
host = {type = "string"},
port = {type = "integer"},
flush_limit = {type = "integer", minimum = 1024, default = 4096},
flush_limit = {type = "integer", minimum = 1, default = 4096},
drop_limit = {type = "integer", default = 1048576},
timeout = {type = "integer", minimum = 1, default = 3},
sock_type = {type = "string", default = "tcp"},
Expand Down Expand Up @@ -80,13 +80,13 @@ function _M.log(conf)
tls = conf.tls,
}
if not ok then
core.log.error("error when initiating the sys logger processor: ", err)
core.log.error("failed when initiating the sys logger processor", err)
end
end

local bytes, err = logger.log(core.json.encode(entry))
if err then
core.log.error("failed to log message:", err)
core.log.error("failed to log message", err)
end
end

Expand Down
242 changes: 242 additions & 0 deletions t/plugin/syslog.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
run_tests;

__DATA__

=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.syslog")
local ok, err = plugin.check_schema({
host = "127.0.0.1",
port = 3000,
})
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
done
--- no_error_log
[error]



=== TEST 2: missing port
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.syslog")
local ok, err = plugin.check_schema({host = "127.0.0.1"})
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
property "port" is required
done
--- no_error_log
[error]



=== TEST 3: wrong type of string
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.syslog")
local ok, err = plugin.check_schema({
host = "127.0.0.1",
port = "3000",
})
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- response_body
property "port" validation failed: wrong type: expected integer, got string
done
--- no_error_log
[error]

=== TEST 4: add plugin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"syslog": {
"host" : "127.0.0.1",
"port" : 5044
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]],
[[{
"node": {
"value": {
"plugins": {
"syslog": {
"host" : "127.0.0.1",
"port" : 5044
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
},
"key": "/apisix/routes/1"
},
"action": "set"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]

=== TEST 5: access
--- request
GET /hello
--- response_body
hello world
--- no_error_log
[error]
--- wait: 0.2

=== TEST 6: flush manually
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.syslog")
local ok, err = plugin.flush_syslog()
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- request
GET /t
--- no_error_log
[error]

=== TEST 7: small flush_limit, instant flush
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"syslog": {
"host" : "127.0.0.1",
"port" : 5044,
"flush_limit" : 1
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]],
[[{
"node": {
"value": {
"plugins": {
"syslog": {
"host" : "127.0.0.1",
"port" : 5044,
"flush_limit" : 1
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
},
"key": "/apisix/routes/1"
},
"action": "set"
}]]
)

if code >= 300 then
ngx.status = code
end

ngx.say(body)

local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local res, err = httpc:request_uri(uri, {method = "GET"})
}
}
--- request
GET /hello
hello world
--- no_error_log
[error]
--- wait: 0.2

0 comments on commit c67eba5

Please sign in to comment.