def setUp(self): self.filesystem = fake_filesystem.FakeFilesystem() self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem) self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem) self.host1 = 'h1' self.port1 = 1 self.host2 = 'h2' self.port2 = 2 self.uplink_host = '127.0.0.1' self.uplink_port = 999 self.username = '******' self.password = '******' self.test1_name = 'Test1' self.test2_name = 'Test2' self.test2_version = 'testing' @googleads.common.RegisterUtility(self.test1_name) class Test1(object): def test(self): pass @googleads.common.RegisterUtility(self.test2_name, {'test': self.test2_version}) class Test2(object): def test(self): pass self.test1 = Test1 self.test2 = Test2
def setUp(self): super(FilesTest, self).setUp() self.filesystem = fake_filesystem.FakeFilesystem() files.open = fake_filesystem.FakeFileOpen(self.filesystem) files.file_util.shutil = fake_filesystem_shutil.FakeShutilModule( self.filesystem) files.WindowsError = Exception
def setUp(self, logs): super(BuildInfoTest, self).setUp() self.autobuild = autobuild.AutoBuild() autobuild.logging = logs.logging autobuild.logging.fatal.side_effect = LogFatalError() self.filesystem = fake_filesystem.FakeFilesystem() autobuild.os = fake_filesystem.FakeOsModule(self.filesystem)
def setUp(self): self._dl = download.BaseDownloader() # filesystem self.filesystem = fake_filesystem.FakeFilesystem() self.filesystem.CreateFile(r'C:\input.ini', contents=_TEST_INI) download.os = fake_filesystem.FakeOsModule(self.filesystem) download.open = fake_filesystem.FakeFileOpen(self.filesystem)
def setUp(self): self.filesystem = fake_filesystem.FakeFilesystem() self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem) self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem) self.uri1 = 'http://*****:*****@h1:1' self.uri2 = 'http://h2:2' self.uplink_uri = 'http://127.0.0.1:999' self.test1_name = 'Test1' self.test2_name = 'Test2' self.test2_version = 'testing' self.fake_version = 'ignored' locale_patcher = mock.patch('googleads.common.locale.getdefaultlocale', return_value=('en_us', 'UTF-8')) self.locale_patcher = locale_patcher.start() @googleads.common.RegisterUtility(self.test1_name) class Test1(object): def test(self): pass @googleads.common.RegisterUtility(self.test2_name, {'test': self.test2_version}) class Test2(object): def test(self): pass self.test1 = Test1 self.test2 = Test2
def setUp(self): self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') self.os = fake_filesystem.FakeOsModule(self.filesystem) self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem) self.orig_logging = fake_tempfile.logging self.fake_logging = FakeLogging(self) fake_tempfile.logging = self.fake_logging
async def setup_testcase(self): self.stubs = stubout.StubOutForTesting() self.addCleanup(self.stubs.SmartUnsetAll) # Setup fake filesystem. self.fs = fake_filesystem.FakeFilesystem() self.fake_os = fake_filesystem.FakeOsModule(self.fs) self.fake_open = fake_filesystem.FakeFileOpen(self.fs) self.stubs.SmartSet(storage, 'os', self.fake_os) self.stubs.SmartSet(fileutil, 'os', self.fake_os) self.stubs.SmartSet(project_registry, 'os', self.fake_os) self.stubs.SmartSet(shutil, 'os', self.fake_os) self.stubs.SmartSet(builtins, 'open', self.fake_open) self.music_dir = self.fake_os.path.expanduser('~/Music/Noisicaä') self.fake_os.makedirs(self.music_dir) storage.ProjectStorage.create( self.fake_os.path.join(self.music_dir, 'proj1')).close() storage.ProjectStorage.create( self.fake_os.path.join(self.music_dir, 'proj2')).close() storage.ProjectStorage.create( self.fake_os.path.join(self.music_dir, 'proj3')).close() self.__registry = project_registry.ProjectRegistry( context=self.context) await self.__registry.setup()
def setUp(self): self.buildinfo = buildinfo.BuildInfo() # filesystem self.filesystem = fake_filesystem.FakeFilesystem() self.cb = builder.ConfigBuilder(self.buildinfo) self.cb._task_list = []
def setUp(self): fs = fake_filesystem.FakeFilesystem() fs.CreateDirectory('/windows/panther') fs.CreateFile('/windows/panther/unattend.xml', contents=UNATTEND_XML) self.fake_open = fake_filesystem.FakeFileOpen(fs) sysprep.os = fake_filesystem.FakeOsModule(fs) sysprep.open = self.fake_open
class ConfigTestCase(unittest.TestCase): fs = fake_filesystem.FakeFilesystem() f_open = fake_filesystem.FakeFileOpen(fs) def test_defaults(self): self.assertIsInstance(Config.defaults(), dict) def test_reset(self): config = Config() config['snmp_community'] = 'private' config.reset('snmp_community') self.assertEqual(config['snmp_community'], Config.defaults()['snmp_community']) def test_reset_all(self): config = Config() config['foo'] = 'bar' config.reset() self.assertDictEqual(config, Config.defaults()) @mock.patch('builtins.open', new=f_open) def test_load(self): self.fs.create_file('/etc/abc.conf', contents='''foo: bar\n''') config = Config() config.load('abc') self.assertEqual(config['foo'], 'bar')
def parse_text_fakefs( text: Text, name: Text, print_on_error: bool, *, import_cache: ImportCache, additional_search_paths: Tuple[str, ...], filename: Text) -> Tuple[ast.Module, type_info_mod.TypeInfo]: """Wraps parse_text with a *fake filesystem* holding "text" in "filename". This primarily exists for use from testing infrastructure! For binaries and libraries one would expect things to be in runfiles instead of text. Args: text: Text to put in the fake file. name: Name to use for the module. print_on_error: Whether to print to stderr if an error is encountered parsing/typechecking the DSLX text. import_cache: Import cache to use for any module dependencies. additional_search_paths: Additional search paths to use on import. filename: Path to use in the fake filesystem for the contexts of the fake file (with DSLX text). Returns: The DSLX module and the type information. """ fs = fakefs.FakeFilesystem() fakefs_util.create_file(fs, filename, contents=text) fake_open = fakefs.FakeFileOpen(fs) return parse_text(text, name, print_on_error=print_on_error, import_cache=import_cache, additional_search_paths=additional_search_paths, filename=filename, fs_open=fake_open)
def setUp(self): # Base paths in the real and test file systems. We keep them different # so that missing features in the fake don't fall through to the base # operations and magically succeed. tsname = 'fakefs.%s' % time.time() self.cwd = os.getcwd() # Fully expand the base_path - required on OS X. self.real_base = os.path.realpath( os.path.join(tempfile.gettempdir(), tsname)) os.chdir(tempfile.gettempdir()) if os.path.isdir(self.real_base): shutil.rmtree(self.real_base) os.mkdir(self.real_base) self.fake_base = self._FAKE_FS_BASE # Make sure we can write to the physical testing temp directory. self.assertTrue(os.access(self.real_base, os.W_OK)) self.fake_filesystem = fake_filesystem.FakeFilesystem() self.fake_filesystem.create_dir(self.fake_base) self.fake_os = fake_filesystem.FakeOsModule(self.fake_filesystem) self.fake_open = fake_filesystem.FakeFileOpen(self.fake_filesystem) self._created_files = [] os.chdir(self.real_base) self.fake_os.chdir(self.fake_base)
def parse_text_fakefs(text: Text, name: Text, print_on_error: bool, *, f_import: Optional[Callable], filename: Text) -> Tuple[ast.Module, deduce.NodeToType]: """Wraps parse_text with a *fake filesystem* holding "text" in "filename". This primarily exists for use from testing infrastructure! For binaries and libraries one would expect things to be in runfiles instead of text. Args: text: Text to put in the fake file. name: Name to use for the module. print_on_error: Whether to print to stderr if an error is encountered parsing/typechecking the DSLX text. f_import: Hook used when a module needs to import another module it depends upon. filename: Path to use in the fake filesystem for the contexts of the fake file (with DSLX text). Returns: The DSLX module and the type information. """ fs = fakefs.FakeFilesystem() fs.CreateFile(filename, contents=text) fake_open = fakefs.FakeFileOpen(fs) return parse_text(text, name, print_on_error=print_on_error, f_import=f_import, filename=filename, fs_open=fake_open)
def setUp(self): super(ExecuteTest, self).setUp() self.fs = fake_filesystem.FakeFilesystem() execute.os = fake_filesystem.FakeOsModule(self.fs) execute.open = fake_filesystem.FakeFileOpen(self.fs) self.binary = r'C:\foo.exe' self.fs.create_file(self.binary)
def testGetResponseOutputFile(self): """Test _GetResponse() sending the body to output_file.""" headers = {'foo': 1} status = 200 body = 'howdy sir' body_len = len(body) path = '/file' fs = fake_filesystem.FakeFilesystem() fs.CreateFile(path) fake_open = fake_filesystem.FakeFileOpen(fs) output_file = fake_open(path, 'w') response = mock.create_autospec(httplib.HTTPResponse) response.getheaders.return_value = headers response.read.side_effect = [body, None] response.status = status response.reason = 'Ok' conn = mock.create_autospec(httplib.HTTPSConnection) conn.getresponse.return_value = response r = self.client._GetResponse(conn, output_file=output_file) self.assertEqual(r.headers, headers) self.assertEqual(r.status, status) self.assertEqual(r.body, None) self.assertEqual(r.body_len, body_len) output_file.close() self.assertEqual(body, fake_open(path).read())
def setUp(self): super(PowershellTest, self).setUp() self.fs = fake_filesystem.FakeFilesystem() powershell.os = fake_filesystem.FakeOsModule(self.fs) powershell.resources.os = fake_filesystem.FakeOsModule(self.fs) self.fs.CreateFile('/resources/bin/script.ps1') self.ps = powershell.PowerShell()
def testLoadCACertChain(self, named_temporary_file_mock): """Test _LoadCACertChain().""" temp_filepath = '/tmp/somefilename' fs = fake_filesystem.FakeFilesystem() fs.CreateFile(temp_filepath) fake_open = fake_filesystem.FakeFileOpen(fs) tf = fake_open(temp_filepath, 'w') named_temporary_file_mock.return_value = tf ctx = mock.create_autospec(M2Crypto.SSL.Context) ctx.load_verify_locations.return_value = 1 cert_chain = 'cert chain la la ..' self.mbc._ca_cert_chain = cert_chain self.mbc._LoadCACertChain(ctx) self.assertEqual(cert_chain, fake_open(temp_filepath, 'r').read()) # mock 2.0.0 incorrectly binds spec to calls ctx._spec_signature = None ctx.assert_has_calls([ mock.call.load_verify_locations(cafile=tf.name), mock.call.set_verify(client.SSL.verify_peer | client.SSL.verify_fail_if_no_peer_cert, depth=9, callback=self.mbc._IsValidCert) ])
def testRequestMultiMixed(self, request_mock, send_mock): """Test request() with multiple mixed body elements.""" filepath = '/somefilename' f_body = 'there' fs = fake_filesystem.FakeFilesystem() fs.CreateFile(filepath, contents=f_body) fake_open = fake_filesystem.FakeFileOpen(fs) f = fake_open(filepath) method = 'GET' url = '/foo' body = ['hello', f] content_length = len(body[0]) + len(f_body) headers = { 'Content-Length': content_length, 'Host': self.hostname, } self.mbc.request(method, url, body=body) request_mock.assert_called_once_with(self.mbc, method, url, headers=headers) self.assertEqual(2, send_mock.call_count) send_mock.assert_has_calls([mock.call(body[0]), mock.call(f_body)])
def test_run_snappy_itransfer_raw_data_smoke_test(mocker): fake_base_path = "/base/path" dest_path = "/irods/dest" tsv_path = os.path.join(os.path.dirname(__file__), "data", "germline.out") argv = [ "snappy", "itransfer-raw-data", "--num-parallel-transfers", "1", "--base-path", fake_base_path, tsv_path, dest_path, ] # Setup fake file system but only patch selected modules. We cannot use the Patcher approach here as this would # break both biomedsheets and multiprocessing. fs = fake_filesystem.FakeFilesystem() fake_file_paths = [] for member in ("index", "father", "mother"): for ext in ("", ".md5"): fake_file_paths.append( "%s/ngs_mapping/work/input_links/%s-N1-DNA1-WES1/%s-N1-DNA1-WES1.fastq.gz%s" % (fake_base_path, member, member, ext)) fs.create_file(fake_file_paths[-1]) fake_os = fake_filesystem.FakeOsModule(fs) mocker.patch("glob.os", fake_os) mocker.patch("cubi_tk.snappy.itransfer_common.os", fake_os) mocker.patch("cubi_tk.snappy.itransfer_raw_data.os", fake_os) mock_check_output = mock.mock_open() mocker.patch("cubi_tk.snappy.itransfer_common.check_output", mock_check_output) # Actually exercise code and perform test. parser, subparsers = setup_argparse() args = parser.parse_args(argv) res = main(argv) assert not res # We do not care about call order but simply test call count and then assert that all files are there which would # be equivalent of comparing sets of files. assert mock_check_output.call_count == len(fake_file_paths) * 3 for path in fake_file_paths: index, rel_path = os.path.relpath( path, os.path.join(fake_base_path, "ngs_mapping/work/input_links")).split("/", 1) remote_path = os.path.join(dest_path, index, "raw_data", args.remote_dir_date, rel_path) expected_mkdir_argv = ["imkdir", "-p", os.path.dirname(remote_path)] expected_irsync_argv = [ "irsync", "-a", "-K", path, "i:%s" % remote_path ] expected_ils_argv = ["ils", os.path.dirname(remote_path)] mock_check_output.assert_any_call(expected_mkdir_argv) mock_check_output.assert_any_call(expected_irsync_argv) mock_check_output.assert_any_call(expected_ils_argv, stderr=-2)
def setUp(self): super(SysprepTest, self).setUp() fs = fake_filesystem.FakeFilesystem() fs.create_dir('/windows/panther') fs.create_file('/windows/panther/unattend.xml', contents=UNATTEND_XML) self.fake_open = fake_filesystem.FakeFileOpen(fs) sysprep.os = fake_filesystem.FakeOsModule(fs) sysprep.open = self.fake_open
def setUp(self): self.reports_path = "somewhere/over/the/rainbow" self.context = open_dependency_context() self.files = fakefs.FakeFilesystem() self.fake_open = fakefs.FakeFileOpen(self.files) self.fake_os = fakefs.FakeOsModule(self.files) self.context.inject(open, self.fake_open) self.context.inject(os.walk, fakefs.FakeOsModule(self.files).walk)
def setUp(self): super(SpliceTest, self).setUp() self.fs = fake_filesystem.FakeFilesystem() splice.os = fake_filesystem.FakeOsModule(self.fs) splice.open = fake_filesystem.FakeFileOpen(self.fs) splice.os.environ['ProgramFiles'] = r'C:\Program Files' self.splice = splice.Splice() self.error = splice.execute.Error
def setUp(self): self.cwd = os.getcwd() if not self.use_real_fs(): self.filesystem = fake_filesystem.FakeFilesystem( path_separator=self.path_separator()) self.open = fake_filesystem.FakeFileOpen(self.filesystem) self.os = fake_filesystem.FakeOsModule(self.filesystem) self.create_basepath()
def setUp(self): self.cache = cache.Cache() fs = fake_filesystem.FakeFilesystem() fs.CreateDirectory(r'C:\Directory') os_module = fake_filesystem.FakeOsModule(fs) self.mock_open = fake_filesystem.FakeFileOpen(fs) cache.os = os_module cache.open = self.mock_open
def setUp(self): self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/') self.glob = fake_filesystem_glob.FakeGlobModule(self.filesystem) directory = './xyzzy' self.filesystem.CreateDirectory(directory) self.filesystem.CreateDirectory('%s/subdir' % directory) self.filesystem.CreateDirectory('%s/subdir2' % directory) self.filesystem.CreateFile('%s/subfile' % directory) self.filesystem.CreateFile('[Temp]')
def setup_method(self): self.fake_fs = fake_filesystem.FakeFilesystem() self.fake_fopen = fake_filesystem.FakeFileOpen(self.fake_fs) self.fake_os = fake_filesystem.FakeOsModule(self.fake_fs) self.fake_shutil = fake_filesystem_shutil.FakeShutilModule( self.fake_fs) self.patchers = [] self.setup_os(self.fake_os) self.setup_configuration(self.fake_os)
def setUp(self): self.scope = 'scope' self.private_key = b'IT\'S A SECRET TO EVERYBODY.' self.delegated_account = '*****@*****.**' # Mock out filesystem and file for testing. filesystem = fake_filesystem.FakeFilesystem() tempfile = fake_tempfile.FakeTempfileModule(filesystem) self.fake_open = fake_filesystem.FakeFileOpen(filesystem) self.key_file_path = tempfile.NamedTemporaryFile(delete=False).name self.cert_file_path = tempfile.NamedTemporaryFile(delete=False, prefix='cert_', suffix='.pem').name with self.fake_open(self.key_file_path, 'wb') as file_handle: file_handle.write(self.private_key) self.access_token_unrefreshed = 'a' self.access_token_refreshed = 'b' # Mock out google.auth.transport.Request for testing. self.mock_req = mock.Mock(spec=Request) self.mock_req.return_value = mock.Mock() self.mock_req_instance = self.mock_req.return_value # Mock out service account credentials for testing. self.mock_credentials = mock.Mock() self.mock_credentials.from_service_account_file.return_value = mock.Mock( ) self.mock_credentials_instance = ( self.mock_credentials.from_service_account_file.return_value) self.mock_credentials_instance.token = 'x' self.mock_credentials_instance.expiry = datetime.datetime( 1980, 1, 1, 12) self.mock_credentials_instance.expired = True def apply(headers, token=None): headers['authorization'] = ('Bearer %s' % self.mock_credentials_instance.token) def refresh(request): self.mock_credentials_instance.token = ( self.access_token_unrefreshed if self.mock_credentials_instance.token == 'x' else self.access_token_refreshed) self.mock_credentials_instance.token_expiry = datetime.datetime.utcnow( ) self.mock_credentials_instance.apply = mock.Mock(side_effect=apply) self.mock_credentials_instance.refresh = mock.Mock(side_effect=refresh) with mock.patch('builtins.open', self.fake_open): with mock.patch('google.oauth2.service_account.Credentials', self.mock_credentials): self.sa_client = googleads.oauth2.GoogleServiceAccountClient( self.key_file_path, self.scope) # Undo the call count for the auto-refresh self.mock_credentials_instance.refresh.reset_mock()
def testBuildInfoSaveError(self, build_info, sv): fs = fake_filesystem.FakeFilesystem() installer.open = fake_filesystem.FakeFileOpen(fs) installer.os = fake_filesystem.FakeOsModule(fs) fs.CreateFile('/tmp/build_info.yaml', contents='{BUILD: {opt 1: true}}\n') build_info.CachePath.return_value = '/tmp' s = installer.BuildInfoSave(None, build_info) sv.side_effect = installer.registry.Error self.assertRaises(installer.ActionError, s.Run)
def setUp(self): super(CacheTest, self).setUp() self.cache = cache.Cache() fs = fake_filesystem.FakeFilesystem() fs.create_dir(r'C:\Directory') os_module = fake_filesystem.FakeOsModule(fs) self.mock_open = fake_filesystem.FakeFileOpen(fs) cache.os = os_module cache.open = self.mock_open
class FakeFsMixin(object): fs = fake_filesystem.FakeFilesystem() f_open = fake_filesystem.FakeFileOpen(fs) f_os = fake_filesystem.FakeOsModule(fs) if sys.version_info > (3, 0): builtins_open = 'builtins.open' else: builtins_open = '__builtin__.open'