Example #1
0
class ControllerStateStoreTests(BaseControllerStateTests):
    """ControllerCoreState tests that can use either storage implementation.
    """
    def setUp(self):
        self.domain = LocalDomainStore("david", "domain1", {})

    def test_instances(self):
        launch_id = str(uuid.uuid4())
        instance_id = str(uuid.uuid4())
        self.domain.new_instance_launch("dtid", instance_id, launch_id,
                                             "chicago", "big", timestamp=1)

        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.REQUESTING,
                            state_time=1, health=InstanceHealthState.UNKNOWN)

        msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.STARTED)
        self.domain.new_instance_state(msg, timestamp=2)

        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.STARTED,
                            state_time=2, health=InstanceHealthState.UNKNOWN)

        # bring in a health update
        self.domain.new_instance_health(instance_id,
                                             InstanceHealthState.OK,
                                             errors=['blah'])
        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.STARTED,
                            state_time=2, health=InstanceHealthState.OK,
                            errors=['blah'])

        # another instance state change should preserve health info
        msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.RUNNING)

        self.domain.new_instance_state(msg, timestamp=3)
        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.RUNNING,
                            state_time=3, health=InstanceHealthState.OK,
                            errors=['blah'])

        all_instances = self.domain.get_instance_ids()
        all_instances = set(all_instances)
        self.assertEqual(len(all_instances), 1)
        self.assertIn(instance_id, all_instances)
Example #2
0
class ControllerStateStoreTests(BaseControllerStateTests):
    """ControllerCoreState tests that can use either storage implementation.
    """
    def setUp(self):
        self.domain = LocalDomainStore("david", "domain1", {})

    def test_instances(self):
        launch_id = str(uuid.uuid4())
        instance_id = str(uuid.uuid4())
        self.domain.new_instance_launch("dtid", instance_id, launch_id,
                                             "chicago", "big", timestamp=1)

        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.REQUESTING,
                            state_time=1, health=InstanceHealthState.UNKNOWN)

        msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.STARTED)
        self.domain.new_instance_state(msg, timestamp=2)

        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.STARTED,
                            state_time=2, health=InstanceHealthState.UNKNOWN)

        # bring in a health update
        self.domain.new_instance_health(instance_id,
                                             InstanceHealthState.OK,
                                             errors=['blah'])
        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.STARTED,
                            state_time=2, health=InstanceHealthState.OK,
                            errors=['blah'])

        # another instance state change should preserve health info
        msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.RUNNING)

        self.domain.new_instance_state(msg, timestamp=3)
        self.assertInstance(instance_id, launch_id=launch_id, site="chicago",
                            allocation="big", state=InstanceState.RUNNING,
                            state_time=3, health=InstanceHealthState.OK,
                            errors=['blah'])

        all_instances = self.domain.get_instance_ids()
        all_instances = set(all_instances)
        self.assertEqual(len(all_instances), 1)
        self.assertIn(instance_id, all_instances)

    def test_instance_update_conflict(self):
        launch_id = str(uuid.uuid4())
        instance_id = str(uuid.uuid4())
        self.domain.new_instance_launch("dtid", instance_id, launch_id,
                                             "chicago", "big", timestamp=1)

        sneaky_msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.PENDING)

        # patch in a function that sneaks in an instance record update just
        # before a requested update. This simulates the case where two EPUM
        # workers are competing to update the same instance.
        original_update_instance = self.domain.update_instance

        patch_called = threading.Event()

        def patched_update_instance(*args, **kwargs):
            patch_called.set()
            # unpatch ourself first so we don't recurse forever
            self.domain.update_instance = original_update_instance

            self.domain.new_instance_state(sneaky_msg, timestamp=2)
            original_update_instance(*args, **kwargs)
        self.domain.update_instance = patched_update_instance

        # send our "real" update. should get a conflict
        msg = dict(node_id=instance_id, launch_id=launch_id,
                   site="chicago", allocation="big",
                   state=InstanceState.STARTED)

        with self.assertRaises(WriteConflictError):
            self.domain.new_instance_state(msg, timestamp=2)

        assert patch_called.is_set()