Example #1
1
    def test_handle_error_signal(self):
        if not signals_available:
            # This test requires the blinker lib to run.
            print("Can't test signals without signal support")
            return
        app = Flask(__name__)
        api = flask_restful.Api(app)

        exception = Mock()
        exception.code = 400
        exception.data = {"foo": "bar"}

        recorded = []

        def record(sender, exception):
            recorded.append(exception)

        got_request_exception.connect(record, app)
        try:
            with app.test_request_context("/foo"):
                api.handle_error(exception)
                self.assertEquals(len(recorded), 1)
                self.assertTrue(exception is recorded[0])
        finally:
            got_request_exception.disconnect(record, app)
Example #2
0
 def setUp(self):
     socket = Mock()
     socket.recvfrom = Mock()
     socket.sendto = Mock()
     with patch("socket.socket") as create_socket:
         create_socket.return_value = socket
         self.connection = UDPSocketConnection(("localhost", 1234))
 def test_create_replication_slave(self, mock_load, mock_backup_delete):
     mock_tasks = Mock()
     mock_snapshot = {"dataset": {"snapshot_id": "test-id"}}
     mock_tasks.get_replication_master_snapshot = Mock(return_value=mock_snapshot)
     mock_flavor = Mock()
     with patch.object(models.FreshInstanceTasks, "load", return_value=mock_tasks):
         self.manager.create_instance(
             self.context,
             ["id1"],
             Mock(),
             mock_flavor,
             Mock(),
             None,
             None,
             "mysql",
             "mysql-server",
             2,
             "temp-backup-id",
             None,
             "some_password",
             None,
             Mock(),
             "some-master-id",
             None,
             None,
             None,
             None,
         )
     mock_tasks.get_replication_master_snapshot.assert_called_with(
         self.context, "some-master-id", mock_flavor, "temp-backup-id", replica_number=1
     )
     mock_backup_delete.assert_called_with(self.context, "test-id")
Example #4
0
    def test__green_pidbox_node(self):
        pool = Mock()
        pool.is_green = True
        l = MyKombuConsumer(self.ready_queue, timer=self.timer, pool=pool)
        l.node = Mock()
        controller = find_step(l, consumer.Control)

        class BConsumer(Mock):
            def __enter__(self):
                self.consume()
                return self

            def __exit__(self, *exc_info):
                self.cancel()

        controller.box.node.listen = BConsumer()
        connections = []

        class Connection(object):
            calls = 0

            def __init__(self, obj):
                connections.append(self)
                self.obj = obj
                self.default_channel = self.channel()
                self.closed = False

            def __enter__(self):
                return self

            def __exit__(self, *exc_info):
                self.close()

            def channel(self):
                return Mock()

            def as_uri(self):
                return "dummy://"

            def drain_events(self, **kwargs):
                if not self.calls:
                    self.calls += 1
                    raise socket.timeout()
                self.obj.connection = None
                controller.box._node_shutdown.set()

            def close(self):
                self.closed = True

        l.connection = Mock()
        l.connect = lambda: Connection(obj=l)
        controller = find_step(l, consumer.Control)
        controller.box.loop(l)

        self.assertTrue(controller.box.node.listen.called)
        self.assertTrue(controller.box.consumer)
        controller.box.consumer.consume.assert_called_with()

        self.assertIsNone(l.connection)
        self.assertTrue(connections[0].closed)
Example #5
0
    def test_get_action_schema(self):
        body = {"do_stuff": {}}
        action_schema = Mock()
        action_schema.get = Mock()

        self.controller.get_action_schema(body, action_schema)
        action_schema.get.assert_called_with("do_stuff", {})
