Example #1
0
class TestEventRegistrar(TestCase):
    def setUp(self):
        self.registrar = EventRegistrar()

    def test_emit_with_no_handlers(self):
        self.registrar.emit("test")

    def test_handlers_called_in_order(self):
        tracker = []
        handler_1 = lambda: tracker.append(1)
        handler_2 = lambda: tracker.append(2)
        self.registrar.register("test", handler_1)
        self.registrar.register("test", handler_2)

        self.registrar.emit("test")

        self.assertEqual(tracker, [1, 2])

    def test_handler_exception_emits_exception_event(self):
        self.registrar.register("test", _broken_handler)
        self.exception_mock = Mock()
        self.registrar.register("event_handler_exception", self.exception_mock)

        self.registrar.emit("test")

        self.exception_mock.assert_called_once_with("test", ANY)

    def test_broken_exception_event_does_not_infinite_loop(self):
        self.registrar.register("test", _broken_handler)
        self.registrar.register("event_handler_exception", _broken_handler)

        self.registrar.emit("test")
Example #2
0
 def test_kwarg_pass_through_no_kwargs(self):
     call_func = Mock()
     data = sentinel.data
     axis = sentinel.axis
     aggregator = Aggregator("", call_func)
     aggregator.aggregate(data, axis)
     call_func.assert_called_once_with(data, axis=axis)
Example #3
0
 def test_kwarg_pass_through_no_kwargs(self):
     lazy_func = Mock()
     data = sentinel.data
     axis = sentinel.axis
     aggregator = Aggregator("", None, lazy_func=lazy_func)
     aggregator.lazy_aggregate(data, axis)
     lazy_func.assert_called_once_with(data, axis)
    def test_multiple_errbacks(self):
        session = self.make_session()
        pool = session._pools.get.return_value
        connection = Mock(spec=Connection)
        pool.borrow_connection.return_value = (connection, 1)

        query = SimpleStatement("INSERT INFO foo (a, b) VALUES (1, 2)")
        message = QueryMessage(query=query, consistency_level=ConsistencyLevel.ONE)

        retry_policy = Mock()
        retry_policy.on_unavailable.return_value = (RetryPolicy.RETHROW, None)
        rf = ResponseFuture(session, message, query, 1, retry_policy=retry_policy)
        rf.send_request()

        callback = Mock()
        arg = "positional"
        kwargs = {"one": 1, "two": 2}
        rf.add_errback(callback, arg, **kwargs)

        callback2 = Mock()
        arg2 = "another"
        kwargs2 = {"three": 3, "four": 4}
        rf.add_errback(callback2, arg2, **kwargs2)

        expected_exception = Unavailable("message", 1, 2, 3)
        result = Mock(spec=UnavailableErrorMessage, info={"something": "here"})
        result.to_exception.return_value = expected_exception
        rf._set_result(result)
        self.assertRaises(Exception, rf.result)

        callback.assert_called_once_with(expected_exception, arg, **kwargs)
        callback2.assert_called_once_with(expected_exception, arg2, **kwargs2)
