~nova-coresec/nova/ppa-lucid

« back to all changes in this revision

Viewing changes to nova/tests/model_unittest.py

  • Committer: Soren Hansen
  • Date: 2010-09-28 22:06:35 UTC
  • mfrom: (195.1.70 ubuntu-packaging)
  • Revision ID: soren.hansen@rackspace.com-20100928220635-dd3170esyd303n8o
Merge ubuntu packaging branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2010 United States Government as represented by the
4
 
# Administrator of the National Aeronautics and Space Administration.
5
 
# All Rights Reserved.
6
 
#
7
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8
 
#    not use this file except in compliance with the License. You may obtain
9
 
#    a copy of the License at
10
 
#
11
 
#         http://www.apache.org/licenses/LICENSE-2.0
12
 
#
13
 
#    Unless required by applicable law or agreed to in writing, software
14
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
 
#    License for the specific language governing permissions and limitations
17
 
#    under the License.
18
 
 
19
 
from datetime import datetime, timedelta
20
 
import logging
21
 
import time
22
 
 
23
 
from nova import flags
24
 
from nova import test
25
 
from nova import utils
26
 
from nova.compute import model
27
 
 
28
 
 
29
 
FLAGS = flags.FLAGS
30
 
 
31
 
 
32
 
class ModelTestCase(test.TrialTestCase):
33
 
    def setUp(self):
34
 
        super(ModelTestCase, self).setUp()
35
 
        self.flags(connection_type='fake',
36
 
                   fake_storage=True)
37
 
 
38
 
    def tearDown(self):
39
 
        model.Instance('i-test').destroy()
40
 
        model.Host('testhost').destroy()
41
 
        model.Daemon('testhost', 'nova-testdaemon').destroy()
42
 
 
43
 
    def create_instance(self):
44
 
        inst = model.Instance('i-test')
45
 
        inst['reservation_id'] = 'r-test'
46
 
        inst['launch_time'] = '10'
47
 
        inst['user_id'] = 'fake'
48
 
        inst['project_id'] = 'fake'
49
 
        inst['instance_type'] = 'm1.tiny'
50
 
        inst['mac_address'] = utils.generate_mac()
51
 
        inst['ami_launch_index'] = 0
52
 
        inst['private_dns_name'] = '10.0.0.1'
53
 
        inst.save()
54
 
        return inst
55
 
 
56
 
    def create_host(self):
57
 
        host = model.Host('testhost')
58
 
        host.save()
59
 
        return host
60
 
 
61
 
    def create_daemon(self):
62
 
        daemon = model.Daemon('testhost', 'nova-testdaemon')
63
 
        daemon.save()
64
 
        return daemon
65
 
 
66
 
    def create_session_token(self):
67
 
        session_token = model.SessionToken('tk12341234')
68
 
        session_token['user'] = 'testuser'
69
 
        session_token.save()
70
 
        return session_token
71
 
 
72
 
    def test_create_instance(self):
73
 
        """store with create_instace, then test that a load finds it"""
74
 
        instance = self.create_instance()
75
 
        old = model.Instance(instance.identifier)
76
 
        self.assertFalse(old.is_new_record())
77
 
 
78
 
    def test_delete_instance(self):
79
 
        """create, then destroy, then make sure loads a new record"""
80
 
        instance = self.create_instance()
81
 
        instance.destroy()
82
 
        newinst = model.Instance('i-test')
83
 
        self.assertTrue(newinst.is_new_record())
84
 
 
85
 
    def test_instance_added_to_set(self):
86
 
        """create, then check that it is listed in global set"""
87
 
        instance = self.create_instance()
88
 
        found = False
89
 
        for x in model.InstanceDirectory().all:
90
 
            if x.identifier == 'i-test':
91
 
                found = True
92
 
        self.assert_(found)
93
 
 
94
 
    def test_instance_associates_project(self):
95
 
        """create, then check that it is listed for the project"""
96
 
        instance = self.create_instance()
97
 
        found = False
98
 
        for x in model.InstanceDirectory().by_project(instance.project):
99
 
            if x.identifier == 'i-test':
100
 
                found = True
101
 
        self.assert_(found)
102
 
 
103
 
    def test_instance_associates_ip(self):
104
 
        """create, then check that it is listed for the ip"""
105
 
        instance = self.create_instance()
106
 
        found = False
