Got kunit.sh to generate line coverage reports

Modified kunit.sh and Dockerfile to successfully generate lcov
coverage results in the artifacts directory. Also updated
debs.bzl to get bazel to build with coverage support.

Signed-off-by: Darya Verzhbinsky <daryaver@google.com>
Change-Id: Ie87a100c9e04fee2912df1035ad086811d73cf04
diff --git a/Dockerfile b/Dockerfile
index 71a917c..8ed6d70 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM debian
+FROM debian:stretch
 RUN apt-get update \
   && apt-get install -y \
   build-essential \
@@ -7,6 +7,8 @@
   bison \
   flex \
   python3 \
+  libelf-dev \
+  lcov \
   && apt-get clean
 COPY kunit.sh /kunit.sh
 COPY kunitconfig /kunitconfig
diff --git a/README.md b/README.md
index d332d8e..193fe61 100644
--- a/README.md
+++ b/README.md
@@ -69,22 +69,38 @@
 * flex
 * bison
 * python3
+* libelf-dev
+* lcov
 
 If recursive dependencies change in future snapshots, [debs.bzl](debs.bzl) must
 be manually updated with a list of all dependencies.
 A simple solution:
 
 ```shell
-# run interactive shell in latest debian image
-docker run -it --rm debian
+# run interactive shell in debian:stretch image
+docker run -it --rm debian:stretch
 
 # update list of available packages
 apt update
 
 # generate list of all required dependencies recursively
-apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts \
---no-breaks --no-replaces --no-enhances --no-pre-depends \
-build-essential bc m4 flex bison python3 | grep "^\w" | sort -u
+apt-cache depends \
+    --recurse \
+    --no-recommends \
+    --no-suggests \
+    --no-conflicts \
+    --no-breaks \
+    --no-replaces \
+    --no-enhances \
+    --no-pre-depends \
+    build-essential \
+    bc \
+    m4 \
+    flex \
+    bison \
+    python3 \
+    libelf-dev \
+    lcov | grep "^\w" | sort -u
 ```
 
 The snapshot used can be updated as specified in the
diff --git a/debs.bzl b/debs.bzl
index aec39ca..c2dfdaf 100644
--- a/debs.bzl
+++ b/debs.bzl
@@ -30,6 +30,7 @@
         "gcc-6-base",
         "guile-2.0-libs",
         "install-info",
+        "lcov",
         "libasan3",
         "libatomic1",
         "libbison-dev",
@@ -42,6 +43,8 @@
         "libdb5.3",
         "libdebian-installer4",
         "libdpkg-perl",
+        "libelf-dev",
+        "libelf1",
         "libexpat1",
         "libffi6",
         "libgc1c2",
diff --git a/kunit.sh b/kunit.sh
index 4c2a605..170f11f 100755
--- a/kunit.sh
+++ b/kunit.sh
@@ -21,6 +21,10 @@
 # Include kunitconfig manually until we can pull it from prowjob TODO(avikr)
 cp /kunitconfig ./kunitconfig
 
+# Add correct configs to .config to make GCOV flags work
+cp kunitconfig .config
+echo -e "CONFIG_DEBUG_KERNEL=y\nCONFIG_DEBUG_INFO=y\nCONFIG_GCOV=y" >> .config
+
 # Run the KUnit tool unit tests
 python3 ./tools/testing/kunit/kunit_test.py > "$ARTIFACTS/kunit_test.log" 2>&1
 KUNIT_TEST_EXIT_CODE=$?
@@ -29,6 +33,11 @@
 python3 ./tools/testing/kunit/kunit.py run > "$ARTIFACTS/kunit.log" 2>&1
 KUNIT_EXIT_CODE=$?
 
+# Generate an HTML coverage report using LCOV
+lcov -t "kunit_presubmit_tests" -o coverage.info -c -d . > "$ARTIFACTS/lcov.log" 2>&1
+genhtml -o "$ARTIFACTS/coverage_html" coverage.info > "$ARTIFACTS/genhtml.log" 2>&1
+GENHTML_EXIT_CODE=$?
+
 if [[ $KUNIT_TEST_EXIT_CODE -ne 0 ]]; then
   echo "KUnit Tool Presubmit Failed:"
   echo "============================="
@@ -43,6 +52,18 @@
 else
   echo "KUnit Presubmit Succeeded:"
 fi
+
+echo
+
+if [[ $GENHTML_EXIT_CODE -ne 0 ]]; then
+  echo "Coverage Report Generation Failed:"
+  echo "============================="
+  cat "$ARTIFACTS/lcov.log"
+  echo "============================="
+  cat "$ARTIFACTS/genhtml.log"
+  echo "============================="
+fi
+
 echo "============================="
 cat "$ARTIFACTS/kunit.log"
 echo "============================="