Example #5
0
def test_run_in_subprocess():
    with patch.multiple("pytest_shutil.run", cPickle=DEFAULT, execnet=DEFAULT) as mocks:
        fn = Mock(__name__="fn")
        res = run.run_in_subprocess(fn, python="sentinel.python")(sentinel.arg, kw=sentinel.kw)
        mocks["execnet"].makegateway.assert_called_once_with("popen//python=sentinel.python")
        gw = mocks["execnet"].makegateway.return_value
        ((remote_fn,), _) = gw.remote_exec.call_args
        chan = gw.remote_exec.return_value
        mocks["cPickle"].dumps.assert_called_with((fn, (sentinel.arg,), {"kw": sentinel.kw}), protocol=0)
        chan.send.assert_called_with(mocks["cPickle"].dumps.return_value)
        chan.receive.assert_has_calls([call(None) for _i in range(gw.remote_exec.call_count)])
        mocks["cPickle"].loads.assert_called_once_with(chan.receive.return_value)
        assert res is mocks["cPickle"].loads.return_value
        chan.close.assert_has_calls([call() for _i in range(gw.remote_exec.call_count)])
        gw.exit.assert_called_once_with()

    with patch("six.moves.cPickle") as cPickle:
        channel, fn = Mock(), Mock()
        cPickle.loads.return_value = (fn, (sentinel.arg,), {"kw": sentinel.kw})
        remote_fn(channel)
        channel.receive.assert_called_once_with(None)
        cPickle.loads.assert_called_once_with(channel.receive.return_value)
        fn.assert_called_once_with(sentinel.arg, kw=sentinel.kw)
        cPickle.dumps.assert_called_once_with(fn.return_value, protocol=0)
        channel.send.assert_called_once_with(cPickle.dumps.return_value)
 def test_do_log_rl_err(self):
     _mock = Mock()
     pid = os.getpid()
     sleep(1.1)
     with patch("logging.error", _mock):
         fs.do_log_rl("Hello %s", "world", log_level="blah")
     _mock.assert_called_once_with("[PID:" + str(pid) + "][RateLimitedLog;" "Count:1] Hello %s", "world")
    def test_rmtree_ignore_unlink_rmdir_exception(self):
        dir1_list = ["dir2", "file"]
        empty_list = []
        mock_listdir = Mock()
        mock_listdir.side_effect = [dir1_list, empty_list]

        mock_isdir = Mock()
        mock_isdir.side_effect = [True, False]

        mock_unlink = Mock()
        mock_unlink.side_effect = [OSError]

        mock_rmdir = Mock()
        mock_rmdir.side_effect = [0, OSError]

        mock_islink = Mock()
        mock_islink.return_value = False

        with nested(
            patch("gluster.gfapi.Volume.listdir", mock_listdir),
            patch("gluster.gfapi.Volume.isdir", mock_isdir),
            patch("gluster.gfapi.Volume.islink", mock_islink),
            patch("gluster.gfapi.Volume.unlink", mock_unlink),
            patch("gluster.gfapi.Volume.rmdir", mock_rmdir),
        ):
            self.vol.rmtree("dir1", True)
            mock_rmdir.assert_any_call("dir1/dir2")
            mock_unlink.assert_called_once_with("dir1/file")
            mock_rmdir.assert_called_with("dir1")
Example #8
0
    def test_prepare_execute_chef_node_looked_for(self):
        self.dtrs.result = {
            "document": _get_one_node_cluster_doc("node1", "image1"),
            "node": {"ctx_method": "chef", "chef_attributes": {}, "chef_runlist": [], "chef_credential": "chef1"},
        }
        chef_creds = self._get_chef_creds()
        self.dtrs.credentials["chef"][("asterix", "chef1")] = chef_creds
        launch_id = _new_id()
        instance_id = _new_id()
        caller = "asterix"
        launch, nodes = self.core.prepare_provision(
            launch_id=launch_id, deployable_type="foo", instance_ids=[instance_id], site="site1", caller=caller
        )

        mock_chefapi = Mock()
        mock_chefnode = Mock()
        mock_chefnode.list.return_value = {}
        with patch.multiple("chef", ChefAPI=mock_chefapi, Node=mock_chefnode):
            self.core.execute_provision(launch, nodes, caller)
            mock_chefapi.assert_called_once_with(chef_creds["url"], chef_creds["client_key"], chef_creds["client_name"])
            self.assertTrue(mock_chefnode.list.called)
        self.assertTrue(self.notifier.assure_state(states.PENDING))

        node = self.store.get_node(instance_id)
        self.assertEqual(node["chef_credential"], "chef1")
