示例#1
0
    def test_happy_path_valid_json(self, test_client, session, csrf_token):
        agent = AgentFactory.create(workspaces=[self.workspace])
        executor = ExecutorFactory.create(agent=agent)
        executor2 = ExecutorFactory.create(agent=agent)

        session.add(executor)
        session.commit()

        assert agent.last_run is None
        assert executor.last_run is None
        assert executor2.last_run is None

        payload = {
            'csrf_token': csrf_token,
            'executorData': {
                "args": {
                    "param_name": "test"
                },
                "executor": executor.name
            },
        }
        res = test_client.post(urljoin(self.url(agent), 'run'), json=payload)
        assert res.status_code == 200
        command_id = res.json["command_id"]
        command = Command.query.filter(
            Command.workspace_id == self.workspace.id).one()
        assert command_id == command.id
        assert agent.last_run is not None
        assert executor.last_run is not None
        assert executor2.last_run is None
        assert agent.last_run == executor.last_run
示例#2
0
    def test_run_fails(self, test_client, session,csrf_token):
        workspace = WorkspaceFactory.create()
        session.add(workspace)
        other_workspace = WorkspaceFactory.create()
        session.add(other_workspace)
        session.commit()
        agent = AgentFactory.create(
            workspaces=[workspace, other_workspace]
        )
        executor = ExecutorFactory.create(agent=agent)

        session.add(executor)
        session.commit()
        payload = {
            'csrf_token': csrf_token,
            'executorData': {
                "args": {
                    "param1": True
                },
                "executor": executor.name
            },
        }
        res = test_client.post(
            self.url(agent) + 'run/',
            json=payload
        )
        assert res.status_code == 404
    def test_remove_one_of_two_executors(self, session):
        agent = AgentFactory.create()
        executor = ExecutorFactory.create(name='executor 1',
                                          agent=agent,
                                          parameters_metadata={'param1': True})
        session.add(executor)
        another_executor = ExecutorFactory.create(
            name='executor 2',
            agent=agent,
            parameters_metadata={'param2': True})
        session.add(executor)
        session.add(another_executor)
        session.commit()
        executors = [{'executor_name': 'executor 2', 'args': {'param2': True}}]

        assert update_executors(agent, executors)
        count_executors = Executor.query.filter_by(agent=agent).count()
        assert count_executors == 1
        from_db_executor = Executor.query.filter_by(id=another_executor.id,
                                                    agent=agent).first()
        assert from_db_executor.name == 'executor 2'
    def test_remove_all_executors(self, session):
        agent = AgentFactory.create()
        params = {'old_param': True}
        executor = ExecutorFactory.create(name='old_executor',
                                          agent=agent,
                                          parameters_metadata=params)
        session.add(executor)
        session.commit()
        executors = []

        assert update_executors(agent, executors)
        count_executors = Executor.query.filter_by(agent=agent).count()
        assert count_executors == 0
    def test_executor_already_in_database_but_new_parameters_incoming(
            self, session):
        agent = AgentFactory.create()
        old_params = {'old_param': True}
        executor = ExecutorFactory.create(agent=agent,
                                          parameters_metadata=old_params)
        session.add(executor)
        session.commit()
        new_params = old_params
        new_params.update({'param1': True})
        executors = [{'executor_name': executor.name, 'args': new_params}]

        assert update_executors(agent, executors)
        from_db_executor = Executor.query.filter_by(id=executor.id,
                                                    agent=agent).first()
        assert from_db_executor.parameters_metadata == new_params
示例#6
0
    def test_happy_path_valid_json(self, test_client, session, csrf_token):
        agent = AgentFactory.create(workspaces=[self.workspace])
        executor = ExecutorFactory.create(agent=agent)

        session.add(executor)
        session.commit()
        payload = {
            'csrf_token': csrf_token,
            'executorData': {
                "args": {
                    "param1": True
                },
                "executor": executor.name
            },
        }
        res = test_client.post(self.url(agent.id) + 'run/', json=payload)
        assert res.status_code == 200
示例#7
0
    def test_invalid_parameter_type(self, test_client, session, csrf_token):
        agent = AgentFactory.create(workspaces=[self.workspace])
        executor = ExecutorFactory.create(agent=agent)

        session.add(executor)
        session.commit()

        payload = {
            'csrf_token': csrf_token,
            'executorData': {
                "args": {
                    "param_name": ["test"]
                },
                "executor": executor.name
            },
        }
        res = test_client.post(urljoin(self.url(agent), 'run'), json=payload)
        assert res.status_code == 400
示例#8
0
    def test_run_agent_invalid_executor_argument(self, session, test_client):
        agent = AgentFactory.create(workspaces=[self.workspace])
        executor = ExecutorFactory.create(agent=agent)

        session.add(executor)
        session.commit()

        payload = {
            'executorData': {
                "args": {
                    "another_param_name": 'param_content'
                },
                "executor": executor.name
            }
        }

        res = test_client.post(urljoin(self.url(agent), 'run'), json=payload)

        assert res.status_code == 400
    def test_new_executor_and_delete_the_old_one(self, session):
        agent = AgentFactory.create()
        params = {'old_param': True}
        executor = ExecutorFactory.create(name='old_executor',
                                          agent=agent,
                                          parameters_metadata=params)
        session.add(executor)
        session.commit()
        executors = [{
            'executor_name': 'new executor',
            'args': {
                'param1': True
            }
        }]

        assert update_executors(agent, executors)
        count_executors = Executor.query.filter_by(agent=agent).count()
        assert count_executors == 1
        current_executor = Executor.query.filter_by(agent=agent).first()
        assert current_executor.name == 'new executor'
        assert current_executor.parameters_metadata == {'param1': True}