~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/tests/objects/test_aggregate.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-09-09 13:11:11 UTC
  • mfrom: (1.1.74)
  • Revision ID: package-import@ubuntu.com-20130909131111-aw02ice50wac9tma
Tags: 1:2013.2~b3-0ubuntu1
* New usptream release. 
* debian/patches/avoid_requirements_cheetah.patch: Dropped
* debian/patches/fix-sqlalchemy-0.7.9-usage.patch: Dropped
* debian/patches/fix-requirements.patch: Refreshed.
* debian/patches/path-to-the-xenhost.conf-fixup.patch: Refreshed
* debian/control: Add python-jinja2
* debian/control: Dropped python-cheetah

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Copyright 2013 IBM Corp.
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
 
 
15
import datetime
 
16
 
 
17
from nova import db
 
18
from nova import exception
 
19
from nova.objects import aggregate
 
20
from nova.openstack.common import timeutils
 
21
from nova.tests.objects import test_objects
 
22
 
 
23
 
 
24
NOW = timeutils.utcnow().replace(microsecond=0)
 
25
fake_aggregate = {
 
26
    'created_at': NOW,
 
27
    'updated_at': None,
 
28
    'deleted_at': None,
 
29
    'deleted': False,
 
30
    'id': 123,
 
31
    'name': 'fake-aggregate',
 
32
    'hosts': ['foo', 'bar'],
 
33
    'metadetails': {'this': 'that'},
 
34
    }
 
35
 
 
36
 
 
37
def compare(obj, db_obj):
 
38
    for key in obj.fields:
 
39
        obj_val = obj[key]
 
40
        if isinstance(obj_val, datetime.datetime):
 
41
            obj_val = obj_val.replace(tzinfo=None)
 
42
        if key == 'metadata':
 
43
            key = 'metadetails'
 
44
        db_val = db_obj[key]
 
45
        assert db_val == obj_val, '%s != %s' % (db_val, obj_val)
 
46
 
 
47
 
 
48
class _TestAggregateObject(object):
 
49
    def test_get_by_id(self):
 
50
        self.mox.StubOutWithMock(db, 'aggregate_get')
 
51
        db.aggregate_get(self.context, 123).AndReturn(fake_aggregate)
 
52
        self.mox.ReplayAll()
 
53
        agg = aggregate.Aggregate.get_by_id(self.context, 123)
 
54
        compare(agg, fake_aggregate)
 
55
 
 
56
    def test_create(self):
 
57
        self.mox.StubOutWithMock(db, 'aggregate_create')
 
58
        db.aggregate_create(self.context, {'name': 'foo'},
 
59
                            metadata={'one': 'two'}).AndReturn(fake_aggregate)
 
60
        self.mox.ReplayAll()
 
61
        agg = aggregate.Aggregate()
 
62
        agg.name = 'foo'
 
63
        agg.metadata = {'one': 'two'}
 
64
        agg.create(self.context)
 
65
        compare(agg, fake_aggregate)
 
66
 
 
67
    def test_save(self):
 
68
        self.mox.StubOutWithMock(db, 'aggregate_update')
 
69
        db.aggregate_update(self.context, 123, {'name': 'baz'}).AndReturn(
 
70
            fake_aggregate)
 
71
        self.mox.ReplayAll()
 
72
        agg = aggregate.Aggregate()
 
73
        agg.id = 123
 
74
        agg.name = 'baz'
 
75
        agg.save(self.context)
 
76
        compare(agg, fake_aggregate)
 
77
 
 
78
    def test_save_and_create_no_hosts(self):
 
79
        agg = aggregate.Aggregate()
 
80
        agg.id = 123
 
81
        agg.hosts = ['foo', 'bar']
 
82
        self.assertRaises(exception.ObjectActionError,
 
83
                          agg.create, self.context)
 
84
        self.assertRaises(exception.ObjectActionError,
 
85
                          agg.save, self.context)
 
