Exemplo n.º 1
0
def init():
    global _POLICY_PATH
    global _POLICY_CACHE
    if not _POLICY_PATH:
        _POLICY_PATH = utils.find_config(FLAGS.policy_file)
    utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE,
                           reload_func=_set_brain)
Exemplo n.º 2
0
def init():
    global _POLICY_PATH
    global _POLICY_CACHE
    if not _POLICY_PATH:
        _POLICY_PATH = CONF.policy_file
        if not os.path.exists(_POLICY_PATH):
            _POLICY_PATH = CONF.find_file(_POLICY_PATH)
        if not _POLICY_PATH:
            raise exception.ConfigNotFound(path=CONF.policy_file)
    utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE, reload_func=_set_rules)
Exemplo n.º 3
0
def init():
    global _POLICY_PATH
    global _POLICY_CACHE
    if not _POLICY_PATH:
        _POLICY_PATH = FLAGS.policy_file
        if not os.path.exists(_POLICY_PATH):
            _POLICY_PATH = FLAGS.find_file(_POLICY_PATH)
        if not _POLICY_PATH:
            raise exception.ConfigNotFound(path=FLAGS.policy_file)
    utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE, reload_func=_set_brain)
Exemplo n.º 4
0
def init():
    global _INJECT_FILES_PATH
    global _INJECT_FILES_CACHE
    if not _INJECT_FILES_PATH:
        _INJECT_FILES_PATH = FLAGS.inject_content_file
        if not os.path.exists(_INJECT_FILES_PATH):
            _INJECT_FILES_PATH = FLAGS.find_file(_INJECT_FILES_PATH)
        if not _INJECT_FILES_PATH:
            raise exception.ConfigNotFound(path=FLAGS.inject_content_file)
    utils.read_cached_file(_INJECT_FILES_PATH, _INJECT_FILES_CACHE,
                           reload_func=_load_inject_files)
Exemplo n.º 5
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        self.mox.StubOutWithMock(__builtin__, 'open')
        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)

        fake_contents = "lorem ipsum"
        fake_file = self.mox.CreateMockAnything()
        fake_file.read().AndReturn(fake_contents)
        fake_context_manager = self.mox.CreateMockAnything()
        fake_context_manager.__enter__().AndReturn(fake_file)
        fake_context_manager.__exit__(mox.IgnoreArg(), mox.IgnoreArg(),
                                      mox.IgnoreArg())

        __builtin__.open(mox.IgnoreArg()).AndReturn(fake_context_manager)

        self.mox.ReplayAll()
        cache_data = {"data": 1123, "mtime": 1}
        self.reload_called = False

        def test_reload(reloaded_data):
            self.assertEqual(reloaded_data, fake_contents)
            self.reload_called = True

        data = utils.read_cached_file("/this/is/a/fake",
                                      cache_data,
                                      reload_func=test_reload)
        self.mox.UnsetStubs()
        self.assertEqual(data, fake_contents)
        self.assertTrue(self.reload_called)
Exemplo n.º 6
0
 def test_read_cached_file(self, getmtime):
     utils._FILE_CACHE = {
         '/this/is/a/fake': {"data": 1123, "mtime": 1}
     }
     fresh, data = utils.read_cached_file("/this/is/a/fake")
     fdata = utils._FILE_CACHE['/this/is/a/fake']["data"]
     self.assertEqual(fdata, data)
