Skip to content

Commit

Permalink
Merge pull request #22 from Fiware/hardening/20_sonar_reports
Browse files Browse the repository at this point in the history
Unit test and coverage report generation
  • Loading branch information
Fernando López Aguilar committed Oct 9, 2014
2 parents 638b41e + 8215b99 commit 269f3ce
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 9 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.metadata
.settings
.metadata/
.settings/
report/
site/
sonar*
.project
.deps
.libs
*.o
*.lo
*.la
Expand Down
5 changes: 5 additions & 0 deletions ngsi_event_broker/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
m4/
docs/
.deps/
.libs/
autom4te.cache/
aclocal.m4
depcomp
Expand All @@ -13,6 +15,9 @@ stamp-*
test-driver
Makefile
Makefile.in
*cppunit-results.xml
*.gcno
*.gcda
*.src.tar.gz
*.changes
*.deb
87 changes: 87 additions & 0 deletions ngsi_event_broker/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,90 @@ ACLOCAL_AMFLAGS = -I m4
SUBDIRS = src test
dist_noinst_DATA = LICENSE README.rst
distdir = @PACKAGE_DISTNAME@-@VERSION@.src

## Custom targets ##

PROJECT_DIR = $(CURDIR)
PROJECT_REPORT_DIR = $(PROJECT_DIR)/report
PROJECT_SITE_DIR = $(PROJECT_DIR)/site
CPPUNIT_REPORT_DIR = $(PROJECT_REPORT_DIR)/test
CPPCHECK_REPORT_DIR = $(PROJECT_REPORT_DIR)/cppcheck
COVERAGE_REPORT_DIR = $(PROJECT_REPORT_DIR)/coverage
COVERAGE_SITE_DIR = $(PROJECT_SITE_DIR)/coverage/lcov-report

CPPCHECK_FLAGS = -I/usr/include $(CPPFLAGS)
CPPCHECK_FLAGS += --suppress='*:/usr/include/*.h'
CPPCHECK_FLAGS += --suppress='*:/usr/include/curl/*.h'
CPPCHECK_FLAGS += --enable=all --max-configs=100

XSLTPROC_FLAGS = $(PROJECT_DIR)/script/build/files/cppunit2xunit.xsl

# function to get relative path
relpath = $(subst $(CURDIR)/,,$(1))

# function to call cppcheck with args $1 and filter output to file/descriptor $2
cppcheck = { cppcheck $(CPPCHECK_FLAGS) $(1) 2>&1 1>&3 | fgrep -v 'Unmatched suppression' 1>$(2); } 3>&1

# remove generated reports
clean-local:
@rm -rf $(PROJECT_REPORT_DIR)
@rm -rf $(PROJECT_SITE_DIR)

# check coding style
lint:
$(call cppcheck,src,&2)

# generate coding style report
lint-report: REPORT_DIR = $(call relpath,$(CPPCHECK_REPORT_DIR))
lint-report:
@mkdir -p $(CPPCHECK_REPORT_DIR)
$(call cppcheck,--xml src,$(CPPCHECK_REPORT_DIR)/cppcheck-result.xml)
@echo "Generated \`$(REPORT_DIR)/cppcheck-result.xml'"

# synonym for standard 'check' target
test: check

# generate xunit test reports
test-report: REPORT_DIR = $(call relpath,$(CPPUNIT_REPORT_DIR))
test-report:
@$(MAKE) check
@rm -rf $(CPPUNIT_REPORT_DIR)
@mkdir -p $(CPPUNIT_REPORT_DIR)
@cd test; for i in *-cppunit-results.xml; do \
xsltproc $(XSLTPROC_FLAGS) $$i \
> $(CPPUNIT_REPORT_DIR)/TEST-xunit-$${i%-cppunit-results.xml}.xml \
&& echo "Generated \`$(REPORT_DIR)/TEST-xunit-$${i%-cppunit-results.xml}.xml'"; \
done

if !GCOV_ENABLED

coverage coverage-report:
@printf "Please run \`./configure --enable-gcov'\n\n" 1>&2

else GCOV_ENABLED

# check coverage
coverage:
@echo "Running tests..."
@$(MAKE) check >/dev/null
cd src/.libs; gcovr --root=. --print-summary