107
 
        x = model.InstanceDirectory().by_ip(instance['private_dns_name'])
108
 
        self.assertEqual(x.identifier, 'i-test')
109
 
 
110
 
    def test_instance_associates_node(self):
111
 
        """create, then check that it is listed for the node_name"""
112
 
        instance = self.create_instance()
113
 
        found = False
114
 
        for x in model.InstanceDirectory().by_node(FLAGS.node_name):
115
 
            if x.identifier == 'i-test':
116
 
                found = True
117
 
        self.assertFalse(found)
118
 
        instance['node_name'] = 'test_node'
119
 
        instance.save()
120
 
        for x in model.InstanceDirectory().by_node('test_node'):
121
 
            if x.identifier == 'i-test':
122
 
                found = True
123
 
        self.assert_(found)
124
 
 
125
 
 
126
 
    def test_host_class_finds_hosts(self):
127
 
        host = self.create_host()
128
 
        self.assertEqual('testhost', model.Host.lookup('testhost').identifier)
129
 
 
130
 
    def test_host_class_doesnt_find_missing_hosts(self):
131
 
        rv = model.Host.lookup('woahnelly')
132
 
        self.assertEqual(None, rv)
133
 
 
134
 
    def test_create_host(self):
135
 
        """store with create_host, then test that a load finds it"""
136
 
        host = self.create_host()
137
 
        old = model.Host(host.identifier)
138
 
        self.assertFalse(old.is_new_record())
139
 
 
140
 
    def test_delete_host(self):
141
 
        """create, then destroy, then make sure loads a new record"""
142
 
        instance = self.create_host()
143
 
        instance.destroy()
144
 
        newinst = model.Host('testhost')
145
 
        self.assertTrue(newinst.is_new_record())
146
 
 
147
 
    def test_host_added_to_set(self):
148
 
        """create, then check that it is included in list"""
149
 
        instance = self.create_host()
150
 
        found = False
151
 
        for x in model.Host.all():
152
 
            if x.identifier == 'testhost':
153
 
                found = True
154
 
        self.assert_(found)
155
 
 
156
 
    def test_create_daemon_two_args(self):
157
 
        """create a daemon with two arguments"""
158
 
        d = self.create_daemon()
159
 
        d = model.Daemon('testhost', 'nova-testdaemon')
160
 
        self.assertFalse(d.is_new_record())
161
 
 
162
 
    def test_create_daemon_single_arg(self):
163
 
        """Create a daemon using the combined host:bin format"""
164
 
        d = model.Daemon("testhost:nova-testdaemon")
165
 
        d.save()
166
 
        d = model.Daemon('testhost:nova-testdaemon')
167
 
        self.assertFalse(d.is_new_record())
168
 
 
169
 
    def test_equality_of_daemon_single_and_double_args(self):
170
 
        """Create a daemon using the combined host:bin arg, find with 2"""
171
 
        d = model.Daemon("testhost:nova-testdaemon")
172
 
        d.save()
173
 
        d = model.Daemon('testhost', 'nova-testdaemon')
174
 
        self.assertFalse(d.is_new_record())
175
 
 
176
 
    def test_equality_daemon_of_double_and_single_args(self):
177
 
        """Create a daemon using the combined host:bin arg, find with 2"""
178
 
        d = self.create_daemon()
179
 
        d = model.Daemon('testhost:nova-testdaemon')
180
 
        self.assertFalse(d.is_new_record())
181
 
 
182
 
    def test_delete_daemon(self):
183
 
        """create, then destroy, then make sure loads a new record"""
184
 
        instance = self.create_daemon()
185
 
        instance.destroy()
186
 
        newinst = model.Daemon('testhost', 'nova-testdaemon')
187
 
        self.assertTrue(newinst.is_new_record())
188
 
 
189
 
    def test_daemon_heartbeat(self):
190
 
        """Create a daemon, sleep, heartbeat, check for update"""
191
 
        d = self.create_daemon()
192
 
        ts = d['updated_at']
193
 
        time.sleep(2)
194
 
        d.heartbeat()
195
 
        d2 = model.Daemon('testhost', 'nova-testdaemon')
196
 
        ts2 = d2['updated_at']
197
 
        self.assert_(ts2 > ts)
198
 
 
199
 
    def test_daemon_added_to_set(self):
200
 
        """create, then check that it is included in list"""
