def test_random_scheduling(self):
        random_patch = mock.patch("random.choice")
        random_mock = random_patch.start()

        def side_effect(seq):
            return seq[0]

        random_mock.side_effect = side_effect

        si = test_utils.create_test_si(name="test-instance", count=2, intf_list=["vn1", "vn2"])
        vm = test_utils.create_test_virtual_machine("vm")

        with mock.patch.object(scheduler.RandomScheduler, "_get_candidates", return_value=["vrouter1", "vrouter2"]):
            chosen_vrouter = self.scheduler.schedule(si, vm)
            self.assertEqual(random_mock.call_count, 1)
            self.assertEqual(chosen_vrouter, "vrouter1")

        random_patch.stop()
    def test_get_candidates(self):
        test_utils.create_test_project('fake-domain:fake-project')
        test_utils.create_test_virtual_network('fake-domain:fake-project:vn1')
        test_utils.create_test_virtual_network('fake-domain:fake-project:vn2')
        test_utils.create_test_security_group('fake-domain:fake-project:default')
        st = test_utils.create_test_st(name='test-template',
            virt_type='network-namespace',
            intf_list=[['right', True], ['left', True]])
        si = test_utils.create_test_si(name='test-instance', count=2,
            intf_list=['vn1', 'vn2'])

        # test anti-affinity
        vr1 = test_utils.create_test_virtual_router('vr-candidate1')
        vr2 = test_utils.create_test_virtual_router('vr-candidate2')
        vm1 = test_utils.create_test_virtual_machine('vm1')
        vm2 = test_utils.create_test_virtual_machine('vm2')
        si.virtual_machines.add(vm1.uuid)
        si.virtual_machines.add(vm2.uuid)
        vm1.virtual_router = vr1.uuid
        candidates = self.scheduler._get_candidates(si, vm2)
        self.assertEqual(candidates, [vr2.uuid])

        # test same vrouter returned if already scheduled
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 1) 
        self.assertEqual(candidates, [vr1.uuid])

        # test all candidates returned
        vm1.virtual_router = None
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 2)

        # test non running candidates returned
        vr1.agent_state = False
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 1)
        self.assertEqual(candidates, [vr2.uuid])

        # test no candidates
        vr1.agent_state = False
        vr2.agent_state = False
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 0)
    def test_random_scheduling(self):
        random_patch = mock.patch('random.choice')
        random_mock = random_patch.start()

        def side_effect(seq):
            return seq[0]
        random_mock.side_effect = side_effect

        si = test_utils.create_test_si(name='test-instance', count=2,
            intf_list=['vn1', 'vn2'])
        vm = test_utils.create_test_virtual_machine('vm')

        with mock.patch.object(scheduler.RandomScheduler, '_get_candidates',
                return_value=['vrouter1', 'vrouter2']):
            chosen_vrouter = self.scheduler.schedule(si, vm)
            self.assertEqual(random_mock.call_count, 1)
            self.assertEqual(chosen_vrouter, 'vrouter1')

        random_patch.stop()
    def test_get_candidates(self):
        test_utils.create_test_project("fake-domain:fake-project")
        test_utils.create_test_virtual_network("fake-domain:fake-project:vn1")
        test_utils.create_test_virtual_network("fake-domain:fake-project:vn2")
        test_utils.create_test_security_group("fake-domain:fake-project:default")
        st = test_utils.create_test_st(
            name="test-template", virt_type="network-namespace", intf_list=[["right", True], ["left", True]]
        )
        si = test_utils.create_test_si(name="test-instance", count=2, intf_list=["vn1", "vn2"])

        # test anti-affinity
        vr1 = test_utils.create_test_virtual_router("vr-candidate1")
        vr2 = test_utils.create_test_virtual_router("vr-candidate2")
        vm1 = test_utils.create_test_virtual_machine("vm1")
        vm2 = test_utils.create_test_virtual_machine("vm2")
        si.virtual_machines.add(vm1.uuid)
        si.virtual_machines.add(vm2.uuid)
        vm1.virtual_router = vr1.uuid
        candidates = self.scheduler._get_candidates(si, vm2)
        self.assertEqual(candidates, [vr2.uuid])

        # test same vrouter returned if already scheduled
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 1)
        self.assertEqual(candidates, [vr1.uuid])

        # test all candidates returned
        vm1.virtual_router = None
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 2)

        # test non running candidates returned
        vr1.agent_state = False
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 1)
        self.assertEqual(candidates, [vr2.uuid])

        # test no candidates
        vr1.agent_state = False
        vr2.agent_state = False
        candidates = self.scheduler._get_candidates(si, vm1)
        self.assertEqual(len(candidates), 0)