Skip to content

Commit c9245b8

Browse files
authored
Merge pull request #1 from dol/feature/ci-integration
build: Added basic CI support
2 parents 85426ae + bcbf163 commit c9245b8

File tree

10 files changed

+158
-20
lines changed

10 files changed

+158
-20
lines changed

.busted

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
-- luacheck: ignore 631
2+
local current_folder = debug.getinfo(1).source:match("@?(.*/)"):sub(1, -2)
3+
14
return {
25
_all = {
6+
ROOT = {current_folder .. "/spec"},
37
verbose = false,
4-
coverage = false,
8+
["coverage-config-file"] = current_folder .. "/.luacov",
9+
},
10+
default = {
511
output = "gtest",
612
},
713
ci = {
8-
["exclude-tags"] = "postgres",
14+
output = "junit",
15+
-- First argument is the path to the output file
16+
-- https://github.com/lunarmodules/busted/blob/a144124839f027a2d0a95791936c478d047126fc/busted/outputHandlers/junit.lua#L34C34-L34C43
17+
Xoutput = current_folder .. "/test-results/busted-junit.xml",
918
},
1019
}

.github/workflows/lint.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Lint
2+
3+
on: [push, pull_request]
4+
5+
concurrency:
6+
group: ${{ github.workflow }}-${{ github.ref }}
7+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
8+
9+
jobs:
10+
lua-check:
11+
timeout-minutes: ${{ fromJSON(vars.GHA_DEFAULT_TIMEOUT || 10) }}
12+
name: Lua Check
13+
runs-on: ubuntu-24.04
14+
permissions:
15+
contents: read
16+
issues: read
17+
checks: write
18+
pull-requests: write
19+
if: (github.actor != 'dependabot[bot]')
20+
21+
steps:
22+
- name: Checkout source code
23+
uses: actions/checkout@v3
24+
25+
- name: Lua Check
26+
uses: Kong/public-shared-actions/code-check-actions/lua-lint@0ccacffed804d85da3f938a1b78c12831935f992 # v2.8.0
27+
with:
28+
additional_args: '--no-default-config --config .luacheckrc'
29+
action_fail: true
30+
print_results: true

.github/workflows/sast.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: SAST
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
branches:
7+
- master
8+
- main
9+
workflow_dispatch: {}
10+
11+
12+
jobs:
13+
semgrep:
14+
timeout-minutes: ${{ fromJSON(vars.GHA_DEFAULT_TIMEOUT || 10) }}
15+
name: Semgrep SAST
16+
runs-on: ubuntu-latest
17+
permissions:
18+
# required for all workflows
19+
security-events: write
20+
# only required for workflows in private repositories
21+
actions: read
22+
contents: read
23+
24+
if: (github.actor != 'dependabot[bot]')
25+
26+
steps:
27+
- uses: actions/checkout@v3
28+
- uses: Kong/public-shared-actions/security-actions/semgrep@v2

.github/workflows/tests.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
timeout-minutes: ${{ fromJSON(vars.GHA_DEFAULT_TIMEOUT || 10) }}
8+
name: Busted Tests
9+
10+
runs-on: ubuntu-24.04
11+
permissions:
12+
checks: write
13+
pull-requests: write
14+
15+
steps:
16+
- name: Checkout source code
17+
uses: actions/checkout@main
18+
- name: Set up Docker Buildx
19+
uses: docker/setup-buildx-action@v3
20+
- name: Run tests
21+
run: make test-unit DOCKER_RUN_FLAGS_TTY='' BUSTED_COVERAGE=true BUSTED_RUN_PROFILE=ci
22+
- name: Publish Test Results
23+
uses: EnricoMi/publish-unit-test-result-action@v2
24+
if: always()
25+
with:
26+
files: |
27+
test-results/**/*.xml
28+
- name: Setup LCOV
29+
uses: hrishikesh-kadam/setup-lcov@v1
30+
- name: Report code coverage
31+
uses: zgosalvez/github-actions-report-lcov@v4
32+
with:
33+
coverage-files: test-results/lcov.info
34+
artifact-name: code-coverage-report
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
update-comment: true

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ luacov.report.out
2424
/lua_modules/
2525
/.luarocks
2626

27+
# LuaCov and Busted test results
28+
/test-results/
29+
2730
/.docker/
2831

2932
# Local folder for scratch files