Example #9
0
    def test_terminate_chef_node_default_client_name(self):
        caller = "asterix"
        launch_id = _new_id()
        node_id = _new_id()

        chef_creds = self._get_chef_creds(client_name=None)
        self.dtrs.credentials["chef"][("asterix", "chef1")] = chef_creds

        launch = {"launch_id": launch_id, "node_ids": [node_id], "state": states.PENDING, "creator": caller}
        req_node = {
            "launch_id": launch_id,
            "node_id": node_id,
            "iaas_id": "i-deadbeef",
            "state": states.PENDING,
            "creator": caller,
            "site": "site1",
            "ctx_method": "chef",
            "chef_credential": "chef1",
        }
        self.store.add_launch(launch)
        self.store.add_node(req_node)

        mock_chefapi = Mock()
        mock_chefnode = Mock()
        mock_node = Mock()
        mock_chefnode.return_value = mock_node
        with patch.multiple("chef", ChefAPI=mock_chefapi, Node=mock_chefnode):
            self.core.terminate_nodes([node_id], remove_terminating=False)
        mock_chefapi.assert_called_once_with(chef_creds["url"], chef_creds["client_key"], chefutil.DEFAULT_CLIENT_NAME)
        self.assertTrue(mock_chefnode.called)
        self.assertTrue(mock_node.delete.called)

        node = self.store.get_node(node_id)
        self.assertEqual(node["state"], states.TERMINATED)
    def test_fail_job(self):
        """
        Tests that scheduler is stopped whenever a port is failing.
        """
        port_out = object()
        port_in = Mock(spec=_port_callback, side_effect=RuntimeError("failed!"))
        self.flowmap[port_out] = port_in

        run_deferred = self.scheduler.run(self.clock)
        self.scheduler.send("some item", port_out)

        expected_message = "Job failed on {:s} while processing {:s}".format(str(port_in), "some item")

        from testtools.twistedsupport._runtest import _NoTwistedLogObservers

        with _NoTwistedLogObservers():
            with twistedsupport.CaptureTwistedLogs() as twisted_logs:
                # Trigger queue run.
                self.clock.advance(self.epsilon)
                assert_that(
                    twisted_logs.getDetails(),
                    matchers.MatchesDict(
                        {
                            "twisted-log": matchers.AfterPreprocessing(
                                lambda log: log.as_text(), matchers.Contains(expected_message)
                            )
                        }
                    ),
                )

        port_in.assert_called_once_with("some item", self.scheduler.send)

        matcher = matchers.AfterPreprocessing(lambda f: f.value, matchers.IsInstance(RuntimeError))
        assert_that(run_deferred, twistedsupport.failed(matcher))
Example #11
0
    def test_register_controller_extensions_without_mail(self):
        from pyramid_signup.views import RegisterController
        from pyramid_signup.interfaces import ISURegisterSchema
        from pyramid_signup.interfaces import ISURegisterForm

        self.config.add_route("index", "/")

        request = testing.DummyRequest()

        getUtility = Mock()
        getUtility.return_value = True

        schema = Mock()
        form = Mock()

        self.config.registry.settings["su.require_activation"] = False
        self.config.registry.registerUtility(schema, ISURegisterSchema)
        self.config.registry.registerUtility(form, ISURegisterForm)

        with patch("pyramid_signup.views.get_mailer") as get_mailer:
            RegisterController(request)
            assert not get_mailer.called

        schema.assert_called_once_with()
        assert form.called
Example #12
0
 def test_double_registered_event_called_only_once(self):
     handler = Mock()
     document = Mock()
     self.registrar.register("save", handler)
     self.registrar.register("save", handler)
     self.registrar.apply("save", document)
     handler.assert_called_once_with(document)
Example #13
0
 def sends_escape_byte_sequence(self):
     for pty in (True, False):
         runner = _KeyboardInterruptingRunner(Context())
         mock_stdin = Mock()
         runner.write_proc_stdin = mock_stdin
         runner.run(_, pty=pty)
         mock_stdin.assert_called_once_with(u"\x03")
Example #14
0
    def test_manual_execute_with_ids_ignored_while_in_execute(self):
        waiter = Event()

        long_execute_waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()
            long_execute_waiter.wait(1)
            self.assertTrue(long_execute_waiter.is_set)

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock

        self.create_runner(interval=1)
        self.engine_runner.execute(None)
        waiter.wait(0.3)
        waiter.clear()
        ids = [1, 2, 3]
        self.engine_runner.execute(ids)
        long_execute_waiter.set()
        waiter.wait(0.3)
        self.assertTrue(waiter.is_set)

        self.stop_runner()

        execute_mock.assert_called_once_with(ANY, None)
