Esempio n. 1
0
class NPreservingPolicyTest(unittest.TestCase):
    def setUp(self):

        self.mock_schedule = Mock()
        self.mock_terminate = Mock()

        self.policy = NPreservingPolicy(
            schedule_process_callback=self.mock_schedule, terminate_process_callback=self.mock_terminate
        )

    def test_apply_policy(self):

        parameters = {"preserve_n": 2, "execution_engine_id": "some_ee", "node_exclusive": "hats"}

        self.policy.parameters = parameters

        upids = []
        all_procs = {"pd0": []}

        self.policy.apply_policy(all_procs, upids)

        schedule_call = call(ANY, ANY, configuration=None, execution_engine_id="some_ee", node_exclusive="hats")

        self.assertEqual(self.mock_schedule.call_args_list, [schedule_call, schedule_call])
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        owner = "fred"
        upids = ["myupid0", "myupid1"]
        definition = None
        state = None

        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state),
                ProcessRecord.new(owner, upids[1], definition, state),
            ]
        }

        parameters["preserve_n"] = 1
        self.policy.parameters = parameters
        self.policy.apply_policy(all_procs, upids)

        self.assertEqual(self.mock_terminate.call_count, 1)
        self.assertEqual(self.mock_schedule.call_count, 0)

    def test_rejected_procs(self):

        parameters = {"preserve_n": 2, "execution_engine_id": "some_ee", "node_exclusive": "hats"}

        self.policy.parameters = parameters

        upids = []
        all_procs = {"pd0": []}

        self.policy.apply_policy(all_procs, upids)

        schedule_call = call(ANY, ANY, configuration=None, execution_engine_id="some_ee", node_exclusive="hats")

        self.assertEqual(self.mock_schedule.call_args_list, [schedule_call, schedule_call])
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        owner = "fred"
        upids = ["myupid0", "myupid1"]
        definition = None
        state_running = ProcessState.RUNNING
        state_rejected = ProcessState.REJECTED

        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_rejected),
            ]
        }

        self.policy.apply_policy(all_procs, upids)
        self.assertEqual(self.mock_schedule.call_count, 1)
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        upids = ["myupid0", "myupid1", "myupid2"]
        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_rejected),
                ProcessRecord.new(owner, upids[2], definition, state_running),
            ]
        }

        self.policy.apply_policy(all_procs, upids)
        self.assertEqual(self.mock_schedule.call_count, 0)
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        parameters["preserve_n"] = 1
        self.policy.parameters = parameters
        self.policy.apply_policy(all_procs, upids)

        self.assertEqual(self.mock_terminate.call_count, 1)
        self.assertEqual(self.mock_schedule.call_count, 0)

    def test_set_status(self):

        self.assertEqual(self.policy.status(), HAState.PENDING)

        owner = "fred"
        upids = ["myupid0", "myupid1"]
        definition = None
        state_running = ProcessState.RUNNING
        state_pending = ProcessState.PENDING

        to_rebalance = 0

        parameters = {"preserve_n": 3}
        self.policy.parameters = parameters

        self.assertEqual(self.policy.status(), HAState.PENDING)

        upids = ["myupid0"]
        all_procs = {"pd0": [ProcessRecord.new(owner, upids[0], definition, state_pending)]}

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.PENDING)

        upids = ["myupid0"]
        all_procs = {"pd0": [ProcessRecord.new(owner, upids[0], definition, state_running)]}

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ["myupid0", "myupid1"]
        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
            ]
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ["myupid0", "myupid1", "myupid2"]
        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
                ProcessRecord.new(owner, upids[2], definition, state_pending),
            ]
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ["myupid0", "myupid1", "myupid2"]
        all_procs = {
            "pd0": [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
                ProcessRecord.new(owner, upids[2], definition, state_running),
            ]
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.STEADY)

        self.policy._status = HAState.FAILED
        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.FAILED)