Example #6
0
    def test_container_ip_remove_netns_error(
        self, m_netns, m_client, m_get_container_info_or_exit, m_get_pool_or_exit, m_enforce_root
    ):
        """
        Test container_ip_remove when client fails on removing ip from interface

        netns.remove_ip_from_ns_veth raises a CalledProcessError
        Assert that the system then exits and all expected calls are made
        """
        # Set up mock objects
        m_get_container_info_or_exit.return_value = {"Id": 666, "State": {"Running": 1, "Pid": "Pid_info"}}
        ipv6_nets = set()
        ipv6_nets.add(IPNetwork(IPAddress("1::1")))
        m_endpoint = Mock(spec=Endpoint)
        m_endpoint.ipv6_nets = ipv6_nets
        m_client.get_endpoint.return_value = m_endpoint
        err = CalledProcessError(1, m_netns, "Error removing ip")
        m_netns.remove_ip_from_ns_veth.side_effect = err

        # Set up arguments to pass to method under test
        container_name = "container1"
        ip = "1::1"
        interface = "interface"

        # Call method under test expecting a SystemExit
        self.assertRaises(SystemExit, container.container_ip_remove, container_name, ip, interface)

        # Assert
        self.assertTrue(m_enforce_root.called)
        self.assertTrue(m_get_pool_or_exit.called)
        self.assertTrue(m_get_container_info_or_exit.called)
        self.assertTrue(m_client.get_endpoint.called)
        self.assertTrue(m_client.update_endpoint.called)
        self.assertTrue(m_netns.remove_ip_from_ns_veth.called)
        self.assertFalse(m_client.unassign_address.called)
Example #7
0
    def test_container_ip_remove_ipv6(
        self, m_netns, m_client, m_get_container_info_or_exit, m_get_pool_or_exit, m_enforce_root
    ):
        """
        Test for container_ip_remove with an ipv6 ip argument
        """
        # Set up mock objects
        m_get_pool_or_exit.return_value = "pool"
        m_get_container_info_or_exit.return_value = {"Id": 666, "State": {"Running": 1, "Pid": "Pid_info"}}
        ipv6_nets = set()
        ipv6_nets.add(IPNetwork(IPAddress("1::1")))
        m_endpoint = Mock(spec=Endpoint)
        m_endpoint.ipv6_nets = ipv6_nets
        m_client.get_endpoint.return_value = m_endpoint

        # Set up arguments to pass to method under test
        container_name = "container1"
        ip = "1::1"
        interface = "interface"

        # Call method under test
        container.container_ip_remove(container_name, ip, interface)

        # Assert
        m_enforce_root.assert_called_once_with()
        m_get_pool_or_exit.assert_called_once_with(IPAddress(ip))
        m_get_container_info_or_exit.assert_called_once_with(container_name)
        m_client.get_endpoint.assert_called_once_with(
            hostname=utils.hostname, orchestrator_id=utils.ORCHESTRATOR_ID, workload_id=666
        )
        m_client.update_endpoint.assert_called_once_with(m_endpoint)
        m_netns.remove_ip_from_ns_veth.assert_called_once_with("Pid_info", IPAddress(ip), interface)
        m_client.unassign_address.assert_called_once_with("pool", ip)
Example #8
0
    def test_container_remove(self, m_netns, m_client, m_get_container_id, m_enforce_root):
        """
        Test for container_remove of calicoctl container command
        """
        # Set up mock objects
        m_get_container_id.return_value = 666
        ipv4_nets = set()
        ipv4_nets.add(IPNetwork(IPAddress("1.1.1.1")))
        ipv6_nets = set()
        m_endpoint = Mock(spec=Endpoint)
        m_endpoint.ipv4_nets = ipv4_nets
        m_endpoint.ipv6_nets = ipv6_nets
        m_endpoint.endpoint_id = 12
        m_endpoint.name = "eth1234"
        ippool = IPPool("1.1.1.1/24")
        m_client.get_endpoint.return_value = m_endpoint
        m_client.get_ip_pools.return_value = [ippool]

        # Call method under test
        container.container_remove("container1")

        # Assert
        m_enforce_root.assert_called_once_with()
        m_get_container_id.assert_called_once_with("container1")
        m_client.get_endpoint.assert_called_once_with(
            hostname=utils.hostname, orchestrator_id=utils.ORCHESTRATOR_ID, workload_id=666
        )
        self.assertEqual(m_client.unassign_address.call_count, 1)
        m_netns.remove_veth.assert_called_once_with("eth1234")
        m_client.remove_workload.assert_called_once_with(utils.hostname, utils.ORCHESTRATOR_ID, 666)
