Skip to content

Commit

Permalink
feat: First version of Mender googletest framework in C++.
Browse files Browse the repository at this point in the history
This comes with several cleanups in the gitignore file, as well as
provisionary Makefile changes to get this off the ground. The Makefile
is expected to change substantially once we get the new build system
in place though.

Changelog: None
Ticket: MEN-5794

Signed-off-by: Kristian Amlie <[email protected]>
  • Loading branch information
Kristian Amlie committed Dec 2, 2022
1 parent 3dfc9a0 commit 648f80e
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 171 deletions.
29 changes: 10 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.gcda
*.gcno
*.gcov
*.lcov

# Test binaries
*_test

mender
/mender

# Go test coverage output
*coverage*.txt
# Test reports
/reports
63 changes: 27 additions & 36 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,30 @@ stages:
- trigger_prep
- trigger

variables:
GOLANG_VERSION: "1.17.13"

include:
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-check-golang-lint.yml'
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-github-status-updates.yml'
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-check-commits.yml'
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-check-license.yml'

variables:
GIT_SUBMODULE_STRATEGY: recursive

test:
stage: test
image: golang:1.17
image: debian:11
before_script:
- apt-get update && apt-get install -yyq liblzma-dev libssl-dev libglib2.0-dev dbus clang-format-9
- GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
- apt update && apt install -yyq g++ cmake git liblzma-dev libssl-dev dbus clang-format-9 make lcov
script:
- git ls-tree -r --name-only HEAD | grep -v vendor/ | grep '\.[ch]$' | xargs clang-format-9 -i
- make coverage
- make
- mkdir -p tests/unit-coverage && find . -name 'coverage.txt' -exec cp --parents {} ./tests/unit-coverage \;
- tar -cvf $CI_PROJECT_DIR/unit-coverage.tar tests/unit-coverage
tags:
- mender-qa-worker-generic
artifacts:
expire_in: 2w
reports:
junit: report.xml
junit: reports/*.xml
paths:
- unit-coverage.tar
- coverage.lcov

test:modules-artifact-gen:
stage: test
Expand All @@ -62,31 +53,31 @@ test:docker:

publish:tests:
stage: publish
image: golang:1.17-alpine3.14
image: python:3.11
dependencies:
- test
before_script:
# Install dependencies
- apk add --no-cache git
- GO111MODULE=off go get -u github.com/mattn/goveralls
# Coveralls env variables:
# According to https://docs.coveralls.io/supported-ci-services
# we should set CI_NAME, CI_BUILD_NUMBER, etc. But according
# to goveralls source code (https://github.com/mattn/goveralls)
# many of these are not supported. Set CI_BRANCH,
# and pass few others as command line arguments.
# See also https://docs.coveralls.io/api-reference
- export CI_BRANCH=${CI_COMMIT_BRANCH}
- apt update && apt install -yyq lcov
- pip install cpp-coveralls pyyaml

# eddyxu/cpp-coveralls appears dead, but there doesn't seem to be an
# alternative. Use this patch from someone who attempted to fix it. An
# alternative to this is to use pyyaml<6, but it's better to use just one
# old component than two.
- curl -f https://github.com/eddyxu/cpp-coveralls/commit/067c837c04e039e8c70aa53bceda1cded6751408.patch | patch -f /usr/local/lib/python3.11/site-packages/cpp_coveralls/__init__.py

- export CI_BRANCH=$CI_COMMIT_BRANCH
# "TRAVIS_JOB_ID" is hardcoded in cpp-coveralls, but it is semantically the
# same as "CI_JOB_ID" would be.
- export TRAVIS_JOB_ID=$CI_PIPELINE_ID

script:
- tar -xvf unit-coverage.tar
- goveralls
-repotoken ${COVERALLS_TOKEN}
-service gitlab-ci
-jobid $(git rev-parse HEAD)
-covermode set
-flagname unittests
-parallel
-coverprofile $(find tests/unit-coverage -name 'coverage.txt' | tr '\n' ',' | sed 's/,$//')
- 'echo "service_name: gitlab-ci" > .coveralls.yml'
- cpp-coveralls
--repo-token ${COVERALLS_TOKEN}
--no-gcov
--lcov-file coverage.lcov

generate-qa-trigger:
image: python:alpine
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "tests/googletest"]
path = vendor/googletest
url = https://github.com/google/googletest.git
branch = v1.12.x
3 changes: 3 additions & 0 deletions LIC_FILES_CHKSUM.sha256
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Apache-2.0 license.
1033348db7606a7e61b6484f293847cf8d7a35766efebb97e304d4bd5d7f3f6b LICENSE
#
# BSD 3 Clause license.
9702de7e4117a8e2b20dafab11ffda58c198aede066406496bef670d40a22138 vendor/googletest/LICENSE
151 changes: 35 additions & 116 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,8 @@ sysconfdir ?= /etc
systemd_unitdir ?= /lib/systemd
docexamplesdir ?= /usr/share/doc/mender-client/examples

GO ?= go
GOFMT ?= gofmt
V ?=
PKGS = $(shell go list ./... | grep -v vendor)
PKGFILES = $(shell find . \( -path ./vendor -o -path ./Godeps \) -prune \
-o -type f -name '*.go' -print)
PKGFILES_notest = $(shell echo $(PKGFILES) | tr ' ' '\n' | grep -v '\(client/test\|_test.go\)' )
GOCYCLO ?= 15

CGO_ENABLED=1
export CGO_ENABLED

# Get rid of useless warning in lmdb
CGO_CFLAGS ?= -Wno-implicit-fallthrough -Wno-stringop-overflow
export CGO_CFLAGS

TOOLS = \
github.com/fzipp/gocyclo/... \
gitlab.com/opennota/check/cmd/varcheck \
github.com/mendersoftware/deadcode \
github.com/mendersoftware/gobinarycoverage \
github.com/jstemmer/go-junit-report

VERSION = $(shell git describe --tags --dirty --exact-match 2>/dev/null || git rev-parse --short HEAD)

GO_LDFLAGS = \
-ldflags "-X github.com/mendersoftware/mender/conf.Version=$(VERSION)"

ifeq ($(V),1)
BUILDV = -v
endif

TAGS =
ifeq ($(LOCAL),1)
TAGS += local
endif

ifneq ($(TAGS),)
BUILDTAGS = -tags '$(TAGS)'
endif

IDENTITY_SCRIPTS = \
support/mender-device-identity

Expand Down Expand Up @@ -81,8 +42,8 @@ DBUS_POLICY_FILES = \

build: mender

mender: $(PKGFILES)
$(GO) build $(GO_LDFLAGS) $(BUILDV) $(BUILDTAGS)
mender:
g++ -o mender main.cpp $(CXXFLAGS) $(LDFLAGS)

install: install-bin \
install-conf \
Expand Down Expand Up @@ -202,85 +163,44 @@ uninstall-examples:
-rmdir -p $(prefix)$(docexamplesdir)

clean:
$(GO) clean
rm -f coverage.txt

get-tools:
set -e ; for t in $(TOOLS); do \
echo "-- go getting $$t"; \
GO111MODULE=off go get -u $$t; \
done

check: test extracheck

test:
$(GO) test $(BUILDV) $(PKGS)

extracheck: gofmt govet godeadcode govarcheck gocyclo
echo "All extra-checks passed!"

gofmt:
echo "-- checking if code is gofmt'ed"
if [ -n "$$($(GOFMT) -d $(PKGFILES))" ]; then \
"$$($(GOFMT) -d $(PKGFILES))" \
echo "-- gofmt check failed"; \
/bin/false; \
fi

govet:
echo "-- checking with govet"
$(GO) vet -composites=false -unsafeptr=false ./...

godeadcode:
echo "-- checking for dead code"
deadcode -ignore version.go:Version

govarcheck:
echo "-- checking with varcheck"
varcheck ./...

gocyclo:
echo "-- checking cyclometric complexity > $(GOCYCLO)"
gocyclo -over $(GOCYCLO) $(PKGFILES_notest)

cover: coverage
$(GO) tool cover -func=coverage.txt

htmlcover: coverage
$(GO) tool cover -html=coverage.txt

coverage:
rm -f coverage.txt
$(GO) test -v -coverprofile=coverage-tmp.txt -coverpkg=./... ./... > .tmp.go-test.txt ; echo $$? > .tmp.return-code.txt
cat .tmp.go-test.txt
go-junit-report < .tmp.go-test.txt > report.xml
rm -f .tmp.go-test.txt
if [ -f coverage-missing-subtests.txt ]; then \
echo 'mode: set' > coverage.txt; \
cat coverage-tmp.txt coverage-missing-subtests.txt | grep -v 'mode: set' >> coverage.txt; \
else \
mv coverage-tmp.txt coverage.txt; \
fi
rm -f coverage-tmp.txt coverage-missing-subtests.txt
failing_tests=$$(grep -Po '(?<=failures=")\d+' report.xml | awk '{s+=$$1} END {print s}'); \
echo FAILING TESTS: $$failing_tests
return_code=$$(cat .tmp.return-code.txt); \
rm -f .tmp.return-code.txt; \
exit $$return_code

instrument-binary:
# Patch the client to make it ready for coverage analysis
git apply patches/0001-Instrument-Mender-client-for-coverage-analysis.patch
# Then instrument the files with the gobinarycoverage tool
gobinarycoverage github.com/mendersoftware/mender
rm -f mender main_test

check: test

test: main_test
./main_test $(TESTFLAGS)

coverage: Makefile
rm -rf reports/*.xml
$(MAKE) \
CXXFLAGS="$(CXXFLAGS) --coverage" \
TESTFLAGS="$(TESTFLAGS) --gtest_output=xml:reports/" \
test
lcov --capture \
--quiet \
--directory . \
--output-file coverage.lcov \
--exclude '/usr/*' \
--exclude '*/googletest/*'

vendor/googletest/lib/libgtest.a:
( cd vendor/googletest && cmake . && make )

main_test: main_test.cpp Makefile vendor/googletest/lib/libgtest.a
g++ \
-o main_test \
main_test.cpp \
-pthread \
vendor/googletest/lib/libgtest.a \
vendor/googletest/lib/libgtest_main.a \
-I vendor/googletest/googletest/include \
$(CXXFLAGS) \
$(LDFLAGS)

.PHONY: build
.PHONY: clean
.PHONY: get-tools
.PHONY: test
.PHONY: check
.PHONY: cover
.PHONY: htmlcover
.PHONY: coverage
.PHONY: install
.PHONY: install-bin
Expand All @@ -307,4 +227,3 @@ instrument-binary:
.PHONY: uninstall-modules-gen
.PHONY: uninstall-systemd
.PHONY: uninstall-examples
.PHONY: instrument-binary
22 changes: 22 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <iostream>

using namespace std;

int main() {
cout << "Hello world!\n";
return 0;
}
19 changes: 19 additions & 0 deletions main_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

TEST(Main, Main) {
EXPECT_TRUE(true) << "Printed if test fails";
}
1 change: 1 addition & 0 deletions vendor/googletest
Submodule googletest added at 58d77f

0 comments on commit 648f80e

Please sign in to comment.