Exemplo n.º 1
0
    def clear_cache(cls):
        """Clears the cache of .cached().

    So the next call to .cached() returns the fresh instance from ndb.
    """
        if cls._config_fetcher:
            utils.clear_cache(cls._config_fetcher)
Exemplo n.º 2
0
  def clear_cache(cls):
    """Clears the cache of .cached().

    So the next call to .cached() returns the fresh instance from ndb.
    """
    if cls._config_fetcher:
      utils.clear_cache(cls._config_fetcher)
Exemplo n.º 3
0
  def test_cache_with_expiration(self):
    ran = []
    self.mock(utils, 'time_time', lambda: 1000)

    @utils.cache_with_expiration(30)
    def do_work():
      ran.append(1)
      return len(ran)

    self.assertEqual(30, do_work.__parent_cache__.expiration_sec)
    self.assertEqual(None, do_work.__parent_cache__.expires)

    self.assertEqual(1, do_work())
    self.assertEqual(1, do_work())
    self.assertEqual(1, len(ran))
    self.assertEqual(1030, do_work.__parent_cache__.expires)

    utils.clear_cache(do_work)
    self.assertEqual(2, do_work())
    self.assertEqual(2, len(ran))
    self.assertEqual(1030, do_work.__parent_cache__.expires)

    self.mock(utils, 'time_time', lambda: 1029)
    self.assertEqual(2, do_work())
    self.assertEqual(2, do_work())
    self.assertEqual(2, len(ran))

    self.mock(utils, 'time_time', lambda: 1030)
    self.assertEqual(3, do_work())
    self.assertEqual(3, do_work())
    self.assertEqual(3, len(ran))
    self.assertEqual(1060, do_work.__parent_cache__.expires)
Exemplo n.º 4
0
    def mock_config(self, cfg):
        def get_self_config_mock(path, cls, **_kwargs):
            self.assertEquals('bots.cfg', path)
            self.assertEquals(cls, bots_pb2.BotsCfg)
            return None, cfg

        self.mock(config, 'get_self_config', get_self_config_mock)
        utils.clear_cache(bot_groups_config._fetch_bot_groups)
Exemplo n.º 5
0
    def mock_config(self, cfg):
        def get_self_config_mock(path, cls=None, **kwargs):
            self.assertEqual({'store_last_good': True}, kwargs)
            self.assertEqual('pools.cfg', path)
            self.assertEqual(cls, pools_pb2.PoolsCfg)
            return 'rev', cfg

        self.mock(config, 'get_self_config', get_self_config_mock)
        utils.clear_cache(pools_config._fetch_pools_config)
Exemplo n.º 6
0
 def setUp(self):
     super(RealmsTest, self).setUp()
     self._has_permission_mock = mock.Mock()
     self._has_permission_dryrun_mock = mock.Mock()
     self.mock(auth, 'has_permission', self._has_permission_mock)
     self.mock(auth, 'has_permission_dryrun',
               self._has_permission_dryrun_mock)
     self.mock(service_accounts, 'has_token_server', lambda: True)
     utils.clear_cache(config.settings)
Exemplo n.º 7
0
def reset():
  """To be used in tests only."""
  global _TEMPLATE_PATHS
  global _GLOBAL_ENV
  global _GLOBAL_FILTERS
  _TEMPLATE_PATHS = {}
  _GLOBAL_ENV = {}
  _GLOBAL_FILTERS = _DEFAULT_GLOBAL_FILTERS.copy()
  utils.clear_cache(get_jinja_env)
Exemplo n.º 8
0
def reset():
  """To be used in tests only."""
  global _TEMPLATE_PATHS
  global _GLOBAL_ENV
  global _GLOBAL_FILTERS
  _TEMPLATE_PATHS = {}
  _GLOBAL_ENV = {}
  _GLOBAL_FILTERS = _DEFAULT_GLOBAL_FILTERS.copy()
  utils.clear_cache(get_jinja_env)
Exemplo n.º 9
0
  def test_clear_cache(self):
    calls = []

    @utils.cache
    def get_me():
      calls.append(1)
      return len(calls)

    self.assertEqual(1, get_me())
    utils.clear_cache(get_me)
    self.assertEqual(2, get_me())
    self.assertEqual(2, len(calls))
Exemplo n.º 10
0
    def test_clear_cache(self):
        calls = []

        @utils.cache
        def get_me():
            calls.append(1)
            return len(calls)

        self.assertEqual(1, get_me())
        utils.clear_cache(get_me)
        self.assertEqual(2, get_me())
        self.assertEqual(2, len(calls))
