~danwent/nova/trunk

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/contrib/test_security_groups.py

  • Committer: Tarmac
  • Author(s): Tushar Patil
  • Date: 2011-08-21 00:04:29 UTC
  • mfrom: (1424.3.4 nova)
  • Revision ID: tarmac-20110821000429-rnzlmax2a25qnkj9
Added OS APIs to associate/disassociate security groups to/from instances.

I will add views to return list of security groups associated with the servers later after this branch is merged into trunk. The reason I will do this later is because my previous merge proposal (https://code.launchpad.net/~tpatil/nova/add-options-network-create-os-apis/+merge/68292) is dependent on this work. In this merge proposal I have added a new extension which still uses default OS v1.1 controllers and views, but I am going to override views in this extension to send extra information like security groups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    under the License.
16
16
 
17
17
import json
 
18
import mox
 
19
import nova
18
20
import unittest
19
21
import webob
20
22
from xml.dom import minidom
21
23
 
 
24
from nova import exception
22
25
from nova import test
23
26
from nova.api.openstack.contrib import security_groups
24
27
from nova.tests.api.openstack import fakes
51
54
    return {'security_group': sg}
52
55
 
53
56
 
 
57
def return_server(context, server_id):
 
58
    return {'id': server_id, 'state': 0x01, 'host': "localhost"}
 
59
 
 
60
 
 
61
def return_non_running_server(context, server_id):
 
62
    return {'id': server_id, 'state': 0x02,
 
63
                        'host': "localhost"}
 
64
 
 
65
 
 
66
def return_security_group(context, project_id, group_name):
 
67
    return {'id': 1, 'name': group_name, "instances": [
 
68
              {'id': 1}]}
 
69
 
 
70
 
 
71
def return_security_group_without_instances(context, project_id, group_name):
 
72
    return {'id': 1, 'name': group_name}
 
73
 
 
74
 
 
75
def return_server_nonexistant(context, server_id):
 
76
    raise exception.InstanceNotFound(instance_id=server_id)
 
77
 
 
78
 
54
79
class TestSecurityGroups(test.TestCase):
55
80
    def setUp(self):
56
81
        super(TestSecurityGroups, self).setUp()
325
350
        response = self._delete_security_group(11111111)
326
351
        self.assertEquals(response.status_int, 404)
327
352
 
 
353
    def test_associate_by_non_existing_security_group_name(self):
 
354
        body = dict(addSecurityGroup=dict(name='non-existing'))
 
355
        req = webob.Request.blank('/v1.1/servers/1/action')
 
356
        req.headers['Content-Type'] = 'application/json'
 
357
        req.method = 'POST'
 
358
        req.body = json.dumps(body)
 
359
        response = req.get_response(fakes.wsgi_app())
 
360
        self.assertEquals(response.status_int, 404)
 
361
 
 
362
    def test_associate_by_invalid_server_id(self):
 
363
        body = dict(addSecurityGroup=dict(name='test'))
 
364
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
365
                       return_security_group)
 
366
        req = webob.Request.blank('/v1.1/servers/invalid/action')
 
367
        req.headers['Content-Type'] = 'application/json'
 
368
        req.method = 'POST'
 
369
        req.body = json.dumps(body)
 
370
        response = req.get_response(fakes.wsgi_app())
 
371
        self.assertEquals(response.status_int, 400)
 
372
 
 
373
    def test_associate_without_body(self):
 
374
        req = webob.Request.blank('/v1.1/servers/1/action')
 
375
        body = dict(addSecurityGroup=None)
 
376
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
377
        req.headers['Content-Type'] = 'application/json'
 
378
        req.method = 'POST'
 
379
        req.body = json.dumps(body)
 
380
        response = req.get_response(fakes.wsgi_app())
 
381
        self.assertEquals(response.status_int, 400)
 
382
 
 
383
    def test_associate_no_security_group_name(self):
 
384
        req = webob.Request.blank('/v1.1/servers/1/action')
 
385
        body = dict(addSecurityGroup=dict())
 
386
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
387
        req.headers['Content-Type'] = 'application/json'
 
388
        req.method = 'POST'
 
389
        req.body = json.dumps(body)
 
390
        response = req.get_response(fakes.wsgi_app())
 
391
        self.assertEquals(response.status_int, 400)
 
392
 
 
393
    def test_associate_security_group_name_with_whitespaces(self):
 
394
        req = webob.Request.blank('/v1.1/servers/1/action')
 
395
        body = dict(addSecurityGroup=dict(name="   "))
 
396
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
397
        req.headers['Content-Type'] = 'application/json'
 
398
        req.method = 'POST'
 
399
        req.body = json.dumps(body)
 
400
        response = req.get_response(fakes.wsgi_app())
 
401
        self.assertEquals(response.status_int, 400)
 
402
 
 
403
    def test_associate_non_existing_instance(self):
 
