summaryrefslogtreecommitdiff
path: root/chromium/build/fuchsia/fvdl_target_test.py
blob: 554e3b966006541b6a9c73436d7752bf1cf0b7b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env vpython3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests different flags to see if they are being used correctly"""

import boot_data
import common
import os
import tempfile
import unittest
import unittest.mock as mock

from argparse import Namespace
from ffx_session import FfxRunner
from fvdl_target import FvdlTarget, _SSH_KEY_DIR


@mock.patch.object(FfxRunner, 'daemon_stop')
class TestBuildCommandFvdlTarget(unittest.TestCase):
  def setUp(self):
    self.args = Namespace(out_dir='outdir',
                          system_log_file=None,
                          target_cpu='x64',
                          require_kvm=True,
                          enable_graphics=False,
                          hardware_gpu=False,
                          with_network=False,
                          ram_size_mb=8192,
                          logs_dir=None,
                          custom_image=None,
                          cpu_cores=10)
    common.EnsurePathExists = mock.MagicMock(return_value='image')
    boot_data.ProvisionSSH = mock.MagicMock()
    FvdlTarget._Shutdown = mock.MagicMock()

  def testBasicEmuCommand(self, mock_daemon_stop):
    with FvdlTarget.CreateFromArgs(self.args) as target:
      build_command = target._BuildCommand()
      self.assertIn(target._FVDL_PATH, build_command)
      self.assertIn('--sdk', build_command)
      self.assertIn('start', build_command)
      self.assertNotIn('--noacceleration', build_command)
      self.assertIn('--headless', build_command)
      self.assertNotIn('--host-gpu', build_command)
      self.assertNotIn('-N', build_command)
      self.assertIn('--device-proto', build_command)
      self.assertNotIn('--emulator-log', build_command)
      self.assertNotIn('--envs', build_command)
      self.assertTrue(os.path.exists(target._device_proto_file.name))
      correct_ram_amount = False
      with open(target._device_proto_file.name) as file:
        for line in file:
          if line.strip() == 'ram:  8192':
            correct_ram_amount = True
            break
      self.assertTrue(correct_ram_amount)
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckIfNotRequireKVMSetNoAcceleration(
      self, mock_daemon_stop):
    self.args.require_kvm = False
    with FvdlTarget.CreateFromArgs(self.args) as target:
      self.assertIn('--noacceleration', target._BuildCommand())
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckIfNotEnableGraphicsSetHeadless(
      self, mock_daemon_stop):
    self.args.enable_graphics = True
    with FvdlTarget.CreateFromArgs(self.args) as target:
      self.assertNotIn('--headless', target._BuildCommand())
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckIfHardwareGpuSetHostGPU(self, mock_daemon_stop):
    self.args.hardware_gpu = True
    with FvdlTarget.CreateFromArgs(self.args) as target:
      self.assertIn('--host-gpu', target._BuildCommand())
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckIfWithNetworkSetTunTap(self, mock_daemon_stop):
    self.args.with_network = True
    with FvdlTarget.CreateFromArgs(self.args) as target:
      self.assertIn('-N', target._BuildCommand())
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckRamSizeNot8192SetRamSize(self, mock_daemon_stop):
    custom_ram_size = 4096
    self.args.ram_size_mb = custom_ram_size
    with FvdlTarget.CreateFromArgs(self.args) as target:
      self.assertIn('--device-proto', target._BuildCommand())
      self.assertTrue(os.path.exists(target._device_proto_file.name))
      correct_ram_amount = False
      with open(target._device_proto_file.name, 'r') as f:
        self.assertTrue('  ram:  {}\n'.format(custom_ram_size) in f.readlines())
    mock_daemon_stop.assert_called_once()

  def testBuildCommandCheckEmulatorLogSetup(self, mock_daemon_stop):
    with tempfile.TemporaryDirectory() as logs_dir:
      self.args.logs_dir = logs_dir
      with FvdlTarget.CreateFromArgs(self.args) as target:
        build_command = target._BuildCommand()
        self.assertIn('--emulator-log', build_command)
        self.assertIn('--envs', build_command)
      mock_daemon_stop.assert_called_once()


if __name__ == '__main__':
  unittest.main()