Example #1
0
    def test_generating_agent_policy_checks(self):
        from .tasks import generate_agent_checks_task

        # setup data
        policy = baker.make("automation.Policy", active=True)
        checks = self.create_checks(policy=policy)
        agent = baker.make_recipe("agents.agent", policy=policy)

        # test policy assigned to agent
        generate_agent_checks_task(policy=policy.id)  # type: ignore

        # make sure all checks were created. should be 7
        agent_checks = Agent.objects.get(pk=agent.id).agentchecks.all()
        self.assertEquals(len(agent_checks), 7)

        # make sure checks were copied correctly
        for check in agent_checks:

            self.assertTrue(check.managed_by_policy)
            if check.check_type == "diskspace":
                self.assertEqual(check.parent_check, checks[0].id)
                self.assertEqual(check.disk, checks[0].disk)
                self.assertEqual(check.error_threshold,
                                 checks[0].error_threshold)
                self.assertEqual(check.warning_threshold,
                                 checks[0].warning_threshold)
            elif check.check_type == "ping":
                self.assertEqual(check.parent_check, checks[1].id)
                self.assertEqual(check.ip, checks[1].ip)
            elif check.check_type == "cpuload":
                self.assertEqual(check.parent_check, checks[2].id)
                self.assertEqual(check.error_threshold,
                                 checks[2].error_threshold)
                self.assertEqual(check.warning_threshold,
                                 checks[2].warning_threshold)
            elif check.check_type == "memory":
                self.assertEqual(check.parent_check, checks[3].id)
                self.assertEqual(check.error_threshold,
                                 checks[3].error_threshold)
                self.assertEqual(check.warning_threshold,
                                 checks[3].warning_threshold)
            elif check.check_type == "winsvc":
                self.assertEqual(check.parent_check, checks[4].id)
                self.assertEqual(check.svc_name, checks[4].svc_name)
                self.assertEqual(check.svc_display_name,
                                 checks[4].svc_display_name)
                self.assertEqual(check.svc_policy_mode,
                                 checks[4].svc_policy_mode)
            elif check.check_type == "script":
                self.assertEqual(check.parent_check, checks[5].id)
                self.assertEqual(check.script, checks[5].script)
            elif check.check_type == "eventlog":
                self.assertEqual(check.parent_check, checks[6].id)
                self.assertEqual(check.event_id, checks[6].event_id)
                self.assertEqual(check.event_type, checks[6].event_type)
Example #2
0
    def test_generate_agent_checks_with_agentpks(self, generate_checks, generate_tasks):
        from automation.tasks import generate_agent_checks_task

        agents = baker.make_recipe("agents.agent", _quantity=5)

        # reset because creating agents triggers it
        generate_checks.reset_mock()
        generate_tasks.reset_mock()

        generate_agent_checks_task([agent.pk for agent in agents])
        self.assertEquals(generate_checks.call_count, 5)
        generate_tasks.assert_not_called()
        generate_checks.reset_mock()

        generate_agent_checks_task([agent.pk for agent in agents], create_tasks=True)
        self.assertEquals(generate_checks.call_count, 5)
        self.assertEquals(generate_checks.call_count, 5)
Example #3
0
    def test_generating_agent_policy_checks_with_enforced(self):
        from .tasks import generate_agent_checks_task

        # setup data
        policy = baker.make("automation.Policy", active=True, enforced=True)
        script = baker.make_recipe("scripts.script")
        self.create_checks(policy=policy, script=script)
        site = baker.make("clients.Site")
        agent = baker.make_recipe("agents.agent", site=site, policy=policy)
        self.create_checks(agent=agent, script=script)

        generate_agent_checks_task(policy=policy.id, create_tasks=True)  # type: ignore

        # make sure each agent check says overriden_by_policy
        self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 14)
        self.assertEqual(
            Agent.objects.get(pk=agent.id)
            .agentchecks.filter(overriden_by_policy=True)
            .count(),
            7,
        )
