Enabling jenkins code coverage

Change-Id: If69a83ffcf0450ac06f7dab17bf862e36aef37ed
Refs: #3434
diff --git a/.jenkins b/.jenkins
new file mode 100755
index 0000000..674d751
--- /dev/null
+++ b/.jenkins
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+set -e
+
+DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+
+for file in "$DIR"/.jenkins.d/*; do
+    [[ -f $file && -x $file ]] || continue
+    echo "Run: $file"
+    "$file"
+done
diff --git a/.jenkins.d/00-deps.sh b/.jenkins.d/00-deps.sh
new file mode 100755
index 0000000..c32219b
--- /dev/null
+++ b/.jenkins.d/00-deps.sh
@@ -0,0 +1,29 @@
+#!/usr/bin/env bash
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+if has OSX $NODE_LABELS; then
+    if has OSX-10.8 $NODE_LABELS; then
+        EXTRA_FLAGS=--c++11
+    fi
+
+    set -x
+    brew update
+    brew upgrade
+    brew install boost pkg-config cryptopp $EXTRA_FLAGS
+    brew cleanup
+fi
+
+if has Ubuntu $NODE_LABELS; then
+    BOOST_PKG=libboost-all-dev
+    if has Ubuntu-12.04 $NODE_LABELS; then
+        BOOST_PKG=libboost1.48-all-dev
+    fi
+
+    set -x
+    sudo apt-get update -qq -y
+    sudo apt-get -qq -y install build-essential pkg-config $BOOST_PKG \
+                                libcrypto++-dev libsqlite3-dev
+fi
diff --git a/.jenkins.d/01-ndn-cxx.sh b/.jenkins.d/01-ndn-cxx.sh
new file mode 100755
index 0000000..2bc46fd
--- /dev/null
+++ b/.jenkins.d/01-ndn-cxx.sh
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+set -x
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+pushd ${CACHE_DIR:-/tmp} >/dev/null
+
+INSTALLED_VERSION=$((cd ndn-cxx && git rev-parse HEAD) 2>/dev/null || echo NONE)
+
+sudo rm -Rf ndn-cxx-latest
+
+git clone --depth 1 git://github.com/named-data/ndn-cxx ndn-cxx-latest
+
+LATEST_VERSION=$((cd ndn-cxx-latest && git rev-parse HEAD) 2>/dev/null || echo UNKNOWN)
+
+if [[ $INSTALLED_VERSION != $LATEST_VERSION ]]; then
+    sudo rm -Rf ndn-cxx
+    mv ndn-cxx-latest ndn-cxx
+else
+    sudo rm -Rf ndn-cxx-latest
+fi
+
+sudo rm -Rf /usr/local/include/ndn-cxx
+sudo rm -f /usr/local/lib/libndn-cxx*
+sudo rm -f /usr/local/lib/pkgconfig/libndn-cxx*
+
+pushd ndn-cxx >/dev/null
+
+./waf configure -j1 --color=yes --enable-shared --disable-static --without-osx-keychain
+./waf -j1 --color=yes
+sudo ./waf install -j1 --color=yes
+
+popd >/dev/null
+popd >/dev/null
+
+if has Linux $NODE_LABELS; then
+    sudo ldconfig
+elif has FreeBSD $NODE_LABELS; then
+    sudo ldconfig -a
+fi
diff --git a/.jenkins.d/10-build.sh b/.jenkins.d/10-build.sh
new file mode 100755
index 0000000..4fef69c
--- /dev/null
+++ b/.jenkins.d/10-build.sh
@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+set -x
+set -e
+
+git submodule init
+git submodule sync
+git submodule update
+
+# Cleanup
+sudo ./waf -j1 --color=yes distclean
+
+# Configure/build in optimized mode with tests and precompiled headers
+./waf -j1 --color=yes configure --with-tests
+./waf -j1 --color=yes build
+
+# Cleanup
+sudo ./waf -j1 --color=yes distclean
+
+# Configure/build in optimized mode without tests and with precompiled headers
+./waf -j1 --color=yes configure
+./waf -j1 --color=yes build
+
+# Cleanup
+sudo ./waf -j1 --color=yes distclean
+
+./waf -j1 --color=yes configure --with-tests
+./waf -j1 --color=yes build
+
+# (tests will be run against debug version)
+
+# Install
+sudo ./waf -j1 --color=yes install
diff --git a/.jenkins.d/20-tests.sh b/.jenkins.d/20-tests.sh
new file mode 100755
index 0000000..bb1cf63
--- /dev/null
+++ b/.jenkins.d/20-tests.sh
@@ -0,0 +1,23 @@
+#!/usr/bin/env bash
+set -x
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+# Prepare environment
+rm -Rf ~/.ndn
+
+if has OSX $NODE_LABELS; then
+  echo "Unlocking OSX Keychain"
+  security unlock-keychain -p "named-data"
+fi
+
+ndnsec-keygen "/tmp/jenkins/$NODE_NAME" | ndnsec-install-cert -
+
+# Run unit tests
+if [[ -n "$XUNIT" ]]; then
+    ./build/unit-tests --log_level=all -- --log_format2=XML --log_sink2=build/xunit-report.xml
+else
+    ./build/unit-tests -l test_suite
+fi
diff --git a/.jenkins.d/30-coverage.sh b/.jenkins.d/30-coverage.sh
new file mode 100644
index 0000000..a00f8f1
--- /dev/null
+++ b/.jenkins.d/30-coverage.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+set -x
+set -e
+
+JDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
+source "$JDIR"/util.sh
+
+if [[ "$JOB_NAME" == *"code-coverage" ]]; then
+  BASE="`pwd | sed -e 's|/|\\\/|g'`\\"
+  (cd build && gcovr -x -f $BASE/core -f $BASE/daemon -f $BASE/rib -r ../ -o coverage.xml ./)
+fi
diff --git a/.jenkins.d/util.sh b/.jenkins.d/util.sh
new file mode 100755
index 0000000..81c8931
--- /dev/null
+++ b/.jenkins.d/util.sh
@@ -0,0 +1,9 @@
+has() {
+    local p=$1
+    shift
+    local x
+    for x in "$@"; do
+        [[ "${x}" == "${p}" ]] && return 0
+    done
+    return 1
+}
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..9503148
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,21 @@
+sudo: required
+language: generic
+matrix:
+  include:
+    - os: linux
+      dist: trusty
+      compiler: gcc
+    - os: linux
+      dist: trusty
+      compiler: clang
+    - os: osx
+      compiler: clang
+notifications:
+  email:
+    on_success: always
+    on_failure: always
+script:
+  - if [[ $TRAVIS_OS_NAME == linux ]]; then export NODE_LABELS="Linux Ubuntu Ubuntu-14.04"; fi
+  - if [[ $TRAVIS_OS_NAME == osx ]]; then export NODE_LABELS="OSX OSX-10.9"; fi
+  - echo $NODE_LABELS
+  - ./.jenkins
diff --git a/.waf-tools/coverage.py b/.waf-tools/coverage.py
new file mode 100644
index 0000000..ce92883
--- /dev/null
+++ b/.waf-tools/coverage.py
@@ -0,0 +1,22 @@
+# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+from waflib import TaskGen, Logs
+
+def options(opt):
+    opt.add_option('--with-coverage', action='store_true', default=False, dest='with_coverage',
+                   help='''Set compiler flags for gcc to enable code coverage information''')
+
+def configure(conf):
+    if conf.options.with_coverage:
+        if not conf.options.debug:
+            conf.fatal("Code coverage flags require debug mode compilation (add --debug)")
+        conf.check_cxx(cxxflags=['-fprofile-arcs', '-ftest-coverage', '-fPIC'],
+                       linkflags=['-fprofile-arcs'], uselib_store='GCOV', mandatory=True)
+
+@TaskGen.feature('cxx','cc')
+@TaskGen.after('process_source')
+def add_coverage(self):
+    if getattr(self, 'use', ''):
+        self.use += ' GCOV'
+    else:
+        self.use = 'GCOV'
diff --git a/tests/unit-tests/boost-test.hpp b/tests/unit-tests/boost-test.hpp
new file mode 100644
index 0000000..149c310
--- /dev/null
+++ b/tests/unit-tests/boost-test.hpp
@@ -0,0 +1,33 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#ifndef NTORRENT_TESTS_BOOST_TEST_HPP
+#define NTORRENT_TESTS_BOOST_TEST_HPP
+
+// suppress warnings from Boost.Test
+#pragma GCC system_header
+#pragma clang system_header
+
+#include <boost/test/unit_test.hpp>
+#include <boost/concept_check.hpp>
+#include <boost/test/output_test_stream.hpp>
+
+#endif // NTORRENT_TESTS_BOOST_TEST_HPP
diff --git a/tests/unit-tests/dummy.t.cpp b/tests/unit-tests/dummy.t.cpp
new file mode 100644
index 0000000..de6d256
--- /dev/null
+++ b/tests/unit-tests/dummy.t.cpp
@@ -0,0 +1,37 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "boost-test.hpp"
+
+namespace nTorrent {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(DummyTest)
+
+BOOST_AUTO_TEST_CASE(Dummy)
+{
+  BOOST_CHECK_EQUAL(1, 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nTorrent
diff --git a/wscript b/wscript
index 551c572..fc24ded 100644
--- a/wscript
+++ b/wscript
@@ -9,7 +9,7 @@
 
     opt.load(['compiler_c', 'compiler_cxx', 'gnu_dirs'])
 
-    opt.load(['default-compiler-flags', 'boost',
+    opt.load(['default-compiler-flags', 'coverage', 'boost',
               'doxygen', 'sphinx_build'],
               tooldir=['.waf-tools'])
 
@@ -42,6 +42,9 @@
                    " (http://redmine.named-data.net/projects/nfd/wiki/Boost_FAQ)")
         return
 
+    # Loading "late" to prevent tests to be compiled with profiling flags
+    conf.load('coverage')
+
     conf.write_config_header('src/config.h')
 
 def build (bld):