Best Python code snippet using autotest_python
binary_deps_manager_unittest.py
Source: binary_deps_manager_unittest.py
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,...
Check out the latest blogs from LambdaTest on this topic:
How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.
Lack of training is something that creates a major roadblock for a tester. Often, testers working in an organization are all of a sudden forced to learn a new framework or an automation tool whenever a new project demands it. You may be overwhelmed on how to learn test automation, where to start from and how to master test automation for web applications, and mobile applications on a new technology so soon.
Before we discuss the Joomla testing, let us understand the fundamentals of Joomla and how this content management system allows you to create and maintain web-based applications or websites without having to write and implement complex coding requirements.
The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.
Hola Testers! Hope you all had a great Thanksgiving weekend! To make this time more memorable, we at LambdaTest have something to offer you as a token of appreciation.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!