Add support for passing a custom run plan to TF.

Also:
- Fix for VTS parameters.
- Optimization of internal TF docker script (reduces docker image by
  300M)

BUG=None
TEST=Local led test

Change-Id: I85eb77a9e7f4f9b3f09b51bd11866c5c20543268
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/5923296
Reviewed-by: Varun Srivastav <varunsrivastav@google.com>
Auto-Submit: Alex Bergman <abergman@google.com>
Tested-by: Alex Bergman <abergman@google.com>
Commit-Queue: Alex Bergman <abergman@google.com>
Commit-Queue: Varun Srivastav <varunsrivastav@google.com>
diff --git a/src/chromiumos/test/dockerfiles/cros-test/Dockerfile_TF b/src/chromiumos/test/dockerfiles/cros-test/Dockerfile_TF
index 301aca3..a82557e 100644
--- a/src/chromiumos/test/dockerfiles/cros-test/Dockerfile_TF
+++ b/src/chromiumos/test/dockerfiles/cros-test/Dockerfile_TF
@@ -23,9 +23,9 @@
     tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
     curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | \
     apt-key --keyring /usr/share/keyrings/cloud.google.gpg  \
-    add - && apt-get update -y && apt-get install google-cloud-sdk=369.0.0-0 -y
-RUN rm -rf /usr/lib/google-cloud-sdk/bin/anthoscli \
-  /usr/lib/google-cloud-sdk/bin/kuberun
+    add - && apt-get update -y && apt-get install google-cloud-sdk=369.0.0-0 -y && \
+    rm -rf /usr/lib/google-cloud-sdk/bin/anthoscli \
+    /usr/lib/google-cloud-sdk/bin/kuberun
 
 # Point /usr/bin/python3 to usr/local/bin/python3. See b/191884161
 RUN sudo rm /usr/bin/python3*; exit 0
diff --git a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/driver/tradefed_xts_command_builder.go b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/driver/tradefed_xts_command_builder.go
index caeceaa..eb20d45 100644
--- a/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/driver/tradefed_xts_command_builder.go
+++ b/src/go.chromium.org/chromiumos/test/execution/cmd/cros-test/internal/driver/tradefed_xts_command_builder.go
@@ -15,6 +15,7 @@
 )
 
 const (
+	planMetadataFlag     = "plan"
 	tfGoogleTestRunner   = "google/cts/google-cts-launcher-for-aosp"
 	tfAospTestRunner     = "common-compatibility-config"
 	invocationTimeout    = "10h"
@@ -50,19 +51,54 @@
 	}
 }
 
