ninjalog_uploader_test.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2018 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. import http
  6. import json
  7. import os
  8. import sys
  9. import unittest
  10. import unittest.mock
  11. import urllib.error
  12. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  13. sys.path.insert(0, ROOT_DIR)
  14. import ninjalog_uploader
  15. class NinjalogUploaderTest(unittest.TestCase):
  16. def test_parse_gn_args(self):
  17. gn_args, explicit_keys = ninjalog_uploader.ParseGNArgs(json.dumps([]))
  18. self.assertEqual(gn_args, {})
  19. self.assertEqual(explicit_keys, [])
  20. # Extract current configs from GN's output json.
  21. gn_args, explicit_keys = ninjalog_uploader.ParseGNArgs(
  22. json.dumps([
  23. {
  24. 'current': {
  25. 'value': 'true',
  26. 'file': '//path/to/args.gn',
  27. },
  28. 'default': {
  29. 'value': 'false'
  30. },
  31. 'name': 'is_component_build'
  32. },
  33. {
  34. 'default': {
  35. 'value': '"x64"'
  36. },
  37. 'name': 'host_cpu'
  38. },
  39. ]))
  40. self.assertEqual(gn_args, {
  41. 'is_component_build': 'true',
  42. 'host_cpu': 'x64',
  43. })
  44. self.assertEqual(explicit_keys, ['is_component_build'])
  45. gn_args, explicit_keys = ninjalog_uploader.ParseGNArgs(
  46. json.dumps([
  47. {
  48. 'current': {
  49. 'value': 'true',
  50. 'file': '//.gn',
  51. },
  52. 'default': {
  53. 'value': 'false'
  54. },
  55. 'name': 'is_component_build'
  56. },
  57. {
  58. 'current': {
  59. 'value': 'false',
  60. 'file': '//path/to/args.gn',
  61. },
  62. 'default': {
  63. 'value': 'false'
  64. },
  65. 'name': 'use_remoteexec'
  66. },
  67. ]))
  68. self.assertEqual(gn_args, {
  69. 'is_component_build': 'true',
  70. 'use_remoteexec': 'false'
  71. })
  72. self.assertEqual(explicit_keys, ['use_remoteexec'])
  73. # Do not include sensitive information.
  74. with unittest.mock.patch('getpass.getuser', return_value='bob'):
  75. gn_args, explicit_keys = ninjalog_uploader.ParseGNArgs(
  76. json.dumps([
  77. {
  78. 'current': {
  79. 'value': 'xyz',
  80. 'file': '//path/to/args.gn',
  81. },
  82. 'default': {
  83. 'value': ''
  84. },
  85. 'name': 'google_api_key'
  86. },
  87. {
  88. 'current': {
  89. 'value': '/home/bob/bobo',
  90. 'file': '//path/to/args.gn',
  91. },
  92. 'default': {
  93. 'value': ''
  94. },
  95. 'name': 'path_with_homedir'
  96. },
  97. ]))
  98. self.assertEqual(
  99. gn_args, {
  100. 'google_api_key': '<omitted>',
  101. 'path_with_homedir': '/home/$USER/bobo',
  102. })
  103. def test_get_ninjalog(self):
  104. # No args => default to cwd.
  105. self.assertEqual(ninjalog_uploader.GetNinjalog(['ninja']),
  106. './.ninja_log')
  107. # Specified by -C case.
  108. self.assertEqual(
  109. ninjalog_uploader.GetNinjalog(['ninja', '-C', 'out/Release']),
  110. 'out/Release/.ninja_log')
  111. self.assertEqual(
  112. ninjalog_uploader.GetNinjalog(['ninja', '-Cout/Release']),
  113. 'out/Release/.ninja_log')
  114. # Invalid -C flag case.
  115. self.assertEqual(ninjalog_uploader.GetNinjalog(['ninja', '-C']),
  116. './.ninja_log')
  117. # Multiple target directories => use the last directory.
  118. self.assertEqual(
  119. ninjalog_uploader.GetNinjalog(
  120. ['ninja', '-C', 'out/Release', '-C', 'out/Debug']),
  121. 'out/Debug/.ninja_log')
  122. def test_get_build_target_from_command_line(self):
  123. self.assertEqual(
  124. ninjalog_uploader.GetBuildTargetFromCommandLine(
  125. ['python3', 'ninja.py', 'chrome']), ['chrome'])
  126. self.assertEqual(
  127. ninjalog_uploader.GetBuildTargetFromCommandLine(
  128. ['python3', 'ninja.py']), [])
  129. self.assertEqual(
  130. ninjalog_uploader.GetBuildTargetFromCommandLine(
  131. ['python3', 'ninja.py', '-j', '1000', 'chrome']), ['chrome'])
  132. self.assertEqual(
  133. ninjalog_uploader.GetBuildTargetFromCommandLine(
  134. ['python3', 'ninja.py', 'chrome', '-j', '1000']), ['chrome'])
  135. self.assertEqual(
  136. ninjalog_uploader.GetBuildTargetFromCommandLine(
  137. ['python3', 'ninja.py', '-C', 'chrome']), [])
  138. self.assertEqual(
  139. ninjalog_uploader.GetBuildTargetFromCommandLine(
  140. ['python3', 'ninja.py', '-Cout/Release', 'chrome']), ['chrome'])
  141. self.assertEqual(
  142. ninjalog_uploader.GetBuildTargetFromCommandLine(
  143. ['python3', 'ninja.py', '-C', 'out/Release', 'chrome', 'all']),
  144. ['chrome', 'all'])
  145. @unittest.skipIf(sys.platform == 'win32', 'posix path test')
  146. def test_get_build_target_from_command_line_filter_posix(self):
  147. self.assertEqual(
  148. ninjalog_uploader.GetBuildTargetFromCommandLine([
  149. 'python3', 'ninja.py', '-C', 'out/Release', 'chrome', 'all',
  150. '/path/to/foo', '-p'
  151. ]), ['chrome', 'all'])
  152. @unittest.skipUnless(sys.platform == 'win32', 'Windows path test')
  153. def test_get_build_target_from_command_line_filter_win(self):
  154. self.assertEqual(
  155. ninjalog_uploader.GetBuildTargetFromCommandLine([
  156. 'python3', 'ninja.py', '-C', 'out/Release', 'chrome', 'all',
  157. 'C:\\path\\to\\foo', '-p'
  158. ]), ['chrome', 'all'])
  159. def test_get_j_flag(self):
  160. self.assertEqual(ninjalog_uploader.GetJflag(['ninja']), None)
  161. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-j', '1000']),
  162. 1000)
  163. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-j', '1000a']),
  164. None)
  165. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-j', 'a']), None)
  166. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-j1000']), 1000)
  167. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-ja']), None)
  168. self.assertEqual(ninjalog_uploader.GetJflag(['ninja', '-j']), None)
  169. def test_get_gce_metadata(self):
  170. with unittest.mock.patch('urllib.request.urlopen') as urlopen_mock:
  171. urlopen_mock.side_effect = urllib.error.HTTPError(
  172. 'http://test/not-found', http.HTTPStatus.NOT_FOUND, 'not found',
  173. None, None)
  174. self.assertEqual(ninjalog_uploader.GetGCEMetadata(), {})
  175. with unittest.mock.patch(
  176. 'ninjalog_uploader._getGCEInfo') as getGCEInfo_mock:
  177. getGCEInfo_mock.return_value = {
  178. "instance": {
  179. "machineType":
  180. "projects/12345/machineTypes/n2d-standard-128",
  181. },
  182. "project": {
  183. "projectId": "cloudtop-test"
  184. }
  185. }
  186. self.assertEqual(ninjalog_uploader.GetGCEMetadata(), {
  187. 'gce_machine_type': 'n2d-standard-128',
  188. 'is_cloudtop': True,
  189. })
  190. with unittest.mock.patch(
  191. 'ninjalog_uploader._getGCEInfo') as getGCEInfo_mock:
  192. getGCEInfo_mock.return_value = {
  193. "instance": {
  194. "machineType":
  195. "projects/12345/machineTypes/n2d-standard-128",
  196. },
  197. "project": {
  198. "projectId": "gce-project"
  199. }
  200. }
  201. self.assertEqual(ninjalog_uploader.GetGCEMetadata(), {
  202. 'gce_machine_type': 'n2d-standard-128',
  203. })
  204. if __name__ == '__main__':
  205. unittest.main()