Merge "Teach acloud how to start FVP on GCE."
diff --git a/OWNERS b/OWNERS
index 1449168..c2158b3 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,2 +1,3 @@
+herbertxue@google.com
 kevcheng@google.com
 samchiu@google.com
diff --git a/create/create.py b/create/create.py
index 2c2dbee..711134f 100644
--- a/create/create.py
+++ b/create/create.py
@@ -169,6 +169,7 @@
     args.host = False
     args.host_base = False
     args.force = False
+    args.update_config = None
     # Remote image/instance requires the GCP config setup.
     if not args.local_instance or args.local_image == "":
         gcp_setup = gcp_setup_runner.GcpTaskRunner(args.config_file)
diff --git a/create/local_image_remote_instance.py b/create/local_image_remote_instance.py
index 3ad6a1f..811ebcf 100644
--- a/create/local_image_remote_instance.py
+++ b/create/local_image_remote_instance.py
@@ -62,7 +62,8 @@
             boot_timeout_secs=avd_spec.boot_timeout_secs,
             unlock_screen=avd_spec.unlock_screen,
             wait_for_boot=False,
-            connect_webrtc=avd_spec.connect_webrtc)
+            connect_webrtc=avd_spec.connect_webrtc,
+            client_adb_port=avd_spec.client_adb_port)
         # Launch vnc client if we're auto-connecting.
         if avd_spec.connect_vnc:
             utils.LaunchVNCFromReport(report, avd_spec, no_prompts)
diff --git a/create/remote_image_remote_instance.py b/create/remote_image_remote_instance.py
index 50bf99a..8044041 100644
--- a/create/remote_image_remote_instance.py
+++ b/create/remote_image_remote_instance.py
@@ -50,7 +50,8 @@
             boot_timeout_secs=avd_spec.boot_timeout_secs,
             unlock_screen=avd_spec.unlock_screen,
             wait_for_boot=False,
-            connect_webrtc=avd_spec.connect_webrtc)
+            connect_webrtc=avd_spec.connect_webrtc,
+            client_adb_port=avd_spec.client_adb_port)
         # Launch vnc client if we're auto-connecting.
         if avd_spec.connect_vnc:
             utils.LaunchVNCFromReport(report, avd_spec, no_prompts)
diff --git a/errors.py b/errors.py
index 6dcd83e..662dbdc 100644
--- a/errors.py
+++ b/errors.py
@@ -131,6 +131,10 @@
     """Error related to user using a not supported os."""
 
 
+class NotSupportedFieldName(SetupError):
+    """Unsupported field name for user config."""
+
+
 class CreateError(Exception):
     """Base Create cmd exception."""
 
diff --git a/internal/lib/cvd_runtime_config_test.py b/internal/lib/cvd_runtime_config_test.py
index f540a88..c201f16 100644
--- a/internal/lib/cvd_runtime_config_test.py
+++ b/internal/lib/cvd_runtime_config_test.py
@@ -16,6 +16,7 @@
 """Tests for cvd_runtime_config class."""
 
 import os
+import unittest
 import mock
 import six
 