Example #9
0
 def test_api_base(self):
     app = Mock()
     app.configure_mock(**{"record.side_effect": AttributeError})
     api = flask_restful.Api(app)
     self.assertEquals(api.urls, {})
     self.assertEquals(api.prefix, "")
     self.assertEquals(api.default_mediatype, "application/json")
Example #10
0
 def test_add_shard(
     self,
     mock_find_all,
     mock_load_any_instance,
     mock_check_quotas,
     mock_instance_create,
     mock_update_db,
     mock_task_api_load,
     mock_load_by_uuid,
 ):
     self.cluster.db_info.task_status = ClusterTasks.NONE
     (mock_find_all.return_value.all.return_value) = [
         DBInstance(InstanceTasks.NONE, name="TestInstance1", shard_id="1", id="1", datastore_version_id="1"),
         DBInstance(InstanceTasks.NONE, name="TestInstance2", shard_id="1", id="2", datastore_version_id="1"),
         DBInstance(InstanceTasks.NONE, name="TestInstance3", shard_id="1", id="3", datastore_version_id="1"),
     ]
     mock_datastore_version = Mock()
     mock_datastore_version.manager = "mongodb"
     mock_load_by_uuid.return_value = mock_datastore_version
     mock_task_api = Mock()
     mock_task_api.mongodb_add_shard_cluster.return_value = None
     mock_task_api_load.return_value = mock_task_api
     self.cluster.add_shard()
     mock_update_db.assert_called_with(task_status=ClusterTasks.ADDING_SHARD)
     mock_task_api.mongodb_add_shard_cluster.assert_called
Example #11
0
    def test_recon_fail(self):
        mock_json = Mock()
        mock_json.side_effect = ValueError("error")
        mock_server_type = Mock(spec=ServerType)
        mock_server_type.name = "servertype"
        mock_server_type.is_instance = True

        with patch(BUILTIN_OPEN):
            with patch("json.load", mock_json):
                actual = replication._recon_check(mock_server_type)

        self.assertIsInstance(actual, list)
        self.assertEqual(len(actual), 2)
        actual = [m.metric() for m in actual]

        expected1 = replication.BASE_RESULT.child("servertype.last_replication")
        expected1.value = 0

        self.assertIn(expected1.metric(), actual)
        actual.remove(expected1.metric())

        expected2 = CheckFailure.child(dimensions={"check": expected1.name, "error": "error"})
        expected2.value = Severity.fail

        pprint.pprint(expected2.metric())
        pprint.pprint(actual)
        self.assertIn(expected2.metric(), actual)
        actual.remove(expected2.metric())
    def test_deploy_clone_execute(self):
        # arrange
        deployer = Mock()
        si = Mock()
        template_model = Mock()

        deploy_res = dict()
        deploy_res["vm_path"] = "path"
        deploy_res["vm_name"] = "name"
        deploy_res["uuid"] = "uuid"

        template_model.template_name = "temp name"
        template_model.vm_folder = "temp folder"
        deployer.deploy_from_template = Mock(return_value=deploy_res)

        template_resource_model = vCenterVMFromTemplateResourceModel()

        deploy_params = DeployFromTemplateDetails(template_resource_model, "VM Deployment")

        deploy_command = DeployCommand(deployer)

        resource_context = Mock()

        # act
        result = deploy_command.execute_deploy_clone_from_vm(
            si=si, logger=Mock(), vcenter_data_model=Mock(), deployment_params=deploy_params
        )

        # assert
        self.assertTrue(result)
        self.assertTrue(deployer.deploy_from_template.called_with(si, deploy_params, resource_context))
    def test_deploy_image_execute(self):
        deployer = Mock()
        si = Mock()
        deployment_params = Mock()
        connectivity = Mock()
        res = Mock()
        deployer.deploy_from_image = Mock(return_value=res)
        session = Mock()
        vcenter_data_model = Mock()

        deploy_command = DeployCommand(deployer)

        # act
        result = deploy_command.execute_deploy_from_image(
            si=si,
            logger=Mock(),
            session=session,
            vcenter_data_model=vcenter_data_model,
            deployment_params=deployment_params,
            resource_context=connectivity,
        )

        # assert
        self.assertTrue(result)
        self.assertTrue(deployer.deploy_from_image.called_with(si, deployment_params, connectivity))
