~tr3buchet/nova/lock

« back to all changes in this revision

Viewing changes to nova/tests/virt_unittest.py

  • Committer: Vishvananda Ishaya
  • Date: 2010-12-22 20:59:53 UTC
  • mto: This revision was merged to the branch mainline in revision 482.
  • Revision ID: vishvananda@gmail.com-20101222205953-j2j5t0qjwlcd0t2s
merge trunk and upgrade to cheetah templating

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
        self.network = utils.import_object(FLAGS.network_manager)
41
41
        FLAGS.instances_path = ''
42
42
 
43
 
    def test_get_uri_and_template(self):
44
 
        ip = '10.11.12.13'
45
 
 
46
 
        instance = {'internal_id': 1,
47
 
                    'memory_kb': '1024000',
48
 
                    'basepath': '/some/path',
49
 
                    'bridge_name': 'br100',
50
 
                    'mac_address': '02:12:34:46:56:67',
51
 
                    'vcpus': 2,
52
 
                    'project_id': 'fake',
53
 
                    'bridge': 'br101',
54
 
                    'instance_type': 'm1.small'}
55
 
 
 
43
    test_ip = '10.11.12.13'
 
44
    test_instance = {'memory_kb':     '1024000',
 
45
                     'basepath':      '/some/path',
 
46
                     'bridge_name':   'br100',
 
47
                     'mac_address':   '02:12:34:46:56:67',
 
48
                     'vcpus':         2,
 
49
                     'project_id':    'fake',
 
50
                     'bridge':        'br101',
 
51
                     'instance_type': 'm1.small'}
 
52
 
 
53
    def test_xml_and_uri_no_ramdisk_no_kernel(self):
 
54
        instance_data = dict(self.test_instance)
 
55
        self.do_test_xml_and_uri(instance_data,
 
56
                                 expect_kernel=False, expect_ramdisk=False)
 
57
 
 
58
    def test_xml_and_uri_no_ramdisk(self):
 
59
        instance_data = dict(self.test_instance)
 
60
        instance_data['kernel_id'] = 'aki-deadbeef'
 
61
        self.do_test_xml_and_uri(instance_data,
 
62
                                 expect_kernel=True, expect_ramdisk=False)
 
63
 
 
64
    def test_xml_and_uri_no_kernel(self):
 
65
        instance_data = dict(self.test_instance)
 
66
        instance_data['ramdisk_id'] = 'ari-deadbeef'
 
67
        self.do_test_xml_and_uri(instance_data,
 
68
                                 expect_kernel=False, expect_ramdisk=False)
 
69
 
 
70
    def test_xml_and_uri(self):
 
71
        instance_data = dict(self.test_instance)
 
72
        instance_data['ramdisk_id'] = 'ari-deadbeef'
 
73
        instance_data['kernel_id'] = 'aki-deadbeef'
 
74
        self.do_test_xml_and_uri(instance_data,
 
75
                                 expect_kernel=True, expect_ramdisk=True)
 
76
 
 
77
    def test_xml_and_uri_rescue(self):
 
78
        instance_data = dict(self.test_instance)
 
79
        instance_data['ramdisk_id'] = 'ari-deadbeef'
 
80
        instance_data['kernel_id'] = 'aki-deadbeef'
 
81
        self.do_test_xml_and_uri(instance_data,
 
82
                                 expect_kernel=True, expect_ramdisk=True,
 
83
                                 rescue=True)
 
84
 
 
85
    def do_test_xml_and_uri(self, instance,
 
86
                            expect_ramdisk, expect_kernel,
 
87
                            rescue=False):
56
88
        user_context = context.RequestContext(project=self.project,
57
89
                                              user=self.user)
58
90
        instance_ref = db.instance_create(user_context, instance)
60
92
        self.network.set_network_host(context.get_admin_context(),
61
93
                                      network_ref['id'])
62
94
 
63
 
        fixed_ip = {'address': ip,
 
95
        fixed_ip = {'address':    self.test_ip,
64
96
                    'network_id': network_ref['id']}
65
97
 
66
98
        ctxt = context.get_admin_context()
67
99
        fixed_ip_ref = db.fixed_ip_create(ctxt, fixed_ip)
68
 
        db.fixed_ip_update(ctxt, ip, {'allocated': True,
69
 
                                      'instance_id': instance_ref['id']})
 
100
        db.fixed_ip_update(ctxt, self.test_ip,
 
101
                                 {'allocated':   True,
 
102
                                  'instance_id': instance_ref['id']})
70
103
 
71
104
        type_uri_map = {'qemu': ('qemu:///system',
72
105
                             [(lambda t: t.find('.').get('type'), 'qemu'),
78
111
                              (lambda t: t.find('./devices/emulator'), None)]),
79
112
                        'uml': ('uml:///system',
80
113
                             [(lambda t: t.find('.').get('type'), 'uml'),
81
 
                              (lambda t: t.find('./os/type').text, 'uml')])}
 
114
                              (lambda t: t.find('./os/type').text, 'uml')]),
 
