[cpuperf] Specify runtime deps

The specification of runtime deps on a linux/mac test is needed for
portable testing so that it may be run outside of the tree.

Moreover, this change removes the dependency of these tests on tree
source.

See test_spec.gni in
https://fuchsia-review.googlesource.com/c/fuchsia/+/249798 for how this
information will be aggregated.

Bug: IN-819
Test: No change to production; correct test metadata produced.

Change-Id: I8fe8471ea98ae6666a26f4d927b00dd7da503d2d
diff --git a/garnet/bin/cpuperf/print/tests/BUILD.gn b/garnet/bin/cpuperf/print/tests/BUILD.gn
index c4db3da..7561daa 100644
--- a/garnet/bin/cpuperf/print/tests/BUILD.gn
+++ b/garnet/bin/cpuperf/print/tests/BUILD.gn
@@ -5,10 +5,47 @@
 import("//build/go/go_library.gni")
 import("//build/go/go_test.gni")
 
+copy("raw_printer_test_data") {
+  sources = [
+    "raw-expected-output.txt",
+    "raw-test.0.0.cpuperf",
+    "raw-test.0.1.cpuperf",
+    "raw-test.0.2.cpuperf",
+    "raw-test.0.3.cpuperf",
+    "raw-test.cpsession",
+  ]
+  outputs = [
+    "$root_out_dir/test_data/cpuperf/{{source_file_part}}",
+  ]
+}
+
+# TODO(IN-819): Delete the following target once cpuperf tests are no
+# longer being run out of $root_build_dir/host_tests/.
+copy("raw_printer_test_data_deprecated") {
+  sources = [
+    "raw-expected-output.txt",
+    "raw-test.0.0.cpuperf",
+    "raw-test.0.1.cpuperf",
+    "raw-test.0.2.cpuperf",
+    "raw-test.0.3.cpuperf",
+    "raw-test.cpsession",
+  ]
+  outputs = [
+    "$root_build_dir/host_tests/test_data/cpuperf/{{source_file_part}}",
+  ]
+}
+
 go_library("raw_printer_test_lib") {
   name = "cpuperf/printer/tests"
-
-  deps = []
+  non_go_deps = [
+    "//garnet/bin/cpuperf/print:cpuperf_print($host_toolchain)",
+    ":raw_printer_test_data",
+    ":raw_printer_test_data_deprecated",
+  ]
+  metadata = {
+    test_runtime_deps = [ "$root_out_dir/cpuperf_print" ] +
+                        get_target_outputs(":raw_printer_test_data")
+  }
 }
 
 go_test("cpuperf_raw_printer_test") {
@@ -16,5 +53,4 @@
   deps = [
     ":raw_printer_test_lib",
   ]
-  non_go_deps = [ "..:host_cpuperf_print" ]
 }
diff --git a/garnet/bin/cpuperf/print/tests/raw_printer_test.go b/garnet/bin/cpuperf/print/tests/raw_printer_test.go
index 340356b..124cd69 100644
--- a/garnet/bin/cpuperf/print/tests/raw_printer_test.go
+++ b/garnet/bin/cpuperf/print/tests/raw_printer_test.go
@@ -8,21 +8,43 @@
 	"io/ioutil"
 	"os"
 	"path"