Example #14
0
    def test_post_reply(self):
        # Create a Tweet to reply to.
        Tweet.objects.create(pk=1, raw_json="{}", locale="en", created=datetime.now())

        # Create a request and mock all the required properties and methods.
        request = RequestFactory().post(
            reverse("customercare.twitter_post"), {"reply_to": 1, "content": "@foobar try Aurora! #fxhelp"}
        )
        request.session = {}
        request.twitter = Mock()
        request.twitter.authed = True
        request.twitter.api = Mock()
        return_value = Mock()
        return_value.__dict__ = {"id": 123456790, "text": "@foobar try Aurora! #fxhelp", "created_at": datetime.now()}
        return_value.author = Mock()
        return_value.author.__dict__ = {
            "lang": "en",
            "id": 42,
            "screen_name": "r1cky",
            "profile_image_url": "http://example.com/profile.jpg",
            "profile_image_url_https": "https://example.com/profile.jpg",
        }
        request.twitter.api.update_status.return_value = return_value

        # Pass the request to the view and verify response.
        response = twitter_post(request)
        eq_(200, response.status_code)

        # Verify the reply was inserted with the right data.
        reply = Reply.objects.all()[0]
        eq_("r1cky", reply.twitter_username)
        eq_(1, reply.reply_to_tweet_id)
        eq_("@foobar try Aurora! #fxhelp", json.loads(reply.raw_json)["text"])
Example #15
0
    def test_poll_export_with_unescaped_characters_xml(self):
        """
        Make sure that poll_module will export fine if its xml contains
        unescaped characters.
        """
        module_system = DummySystem(load_error_modules=True)
        id_generator = Mock()
        id_generator.target_course_id = self.xmodule.course_id
        sample_poll_xml = """
        <poll_question display_name="Poll Question">
            <p>How old are you?</p>
            <answer id="less18">18</answer>
        </poll_question>
        """

        output = PollDescriptor.from_xml(sample_poll_xml, module_system, id_generator)
        # Update the answer with invalid character.
        invalid_characters_poll_answer = output.answers[0]
        # Invalid less-than character.
        invalid_characters_poll_answer["text"] = "< 18"
        output.answers[0] = invalid_characters_poll_answer
        output.save()

        xml = output.definition_to_xml(None)
        # Extract texts of all children.
        child_texts = xml.xpath("//text()")
        # Last index of child_texts contains text of answer tag.
        self.assertEqual(child_texts[-1], "< 18")
Example #16
0
 def test_input(self):
     bbio_gpio = Mock()
     adapter = GPIO.AdafruitBBIOAdapter(bbio_gpio)
     bbio_gpio.input = Mock(return_value=True)
     val = adapter.input(1)
     self.assertTrue(val)
     bbio_gpio.input.assert_called_with(1)
Example #17
0
 def test_input(self):
     rpi_gpio = Mock()
     adapter = GPIO.RPiGPIOAdapter(rpi_gpio)
     rpi_gpio.input = Mock(return_value=True)
     val = adapter.input(1)
     self.assertTrue(val)
     rpi_gpio.input.assert_called_with(1)
Example #18
0
    def test_channel_pool_release(self):
        ncm = Mock()
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid

        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch = self._node.channel(BidirClientChannel)

        # return value is not a mock factory - it returns the same mock instance as we declared above
        # so redeclare it so they get unique chids
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid2
        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch2 = self._node.channel(BidirClientChannel)

        self.assertEquals(ch.get_channel_id(), sentinel.chid)
        self.assertEquals(ch2.get_channel_id(), sentinel.chid2)

        # return ch to the pool
        with patch("pyon.net.messaging.log"):
            self._node.on_channel_request_close(ch)

        # expect to have bidir pool of two, pool map of 1
        self.assertEquals(len(self._node._bidir_pool), 2)
        self.assertEquals(len(self._node._pool_map), 1)

        # ch2 still active so it should be in the pool map
        self.assertIn(sentinel.chid2, self._node._pool_map)
Example #19
0
    def test_container_remove_veth_error(self, m_client, m_get_container_id, m_enforce_root, m_remove_veth):
        """
        Test for container_remove when remove_veth throws an error

        Assert that the system exits and workload is not removed.
        """
        # Set up mock objects
        ipv4_nets = set()
        ipv4_nets.add(IPNetwork(IPAddress("1.1.1.1")))
        ipv6_nets = set()
        m_endpoint = Mock(spec=Endpoint)
        m_endpoint.ipv4_nets = ipv4_nets
        m_endpoint.ipv6_nets = ipv6_nets
        m_endpoint.endpoint_id = 12
        m_endpoint.name = "eth1234"
        ippool = IPPool("1.1.1.1/24")
        m_client.get_endpoint.return_value = m_endpoint
        m_client.get_ip_pools.return_value = [ippool]
        m_get_container_id.return_value = 52
        m_remove_veth.side_effect = CalledProcessError(1, "test")

        # Call function under test expecting a SystemExit
        self.assertRaises(SystemExit, container.container_remove, "container1")

        # Assert
        m_enforce_root.assert_called_once_with()
        m_get_container_id.assert_called_once_with("container1")
        m_client.get_endpoint.assert_called_once_with(
            hostname=utils.hostname, orchestrator_id=utils.ORCHESTRATOR_ID, workload_id=52
        )
        m_client.get_ip_pools.assert_called_once_with(4)
        self.assertFalse(m_client.remove_workload.called)
Example #20
0
    def test_channel_pool_release_reacquire(self, bcbmock):
        ncm = Mock()
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid

        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch = self._node.channel(BidirClientChannel)

        # return value is not a mock factory - it returns the same mock instance as we declared above
        # so redeclare it so they get unique chids
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid2
        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch2 = self._node.channel(BidirClientChannel)

        self.assertEquals(ch.get_channel_id(), sentinel.chid)
        self.assertEquals(ch2.get_channel_id(), sentinel.chid2)

        # return ch to the pool
        with patch("pyon.net.messaging.log"):
            self._node.on_channel_request_close(ch)

        # reacquire ch
        call_count = ncm.call_count
        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch3 = self._node.channel(BidirClientChannel)
            # no new calls to the create method have been made
            self.assertEquals(ncm.call_count, call_count)

        # we got the first mocked channel back
        self.assertEquals(ch3.get_channel_id(), sentinel.chid)
Example #21
0
    def test_container_ip_remove_ip_not_assigned(
        self, m_client, m_get_container_info_or_exit, m_get_pool_or_exit, m_enforce_root
    ):
        """
        Test container_ip_remove when an IP address is not assigned to a container

        client.get_endpoint returns an endpoint with no ip nets
        Assert that the system then exits and all expected calls are made
        """
        # Set up mock objects
        m_get_container_info_or_exit.return_value = {"Id": 666, "State": {"Running": 1, "Pid": "Pid_info"}}
        ipv6_nets = set()
        m_endpoint = Mock(spec=Endpoint)
        m_endpoint.ipv6_nets = ipv6_nets
        m_client.get_endpoint.return_value = m_endpoint

        # Set up arguments to pass to method under test
        container_name = "container1"
        ip = "1::1"
        interface = "interface"

        # Call method under test expecting a SystemExit
        self.assertRaises(SystemExit, container.container_ip_remove, container_name, ip, interface)

        # Assert
        self.assertTrue(m_enforce_root.called)
        self.assertTrue(m_get_pool_or_exit.called)
        self.assertTrue(m_get_container_info_or_exit.called)
        self.assertTrue(m_client.get_endpoint.called)
        self.assertFalse(m_client.update_endpoint.called)
Example #22
0
    def test_channel_pool(self):
        ncm = Mock()
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid

        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch = self._node.channel(BidirClientChannel)

            # should expect to see this show up in the node's mappings
            self.assertIn(ch, self._node._bidir_pool.itervalues())
            self.assertIn(sentinel.chid, self._node._pool_map)
            self.assertEquals(len(self._node._pool_map), 1)
            self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool))

        # let's grab another one to watch our pool grow
        # return value is not a mock factory - it returns the same mock instance as we declared above
        # so redeclare it so they get unique chids
        ncm.return_value = Mock(spec=BidirClientChannel)
        ncm.return_value._queue_auto_delete = False
        ncm.return_value.get_channel_id.return_value = sentinel.chid2

        with patch("pyon.net.messaging.NodeB._new_channel", ncm):
            ch2 = self._node.channel(BidirClientChannel)

        self.assertEquals(ch.get_channel_id(), sentinel.chid)
        self.assertEquals(ch2.get_channel_id(), sentinel.chid2)
        self.assertNotEqual(ch, ch2)
        self.assertIn(ch2, self._node._bidir_pool.itervalues())
        self.assertIn(sentinel.chid2, self._node._pool_map)
        self.assertEquals(len(self._node._pool_map), 2)
        self.assertEquals(len(self._node._pool_map), len(self._node._bidir_pool))
Example #23
0
    def test_pool_restart_relaod_modules(self):
        consumer = Consumer()
        consumer.controller = _WC(app=current_app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        current_app.conf.CELERYD_POOL_RESTARTS = True
        try:
            with patch.dict(sys.modules, {"foo": None}):
                panel.handle("pool_restart", {"modules": ["foo"], "reload": False, "reloader": _reload})

                self.assertTrue(consumer.controller.pool.restart.called)
                self.assertFalse(_reload.called)
                self.assertFalse(_import.called)

                _import.reset_mock()
                _reload.reset_mock()
                consumer.controller.pool.restart.reset_mock()

                panel.handle("pool_restart", {"modules": ["foo"], "reload": True, "reloader": _reload})

                self.assertTrue(consumer.controller.pool.restart.called)
                self.assertTrue(_reload.called)
                self.assertFalse(_import.called)
        finally:
            current_app.conf.CELERYD_POOL_RESTARTS = False
    def testBasic(self):
        s = JSONPropertiesDownload("props.json")
        s.build = Mock()
        props = Properties()
        props.setProperty("key1", "value1", "test")
        s.build.getProperties.return_value = props
        s.build.getSlaveCommandVersion.return_value = 1
        ss = Mock()
        ss.asDict.return_value = dict(revision="12345")
        s.build.getSourceStamp.return_value = ss

        s.step_status = Mock()
        s.buildslave = Mock()
        s.remote = Mock()

        s.start()

        for c in s.remote.method_calls:
            name, command, args = c
            commandName = command[3]
            kwargs = command[-1]
            if commandName == "downloadFile":
                self.assertEquals(kwargs["slavedest"], "props.json")
                reader = kwargs["reader"]
                data = reader.remote_read(100)
                self.assertEquals(data, json.dumps(dict(sourcestamp=ss.asDict(), properties={"key1": "value1"})))
                break
        else:
            self.assert_(False, "No downloadFile command found")
Example #25
0
 def create_mock_scheduled_tasks(cls):
     jobs = []
     for name in ["foo", "bar", "baz"]:
         job = Mock()
         job.key = JobKey(role=cls.TEST_ROLE, environment=cls.TEST_ENV, name=name)
         job.failure_count = 0
         job.assignedTask = Mock(spec=AssignedTask)
         job.assignedTask.slaveHost = "slavehost"
         job.assignedTask.task = Mock(spec=TaskConfig)
         job.assignedTask.task.maxTaskFailures = 1
         job.assignedTask.task.executorConfig = Mock(spec=ExecutorConfig)
         job.assignedTask.task.executorConfig.data = Mock()
         job.assignedTask.task.packages = []
         job.assignedTask.task.owner = Identity(role="mchucarroll")
         job.assignedTask.task.environment = "test"
         job.assignedTask.task.jobName = "woops"
         job.assignedTask.task.numCpus = 2
         job.assignedTask.task.ramMb = 2
         job.assignedTask.task.diskMb = 2
         job.assignedTask.instanceId = 4237894
         job.assignedTask.assignedPorts = None
         job.status = ScheduleStatus.RUNNING
         mockEvent = Mock(spec=TaskEvent)
         mockEvent.timestamp = 28234726395
         mockEvent.status = ScheduleStatus.RUNNING
         mockEvent.message = "Hi there"
         job.taskEvents = [mockEvent]
         jobs.append(job)
     return jobs
Example #26
0
    def test__build_instances(self, *args):
        cluster = Mock()
        cluster.instances = []
        cluster.instances.append(Mock())
        cluster.instances.append(Mock())
        cluster.instances.append(Mock())
        cluster.instances[0].type = "configsvr"
        cluster.instances[0].get_visible_ip_addresses = lambda: ["1.2.3.4"]
        cluster.instances[0].datastore_version.manager = "mongodb"
        cluster.instances[1].type = "query_router"
        cluster.instances[1].get_visible_ip_addresses = lambda: ["1.2.3.4"]
        cluster.instances[1].datastore_version.manager = "mongodb"
        cluster.instances[2].type = "member"
        cluster.instances[2].get_visible_ip_addresses = lambda: ["1.2.3.4"]
        cluster.instances[2].datastore_version.manager = "mongodb"

        def test_case(
            ip_to_be_published_for,
            instance_dict_to_be_published_for,
            number_of_ip_published,
            number_of_instance_dict_published,
        ):
            view = ClusterView(cluster, MagicMock())
            instances, ip_list = view._build_instances(ip_to_be_published_for, instance_dict_to_be_published_for)

            self.assertEqual(number_of_ip_published, len(ip_list))
            self.assertEqual(number_of_instance_dict_published, len(instances))

        test_case([], [], 0, 0)
        test_case(["abc"], ["def"], 0, 0)
        test_case(["query_router"], ["member"], 1, 1)
        test_case(["query_router"], ["query_router", "configsvr", "member"], 1, 3)
        test_case(["query_router", "member"], ["member"], 2, 1)
Example #27
0
    def test_process_initializer(self, set_mp_process_title, _signals):
        from celery import Celery
        from celery import signals
        from celery._state import _tls
        from celery.concurrency.processes import process_initializer
        from celery.concurrency.processes import WORKER_SIGRESET, WORKER_SIGIGNORE

        def on_worker_process_init(**kwargs):
            on_worker_process_init.called = True

        on_worker_process_init.called = False
        signals.worker_process_init.connect(on_worker_process_init)

        loader = Mock()
        loader.override_backends = {}
        app = Celery(loader=loader, set_as_current=False)
        app.loader = loader
        app.conf = AttributeDict(DEFAULTS)
        process_initializer(app, "awesome.worker.com")
        _signals.ignore.assert_any_call(*WORKER_SIGIGNORE)
        _signals.reset.assert_any_call(*WORKER_SIGRESET)
        self.assertTrue(app.loader.init_worker.call_count)
        self.assertTrue(on_worker_process_init.called)
        self.assertIs(_tls.current_app, app)
        set_mp_process_title.assert_called_with("celeryd", hostname="awesome.worker.com")
Example #28
0
def __one_bad_two_ok(*args):
    response = Mock()
    response.ok = args[0] == "http://example.com/oembed2"
    response.text = "hello, world"
    response.headers = {"content-type": "application/json"}

    return response
Example #29
0
    def test_annotate_decorator(self):
        from celery.app.task import Task

        class adX(Task):
            abstract = True

            def run(self, y, z, x):
                return y, z, x

        check = Mock()

        def deco(fun):
            def _inner(*args, **kwargs):
                check(*args, **kwargs)
                return fun(*args, **kwargs)

            return _inner

        app = Celery(set_as_current=False)
        app.conf.CELERY_ANNOTATIONS = {adX.name: {"@__call__": deco}}
        adX.bind(app)
        self.assertIs(adX.app, app)

        i = adX()
        i(2, 4, x=3)
        check.assert_called_with(i, 2, 4, x=3)

        i.annotate()
        i.annotate()
Example #30
0
 def setUp(self):
     socket = Mock()
     socket.recv = Mock()
     socket.send = Mock()
     with patch("socket.create_connection") as create_connection:
         create_connection.return_value = socket
         self.connection = TCPSocketConnection(("localhost", 1234))