Exemplo n.º 7
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        self.mox.StubOutWithMock(__builtin__, 'open')
        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)

        fake_contents = "lorem ipsum"
        fake_file = self.mox.CreateMockAnything()
        fake_file.read().AndReturn(fake_contents)
        fake_context_manager = self.mox.CreateMockAnything()
        fake_context_manager.__enter__().AndReturn(fake_file)
        fake_context_manager.__exit__(mox.IgnoreArg(),
                                      mox.IgnoreArg(),
                                      mox.IgnoreArg())

        __builtin__.open(mox.IgnoreArg()).AndReturn(fake_context_manager)

        self.mox.ReplayAll()
        cache_data = {"data": 1123, "mtime": 1}
        self.reload_called = False

        def test_reload(reloaded_data):
            self.assertEqual(reloaded_data, fake_contents)
            self.reload_called = True

        data = utils.read_cached_file("/this/is/a/fake", cache_data,
                                                reload_func=test_reload)
        self.mox.UnsetStubs()
        self.assertEqual(data, fake_contents)
        self.assertTrue(self.reload_called)
Exemplo n.º 8
0
 def _load_policy_file(self, path, force_reload, overwrite=True):
     reloaded, data = utils.read_cached_file(path,
                                             force_reload=force_reload)
     if reloaded or not self.rules or not overwrite:
         rules = Rules.load_json(data, self.default_rule)
         self.set_rules(rules, overwrite=overwrite, use_conf=True)
         LOG.debug("Reloaded policy file: %(path)s", {'path': path})
Exemplo n.º 9
0
 def test_read_cached_file(self, getmtime):
     utils._FILE_CACHE = {
         '/this/is/a/fake': {"data": 1123, "mtime": 1}
     }
     fresh, data = utils.read_cached_file("/this/is/a/fake")
     fdata = utils._FILE_CACHE['/this/is/a/fake']["data"]
     self.assertEqual(fdata, data)
Exemplo n.º 10
0
 def _load_policy_file(self, path, force_reload, overwrite=True):
         reloaded, data = utils.read_cached_file(
             path, force_reload=force_reload)
         if reloaded or not self.rules or not overwrite:
             rules = Rules.load_json(data, self.default_rule)
             self.set_rules(rules, overwrite=overwrite, use_conf=True)
             LOG.debug("Reloaded policy file: %(path)s",
                       {'path': path})
Exemplo n.º 11
0
    def test_read_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        os.path.getmtime(mox.IgnoreArg()).AndReturn(1)
        self.mox.ReplayAll()

        cache_data = {"data": 1123, "mtime": 1}
        data = utils.read_cached_file("/this/is/a/fake", cache_data)
        self.assertEqual(cache_data["data"], data)
Exemplo n.º 12
0
    def test_read_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        os.path.getmtime(mox.IgnoreArg()).AndReturn(1)
        self.mox.ReplayAll()

        cache_data = {"data": 1123, "mtime": 1}
        data = utils.read_cached_file("/this/is/a/fake", cache_data)
        self.assertEqual(cache_data["data"], data)
Exemplo n.º 13
0
    def test_read_modified_cached_file(self, getmtime):

        utils._FILE_CACHE = {"/this/is/a/fake": {"data": 1123, "mtime": 1}}

        fake_contents = "lorem ipsum"

        with mock.patch("six.moves.builtins.open", mock.mock_open(read_data=fake_contents)):
            fresh, data = utils.read_cached_file("/this/is/a/fake")

        self.assertEqual(data, fake_contents)
        self.assertTrue(fresh)
Exemplo n.º 14
0
    def test_read_modified_cached_file(self, getmtime):

        utils._FILE_CACHE = {'/this/is/a/fake': {"data": 1123, "mtime": 1}}

        fake_contents = "lorem ipsum"

        with mock.patch('six.moves.builtins.open',
                        mock.mock_open(read_data=fake_contents)):
            fresh, data = utils.read_cached_file("/this/is/a/fake")

        self.assertEqual(data, fake_contents)
        self.assertTrue(fresh)
Exemplo n.º 15
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        self.mox.StubOutWithMock(__builtin__, 'open')

        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)

        fake_contents = "lorem ipsum"
        fake_file = self.mox.CreateMockAnything()
        fake_file.read().AndReturn(fake_contents)
        __builtin__.open(mox.IgnoreArg()).AndReturn(fake_file)

        self.mox.ReplayAll()
        cache_data = {"data": 1123, "mtime": 1}
        data = utils.read_cached_file("/this/is/a/fake", cache_data)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        self.assertEqual(data, fake_contents)
