Exemple #1
0
    def test_get_task_status__is_subprocess(self):
        task = MockTaskFlowInstance(
            get_status_raise=TaskFlowInstance.DoesNotExist())

        with mock.patch(TASKINSTANCE_GET, MagicMock(return_value=task)):
            response = self.client.get(path=self.GET_TASK_STATUS_URL.format(
                task_id=TEST_TASKFLOW_ID, bk_biz_id=TEST_BIZ_CC_ID))

            TaskFlowInstance.format_pipeline_status.assert_called_once_with(
                TEST_DATA)

            data = json.loads(response.content)
            self.assertTrue(data['result'])
            self.assertEqual(data['data'], TEST_DATA)
class NodeCallbackAPITest(APITest):
    def url(self):
        return "/apigw/node_callback/{task_id}/{project_id}/"

    @mock.patch(
        PROJECT_GET,
        MagicMock(
            return_value=MockProject(
                project_id=TEST_PROJECT_ID, name=TEST_PROJECT_NAME, bk_biz_id=TEST_BIZ_CC_ID, from_cmdb=True,
            )
        ),
    )
    def test_node_callback__success(self):
        mock_instance = MockTaskFlowInstance()
        with mock.patch(TASKINSTANCE_GET, MagicMock(return_value=mock_instance)):
            response = self.client.post(
                path=self.url().format(task_id=TEST_TASKFLOW_ID, project_id=TEST_PROJECT_ID),
                data=json.dumps({"node_id": TEST_NODE_ID, "callback_data": TEST_CALLBACK_DATA}),
                content_type="application/json",
            )

            data = json.loads(response.content)

            self.assertTrue(data["result"], msg=data)
            mock_instance.callback.assert_called_once_with(TEST_NODE_ID, TEST_CALLBACK_DATA, None)

    @mock.patch(TASKINSTANCE_GET, MagicMock(side_effect=TaskFlowInstance.DoesNotExist()))
    def test_node_callback__taskflow_does_not_exists(self):
        response = self.client.post(
            path=self.url().format(task_id=TEST_TASKFLOW_ID, project_id=TEST_PROJECT_ID),
            data=json.dumps({"node_id": TEST_NODE_ID, "callback_data": TEST_CALLBACK_DATA}),
            content_type="application/json",
        )

        data = json.loads(response.content)

        self.assertFalse(data["result"])
        self.assertTrue("message" in data)
Exemple #3
0
    def test_get_task_status__is_subprocess_raise(self):
        task = MockTaskFlowInstance(
            get_status_raise=TaskFlowInstance.DoesNotExist())

        with mock.patch(TASKINSTANCE_GET, MagicMock(return_value=task)):
            with mock.patch(APIGW_VIEW_PIPELINE_API_GET_STATUS_TREE,
                            MagicMock(side_effect=Exception())):
                response = self.client.get(
                    path=self.GET_TASK_STATUS_URL.format(
                        task_id=TEST_TASKFLOW_ID, bk_biz_id=TEST_BIZ_CC_ID))

                data = json.loads(response.content)
                self.assertFalse(data['result'])
                self.assertTrue('message' in data)

            with mock.patch(TASKINSTANCE_FORMAT_STATUS,
                            MagicMock(side_effect=Exception())):
                response = self.client.get(
                    path=self.GET_TASK_STATUS_URL.format(
                        task_id=TEST_TASKFLOW_ID, bk_biz_id=TEST_BIZ_CC_ID))

                data = json.loads(response.content)
                self.assertFalse(data['result'])
                self.assertTrue('message' in data)
class GetTaskDetailAPITest(APITest):
    def url(self):
        return "/apigw/get_task_detail/{task_id}/{project_id}/"

    @mock.patch(
        PROJECT_GET,
        MagicMock(return_value=MockProject(
            project_id=TEST_PROJECT_ID,
            name=TEST_PROJECT_NAME,
            bk_biz_id=TEST_BIZ_CC_ID,
            from_cmdb=True,
        )),
    )
    def test_get_task_detail__success(self):
        mock_taskflow = MockTaskFlowInstance(get_task_detail_return=TEST_DATA)
        with mock.patch(TASKINSTANCE_GET,
                        MagicMock(return_value=mock_taskflow)):
            assert_data = TEST_DATA
            response = self.client.get(path=self.url().format(
                task_id=TEST_TASKFLOW_ID, project_id=TEST_PROJECT_ID))

            data = json.loads(response.content)

            self.assertTrue(data["result"], msg=data)
            self.assertEqual(data["data"], assert_data)

    @mock.patch(TASKINSTANCE_GET,
                MagicMock(side_effect=TaskFlowInstance.DoesNotExist()))
    def test_get_task_detail__success__taskflow_does_not_exists(self):
        response = self.client.get(path=self.url().format(
            task_id=TEST_TASKFLOW_ID, project_id=TEST_PROJECT_ID))

        data = json.loads(response.content)

        self.assertFalse(data["result"])
        self.assertTrue("message" in data)
Exemple #5
0
class GetTaskNodeDetailAPITest(APITest):
    def url(self):
        return "/apigw/get_task_node_detail/{task_id}/{project_id}/"

    @mock.patch(
        PROJECT_GET,
        MagicMock(return_value=MockProject(
            project_id=TEST_PROJECT_ID,
            name=TEST_PROJECT_NAME,
            bk_biz_id=TEST_BIZ_CC_ID,
            from_cmdb=True,
        )),
    )
    def test_get_task_node_detail__success(self):
        mock_taskflow = MockTaskFlowInstance(get_node_detail_return={
            "result": True,
            "data": TEST_DATA
        })
        with mock.patch(TASKINSTANCE_GET,
                        MagicMock(return_value=mock_taskflow)):
            assert_data = TEST_DATA
            response = self.client.get(
                path=self.url().format(task_id=TEST_TASKFLOW_ID,
                                       project_id=TEST_PROJECT_ID),
                data={
                    "node_id": TEST_NODE_ID,
                    "component_code": TEST_COMPONENT_CODE,
                    "subprocess_stack": TEST_SUBPROCESS_STACK,
                },
            )

            data = json.loads(response.content)

            self.assertTrue(data["result"], msg=data)
            self.assertEqual(data["data"], assert_data)
            mock_taskflow.get_node_detail.assert_called_once_with(
                component_code="component_code",
                node_id="node_id",
                project_id="123",
                subprocess_stack=[1, 2, 3],
                username="",
            )

    @mock.patch(TASKINSTANCE_GET,
                MagicMock(side_effect=TaskFlowInstance.DoesNotExist()))
    def test_get_task_node_detail__taskflow_doest_not_exist(self):
        response = self.client.get(
            path=self.url().format(task_id=TEST_TASKFLOW_ID,
                                   project_id=TEST_PROJECT_ID),
            data={
                "node_id": TEST_NODE_ID,
                "component_code": TEST_COMPONENT_CODE,
                "subprocess_stack": TEST_SUBPROCESS_STACK,
            },
        )

        data = json.loads(response.content)
        self.assertFalse(data["result"])
        self.assertTrue("message" in data)

    def test_get_task_node_detail__with_invalid_subprocess_stack(self):
        response = self.client.get(
            path=self.url().format(task_id=TEST_TASKFLOW_ID,
                                   project_id=TEST_PROJECT_ID),
            data={
                "node_id": TEST_NODE_ID,
                "component_code": TEST_COMPONENT_CODE,
                "subprocess_stack": "abcdefg"
            },
        )

        data = json.loads(response.content)
        self.assertFalse(data["result"])
        self.assertTrue("message" in data)