~mdragon/nova/multi-tenant-accounting

« back to all changes in this revision

Viewing changes to nova/tests/test_zones.py

  • Committer: Monsyne Dragon
  • Date: 2011-03-11 19:49:32 UTC
  • mfrom: (752.2.34 nova)
  • Revision ID: mdragon@rackspace.com-20110311194932-iqst41u5jymlu66j
remerge trunk (again). fix issues caused by changes to deserialization calls on controllers. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 United States Government as represented by the
 
2
# All Rights Reserved.
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
"""
 
16
Tests For ZoneManager
 
17
"""
 
18
 
 
19
import datetime
 
20
import mox
 
21
import novaclient
 
22
 
 
23
from nova import context
 
24
from nova import db
 
25
from nova import flags
 
26
from nova import service
 
27
from nova import test
 
28
from nova import rpc
 
29
from nova import utils
 
30
from nova.auth import manager as auth_manager
 
31
from nova.scheduler import zone_manager
 
32
 
 
33
FLAGS = flags.FLAGS
 
34
 
 
35
 
 
36
class FakeZone:
 
37
    """Represents a fake zone from the db"""
 
38
    def __init__(self, *args, **kwargs):
 
39
        for k, v in kwargs.iteritems():
 
40
            setattr(self, k, v)
 
41
 
 
42
 
 
43
def exploding_novaclient(zone):
 
44
    """Used when we want to simulate a novaclient call failing."""
 
45
    raise Exception("kaboom")
 
46
 
 
47
 
 
48
class ZoneManagerTestCase(test.TestCase):
 
49
    """Test case for zone manager"""
 
50
    def test_ping(self):
 
51
        zm = zone_manager.ZoneManager()
 
52
        self.mox.StubOutWithMock(zm, '_refresh_from_db')
 
53
        self.mox.StubOutWithMock(zm, '_poll_zones')
 
54
        zm._refresh_from_db(mox.IgnoreArg())
 
55
        zm._poll_zones(mox.IgnoreArg())
 
56
 
 
57
        self.mox.ReplayAll()
 
58
        zm.ping(None)
 
59
        self.mox.VerifyAll()
 
60
 
 
61
    def test_refresh_from_db_new(self):
 
62
        zm = zone_manager.ZoneManager()
 
63
 
 
64
        self.mox.StubOutWithMock(db, 'zone_get_all')
 
65
        db.zone_get_all(mox.IgnoreArg()).AndReturn([
 
66
               FakeZone(id=1, api_url='http://foo.com', username='user1',
 
67
                    password='pass1'),
 
68
            ])
 
69
 
 
70
        self.assertEquals(len(zm.zone_states), 0)
 
71
 
 
72
        self.mox.ReplayAll()
 
73
        zm._refresh_from_db(None)
 
74
        self.mox.VerifyAll()
 
75
 
 
76
        self.assertEquals(len(zm.zone_states), 1)
 
77
        self.assertEquals(zm.zone_states[1].username, 'user1')
 
78
 
 
79
    def test_refresh_from_db_replace_existing(self):
 
80
        zm = zone_manager.ZoneManager()
 
81
        zone_state = zone_manager.ZoneState()
 
82
        zone_state.update_credentials(FakeZone(id=1, api_url='http://foo.com',
 
83
                        username='user1', password='pass1'))
 
84
        zm.zone_states[1] = zone_state
 
85
 
 
86
        self.mox.StubOutWithMock(db, 'zone_get_all')
 
87
        db.zone_get_all(mox.IgnoreArg()).AndReturn([
 
88
               FakeZone(id=1, api_url='http://foo.com', username='user2',
 
89
                    password='pass2'),
 
90
            ])
 
91
 
 
92
        self.assertEquals(len(zm.zone_states), 1)
 
93
 
 
94
        self.mox.ReplayAll()
 
95
        zm._refresh_from_db(None)
 
