Repository: trafficserver Updated Branches: refs/heads/master 6a111c1e6 -> d3d903aa8
TS-3876: add unit test framework info for lua scripts for ts_lua plugin Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/d3d903aa Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/d3d903aa Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/d3d903aa Branch: refs/heads/master Commit: d3d903aa8289c72c04f28fb0b8b3804ee01d8c06 Parents: 6a111c1 Author: Kit Chan <[email protected]> Authored: Tue Oct 27 23:28:38 2015 -0700 Committer: Kit Chan <[email protected]> Committed: Tue Oct 27 23:28:38 2015 -0700 ---------------------------------------------------------------------- doc/reference/plugins/ts_lua.en.rst | 31 ++++++++ plugins/experimental/ts_lua/ci/.luacov | 79 +++++++++++++++++++++ plugins/experimental/ts_lua/ci/README | 27 +++++++ plugins/experimental/ts_lua/ci/module.lua | 49 +++++++++++++ plugins/experimental/ts_lua/ci/module_test.lua | 68 ++++++++++++++++++ plugins/experimental/ts_lua/ci/script.lua | 22 ++++++ plugins/experimental/ts_lua/ci/script_test.lua | 39 ++++++++++ 7 files changed, 315 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/doc/reference/plugins/ts_lua.en.rst ---------------------------------------------------------------------- diff --git a/doc/reference/plugins/ts_lua.en.rst b/doc/reference/plugins/ts_lua.en.rst index a76a7ae..e20514e 100644 --- a/doc/reference/plugins/ts_lua.en.rst +++ b/doc/reference/plugins/ts_lua.en.rst @@ -2871,6 +2871,37 @@ maintaining the lua state in the ATS core. `TOP <#ts-lua-plugin>`_ +Notes on Unit Testing Lua scripts for ATS Lua Plugin +==================================================== +Follow the steps below to use busted framework to run some unit tests on sample scripts and modules + +* Build and install lua 5.1.5 using the source code from here - http://www.lua.org/ftp/lua-5.1.tar.gz + +* Build and install luarocks 2.2.2 from here - https://github.com/keplerproject/luarocks/wiki/Download + +* Run "sudo luarocks install busted" + +* Run "sudo luarocks install luacov" + +* "cd trafficserver/plugins/experimental/ts_lua/ci" + +* Run "busted -c module_test.lua; luacov". It will produce "luacov.report.out" containing the code coverage for +* "module.lua" + +* Run "busted -c script_test.lua; luacov". It will produce "luacov.report.out" containing the code coverage for +* "script.lua" + +Reference for further information + +* Busted - http://olivinelabs.com/busted/ + +* Specifications for asserts/mocks/stubs/etc inside busted framework - +https://github.com/Olivine-Labs/luassert/tree/master/spec + +* luacov - https://luarocks.org/modules/hisham/luacov + +`TOP <#ts-lua-plugin>`_ + More docs ========= http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/.luacov ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/.luacov b/plugins/experimental/ts_lua/ci/.luacov new file mode 100644 index 0000000..0d92930 --- /dev/null +++ b/plugins/experimental/ts_lua/ci/.luacov @@ -0,0 +1,79 @@ +-- 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. + + +--- Global configuration file. Copy, customize and store in your +-- project folder as '.luacov' for project specific configuration +-- @class module +-- @name luacov.defaults +return { + + -- default filename to load for config options if not provided + -- only has effect in 'luacov.defaults.lua' + ["configfile"] = ".luacov", + + -- filename to store stats collected + ["statsfile"] = "luacov.stats.out", + + -- filename to store report + ["reportfile"] = "luacov.report.out", + + -- luacov.stats file updating frequency. + -- The lower this value - the more frequenty results will be written out to luacov.stats + -- You may want to reduce this value for short lived scripts (to for example 2) to avoid losing coverage data. + ["savestepsize"] = 100, + + -- Run reporter on completion? (won't work for ticks) + runreport = false, + + -- Delete stats file after reporting? + deletestats = false, + + -- Process Lua code loaded from raw strings + -- (that is, when the 'source' field in the debug info + -- does not start with '@') + codefromstrings = false, + + -- Patterns for files to include when reporting + -- all will be included if nothing is listed + -- (exclude overrules include, do not include + -- the .lua extension, path separator is always '/') + ["include"] = { + }, + + -- Patterns for files to exclude when reporting + -- all will be included if nothing is listed + -- (exclude overrules include, do not include + -- the .lua extension, path separator is always '/') + ["exclude"] = { + "luacov$", + "luacov/reporter$", + "luacov/defaults$", + "luacov/runner$", + "luacov/stats$", + "luacov/tick$", + "busted/.*", + "luarocks/.*", + "luassert/.*", + "pl/.*", + "mediator$", + "term/.*", + "say/.*", + ".*_test$", + }, + + +} http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/README ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/README b/plugins/experimental/ts_lua/ci/README new file mode 100644 index 0000000..a96e0ac --- /dev/null +++ b/plugins/experimental/ts_lua/ci/README @@ -0,0 +1,27 @@ +Notes on Unit Testing Lua scripts for ATS Lua Plugin + +Follow the steps below to use busted framework to run some unit tests on sample scripts and modules + +1) Build and install lua 5.1.5 using the source code from here - http://www.lua.org/ftp/lua-5.1.tar.gz + +2) Build and install luarocks 2.2.2 from here - https://github.com/keplerproject/luarocks/wiki/Download + +3) Run "sudo luarocks install busted" + +4) Run "sudo luarocks install luacov" + +5) "cd trafficserver/plugins/experimental/ts_lua/ci" + +6) Run "busted -c module_test.lua; luacov". It will produce "luacov.report.out" containing the code coverage on +"module.lua" + +7) Run "busted -c script_test.lua; luacov". It will produce "luacov.report.out" containing the code coverage on +"script.lua" + +Reference for further information +1) Busted - http://olivinelabs.com/busted/ + +2) Specifications for asserts/mocks/stubs/etc inside busted framework - +https://github.com/Olivine-Labs/luassert/tree/master/spec + +3) luacov - https://luarocks.org/modules/hisham/luacov http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/module.lua ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/module.lua b/plugins/experimental/ts_lua/ci/module.lua new file mode 100755 index 0000000..aab9735 --- /dev/null +++ b/plugins/experimental/ts_lua/ci/module.lua @@ -0,0 +1,49 @@ +-- 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. + + +local module = {} + +function module.test() + return 0 +end + +function module.set_hook() + ts.hook(TS_LUA_HOOK_TXN_CLOSE, module.test) +end + + +function module.set_context() + ts.ctx['test'] = 'test10' +end + +function module.check_internal() + return ts.http.is_internal_request() +end + +function module.return_constant() + return TS_LUA_REMAP_DID_REMAP +end + +function module.split(s, delimiter) + result = {} + for match in (s..delimiter):gmatch("(.-)"..delimiter) do + table.insert(result, match) + end + return result +end + +return module http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/module_test.lua ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/module_test.lua b/plugins/experimental/ts_lua/ci/module_test.lua new file mode 100644 index 0000000..b5ccd16 --- /dev/null +++ b/plugins/experimental/ts_lua/ci/module_test.lua @@ -0,0 +1,68 @@ +-- 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. + +_G.ts = { http = {}, client_request = {}, ctx = {} } +_G.TS_LUA_HOOK_TXN_CLOSE = 4 +_G.TS_LUA_REMAP_DID_REMAP = 1 + +describe("Busted unit testing framework", function() + describe("script for ATS Lua Plugin", function() + + it("test - module.split", function() + + local module = require("module") + + local results = module.split('a,b,c', ',') + assert.are.equals('a', results[1]) + assert.are.equals('b', results[2]) + end) + + it("test - module.set_hook", function() + stub(ts, "hook") + local module = require("module") + + module.set_hook() + + assert.stub(ts.hook).was.called_with(TS_LUA_HOOK_TXN_CLOSE, module.test) + end) + + it("test - module.set_context", function() + + local module = require("module") + + module.set_context() + + assert.are.equals('test10', ts.ctx['test']) + end) + + it("test - module.check_internal", function() + stub(ts.http, "is_internal_request").returns(0) + local module = require("module") + local result = module.check_internal() + + assert.are.equals(0, result) + end) + + it("test - module.return_constant", function() + local module = require("module") + + local result = module.return_constant() + + assert.are.equals(TS_LUA_REMAP_DID_REMAP, result) + end) + + end) +end) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/script.lua ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/script.lua b/plugins/experimental/ts_lua/ci/script.lua new file mode 100644 index 0000000..d15abb5 --- /dev/null +++ b/plugins/experimental/ts_lua/ci/script.lua @@ -0,0 +1,22 @@ +-- 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. + +function do_remap() + ts.client_request.set_url_host('192.168.231.130') + ts.client_request.set_url_port(80) + ts.client_request.set_url_scheme('http') + return TS_LUA_REMAP_DID_REMAP +end http://git-wip-us.apache.org/repos/asf/trafficserver/blob/d3d903aa/plugins/experimental/ts_lua/ci/script_test.lua ---------------------------------------------------------------------- diff --git a/plugins/experimental/ts_lua/ci/script_test.lua b/plugins/experimental/ts_lua/ci/script_test.lua new file mode 100644 index 0000000..1c1d158 --- /dev/null +++ b/plugins/experimental/ts_lua/ci/script_test.lua @@ -0,0 +1,39 @@ +-- 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. + +_G.ts = { client_request = {} } +_G.TS_LUA_REMAP_DID_REMAP = 1 + +describe("Busted unit testing framework", function() + describe("script for ATS Lua Plugin", function() + + it("test - script", function() + stub(ts.client_request, "set_url_host") + stub(ts.client_request, "set_url_port") + stub(ts.client_request, "set_url_scheme") + + require("script") + local result = do_remap() + + assert.stub(ts.client_request.set_url_host).was.called_with("192.168.231.130") + assert.stub(ts.client_request.set_url_port).was.called_with(80) + assert.stub(ts.client_request.set_url_scheme).was.called_with("http") + + assert.are.equals(TS_LUA_REMAP_DID_REMAP, result) + end) + + end) +end)
