~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/tests/test_nova_manage.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import StringIO
21
21
import sys
22
22
 
23
 
import nova.auth.manager
24
23
from nova import context
25
24
from nova import db
 
25
from nova import exception
26
26
from nova import test
27
27
from nova.tests.db import fakes as db_fakes
28
28
 
34
34
NOVA_MANAGE_PATH = os.path.join(TOPDIR, 'bin', 'nova-manage')
35
35
 
36
36
sys.dont_write_bytecode = True
37
 
nova_manage = imp.load_source('nova_manage.py', NOVA_MANAGE_PATH)
 
37
nova_manage = imp.load_source('nova_manage', NOVA_MANAGE_PATH)
38
38
sys.dont_write_bytecode = False
39
39
 
40
40
 
67
67
                          '55.55.55.55')
68
68
 
69
69
 
 
70
class FloatingIpCommandsTestCase(test.TestCase):
 
71
    def setUp(self):
 
72
        super(FloatingIpCommandsTestCase, self).setUp()
 
73
        db_fakes.stub_out_db_network_api(self.stubs)
 
74
        self.commands = nova_manage.FloatingIpCommands()
 
75
 
 
76
    def test_address_to_hosts(self):
 
77
        def assert_loop(result, expected):
 
78
            for ip in result:
 
79
                self.assertTrue(str(ip) in expected)
 
80
 
 
81
        address_to_hosts = self.commands.address_to_hosts
 
82
        # /32 and /31
 
83
        self.assertRaises(exception.InvalidInput, address_to_hosts,
 
84
                          '192.168.100.1/32')
 
85
        self.assertRaises(exception.InvalidInput, address_to_hosts,
 
86
                          '192.168.100.1/31')
 
87
        # /30
 
88
        expected = ["192.168.100.%s" % i for i in range(1, 3)]
 
89
        result = address_to_hosts('192.168.100.0/30')
 
90
        self.assertTrue(len(list(result)) == 2)
 
91
        assert_loop(result, expected)
 
92
        # /29
 
93
        expected = ["192.168.100.%s" % i for i in range(1, 7)]
 
94
        result = address_to_hosts('192.168.100.0/29')
 
95
        self.assertTrue(len(list(result)) == 6)
 
96
        assert_loop(result, expected)
 
97
        # /28
 
98
        expected = ["192.168.100.%s" % i for i in range(1, 15)]
 
99
        result = address_to_hosts('192.168.100.0/28')
 
100
        self.assertTrue(len(list(result)) == 14)
 
101
        assert_loop(result, expected)
 
102
 
 
103
 
70
104
class NetworkCommandsTestCase(test.TestCase):
71
105
    def setUp(self):
72
106
        super(NetworkCommandsTestCase, self).setUp()
236
270
                               dis_host=True)
237
271
 
238
272
 
239
 
class ExportAuthTestCase(test.TestCase):
240
 
 
241
 
    def test_export_with_noauth(self):
242
 
        self._do_test_export()
243
 
 
244
 
    def test_export_with_deprecated_auth(self):
245
 
        self.flags(auth_strategy='deprecated')
246
 
        self._do_test_export(noauth=False)
247
 
 
248
 
    def _do_test_export(self, noauth=True):
249
 
        self.flags(allowed_roles=['role1', 'role2'])
250
 
        am = nova.auth.manager.AuthManager(new=True)
251
 
        user1 = am.create_user('user1', 'a1', 's1')
252
 
        user2 = am.create_user('user2', 'a2', 's2')
253
 
        user3 = am.create_user('user3', 'a3', 's3')
254
 
        proj1 = am.create_project('proj1', user1, member_users=[user1, user2])
255
 
        proj2 = am.create_project('proj2', user2, member_users=[user2, user3])
256
 
        am.add_role(user1, 'role1', proj1)
257
 
        am.add_role(user1, 'role1', proj2)
258
 
        am.add_role(user3, 'role1', proj1)
259
 
        am.add_role(user3, 'role2', proj2)
260
 
 
261
 
        commands = nova_manage.ExportCommands()
262
 
        output = commands._get_auth_data()
263
 
 
264
 
        def pw(idx):
265
 
            return ('user' if noauth else 'a') + str(idx)
