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()
        # 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.CreateDirectory(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)
示例#2
0
    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
示例#3
0
  def setUp(self):
    self.scope = 'scope'
    self.service_account_email = '*****@*****.**'
    self.private_key_password = '******'
    https_proxy_host = 'myproxy.com'
    https_proxy_port = 443
    https_proxy = googleads.common.ProxyConfig.Proxy(https_proxy_host,
                                                     https_proxy_port)
    self.proxy_config = googleads.common.ProxyConfig(https_proxy=https_proxy)
    self.https_proxy = '%s:%s' % (https_proxy_host, https_proxy_port)
    self.access_token_unrefreshed = 'a'
    self.access_token_refreshed = 'b'

    # Mock out filesystem and file for testing.
    filesystem = fake_filesystem.FakeFilesystem()
    tempfile = fake_tempfile.FakeTempfileModule(filesystem)
    fake_open = fake_filesystem.FakeFileOpen(filesystem)
    key_file_path = tempfile.NamedTemporaryFile(delete=False).name

    with fake_open(key_file_path, 'w') as file_handle:
      file_handle.write('IT\'S A SECRET TO EVERYBODY.')

    # Mock out httplib2.Http for testing.
    self.http = mock.Mock(spec=httplib2.Http)
    self.opener = self.http.return_value = mock.Mock()
    self.opener.proxy_info = self.proxy_config.proxy_info
    self.opener.ca_certs = self.proxy_config.cafile
    self.opener.disable_ssl_certificate_valiation = (
        self.proxy_config.disable_certificate_validation)

    # Mock out oauth2client.client.OAuth2Credentials for testing
    self.oauth2_credentials = mock.Mock(spec=SignedJwtAssertionCredentials)
    self.mock_oauth2_credentials = self.oauth2_credentials.return_value = (
        mock.Mock())
    self.mock_oauth2_credentials.access_token = 'x'
    self.mock_oauth2_credentials.token_expiry = datetime.datetime(1980, 1, 1,
                                                                  12)

    def apply(headers):
      headers['Authorization'] = ('Bearer %s'
                                  % self.mock_oauth2_credentials.access_token)

    def refresh(mock_http):
      self.mock_oauth2_credentials.access_token = (
          self.access_token_unrefreshed if
          self.mock_oauth2_credentials.access_token is 'x'
          else self.access_token_refreshed)
      self.mock_oauth2_credentials.token_expiry = datetime.datetime.utcnow()

    self.mock_oauth2_credentials.apply = mock.Mock(side_effect=apply)
    self.mock_oauth2_credentials.refresh = mock.Mock(side_effect=refresh)
    with mock.patch('__builtin__.open', fake_open):
      with mock.patch('oauth2client.client'
                      '.SignedJwtAssertionCredentials',
                      self.oauth2_credentials):
        self.googleads_client = googleads.oauth2.GoogleServiceAccountClient(
            self.scope, self.service_account_email, key_file_path,
            self.private_key_password, proxy_config=self.proxy_config)
      # Undo the call count for the auto-refresh
      self.mock_oauth2_credentials.refresh.reset_mock()
示例#4
0
    def setUp(self):
        """ Initialize a fake filesystem and dirtools. """

        # First we create a fake filesystem in order to test dirtools
        fk = fake_filesystem.FakeFilesystem()
        fk.CreateDirectory('/test_dirtools')
        fk.CreateFile('/test_dirtools/file1', contents='contents1')
        fk.CreateFile('/test_dirtools/file2', contents='contents2')
        fk.CreateFile('/test_dirtools/file3.py', contents='print "ok"')
        fk.CreateFile('/test_dirtools/file3.pyc', contents='')
        fk.CreateFile('/test_dirtools/.exclude',
                      contents='excluded_dir/\n*.pyc')

        fk.CreateDirectory('/test_dirtools/excluded_dir')
        fk.CreateFile('/test_dirtools/excluded_dir/excluded_file',
                      contents='excluded')

        fk.CreateDirectory('/test_dirtools/dir1')
        fk.CreateDirectory('/test_dirtools/dir1/subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/file_subdir1',
                      contents='inside subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/.project')

        fk.CreateDirectory('/test_dirtools/dir2')
        fk.CreateFile('/test_dirtools/dir2/file_dir2', contents='inside dir2')

        # Sort of "monkey patch" to make dirtools use the fake filesystem
        dirtools.os = fake_filesystem.FakeOsModule(fk)
        dirtools.open = fake_filesystem.FakeFileOpen(fk)

        # Dirtools initialization
        self.dir = dirtools.Dir('/test_dirtools')
        self.os = dirtools.os
        self.open = dirtools.open
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem)
     self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
     self.fake_proxy = mock.Mock()
     self.fake_proxy.return_value = mock.Mock()
     self.fake_proxy.return_value.proxy_host = 'ahost'
     self.fake_proxy.return_value.proxy_port = 'aport'
示例#6
0
 def setUp(self):
     self.parser = utils.options.OptionParser()
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.open_func = sys.modules['__builtin__'].open
     sys.modules['__builtin__'].open = \
       fake_filesystem.FakeFileOpen(self.filesystem)
     self.filesystem.CreateDirectory('/dev')
     open('/dev/null', 'a').close()