Example #15
0
    def test_visitors_location_is_derived_from_ip(self):
        user = User.create(name="Joe", email="joe@joe.com", password="12345")
        site = Site.create(user_id=user.id)

        mock_geodata = Mock(name="get_geodata")
        mock_geodata.return_value = {
            "city": "Los Angeles",
            "zipcode": "90001",
            "latitude": "34.05",
            "longitude": "-118.25",
        }

        url = url_for("tracking.register_visit", site_id=site.id)
        wsgi_environment = {"REMOTE_ADDR": "1.2.3.4"}

        with patch.object(app.tracking.views, "get_geodata", mock_geodata):
            with self.client:
                self.client.get(url, environ_overrides=wsgi_environment)

                visits = Visit.query.all()

                mock_geodata.assert_called_once_with("1.2.3.4")
                self.assertEquals(1, len(visits))
                self.assertEquals("Los Angeles, 90001", visits[0].location)
                self.assertEquals("Los Angeles, 90001, 34.05, -118.25", visits[0].location_full)
Example #16
0
    def test_terminate_chef_node_notfound(self):
        caller = "asterix"
        launch_id = _new_id()
        node_id = _new_id()

        chef_creds = self._get_chef_creds()
        self.dtrs.credentials["chef"][("asterix", "chef1")] = chef_creds

        launch = {"launch_id": launch_id, "node_ids": [node_id], "state": states.PENDING, "creator": caller}
        req_node = {
            "launch_id": launch_id,
            "node_id": node_id,
            "iaas_id": "i-deadbeef",
            "state": states.PENDING,
            "creator": caller,
            "site": "site1",
            "ctx_method": "chef",
            "chef_credential": "chef1",
        }
        self.store.add_launch(launch)
        self.store.add_node(req_node)

        mock_chefapi = Mock()
        mock_chefnode = Mock()
        mock_node = Mock()
        mock_chefnode.return_value = mock_node
        mock_node.delete.side_effect = chef.exceptions.ChefServerNotFoundError("notfound")
        with patch.multiple("chef", ChefAPI=mock_chefapi, Node=mock_chefnode):
            self.core.terminate_nodes([node_id], remove_terminating=False)
        mock_chefapi.assert_called_once_with(chef_creds["url"], chef_creds["client_key"], chef_creds["client_name"])
        self.assertTrue(mock_chefnode.called)
        self.assertTrue(mock_node.delete.called)

        node = self.store.get_node(node_id)
        self.assertEqual(node["state"], states.TERMINATED)
Example #17
0
 def test_run_rule_with_side_effect(self, capsys):
     side_effect = Mock()
     settings = Mock(debug=False)
     command = Command()
     main.run_rule(Rule(get_new_command=lambda *_: "new-command", side_effect=side_effect), command, settings)
     assert capsys.readouterr() == ("new-command\n", "")
     side_effect.assert_called_once_with(command, settings)
 def test_configcluagent_call_before_execute(self):
     cluagent = ConfigurableCluAgent({})
     before_execute = Mock()
     cluagent.before_execute = before_execute
     with self.assertRaises(CluException):
         cluagent.run()
     before_execute.assert_called_once_with()
 def test_set_logging_err(self):
     v = Volume("host", "vol")
     v.fs = 12345
     _m_set_logging = Mock(return_value=-1)
     with patch("gluster.gfapi.api.glfs_set_logging", _m_set_logging):
         self.assertRaises(LibgfapiException, v.set_logging, "/dev/null", 7)
         _m_set_logging.assert_called_once_with(v.fs, None, 7)