.luacov

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
include = {
2-
"%/kong%-plugin%/kong%/.+$",
1+
return {
2+
statsfile = "/kong-plugin/test-results/luacov.stats.out",
3+
reportfile = "/kong-plugin/test-results/luacov.report.out",
4+
runreport = false,
5+
include = {
6+
"%/kong%-plugin%/kong%/.+$",
7+
}
38
}
4-
5-
statsfile = "/kong-plugin/luacov.stats.out"
6-
reportfile = "/kong-plugin/luacov.report.out"
7-
runreport = true

BACKLOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
## Cleanup
1515

1616
- [ ] The OpenFGA store id in the sqlite database is fixed. Make it dynamic when loading the data.
17+
- [ ] Test with PostgreSQL as database backend.

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Initial implementation of plugin
13+
- Added GitHub action build for linting and unit testing
1314

1415
### Changed
1516

Makefile

+39-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ ROCK_FILE := kong-plugin-$(KONG_PLUGIN_NAME)-$(KONG_PLUGIN_VERSION)-$(KONG_PLUGI
99

1010
SERVROOT_PATH := servroot
1111

12+
TEST_RESULTS_PATH := test-results
13+
1214
# Overwrite if you want to use `docker` with sudo
1315
DOCKER ?= docker
1416

@@ -20,19 +22,30 @@ _docker_is_podman = $(shell $(DOCKER) --version | grep podman 2>/dev/null)
2022
# - set username/UID to executor
2123
DOCKER_USER ?= $$(id -u)
2224
DOCKER_USER_OPT = $(if $(_docker_is_podman),--userns keep-id,--user $(DOCKER_USER))
23-
DOCKER_RUN_FLAGS ?= --rm --interactive --tty $(DOCKER_USER_OPT)
25+
DOCKER_RUN_FLAGS_TTY ?= --tty
26+
DOCKER_RUN_FLAGS ?= --rm --interactive $(DOCKER_RUN_FLAGS_TTY) $(DOCKER_USER_OPT)
27+
28+
DOCKER_MOUNT_IN_CONTAINER := /kong-plugin
2429

2530
DOCKER_NO_CACHE :=
2631

2732
BUILDKIT_PROGRESS :=
2833

34+
BUSTED_RUN_PROFILE := default
2935
BUSTED_FILTER :=
3036

31-
BUSTED_ARGS = --config-file /kong-plugin/.busted --run ci --filter '$(BUSTED_FILTER)'
37+
BUSTED_EXCLUDE_TAGS := postgres
38+
BUSTED_COVERAGE := false
39+
40+
BUSTED_ARGS = --config-file $(DOCKER_MOUNT_IN_CONTAINER)/.busted --run '$(BUSTED_RUN_PROFILE)' --exclude-tags='$(BUSTED_EXCLUDE_TAGS)' --filter '$(BUSTED_FILTER)'
3241
ifdef BUSTED_NO_KEEP_GOING
3342
BUSTED_ARGS += --no-keep-going
3443
endif
3544

45+
ifneq ($(BUSTED_COVERAGE), false)
46+
BUSTED_ARGS += --coverage
47+
endif
48+
3649
KONG_SMOKE_TEST_DEPLOYMENT_PATH := _build/deployment/kong-smoke-test
3750

3851
CONTAINER_CI_KONG_TOOLING_IMAGE_PATH := _build/images/kong-tooling
@@ -126,10 +139,10 @@ CONTAINER_CI_KONG_SMOKE_TEST_BUILD = DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=$(BUILD
126139
.
127140

128141
CONTAINER_CI_KONG_TOOLING_RUN := MSYS_NO_PATHCONV=1 $(DOCKER) run $(DOCKER_RUN_FLAGS) \
129-
-v '$(PWD):/kong-plugin' \
142+
-v '$(PWD):$(DOCKER_MOUNT_IN_CONTAINER)' \
130143
-e KONG_SPEC_TEST_REDIS_HOST='$(CONTAINER_CI_REDIS_NAME)' \
131144
-e KONG_SPEC_TEST_LIVE_HOSTNAME='$(CONTAINER_CI_OPENFGA_NAME)' \
132-
-e KONG_LICENSE_PATH=/kong-plugin/kong-license.json \
145+
-e KONG_LICENSE_PATH=$(DOCKER_MOUNT_IN_CONTAINER)/kong-license.json \
133146
-e KONG_DNS_ORDER='LAST,A,SRV' \
134147
--network='$(CONTAINER_CI_NETWORK_NAME)' \
135148
'$(CONTAINER_CI_KONG_TOOLING_IMAGE_NAME)'
@@ -148,7 +161,7 @@ CONTAINER_CI_KONG_SMOKE_TEST_RUN_SERVER := MSYS_NO_PATHCONV=1 $(DOCKER) run $(DO
148161
-e KONG_VITALS=off \
149162
-e KONG_NGINX_HTTP_INCLUDE=/kong/smoke-test.nginx.conf \
150163
-e KONG_DECLARATIVE_CONFIG=/kong/kong.yaml \
151-
-e KONG_LICENSE_PATH=/kong-plugin/kong-license.json \
164+
-e KONG_LICENSE_PATH=$(DOCKER_MOUNT_IN_CONTAINER)/kong-license.json \
152165
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
153166
-e KONG_ADMIN_GUI_URL=http://localhost:8002/ \
154167
--env-file .env \
@@ -178,7 +191,10 @@ $(ROCKSPEC_FILE): kong-plugin.rockspec
178191

179192
# Rebuild the rock file every time the rockspec or the kong/**/.lua files change
180193
$(ROCK_FILE): container-ci-kong-tooling $(ROCKSPEC_FILE) $(PLUGIN_FILES)
181-
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd /kong-plugin; luarocks make --pack-binary-rock --deps-mode none $(ROCKSPEC_FILE))'
194+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd $(DOCKER_MOUNT_IN_CONTAINER); luarocks make --pack-binary-rock --deps-mode none $(ROCKSPEC_FILE))'
195+
196+
test-results:
197+
mkdir -p $(TEST_RESULTS_PATH)
182198

183199
.PHONY: tail-logs
184200
tail-logs:
@@ -254,15 +270,21 @@ stop-services: stop-service-redis stop-service-openfga stop-service-postgres
254270

255271
.PHONY: lint
256272
lint: container-ci-kong-tooling
257-
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd /kong-plugin; luacheck .)'
273+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd $(DOCKER_MOUNT_IN_CONTAINER); luacheck --no-default-config --config .luacheckrc .)'
258274