# generate coverage report (and site)
coverage-report: REPORT_DIR = $(call relpath,$(COVERAGE_REPORT_DIR))
coverage-report: SITE_DIR = $(call relpath,$(COVERAGE_SITE_DIR))
coverage-report:
@echo "Running tests..."
@$(MAKE) check >/dev/null
@rm -rf $(COVERAGE_SITE_DIR)
@mkdir -p $(COVERAGE_SITE_DIR)
@mkdir -p $(COVERAGE_REPORT_DIR)
lcov -d src/.libs --capture --output-file $(COVERAGE_REPORT_DIR)/lcov.info
lcov -r $(COVERAGE_REPORT_DIR)/lcov.info "/usr/include/*" -o $(COVERAGE_REPORT_DIR)/lcov.info
genhtml -o $(COVERAGE_SITE_DIR) $(COVERAGE_REPORT_DIR)/lcov.info
@sed -i s:"$(CURDIR)":".":g $(COVERAGE_REPORT_DIR)/lcov.info
cd src/.libs; gcovr --root=. -x -o $(COVERAGE_REPORT_DIR)/cobertura-coverage.xml
@sed -i s#filename=\"#filename=\"src/#g $(COVERAGE_REPORT_DIR)/cobertura-coverage.xml
@for i in $(REPORT_DIR)/*; do echo "Generated \`$$i'"; done
@if [ -d "$(SITE_DIR)" ]; then echo "Generated \`$(SITE_DIR)'"; fi

endif
15 changes: 15 additions & 0 deletions ngsi_event_broker/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ if test "$libdir" = '${exec_prefix}/lib'; then
[libdir=$exec_prefix/lib/nagios])
fi

# Optional features.
AC_ARG_ENABLE(gcov,
AC_HELP_STRING([--enable-gcov],
[Turn on gcov coverage testing (GCC only).]),
[if test "$enableval" = "yes"; then
if test "$GCC" = "yes"; then
AC_MSG_NOTICE([enabled gcov coverage])
CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage -g -UNDEBUG"
LDFLAGS="$LDFLAGS -coverage"
else
AC_MSG_ERROR([--enable-gcov requires GCC.])
fi
fi])
AM_CONDITIONAL([GCOV_ENABLED], [test "$enableval" = "yes"])

# Configuration arguments.
AC_ARG_WITH(nagios-srcdir,
AC_HELP_STRING([--with-nagios-srcdir=DIR],
Expand Down
51 changes: 51 additions & 0 deletions ngsi_event_broker/script/build/files/cppunit2xunit.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<testsuite>
<xsl:attribute name="errors">
<xsl:value-of select="TestRun/Statistics/Errors"/>
</xsl:attribute>
<xsl:attribute name="failures">
<xsl:value-of select="TestRun/Statistics/Failures"/>
</xsl:attribute>
<xsl:attribute name="tests">
<xsl:value-of select="TestRun/Statistics/Tests"/>
</xsl:attribute>
<xsl:attribute name="name">from cppunit</xsl:attribute>
<xsl:apply-templates/>
</testsuite>
</xsl:template>
<xsl:template match="/TestRun/SuccessfulTests/Test">
<testcase>
<xsl:attribute name="classname">
<xsl:value-of select="substring-before(Name, '::')"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="substring-after(Name, '::')"/>
</xsl:attribute>
</testcase>
</xsl:template>
<xsl:template match="/TestRun/FailedTests/FailedTest">
<testcase>
<xsl:attribute name="classname">
<xsl:value-of select="substring-before(Name, '::')"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="substring-after(Name, '::')"/>
</xsl:attribute>
<error>
<xsl:attribute name="message">
<xsl:value-of select=" normalize-space(Message)"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="FailureType"/>
</xsl:attribute>
<xsl:value-of select="Message"/>
File:<xsl:value-of select="Location/File"/>
Line:<xsl:value-of select="Location/Line"/>
</error>
</testcase>
</xsl:template>
<xsl:template match="text()|@*"/>
</xsl:stylesheet>
8 changes: 8 additions & 0 deletions ngsi_event_broker/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ ngsi_event_broker_xifi_la_CFLAGS = -Wall
ngsi_event_broker_xifi_la_LDFLAGS = -module -avoid-version
ngsi_event_broker_xifi_la_LIBADD = -lcurl

# remove unnecessary files
install-exec-hook:
@rm -f $(DESTDIR)$(libdir)/ngsi_event_broker_*.la


# reset all execution counts to zero prior coverage check
if GCOV_ENABLED
check-local:
lcov -d .libs --zerocounters
endif
4 changes: 4 additions & 0 deletions ngsi_event_broker/test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ $(UNITTESTS_NAGIOS_MAIN): @NAGIOS_SRCDIR@/base/nagios.c

$(UNITTESTS_BROKER_COMMON_STUB): $(top_builddir)/src/ngsi_event_broker_common.c
$(SED) '/char\*[ \t]*find_plugin_command_name/,/^}/ { d; }' $< > $@

# remove gcov and cppunit files
clean-local:
rm -f *.gcda *.gcno *-cppunit-results.xml
8 changes: 7 additions & 1 deletion ngsi_event_broker/test/suite_argument_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@
#include <list>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include "argument_parser.h"
#include "cppunit/TestResult.h"
#include "cppunit/TestFixture.h"
#include "cppunit/TextTestRunner.h"
#include "cppunit/XmlOutputter.h"
#include "cppunit/BriefTestProgressListener.h"
#include "cppunit/extensions/HelperMacros.h"


using CppUnit::TestResult;
using CppUnit::TestFixture;
using CppUnit::TextTestRunner;
using CppUnit::XmlOutputter;
using CppUnit::BriefTestProgressListener;
using namespace std;

Expand Down Expand Up @@ -94,7 +97,7 @@ class ArgumentParserTest: public TestFixture


/// Suite startup
int main(void)
int main(int argc, char* argv[])
{
TextTestRunner runner;
BriefTestProgressListener progress;
Expand All @@ -104,6 +107,9 @@ int main(void)
cout << endl << endl;
bool success = runner.run("", false, true, false);
ArgumentParserTest::suiteTearDown();
ofstream xmlFileOut((string(argv[0]) + "-cppunit-results.xml").c_str());
XmlOutputter xmlOut(&runner.result(), xmlFileOut);
xmlOut.write();
return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
8 changes: 7 additions & 1 deletion ngsi_event_broker/test/suite_broker_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@

#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include "config.h"
#include "ngsi_event_broker_common.h"
#include "cppunit/TestResult.h"
#include "cppunit/TestFixture.h"
#include "cppunit/TextTestRunner.h"
#include "cppunit/XmlOutputter.h"
#include "cppunit/BriefTestProgressListener.h"
#include "cppunit/extensions/HelperMacros.h"


using CppUnit::TestResult;
using CppUnit::TestFixture;
using CppUnit::TextTestRunner;
using CppUnit::XmlOutputter;
using CppUnit::BriefTestProgressListener;
using namespace std;

Expand Down Expand Up @@ -101,7 +104,7 @@ class BrokerCommonTest: public TestFixture


/// Suite startup
int main(void)
int main(int argc, char* argv[])
{
TextTestRunner runner;
BriefTestProgressListener progress;
Expand All @@ -111,6 +114,9 @@ int main(void)
cout << endl << endl;
bool success = runner.run("", false, true, false);
BrokerCommonTest::suiteTearDown();
ofstream xmlFileOut((string(argv[0]) + "-cppunit-results.xml").c_str());
XmlOutputter xmlOut(&runner.result(), xmlFileOut);
xmlOut.write();
return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
8 changes: 7 additions & 1 deletion ngsi_event_broker/test/suite_broker_xifi_dem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
Expand All @@ -40,13 +41,15 @@
#include "cppunit/TestResult.h"
#include "cppunit/TestFixture.h"
#include "cppunit/TextTestRunner.h"
#include "cppunit/XmlOutputter.h"
#include "cppunit/BriefTestProgressListener.h"
#include "cppunit/extensions/HelperMacros.h"


using CppUnit::TestResult;
using CppUnit::TestFixture;
using CppUnit::TextTestRunner;
using CppUnit::XmlOutputter;
using CppUnit::BriefTestProgressListener;
using namespace std;

Expand Down Expand Up @@ -122,7 +125,7 @@ class BrokerXifiDemTest: public TestFixture


/// Suite startup
int main(void)
int main(int argc, char* argv[])
{
TextTestRunner runner;
BriefTestProgressListener progress;
Expand All @@ -132,6 +135,9 @@ int main(void)
cout << endl << endl;
bool success = runner.run("", false, true, false);
BrokerXifiDemTest::suiteTearDown();
ofstream xmlFileOut((string(argv[0]) + "-cppunit-results.xml").c_str());
XmlOutputter xmlOut(&runner.result(), xmlFileOut);
xmlOut.write();
return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
8 changes: 7 additions & 1 deletion ngsi_event_broker/test/suite_broker_xifi_npm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
Expand All @@ -40,13 +41,15 @@
#include "cppunit/TestResult.h"
#include "cppunit/TestFixture.h"
#include "cppunit/TextTestRunner.h"
#include "cppunit/XmlOutputter.h"
#include "cppunit/BriefTestProgressListener.h"
#include "cppunit/extensions/HelperMacros.h"


using CppUnit::TestResult;
using CppUnit::TestFixture;
using CppUnit::TextTestRunner;
using CppUnit::XmlOutputter;
using CppUnit::BriefTestProgressListener;
using namespace std;

Expand Down Expand Up @@ -140,7 +143,7 @@ class BrokerXifiNpmTest: public TestFixture


/// Suite startup
int main(void)
int main(int argc, char* argv[])
{
TextTestRunner runner;
BriefTestProgressListener progress;
Expand All @@ -150,6 +153,9 @@ int main(void)
cout << endl << endl;
bool success = runner.run("", false, true, false);
BrokerXifiNpmTest::suiteTearDown();
ofstream xmlFileOut((string(argv[0]) + "-cppunit-results.xml").c_str());
XmlOutputter xmlOut(&runner.result(), xmlFileOut);
xmlOut.write();
return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
}

Expand Down
Loading

0 comments on commit 269f3ce

Please sign in to comment.