Exemplo n.º 11
0
 def test_get_delegation_config(self):
   # Missing -> returns empty proto.
   proto = config.get_delegation_config()
   self.assertFalse(proto.rules)
   # Add some.
   body = """rules {
     user_id: "service:abc"
     target_service: "*"
     max_validity_duration: 3600
   }"""
   config._update_service_config(
       'delegation.cfg', config.Revision('rev', 'url'), body)
   utils.clear_cache(config.get_delegation_config)
   proto = config.get_delegation_config()
   self.assertEqual(1, len(proto.rules))
Exemplo n.º 12
0
 def test_get_delegation_config(self):
     # Missing -> returns empty proto.
     proto = config.get_delegation_config()
     self.assertFalse(proto.rules)
     # Add some.
     body = """rules {
   user_id: "service:abc"
   target_service: "*"
   max_validity_duration: 3600
 }"""
     config._update_service_config('delegation.cfg',
                                   config.Revision('rev', 'url'), body)
     utils.clear_cache(config.get_delegation_config)
     proto = config.get_delegation_config()
     self.assertEqual(1, len(proto.rules))
Exemplo n.º 13
0
def bootstrap(paths, global_env=None, filters=None):
  """Resets cached Jinja2 env to pick up new template paths.

  This is purely additive and idempotent. So consecutive calls to this functions
  with different arguments is fine.

  Args:
    paths: dict {prefix -> template_dir}, templates under template_dir would be
        accessible as <prefix>/<path relative to template_dir>.
    global_env: dict with variables to add to global template environment.
    filters: dict with filters to add to global filter list.
  """
  assert isinstance(paths, dict), paths
  assert all(
      _TEMPLATE_PATHS.get(k, v) == v for k, v in paths.items()), paths
  assert all(os.path.isabs(p) for p in paths.values()), paths
  assert all(os.path.isdir(p) for p in paths.values()), paths

  if global_env is not None:
    assert isinstance(global_env, dict), global_env
    assert all(isinstance(k, str) for k in global_env), global_env
    assert all(
        _GLOBAL_ENV.get(k, v) == v
        for k, v in global_env.items()), global_env

  if filters is not None:
    assert isinstance(filters, dict), filters
    assert all(
        isinstance(k, str) and callable(v)
        for k, v in filters.items()), filters
    assert all(
        _GLOBAL_FILTERS.get(k, v) == v
        for k, v in filters.items()), filters

  _TEMPLATE_PATHS.update(paths)

  if global_env:
    _GLOBAL_ENV.update(global_env)
  # These are immutable.
  _GLOBAL_ENV.setdefault('app_id', app_identity.get_application_id())
  _GLOBAL_ENV.setdefault('app_version', utils.get_app_version())
  _GLOBAL_ENV.setdefault('app_revision_url', utils.get_app_revision_url())

  if filters:
    _GLOBAL_FILTERS.update(filters)
  utils.clear_cache(get_jinja_env)
Exemplo n.º 14
0
def bootstrap(paths, global_env=None, filters=None):
  """Resets cached Jinja2 env to pick up new template paths.

  This is purely additive and idempotent. So consecutive calls to this functions
  with different arguments is fine.

  Args:
    paths: dict {prefix -> template_dir}, templates under template_dir would be
        accessible as <prefix>/<path relative to template_dir>.
    global_env: dict with variables to add to global template environment.
    filters: dict with filters to add to global filter list.
  """
  assert isinstance(paths, dict), paths
  assert all(
      _TEMPLATE_PATHS.get(k, v) == v for k, v in paths.iteritems()), paths
  assert all(os.path.isabs(p) for p in paths.itervalues()), paths
  assert all(os.path.isdir(p) for p in paths.itervalues()), paths

  if global_env is not None:
    assert isinstance(global_env, dict), global_env
    assert all(isinstance(k, str) for k in global_env), global_env
    assert all(
        _GLOBAL_ENV.get(k, v) == v
        for k, v in global_env.iteritems()), global_env

  if filters is not None:
    assert isinstance(filters, dict), filters
    assert all(
        isinstance(k, str) and callable(v)
        for k, v in filters.iteritems()), filters
    assert all(
        _GLOBAL_FILTERS.get(k, v) == v
        for k, v in filters.iteritems()), filters

  _TEMPLATE_PATHS.update(paths)

  if global_env:
    _GLOBAL_ENV.update(global_env)
  _GLOBAL_ENV.setdefault('app_version', utils.get_app_version())
  _GLOBAL_ENV.setdefault('app_revision_url', utils.get_app_revision_url())

  if filters:
    _GLOBAL_FILTERS.update(filters)
  utils.clear_cache(get_jinja_env)
