git » kxd » commit 2e76d3b

tests: Generate code coverage report

author Alberto Bertogli
2024-08-15 21:53:12 UTC
committer Alberto Bertogli
2024-09-08 09:33:43 UTC
parent 4492360ed497a8e06e13b76c419e178285c5fa82

tests: Generate code coverage report

This patch adds support to generate a code coverage report from the
default tests.

It also makes the Github CI do this, and upload the results to codecov.

.github/workflows/tests.yaml +18 -0
README.md +1 -0
kxd/kxd.go +18 -0
tests/cover.sh +19 -0
tests/run_tests +2 -2

diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml
index 7bd7b26..8a5a152 100644
--- a/.github/workflows/tests.yaml
+++ b/.github/workflows/tests.yaml
@@ -25,3 +25,21 @@ jobs:
           check-latest: true
       - name: test
         run: make test
+
+  coverage:
+    needs: tests
+    runs-on: ubuntu-latest
+    timeout-minutes: 5
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-go@v5
+        with:
+          go-version: 'stable'
+          check-latest: true
+      - name: coverage tests
+        run: ./tests/cover.sh
+      - name: upload
+        uses: codecov/codecov-action@v4
+        with:
+          token: ${{ secrets.CODECOV_TOKEN }}
+          file: .cover-merged.out
diff --git a/README.md b/README.md
index c0590fe..7226e03 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ without having to store them on the local machine.
 
 [![Docs](https://img.shields.io/badge/docs-reference-blue.svg)](https://blitiri.com.ar/p/kxd/)
 [![Tests](https://github.com/albertito/kxd/actions/workflows/tests.yaml/badge.svg)](https://github.com/albertito/kxd/actions)
+[![Coverage](https://codecov.io/gh/albertito/kxd/branch/next/graph/badge.svg?token=WMRDGeHOUK)](https://codecov.io/gh/albertito/kxd)
 
 
 ## Quick start
diff --git a/kxd/kxd.go b/kxd/kxd.go
index 3e1911b..1e7a6b5 100644
--- a/kxd/kxd.go
+++ b/kxd/kxd.go
@@ -18,8 +18,10 @@ import (
 	"log/syslog"
 	"net/http"
 	"os"
+	"os/signal"
 	"path"
 	"strings"
+	"syscall"
 )
 
 var port = flag.Int(
@@ -216,11 +218,27 @@ func initLog() {
 		log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
 }
 
+func signalHandler() {
+	signals := make(chan os.Signal, 1)
+	signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
+	for {
+		switch sig := <-signals; sig {
+		case syscall.SIGTERM, syscall.SIGINT:
+			logging.Printf("Received signal %s, exiting", sig)
+			os.Exit(0)
+		default:
+			logging.Printf("Received unexpected signal %s", sig)
+		}
+	}
+}
+
 func main() {
 	flag.Parse()
 
 	initLog()
 
+	go signalHandler()
+
 	if *smtpAddr == "" {
 		logging.Print(
 			"WARNING: No emails will be sent, use --smtp_addr")
diff --git a/tests/cover.sh b/tests/cover.sh
new file mode 100755
index 0000000..d9c211c
--- /dev/null
+++ b/tests/cover.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+set -e
+
+cd "$(realpath `dirname ${0}`)/../"
+
+make GOFLAGS="-cover -covermode=count"
+
+rm -rf .coverage/
+mkdir -p .coverage
+export GOCOVERDIR="${PWD}/.coverage"
+
+tests/run_tests -b
+
+go tool covdata textfmt -i "${GOCOVERDIR}" -o .cover-merged.out
+go tool cover -func=.cover-merged.out | grep -i total
+go tool cover -html=.cover-merged.out -o .cover-kxd.html
+
+echo "file:///$PWD/.cover-kxd.html"
diff --git a/tests/run_tests b/tests/run_tests
index 738c1d5..45ebfc4 100755
--- a/tests/run_tests
+++ b/tests/run_tests
@@ -201,7 +201,7 @@ class TestCase(unittest.TestCase):
 
     def tearDown(self):
         if self.daemon:
-            self.daemon.kill()
+            self.daemon.terminate()
             self.daemon.wait()
 
     def launch_server(self, server):
@@ -463,7 +463,7 @@ class Multiples(TestCase):
         key = self.client.call(server_certs_path, "kxd://localhost/k1")
         self.assertEqual(key, server1.keys["k1"])
 
-        self.daemon.kill()
+        self.daemon.terminate()
         self.daemon.wait()
         time.sleep(0.5)
         self.launch_server(server2)