Skip to content

Commit dd02d67

Browse files
committed
chore(QA-148): CLI acceptance tests coverage
This commit have no practical implications on the test coverage, but it aligns the acceptance testing environment with the rest of the backend components. changelog: none Signed-off-by: Alf-Rune Siqveland <[email protected]>
1 parent eeb8699 commit dd02d67

8 files changed

+98
-50
lines changed

.gitlab-ci.yml

+9-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ include:
2929
file: '.gitlab-ci-check-docker-build.yml'
3030

3131
test:unit:
32-
image: golang:1.15
3332
needs: []
3433
services:
3534
- name: mongo:4.4
@@ -44,17 +43,21 @@ test:acceptance:
4443
- /^saas-[a-zA-Z0-9.-]+$/
4544
tags:
4645
- docker
47-
image: docker:19.03.13
46+
image: docker:20.10.12
4847
services:
49-
- name: docker:19.03.13-dind
48+
- name: docker:20.10.12-dind
5049
alias: docker
5150
before_script:
5251
- apk add docker-compose make
5352
script:
5453
- make acceptance-tests
5554
after_script:
56-
- make acceptance-tests-logs
57-
- make acceptance-tests-down
55+
- set -- tests/coverage-acceptance@*.txt
56+
- head -n 1 $1 > tests/coverage-acceptance.txt
57+
- |
58+
for cover in $@; do
59+
tail -n +2 $cover >> tests/coverage-acceptance.txt;
60+
done
5861
artifacts:
5962
expire_in: 2w
6063
paths:
@@ -66,7 +69,7 @@ publish:acceptance:
6669
stage: publish
6770
except:
6871
- /^saas-[a-zA-Z0-9.-]+$/
69-
image: golang:1.15
72+
image: golang:1.17
7073
needs:
7174
- job: test:acceptance
7275
artifacts: true
@@ -80,7 +83,6 @@ publish:acceptance:
8083
# and pass few others as command line arguments.
8184
# See also https://docs.coveralls.io/api-reference
8285
- export CI_BRANCH=${CI_COMMIT_BRANCH}
83-
- export CI_PR_NUMBER=${CI_COMMIT_BRANCH#pr_}
8486
script:
8587
- goveralls
8688
-repotoken ${COVERALLS_TOKEN}

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.16.5-alpine3.12 as builder
1+
FROM golang:1.17.6-alpine3.15 as builder
22
RUN apk add --no-cache \
33
xz-dev \
44
musl-dev \

Dockerfile.acceptance

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ FROM alpine:3.15.0
1313
RUN apk add --no-cache ca-certificates xz
1414
RUN mkdir -p /etc/deviceconnect
1515
COPY ./config.yaml /etc/deviceconnect
16-
COPY --from=builder /go/src/github.com/mendersoftware/deviceconnect/bin/deviceconnect.test /usr/bin
17-
ENTRYPOINT ["/usr/bin/deviceconnect.test", \
18-
"-test.coverprofile=/testing/coverage-acceptance.txt", \
19-
"-acceptance-testing", \
20-
"--", "--config=/etc/deviceconnect/config.yaml", \
16+
COPY --from=builder /go/src/github.com/mendersoftware/deviceconnect/bin/deviceconnect.test /usr/bin/deviceconnect
17+
ENTRYPOINT ["/usr/bin/deviceconnect", \
18+
"--config=/etc/deviceconnect/config.yaml", \
2119
"server", "--automigrate"]
2220

2321
EXPOSE 8080

Makefile

+14-13
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ $(BINFILE): $(SRCFILES)
3737
$(GO) build -o $@ .
3838

3939
$(BINFILE).test: $(GOFILES)
40-
go test -c -o $(BINFILE).test \
41-
-cover -covermode atomic \
42-
-coverpkg $(PACKAGES)
40+
go test -c -o $(BINFILE).test -tags main \
41+
-cover -covermode atomic -coverpkg $(PACKAGES)
4342

4443
$(COVERFILE): $(GOFILES)
4544
$(GO) test -cover -covermode=atomic -coverprofile=$@ ${TEST_FLAGS} ./...
@@ -70,26 +69,28 @@ docker: bin/deviceconnect.docker
7069
.PHONY: docker-test
7170
docker-test: bin/deviceconnect.acceptance.docker
7271

73-
.PHONY: acceptance-tests
74-
acceptance-tests: docker-test docs
72+
.PHONY: acceptance-tests-run
73+
acceptance-tests-run: docker-test docs
7574
docker-compose \
76-
-f tests/docker-compose-acceptance.yml \
7775
-p acceptance \
78-
up -d
79-
docker attach acceptance_acceptance_1
76+
-f tests/docker-compose.yml \
77+
run tester
8078

8179
.PHONY: acceptance-tests-logs
82-
acceptance-tests-logs:
83-
for service in $(shell docker-compose -f tests/docker-compose-acceptance.yml -p acceptance ps -a --services); do \
84-
docker-compose -p acceptance -f tests/docker-compose-acceptance.yml \
80+
acceptance-tests-logs: acceptance-tests-run
81+
for service in $(shell docker-compose -f tests/docker-compose.yml -p acceptance ps -a --services); do \
82+
docker-compose -p acceptance -f tests/docker-compose.yml \
8583
logs --no-color $$service > "tests/acceptance.$${service}.logs"; \
8684
done
8785

8886
.PHONY: acceptance-tests-down
8987
acceptance-tests-down:
9088
docker-compose \
91-
-f tests/docker-compose-acceptance.yml \
92-
-p acceptance down
89+
-f tests/docker-compose.yml \
90+
-p acceptance down --remove-orphans
91+
92+
.PHONY: acceptance-tests
93+
acceptance-tests: acceptance-tests-logs acceptance-tests-down
9394

9495

9596
.PHONY: fmt

main_test.go

+55-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Northern.tech AS
1+
// Copyright 2022 Northern.tech AS
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,39 +12,74 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//go:build main
16+
// +build main
17+
1518
package main
1619

1720
import (
21+
"crypto/rand"
22+
"encoding/hex"
1823
"flag"
24+
"fmt"
25+
"hash/crc64"
26+
"io"
1927
"os"
28+
"os/signal"
2029
"testing"
21-
)
2230

23-
var (
24-
acceptanceTesting bool
31+
"github.com/mendersoftware/go-lib-micro/log"
2532
)
2633

2734
func init() {
28-
flag.BoolVar(&acceptanceTesting, "acceptance-testing", false,
29-
"Acceptance testing mode, starts the application main function "+
30-
"with cover mode enabled. Non-flag arguments are passed"+
31-
"to the main application, add '--' after test flags to"+
32-
"pass flags to main.",
33-
)
35+
// Make sure main does not exit before we have gathered coverage.
36+
log.Log.ExitFunc = func(int) {}
3437
}
3538

39+
const (
40+
coverName = "coverage-acceptance"
41+
coverExt = ".txt"
42+
)
43+
44+
var stdout = os.Stdout
45+
3646
func TestMain(m *testing.M) {
37-
flag.Parse()
38-
if acceptanceTesting {
39-
// Override 'run' flags to only execute TestDoMain
40-
flag.Set("test.run", "TestDoMain")
47+
argHash := crc64.New(crc64.MakeTable(crc64.ECMA))
48+
for _, arg := range os.Args {
49+
_, _ = argHash.Write([]byte(arg))
4150
}
42-
os.Exit(m.Run())
51+
var b [6]byte
52+
_, err := io.ReadFull(rand.Reader, b[:])
53+
if err != nil {
54+
panic(err)
55+
}
56+
// filename = "{coverName}@{hash(args)}-{48-bit rand}.txt"
57+
fileNameCover := fmt.Sprintf("%s@%s-%s%s",
58+
coverName,
59+
hex.EncodeToString(argHash.Sum(nil)),
60+
hex.EncodeToString(b[:]),
61+
coverExt,
62+
)
63+
64+
// Override arguments passed to "testing" package
65+
os.Args = os.Args[:1]
66+
flag.Set("test.run", "TestRunMain")
67+
flag.Set("test.coverprofile", fileNameCover)
68+
69+
// Run tests
70+
exitCode := m.Run()
71+
os.Stdout = stdout
72+
os.Exit(exitCode)
4373
}
4474

45-
func TestDoMain(t *testing.T) {
46-
if !acceptanceTesting {
47-
t.Skip()
48-
}
49-
doMain(append(os.Args[:1], flag.Args()...))
75+
func TestRunMain(t *testing.T) {
76+
stopChan := make(chan os.Signal, 1)
77+
signal.Notify(stopChan, os.Interrupt)
78+
go func() {
79+
doMain(os.Args[:cap(os.Args)])
80+
stopChan <- os.Interrupt
81+
}()
82+
<-stopChan
83+
// Prevent the output from testing to hit stdout
84+
os.Stdout = os.Stderr
5085
}

tests/Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM mendersoftware/mender-test-containers:acceptance-testing
2+
3+
# FIXME: The old and deprecated version of msgpack is a dependency of bravado (unused)
4+
RUN pip uninstall -y msgpack && pip install msgpack==1.0.3

tests/docker-compose-acceptance.yml tests/docker-compose.yml

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
version: '2.1'
22
services:
33

4-
acceptance:
5-
image: mendersoftware/mender-test-containers:acceptance-testing
4+
tester:
5+
build:
6+
context: "."
7+
dockerfile: Dockerfile
8+
image: mendersoftware/mender-test-containers:acceptance-testing-deviceconnect
69
networks:
710
- mender
811
volumes:
912
- ".:/testing"
13+
- "/var/run/docker.sock:/var/run/docker.sock"
1014
depends_on:
1115
- mender-deviceconnect
16+
- mender-mongo
17+
- mender-nats
1218
- mmock
1319

1420
mender-deviceconnect:
21+
build:
22+
dockerfile: "Dockerfile.acceptance"
23+
context: ".."
1524
image: mendersoftware/deviceconnect:prtest
1625
networks:
1726
mender:
1827
aliases:
1928
- mender-deviceconnect
2029
volumes:
2130
- ".:/testing"
31+
working_dir: /testing
2232
depends_on:
2333
- mender-mongo
2434
- mender-nats

tests/run.sh

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export TESTING_MMOCK_HOST=${TESTING_MMOCK_HOST:="mmock:8080"}
99

1010
export PYTHONDONTWRITEBYTECODE=1
1111

12-
pip3 install --quiet --force-reinstall -r requirements.txt
13-
1412
# if we're running in a container, wait a little before starting tests
1513
[ $$ -eq 1 ] && sleep 10
1614

0 commit comments

Comments
 (0)