0

I am writing a few kong custom plugins in Lua. I am using Kong 2.3.3 and Lua 5.1.

I have some test cases (unit tests + integration tests) and i am running them with pongo run -coverage option. I have already installed luacov (and also cluacov, both with luarocks install) and all my tests are passing but no luacov files are being generated with coverage data. I am not running pongo from Docker, i have installed and configured it in my local machine (which is Linux Ubuntu 20.04).

I have already tried a few things as follows:

  • my .busted file is setting coverage = true, verbose = true and output = "gtest" (already tried utfTerminal, tap and json too)
  • tried adding luacov as a dependency to my rockspec file... the build does not fail but no coverage file is generated
  • i even tried running the tests without pongo, using busted directly but this is a very bad option because things like spec.helpers, or the cjson lib are not set in my LUAPATH
  • 1
    Have you created a `.luacov` file in your project ? – Ôrel Apr 02 '21 at 13:21
  • no i havent.... i will try to appy the solution you are suggesting in your answer and see if that works... me and my team have figured out another way but your proposal seems good and will be tested, thanks for answering – bovino Marcelo Bezerra Apr 02 '21 at 23:49

3 Answers3

1

A quick way to do this is to modify pongo

Edit your pongo.sh file to:

  • add coverage flag to busted --coverage
  • call luacov to generate the report luacov
  • display the report cat luacov.report.out

locate where busted is called, line 959 for me:

"/bin/sh" "-c" "bin/busted --coverage --helper=bin/busted_helper.lua ${busted_params[*]} ${busted_files[*]};luacov;cat luacov.report.out"

Install luacov, edit assets/Dockerfile after busted installation add luacov:

    && luarocks install busted-htest \
    && luarocks install luacov \

pongo run will give you

[...]
==============================================================================
Summary
==============================================================================

File                                                                              Hits  Missed Coverage
-------------------------------------------------------------------------------------------------------
/kong-plugin/kong/plugins/myplugin/schema.lua                                     105   1      99.06%
/kong-plugin/spec/myplugin/01-schema_spec.lua                                     199   5      97.55%
[...]
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • In fact, by modifyng pongo.sh (there was no need to touch "assests/Dockerfile") i was able to see the content of the report being printed out. And for this reason i will mark your answer as correct. But..... (part 1/2) – bovino Marcelo Bezerra Apr 03 '21 at 21:58
  • But I am still having some problems with this approach: 1) it will not work in my GitlabCI script (where i download and install pongo on the fly) and; 2) i was able to see the coverage being printed out locally but i was not able to move the file luacov.report.out to outside of the pongo container (will probalby have to do some volumes configuration or something like this) 3) i was not able to filter the output report to include only the specific lua files from my custom plugin (i tried adding a .luacov in a lot places hopping it would be visible inside pongo container) (part 2/2) – bovino Marcelo Bezerra Apr 03 '21 at 21:58
  • To match your need, fork pongo, to use your own on the fly (push the PR it is a nice feature), mount a volume to get the output instead of printing it. – Ôrel Apr 06 '21 at 14:56
0

You can create a docker image based on pongo

spec/unit/docker/Dockerfile

FROM kong-pongo-test:2.3.2
USER root
RUN luarocks install luacov
WORKDIR /kong-plugin
COPY . .

spec/unit/docker/run.sh

#!/bin/sh
busted --coverage spec/unit
luacov
cat luacov.report.out

Run

docker build -f spec/unit/docker/Dockerfile -t my-coverage .
docker run my-coverage sh spec/unit/docker/run.sh
0

Pongo gained some support for this (still a PR). Note that it only covers unit tests, not integration ones.

See https://github.com/Kong/kong-pongo/pull/184

btw: the other anwers are too complex imo, you can add .pongo/pongo-setup.sh to install LuaCov, and move the .luacov file from /kong-plugin to /kong. That should be all that is necessary.

Running tests with coverage can be simply done by passing the flag, without any need to edit pongo or the dockerfile. Try pongo run -- --coverage for example.

Tieske
  • 363
  • 3
  • 10