Fixes to bazel-job (#91)

 - Move the BEP events file to ${test_folder}:
   if we do not do move it to a unique folder, then
   each configuration overwrite each other.
 - Correctly inject the drive letter from URI when
   copying files on Windows.
 - Have a unique name for the test log output. The
   previous set-up was generating duplicate artifacts
   and they were overwriting each other, leading to
   some test not being seen.

Fixes #91.

Change-Id: If28ca9b751ab8d241bda30b573a38fc3bd5bd830
diff --git a/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy b/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
index 3fee4d1..7d62da7 100644
--- a/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
+++ b/jenkins/lib/src/build/bazel/ci/BazelUtils.groovy
@@ -157,12 +157,17 @@
   }
 
   @NonCPS
-  private static def copyCommands(cp_lines, log, test_folder) {
+  private def copyCommands(cp_lines, log, test_folder) {
     if (log != null) {
       def uri = URI.create(log.uri)
-      def relativePath = uri.path.substring(uri.path.indexOf("/testlogs/") + 10)
+      def path = uri.path
+      if (isWindows) {
+	// on windows the host is the drive letter, add it to the path.
+	path = "/${uri.host}${path}"
+      }
+      def relativePath = path.substring(path.indexOf("/testlogs/") + 10)
       cp_lines.add("mkdir -p \$(dirname '${test_folder}/${relativePath}')")
-      cp_lines.add("cp -r '${uri.path}' '${test_folder}/${relativePath}'")
+      cp_lines.add("cp -r '${path}' '${test_folder}/${relativePath}'")
     }
   }
 
@@ -190,19 +195,17 @@
   def testlogs(test_folder) {
     // JUnit test result does not look at test result if they are "old", copying them to a new
     // location, unique accross configurations.
-    def res = script.sh(
-      script: "rm -fr ${test_folder}\n" + generateTestLogsCopy(testEvents(), test_folder),
-      returnStatus: true)
-    // Archive BEP files
-    if (script.fileExists(BUILD_EVENTS_FILE)) {
-      script.archiveArtifacts artifacts: BUILD_EVENTS_FILE
-    }
-    if (script.fileExists(TEST_EVENTS_FILE)) {
-      script.archiveArtifacts artifacts: TEST_EVENTS_FILE
-    }
+    def res = script.sh(script: """#!/bin/bash
+echo 'Copying test outputs and events file for archiving'
+rm -fr ${test_folder}
+mkdir -p ${test_folder}
+touch ${BUILD_EVENTS_FILE} ${TEST_EVENTS_FILE}
+cp -f ${BUILD_EVENTS_FILE} ${TEST_EVENTS_FILE} ${test_folder}
+""" + generateTestLogsCopy(testEvents(), test_folder),
+                        returnStatus: true)
     if (res == 0) {
       // Archive the test logs and xml files
-      script.archiveArtifacts artifacts: "${test_folder}/**/test.log"
+      script.archiveArtifacts artifacts: "${test_folder}/**/test.log,${test_folder}/*.json"
       script.junit testResults: "${test_folder}/**/test.xml", allowEmptyResults: true
     }
   }
diff --git a/jenkins/lib/vars/bazelCiConfiguredJob.groovy b/jenkins/lib/vars/bazelCiConfiguredJob.groovy
index 08a6fe6..4c10dff 100644
--- a/jenkins/lib/vars/bazelCiConfiguredJob.groovy
+++ b/jenkins/lib/vars/bazelCiConfiguredJob.groovy
@@ -35,21 +35,22 @@
     def configName = descriptorToString(conf)
     configNames.add(configName)
     cfgs.add({ ->
-      script.bazelCiJob(repository: config.repository,
-                        branch: config.branch,
-                        refspec: config.refspec,
-                        node_label: conf["node"],
-                        targets: params.get("targets", ["//..."]),
-                        tests: params.get("tests", ["//..."]),
-                        configuration: params.get("configure", []),
-                        build_opts: params.get("build_opts", []),
-                        test_opts: params.get("test_opts", []),
-                        bazel_version: config.bazel_version + conf.get("variation", ""),
-                        extra_bazelrc: config.extra_bazelrc,
-                        build_tag_filters: params.get("build_tag_filters", []),
-                        test_tag_filters: params.get("test_tag_filters", []),
-                        workspace: config.workspace,
-                        sauce: config.sauce
+        script.bazelCiJob(name: configName,
+                          repository: config.repository,
+                          branch: config.branch,
+                          refspec: config.refspec,
+                          node_label: conf["node"],
+                          targets: params.get("targets", ["//..."]),
+                          tests: params.get("tests", ["//..."]),
+                          configuration: params.get("configure", []),
+                          build_opts: params.get("build_opts", []),
+                          test_opts: params.get("test_opts", []),
+                          bazel_version: config.bazel_version + conf.get("variation", ""),
+                          extra_bazelrc: config.extra_bazelrc,
+                          build_tag_filters: params.get("build_tag_filters", []),
+                          test_tag_filters: params.get("test_tag_filters", []),
+                          workspace: config.workspace,
+                          sauce: config.sauce
       )
     })
   }
diff --git a/jenkins/lib/vars/bazelCiJob.groovy b/jenkins/lib/vars/bazelCiJob.groovy
index 654585c..e2a4b96 100644
--- a/jenkins/lib/vars/bazelCiJob.groovy
+++ b/jenkins/lib/vars/bazelCiJob.groovy
@@ -50,7 +50,7 @@
   config["branch"] = config.get("branch", "master")
   config["refspec"] = config.get("refspec", "+refs/heads/*:refs/remotes/origin/*")
 
-  def prefix = "${config.node_label}"
+  def prefix = config.get("name", "${config.node_label}-${config.bazel_version}")
   def workspace = ""
 
   config.test_tag_filters += ["-noci", "-manual"]
@@ -63,8 +63,7 @@
     "-k"
   ]
   machine(config.node_label) {
-    ws("workspace/${currentBuild.fullProjectName}-" +
-       config.get("name", config.node_label + "-" + config.bazel_version)) {
+    ws("workspace/${currentBuild.fullProjectName}-${prefix}") {
       maybeSauce(config.sauce) {
         // Checkout the code
         echo "Checkout ${config.repository}"
diff --git a/jenkins/lib/vars/bazelJob.groovy b/jenkins/lib/vars/bazelJob.groovy
index acc86fd..d8ba531 100644
--- a/jenkins/lib/vars/bazelJob.groovy
+++ b/jenkins/lib/vars/bazelJob.groovy
@@ -73,7 +73,7 @@
     }
   } finally {
     stage("${stage_prefix}Results") {
-      utils.testlogs("tests-${config.stage_name}")
+      utils.testlogs("tests-${config.stage_name.replaceAll(',', '-')}")
     }
   }
 }