How to use insert_patch method in autotest

Best Python code snippet using autotest_python

binary_deps_manager_unittest.py

Source:binary_deps_manager_unittest.py Github

copy

Full Screen

1# Copyright 2020 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import json5import os6import shutil7import tempfile8import unittest9from core.perfetto_binary_roller import binary_deps_manager10import mock11class BinaryDepsManagerTests(unittest.TestCase):12 def setUp(self):13 self.temp_dir = tempfile.mkdtemp()14 self.config_path = os.path.join(self.temp_dir, 'config.json')15 self.original_config_path = binary_deps_manager.CONFIG_PATH16 binary_deps_manager.CONFIG_PATH = self.config_path17 def tearDown(self):18 binary_deps_manager.CONFIG_PATH = self.original_config_path19 shutil.rmtree(self.temp_dir)20 def writeConfig(self, config):21 with open(self.config_path, 'w') as f:22 json.dump(config, f)23 def readConfig(self):24 with open(self.config_path) as f:25 return json.load(f)26 def testUploadHostBinary(self):27 with mock.patch('py_utils.cloud_storage.Exists') as exists_patch:28 with mock.patch('py_utils.cloud_storage.Insert') as insert_patch:29 with mock.patch('py_utils.GetHostOsName') as get_os_patch:30 exists_patch.return_value = False31 get_os_patch.return_value = 'testos'32 binary_deps_manager.UploadHostBinary('dep', '/path/to/bin', 'abc123')33 insert_patch.assert_has_calls([34 mock.call(35 'chromium-telemetry',36 'perfetto_binaries/dep/testos/abc123/bin',37 '/path/to/bin',38 publicly_readable=True),39 mock.call(40 'chromium-telemetry',41 'perfetto_binaries/dep/testos/latest',42 mock.ANY,43 publicly_readable=True),44 ])45 def testUploadHostBinaryExists(self):46 with mock.patch('py_utils.cloud_storage.Exists') as exists_patch:47 with mock.patch('py_utils.cloud_storage.Insert') as insert_patch:48 with mock.patch('py_utils.GetHostOsName') as get_os_patch:49 exists_patch.return_value = True50 get_os_patch.return_value = 'testos'51 binary_deps_manager.UploadHostBinary('dep', '/path/to/bin', 'abc123')52 insert_patch.assert_called_once_with(53 'chromium-telemetry',54 'perfetto_binaries/dep/testos/latest',55 mock.ANY,56 publicly_readable=True,57 )58 def testSwitchBinaryToNewPath(self):59 self.writeConfig({'dep': {'testos': {'remote_path': 'old/path/to/bin'}}})60 latest_path = 'new/path/to/bin'61 def write_latest_path(bucket, remote_path, local_path):62 del bucket, remote_path # unused63 with open(local_path, 'w') as f:64 f.write(latest_path)65 with mock.patch('py_utils.cloud_storage.Get') as get_patch:66 with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:67 get_patch.side_effect = write_latest_path68 hash_patch.return_value = '123'69 binary_deps_manager.SwitchBinaryToNewPath('dep', 'testos', latest_path)70 self.assertEqual(71 self.readConfig(),72 {'dep': {73 'testos': {74 'remote_path': latest_path,75 'hash': '123',76 }77 }})78 def testFetchHostBinary(self):79 remote_path = 'remote/path/to/bin'80 self.writeConfig({81 'dep': {82 'testos': {83 'remote_path': remote_path,84 'hash': '123',85 }86 }87 })88 with mock.patch('py_utils.cloud_storage.Get') as get_patch:89 with mock.patch('py_utils.GetHostOsName') as get_os_patch:90 with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:91 with mock.patch('os.stat'):92 with mock.patch('os.chmod'):93 hash_patch.return_value = '123'94 get_os_patch.return_value = 'testos'95 local_path = binary_deps_manager.FetchHostBinary('dep')96 self.assertEqual(os.path.basename(local_path), 'bin')97 get_patch.assert_called_once_with('chromium-telemetry', remote_path,98 local_path)99 def testFetchHostBinaryWrongHash(self):100 remote_path = 'remote/path/to/bin'101 self.writeConfig({102 'dep': {103 'testos': {104 'remote_path': remote_path,105 'hash': '123',106 }107 }108 })109 with mock.patch('py_utils.cloud_storage.Get'):110 with mock.patch('py_utils.GetHostOsName') as get_os_patch:111 with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:112 hash_patch.return_value = '234'113 get_os_patch.return_value = 'testos'114 with self.assertRaises(RuntimeError):115 binary_deps_manager.FetchHostBinary('dep')116 def testUploadAndSwitchDataFile(self):117 self.writeConfig({'data_dep': {'remote_path': 'old/path/to/data'}})118 new_path = 'new/path/to/data'119 with mock.patch('py_utils.cloud_storage.Exists') as exists_patch:120 with mock.patch('py_utils.cloud_storage.Insert') as insert_patch:121 with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:122 exists_patch.return_value = False123 hash_patch.return_value = '123'124 binary_deps_manager.UploadAndSwitchDataFile('data_dep', new_path,125 'abc123')126 insert_patch.assert_called_once_with(127 'chrome-telemetry',128 'perfetto_data/data_dep/abc123/data',129 'new/path/to/data',130 publicly_readable=False,131 )132 self.assertEqual(133 self.readConfig(), {134 'data_dep': {135 'remote_path': 'perfetto_data/data_dep/abc123/data',136 'hash': '123',137 }138 })139 def testFetchDataFile(self):140 remote_path = 'remote/path/to/data'141 self.writeConfig(142 {'data_dep': {143 'remote_path': remote_path,144 'hash': '123',145 }})146 with mock.patch('py_utils.cloud_storage.Get') as get_patch:147 with mock.patch('py_utils.cloud_storage.CalculateHash') as hash_patch:148 hash_patch.return_value = '123'149 local_path = binary_deps_manager.FetchDataFile('data_dep')150 self.assertEqual(os.path.basename(local_path), 'data')151 get_patch.assert_called_once_with('chrome-telemetry', remote_path,...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful