~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/tests/test_compute.py

  • Committer: Tarmac
  • Author(s): Brian Lamar, Dan Prince
  • Date: 2011-08-31 22:55:34 UTC
  • mfrom: (1443.3.61 instance_states)
  • Revision ID: tarmac-20110831225534-upfhtsvcsafyql6x
Fixed and improved the way instance "states" are set. Instead of relying on solely the power_state of a VM, there are now explicitly defined VM states and VM task states which respectively define the current state of the VM and the task which is currently being performed by the VM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from nova.compute import instance_types
25
25
from nova.compute import manager as compute_manager
26
26
from nova.compute import power_state
 
27
from nova.compute import vm_states
27
28
from nova import context
28
29
from nova import db
29
30
from nova.db.sqlalchemy import models
763
764
                                     'block_migration': False,
764
765
                                     'disk': None}}).\
765
766
                            AndRaise(rpc.RemoteError('', '', ''))
766
 
        dbmock.instance_update(c, i_ref['id'], {'state_description': 'running',
767
 
                                                'state': power_state.RUNNING,
 
767
        dbmock.instance_update(c, i_ref['id'], {'vm_state': vm_states.ACTIVE,
 
768
                                                'task_state': None,
768
769
                                                'host': i_ref['host']})
769
770
        for v in i_ref['volumes']:
770
771
            dbmock.volume_update(c, v['id'], {'status': 'in-use'})
795
796
                                     'block_migration': False,
796
797
                                     'disk': None}}).\
797
798
                            AndRaise(rpc.RemoteError('', '', ''))
798
 
        dbmock.instance_update(c, i_ref['id'], {'state_description': 'running',
799
 
                                                'state': power_state.RUNNING,
 
799
        dbmock.instance_update(c, i_ref['id'], {'vm_state': vm_states.ACTIVE,
 
800
                                                'task_state': None,
800
801
                                                'host': i_ref['host']})
801
802
 
802
803
        self.compute.db = dbmock
841
842
        c = context.get_admin_context()
842
843
        instance_id = self._create_instance()
843
844
        i_ref = db.instance_get(c, instance_id)
844
 
        db.instance_update(c, i_ref['id'], {'state_description': 'migrating',
845
 
                                            'state': power_state.PAUSED})
 
845
        db.instance_update(c, i_ref['id'], {'vm_state': vm_states.MIGRATING,
 
846
                                            'power_state': power_state.PAUSED})
846
847
        v_ref = db.volume_create(c, {'size': 1, 'instance_id': instance_id})
847
848
        fix_addr = db.fixed_ip_create(c, {'address': '1.1.1.1',
848
849
                                          'instance_id': instance_id})
903
904
        instances = db.instance_get_all(context.get_admin_context())
904
905
        LOG.info(_("After force-killing instances: %s"), instances)
905
906
        self.assertEqual(len(instances), 1)
906
 
        self.assertEqual(power_state.SHUTOFF, instances[0]['state'])
 
907
        self.assertEqual(power_state.NOSTATE, instances[0]['power_state'])
907
908
 
908
909
    def test_get_all_by_name_regexp(self):
909
910
        """Test searching instances by name (display_name)"""
1323
1324
        """Test searching instances by state"""
1324
1325
 
1325
1326
        c = context.get_admin_context()
1326
 
        instance_id1 = self._create_instance({'state': power_state.SHUTDOWN})
 
1327
        instance_id1 = self._create_instance({
 
1328
            'power_state': power_state.SHUTDOWN,
 
1329
        })
1327
1330
        instance_id2 = self._create_instance({
1328
 
                'id': 2,
1329
 
                'state': power_state.RUNNING})
 
1331
            'id': 2,
 
1332
            'power_state': power_state.RUNNING,
 
1333
        })
1330
1334
        instance_id3 = self._create_instance({
1331
 
                'id': 10,
1332
 
                'state': power_state.RUNNING})
1333
 
 
 
1335
            'id': 10,
 
1336
            'power_state': power_state.RUNNING,
 
1337
        })
1334
1338
        instances = self.compute_api.get_all(c,
1335
 
                search_opts={'state': power_state.SUSPENDED})
 
1339
                search_opts={'power_state': power_state.SUSPENDED})
1336
1340
        self.assertEqual(len(instances), 0)
1337
1341
 
1338
1342
        instances = self.compute_api.get_all(c,
1339
 
                search_opts={'state': power_state.SHUTDOWN})
 
1343
                search_opts={'power_state': power_state.SHUTDOWN})
1340
1344
        self.assertEqual(len(instances), 1)
1341
1345
        self.assertEqual(instances[0].id, instance_id1)
1342
1346
 
1343
1347
        instances = self.compute_api.get_all(c,
1344
 
                search_opts={'state': power_state.RUNNING})
 
1348
                search_opts={'power_state': power_state.RUNNING})
1345
1349
        self.assertEqual(len(instances), 2)
1346
1350
        instance_ids = [instance.id for instance in instances]
1347
1351
        self.assertTrue(instance_id2 in instance_ids)
1349
1353
 
1350
1354
        # Test passing a list as search arg
1351
1355
        instances = self.compute_api.get_all(c,
1352
 
                search_opts={'state': [power_state.SHUTDOWN,
 
1356
                search_opts={'power_state': [power_state.SHUTDOWN,
1353
1357
                        power_state.RUNNING]})
1354
1358
        self.assertEqual(len(instances), 3)
1355
1359