@@ -31,7 +32,7 @@
  "x_res" : 720,
  "y_res" : 1280,
  "instances": {
-   "1":{
+   "2":{
        "adb_ip_and_port": "127.0.0.1:6520",
        "host_port": 6520,
        "instance_dir": "/path-to-instance-dir",
@@ -41,7 +42,33 @@
 }
 """
 
-    # pylint: disable=protected-access
+    CF_RUNTIME_CONFIG_WEBRTC = """
+{"x_display" : ":20",
+ "x_res" : 720,
+ "y_res" : 1280,
+ "dpi" : 320,
+ "instances" : {
+   "1":{
+       "adb_ip_and_port": "127.0.0.1:6520",
+       "host_port": 6520,
+       "instance_dir": "/path-to-instance-dir",
+       "vnc_server_port": 6444,
+       "virtual_disk_paths": ["/path-to-image"]
+   }
+ },
+ "enable_webrtc" : true,
+ "vnc_server_binary" : "/home/vsoc-01/bin/vnc_server",
+ "adb_connector_binary" : "/home/vsoc-01/bin/adb_connector",
+ "webrtc_assets_dir" : "/home/vsoc-01/usr/share/webrtc/assets",
+ "webrtc_binary" : "/home/vsoc-01/bin/webRTC",
+ "webrtc_certs_dir" : "/home/vsoc-01/usr/share/webrtc/certs",
+ "webrtc_enable_adb_websocket" : false,
+ "webrtc_public_ip" : "127.0.0.1"
+}
+"""
+
+
+    # pylint: disable=protected-access, no-member
     def testGetCuttlefishRuntimeConfig(self):
         """Test GetCuttlefishRuntimeConfig."""
         # Should raise error when file does not exist.
@@ -52,7 +79,7 @@
                          u'x_res': 720,
                          u'x_display': u':20',
                          u'instances':
-                             {u'1':
+                             {u'2':
                                   {u'adb_ip_and_port': u'127.0.0.1:6520',
                                    u'host_port': 6520,
                                    u'instance_dir': u'/path-to-instance-dir',
@@ -60,7 +87,51 @@
                              },
                         }
         mock_open = mock.mock_open(read_data=self.CF_RUNTIME_CONFIG)
-        cf_cfg_path = "/fake-path/local-instance-1/fake.config"
+        cf_cfg_path = "/fake-path/local-instance-2/fake.config"
         with mock.patch.object(six.moves.builtins, "open", mock_open):
-            self.assertEqual(expected_dict,
-                             cf_cfg.CvdRuntimeConfig(cf_cfg_path)._config_dict)
+            fake_cvd_runtime_config = cf_cfg.CvdRuntimeConfig(cf_cfg_path)
+            self.assertEqual(fake_cvd_runtime_config._config_dict, expected_dict)
+            self.assertEqual(fake_cvd_runtime_config.enable_webrtc, None)
+            self.assertEqual(fake_cvd_runtime_config.config_path,
+                             "/fake-path/local-instance-2/fake.config")
+            self.assertEqual(fake_cvd_runtime_config.instance_id, "2")
+
+        # Test read runtime config from raw_data and webrtc AVD.
+        self.Patch(cf_cfg, "_GetIdFromInstanceDirStr")
+        fake_cvd_runtime_config_webrtc = cf_cfg.CvdRuntimeConfig(
+            raw_data=self.CF_RUNTIME_CONFIG_WEBRTC)
+        cf_cfg._GetIdFromInstanceDirStr.assert_not_called()
+        self.assertEqual(fake_cvd_runtime_config_webrtc.config_path, None)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_id, "1")
+        self.assertEqual(fake_cvd_runtime_config_webrtc.enable_webrtc, True)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.x_res, 720)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.y_res, 1280)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.dpi, 320)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_ip_port, "127.0.0.1:6520")
+        self.assertEqual(fake_cvd_runtime_config_webrtc.instance_dir, "/path-to-instance-dir")
+        self.assertEqual(fake_cvd_runtime_config_webrtc.vnc_port, 6444)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.adb_port, 6520)
+        self.assertEqual(fake_cvd_runtime_config_webrtc.virtual_disk_paths, ['/path-to-image'])
+        self.assertEqual(fake_cvd_runtime_config_webrtc.cvd_tools_path, "/home/vsoc-01/bin")
+
+
+class CvdRuntimeconfigFunctionTest(driver_test_lib.BaseDriverTest):
+    """Test CvdRuntimeconfigFunctionTest class."""
+
+    # pylint: disable=protected-access
+    def testGetIdFromInstanceDirStr(self):
+        """Test GetIdFromInstanceDirStr."""
+        fake_instance_dir = "/path-to-instance-dir"
+        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), None)
+
+        fake_instance_dir = "/fake-path/local-instance-1/"
+        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), "1")
+
+        fake_home_path = "/home/fake_user/"
+        self.Patch(os.path, 'expanduser', return_value=fake_home_path)
+        fake_instance_dir = "/home/fake_user/local-instance/"
+        self.assertEqual(cf_cfg._GetIdFromInstanceDirStr(fake_instance_dir), "1")
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/public/acloud_main.py b/public/acloud_main.py
index b2d19da..0feacf8 100644
--- a/public/acloud_main.py
+++ b/public/acloud_main.py
@@ -259,6 +259,8 @@
     """
     if parsed_args.which == create_args.CMD_CREATE:
         create_args.VerifyArgs(parsed_args)
+    if parsed_args.which == setup_args.CMD_SETUP:
+        setup_args.VerifyArgs(parsed_args)
     if parsed_args.which == CMD_CREATE_CUTTLEFISH:
         if not parsed_args.build_id and not parsed_args.branch:
             raise errors.CommandArgError(
diff --git a/public/actions/common_operations_test.py b/public/actions/common_operations_test.py
index 1226b4b..82da37c 100644
--- a/public/actions/common_operations_test.py
+++ b/public/actions/common_operations_test.py
@@ -18,6 +18,7 @@
 from __future__ import absolute_import
 from __future__ import division
 
+import shlex
 import unittest
 import mock
 
@@ -25,6 +26,7 @@
 from acloud.internal.lib import android_compute_client
 from acloud.internal.lib import auth
 from acloud.internal.lib import driver_test_lib
+from acloud.internal.lib import utils
 from acloud.internal.lib import ssh
 from acloud.public import report
 from acloud.public.actions import common_operations
@@ -113,6 +115,32 @@
                 "gcs_bucket_build_id": self.BUILD_ID,
             }]})
 
