# Path: herc/backends/auroramock.py
# class AuroraMock(object):
#     """Mock Aurora backend that returns correct-looking data."""
# 
#     def __init__(self, fakestatuspath = "data/stub_aurora_jobstatus.json"):
#         self.fakestatuspath = fakestatuspath
# 
#     def requestjob(self, jobid, jobrq, vault_api_token):
#         """No return value required; nothing to do."""
#         return None
# 
#     def status(self, jobid):
#         """Returns a good-looking placeholder job status."""
#         with open(self.fakestatuspath, 'r', encoding='utf-8') as fakestatus:
#             statusstring = fakestatus.read()
#             statusjson = json.loads(statusstring)
#             munchifiedstatus = munch.munchify(statusjson)
#             return munchifiedstatus
# 
#     def kill(self, jobid):
#         return { "success" : "Job killed." }


import herc.aurorasched as scheduler
import mock
import munch
import tornado.testing
from herc.backends import AuroraMock
from tornado.testing import gen_test
        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Memory limit exceeded: Requested 128MB, Used 130MB." )
        aurora_status = scheduler.determine_true_status(failed_status)
        self.assertEqual( aurora_status[0], "FAILED" )
        self.assertEqual( aurora_status[1]['reason'], "MEM_EXCEEDED" )
        self.assertEqual( aurora_status[1]['requested'], "128MB" )
        self.assertEqual( aurora_status[1]['used'], "130MB" )

    def test_disk_exceeded(self):
        """Test that we report disk exceeded jobs as such, and correctly pull out requested and used limits"""
        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Disk limit exceeded.  Reserved 1234 bytes vs used 2345 bytes." )
        aurora_status = scheduler.determine_true_status(failed_status)
        self.assertEqual( aurora_status[0], "FAILED" )
        self.assertEqual( aurora_status[1]['reason'], "DISK_EXCEEDED" )
        self.assertEqual( aurora_status[1]['requested'], "1234BYTES" )
        self.assertEqual( aurora_status[1]['used'], "2345BYTES" )

    def test_unexpected_overlimit_string(self):
        """Test that we correctly assert if the overlimit string is unexpected."""
        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Memory limit exceeded: asked for 128MB, spent 130MB." )
        with self.assertRaises(AssertionError):
            scheduler.determine_true_status(failed_status)

    @tornado.testing.gen_test #required because scheduler.status uses @async.usepool
    def test_handle_interleaved_jobcompletion(self):
        """Test that job retries finishing in strange orders return a consistent order."""
        with mock.patch('herc.aurorasched.get_backend') as asc_get_backend:
            #submit times are ascending, but completion times are descending

# Path: herc/backends/auroramock.py
# class AuroraMock(object):
#     """Mock Aurora backend that returns correct-looking data."""
# 
#     def __init__(self, fakestatuspath = "data/stub_aurora_jobstatus.json"):
#         self.fakestatuspath = fakestatuspath
# 
#     def requestjob(self, jobid, jobrq, vault_api_token):
#         """No return value required; nothing to do."""
#         return None
# 
#     def status(self, jobid):
#         """Returns a good-looking placeholder job status."""
#         with open(self.fakestatuspath, 'r', encoding='utf-8') as fakestatus:
#             statusstring = fakestatus.read()
#             statusjson = json.loads(statusstring)
#             munchifiedstatus = munch.munchify(statusjson)
#             return munchifiedstatus
# 
#     def kill(self, jobid):
#         return { "success" : "Job killed." }


# Path: tests/test_scheduler.py
import herc.aurorasched as scheduler
import mock
import munch
import tornado.testing
from herc.backends import AuroraMock
from tornado.testing import gen_test

        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Memory limit exceeded: Requested 128MB, Used 130MB." )
        aurora_status = scheduler.determine_true_status(failed_status)
        self.assertEqual( aurora_status[0], "FAILED" )
        self.assertEqual( aurora_status[1]['reason'], "MEM_EXCEEDED" )
        self.assertEqual( aurora_status[1]['requested'], "128MB" )
        self.assertEqual( aurora_status[1]['used'], "130MB" )

    def test_disk_exceeded(self):
        """Test that we report disk exceeded jobs as such, and correctly pull out requested and used limits"""
        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Disk limit exceeded.  Reserved 1234 bytes vs used 2345 bytes." )
        aurora_status = scheduler.determine_true_status(failed_status)
        self.assertEqual( aurora_status[0], "FAILED" )
        self.assertEqual( aurora_status[1]['reason'], "DISK_EXCEEDED" )
        self.assertEqual( aurora_status[1]['requested'], "1234BYTES" )
        self.assertEqual( aurora_status[1]['used'], "2345BYTES" )

    def test_unexpected_overlimit_string(self):
        """Test that we correctly assert if the overlimit string is unexpected."""
        failed_status = self.build_mock_jobstatus([ "INIT", "PENDING", "ASSIGNED", "RUNNING", "FAILED" ],
                                                  failmsg = "Memory limit exceeded: asked for 128MB, spent 130MB." )
        with self.assertRaises(AssertionError):
            scheduler.determine_true_status(failed_status)

    @tornado.testing.gen_test #required because scheduler.status uses @async.usepool
    def test_handle_interleaved_jobcompletion(self):
        """Test that job retries finishing in strange orders return a consistent order."""
        with mock.patch('herc.aurorasched.get_backend') as asc_get_backend:
            #submit times are ascending, but completion times are descending

            asc_get_backend.return_value = AuroraMock("tests/data/jobstatus_ascending_submittimes.json")