404
        self.stubs.Set(nova.db, 'instance_get', return_server_nonexistant)
 
405
        body = dict(addSecurityGroup=dict(name="test"))
 
406
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
407
                       return_security_group)
 
408
        req = webob.Request.blank('/v1.1/servers/10000/action')
 
409
        req.headers['Content-Type'] = 'application/json'
 
410
        req.method = 'POST'
 
411
        req.body = json.dumps(body)
 
412
        response = req.get_response(fakes.wsgi_app())
 
413
        self.assertEquals(response.status_int, 404)
 
414
 
 
415
    def test_associate_non_running_instance(self):
 
416
        self.stubs.Set(nova.db, 'instance_get', return_non_running_server)
 
417
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
418
                       return_security_group_without_instances)
 
419
        body = dict(addSecurityGroup=dict(name="test"))
 
420
        req = webob.Request.blank('/v1.1/servers/1/action')
 
421
        req.headers['Content-Type'] = 'application/json'
 
422
        req.method = 'POST'
 
423
        req.body = json.dumps(body)
 
424
        response = req.get_response(fakes.wsgi_app())
 
425
        self.assertEquals(response.status_int, 400)
 
426
 
 
427
    def test_associate_already_associated_security_group_to_instance(self):
 
428
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
429
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
430
                       return_security_group)
 
431
        body = dict(addSecurityGroup=dict(name="test"))
 
432
        req = webob.Request.blank('/v1.1/servers/1/action')
 
433
        req.headers['Content-Type'] = 'application/json'
 
434
        req.method = 'POST'
 
435
        req.body = json.dumps(body)
 
436
        response = req.get_response(fakes.wsgi_app())
 
437
        self.assertEquals(response.status_int, 400)
 
438
 
 
439
    def test_associate(self):
 
440
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
441
        self.mox.StubOutWithMock(nova.db, 'instance_add_security_group')
 
442
        nova.db.instance_add_security_group(mox.IgnoreArg(),
 
443
                                    mox.IgnoreArg(),
 
444
                                    mox.IgnoreArg())
 
445
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
446
                       return_security_group_without_instances)
 
447
        self.mox.ReplayAll()
 
448
 
 
449
        body = dict(addSecurityGroup=dict(name="test"))
 
450
        req = webob.Request.blank('/v1.1/servers/1/action')
 
451
        req.headers['Content-Type'] = 'application/json'
 
452
        req.method = 'POST'
 
453
        req.body = json.dumps(body)
 
454
        response = req.get_response(fakes.wsgi_app())
 
455
        self.assertEquals(response.status_int, 202)
 
456
 
 
457
    def test_associate_xml(self):
 
458
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
459
        self.mox.StubOutWithMock(nova.db, 'instance_add_security_group')
 
460
        nova.db.instance_add_security_group(mox.IgnoreArg(),
 
461
                                    mox.IgnoreArg(),
 
462
                                    mox.IgnoreArg())
 
463
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
464
                       return_security_group_without_instances)
 
465
        self.mox.ReplayAll()
 
466
 
 
467
        req = webob.Request.blank('/v1.1/servers/1/action')
 
468
        req.headers['Content-Type'] = 'application/xml'
 
469
        req.method = 'POST'
 
470
        req.body = """<addSecurityGroup>
 
471
                           <name>test</name>
 
472
                    </addSecurityGroup>"""
 
473
        response = req.get_response(fakes.wsgi_app())
 
474
        self.assertEquals(response.status_int, 202)
 
475
 
 
476
    def test_disassociate_by_non_existing_security_group_name(self):
 
477
        body = dict(removeSecurityGroup=dict(name='non-existing'))
 
478
        req = webob.Request.blank('/v1.1/servers/1/action')
 
479
        req.headers['Content-Type'] = 'application/json'
 
480
        req.method = 'POST'
 
481
        req.body = json.dumps(body)
 
482
        response = req.get_response(fakes.wsgi_app())
 
483
        self.assertEquals(response.status_int, 404)
 
484
 
 
485
    def test_disassociate_by_invalid_server_id(self):
 
486
        body = dict(removeSecurityGroup=dict(name='test'))
 
487
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
488
                       return_security_group)
 
489
        req = webob.Request.blank('/v1.1/servers/invalid/action')
 
490
        req.headers['Content-Type'] = 'application/json'
 
491
        req.method = 'POST'
 
492
        req.body = json.dumps(body)
 
493
        response = req.get_response(fakes.wsgi_app())
 
494
        self.assertEquals(response.status_int, 400)
 
495
 
 
496
    def test_disassociate_without_body(self):
 
497
        req = webob.Request.blank('/v1.1/servers/1/action')
 
498
        body = dict(removeSecurityGroup=None)
 
499
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
500
        req.headers['Content-Type'] = 'application/json'
 
501
        req.method = 'POST'
 
502
        req.body = json.dumps(body)
 
503
        response = req.get_response(fakes.wsgi_app())
 