Esempio n. 2
0
class NPreservingPolicyTest(unittest.TestCase):

    def setUp(self):

        self.mock_schedule = Mock()
        self.mock_terminate = Mock()

        self.policy = NPreservingPolicy(schedule_process_callback=self.mock_schedule,
            terminate_process_callback=self.mock_terminate,)

    def test_apply_policy(self):

        parameters = {
            'preserve_n': 2,
            'execution_engine_id': 'some_ee',
            'node_exclusive': 'hats'
        }

        self.policy.parameters = parameters

        upids = []
        all_procs = {'pd0': []}

        self.policy.apply_policy(all_procs, upids)

        schedule_call = call(ANY, ANY, configuration=None,
            execution_engine_id='some_ee', node_exclusive='hats')

        self.assertEqual(self.mock_schedule.call_args_list, [schedule_call, schedule_call])
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        owner = 'fred'
        upids = ['myupid0', 'myupid1']
        definition = None
        state = None

        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state),
                ProcessRecord.new(owner, upids[1], definition, state),
            ],
        }

        parameters['preserve_n'] = 1
        self.policy.parameters = parameters
        self.policy.apply_policy(all_procs, upids)

        self.assertEqual(self.mock_terminate.call_count, 1)
        self.assertEqual(self.mock_schedule.call_count, 0)

    def test_rejected_procs(self):

        parameters = {
            'preserve_n': 2,
            'execution_engine_id': 'some_ee',
            'node_exclusive': 'hats'
        }

        self.policy.parameters = parameters

        upids = []
        all_procs = {'pd0': []}

        self.policy.apply_policy(all_procs, upids)

        schedule_call = call(ANY, ANY, configuration=None,
            execution_engine_id='some_ee', node_exclusive='hats')

        self.assertEqual(self.mock_schedule.call_args_list, [schedule_call, schedule_call])
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        owner = 'fred'
        upids = ['myupid0', 'myupid1']
        definition = None
        state_running = ProcessState.RUNNING
        state_rejected = ProcessState.REJECTED

        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_rejected),
            ],
        }

        self.policy.apply_policy(all_procs, upids)
        self.assertEqual(self.mock_schedule.call_count, 1)
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        upids = ['myupid0', 'myupid1', 'myupid2']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_rejected),
                ProcessRecord.new(owner, upids[2], definition, state_running),
            ],
        }

        self.policy.apply_policy(all_procs, upids)
        self.assertEqual(self.mock_schedule.call_count, 0)
        self.assertEqual(self.mock_terminate.call_count, 0)
        self.mock_schedule.reset_mock()
        self.mock_terminate.reset_mock()

        parameters['preserve_n'] = 1
        self.policy.parameters = parameters
        self.policy.apply_policy(all_procs, upids)

        self.assertEqual(self.mock_terminate.call_count, 1)
        self.assertEqual(self.mock_schedule.call_count, 0)

    def test_set_status(self):

        self.assertEqual(self.policy.status(), HAState.PENDING)

        owner = 'fred'
        upids = ['myupid0', 'myupid1']
        definition = None
        state_running = ProcessState.RUNNING
        state_pending = ProcessState.PENDING

        to_rebalance = 0

        parameters = {
            'preserve_n': 3,
        }
        self.policy.parameters = parameters

        self.assertEqual(self.policy.status(), HAState.PENDING)

        upids = ['myupid0']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_pending),
            ],
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.PENDING)

        upids = ['myupid0']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
            ],
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ['myupid0', 'myupid1']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
            ],
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ['myupid0', 'myupid1', 'myupid2']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
                ProcessRecord.new(owner, upids[2], definition, state_pending),
            ],
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.READY)

        upids = ['myupid0', 'myupid1', 'myupid2']
        all_procs = {
            'pd0': [
                ProcessRecord.new(owner, upids[0], definition, state_running),
                ProcessRecord.new(owner, upids[1], definition, state_running),
                ProcessRecord.new(owner, upids[2], definition, state_running),
            ],
        }

        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.STEADY)

        self.policy._status = HAState.FAILED
        self.policy._set_status(to_rebalance, upids, all_procs)
        self.assertEqual(self.policy.status(), HAState.FAILED)