fvdl_target_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env vpython3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests different flags to see if they are being used correctly"""
  6. import boot_data
  7. import common
  8. import os
  9. import tempfile
  10. import unittest
  11. import unittest.mock as mock
  12. from argparse import Namespace
  13. from ffx_session import FfxRunner
  14. from fvdl_target import FvdlTarget, _SSH_KEY_DIR
  15. @mock.patch.object(FfxRunner, 'daemon_stop')
  16. class TestBuildCommandFvdlTarget(unittest.TestCase):
  17. def setUp(self):
  18. self.args = Namespace(out_dir='outdir',
  19. system_log_file=None,
  20. target_cpu='x64',
  21. require_kvm=True,
  22. enable_graphics=False,
  23. hardware_gpu=False,
  24. with_network=False,
  25. ram_size_mb=8192,
  26. logs_dir=None,
  27. custom_image=None,
  28. cpu_cores=10)
  29. common.EnsurePathExists = mock.MagicMock(return_value='image')
  30. boot_data.ProvisionSSH = mock.MagicMock()
  31. FvdlTarget._Shutdown = mock.MagicMock()
  32. def testBasicEmuCommand(self, mock_daemon_stop):
  33. with FvdlTarget.CreateFromArgs(self.args) as target:
  34. build_command = target._BuildCommand()
  35. self.assertIn(target._FVDL_PATH, build_command)
  36. self.assertIn('--sdk', build_command)
  37. self.assertIn('start', build_command)
  38. self.assertNotIn('--noacceleration', build_command)
  39. self.assertIn('--headless', build_command)
  40. self.assertNotIn('--host-gpu', build_command)
  41. self.assertNotIn('-N', build_command)
  42. self.assertIn('--device-proto', build_command)
  43. self.assertNotIn('--emulator-log', build_command)
  44. self.assertNotIn('--envs', build_command)
  45. self.assertTrue(os.path.exists(target._device_proto_file.name))
  46. correct_ram_amount = False
  47. with open(target._device_proto_file.name) as file:
  48. for line in file:
  49. if line.strip() == 'ram: 8192':
  50. correct_ram_amount = True
  51. break
  52. self.assertTrue(correct_ram_amount)
  53. mock_daemon_stop.assert_called_once()
  54. def testBuildCommandCheckIfNotRequireKVMSetNoAcceleration(
  55. self, mock_daemon_stop):
  56. self.args.require_kvm = False
  57. with FvdlTarget.CreateFromArgs(self.args) as target:
  58. self.assertIn('--noacceleration', target._BuildCommand())
  59. mock_daemon_stop.assert_called_once()
  60. def testBuildCommandCheckIfNotEnableGraphicsSetHeadless(
  61. self, mock_daemon_stop):
  62. self.args.enable_graphics = True
  63. with FvdlTarget.CreateFromArgs(self.args) as target:
  64. self.assertNotIn('--headless', target._BuildCommand())
  65. mock_daemon_stop.assert_called_once()
  66. def testBuildCommandCheckIfHardwareGpuSetHostGPU(self, mock_daemon_stop):
  67. self.args.hardware_gpu = True
  68. with FvdlTarget.CreateFromArgs(self.args) as target:
  69. self.assertIn('--host-gpu', target._BuildCommand())
  70. mock_daemon_stop.assert_called_once()
  71. def testBuildCommandCheckIfWithNetworkSetTunTap(self, mock_daemon_stop):
  72. self.args.with_network = True
  73. with FvdlTarget.CreateFromArgs(self.args) as target:
  74. self.assertIn('-N', target._BuildCommand())
  75. mock_daemon_stop.assert_called_once()
  76. def testBuildCommandCheckRamSizeNot8192SetRamSize(self, mock_daemon_stop):
  77. custom_ram_size = 4096
  78. self.args.ram_size_mb = custom_ram_size
  79. with FvdlTarget.CreateFromArgs(self.args) as target:
  80. self.assertIn('--device-proto', target._BuildCommand())
  81. self.assertTrue(os.path.exists(target._device_proto_file.name))
  82. correct_ram_amount = False
  83. with open(target._device_proto_file.name, 'r') as f:
  84. self.assertTrue(' ram: {}\n'.format(custom_ram_size) in f.readlines())
  85. mock_daemon_stop.assert_called_once()
  86. def testBuildCommandCheckEmulatorLogSetup(self, mock_daemon_stop):
  87. with tempfile.TemporaryDirectory() as logs_dir:
  88. self.args.logs_dir = logs_dir
  89. with FvdlTarget.CreateFromArgs(self.args) as target:
  90. build_command = target._BuildCommand()
  91. self.assertIn('--emulator-log', build_command)
  92. self.assertIn('--envs', build_command)
  93. mock_daemon_stop.assert_called_once()
  94. if __name__ == '__main__':
  95. unittest.main()