~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to nova/tests/cloud_unittest.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright [2010] [Anso Labs, LLC]
 
3
 
4
#    Licensed under the Apache License, Version 2.0 (the "License");
 
5
#    you may not use this file except in compliance with the License.
 
6
#    You may obtain a copy of the License at
 
7
 
8
#        http://www.apache.org/licenses/LICENSE-2.0
 
9
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS,
 
12
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#    See the License for the specific language governing permissions and
 
14
#    limitations under the License.
 
15
 
 
16
import logging
 
17
import StringIO
 
18
import time
 
19
import unittest
 
20
from xml.etree import ElementTree
 
21
 
 
22
from nova import vendor
 
23
import mox
 
24
from tornado import ioloop
 
25
from twisted.internet import defer
 
26
 
 
27
from nova import flags
 
28
from nova import rpc
 
29
from nova import test
 
30
from nova.auth import users
 
31
from nova.compute import node
 
32
from nova.endpoint import api
 
33
from nova.endpoint import cloud
 
34
 
 
35
 
 
36
FLAGS = flags.FLAGS
 
37
 
 
38
 
 
39
class CloudTestCase(test.BaseTestCase):
 
40
    def setUp(self):
 
41
        super(CloudTestCase, self).setUp()
 
42
        self.flags(fake_libvirt=True,
 
43
                   fake_storage=True,
 
44
                   fake_users=True,
 
45
                    redis_db=8)
 
46
 
 
47
        self.conn = rpc.Connection.instance()
 
48
        logging.getLogger().setLevel(logging.DEBUG)
 
49
 
 
50
        # set up our cloud
 
51
        self.cloud = cloud.CloudController()
 
52
        self.cloud_consumer = rpc.AdapterConsumer(connection=self.conn,
 
53
                                                      topic=FLAGS.cloud_topic,
 
54
                                                      proxy=self.cloud)
 
55
        self.injected.append(self.cloud_consumer.attach_to_tornado(self.ioloop))
 
56
        
 
57
        # set up a node
 
58
        self.node = node.Node()
 
59
        self.node_consumer = rpc.AdapterConsumer(connection=self.conn,
 
60
                                                     topic=FLAGS.compute_topic,
 
61
                                                     proxy=self.node)
 
62
        self.injected.append(self.node_consumer.attach_to_tornado(self.ioloop))
 
63
        
 
64
        user_mocker = mox.Mox()
 
65
        self.admin = user_mocker.CreateMock(users.User)
 
66
        self.admin.is_authorized(mox.IgnoreArg()).AndReturn(True)
 
67
        self.context = api.APIRequestContext(handler=None,user=self.admin) 
 
68
 
 
69
    def test_console_output(self):
 
70
        if FLAGS.fake_libvirt:
 
71
            logging.debug("Can't test instances without a real virtual env.")
 
72
            return
 
73
        instance_id = 'foo'
 
74
        inst = yield self.node.run_instance(instance_id)
 
75
        output = yield self.cloud.get_console_output(self.context, [instance_id])
 
76
        logging.debug(output)
 
77
        self.assert_(output)
 
78
        rv = yield self.node.terminate_instance(instance_id)
 
79
    
 
80
    def test_run_instances(self):
 
81
        if FLAGS.fake_libvirt:
 
82
            logging.debug("Can't test instances without a real virtual env.")
 
83
            return
 
84
        image_id = FLAGS.default_image
 
85
        instance_type = FLAGS.default_instance_type
 
86
        max_count = 1
 
87
        kwargs = {'image_id': image_id,
 
88
                  'instance_type': instance_type,
 
89
                  'max_count': max_count}
 
90
        rv = yield self.cloud.run_instances(self.context, **kwargs)
 
91
        # TODO: check for proper response
 
92
        instance = rv['reservationSet'][0][rv['reservationSet'][0].keys()[0]][0]
 
93
        logging.debug("Need to watch instance %s until it's running..." % instance['instance_id'])
 
94
        while True:
 
95
            rv = yield defer.succeed(time.sleep(1))
 
96
            info = self.cloud._get_instance(instance['instance_id'])
 
97
            logging.debug(info['state'])
 
98
            if info['state'] == node.Instance.RUNNING:
 
99
                break
 
100
        self.assert_(rv)
 
101
 
 
102
        if not FLAGS.fake_libvirt:
 
103
            time.sleep(45) # Should use boto for polling here
 
104
        for reservations in rv['reservationSet']:
 
105
            # for res_id in reservations.keys():
 
106
            #  logging.debug(reservations[res_id])
 
107
             # for instance in reservations[res_id]:
 
108
           for instance in reservations[reservations.keys()[0]]:
 
109
               logging.debug("Terminating instance %s" % instance['instance_id'])
 
110
               rv = yield self.node.terminate_instance(instance['instance_id'])
 
111
 
 
112
    def test_instance_update_state(self):
 
113
        def instance(num):
 
114
            return {
 
115
                'reservation_id': 'r-1',
 
116
                'instance_id': 'i-%s' % num,
 
117
                'image_id': 'ami-%s' % num,
 
118
                'private_dns_name': '10.0.0.%s' % num,
 
119
                'dns_name': '10.0.0%s' % num,
 
120
                'ami_launch_index': str(num),
 
121
                'instance_type': 'fake',
 
122
                'availability_zone': 'fake',
 
123
                'key_name': None,
 
124
                'kernel_id': 'fake',
 
125
                'ramdisk_id': 'fake',
 
126
                'groups': ['default'],
 
127
                'product_codes': None,
 
128
                'state': 0x01,
 
129
                'user_data': ''
 
130
            }
 
131
        
 
132
        rv = self.cloud.format_instances(self.admin)
 
133
        print rv
 
134
        self.assert_(len(rv['reservationSet']) == 0)
 
135
 
 
136
        # simulate launch of 5 instances
 
137
        # self.cloud.instances['pending'] = {}
 
138
        #for i in xrange(5):
 
139
        #    inst = instance(i)
 
140
        #    self.cloud.instances['pending'][inst['instance_id']] = inst
 
141
 
 
142
        #rv = self.cloud.format_instances(self.admin)
 
143
        #self.assert_(len(rv['reservationSet']) == 1)
 
144
        #self.assert_(len(rv['reservationSet'][0]['instances_set']) == 5)
 
145
        
 
146
        # report 4 nodes each having 1 of the instances
 
147
        #for i in xrange(4):
 
148
        #    self.cloud.update_state('instances', {('node-%s' % i): {('i-%s' % i): instance(i)}})
 
149
            
 
150
        # one instance should be pending still
 
151
        #self.assert_(len(self.cloud.instances['pending'].keys()) == 1)
 
152
 
 
153
        # check that the reservations collapse
 
154
        #rv = self.cloud.format_instances(self.admin)
 
155
        #self.assert_(len(rv['reservationSet']) == 1)
 
156
        #self.assert_(len(rv['reservationSet'][0]['instances_set']) == 5)
 
157
 
 
158
        # check that we can get metadata for each instance
 
159
        #for i in xrange(4):
 
160
        #    data = self.cloud.get_metadata(instance(i)['private_dns_name'])
 
161
        #    self.assert_(data['meta-data']['ami-id'] == 'ami-%s' % i)