示例#1
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()
    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)
示例#3
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
示例#4
0
 def setUp(self):
     self.real_tempfile = sys.modules['tempfile']
     self.real_os = sys.modules['os']
     self.filesystem = fake_filesystem.FakeFilesystem()
     sys.modules['tempfile'] = fake_tempfile.FakeTempfileModule(
         self.filesystem)
     sys.modules['os'] = fake_filesystem.FakeOsModule(self.filesystem)
     random.seed(32452)
示例#5
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()
 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'
    def setUp(self):
        self._fs = fake_filesystem.FakeFilesystem()
        self._os = fake_filesystem.FakeOsModule(self._fs)
        self._real_os = ptc_fetch.os
        ptc_fetch.os = self._os
        self._subprocess_call_args = []

        self._raise_error_on_file = None
    def setUp(self):
        self._fs = fake_filesystem.FakeFilesystem()
        self._os = fake_filesystem.FakeOsModule(self._fs)
        self._real_os_path = ptc_fetch.os.path
        ptc_fetch.os.path = self._os.path

        self._mox = mox.Mox()
        self._mox.StubOutWithMock(ptc_fetch, 'requests')
示例#9
0
 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]')
示例#10
0
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.os_path = os.path
     sdk_dir = './sdks'
     self.sdks = ['java', 'rails', 'ruby', 'whatever']
     self.filesystem.CreateDirectory(sdk_dir)
     for sdk in self.sdks:
         self.filesystem.CreateDirectory(os.path.join(sdk_dir, sdk))
     self.os = fake_filesystem.FakeOsModule(self.filesystem)
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.glob = fake_filesystem_glob.FakeGlobModule(self.filesystem)
     directory = './xyzzy'
     self.filesystem.CreateDirectory(directory)
     self.filesystem.CreateDirectory(os.path.join(directory, 'subdir'))
     self.filesystem.CreateDirectory(os.path.join(directory, 'subdir2'))
     self.filesystem.CreateFile(os.path.join(directory, 'subfile'))
     self.filesystem.CreateFile('[Temp]')
示例#12
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
示例#13
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
示例#14
0
    def setUp(self):
        fake_fs = pyfakefs.FakeFilesystem()
        fake_os = pyfakefs.FakeOsModule(fake_fs)
        fake_fs.CreateFile(HdfsTest._site_config,
                           contents="this is not a real file.")
        fake_fs.CreateFile(
            "src",
            contents="heh. before pyfakefs this was unintentionally a dir.")

        self.original_os = twitter.common.fs.hdfs.os
        twitter.common.fs.hdfs.os = fake_os
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()
示例#16
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
示例#17
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
示例#18
0
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
     self.tempfile = fake_tempfile.FakeTempfileModule(self.filesystem)
     self.orig_logging = fake_tempfile.logging
     self.fake_logging = FakeLogging(self)
     fake_tempfile.logging = self.fake_logging
示例#19
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)
示例#20
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')
示例#21
0
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem(path_separator='/')
     self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem)
示例#22
0
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.shutil = fake_filesystem_shutil.FakeShutilModule(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)