Example #20
0
    def test_can_get_by_violation_key_name_with_invalid_response(self):
        def search_side_effect(*args, **kwargs):
            kwargs["callback"](Mock(error=None))

        def exception_raiser(*args, **kwargs):
            raise KeyError(args[0])

        self.ES.asyncES = Mock(search=Mock(side_effect=search_side_effect))

        getitem_mock = Mock(__getitem__=Mock(side_effect=exception_raiser))
        hits = [{"_source": getitem_mock}]
        fake_body = {"total": len(hits), "hits": {"hits": hits}}

        utcfromtimestamp_mock = Mock(return_value="datetime")

        callback_mock = Mock(side_effect=self.stop)

        with patch("holmes.search_providers.elastic.loads", return_value=fake_body):
            with patch("holmes.search_providers.elastic.datetime", utcfromtimestamp=utcfromtimestamp_mock):
                with patch("logging.error") as logging_error_mock:
                    response = yield self.ES.get_by_violation_key_name(
                        key_id=1, current_page=1, page_size=10, callback=callback_mock
                    )

        expect(self.ES.asyncES.search.call_count).to_equal(1)
        expect(utcfromtimestamp_mock.call_count).to_equal(0)
        expect(getitem_mock.__getitem__.call_count).to_equal(1)
        logging_error_mock.assert_called_once_with(
            "ElasticSearchProvider: invalid response (<type 'exceptions.KeyError'> [completed_date])"
        )
        callback_mock.assert_called_once_with(response)
Example #21
0
    def test_close_with_callbacks(self):
        m = Mock()
        self.lt.add_on_close_callback(m)

        self.lt.close()

        m.assert_called_once_with(self.lt, 200, "Closed ok")
Example #22
0
    def test_can_get_domain_active_reviews_with_invalid_response_again(self):
        def search_side_effect(*args, **kwargs):
            kwargs["callback"](Mock(error=None))

        def exception_raiser(*args, **kwargs):
            raise TypeError("a float is required")

        self.ES.asyncES = Mock(search=Mock(side_effect=search_side_effect))

        getitem_mock = Mock(__getitem__=Mock(return_value=[]))
        hits = [{"_source": getitem_mock}]
        fake_body = {"total": len(hits), "hits": {"hits": hits}}

        utcfromtimestamp_mock = Mock(return_value="datetime", side_effect=exception_raiser)

        callback_mock = Mock(side_effect=self.stop)

        with patch("holmes.search_providers.elastic.loads", return_value=fake_body):
            with patch("holmes.search_providers.elastic.datetime", utcfromtimestamp=utcfromtimestamp_mock):
                with patch("logging.error") as logging_error_mock:
                    response = yield self.ES.get_domain_active_reviews(
                        domain=Mock(id=359), current_page=1, page_size=10, callback=callback_mock
                    )

        expect(self.ES.asyncES.search.call_count).to_equal(1)
        expect(utcfromtimestamp_mock.call_count).to_equal(1)
        expect(getitem_mock.__getitem__.call_count).to_equal(1)
        logging_error_mock.assert_called_once_with(
            "ElasticSearchProvider: invalid response (<type 'exceptions.TypeError'> [a float is required])"
        )
        callback_mock.assert_called_once_with(response)
Example #23
0
 def test_sucesso(self):
     write_mock = Mock()
     curso = Curso(nome="Foo")
     chave = curso.put()
     crud.detalhar(write_mock, str(chave.id()))
     write_mock.assert_called_once_with("curso_detalhe.html", {"curso": curso})
     tmpl.render("curso_detalhe.html", {"curso": curso, "_path": "/"})
Example #24
0
    def test_can_save_requests(self):
        self.db.query(Request).delete()

        domain = DomainFactory.create(name="t.com")
        page = PageFactory.create(domain=domain, url="http://t.com/a.html")

        requests = []
        for x in range(3):
            url = "http://t.com/file%d.html" % x
            response_mock = Mock(status_code=100 * x, text="OK", request_time=0.1 * x, effective_url=url)
            requests.append((url, response_mock))

        publish = Mock()

        Request.save_requests(self.db, publish, page, requests)

        loaded_requests = self.db.query(Request).all()

        expect(loaded_requests).to_length(3)

        for idx, request in enumerate(loaded_requests):
            expect(request.url).to_equal("http://t.com/file%d.html" % idx)
            expect(request.status_code).to_equal(100 * idx)
            expect(request.response_time).to_equal(0.1 * idx)
            expect(request.domain_name).to_equal("t.com")
            expect(request.review_url).to_equal("http://t.com/a.html")

        expect(publish.called).to_be_true()

        publish.assert_called_once_with(dumps({"url": url, "type": "new-request"}))
