1
# Copyright 2013 IBM Corp.
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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
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
24
NOW = timeutils.utcnow().replace(microsecond=0)
31
'name': 'fake-aggregate',
32
'hosts': ['foo', 'bar'],
33
'metadetails': {'this': 'that'},
37
def compare(obj, db_obj):
38
for key in obj.fields:
40
if isinstance(obj_val, datetime.datetime):
41
obj_val = obj_val.replace(tzinfo=None)
45
assert db_val == obj_val, '%s != %s' % (db_val, obj_val)
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)
53
agg = aggregate.Aggregate.get_by_id(self.context, 123)
54
compare(agg, fake_aggregate)
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)
61
agg = aggregate.Aggregate()
63
agg.metadata = {'one': 'two'}
64
agg.create(self.context)
65
compare(agg, fake_aggregate)
68
self.mox.StubOutWithMock(db, 'aggregate_update')
69
db.aggregate_update(self.context, 123, {'name': 'baz'}).AndReturn(
72
agg = aggregate.Aggregate()
75
agg.save(self.context)
76
compare(agg, fake_aggregate)
78
def test_save_and_create_no_hosts(self):
79
agg = aggregate.Aggregate()
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)
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'})
93
agg = aggregate.Aggregate()
94
agg._context = self.context
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)
101
def test_destroy(self):
102
self.mox.StubOutWithMock(db, 'aggregate_delete')
103
db.aggregate_delete(self.context, 123)
105
agg = aggregate.Aggregate()
107
agg.destroy(self.context)
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'})
114
agg = aggregate.Aggregate()
117
agg._context = self.context
119
self.assertEqual(agg.hosts, ['foo', 'bar'])
121
def test_delete_host(self):
122
self.mox.StubOutWithMock(db, 'aggregate_host_delete')
123
db.aggregate_host_delete(self.context, 123, 'foo')
125
agg = aggregate.Aggregate()
127
agg.hosts = ['foo', 'bar']
128
agg._context = self.context
129
agg.delete_host('foo')
130
self.assertEqual(agg.hosts, ['bar'])
132
def test_availability_zone(self):
133
agg = aggregate.Aggregate()
134
agg.metadata = {'availability_zone': 'foo'}
135
self.assertEqual('foo', agg.availability_zone)
137
def test_get_all(self):
138
self.mox.StubOutWithMock(db, 'aggregate_get_all')
139
db.aggregate_get_all(self.context).AndReturn([fake_aggregate])
141
aggs = aggregate.AggregateList.get_all(self.context)
142
self.assertEqual(1, len(aggs))
143
compare(aggs[0], fake_aggregate)
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])
150
aggs = aggregate.AggregateList.get_by_host(self.context, 'fake-host')
151
self.assertEqual(1, len(aggs))
152
compare(aggs[0], fake_aggregate)
155
class TestAggregateObject(test_objects._LocalTest,
156
_TestAggregateObject):
160
class TestRemoteAggregateObject(test_objects._RemoteTest,
161
_TestAggregateObject):