def test_find_state_from_events_not_yet_started(self):
        """Test for problems when status is called before epu is booted
        """
        from epumgmt.main.em_core_status import _find_latest_worker_status
        from mocks.common import FakeCommon
        from mocks.modules import FakeModules
        from mocks.remote_svc_adapter import FakeRemoteSvcAdapter

        svc_adapter = FakeRemoteSvcAdapter()
        modules = FakeModules(remote_svc_adapter=svc_adapter)
        common = FakeCommon()
        allvms = []

        provisioner = epumgmt.api.RunVM()
        provisioner.instanceid = "i-fsdfdsfds"
        provisioner_service_type = "provisioner"
        provisioner.service_type = provisioner_service_type
        allvms.append(provisioner)

        svc_adapter.fake_controller_map = {provisioner.instanceid: [provisioner]}

        worker_state_exception = Exception("Something broke!")
        svc_adapter.worker_state_raises = worker_state_exception
        _find_latest_worker_status(common, modules, "", None, allvms)

        print common.log.transcript

        errors = [error for error in common.log.transcript if error[0] == "ERROR"]
        _, last_error = errors[-1]
        assert last_error.find("Unable to get worker state for controllers") != -1
    def test_find_latest_worker_status(self):
        from epumgmt.main.em_core_status import _find_latest_worker_status
        from epumgmt.api.exceptions import ProgrammingError
        from mocks.common import FakeCommon
        from mocks.modules import FakeModules
        from mocks.remote_svc_adapter import FakeRemoteSvcAdapter
        from mocks.state import EPUControllerState, WorkerInstanceState

        svc_adapter = FakeRemoteSvcAdapter()
        modules = FakeModules(remote_svc_adapter=svc_adapter)
        common = FakeCommon()

        svc_adapter.allow_initialize = False
        _find_latest_worker_status(common, modules, "", None, [])

        # Check that we've logged something to WARNING
        # Filter out WARNING log messages
        warnings = [warning for warning in common.log.transcript if warning[0] == "WARNING"]
        assert len(warnings) == 1
        warning = warnings[0]
        warning_msg = warning[1]
        assert warning_msg.find("no channel open") != -1


        # reset log transcript
        common.log.transcript = []

        svc_adapter.allow_initialize = True
        svc_adapter.allow_controller_map = False
        _find_latest_worker_status(common, modules, "", None, [])

        warnings = [warning for warning in common.log.transcript if warning[0] == "WARNING"]
        assert len(warnings) == 1
        warning = warnings[0]
        warning_msg = warning[1]
        assert warning_msg.find("no controllers are configured") != -1


        common.log.transcript = []
        svc_adapter.allow_controller_map = True       


        allvms = []

        empty_instance_0 = "P-77686559"
        empty_vm_0 = epumgmt.api.RunVM()
        empty_vm_0.instanceid = empty_instance_0
        empty_vm_0.nodeid = "1234"
        allvms.append(empty_vm_0)

        empty_instance_1 = "P-88033864"
        empty_vm_1 = epumgmt.api.RunVM()
        empty_vm_1.instanceid = empty_instance_1
        empty_vm_1.nodeid = "2345"
        allvms.append(empty_vm_1)

        empty_instance_2 = "i-6f54ea01"
        empty_vm_2 = epumgmt.api.RunVM()
        empty_vm_2.instanceid = empty_instance_2
        empty_vm_2.nodeid = "3456"
        allvms.append(empty_vm_2)

        controller_instance = "P-32182444"
        controller_name = 'epu_controller_sleeper1'
        controller_vm = epumgmt.api.RunVM()
        controller_vm.instanceid = controller_instance
        allvms.append(controller_vm)

        svc_adapter.fake_controller_map = {empty_instance_0: [],
                                           empty_instance_1: [],
                                           empty_instance_2: [],
                                           controller_instance: [controller_name]}


        # Test for failure when no provisioner is available
        try:
            _find_latest_worker_status(common, modules, "", None, [])
        except ProgrammingError:
            raised_programming_error = True
        assert raised_programming_error


        provisioner = epumgmt.api.RunVM()
        provisioner_service_type = "provisioner"
        provisioner.service_type = provisioner_service_type
        allvms.append(provisioner)

        controller_state = EPUControllerState()

        empty_wis_0 = WorkerInstanceState(instanceid=empty_instance_0, nodeid=empty_vm_0.nodeid)
        controller_state.instances.append(empty_wis_0)
        empty_wis_1 = WorkerInstanceState(instanceid=empty_instance_1, nodeid=empty_vm_1.nodeid)
        controller_state.instances.append(empty_wis_1)
        empty_wis_2 = WorkerInstanceState(instanceid=empty_instance_2, nodeid=empty_vm_2.nodeid)
        controller_state.instances.append(empty_wis_2)

        svc_adapter.fake_worker_state = {'epu_controller_sleeper1': controller_state}

        _find_latest_worker_status(common, modules, "", None, allvms)

        # make sure at least one vm added to persistence
        assert len(modules.persistence.vm_store) > 0
        for run_name in modules.persistence.vm_store.keys():
            for persisted_vm in modules.persistence.vm_store[run_name]:
                assert persisted_vm.parent == controller_name