def test_instance_health2(self):
        i1 = Mock(instance_id="i1", state=InstanceState.RUNNING,
                  health=InstanceHealthState.OK)
        i2 = Mock(instance_id="i2", state=InstanceState.RUNNING_FAILED,
                  health=InstanceHealthState.OK)
        i3 = Mock(instance_id="i3", state=InstanceState.RUNNING_FAILED,
                  health=InstanceHealthState.MISSING)

        instances = dict(i1=i1, i2=i2, i3=i3)
        es = EngineState()
        es.instances = instances

        healthy = es.get_healthy_instances()
        self.assertEqual(healthy, [i1])

        unhealthy = es.get_unhealthy_instances()
        self.assertTrue(i2 in unhealthy)
        self.assertTrue(i3 in unhealthy)
        self.assertEqual(2, len(unhealthy))

        # Should not matter if health is present or not, it's RUNNING_FAILED
        i3.health = InstanceHealthState.MISSING
        unhealthy = es.get_unhealthy_instances()
        self.assertTrue(i2 in unhealthy)
        self.assertTrue(i3 in unhealthy)
    def test_instance_health(self):
        i1 = Mock(instance_id="i1", state=InstanceState.RUNNING,
                  health=InstanceHealthState.OK)
        i2 = Mock(instance_id="i2", state=InstanceState.FAILED,
                  health=InstanceHealthState.OK)
        i3 = Mock(instance_id="i3", state=InstanceState.TERMINATED,
                  health=InstanceHealthState.MISSING)

        instances = dict(i1=i1, i2=i2, i3=i3)
        es = EngineState()
        es.instances = instances

        healthy = es.get_healthy_instances()
        self.assertEqual(healthy, [i1])

        unhealthy = es.get_unhealthy_instances()
        self.assertFalse(unhealthy)

        i1.health = InstanceHealthState.MISSING
        healthy = es.get_healthy_instances()
        self.assertFalse(healthy)
        unhealthy = es.get_unhealthy_instances()
        self.assertEqual(unhealthy, [i1])
    def test_instances(self):
        i1 = [Mock(instance_id="i1", state=state)
              for state in (InstanceState.REQUESTING,
                            InstanceState.REQUESTED,
                            InstanceState.PENDING,
                            InstanceState.RUNNING)]
        i2 = [Mock(instance_id="i2", state=state)
              for state in (InstanceState.REQUESTING,
                            InstanceState.REQUESTED,
                            InstanceState.FAILED)]
        i3 = [Mock(instance_id="i3", state=state)
              for state in InstanceState.REQUESTING, InstanceState.PENDING]

        changes = dict(i1=i1, i2=i2, i3=i3)
        instances = dict(i1=i1[-1], i2=i2[-1], i3=i3[-1])
        es = EngineState()
        es.instance_changes = changes
        es.instances = instances

        self.assertEqual(es.get_instance("i1").state, InstanceState.RUNNING)
        self.assertEqual(es.get_instance("i2").state, InstanceState.FAILED)
        self.assertEqual(es.get_instance("i3").state, InstanceState.PENDING)
        self.assertEqual(es.get_instance("i4"), None)  # there is no i4
        self.assertEqual(len(es.get_instance_changes("i1")), 4)
        self.assertEqual(len(es.get_instance_changes("i2")), 3)
        self.assertEqual(len(es.get_instance_changes("i3")), 2)
        self.assertEqual(es.get_instance_changes("i4"), [])

        all_changes = es.get_instance_changes()
        changeset = set((change.instance_id, change.state) for change in all_changes)
        for item in itertools.chain(i1, i2, i3):
            self.assertIn((item.instance_id, item.state), changeset)

        failed = es.get_instances_by_state(InstanceState.FAILED)
        self.assertEqual(len(failed), 1)
        self.assertEqual(failed[0].instance_id, "i2")
        self.assertEqual(failed[0].state, InstanceState.FAILED)

        pending2running = es.get_instances_by_state(InstanceState.PENDING,
                                                    InstanceState.RUNNING)
        self.assertEqual(len(pending2running), 2)
        ids = (pending2running[0].instance_id, pending2running[1].instance_id)
        self.assertIn("i1", ids)
        self.assertIn("i3", ids)

        pending = es.get_pending_instances()
        self.assertEqual(len(pending), 1)
        self.assertEqual(pending[0].instance_id, "i3")