Exemplo n.º 15
0
    def setUp(self):
        super(AppTestBase, self).setUp()
        self.bot_version = None
        self.source_ip = '192.168.2.2'
        self.testbed.init_user_stub()

        gae_ts_mon.reset_for_unittest(disable=True)
        event_mon_metrics.initialize()

        # By default requests in tests are coming from bot with fake IP.
        # WSGI app that implements auth REST API.
        self.auth_app = webtest.TestApp(
            auth.create_wsgi_application(debug=True),
            extra_environ={
                'REMOTE_ADDR': self.source_ip,
                'SERVER_SOFTWARE': os.environ['SERVER_SOFTWARE'],
            })

        admins_group = 'test_admins_group'
        priv_users_group = 'test_priv_users_group'
        users_group = 'test_users_group'

        cfg = config_pb2.SettingsCfg(auth=config_pb2.AuthSettings(
            admins_group=admins_group,
            privileged_users_group=priv_users_group,
            users_group=users_group,
        ))
        self.mock(config, '_get_settings', lambda: ('test_rev', cfg))
        utils.clear_cache(config.settings)

        # Note that auth.ADMIN_GROUP != admins_group.
        auth.bootstrap_group(
            auth.ADMIN_GROUP,
            [auth.Identity(auth.IDENTITY_USER, '*****@*****.**')])
        auth.bootstrap_group(
            admins_group,
            [auth.Identity(auth.IDENTITY_USER, '*****@*****.**')])
        auth.bootstrap_group(
            priv_users_group,
            [auth.Identity(auth.IDENTITY_USER, '*****@*****.**')])
        auth.bootstrap_group(
            users_group,
            [auth.Identity(auth.IDENTITY_USER, '*****@*****.**')])
Exemplo n.º 16
0
    def test_settings_updates(self):
        # Fetch only settings.cfg in this test case.
        self.mock(config, 'is_remote_configured', lambda: True)
        self.mock(config, '_CONFIG_SCHEMAS', {
            'settings.cfg': config._CONFIG_SCHEMAS['settings.cfg'],
        })

        # Default settings.
        self.assertEqual(config_pb2.SettingsCfg(), config.get_settings())

        # Mock new settings value in luci-config.
        settings_cfg_text = 'enable_ts_monitoring: true'
        self.mock(
            config, '_fetch_configs', lambda _: {
                'settings.cfg':
                (config.Revision('rev', 'url'), settings_cfg_text),
            })

        # Fetch them.
        config.refetch_config()

        # Verify they are used now.
        utils.clear_cache(config.get_settings)
        self.assertEqual(config_pb2.SettingsCfg(enable_ts_monitoring=True),
                         config.get_settings())

        # "Delete" them from luci-config.
        self.mock(
            config, '_fetch_configs', lambda _: {
                'settings.cfg': (config.Revision('0' * 40, 'url'), ''),
            })

        # Fetch them.
        config.refetch_config()

        # Verify defaults are restored.
        utils.clear_cache(config.get_settings)
        self.assertEqual(config_pb2.SettingsCfg(), config.get_settings())
Exemplo n.º 17
0
 def test_works(self):
     utils.clear_cache(api.get_web_client_id)
     self.assertEqual('', api.get_web_client_id_uncached())
     api.set_web_client_id('zzz')
     self.assertEqual('zzz', api.get_web_client_id_uncached())
     self.assertEqual('zzz', api.get_web_client_id())
Exemplo n.º 18
0
 def tearDown(self):
     super(RealmsTest, self).tearDown()
     utils.clear_cache(config.settings)
Exemplo n.º 19
0
  def setUp(self):
    super(ConfigTest, self).setUp()

    utils.clear_cache(config.settings)
Exemplo n.º 20
0
 def setUp(self):
   super(ConfigTest, self).setUp()
   utils.clear_cache(config.get_delegation_config)
Exemplo n.º 21
0
    def setUp(self):
        super(ConfigTest, self).setUp()

        utils.clear_cache(config._get_settings_cached)
        config.GlobalConfig.clear_cache()
Exemplo n.º 22
0
 def setUp(self):
     super(ConfigTest, self).setUp()
     utils.clear_cache(config.get_delegation_config)