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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (c) 2012 OpenStack, LLC
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import datetime

from nova.api.openstack.compute.contrib import instance_usage_audit_log as ial
from nova import context
from nova import db
from nova.openstack.common import timeutils
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils


TEST_COMPUTE_SERVICES = [dict(host='foo', topic='compute'),
                         dict(host='bar', topic='compute'),
                         dict(host='baz', topic='compute'),
                         dict(host='plonk', topic='compute'),
                         dict(host='wibble', topic='bogus'),
                         ]


begin1 = datetime.datetime(2012, 7, 4, 6, 0, 0)
begin2 = end1 = datetime.datetime(2012, 7, 5, 6, 0, 0)
begin3 = end2 = datetime.datetime(2012, 7, 6, 6, 0, 0)
end3 = datetime.datetime(2012, 7, 7, 6, 0, 0)


#test data


TEST_LOGS1 = [
    #all services done, no errors.
    dict(host="plonk", period_beginning=begin1, period_ending=end1,
         state="DONE", errors=0, task_items=23, message="test1"),
    dict(host="baz", period_beginning=begin1, period_ending=end1,
         state="DONE", errors=0, task_items=17, message="test2"),
    dict(host="bar", period_beginning=begin1, period_ending=end1,
         state="DONE", errors=0, task_items=10, message="test3"),
    dict(host="foo", period_beginning=begin1, period_ending=end1,
         state="DONE", errors=0, task_items=7, message="test4"),
    ]


TEST_LOGS2 = [
    #some still running...
    dict(host="plonk", period_beginning=begin2, period_ending=end2,
         state="DONE", errors=0, task_items=23, message="test5"),
    dict(host="baz", period_beginning=begin2, period_ending=end2,
         state="DONE", errors=0, task_items=17, message="test6"),
    dict(host="bar", period_beginning=begin2, period_ending=end2,
         state="RUNNING", errors=0, task_items=10, message="test7"),
    dict(host="foo", period_beginning=begin2, period_ending=end2,
         state="DONE", errors=0, task_items=7, message="test8"),
    ]


TEST_LOGS3 = [
    #some errors..
    dict(host="plonk", period_beginning=begin3, period_ending=end3,
         state="DONE", errors=0, task_items=23, message="test9"),
    dict(host="baz", period_beginning=begin3, period_ending=end3,
         state="DONE", errors=2, task_items=17, message="test10"),
    dict(host="bar", period_beginning=begin3, period_ending=end3,
         state="DONE", errors=0, task_items=10, message="test11"),
    dict(host="foo", period_beginning=begin3, period_ending=end3,
         state="DONE", errors=1, task_items=7, message="test12"),
    ]


def fake_task_log_get_all(context, task_name, begin, end,
                          host=None, state=None):
    assert task_name == "instance_usage_audit"

    if begin == begin1 and end == end1:
        return TEST_LOGS1
    if begin == begin2 and end == end2:
        return TEST_LOGS2
    if begin == begin3 and end == end3:
        return TEST_LOGS3
    raise AssertionError("Invalid date %s to %s" % (begin, end))


def fake_last_completed_audit_period(unit=None, before=None):
    audit_periods = [(begin3, end3),
                     (begin2, end2),
                     (begin1, end1)]
    if before is not None:
        for begin, end in audit_periods:
            if before > end:
                return begin, end
        raise AssertionError("Invalid before date %s" % (before))
    return begin1, end1


class InstanceUsageAuditLogTest(test.TestCase):
    def setUp(self):
        super(InstanceUsageAuditLogTest, self).setUp()
        self.context = context.get_admin_context()
        timeutils.set_time_override(datetime.datetime(2012, 7, 5, 10, 0, 0))
        self.controller = ial.InstanceUsageAuditLogController()
        self.host_api = self.controller.host_api

        def fake_service_get_all(context, disabled):
            self.assertTrue(disabled is None)
            return TEST_COMPUTE_SERVICES

        self.stubs.Set(utils, 'last_completed_audit_period',
                            fake_last_completed_audit_period)
        self.stubs.Set(db, 'service_get_all',
                       fake_service_get_all)
        self.stubs.Set(db, 'task_log_get_all',
                       fake_task_log_get_all)

    def tearDown(self):
        super(InstanceUsageAuditLogTest, self).tearDown()
        timeutils.clear_time_override()

    def test_index(self):
        req = fakes.HTTPRequest.blank('/v2/fake/os-instance_usage_audit_log')
        result = self.controller.index(req)
        self.assertIn('instance_usage_audit_logs', result)
        logs = result['instance_usage_audit_logs']
        self.assertEquals(57, logs['total_instances'])
        self.assertEquals(0, logs['total_errors'])
        self.assertEquals(4, len(logs['log']))
        self.assertEquals(4, logs['num_hosts'])
        self.assertEquals(4, logs['num_hosts_done'])
        self.assertEquals(0, logs['num_hosts_running'])
        self.assertEquals(0, logs['num_hosts_not_run'])
        self.assertEquals("ALL hosts done. 0 errors.", logs['overall_status'])

    def test_show(self):
        req = fakes.HTTPRequest.blank(
                    '/v2/fake/os-instance_usage_audit_log/show')
        result = self.controller.show(req, '2012-07-05 10:00:00')
        self.assertIn('instance_usage_audit_log', result)
        logs = result['instance_usage_audit_log']
        self.assertEquals(57, logs['total_instances'])
        self.assertEquals(0, logs['total_errors'])
        self.assertEquals(4, len(logs['log']))
        self.assertEquals(4, logs['num_hosts'])
        self.assertEquals(4, logs['num_hosts_done'])
        self.assertEquals(0, logs['num_hosts_running'])
        self.assertEquals(0, logs['num_hosts_not_run'])
        self.assertEquals("ALL hosts done. 0 errors.", logs['overall_status'])

    def test_show_with_running(self):
        req = fakes.HTTPRequest.blank(
                    '/v2/fake/os-instance_usage_audit_log/show')
        result = self.controller.show(req, '2012-07-06 10:00:00')
        self.assertIn('instance_usage_audit_log', result)
        logs = result['instance_usage_audit_log']
        self.assertEquals(57, logs['total_instances'])
        self.assertEquals(0, logs['total_errors'])
        self.assertEquals(4, len(logs['log']))
        self.assertEquals(4, logs['num_hosts'])
        self.assertEquals(3, logs['num_hosts_done'])
        self.assertEquals(1, logs['num_hosts_running'])
        self.assertEquals(0, logs['num_hosts_not_run'])
        self.assertEquals("3 of 4 hosts done. 0 errors.",
                          logs['overall_status'])

    def test_show_with_errors(self):
        req = fakes.HTTPRequest.blank(
                    '/v2/fake/os-instance_usage_audit_log/show')
        result = self.controller.show(req, '2012-07-07 10:00:00')
        self.assertIn('instance_usage_audit_log', result)
        logs = result['instance_usage_audit_log']
        self.assertEquals(57, logs['total_instances'])
        self.assertEquals(3, logs['total_errors'])
        self.assertEquals(4, len(logs['log']))
        self.assertEquals(4, logs['num_hosts'])
        self.assertEquals(4, logs['num_hosts_done'])
        self.assertEquals(0, logs['num_hosts_running'])
        self.assertEquals(0, logs['num_hosts_not_run'])
        self.assertEquals("ALL hosts done. 3 errors.",
                          logs['overall_status'])