259275
.PHONY: format-code
260276
format-code: container-ci-kong-tooling
261-
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd /kong-plugin; stylua --check . || stylua --verify .)'
277+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd $(DOCKER_MOUNT_IN_CONTAINER); stylua --check . || stylua --verify .)'
262278

263279
.PHONY: test-unit
264-
test-unit: container-ci-kong-tooling clean-servroot service-openfga
265-
$(CONTAINER_CI_KONG_TOOLING_RUN) busted $(BUSTED_ARGS) /kong-plugin/spec
280+
test-unit: clean-test-results test-results container-ci-kong-tooling clean-servroot service-openfga
281+
$(CONTAINER_CI_KONG_TOOLING_RUN) busted $(BUSTED_ARGS)
282+
@if [ -f $(TEST_RESULTS_PATH)/luacov.stats.out ]; then \
283+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd $(DOCKER_MOUNT_IN_CONTAINER)/$(TEST_RESULTS_PATH); luacov-console $(DOCKER_MOUNT_IN_CONTAINER)/kong; luacov-console -s);' ;\
284+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c '(cd $(DOCKER_MOUNT_IN_CONTAINER)/$(TEST_RESULTS_PATH); luacov -r html; mv luacov.report.out luacov.report.html);' ;\
285+
echo "Coverage report: file://$(PWD)/$(TEST_RESULTS_PATH)/luacov.report.html" ;\
286+
$(CONTAINER_CI_KONG_TOOLING_RUN) sh -c "(cd $(DOCKER_MOUNT_IN_CONTAINER)/$(TEST_RESULTS_PATH); luacov -r lcov; sed -e 's|/kong-plugin/||' -e 's/^\(DA:[0-9]\+,[0-9]\+\),[^,]*/\1/' luacov.report.out > lcov.info)" ;\
287+
fi
266288

267289
.PHONY: tooling-shell
268290
tooling-shell: container-ci-kong-tooling
@@ -284,8 +306,12 @@ smoke-test-run-test: container-network-ci
284306
.PHONY: lua-language-server-add-kong
285307
lua-language-server-add-kong: container-ci-kong-tooling
286308
-mkdir -p .luarocks
287-
$(CONTAINER_CI_KONG_TOOLING_RUN) cp -r /usr/local/share/lua/5.1/. /kong-plugin/.luarocks
288-
$(CONTAINER_CI_KONG_TOOLING_RUN) cp -r /kong /kong-plugin/.luarocks
309+
$(CONTAINER_CI_KONG_TOOLING_RUN) cp -rv /usr/local/share/lua/5.1/. $(DOCKER_MOUNT_IN_CONTAINER)/.luarocks
310+
$(CONTAINER_CI_KONG_TOOLING_RUN) cp -rv /kong $(DOCKER_MOUNT_IN_CONTAINER)/.luarocks
311+
312+
.PHONY: clean-test-results
313+
clean-test-results:
314+
-$(RMDIR) test-results
289315

290316
.PHONY: clean-servroot
291317
clean-servroot:
@@ -328,6 +354,7 @@ clean-container-smoke-test-network:
328354
-$(DOCKER) network rm '$(CONTAINER_CI_NETWORK_NAME)'
329355

330356
.PHONY: clean
357+
clean: clean-test-results
331358
clean: clean-rock clean-rockspec
332359
clean: clean-servroot
333360
clean: clean-container-ci-kong-tooling clean-container-ci-kong-smoke-test clean-container-smoke-test-network

_build/images/kong-plugin-testing-0.1.0-0.rockspec

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ dependencies = {
1414
"busted-hjtest = 0.0.5",
1515
"luacheck = 1.2.0",
1616
"luacov = 0.16.0",
17+
"luacov-reporter-lcov = 0.2",
18+
"luacov-console = 1.1.0",
1719
"lua-llthreads2 = 0.1.6",
1820
"http = 0.4",
1921
}

0 commit comments

Comments
 (0)