From ac4f581f4b740d652d79fa0556d1f533c3952b3b Mon Sep 17 00:00:00 2001
From: Metin Kaya <metin.kaya@arm.com>
Date: Fri, 9 Feb 2024 15:48:33 +0000
Subject: [PATCH] target: tests: Add support for testing ChromeOS targets

We can mimic ChromeOS target by combining a QEMU guest (for Linux
bindings of ``ChromeOsTarget`` class) with a Android virtual desktop
(for Android bits of ``ChromeOsTarget``).

Note that Android bindings of ``ChromeOsTarget`` class also requires
existence of ``/opt/google/containers/android`` folder on the Linux
guest.

Signed-off-by: Metin Kaya <metin.kaya@arm.com>
---
 tests/target_configs.yaml.example |  8 ++++++++
 tests/test_target.py              | 31 +++++++++++++++++++++++++++++--
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/tests/target_configs.yaml.example b/tests/target_configs.yaml.example
index 6ad859a..a47acea 100644
--- a/tests/target_configs.yaml.example
+++ b/tests/target_configs.yaml.example
@@ -3,6 +3,14 @@ AndroidTarget:
     connection_settings:
       device: 'emulator-5554'
 
+ChromeOsTarget:
+  entry-0:
+    connection_settings:
+      device: 'emulator-5556'
+      host: 'example.com'
+      username: 'username'
+      password: 'password'
+
 LinuxTarget:
   entry-0:
     connection_settings:
diff --git a/tests/test_target.py b/tests/test_target.py
index 63f806f..05f5685 100644
--- a/tests/test_target.py
+++ b/tests/test_target.py
@@ -20,7 +20,7 @@ import os
 from pprint import pp
 import pytest
 
-from devlib import AndroidTarget, LinuxTarget, LocalLinuxTarget, QEMUTargetRunner
+from devlib import AndroidTarget, ChromeOsTarget, LinuxTarget, LocalLinuxTarget, QEMUTargetRunner
 from devlib.utils.android import AdbConnection
 from devlib.utils.misc import load_struct_from_yaml
 
@@ -53,6 +53,16 @@ def build_targets():
             l_target = LinuxTarget(connection_settings=entry['connection_settings'])
             targets.append((l_target, None))
 
+    if target_configs.get('ChromeOsTarget') is not None:
+        print('> ChromeOS targets:')
+        for entry in target_configs['ChromeOsTarget'].values():
+            pp(entry)
+            c_target = ChromeOsTarget(
+                connection_settings=entry['connection_settings'],
+                working_directory='/tmp/devlib-target',
+            )
+            targets.append((c_target, None))
+
     if target_configs.get('LocalLinuxTarget') is not None:
         print('> LocalLinux targets:')
         for entry in target_configs['LocalLinuxTarget'].values():
@@ -72,7 +82,24 @@ def build_targets():
                 qemu_settings=qemu_settings,
                 connection_settings=connection_settings,
             )
-            targets.append((qemu_runner.target, qemu_runner))
+
+            if entry.get('ChromeOsTarget') is None:
+                targets.append((qemu_runner.target, qemu_runner))
+                continue
+
+            # Leave termination of QEMU runner to ChromeOS target.
+            targets.append((qemu_runner.target, None))
+
+            print('> ChromeOS targets:')
+            pp(entry['ChromeOsTarget'])
+            c_target = ChromeOsTarget(
+                connection_settings={
+                    **entry['ChromeOsTarget']['connection_settings'],
+                    **qemu_runner.target.connection_settings,
+                },
+                working_directory='/tmp/devlib-target',
+            )
+            targets.append((c_target, qemu_runner))
 
     return targets