+	"path/filepath"
+	"runtime"
 	"testing"
 )
 
 const (
-	sessionFile        string = "garnet/bin/cpuperf/print/tests/raw-test.cpsession"
-	expectedOutputFile string = "garnet/bin/cpuperf/print/tests/raw-expected-output.txt"
-	printerProgram     string = "cpuperf_print"
-	outputTempFile            = "raw-printer-test."
+	sessionFilename        = "raw-test.cpsession"
+	expectedOutputFilename = "raw-expected-output.txt"
+	printerProgram         = "cpuperf_print"
+	outputTempFile         = "raw-printer-test."
 )
 
 func TestRawPrinter(t *testing.T) {
-	hostBuildDir := getHostBuildDir()
+	testPath, err := filepath.Abs(os.Args[0])
+	if err != nil {
+		t.Fatal(err)
+	}
+	outDir := filepath.Dir(testPath)
+
+	// TODO(IN-819): a required dance, so long as we are running tests out
+	// of $root_build_dir/host_tests, given how the cpuperf_print tool is
+	// symlinked when built with a variant (e.g., host_asan). When this is
+	// no longer the case, we may set
+	// printerProgramPath := path.Join(outDir, printerProgram)
+	rootBuildDir := filepath.Dir(outDir)
+	var hostBuildDir string
+	switch runtime.GOARCH {
+	case "amd64":
+		hostBuildDir = path.Join(rootBuildDir, "host_x64")
+	case "arm64":
+		hostBuildDir = path.Join(rootBuildDir, "host_arm64")
+	}
 	printerProgramPath := path.Join(hostBuildDir, printerProgram)
-	sessionSpecPath := path.Join(fuchsiaRoot, sessionFile)
-	expectedOutputPath := path.Join(fuchsiaRoot, expectedOutputFile)
+
+	testDataDir := path.Join(outDir, "test_data", "cpuperf")
+	sessionSpecPath := path.Join(testDataDir, sessionFilename)
+	expectedOutputPath := path.Join(testDataDir, expectedOutputFilename)
 
 	outputFile, err := ioutil.TempFile("", outputTempFile)
 	if err != nil {
@@ -42,7 +64,6 @@
 
 	err = compareFiles(expectedOutputPath, outputFile.Name())
 	if err != nil {
-		t.Fatalf("Error comparing output with expected output: ",
-			err.Error())
+		t.Fatalf("Error comparing output with expected output: %v", err)
 	}
 }
diff --git a/garnet/bin/cpuperf/print/tests/util.go b/garnet/bin/cpuperf/print/tests/util.go
index 2aff407..5136c72 100644
--- a/garnet/bin/cpuperf/print/tests/util.go
+++ b/garnet/bin/cpuperf/print/tests/util.go
@@ -13,76 +13,9 @@
 	"fmt"
 	"io"
 	"io/ioutil"
-	"os"
 	"os/exec"
-	"path"
-	"runtime"
 )
 
-var (
-	fuchsiaRoot = getFuchsiaRoot()
-	buildRoot   = getBuildRoot(fuchsiaRoot)
-)
-
-// TODO(dje): Move into a common package, use by traceutil too.
-func getFuchsiaRoot() string {
-	execPath, err := os.Executable()
-	if err != nil {
-		panic(err.Error())
-	}
-
-	dir, _ := path.Split(execPath)
-	for dir != "" && dir != "/" {
-		dir = path.Clean(dir)
-		manifestPath := path.Join(dir, ".jiri_manifest")
-		if _, err = os.Stat(manifestPath); !os.IsNotExist(err) {
-			return dir
-		}
-		dir, _ = path.Split(dir)
-	}
-
-	panic("Can not determine Fuchsia source root based on executable path.")
-}
-
-func getProgramBuildDir() string {
-	execPath, err := os.Executable()
-	if err != nil {
-		panic(err.Error())
-	}
-	dir, _ := path.Split(execPath)
-	return dir
-}
-
-func getBuildRoot(fxRoot string) string {
-	execPath := getProgramBuildDir()
-
-	outPath := path.Join(fxRoot, "out")
-	dir, file := path.Split(execPath)
-	for dir != "" && dir != "/" {
-		dir = path.Clean(dir)
-		if dir == outPath {
-			return path.Join(dir, file)
-		}
-		dir, file = path.Split(dir)
-	}
-
-	panic("Can not determine output directory based on executable path.")
-}
-
-// Return (effectively) $FUCHSIA_DIR/out/$ARCH/host_$ARCH.
-func getHostBuildDir() string {
-	arch := ""
-	switch runtime.GOARCH {
-	case "amd64":
-		arch = "x64"
-	case "arm64":
-		arch = "arm64"
-	default:
-		panic(fmt.Errorf("unknown GOARCH: %s", runtime.GOARCH))
-	}
-	return path.Join(buildRoot, "host_"+arch)
-}
-
 func runCommandWithOutputToFile(command string, args []string,
 	output io.Writer) error {
 	// This doesn't use testing.Logf or some such because we always