Exemplo n.º 16
0
    def _cell_data_sync(self, force=False):
        """Update cell status for all cells from the backing data store
        when necessary.

        :param force: If True, cell status will be updated regardless
                      of whether it's time to do so.
        """
        reloaded, data = utils.read_cached_file(self.cells_config_path, force_reload=force)

        if reloaded:
            LOG.debug("Updating cell cache from config file.")
            self.cells_config_data = jsonutils.loads(data)
            self._refresh_cells_from_dict(self.cells_config_data)

        if force or self._time_to_sync():
            self.last_cell_db_check = timeutils.utcnow()
            self._update_our_capacity()
Exemplo n.º 17
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        self.mox.StubOutWithMock(__builtin__, 'open')

        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)

        fake_contents = "lorem ipsum"
        fake_file = self.mox.CreateMockAnything()
        fake_file.read().AndReturn(fake_contents)
        __builtin__.open(mox.IgnoreArg()).AndReturn(fake_file)

        self.mox.ReplayAll()
        cache_data = {"data": 1123, "mtime": 1}
        data = utils.read_cached_file("/this/is/a/fake", cache_data)
        self.mox.VerifyAll()
        self.mox.UnsetStubs()
        self.assertEqual(data, fake_contents)
Exemplo n.º 18
0
    def _cell_data_sync(self, force=False):
        """Update cell status for all cells from the backing data store
        when necessary.

        :param force: If True, cell status will be updated regardless
                      of whether it's time to do so.
        """
        reloaded, data = utils.read_cached_file(self.cells_config_path,
                                                force_reload=force)

        if reloaded:
            LOG.debug("Updating cell cache from config file.")
            self.cells_config_data = jsonutils.loads(data)
            self._refresh_cells_from_dict(self.cells_config_data)

        if force or self._time_to_sync():
            self.last_cell_db_check = timeutils.utcnow()
            self._update_our_capacity()
Exemplo n.º 19
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)
        self.mox.ReplayAll()

        fake_contents = "lorem ipsum"
        m = mock.mock_open(read_data=fake_contents)
        with mock.patch("__builtin__.open", m, create=True):
            cache_data = {"data": 1123, "mtime": 1}
            self.reload_called = False

            def test_reload(reloaded_data):
                self.assertEqual(reloaded_data, fake_contents)
                self.reload_called = True

            data = utils.read_cached_file("/this/is/a/fake", cache_data, reload_func=test_reload)
            self.assertEqual(data, fake_contents)
            self.assertTrue(self.reload_called)
Exemplo n.º 20
0
    def test_read_modified_cached_file(self):
        self.mox.StubOutWithMock(os.path, "getmtime")
        os.path.getmtime(mox.IgnoreArg()).AndReturn(2)
        self.mox.ReplayAll()

        fake_contents = "lorem ipsum"
        m = mock.mock_open(read_data=fake_contents)
        with mock.patch("__builtin__.open", m, create=True):
            cache_data = {"data": 1123, "mtime": 1}
            self.reload_called = False

            def test_reload(reloaded_data):
                self.assertEqual(reloaded_data, fake_contents)
                self.reload_called = True

            data = utils.read_cached_file("/this/is/a/fake", cache_data,
                                                    reload_func=test_reload)
            self.assertEqual(data, fake_contents)
            self.assertTrue(self.reload_called)
Exemplo n.º 21
0
def init():
    global _POLICY_PATH
    global _POLICY_CACHE
    if not _POLICY_PATH:
        _POLICY_PATH = utils.find_config(FLAGS.policy_file)
    utils.read_cached_file(_POLICY_PATH, _POLICY_CACHE, reload_func=_set_brain)