266
 
 
267
 
        expected = {
268
 
            "users": [
269
 
                {"id": "user1", "name": "user1", 'password': pw(1)},
270
 
                {"id": "user2", "name": "user2", 'password': pw(2)},
271
 
                {"id": "user3", "name": "user3", 'password': pw(3)},
272
 
            ],
273
 
            "roles": ["role1", "role2"],
274
 
            "role_user_tenant_list": [
275
 
                {"user_id": "user1", "role": "role1", "tenant_id": "proj1"},
276
 
                {"user_id": "user3", "role": "role2", "tenant_id": "proj2"},
277
 
            ],
278
 
            "user_tenant_list": [
279
 
                {"tenant_id": "proj1", "user_id": "user1"},
280
 
                {"tenant_id": "proj1", "user_id": "user2"},
281
 
                {"tenant_id": "proj2", "user_id": "user2"},
282
 
                {"tenant_id": "proj2", "user_id": "user3"},
283
 
            ],
284
 
            "ec2_credentials": [
285
 
                {"access_key": pw(1), "secret_key": "s1", "user_id": "user1"},
286
 
                {"access_key": pw(2), "secret_key": "s2", "user_id": "user2"},
287
 
                {"access_key": pw(3), "secret_key": "s3", "user_id": "user3"},
288
 
            ],
289
 
            "tenants": [
290
 
                {"description": "proj1", "id": "proj1", "name": "proj1"},
291
 
                {"description": "proj2", "id": "proj2", "name": "proj2"},
292
 
            ],
293
 
        }
294
 
 
295
 
        self.assertDictMatch(output, expected)
 
273
class InstanceTypeCommandsTestCase(test.TestCase):
 
274
    def setUp(self):
 
275
        super(InstanceTypeCommandsTestCase, self).setUp()
 
276
 
 
277
        values = dict(name="test.small",
 
278
                      memory_mb=220,
 
279
                      vcpus=1,
 
280
                      root_gb=16,
 
281
                      ephemeral_gb=32,
 
282
                      flavorid=105)
 
283
        ref = db.instance_type_create(context.get_admin_context(),
 
284
                                      values)
 
285
        self.instance_type_name = ref["name"]
 
286
        self.instance_type_id = ref["id"]
 
287
        self.instance_type_flavorid = ref["flavorid"]
 
288
        self.set_key = nova_manage.InstanceTypeCommands().set_key
 
289
        self.unset_key = nova_manage.InstanceTypeCommands().unset_key
 
290
 
 
291
    def tearDown(self):
 
292
        db.instance_type_destroy(context.get_admin_context(),
 
293
                                 "test.small")
 
294
        super(InstanceTypeCommandsTestCase, self).tearDown()
 
295
 
 
296
    def _test_extra_specs_empty(self):
 
297
        empty_specs = {}
 
298
        actual_specs = db.instance_type_extra_specs_get(
 
299
                              context.get_admin_context(),
 
300
                              self.instance_type_id)
 
301
        self.assertEquals(empty_specs, actual_specs)
 
302
 
 
303
    def test_extra_specs_set_unset(self):
 
304
        expected_specs = {'k1': 'v1'}
 
305
 
 
306
        self._test_extra_specs_empty()
 
307
 
 
308
        self.set_key(self.instance_type_name, "k1", "v1")
 
309
        actual_specs = db.instance_type_extra_specs_get(
 
310
                              context.get_admin_context(),
 
311
                              self.instance_type_flavorid)
 
312
        self.assertEquals(expected_specs, actual_specs)
 
313
 
 
314
        self.unset_key(self.instance_type_name, "k1")
 
315
        self._test_extra_specs_empty()
 
316
 
 
317
    def test_extra_specs_update(self):
 
318
        expected_specs = {'k1': 'v1'}
 
319
        updated_specs = {'k1': 'v2'}
 
320
 
 
321
        self._test_extra_specs_empty()
 
322
 
 
323
        self.set_key(self.instance_type_name, "k1", "v1")
 
324
        actual_specs = db.instance_type_extra_specs_get(
 
325
                              context.get_admin_context(),
 
326
                              self.instance_type_flavorid)
 
327
        self.assertEquals(expected_specs, actual_specs)
 
328
 
 
329
        self.set_key(self.instance_type_name, "k1", "v2")
 
330
        actual_specs = db.instance_type_extra_specs_get(
 
331
                              context.get_admin_context(),
 
332
                              self.instance_type_flavorid)
 
333
        self.assertEquals(updated_specs, actual_specs)
 
334
 
 
335
        self.unset_key(self.instance_type_name, "k1")
 
336
 
 
337
    def test_extra_specs_multiple(self):
 
338
        two_items_extra_specs = {'k1': 'v1',
 
339
                                'k3': 'v3'}
 
340
 
 
341
        self._test_extra_specs_empty()
 
342
 
 
343
        self.set_key(self.instance_type_name, "k1", "v1")
 
344
        self.set_key(self.instance_type_name, "k3", "v3")
 
345
        actual_specs = db.instance_type_extra_specs_get(
 
346
                              context.get_admin_context(),
 
347
                              self.instance_type_flavorid)
 
348
        self.assertEquals(two_items_extra_specs, actual_specs)
 
349
 
 
350
        self.unset_key(self.instance_type_name, "k1")
 
351
        self.unset_key(self.instance_type_name, "k3")