201
 
        instance = self.create_daemon()
202
 
        found = False
203
 
        for x in model.Daemon.all():
204
 
            if x.identifier == 'testhost:nova-testdaemon':
205
 
                found = True
206
 
        self.assert_(found)
207
 
 
208
 
    def test_daemon_associates_host(self):
209
 
        """create, then check that it is listed for the host"""
210
 
        instance = self.create_daemon()
211
 
        found = False
212
 
        for x in model.Daemon.by_host('testhost'):
213
 
            if x.identifier == 'testhost:nova-testdaemon':
214
 
                found = True
215
 
        self.assertTrue(found)
216
 
 
217
 
    def test_create_session_token(self):
218
 
        """create"""
219
 
        d = self.create_session_token()
220
 
        d = model.SessionToken(d.token)
221
 
        self.assertFalse(d.is_new_record())
222
 
 
223
 
    def test_delete_session_token(self):
224
 
        """create, then destroy, then make sure loads a new record"""
225
 
        instance = self.create_session_token()
226
 
        instance.destroy()
227
 
        newinst = model.SessionToken(instance.token)
228
 
        self.assertTrue(newinst.is_new_record())
229
 
 
230
 
    def test_session_token_added_to_set(self):
231
 
        """create, then check that it is included in list"""
232
 
        instance = self.create_session_token()
233
 
        found = False
234
 
        for x in model.SessionToken.all():
235
 
            if x.identifier == instance.token:
236
 
                found = True
237
 
        self.assert_(found)
238
 
 
239
 
    def test_session_token_associates_user(self):
240
 
        """create, then check that it is listed for the user"""
241
 
        instance = self.create_session_token()
242
 
        found = False
243
 
        for x in model.SessionToken.associated_to('user', 'testuser'):
244
 
            if x.identifier == instance.identifier:
245
 
                found = True
246
 
        self.assertTrue(found)
247
 
 
248
 
    def test_session_token_generation(self):
249
 
        instance = model.SessionToken.generate('username', 'TokenType')
250
 
        self.assertFalse(instance.is_new_record())
251
 
 
252
 
    def test_find_generated_session_token(self):
253
 
        instance = model.SessionToken.generate('username', 'TokenType')
254
 
        found = model.SessionToken.lookup(instance.identifier)
255
 
        self.assert_(found)
256
 
 
257
 
    def test_update_session_token_expiry(self):
258
 
        instance = model.SessionToken('tk12341234')
259
 
        oldtime = datetime.utcnow()
260
 
        instance['expiry'] = oldtime.strftime(utils.TIME_FORMAT)
261
 
        instance.update_expiry()
262
 
        expiry = utils.parse_isotime(instance['expiry'])
263
 
        self.assert_(expiry > datetime.utcnow())
264
 
 
265
 
    def test_session_token_lookup_when_expired(self):
266
 
        instance = model.SessionToken.generate("testuser")
267
 
        instance['expiry'] = datetime.utcnow().strftime(utils.TIME_FORMAT)
268
 
        instance.save()
269
 
        inst = model.SessionToken.lookup(instance.identifier)
270
 
        self.assertFalse(inst)
271
 
 
272
 
    def test_session_token_lookup_when_not_expired(self):
273
 
        instance = model.SessionToken.generate("testuser")
274
 
        inst = model.SessionToken.lookup(instance.identifier)
275
 
        self.assert_(inst)
276
 
 
277
 
    def test_session_token_is_expired_when_expired(self):
278
 
        instance = model.SessionToken.generate("testuser")
279
 
        instance['expiry'] = datetime.utcnow().strftime(utils.TIME_FORMAT)
280
 
        self.assert_(instance.is_expired())
281
 
 
282
 
    def test_session_token_is_expired_when_not_expired(self):
283
 
        instance = model.SessionToken.generate("testuser")
284
 
        self.assertFalse(instance.is_expired())
285
 
 
286
 
    def test_session_token_ttl(self):
287
 
        instance = model.SessionToken.generate("testuser")
288
 
        now = datetime.utcnow()
289
 
        delta = timedelta(hours=1)
290
 
        instance['expiry'] = (now + delta).strftime(utils.TIME_FORMAT)
291
 
        # give 5 seconds of fuzziness
292
 
        self.assert_(abs(instance.ttl() - FLAGS.auth_token_ttl) < 5)