class TestAssignments(BambooAgentIntegrationTest):
    Arguments = dict(assignments=[
        dict(type="plan", key="PL"),
        dict(type="project", key="PR")
    ])
    Home = BambooHome().config(aid=1234)
    ExpectChange = True
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
        templates.SearchAssignment.request(etype="PLAN"),
        templates.SearchAssignment.request(etype="PROJECT"),
        templates.Assignments.request(agent_id=1234),
        templates.AddAssignment.request(agent_id=1234, etype="PROJECT", eid=1),
        templates.RemoveAssignment.request(agent_id=1234,
                                           etype="PROJECT",
                                           eid=2),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([dict(id=1234)]),
        templates.SearchAssignment.response([dict(key="PL", id=0)]),
        templates.SearchAssignment.response([dict(key="PR", id=1)]),
        templates.Assignments.response([
            dict(executableType="PROJECT", executableId=2),
            dict(executableType="PLAN", executableId=0),
        ]),
        templates.AddAssignment.response(),
        templates.RemoveAssignment.response(),
    ]
    ExpectedResult = dict(assignments={"0": "PLAN", "1": "PROJECT"})
class TestSetAgentName(BambooAgentIntegrationTest):
    Arguments = dict(name="new-name")
    Home = BambooHome().config(aid=1234)
    ExpectChange = True
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
        templates.SetName.request(agent_id=1234, name="new-name"),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([dict(id=1234, name="old-name")]),
        templates.SetName.response(),
    ]
    ExpectedResult = dict(id=1234, name="new-name")
class TestAgentDelete(BambooAgentIntegrationTest):
    Arguments = dict(deleted=True)
    Home = BambooHome().config(aid=1234)
    ExpectChange = True
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
        templates.Delete.request(agent_id=1234),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([dict(id=1234, enabled=True)]),
        templates.Delete.response(),
    ]
    ExpectedResult = dict(id=1234, deleted=True)
class TestBlockWhileBusy(BambooAgentIntegrationTest):
    Home = BambooHome().config(aid=1234)
    Arguments = dict(block_while_busy=True,
                     timings=dict(interval_busy_polling=0))
    ExpectChange = True
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
        templates.Agents.request(),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([dict(id=1234, busy=True)]),
        templates.Agents.response([dict(id=1234, busy=False)]),
    ]
    ExpectedResult = dict(id=1234, busy=False)
class TestDiff(BambooAgentIntegrationTest):
    Home = BambooHome().config(aid=1234)
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([
            dict(id=1234,
                 name="agent-name",
                 enabled=True,
                 busy=False,
                 active=True)
        ]),
    ]
    ExpectedResult = dict(diff=dict(
        before=textwrap.dedent("""
                {
                    "assignments": {},
                    "authenticated": true,
                    "deleted": false,
                    "info": {
                        "active": true,
                        "busy": false,
                        "enabled": true,
                        "id": 1234,
                        "name": "agent-name"
                    }
                }
                """).strip(),
        after=textwrap.dedent("""
                {
                    "assignments": {},
                    "authenticated": true,
                    "deleted": false,
                    "info": {
                        "active": true,
                        "busy": false,
                        "enabled": true,
                        "id": 1234,
                        "name": "agent-name"
                    }
                }
                """).strip(),
    ))
class TestReturnValues(BambooAgentIntegrationTest):
    Home = BambooHome().config(aid=1234)
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response([
            dict(id=1234,
                 name="agent-name",
                 enabled=True,
                 busy=False,
                 active=True)
        ]),
    ]
    ExpectedResult = dict(id=1234,
                          name="agent-name",
                          enabled=True,
                          busy=False,
                          active=True)
class TestUnchanged(BambooAgentIntegrationTest):
    Arguments = dict(enabled=True,
                     name="agent-name",
                     assignments=[dict(type="plan", key="PL")])
    Home = BambooHome().config(aid=1234)
    ExpectChange = False
    ExpectedRequests = [
        templates.Pending.request(),
        templates.Agents.request(),
        templates.SearchAssignment.request(etype="PLAN"),
        templates.Assignments.request(agent_id=1234),
    ]
    Responses = [
        ActionResponse([]),
        templates.Agents.response(
            [dict(id=1234, enabled=True, name="agent-name")]),
        templates.SearchAssignment.response([dict(key="PL", id=1)]),
        templates.Assignments.response(
            [dict(executableType="PLAN", executableId=1)]),
    ]
    ExpectedResult = dict(id=1234, name="agent-name", enabled=True)
class TestErrorHandling(BambooAgentIntegrationTest):
    Uuid = "00000000-1111-2222-3333-444444444444"
    Home = BambooHome().temp_uuid(Uuid)
    ExpectFailure = True
    ExpectedRequests = [templates.Pending.request()]
    Responses = [ActionResponse(status_code=400)]
 def test_change_state(self):
     rh = MockRequestHandler(responses=[ActionResponse()])
     agent = make_bamboo_agent(rh)
     agent.state.set("key", dict(sub="value"))
     agent.change(Request("/"), state_update=lambda s: s["key"].pop("sub"))
     self.assertEqual(agent.state["key"], dict())