Exemplo n.º 1
0
 def test_get_requires_no_input(self):
     request = self.get()
     status = Status()
     result = status.render(request)
     self.assertEqual(result, NOT_DONE_YET)
     self.assertTrue(request.finished)
     self.assertEqual(request.responseCode, OK)
     self.assertEqual(len(request.written), 1)
     self.assertIsInstance(loads(request.written[0]), dict)
Exemplo n.º 2
0
    def test_get_result(self):
        process = psutil.Process()
        direct_child_processes = len(process.children(recursive=False))
        all_child_processes = len(process.children(recursive=True))
        grandchild_processes = all_child_processes - direct_child_processes

        # Determine the last time we talked to the master (if ever)
        contacted = config.master_contacted(update=False)
        if isinstance(contacted, datetime):
            contacted = datetime.utcnow() - contacted

        # Determine the last time we announced ourselves to the
        # master (if ever)
        last_announce = config.get("last_announce", None)
        if isinstance(last_announce, datetime):
            last_announce = datetime.utcnow() - last_announce

        future_time = config["start"] + 30
        process_memory = memory.process_memory()
        total_consumption = memory.total_consumption()
        expected_data = {
            "state": config["state"],
            "agent_hostname": config["agent_hostname"],
            "agent_process_ram": process_memory,
            "consumed_ram": total_consumption,
            "child_processes": direct_child_processes,
            "grandchild_processes": grandchild_processes,
            "pids": config["pids"],
            "agent_id": str(config["agent_id"]),
            "last_master_contact": contacted,
            "last_announce": last_announce,
            "agent_lock_file": config["agent_lock_file"],
            "free_ram": 4242,
            "uptime": total_seconds(
                timedelta(seconds=future_time - config["start"])),
            "jobs": list(config["jobtypes"].keys())}

        request = self.get()
        status = Status()

        with nested(
            mock.patch.object(memory, "free_ram", return_value=4242),
            mock.patch.object(time, "time", return_value=future_time),
            mock.patch.object(
                memory, "process_memory", return_value=process_memory),
            mock.patch.object(
                memory, "total_consumption", return_value=total_consumption)
        ):
            response = status.render(request)

        self.assertEqual(response, NOT_DONE_YET)
        self.assertTrue(request.finished)
        self.assertEqual(request.responseCode, OK)
        self.assertEqual(len(request.written), 1)
        self.assertEqual(loads(request.written[0]), expected_data)