86
 
 
87
    def test_update_metadata(self):
 
88
        self.mox.StubOutWithMock(db, 'aggregate_metadata_delete')
 
89
        self.mox.StubOutWithMock(db, 'aggregate_metadata_add')
 
90
        db.aggregate_metadata_delete(self.context, 123, 'todelete')
 
91
        db.aggregate_metadata_add(self.context, 123, {'toadd': 'myval'})
 
92
        self.mox.ReplayAll()
 
93
        agg = aggregate.Aggregate()
 
94
        agg._context = self.context
 
95
        agg.id = 123
 
96
        agg.metadata = {'foo': 'bar'}
 
97
        agg.obj_reset_changes()
 
98
        agg.update_metadata({'todelete': None, 'toadd': 'myval'})
 
99
        self.assertEqual({'foo': 'bar', 'toadd': 'myval'}, agg.metadata)
 
100
 
 
101
    def test_destroy(self):
 
102
        self.mox.StubOutWithMock(db, 'aggregate_delete')
 
103
        db.aggregate_delete(self.context, 123)
 
104
        self.mox.ReplayAll()
 
105
        agg = aggregate.Aggregate()
 
106
        agg.id = 123
 
107
        agg.destroy(self.context)
 
108
 
 
109
    def test_add_host(self):
 
110
        self.mox.StubOutWithMock(db, 'aggregate_host_add')
 
111
        db.aggregate_host_add(self.context, 123, 'bar'
 
112
                              ).AndReturn({'host': 'bar'})
 
113
        self.mox.ReplayAll()
 
114
        agg = aggregate.Aggregate()
 
115
        agg.id = 123
 
116
        agg.hosts = ['foo']
 
117
        agg._context = self.context
 
118
        agg.add_host('bar')
 
119
        self.assertEqual(agg.hosts, ['foo', 'bar'])
 
120
 
 
121
    def test_delete_host(self):
 
122
        self.mox.StubOutWithMock(db, 'aggregate_host_delete')
 
123
        db.aggregate_host_delete(self.context, 123, 'foo')
 
124
        self.mox.ReplayAll()
 
125
        agg = aggregate.Aggregate()
 
126
        agg.id = 123
 
127
        agg.hosts = ['foo', 'bar']
 
128
        agg._context = self.context
 
129
        agg.delete_host('foo')
 
130
        self.assertEqual(agg.hosts, ['bar'])
 
131
 
 
132
    def test_availability_zone(self):
 
133
        agg = aggregate.Aggregate()
 
134
        agg.metadata = {'availability_zone': 'foo'}
 
135
        self.assertEqual('foo', agg.availability_zone)
 
136
 
 
137
    def test_get_all(self):
 
138
        self.mox.StubOutWithMock(db, 'aggregate_get_all')
 
139
        db.aggregate_get_all(self.context).AndReturn([fake_aggregate])
 
140
        self.mox.ReplayAll()
 
141
        aggs = aggregate.AggregateList.get_all(self.context)
 
142
        self.assertEqual(1, len(aggs))
 
143
        compare(aggs[0], fake_aggregate)
 
144
 
 
145
    def test_by_host(self):
 
146
        self.mox.StubOutWithMock(db, 'aggregate_get_by_host')
 
147
        db.aggregate_get_by_host(self.context, 'fake-host'
 
148
                                 ).AndReturn([fake_aggregate])
 
149
        self.mox.ReplayAll()
 
150
        aggs = aggregate.AggregateList.get_by_host(self.context, 'fake-host')
 
151
        self.assertEqual(1, len(aggs))
 
152
        compare(aggs[0], fake_aggregate)
 
153
 
 
154
 
 
155
class TestAggregateObject(test_objects._LocalTest,
 
156
                          _TestAggregateObject):
 
157
    pass
 
158
 
 
159
 
 
160
class TestRemoteAggregateObject(test_objects._RemoteTest,
 
161
                                _TestAggregateObject):
 
162
    pass