コード例 #1
0
ファイル: test_controller.py プロジェクト: legnaleurc/ddld
async def fake_download_node(node, local_path):
    r_fake_open = ffs.FakeFileOpen(node._fs)
    l_fake_open = ffs.FakeFileOpen(local_path._fs)

    assert not node.is_folder

    local_file = str(local_path / node.name)
    with r_fake_open(node._path, 'rb') as fin, l_fake_open(local_file,
                                                           'wb') as fout:
        while True:
            chunk = fin.read(65535)
            if not chunk:
                break
            fout.write(chunk)

    return u.get_md5(l_fake_open, local_file)
コード例 #2
0
 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
コード例 #3
0
  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
コード例 #4
0
ファイル: fake_tempfile.py プロジェクト: subhanshuja/ofa
    def NamedTemporaryFile(self,
                           mode='w+b',
                           bufsize=-1,
                           suffix='',
                           prefix=None,
                           dir=None,
                           delete=True):
        """Return a file-like object with name that is deleted on close().

    Python 2.4.1 tempfile.NamedTemporaryFile.__doc__ =
    >This function operates exactly as TemporaryFile() does, except that
    >the file is guaranteed to have a visible name in the file system. That
    >name can be retrieved from the name member of the file object.

    Args:
      mode: optional string, see above
      bufsize: optional int, see above
      suffix: optional string, see above
      prefix: optional string, see above
      dir: optional string, see above
      delete: optional bool, see above
    Returns:
      a file-like object including obj.name
    """
        # pylint: disable-msg=C6002
        # TODO: bufsiz unused?
        temp = self.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
        filename = temp[1]
        mock_open = fake_filesystem.FakeFileOpen(self._filesystem,
                                                 delete_on_close=delete)
        obj = mock_open(filename, mode)
        obj.name = filename
        return obj
コード例 #5
0
    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)])
コード例 #6
0
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')
コード例 #7
0
ファイル: parse_and_typecheck.py プロジェクト: masc-ucsc/xls
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)
コード例 #8
0
    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)
コード例 #9
0
ファイル: parse_and_typecheck.py プロジェクト: 5l1v3r1/xls
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)
コード例 #10
0
ファイル: execute_test.py プロジェクト: l1kw1d/glazier
 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)
コード例 #11
0
    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())
コード例 #12
0
    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()
コード例 #13
0
  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
コード例 #14
0
    def command_fsupload(self, request):
        request_parameters = self.get_parameters(request)
        self.logger.info("fsupload - request - " + request_parameters["NAME"])
        self.events_list.append("fsupload - request - " +
                                request_parameters["NAME"])

        upload_file = request_parameters["NAME"].replace('"', '').split(":")[1]
        self.logger.debug("fsupload - request - requested file: " +
                          upload_file)
        self.events_list.append("fsupload - request - requested file: " +
                                upload_file)
        return_data = ''

        if (self.fos.path.exists(upload_file)):
            contents = ''
            file_module = fake_filesystem.FakeFileOpen(self.fs)
            for line in file_module(upload_file):
                contents += line

            size = self.fos.stat(upload_file).st_size
            return_data = 'FORMAT:BINARY NAME=' + request_parameters[
                'NAME'] + ' OFFSET=0 SIZE=' + str(size) + '\r\n' + contents
        else:
            return_data = 'NAME=' + request_parameters[
                'NAME'] + '\r\nFILEERROR=3\r\n'

        response = '@PJL FSUPLOAD ' + return_data
        self.logger.info("fsupload - response - " +
                         str(response.encode('UTF-8')))
        self.events_list.append("fsupload - response - " +
                                str(response.encode('UTF-8')))
        return response
コード例 #15
0
    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)
        ])
コード例 #16
0
 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
コード例 #17
0
 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)
コード例 #18
0
ファイル: filesystem.py プロジェクト: xzaviourr/Honeypot
 def read_file(self, file_name):
     file_module = fake_filesystem.FakeFileOpen(self.filesystem)
     data = ""
     for line in file_module(self.pathname + str(file_name)):
         data += line
     self.reply = data
     return data
コード例 #19
0
ファイル: sysprep_test.py プロジェクト: wutijat/glazier
 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
コード例 #20
0
 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)
コード例 #21
0
 def test_saves_default_config_file(self):
     test_file = os.path.join('test', 'testFile.mxd')
     self.fs.CreateFile(test_file)
     fake_open = fake_filesystem.FakeFileOpen(self.fs)
     with patch('slap.config_builder.os', self.fake_os):
         with patch('__builtin__.open', fake_open):
             config_builder.create_config(['test'])
             self.assertTrue(self.fake_os.path.isfile('config.json'))
コード例 #22
0
 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
コード例 #23
0
ファイル: test_utils.py プロジェクト: jarondl/pyfakefs
 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()
コード例 #24
0
 def testRmtreeWithOpenFilePosix(self):
     self.fs.is_windows_fs = False
     fake_open = fake_filesystem.FakeFileOpen(self.fs)
     self.fs.CreateFile('/foo/bar')
     self.fs.CreateFile('/foo/baz')
     fake_open('/foo/baz', 'r')
     shutil.rmtree('/foo')
     self.assertFalse(self.fs.Exists('/foo/baz'))
コード例 #25
0
 def testRmtreeWithOpenFileFailsUnderWindows(self):
     self.fs.is_windows_fs = True
     fake_open = fake_filesystem.FakeFileOpen(self.fs)
     self.fs.CreateFile('/foo/bar')
     self.fs.CreateFile('/foo/baz')
     fake_open('/foo/baz', 'r')
     self.assertRaises(OSError, shutil.rmtree, '/foo')
     self.assertTrue(self.fs.Exists('/foo/baz'))
コード例 #26
0
 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
コード例 #27
0
    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()
コード例 #28
0
 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)
コード例 #29
0
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'
コード例 #30
0
ファイル: cache_test.py プロジェクト: wutijat/glazier
 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