+    def testCreateDevicesWithAdbPort(self):
+        """Test Create Devices with adb port for cuttlefish avd type."""
+        self.Patch(utils, "_ExecuteCommand")
+        self.Patch(utils, "PickFreePort", return_value=56789)
+        self.Patch(shlex, "split", return_value=[])
+        cfg = self._CreateCfg()
+        _report = common_operations.CreateDevices(self.CMD, cfg,
+                                                  self.device_factory, 1,
+                                                  "cuttlefish",
+                                                  autoconnect=True,
+                                                  client_adb_port=12345)
+        self.assertEqual(_report.command, self.CMD)
+        self.assertEqual(_report.status, report.Status.SUCCESS)
+        self.assertEqual(
+            _report.data,
+            {"devices": [{
+                "ip": self.IP.external,
+                "instance_name": self.INSTANCE,
+                "branch": self.BRANCH,
+                "build_id": self.BUILD_ID,
+                "adb_port": 12345,
+                "vnc_port": 56789,
+                "build_target": self.BUILD_TARGET,
+                "gcs_bucket_build_id": self.BUILD_ID,
+            }]})
+
     def testCreateDevicesInternalIP(self):
         """Test Create Devices and report internal IP."""
         cfg = self._CreateCfg()
diff --git a/run_tests.sh b/run_tests.sh
index 20c430c..c1be9ab 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -78,8 +78,12 @@
 }
 
 function check_env() {
-    if [ -z "$ANDROID_BUILD_TOP" ]; then
-        echo "Missing ANDROID_BUILD_TOP env variable. Run 'lunch' first."
+    if [ -z "$ANDROID_HOST_OUT" ]; then
+        echo "Missing ANDROID_HOST_OUT env variable. Run 'lunch' first."
+        exit 1
+    fi
+    if [ ! -f "$ANDROID_HOST_OUT/bin/aprotoc" ]; then
+        echo "Missing aprotoc. Run 'm aprotoc' first."
         exit 1
     fi
 
@@ -98,7 +102,7 @@
 
 function gen_proto_py() {
     # Use aprotoc to generate python proto files.
-    local protoc_cmd=$ANDROID_BUILD_TOP/prebuilts/misc/linux-x86/protobuf/aprotoc
+    local protoc_cmd=$ANDROID_HOST_OUT/bin/aprotoc
     pushd $ACLOUD_DIR &> /dev/null
     $protoc_cmd internal/proto/*.proto --python_out=./
     touch internal/proto/__init__.py
diff --git a/setup/setup_args.py b/setup/setup_args.py
index 4190223..1e3583c 100644
--- a/setup/setup_args.py
+++ b/setup/setup_args.py
@@ -18,6 +18,13 @@
 Defines the setup arg parser that holds setup specific args.
 """
 
+from acloud import errors
+# pylint: disable=no-name-in-module,import-error
+from acloud.internal.proto.user_config_pb2 import UserConfig
+
+
+_FIELD_NAMES = sorted([field.name for field in UserConfig.DESCRIPTOR.fields])
+_CONFIG_FIELD = 0
 CMD_SETUP = "setup"
 
 
@@ -68,6 +75,26 @@
         required=False,
         help="Update the acloud user config. The first arg is field name in "
         "config, and the second arg is the value of the field. Command would "
-        "like: 'acloud setup --config stable_host_image_family acloud-release'")
+        "like: 'acloud setup --config stable_host_image_family acloud-release'."
+        " The first arg can be one of following fields:%s" % _FIELD_NAMES)
 
     return setup_parser
+
+
+def VerifyArgs(args):
+    """Verify args.
+
+    One example of command "acloud setup --update-config zone us-central1-c",
+    then this function would check "zone" is a valid field.
+
+    Args:
+        args: Namespace object from argparse.parse_args.
+
+    Raises:
+        errors.NotSupportedFieldName: The field name doesn't support in config.
+    """
+    if args.update_config:
+        if args.update_config[_CONFIG_FIELD] not in _FIELD_NAMES:
+            raise errors.NotSupportedFieldName(
+                "Field[%s] isn't in support list: %s" % (args.update_config[0],
+                                                         _FIELD_NAMES))