Example #4
0
    def test_generating_policy_checks_for_all_agents(self, generate_agent_checks_mock):
        from core.models import CoreSettings

        from .tasks import generate_agent_checks_task

        # setup data
        policy = baker.make("automation.Policy", active=True)
        self.create_checks(policy=policy)

        server_agents = baker.make_recipe("agents.server_agent", _quantity=3)
        workstation_agents = baker.make_recipe("agents.workstation_agent", _quantity=4)
        core = CoreSettings.objects.first()
        core.server_policy = policy
        core.save()

        generate_agent_checks_mock.assert_called_with(all=True, create_tasks=True)
        generate_agent_checks_mock.reset_mock()
        generate_agent_checks_task(all=True, create_tasks=True)

        # all servers should have 7 checks
        for agent in server_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 7)

        for agent in workstation_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 0)

        core.server_policy = None
        core.workstation_policy = policy
        core.save()

        generate_agent_checks_mock.assert_any_call(all=True, create_tasks=True)
        generate_agent_checks_mock.reset_mock()
        generate_agent_checks_task(all=True, create_tasks=True)

        # all workstations should have 7 checks
        for agent in server_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 0)

        for agent in workstation_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 7)

        core.workstation_policy = None
        core.save()

        generate_agent_checks_mock.assert_called_with(all=True, create_tasks=True)
        generate_agent_checks_mock.reset_mock()
        generate_agent_checks_task(all=True, create_tasks=True)

        # nothing should have the checks
        for agent in server_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 0)

        for agent in workstation_agents:
            self.assertEqual(Agent.objects.get(pk=agent.id).agentchecks.count(), 0)
Example #5
0
    def test_generating_agent_policy_checks_by_location(
            self, generate_agent_checks_mock, create_task):
        from automation.tasks import generate_agent_checks_task

        # setup data
        policy = baker.make("automation.Policy", active=True)
        self.create_checks(policy=policy)

        baker.make("autotasks.AutomatedTask",
                   policy=policy,
                   name=seq("Task"),
                   _quantity=3)

        server_agent = baker.make_recipe("agents.server_agent")
        workstation_agent = baker.make_recipe("agents.workstation_agent")

        # no checks should be preset on agents
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)

        # set workstation policy on client and policy checks should be there
        workstation_agent.client.workstation_policy = policy
        workstation_agent.client.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            client=workstation_agent.client.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            client=workstation_agent.client.pk,
            create_tasks=True,
        )

        # make sure the checks were added
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 7)
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)

        # remove workstation policy from client
        workstation_agent.client.workstation_policy = None
        workstation_agent.client.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            client=workstation_agent.client.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            client=workstation_agent.client.pk,
            create_tasks=True,
        )

        # make sure the checks were removed
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)

        # set server policy on client and policy checks should be there
        server_agent.client.server_policy = policy
        server_agent.client.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            client=server_agent.client.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            client=server_agent.client.pk,
            create_tasks=True,
        )

        # make sure checks were added
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 7)
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)

        # remove server policy from client
        server_agent.client.server_policy = None
        server_agent.client.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            client=server_agent.client.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            client=server_agent.client.pk,
            create_tasks=True,
        )

        # make sure checks were removed
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)

        # set workstation policy on site and policy checks should be there
        workstation_agent.site.workstation_policy = policy
        workstation_agent.site.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            site=workstation_agent.site.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            site=workstation_agent.site.pk,
            create_tasks=True,
        )

        # make sure checks were added on workstation
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 7)
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)

        # remove workstation policy from site
        workstation_agent.site.workstation_policy = None
        workstation_agent.site.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            site=workstation_agent.site.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            site=workstation_agent.site.pk,
            create_tasks=True,
        )

        # make sure checks were removed
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)

        # set server policy on site and policy checks should be there
        server_agent.site.server_policy = policy
        server_agent.site.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            site=server_agent.site.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            site=server_agent.site.pk,
            create_tasks=True,
        )

        # make sure checks were added
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 7)
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)

        # remove server policy from site
        server_agent.site.server_policy = None
        server_agent.site.save()

        # should trigger task in save method on core
        generate_agent_checks_mock.assert_called_with(
            site=server_agent.site.pk,
            create_tasks=True,
        )
        generate_agent_checks_mock.reset_mock()

        generate_agent_checks_task(
            site=server_agent.site.pk,
            create_tasks=True,
        )

        # make sure checks were removed
        self.assertEqual(
            Agent.objects.get(pk=server_agent.id).agentchecks.count(), 0)
        self.assertEqual(
            Agent.objects.get(pk=workstation_agent.id).agentchecks.count(), 0)