96
        self.mox.VerifyAll()
 
97
 
 
98
        self.assertEquals(len(zm.zone_states), 1)
 
99
        self.assertEquals(zm.zone_states[1].username, 'user2')
 
100
 
 
101
    def test_refresh_from_db_missing(self):
 
102
        zm = zone_manager.ZoneManager()
 
103
        zone_state = zone_manager.ZoneState()
 
104
        zone_state.update_credentials(FakeZone(id=1, api_url='http://foo.com',
 
105
                        username='user1', password='pass1'))
 
106
        zm.zone_states[1] = zone_state
 
107
 
 
108
        self.mox.StubOutWithMock(db, 'zone_get_all')
 
109
        db.zone_get_all(mox.IgnoreArg()).AndReturn([])
 
110
 
 
111
        self.assertEquals(len(zm.zone_states), 1)
 
112
 
 
113
        self.mox.ReplayAll()
 
114
        zm._refresh_from_db(None)
 
115
        self.mox.VerifyAll()
 
116
 
 
117
        self.assertEquals(len(zm.zone_states), 0)
 
118
 
 
119
    def test_refresh_from_db_add_and_delete(self):
 
120
        zm = zone_manager.ZoneManager()
 
121
        zone_state = zone_manager.ZoneState()
 
122
        zone_state.update_credentials(FakeZone(id=1, api_url='http://foo.com',
 
123
                        username='user1', password='pass1'))
 
124
        zm.zone_states[1] = zone_state
 
125
 
 
126
        self.mox.StubOutWithMock(db, 'zone_get_all')
 
127
 
 
128
        db.zone_get_all(mox.IgnoreArg()).AndReturn([
 
129
               FakeZone(id=2, api_url='http://foo.com', username='user2',
 
130
                    password='pass2'),
 
131
            ])
 
132
        self.assertEquals(len(zm.zone_states), 1)
 
133
 
 
134
        self.mox.ReplayAll()
 
135
        zm._refresh_from_db(None)
 
136
        self.mox.VerifyAll()
 
137
 
 
138
        self.assertEquals(len(zm.zone_states), 1)
 
139
        self.assertEquals(zm.zone_states[2].username, 'user2')
 
140
 
 
141
    def test_poll_zone(self):
 
142
        self.mox.StubOutWithMock(zone_manager, '_call_novaclient')
 
143
        zone_manager._call_novaclient(mox.IgnoreArg()).AndReturn(
 
144
                        dict(name='zohan', capabilities='hairdresser'))
 
145
 
 
146
        zone_state = zone_manager.ZoneState()
 
147
        zone_state.update_credentials(FakeZone(id=2,
 
148
                       api_url='http://foo.com', username='user2',
 
149
                       password='pass2'))
 
150
        zone_state.attempt = 1
 
151
 
 
152
        self.mox.ReplayAll()
 
153
        zone_manager._poll_zone(zone_state)
 
154
        self.mox.VerifyAll()
 
155
        self.assertEquals(zone_state.attempt, 0)
 
156
        self.assertEquals(zone_state.name, 'zohan')
 
157
 
 
158
    def test_poll_zone_fails(self):
 
159
        self.stubs.Set(zone_manager, "_call_novaclient", exploding_novaclient)
 
160
 
 
161
        zone_state = zone_manager.ZoneState()
 
162
        zone_state.update_credentials(FakeZone(id=2,
 
163
                       api_url='http://foo.com', username='user2',
 
164
                       password='pass2'))
 
165
        zone_state.attempt = FLAGS.zone_failures_to_offline - 1
 
166
 
 
167
        self.mox.ReplayAll()
 
168
        zone_manager._poll_zone(zone_state)
 
169
        self.mox.VerifyAll()
 
170
        self.assertEquals(zone_state.attempt, 3)
 
171
        self.assertFalse(zone_state.is_active)
 
172
        self.assertEquals(zone_state.name, None)