示例#7
0
    def setupFilesystem(self, module, path_specs=None, from_module=None):
        """Configure module so that it uses a fake filesystem.

    Raises:
      Error: if the method is called on a module multiple times.
      Error: if from_module is not None and has not been setup.
    Returns fake_filesystem.FakeFilesystem

    Args:
      module: module
        The module to configure.
      path_specs: *list of {str_or_two-tuple_(str/_dict)}
        The paths to create.  If any element is a two-tuple, the
        first element is the path, and the second element is a dict
        of keyword args to send to fake_filesystem.CreateFile().
      from_module: *module
        If specified, the faux filesystem objects are obtained from
        the given module, rather than being created anew.  This means
        that the same filesystem objects are shared across multiple
        modules (arguably the most sensible way to test things).

    Returns: any
    """
        mname = module.__name__
        fsinfo = self._fsinfo
        if mname in fsinfo:
            raise Error(
                'Attempt to setupFilesystem on %s when it is already setup' %
                mname)

        fsinfo[mname] = {'module': module, 'objs': {}}
        info = fsinfo[mname]

        if from_module:
            oname = from_module.__name__
            if oname not in fsinfo:
                raise Error(
                    'Request to setup faux filesystem for module %s based on module %s'
                    ' which has not been set up.' % (mname, oname))
            info['objs'] = fsinfo[oname]['objs']
            objs = info['objs']
            fs = objs['fs']
        else:
            fs = fake_filesystem.FakeFilesystem()
            objs = info['objs']
            objs['fs'] = fs
            objs['fcntl'] = FakeFcntl(fs)
            objs['open'] = fake_filesystem.FakeFileOpen(fs)
            objs['os'] = fake_filesystem.FakeOsModule(fs)

        module.open = objs['open']
        for modname in ('os', 'fcntl'):
            if hasattr(module, modname):
                self._stubs.Set(module, modname, objs[modname])
        if path_specs:
            self.populateFilesystem(fs, path_specs)
        return fs
示例#8
0
    def setUp(self):
        self.access_logger = logging.getLogger('tornado.access')
        self.orig_handlers = copy.copy(self.access_logger.handlers)
        self.filesystem = fake_filesystem.FakeFilesystem()
        self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        cloghandler.open = self.fake_open

        self.logger_configured = utils.logs._added_configured_logger
        utils.logs._added_configured_logger = False
def test_read_key():

    # Create a fake filesystem
    fakefs = fake_filesystem.FakeFilesystem()
    # Create a file our code expects to find
    fakefs.CreateFile('/var/keys/1.pub', contents='$3432adsd')

    # The FakeFileOpen class patches the built-in open()
    # function
    fake_open = fake_filesystem.FakeFileOpen(fakefs)
    with patch('read_key.open', fake_open):
        assert read_key()
示例#10
0
    def _refresh(self):
        '''Renew the fake file system and set the _isStale flag to `False`.'''
        mock.patch.stopall()

        self.fs = fake_filesystem.FakeFilesystem()
        self.fake_os = fake_filesystem.FakeOsModule(self.fs)
        self.fake_glob = fake_filesystem_glob.FakeGlobModule(self.fs)
        self.fake_path = self.fake_os.path
        self.fake_shutil = fake_filesystem_shutil.FakeShutilModule(self.fs)
        self.fake_tempfile_ = fake_tempfile.FakeTempfileModule(self.fs)
        self.fake_open = fake_filesystem.FakeFileOpen(self.fs)

        self._isStale = False
示例#11
0
    def _refresh(self):
        '''Renew the fake file system and set the _isStale flag to `False`.'''
        if self._stubs is not None:
            self._stubs.SmartUnsetAll()
        self._stubs = mox3.stubout.StubOutForTesting()

        self.fs = fake_filesystem.FakeFilesystem()
        self.fake_os = fake_filesystem.FakeOsModule(self.fs)
        self.fake_glob = fake_filesystem_glob.FakeGlobModule(self.fs)
        self.fake_path = self.fake_os.path
        self.fake_shutil = fake_filesystem_shutil.FakeShutilModule(self.fs)
        self.fake_tempfile_ = fake_tempfile.FakeTempfileModule(self.fs)
        self.fake_open = fake_filesystem.FakeFileOpen(self.fs)

        self._isStale = False
    def CallFetchPtcFiles(self):
        self._real_stdout = sys.stdout
        self._real_stderr = sys.stderr
        sys.stdout = cStringIO.StringIO()
        sys.stderr = cStringIO.StringIO()
        self._real_open = __builtins__.open
        __builtins__.open = fake_filesystem.FakeFileOpen(self._fs)

        try:
            ptc_fetch.FetchPtcFiles()
        finally:
            self._stdout = sys.stdout.getvalue()
            self._stderr = sys.stderr.getvalue()
            sys.stdout = self._real_stdout
            sys.stderr = self._real_stderr
            __builtins__.open = self._real_open
示例#13
0
# ./data_handler/provider/local_file

from data_handler.provider import local_file

import tests.fakers

from nose.tools import assert_equal
import mox
import fake_filesystem

filesystem = fake_filesystem.FakeFilesystem()
local_file.os = fake_filesystem.FakeOsModule(filesystem)
local_file.open = fake_filesystem.FakeFileOpen(filesystem)


class TestLocalFile(object):
    @classmethod
    def setup_class(klass):
        """This method is run once for each class before any tests are run"""
        klass.test_file_path = "/test/file"
        klass.test_file_data = "{hostname': 'abc', 'password': '******'}"
        filesystem.CreateFile(klass.test_file_path,
                              contents=klass.test_file_data)

        klass._fakeparser = tests.fakers.FakeParser()
        klass._local_file = local_file.LocalFile(parser=klass._fakeparser,
                                                 filepath=klass.test_file_path)

    def setUp(self):
        self.m = mox.Mox()
        self.m.StubOutWithMock(self._local_file, 'parse')
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem)
     self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
示例#15
0
 def __init__(self, filesystem=None):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.os = fake_filesystem.FakeOsModule(self.filesystem)
     self._open = fake_filesystem.FakeFileOpen(self.filesystem)
     self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem)