Example #1
0
 def test_execute_tasks_task_wait(self, _add_tasks_mock, _get_task_mock, _get_results_mock):
     """Execute tasks waiting for a task to finish."""
     task_1 = Mock()
     task_1.uuid = "test_task_1"
     task_2 = Mock()
     task_2.uuid = "test_task_2"
     _add_tasks_mock.return_value = [task_1, task_2]
     task_3 = Mock()
     task_3.state = "success"
     task_4 = Mock()
     task_4.state = "started"
     task_5 = Mock()
     task_5.state = "success"
     _get_task_mock.side_effect = [task_3, task_4, task_5]
     result1 = Mock()
     result1.tag = json.dumps("test_result_1")
     result2 = Mock()
     result2.tag = json.dumps("test_result_2")
     _get_results_mock.side_effect = [[result1], [result2]]
     tasks = [
         snowfloat.task.Task(operation="test_operation_1", task_filter={"layer_uuid_exact": "test_layer_1"}),
         snowfloat.task.Task(operation="test_operation_2", task_filter={"layer_uuid_exact": "test_layer_2"}),
     ]
     res = self.client.execute_tasks(tasks, interval=0.1)
     self.assertListEqual(res, [["test_result_1"], ["test_result_2"]])
     self.execute_tasks_add_tasks_helper(_add_tasks_mock)
     self.assertEqual(_get_task_mock.call_args_list, [call(task_1.uuid), call(task_2.uuid), call(task_2.uuid)])
     self.assertEqual(_get_results_mock.call_args_list, [call(task_1.uuid), call(task_2.uuid)])
Example #2
0
 def test_execute_tasks_task_failure(self, _add_tasks_mock, _get_task_mock, _get_results_mock):
     """Execute tasks with a task failure."""
     task_1 = Mock()
     task_1.uuid = "test_task_1"
     task_2 = Mock()
     task_2.uuid = "test_task_2"
     _add_tasks_mock.return_value = [task_1, task_2]
     task_3 = Mock()
     task_3.state = "success"
     task_4 = Mock()
     task_4.state = "failure"
     task_4.reason = "test_reason"
     _get_task_mock.side_effect = [task_3, task_4]
     result1 = Mock()
     result1.tag = json.dumps("test_result_1")
     result2 = Mock()
     result2.tag = json.dumps("test_result_2")
     _get_results_mock.side_effect = [[result1], [result2]]
     tasks = [
         snowfloat.task.Task(operation="test_operation_1", task_filter={"layer_uuid_exact": "test_layer_1"}),
         snowfloat.task.Task(operation="test_operation_2", task_filter={"layer_uuid_exact": "test_layer_2"}),
     ]
     res = self.client.execute_tasks(tasks)
     self.assertListEqual(res, [["test_result_1"], {"error": "test_reason"}])
     self.execute_tasks_add_tasks_helper(_add_tasks_mock)
     self.assertEqual(_get_task_mock.call_args_list, [call(task_1.uuid), call(task_2.uuid)])
     self.assertEqual(_get_results_mock.call_args_list, [call(task_1.uuid)])
Example #3
0
    def testGetSuggestedInstancesTwoSameSize(self, getEC2InstancesMock):
        regionMock = Mock(spec="boto.ec2.region.Region")
        regionMock.name = "us-west-2"
        # Instance 1
        instanceMock1 = Mock(spec="boto.ec2.instance.Instance")
        instanceMock1.state = "running"
        instanceMock1.instance_type = "m3.xlarge"
        instanceMock1.launch_time = "2014-05-06T15:17:33.324Z"
        instanceMock1.region = regionMock
        instanceMock1.id = "testId1"
        instanceMock1.tags = {"Name": "testName1"}
        # Instance 2
        instanceMock2 = Mock(spec="boto.ec2.instance.Instance")
        instanceMock2.state = "running"
        instanceMock2.instance_type = "m3.xlarge"
        instanceMock2.launch_time = "2014-05-06T15:18:33.324Z"
        instanceMock2.region = regionMock
        instanceMock2.id = "testId2"
        instanceMock2.tags = {"Name": "testName2"}
        getEC2InstancesMock.return_value = [instanceMock1, instanceMock2]

        suggestions = ec2_utils.getSuggestedInstances(regionMock.name)
        self.assertIsInstance(suggestions, types.GeneratorType)
        suggestions = list(suggestions)

        self.assertSequenceEqual(
            suggestions,
            [
                {"id": "testId1", "name": "testName1", "namespace": "AWS/EC2", "region": regionMock.name},
                {"id": "testId2", "name": "testName2", "namespace": "AWS/EC2", "region": regionMock.name},
            ],
        )
        getEC2InstancesMock.assert_called_once_with(regionMock.name)
Example #4
0
    def test_we_can_get_all_instances_with_region(self, mock_landlord, mock_ec2):
        persistence.save("Instance1", "v43")
        persistence.save("Instance2", "v43")
        mock_landlord.Tenant = StubLandlord
        mock_connection = Mock()
        mock_ec2.connect_to_region.return_value = mock_connection

        instance1 = Mock()
        instance1.id = "i-938372"
        instance1.ip_address = "192.1.11.1"
        instance1.state = "running"
        instance1.launch_time = datetime.date.today().isoformat()
        instance1.tags = {
            "Name": "STAGE-Instance-1",
            "Project": "Instance",
            "Version": "v43",
            "StopTime": "8",
            "StartTime": "6",
        }

        instance2 = Mock()
        instance2.id = "i-542211"
        instance2.state = "stopped"
        instance2.ip_address = None
        instance2.launch_time = datetime.date.today().isoformat()
        instance2.tags = {
            "Name": "STAGE-Instance-2",
            "Project": "Instance",
            "Version": "v43",
            "StopTime": "9",
            "StartTime": "7",
        }

        mock_connection.get_only_instances.return_value = [instance1, instance2]

        instances = ec2.get_all_instances("myRegion")

        mock_ec2.connect_to_region.assert_called_with(
            "myRegion", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret"
        )
        mock_connection.get_only_instances.assert_called_with(filters={"tag:Deployer": "igor"})
        self.assertEquals(len(instances), 2)
        self.assertEquals(instances[0]["name"], "Instance-1")
        self.assertEquals(instances[0]["version"], "v43")
        self.assertEquals(instances[0]["stopTime"], "8")
        self.assertEquals(instances[0]["startTime"], "6")
        self.assertEquals(instances[0]["project"], "Instance")
        self.assertEquals(instances[0]["date"], datetime.date.today().isoformat())
        self.assertEquals(instances[0]["ip"], "192.1.11.1")
        self.assertEquals(instances[0]["status"], "running")
        self.assertEquals(instances[1]["name"], "Instance-2")
        self.assertEquals(instances[1]["version"], "v43")
        self.assertEquals(instances[1]["startTime"], "7")
        self.assertEquals(instances[1]["stopTime"], "9")
        self.assertEquals(instances[1]["project"], "Instance")
        self.assertEquals(instances[1]["date"], datetime.date.today().isoformat())
        self.assertEquals(instances[1]["ip"], "None")
        self.assertEquals(instances[1]["status"], "stopped")
Example #5
0
    def test_we_can_get_all_instances(self, mock_landlord, mock_ec2):
        persistence.save("Instance1", "v43")
        persistence.save("Instance2", "v43")
        mock_landlord.Tenant = StubLandlord
        mock_connection = Mock()
        mock_ec2.connect_to_region.return_value = mock_connection

        instance1 = Mock()
        instance1.id = "i-938372"
        instance1.dns_name = "192.1.11.1.dnsname"
        instance1.ip_address = "192.1.11.1"
        instance1.state = "running"
        instance1.tags = {"Name": "STAGE-Instance-1", "Project": "Instance", "Version": "v43", "AutoStopped": "True"}
        instance1.launch_time = datetime.date.today().isoformat()
        instance1.image_id = "ami-192812"

        instance2 = Mock()
        instance2.id = "i-542211"
        instance2.dns_name = "192.5.5.5.dnsname"
        instance2.ip_address = "192.5.5.5"
        instance2.state = "stopped"
        instance2.tags = {"Name": "Instance2", "Project": "Instance", "Version": "v43"}
        instance2.launch_time = datetime.date.today().isoformat()
        instance2.image_id = "ami-237829"

        mock_connection.get_only_instances.return_value = [instance1, instance2]

        instances = ec2.get_all_instances()

        mock_ec2.connect_to_region.assert_called_with(
            "eu-west-1", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret"
        )
        mock_connection.get_only_instances.assert_called_with(filters={"tag:Deployer": "igor"})
        self.assertEquals(len(instances), 2)
        self.assertEquals(instances[0]["name"], "Instance-1")
        self.assertEquals(instances[0]["version"], "v43")
        self.assertEquals(instances[0]["project"], "Instance")
        self.assertEquals(instances[0]["date"], datetime.date.today().isoformat())
        self.assertEquals(instances[0]["ip"], "192.1.11.1")
        self.assertEquals(instances[0]["status"], "running")
        self.assertEquals(instances[0]["autoStopped"], "True")

        self.assertEquals(instances[1]["name"], "Instance2")
        self.assertEquals(instances[1]["version"], "v43")
        self.assertEquals(instances[1]["project"], "Instance")
        self.assertEquals(instances[1]["date"], datetime.date.today().isoformat())
        self.assertEquals(instances[1]["ip"], "192.5.5.5")
        self.assertEquals(instances[1]["status"], "stopped")
        self.assertEquals(instances[1]["ami"], "ami-237829")
        self.assertEquals(instances[1]["autoStopped"], "NA")
Example #6
0
 def setUp(self):
     self.coreMock = mopidy.core.Core(None, [])
     self.buttonPressEvent = lib.Event()
     playback = Mock()
     playback.mute = self.WithGet(False)
     playback.volume = self.WithGet(50)
     playback.state = self.WithGet(mopidy.core.PlaybackState.STOPPED)
     self.coreMock.playback = playback
Example #7
0
 def test_failed_join_report(self):
     res = Mock()
     ts = self.app.GroupResult(uuid(), [res])
     res.state = states.FAILURE
     res.backend.is_cached.return_value = True
     self.assertIs(next(ts._failed_join_report()), res)
     res.backend.is_cached.return_value = False
     with self.assertRaises(StopIteration):
         next(ts._failed_join_report())
Example #8
0
 def _get_output_for_task_success(self, attempted, succeeded, total, student=None):
     """returns the task_id and the result returned by instructor_task_status()."""
     # view task entry for task in progress
     instructor_task = self._create_progress_entry(student)
     task_id = instructor_task.task_id
     mock_result = Mock()
     mock_result.task_id = task_id
     mock_result.state = SUCCESS
     mock_result.result = {"attempted": attempted, "succeeded": succeeded, "total": total, "action_name": "rescored"}
     output = self._test_get_status_from_result(task_id, mock_result)
     return output
Example #9
0
    def testGetSuggestedInstancesNoRunning(self, getEC2InstancesMock):
        instanceMock1 = Mock(spec="boto.ec2.instance.Instance")
        instanceMock1.state = "stopped"
        getEC2InstancesMock.return_value = [instanceMock1]

        suggestions = ec2_utils.getSuggestedInstances("dummy-region")
        self.assertIsInstance(suggestions, types.GeneratorType)
        suggestions = list(suggestions)

        self.assertSequenceEqual(suggestions, [])
        getEC2InstancesMock.assert_called_once_with("dummy-region")
Example #10
0
 def test_get_status_to_pending(self):
     # get status for a task that hasn't begun to run yet
     instructor_task = self._create_entry()
     task_id = instructor_task.task_id
     mock_result = Mock()
     mock_result.task_id = task_id
     mock_result.state = PENDING
     output = self._test_get_status_from_result(task_id, mock_result)
     for key in ["message", "succeeded", "task_progress"]:
         self.assertTrue(key not in output)
     self.assertEquals(output["task_state"], "PENDING")
     self.assertTrue(output["in_progress"])
