Add mts-tradefed

Test: m mts, mts-tradefed run mts (add mts to CtsSampleDeviceTests build
    file)

Bug: 123411735
Change-Id: I286c72f9b3e1a04f42aafde3c74ec04da0fb4a27
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..8b31d1e
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,22 @@
+#
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+ACTUAL_LOCAL_PATH := $(LOCAL_PATH)
+
+# all-makefiles-under only searches one level of dir - meaning every new dir
+# must have Android.mk to include all makefiles under it.
+include $(call all-makefiles-under,$(ACTUAL_LOCAL_PATH))
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..72369da
--- /dev/null
+++ b/README.md
@@ -0,0 +1,13 @@
+# Android Mainline Test Suite (MTS)
+
+MTS consists of a set of testing frameworks and test cases, designed to help
+enhance the robustness, reliability, and compliance of a mainline train (i.e. 
+a set of mainline modules).
+
+MTS has mainly three types of tests:
+
+  * Unit tests
+  * CTS tests
+  * GTS tests
+
+//TODO: add user manual
diff --git a/tests/Android.mk b/tests/Android.mk
new file mode 100644
index 0000000..d4f99e1
--- /dev/null
+++ b/tests/Android.mk
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include $(call all-subdir-makefiles)
diff --git a/tools/Android.mk b/tools/Android.mk
new file mode 100644
index 0000000..d4f99e1
--- /dev/null
+++ b/tools/Android.mk
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include $(call all-subdir-makefiles)
diff --git a/tools/build/config.mk b/tools/build/config.mk
new file mode 100644
index 0000000..7dd736f
--- /dev/null
+++ b/tools/build/config.mk
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+COMPATIBILITY_TESTCASES_OUT_mts := $(HOST_OUT)/mts/android-mts/testcases
+
diff --git a/tools/mts-tradefed/Android.mk b/tools/mts-tradefed/Android.mk
new file mode 100644
index 0000000..7ac2f70
--- /dev/null
+++ b/tools/mts-tradefed/Android.mk
@@ -0,0 +1,36 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+#LOCAL_SRC_FILES += $(call all-java-files-under, src)
+
+LOCAL_JAVA_RESOURCE_DIRS := res
+
+LOCAL_SUITE_BUILD_NUMBER := $(BUILD_NUMBER_FROM_FILE)
+LOCAL_SUITE_TARGET_ARCH := $(TARGET_ARCH)
+LOCAL_SUITE_NAME := MTS
+LOCAL_SUITE_FULLNAME := "Mainline Test Suite"
+LOCAL_SUITE_VERSION := 1.0
+
+LOCAL_STATIC_JAVA_LIBRARIES := cts-tradefed-harness
+LOCAL_MODULE := mts-tradefed
+
+include $(BUILD_COMPATIBILITY_SUITE)
+
+# Build all sub-directories
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tools/mts-tradefed/README b/tools/mts-tradefed/README
new file mode 100644
index 0000000..d29070a
--- /dev/null
+++ b/tools/mts-tradefed/README
@@ -0,0 +1,61 @@
+MTS Trade Federation
+---------------------
+
+Mainline Test Suite Trade Federation, mts-tradefed for short, is built on
+top of the Android Trade Federation test harness to test mainline modules.
+
+Configuring mts-tradefed
+------------------------
+
+Ensure 'adb' is in your current PATH. adb can be found in the
+Android SDK available from http://developer.android.com
+
+Example:
+  PATH=$PATH:/home/myuser/android-sdk-linux_x86/platform-tools
+
+And ensure device is visible via 'adb devices'
+
+Using mts-tradefed
+-------------------
+
+To run a test plan on a single device:
+
+1. Make sure you have at least one device connected
+2. Launch the mts-tradefed console by running the 'mts-tradefed'. If you
+are working from the Android source tree and have run 'm mts',
+the script can be found at
+  out/host/linux-x86/ats/android-mts/tools/mts-tradefed
+3. Type:
+'run mts' to run the default MTS plan
+
+Some other useful commands are
+
+To run a test module:
+'run mts --module <module_name>'
+
+To run a specific test:
+'run mts --test <test_name>'
+
+To shard a plan test run on multiple devices
+'run mts --shards <number of shards>
+note: all connected devices must be running the same build
+
+For more options:
+'run mts --help'
+
+MTS Tradefed Development
+------------------------
+See http://source.android.com for instructions on obtaining the Android
+platform source code and setting up a build environment.
+
+The source for the tradefed framework can be found on the 'tradefed' branch.
+
+Perform these steps to build and run mts-tradefed from the development
+environment:
+cd <path to android source root>
+m mts
+mts-tradefed
+
+More documentation and details on using and extending trade federation will
+be forthcoming in the near future.
+
diff --git a/tools/mts-tradefed/etc/Android.mk b/tools/mts-tradefed/etc/Android.mk
new file mode 100644
index 0000000..a99fef9
--- /dev/null
+++ b/tools/mts-tradefed/etc/Android.mk
@@ -0,0 +1,23 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_PREBUILT_EXECUTABLES := mts-tradefed
+include $(BUILD_HOST_PREBUILT)
+
diff --git a/tools/mts-tradefed/etc/mts-tradefed b/tools/mts-tradefed/etc/mts-tradefed
new file mode 100755
index 0000000..e681ead
--- /dev/null
+++ b/tools/mts-tradefed/etc/mts-tradefed
@@ -0,0 +1,124 @@
+#!/bin/bash
+
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# launcher script for mts-tradefed harness
+# can be used from an Android build environment, or a standalone mts zip
+
+checkFile() {
+    if [ ! -f "$1" ]; then
+        echo "Unable to locate $1"
+        exit
+    fi;
+}
+
+checkPath() {
+    if ! type -P $1 &> /dev/null; then
+        echo "Unable to find $1 in path."
+        exit
+    fi;
+}
+
+checkPath adb
+checkPath java
+
+# check java version
+JAVA_VERSION=$(java -version 2>&1 | head -n 1 | grep 'version [ "]\(1\.8\|9\).*[ "]')
+if [ "${JAVA_VERSION}" == "" ]; then
+    echo "Wrong java version. 1.8 or 9 is required."
+    exit
+fi
+
+# check debug flag and set up remote debugging
+if [ -n "${TF_DEBUG}" ]; then
+  if [ -z "${TF_DEBUG_PORT}" ]; then
+    TF_DEBUG_PORT=10088
+  fi
+  RDBG_FLAG=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${TF_DEBUG_PORT}
+fi
+
+# get OS
+HOST=`uname`
+if [ "$HOST" == "Linux" ]; then
+    OS="linux-x86"
+elif [ "$HOST" == "Darwin" ]; then
+    OS="darwin-x86"
+else
+    echo "Unrecognized OS"
+    exit
+fi
+
+# check if in Android build env
+if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
+    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
+      MTS_ROOT=${ANDROID_HOST_OUT}/mts
+    else
+      MTS_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/mts
+    fi
+    if [ ! -d ${MTS_ROOT} ]; then
+        echo "Could not find $MTS_ROOT in Android build environment. Try 'make mts'"
+        exit
+    fi;
+fi;
+
+if [ -z ${MTS_ROOT} ]; then
+    # assume we're in an extracted mts install
+    MTS_ROOT="$(dirname $0)/../.."
+fi;
+
+JAR_DIR=${MTS_ROOT}/android-mts/tools
+
+TRADEFED_JAR="tradefed"
+
+JARS="tradefed
+  hosttestlib
+  compatibility-host-util
+  mts-tradefed
+  mts-tradefed-tests"
+
+for JAR in $JARS; do
+    checkFile ${JAR_DIR}/${JAR}.jar
+    JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
+done
+
+OPTIONAL_JARS="
+  google-tradefed
+  google-tradefed-tests
+  google-tf-prod-tests"
+
+for JAR in $OPTIONAL_JARS; do
+    if [ -f "$JAR.jar" ]; then
+        JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
+    fi;
+done
+
+# load any shared libraries for host-side executables
+LIB_DIR=${MTS_ROOT}/android-mts/lib
+if [ "$HOST" == "Linux" ]; then
+    LD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${LD_LIBRARY_PATH}
+    export LD_LIBRARY_PATH
+elif [ "$HOST" == "Darwin" ]; then
+    DYLD_LIBRARY_PATH=${LIB_DIR}:${LIB_DIR}64:${DYLD_LIBRARY_PATH}
+    export DYLD_LIBRARY_PATH
+fi
+
+# include any host-side test jars
+for j in ${MTS_ROOT}/android-mts/testcases/*.jar; do
+    JAR_PATH=${JAR_PATH}:$j
+done
+
+java $RDBG_FLAG -cp ${JAR_PATH} -DATS_ROOT=${MTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
+
+
diff --git a/tools/mts-tradefed/res/config/mts-dev.xml b/tools/mts-tradefed/res/config/mts-dev.xml
new file mode 100644
index 0000000..5e2de8d
--- /dev/null
+++ b/tools/mts-tradefed/res/config/mts-dev.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs MTS with common options set developer workflow: skips most checks">
+
+    <include name="mts" />
+
+    <option name="log-level" value="verbose" />
+    <option name="skip-preconditions" value="true" />
+
+    <option name="plan" value="mts-dev" />
+    <option name="compatibility:skip-all-system-status-check" value="true" />
+    <option name="compatibility:primary-abi-only" value="true" />
+
+</configuration>
+
diff --git a/tools/mts-tradefed/res/config/mts-unit-tests.xml b/tools/mts-tradefed/res/config/mts-unit-tests.xml
new file mode 100644
index 0000000..1b21102
--- /dev/null
+++ b/tools/mts-tradefed/res/config/mts-unit-tests.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Executes the MTS unit tests">
+    <option name="null-device" value="true" />
+    <build_provider class="com.android.tradefed.build.StubBuildProvider" />
+    <test class="com.android.tradefed.testtype.HostTest" >
+        <option name="class" value="com.android.compatibility.tradefed.MtsTradefedTest" />
+    </test>
+    <logger class="com.android.tradefed.log.FileLogger" />
+
+    <result_reporter class="com.android.tradefed.result.ConsoleResultReporter">
+        <option name="suppress-passed-tests" value="true" />
+    </result_reporter>
+    <template-include name="reporters" default="empty" />
+</configuration>
+
diff --git a/tools/mts-tradefed/res/config/mts.xml b/tools/mts-tradefed/res/config/mts.xml
new file mode 100644
index 0000000..ad6e8fe
--- /dev/null
+++ b/tools/mts-tradefed/res/config/mts.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project.
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs MTS">
+
+    <include name="everything" />
+    <option name="plan" value="mts" />
+    <option name="test-tag" value="mts" />
+
+    <option name="enable-root" value="false" />
+
+    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
+        <option name="run-command" value="settings put global package_verifier_enable 0" />
+        <option name="teardown-command" value="settings put global package_verifier_enable 1"/>
+    </target_preparer>
+
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.PropertyCheck">
+        <option name="property-name" value="ro.build.type" />
+        <option name="expected-value" value="user"/> <!-- Device should have user build -->
+        <option name="throw-error" value="false"/> <!-- Only print warning if not user build -->
+    </target_preparer>
+
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.PropertyCheck">
+        <option name="property-name" value="ro.product.locale" />
+        <option name="expected-value" value="en-US"/> <!-- Device locale should be US English -->
+        <option name="throw-error" value="false"/> <!-- Only print warning if not en-US -->
+    </target_preparer>
+
+    <template-include name="reporters" default="basic-reporters" />
+</configuration>
+
diff --git a/tools/mts-tradefed/tests/Android.mk b/tools/mts-tradefed/tests/Android.mk
new file mode 100644
index 0000000..748565e
--- /dev/null
+++ b/tools/mts-tradefed/tests/Android.mk
@@ -0,0 +1,27 @@
+#
+# Copyright (C) 2019 The Android Open Source Project.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_MODULE := mts-tradefed-tests
+LOCAL_MODULE_TAGS := optional
+LOCAL_JAVA_LIBRARIES := tradefed mts-tradefed
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
diff --git a/tools/mts-tradefed/tests/src/com/android/compatibility/tradefed/MtsTradefedTest.java b/tools/mts-tradefed/tests/src/com/android/compatibility/tradefed/MtsTradefedTest.java
new file mode 100644
index 0000000..4804fc2
--- /dev/null
+++ b/tools/mts-tradefed/tests/src/com/android/compatibility/tradefed/MtsTradefedTest.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.compatibility.tradefed;
+
+import static org.junit.Assert.*;
+
+import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
+import com.android.compatibility.common.tradefed.build.CompatibilityBuildProvider;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.util.FileUtil;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.File;
+
+/**
+ * Tests for mts-tradefed.
+ */
+@RunWith(JUnit4.class)
+public class MtsTradefedTest {
+
+    private static final String PROPERTY_NAME = "MTS_ROOT";
+    private static final String SUITE_FULL_NAME = "Mainline Test Suite";
+    private static final String SUITE_NAME = "MTS";
+
+    @Test
+    public void testSuiteInfoLoad() throws Exception {
+        // Test the values in the manifest can be loaded
+        File root = FileUtil.createTempDir("root");
+        System.setProperty(PROPERTY_NAME, root.getAbsolutePath());
+        File base = new File(root, "android-mts");
+        base.mkdirs();
+        File tests = new File(base, "testcases");
+        tests.mkdirs();
+        CompatibilityBuildProvider provider = new CompatibilityBuildProvider() {
+            @Override
+            protected String getSuiteInfoName() {
+                return SUITE_NAME;
+            }
+            @Override
+            protected String getSuiteInfoFullname() {
+                return SUITE_FULL_NAME;
+            }
+        };
+        IBuildInfo info = provider.getBuild();
+        CompatibilityBuildHelper helper = new CompatibilityBuildHelper(info);
+        assertEquals("Incorrect suite full name", SUITE_FULL_NAME, helper.getSuiteFullName());
+        assertEquals("Incorrect suite name", SUITE_NAME, helper.getSuiteName());
+        FileUtil.recursiveDelete(root);
+        System.clearProperty(PROPERTY_NAME);
+    }
+}
+