Skip to content

Commit 4e774e9

Browse files
author
Serhii Zakharov
committed
implemented a tool for making coverage reports by external tests
1 parent 61749a1 commit 4e774e9

File tree

6 files changed

+110
-5
lines changed

6 files changed

+110
-5
lines changed

aggregator.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,11 @@ func main() {
445445
command = strings.ToLower(strings.TrimSpace(os.Args[1]))
446446
}
447447

448-
os.Exit(handleCommand(command))
448+
errCode := handleCommand(command)
449+
if errCode != 0 {
450+
log.Error().Msgf("Service exited with non-zero code %v", errCode)
451+
os.Exit(errCode)
452+
}
449453
}
450454

451455
func handleCommand(command string) int {

consumer/consumer.go

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type KafkaConsumer struct {
5757
numberOfSuccessfullyConsumedMessages uint64
5858
numberOfErrorsConsumingMessages uint64
5959
ready chan bool
60+
cancel context.CancelFunc
6061
}
6162

6263
// DefaultSaramaConfig is a config which will be used by default
@@ -100,6 +101,7 @@ func NewWithSaramaConfig(
100101
// Serve starts listening for messages and processing them. It blocks current thread.
101102
func (consumer *KafkaConsumer) Serve() {
102103
ctx, cancel := context.WithCancel(context.Background())
104+
consumer.cancel = cancel
103105

104106
go func() {
105107
for {
@@ -180,6 +182,10 @@ func (consumer *KafkaConsumer) ConsumeClaim(session sarama.ConsumerGroupSession,
180182

181183
// Close method closes all resources used by consumer
182184
func (consumer *KafkaConsumer) Close() error {
185+
if consumer.cancel != nil {
186+
consumer.cancel()
187+
}
188+
183189
if consumer.ConsumerGroup != nil {
184190
if err := consumer.ConsumerGroup.Close(); err != nil {
185191
log.Error().Err(err).Msg("Unable to close consumer group")

export_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ var (
3939
SetMigrationVersion = setMigrationVersion
4040
PerformMigrations = performMigrations
4141
AutoMigratePtr = &autoMigrate
42+
Main = main
4243
)

make-coverage.sh

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
rm coverage.out 2>/dev/null
4+
5+
case $1 in
6+
"unit-sqlite")
7+
echo "Running unit tests with SQLite in memory..."
8+
go test -coverprofile=coverage.out ./... 1>&2
9+
;;
10+
"unit-postgres")
11+
export INSIGHTS_RESULTS_AGGREGATOR__TESTS_DB="postgres"
12+
export INSIGHTS_RESULTS_AGGREGATOR__TESTS_DB_ADMIN_PASS="admin"
13+
14+
echo "Running unit tests with postgres..."
15+
go test -coverprofile=coverage.out ./... 1>&2
16+
;;
17+
"rest")
18+
echo rest # TODO: rest
19+
echo "Running REST API tests..."
20+
rm test.db 2> /dev/null
21+
rm ./insights-results-aggregator.test 2> /dev/null
22+
23+
go test -c -v -tags testrunmain -coverpkg="./..." . 1>&2
24+
25+
(
26+
export INSIGHTS_RESULTS_AGGREGATOR_CONFIG_FILE=./tests/tests
27+
28+
echo "Migrating the DB"
29+
go run . migrate latest
30+
echo "Starting a service"
31+
./insights-results-aggregator.test -test.v -test.run "^TestRunMain$" -test.coverprofile=coverage.out 1>&2
32+
) &
33+
34+
# TODO: use curl in a loop to test when service is available
35+
sleep 2s
36+
./test.sh --verbose --no-service 1>&2
37+
pkill --signal SIGINT -f insights-results-aggregator.test
38+
39+
rm ./insights-results-aggregator.test 2>/dev/null
40+
41+
;;
42+
"integration")
43+
echo "Running a service..."
44+
echo "Start your integration tests and press Ctrl+C when they are done"
45+
46+
export INSIGHTS_RESULTS_AGGREGATOR_CONFIG_FILE=./config-devel
47+
go test -v -tags testrunmain -run "^TestRunMain$" -coverprofile=coverage.out -coverpkg="./..." . 1>&2
48+
;;
49+
*)
50+
echo 'Please, choose "unit-sqlite", "unit-postgres", "rest" or "integration"'
51+
echo "Aggregator's output will be redirected to stderr."
52+
echo "Coverage is saved to 'coverage.out' file"
53+
exit 1
54+
;;
55+
esac
56+
57+
go tool cover -func=coverage.out | grep -E "^total"

run_main_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build testrunmain
2+
3+
package main_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"os/signal"
9+
"testing"
10+
11+
main "github.com/RedHatInsights/insights-results-aggregator"
12+
)
13+
14+
func TestRunMain(t *testing.T) {
15+
interruptSignal := make(chan os.Signal, 1)
16+
signal.Notify(interruptSignal, os.Interrupt)
17+
go func() {
18+
<-interruptSignal
19+
errCode := main.StopService()
20+
if errCode != 0 {
21+
panic(fmt.Sprintf("service has exited with a code %v", errCode))
22+
}
23+
}()
24+
25+
fmt.Println("starting...")
26+
os.Args = []string{"./insights-results-aggregator", "start-service"}
27+
main.Main()
28+
fmt.Println("exiting...")
29+
}

test.sh

+12-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ COLORS_RED='\033[0;31m'
1717
COLORS_RESET='\033[0m'
1818
LOG_LEVEL="fatal"
1919
VERBOSE=false
20+
NO_SERVICE=false
2021

2122
if [[ $* == *verbose* ]]; then
2223
# print all possible logs
2324
LOG_LEVEL=""
2425
VERBOSE=true
2526
fi
2627

28+
if [[ $* == *no-service* ]]; then
29+
NO_SERVICE=true
30+
fi
31+
2732
function cleanup() {
2833
print_descendent_pids() {
2934
pids=$(pgrep -P "$1")
@@ -65,8 +70,7 @@ function migrate_db_to_latest() {
6570
echo "Migrating DB to the latest migration version..."
6671

6772
if INSIGHTS_RESULTS_AGGREGATOR_CONFIG_FILE=./tests/tests \
68-
./insights-results-aggregator migrate latest >/dev/null
69-
then
73+
./insights-results-aggregator migrate latest >/dev/null; then
7074

7175
echo "Database migration was successful"
7276
return 0
@@ -89,6 +93,11 @@ function populate_db_with_mock_data() {
8993
}
9094

9195
function start_service() {
96+
if [ "$NO_SERVICE" = true ]; then
97+
echo "Not starting service"
98+
return
99+
fi
100+
92101
echo "Starting a service"
93102
# TODO: stop parent(this script) if service died
94103
INSIGHTS_RESULTS_AGGREGATOR__LOGGING__LOG_LEVEL=$LOG_LEVEL \
@@ -135,8 +144,7 @@ function test_openapi() {
135144
echo "Testing OpenAPI specifications file"
136145
# shellcheck disable=2181
137146

138-
if docker run --rm -v "${PWD}":/local/:Z openapitools/openapi-generator-cli validate -i ./local/openapi.json
139-
then
147+
if docker run --rm -v "${PWD}":/local/:Z openapitools/openapi-generator-cli validate -i ./local/openapi.json; then
140148
echo "OpenAPI spec file is OK"
141149
else
142150
echo "OpenAPI spec file validation failed"

0 commit comments

Comments
 (0)