Example #11
0
def test_get_ticket_from_issue(ticket):
    issue = Mock(["body", "number", "state", "title"])
    issue.body = "MockBody"
    issue.number = 123
    issue.state = "MockState"
    issue.title = "MockTitle"
    ret = get_ticket_from_issue(issue)
    assert_called_once(
        ticket,
        (),
        {"identifier": 123, "description": "MockBody", "title": "MockTitle", "owner": None, "state": "MockState"},
    )
    assert_equal(ticket.return_value, ret)
Example #12
0
 def test_update_progress_to_revoked(self):
     # view task entry for task in progress that later fails
     instructor_task = self._create_progress_entry()
     task_id = instructor_task.task_id
     mock_result = Mock()
     mock_result.task_id = task_id
     mock_result.state = REVOKED
     output = self._test_get_status_from_result(task_id, mock_result)
     self.assertEquals(output["message"], "Task revoked before running")
     self.assertEquals(output["succeeded"], False)
     self.assertEquals(output["task_state"], REVOKED)
     self.assertFalse(output["in_progress"])
     expected_progress = {"message": "Task revoked before running"}
     self.assertEquals(output["task_progress"], expected_progress)
Example #13
0
 def test_update_progress_to_progress(self):
     # view task entry for task in progress
     instructor_task = self._create_progress_entry()
     task_id = instructor_task.task_id
     mock_result = Mock()
     mock_result.task_id = task_id
     mock_result.state = PROGRESS
     mock_result.result = {"attempted": 5, "succeeded": 4, "total": 10, "action_name": "rescored"}
     output = self._test_get_status_from_result(task_id, mock_result)
     self.assertEquals(output["message"], "Progress: rescored 4 of 5 so far (out of 10)")
     self.assertEquals(output["succeeded"], False)
     self.assertEquals(output["task_state"], PROGRESS)
     self.assertTrue(output["in_progress"])
     self.assertEquals(output["task_progress"], mock_result.result)
Example #14
0
    def test_we_capture_the_checkurl_exception_and_return_false(self, mock_landlord, mock_ec2):
        properties = {"region": "myRegion", "environment": "STAGE", "domain": "this.is.awesome"}
        project = {"name": "MyProject", "version": "v34", "type": "play2"}
        instances_ids = ["blah"]

        mock_landlord.Tenant = StubNameLandlord
        mock_connection = Mock()
        mock_ec2.connect_to_region.return_value = mock_connection

        instance1 = Mock()
        instance1.id = "i-938372"
        instance1.ip_address = "192.1.11.1"
        instance1.state = "running"
        instance1.launch_time = datetime.date.today().isoformat()
        instance1.tags = {"Name": "STAGE-Instance-1", "Project": "Instance", "Version": "v43"}

        instance2 = Mock()
        instance2.id = "i-542211"
        instance2.state = "stopped"
        instance2.ip_address = None
        instance2.launch_time = datetime.date.today().isoformat()
        instance2.tags = {"Name": "STAGE-Instance-2", "Project": "Instance", "Version": "v43"}

        mock_connection.get_only_instances.return_value = [instance1, instance2]

        real_function = ec2.check_url

        ec2.check_url = Mock(
            side_effect=[Exception("BOOM!", "I have created an instance and you are wasting money... muahahaha")]
        )

        result = ec2.is_running(instances_ids, project)

        self.assertEquals(False, result)

        ec2.check_url = real_function