Example #25
0
    def test_command_has_no_sub___action_from_command_is_called(
        self, action_res, other_action_res, sub_parser_names, command_name
    ):
        selected = choice(sub_parser_names)

        action_mock = Mock(return_value=action_res)
        other_action_mock = Mock(return_value=other_action_res)

        class SubCmd(cmd.Base):
            def action(self, args):
                return action_mock(args)

        class OtherSubCmd(cmd.Base):
            def action(self, args):
                return other_action_mock(args)

        class Cmd(cmd.Base):
            sub_commands = dict((n, (SubCmd if n == selected else OtherSubCmd)) for n in sub_parser_names)

        sys.argv = ["foo", selected]

        command = Cmd(command_name)
        res = command.run()

        self.assertEqual(res, action_res)
        action_mock.assert_called_once_with(command.parse_args())
        other_action_mock.assert_not_called()
Example #26
0
    def test_accept(self):
        rmock = Mock()
        rmock.return_value = sentinel.msg

        cacmock = Mock()
        transport = Mock()

        self.ch.recv = rmock
        self.ch._recv_queue.await_n = MagicMock()
        self.ch._create_accepted_channel = cacmock
        self.ch.on_channel_open(transport)
        self.ch._fsm.current_state = self.ch.S_ACTIVE
        self.ch._consuming = True

        retch = self.ch.accept()
        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACCEPTED)
        cacmock.assert_called_once_with(transport, [sentinel.msg])
        retch._recv_queue.put.assert_called_once_with(sentinel.msg)

        # we've mocked all the working machinery of accept's return etc, so we must manually exit accept
        # as if we've ack'd/reject'd
        self.ch.exit_accept()

        self.assertEquals(self.ch._fsm.current_state, self.ch.S_ACTIVE)
        self.assertTrue(self.ch._consuming)
Example #27
0
 def test_mdtol_intercept(self):
     call_func = Mock()
     data = sentinel.data
     axis = sentinel.axis
     aggregator = Aggregator("", call_func)
     aggregator.aggregate(data, axis, wibble="wobble", mdtol=0.8)
     call_func.assert_called_once_with(data, axis=axis, wibble="wobble")
Example #28
0
    def test_register_controller_extensions_without_mail(self):
        from horus.views import RegisterController
        from horus.interfaces import IRegisterSchema
        from horus.interfaces import IRegisterForm
        from horus.interfaces import IUserClass
        from horus.tests.models import User
        from horus.interfaces import IActivationClass
        from horus.tests.models import Activation

        self.config.registry.registerUtility(Activation, IActivationClass)

        self.config.registry.registerUtility(User, IUserClass)

        self.config.add_route("index", "/")

        request = testing.DummyRequest()

        getUtility = Mock()
        getUtility.return_value = True

        schema = Mock()
        form = Mock()

        self.config.registry.settings["horus.require_activation"] = False
        self.config.registry.registerUtility(schema, IRegisterSchema)
        self.config.registry.registerUtility(form, IRegisterForm)

        with patch("horus.views.get_mailer") as get_mailer:
            RegisterController(request)
            assert not get_mailer.called

        schema.assert_called_once_with()
        assert form.called
Example #29
0
 def test_player_credit_passes_data(self):
     activity = {"verb": "test"}
     mock_get = Mock()
     with patch("requests.get", mock_get):
         self.player.credit(activity)
     expected_params = {"do": "create", "data": json.dumps(activity)}
     mock_get.assert_called_once_with(self.credit_url, params=expected_params)
Example #30
0
    def test_simple_handler(self):
        from slamon.agent.executor import Executor
        from slamon.agent.handlers import TaskHandler
        from mock import Mock

        mock_handler = Mock()
        mock_callback = Mock()

        @TaskHandler("test-task", 1)
        def test_handler(input_data):
            mock_handler()
            return input_data

        data = {"data": 1337}

        with Executor() as executor:
            executor.submit_task(
                {
                    "task_id": "123e4567-e89b-12d3-a456-426655440000",
                    "task_type": "test-task",
                    "task_version": 1,
                    "task_data": data,
                },
                mock_callback,
            )

        mock_handler.assert_any_call()
        mock_callback.assert_called_once_with("123e4567-e89b-12d3-a456-426655440000", task_data=data)