115
                        'xen': ('xen:///',
 
116
                             [(lambda t: t.find('.').get('type'), 'xen'),
 
117
                              (lambda t: t.find('./os/type').text, 'linux')]),
 
118
                              }
 
119
 
 
120
        for hypervisor_type in ['qemu', 'kvm', 'xen']:
 
121
            check_list = type_uri_map[hypervisor_type][1]
 
122
 
 
123
            if rescue:
 
124
                check = (lambda t: t.find('./os/kernel').text.split('/')[1],
 
125
                         'rescue-kernel')
 
126
                check_list.append(check)
 
127
                check = (lambda t: t.find('./os/initrd').text.split('/')[1],
 
128
                         'rescue-ramdisk')
 
129
                check_list.append(check)
 
130
            else:
 
131
                if expect_kernel:
 
132
                    check = (lambda t: t.find('./os/kernel').text.split(
 
133
                        '/')[1], 'kernel')
 
134
                else:
 
135
                    check = (lambda t: t.find('./os/kernel'), None)
 
136
                check_list.append(check)
 
137
 
 
138
                if expect_ramdisk:
 
139
                    check = (lambda t: t.find('./os/initrd').text.split(
 
140
                        '/')[1], 'ramdisk')
 
141
                else:
 
142
                    check = (lambda t: t.find('./os/initrd'), None)
 
143
                check_list.append(check)
82
144
 
83
145
        common_checks = [
84
146
            (lambda t: t.find('.').tag, 'domain'),
85
 
            (lambda t: t.find('./devices/interface/filterref/parameter').\
86
 
                         get('name'), 'IP'),
87
 
            (lambda t: t.find('./devices/interface/filterref/parameter').\
88
 
                         get('value'), '10.11.12.13')]
 
147
            (lambda t: t.find(
 
148
                './devices/interface/filterref/parameter').get('name'), 'IP'),
 
149
            (lambda t: t.find(
 
150
                './devices/interface/filterref/parameter').get(
 
151
                    'value'), '10.11.12.13'),
 
152
            (lambda t: t.findall(
 
153
                './devices/interface/filterref/parameter')[1].get(
 
154
                    'name'), 'DHCPSERVER'),
 
155
            (lambda t: t.findall(
 
156
                './devices/interface/filterref/parameter')[1].get(
 
157
                    'value'), '10.0.0.1'),
 
158
            (lambda t: t.find('./devices/serial/source').get(
 
159
                'path').split('/')[1], 'console.log'),
 
160
            (lambda t: t.find('./memory').text, '2097152')]
 
161
 
 
162
        if rescue:
 
163
            common_checks += [
 
164
                (lambda t: t.findall('./devices/disk/source')[0].get(
 
165
                    'file').split('/')[1], 'rescue-disk'),
 
166
                (lambda t: t.findall('./devices/disk/source')[1].get(
 
167
                    'file').split('/')[1], 'disk')]
 
168
        else:
 
169
            common_checks += [(lambda t: t.findall(
 
170
                './devices/disk/source')[0].get('file').split('/')[1],
 
171
                               'disk')]
89
172
 
90
173
        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
91
174
            FLAGS.libvirt_type = libvirt_type
92
175
            conn = libvirt_conn.LibvirtConnection(True)
93
176
 
94
 
            uri, _template, _rescue = conn.get_uri_and_templates()
 
177
            uri = conn.get_uri()
95
178
            self.assertEquals(uri, expected_uri)
96
179
 
97
 
            xml = conn.to_xml(instance_ref)
 
180
            xml = conn.to_xml(instance_ref, rescue)
98
181
            tree = xml_to_tree(xml)
99
182
            for i, (check, expected_result) in enumerate(checks):
100
183
                self.assertEqual(check(tree),
106
189
                                 expected_result,
107
190
                                 '%s failed common check %d' % (xml, i))
108
191
 
 
192
        # This test is supposed to make sure we don't override a specifically
 
193
        # set uri
 
194
        #
109
195
        # Deliberately not just assigning this string to FLAGS.libvirt_uri and
110
196
        # checking against that later on. This way we make sure the
111
197
        # implementation doesn't fiddle around with the FLAGS.
114
200
        for (libvirt_type, (expected_uri, checks)) in type_uri_map.iteritems():
115
201
            FLAGS.libvirt_type = libvirt_type
116
202
            conn = libvirt_conn.LibvirtConnection(True)
117
 
            uri, _template, _rescue = conn.get_uri_and_templates()
 
203
            uri = conn.get_uri()
118
204
            self.assertEquals(uri, testuri)
119
205
 
120
206
    def tearDown(self):