Example #15
0
 def test_execute_tasks(self, _add_tasks_mock, _get_task_mock, _get_results_mock):
     """Execute asynchronous tasks."""
     task_1 = Mock()
     task_1.uuid = "test_task_1"
     task_2 = Mock()
     task_2.uuid = "test_task_2"
     _add_tasks_mock.return_value = [task_1, task_2]
     task_3 = Mock()
     task_3.state = "success"
     task_4 = Mock()
     task_4.state = "success"
     _get_task_mock.side_effect = [task_3, task_4]
     result1 = Mock()
     result1.tag = json.dumps("test_result_1")
     result2 = Mock()
     result2.tag = json.dumps("test_result_2")
     _get_results_mock.side_effect = [[result1], [result2]]
     tasks = [
         snowfloat.task.Task(operation="test_operation_1", task_filter={"layer_uuid_exact": "test_layer_1"}),
         snowfloat.task.Task(operation="test_operation_2", task_filter={"layer_uuid_exact": "test_layer_2"}),
     ]
     res = self.client.execute_tasks(tasks)
     self.assertListEqual(res, [["test_result_1"], ["test_result_2"]])
     self.execute_tasks_add_tasks_helper(_add_tasks_mock)
    def test_znode_watch_triggered_for_child_events_causes_reprocess(self):
        mock_zk = _mock_zk()

        # we're asserting on the side effects of creating the group
        ActiveKazooGroup(mock_zk, DEFAULT_PATH)

        assert mock_zk.get_children_async.call_count == 1

        _, watch_callback = mock_zk.get_children_async.call_args[0]

        mock_watch_event = Mock()
        mock_watch_event.state = KeeperState.CONNECTED
        mock_watch_event.type = EventType.CHILD

        watch_callback(mock_watch_event)

        assert mock_zk.get_children_async.call_count == 2
    def test_znode_watch_triggered_for_deleted_znode_causes_wait_for_it_to_exist(self):
        mock_zk = _mock_zk()

        # we're asserting on the side effects of creating the group
        ActiveKazooGroup(mock_zk, DEFAULT_PATH)

        assert mock_zk.exists_async.call_count == 0

        _, watch_callback = mock_zk.get_children_async.call_args[0]

        mock_watch_event = Mock()
        mock_watch_event.state = KeeperState.CONNECTED
        mock_watch_event.type = EventType.DELETED

        watch_callback(mock_watch_event)

        assert mock_zk.exists_async.call_count == 1
    def test_statejson(self):
        self.config.add_route("result", "/{script}/{taskid}")
        self.request.matchdict["script"] = "plot"
        self.request.matchdict["taskid"] = "b3c84d96-4dc7-4532-a864-3573202f202a"
        views = Views(self.request)
        task_result = Mock(AsyncResult)
        task_result.id = "b3c84d96-4dc7-4532-a864-3573202f202a"
        task_result.state = "PENDING"
        task_result.ready.return_value = False
        task_result.successful.return_value = False
        task_result.failed.return_value = False
        views.celery.AsyncResult = Mock(return_value=task_result)

        result = views.statejson()

        result_url = "/plot/b3c84d96-4dc7-4532-a864-3573202f202a"
        expected_result = {"state": "PENDING", "ready": False, "success": False, "failure": False, "result": result_url}
        self.assertDictEqual(result, expected_result)
Example #19
0
 def test_update_progress_to_failure(self):
     # view task entry for task in progress that later fails
     instructor_task = self._create_progress_entry()
     task_id = instructor_task.task_id
     mock_result = Mock()
     mock_result.task_id = task_id
     mock_result.state = FAILURE
     mock_result.result = NotImplementedError("This task later failed.")
     mock_result.traceback = "random traceback"
     output = self._test_get_status_from_result(task_id, mock_result)
     self.assertEquals(output["message"], "This task later failed.")
     self.assertEquals(output["succeeded"], False)
     self.assertEquals(output["task_state"], FAILURE)
     self.assertFalse(output["in_progress"])
     expected_progress = {
         "exception": "NotImplementedError",
         "message": "This task later failed.",
         "traceback": "random traceback",
     }
     self.assertEquals(output["task_progress"], expected_progress)
def _mock_zk(state=KeeperState.CONNECTED):
    mock_zk = Mock(name="zk", spec=KazooClient)
    mock_zk.state = state

    return mock_zk