+func formatTestName(fqTestName string) string {
+	testName := fqTestName
+
+	// First, trim test suite prefix for all known suites.
+	for _, suite := range knownSuites {
+		testName = strings.TrimPrefix(testName, suite+".")
+	}
+
+	// Then, check if we need to split the test class from the module name,
+	// i.e. "CtsModule#com.android.test.Class#testCase" is converted to
+	// "CtsModule com.android.test.Class#testCase"
+	if strings.Contains(testName, "#") {
+		return strings.Replace(testName, "#", " ", 1)
+	} else {
+		return testName
+	}
+}
+
+func extractMetadataFlag(metadata *api.ExecutionMetadata, flagName string) (string, error) {
+	if metadata != nil && len(metadata.Args) > 0 {
+		for _, arg := range metadata.Args {
+			if arg.Flag == flagName {
+				return arg.Value, nil
+			}
+		}
+	}
+	return "", fmt.Errorf("flag %s for found in test metadata", flagName)
+}
+
 func BuildXtsTestCommand(logger *log.Logger, testType string, tests []*api.TestCaseMetadata,
 	serials []string, metadata *api.ExecutionMetadata) []string {
 
 	cmd := []string{getTestRunner()}
 
+	plan, err := extractMetadataFlag(metadata, planMetadataFlag)
+	if err != nil {
+		// By default, plan is the same as test suite type, i.e. "cts", "dts", etc.
+		plan = testType
+	}
+
 	// Add test type specific arguments.
 	if isAospTradefed() {
-		cmd = append(cmd, "--plan", testType, "--log-level-display", "VERBOSE",
+		cmd = append(cmd, "--plan", plan, "--log-level-display", "VERBOSE",
 			"--test-tag", fmt.Sprintf("cros-%s-test", testType),
 			"--use-device-build-info", "--primary-abi-only", "--stage-remote-file",
 			"--load-configs-with-include-filters", "--no-skip-staging-artifacts")
 	} else {
-		cmd = append(cmd, "--config-name", testType,
+		cmd = append(cmd, "--config-name", plan,
 			"--test-tag", fmt.Sprintf("cros-%s-test", testType),
 			"--cts-package-name", fmt.Sprintf("android-%s.zip", testType),
 			"--rootdir-var", fmt.Sprintf("%s_ROOT", strings.ToUpper(testType)))
@@ -93,7 +129,7 @@
 			"--logcat-on-failure", "--screenshot-on-failure", "--include-test-log-tags",
 			"--result-reporter:disable-result-posting", "--use-log-saver",
 		}
-		if testType != "gts" {
+		if testType != "gts" && testType != "vts" {
 			ctsParams = append(ctsParams, "--property-check:no-throw-error")
 		}
 		for _, param := range ctsParams {
@@ -111,25 +147,13 @@
 				"--ants-result-reporter:save-metric-file")
 
 			// Extract AnTS invocation id and WorkUnit ID from execution metadata.
-			var AnTsInvID string
-			var AnTsWorkUnitID string
-			if metadata != nil && len(metadata.Args) > 0 {
-				for _, arg := range metadata.Args {
-					if arg.Flag == "ants_invocation_id" {
-						AnTsInvID = arg.Value
-					} else if arg.Flag == "ants_work_unit_id" {
-						AnTsWorkUnitID = arg.Value
-					}
-				}
-				logger.Printf("Got invocation id: %s, work unit id: %s.", AnTsInvID, AnTsWorkUnitID)
-			}
-
-			// Add invocation id and workunit id.
-			if len(AnTsInvID) > 0 {
+			if AnTsInvID, err := extractMetadataFlag(metadata, "ants_invocation_id"); err == nil {
 				cmd = append(cmd, "--invocation-data", fmt.Sprintf("invocation_id=%s", AnTsInvID))
+				logger.Printf("Got invocation id: %s", AnTsInvID)
 			}
-			if len(AnTsWorkUnitID) > 0 {
+			if AnTsWorkUnitID, err := extractMetadataFlag(metadata, "ants_work_unit_id"); err == nil {
 				cmd = append(cmd, "--invocation-data", fmt.Sprintf("work_unit_id=%s", AnTsWorkUnitID))
+				logger.Printf("Got work unit id: %s", AnTsWorkUnitID)
 			}
 		} else {
 			cmd = append(cmd, "--cts-params", "--result-reporter:disable")
@@ -146,8 +170,7 @@
 	var buildInfoReported = false
 
 	for _, t := range tests {
-		testName := strings.TrimPrefix(t.TestCase.Name, "cts.")
-		testName = strings.TrimPrefix(testName, "dts.")
+		testName := formatTestName(t.TestCase.Name)
 		branch, target, build := extractBuildInfo(t.TestCase)
 		if len(branch) > 0 && len(target) > 0 && len(build) > 0 && !buildInfoReported {
 			cmd = append(cmd, "--branch", branch, "--build-flavor", target,
@@ -158,6 +181,7 @@
 			}
 			buildInfoReported = true
 		}
+		// quotedTestName := fmt.Sprintf("\"%s\"", testName)
 		logger.Println("Running provided test: ", testName,
 			" for branch/target/build: ", branch, "/", target, "/", build)