504
        self.assertEquals(response.status_int, 400)
 
505
 
 
506
    def test_disassociate_no_security_group_name(self):
 
507
        req = webob.Request.blank('/v1.1/servers/1/action')
 
508
        body = dict(removeSecurityGroup=dict())
 
509
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
510
        req.headers['Content-Type'] = 'application/json'
 
511
        req.method = 'POST'
 
512
        req.body = json.dumps(body)
 
513
        response = req.get_response(fakes.wsgi_app())
 
514
        self.assertEquals(response.status_int, 400)
 
515
 
 
516
    def test_disassociate_security_group_name_with_whitespaces(self):
 
517
        req = webob.Request.blank('/v1.1/servers/1/action')
 
518
        body = dict(removeSecurityGroup=dict(name="   "))
 
519
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
520
        req.headers['Content-Type'] = 'application/json'
 
521
        req.method = 'POST'
 
522
        req.body = json.dumps(body)
 
523
        response = req.get_response(fakes.wsgi_app())
 
524
        self.assertEquals(response.status_int, 400)
 
525
 
 
526
    def test_disassociate_non_existing_instance(self):
 
527
        self.stubs.Set(nova.db, 'instance_get', return_server_nonexistant)
 
528
        body = dict(removeSecurityGroup=dict(name="test"))
 
529
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
530
                       return_security_group)
 
531
        req = webob.Request.blank('/v1.1/servers/10000/action')
 
532
        req.headers['Content-Type'] = 'application/json'
 
533
        req.method = 'POST'
 
534
        req.body = json.dumps(body)
 
535
        response = req.get_response(fakes.wsgi_app())
 
536
        self.assertEquals(response.status_int, 404)
 
537
 
 
538
    def test_disassociate_non_running_instance(self):
 
539
        self.stubs.Set(nova.db, 'instance_get', return_non_running_server)
 
540
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
541
                       return_security_group)
 
542
        body = dict(removeSecurityGroup=dict(name="test"))
 
543
        req = webob.Request.blank('/v1.1/servers/1/action')
 
544
        req.headers['Content-Type'] = 'application/json'
 
545
        req.method = 'POST'
 
546
        req.body = json.dumps(body)
 
547
        response = req.get_response(fakes.wsgi_app())
 
548
        self.assertEquals(response.status_int, 400)
 
549
 
 
550
    def test_disassociate_already_associated_security_group_to_instance(self):
 
551
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
552
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
553
                       return_security_group_without_instances)
 
554
        body = dict(removeSecurityGroup=dict(name="test"))
 
555
        req = webob.Request.blank('/v1.1/servers/1/action')
 
556
        req.headers['Content-Type'] = 'application/json'
 
557
        req.method = 'POST'
 
558
        req.body = json.dumps(body)
 
559
        response = req.get_response(fakes.wsgi_app())
 
560
        self.assertEquals(response.status_int, 400)
 
561
 
 
562
    def test_disassociate(self):
 
563
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
564
        self.mox.StubOutWithMock(nova.db, 'instance_remove_security_group')
 
565
        nova.db.instance_remove_security_group(mox.IgnoreArg(),
 
566
                                    mox.IgnoreArg(),
 
567
                                    mox.IgnoreArg())
 
568
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
569
                       return_security_group)
 
570
        self.mox.ReplayAll()
 
571
 
 
572
        body = dict(removeSecurityGroup=dict(name="test"))
 
573
        req = webob.Request.blank('/v1.1/servers/1/action')
 
574
        req.headers['Content-Type'] = 'application/json'
 
575
        req.method = 'POST'
 
576
        req.body = json.dumps(body)
 
577
        response = req.get_response(fakes.wsgi_app())
 
578
        self.assertEquals(response.status_int, 202)
 
579
 
 
580
    def test_disassociate_xml(self):
 
581
        self.stubs.Set(nova.db, 'instance_get', return_server)
 
582
        self.mox.StubOutWithMock(nova.db, 'instance_remove_security_group')
 
583
        nova.db.instance_remove_security_group(mox.IgnoreArg(),
 
584
                                    mox.IgnoreArg(),
 
585
                                    mox.IgnoreArg())
 
586
        self.stubs.Set(nova.db, 'security_group_get_by_name',
 
587
                       return_security_group)
 
588
        self.mox.ReplayAll()
 
589
 
 
590
        req = webob.Request.blank('/v1.1/servers/1/action')
 
591
        req.headers['Content-Type'] = 'application/xml'
 
592
        req.method = 'POST'
 
593
        req.body = """<removeSecurityGroup>
 
594
                           <name>test</name>
 
595
                    </removeSecurityGroup>"""
 
596
        response = req.get_response(fakes.wsgi_app())
 
597
        self.assertEquals(response.status_int, 202)
 
598
 
328
599
 
329
600
class TestSecurityGroupRules(test.TestCase):
330
601
    def setUp(self):