summaryrefslogtreecommitdiff
path: root/nova/tests/unit/objects/test_task_log.py
blob: 6d93ebab4cc25f8813d3d7032b0286d260601b88 (plain)
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
#    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

import iso8601
import mock
from oslo_utils import timeutils

from nova import objects
from nova.tests.unit.objects import test_objects
from nova import utils

NOW = timeutils.utcnow().replace(microsecond=0)

fake_task_log = {
    'created_at': NOW,
    'updated_at': None,
    'deleted_at': None,
    'deleted': 0,
    'id': 1,
    'task_name': 'fake-name',
    'state': 'fake-state',
    'host': 'fake-host',
    'period_beginning': NOW - datetime.timedelta(seconds=10),
    'period_ending': NOW,
    'message': 'fake-message',
    'task_items': 1,
    'errors': 0,
    }


class _TestTaskLog(object):
    @mock.patch('nova.db.main.api.task_log_get', return_value=fake_task_log)
    def test_get(self, mock_get):
        task_log = objects.TaskLog.get(self.context,
                                       fake_task_log['task_name'],
                                       fake_task_log['period_beginning'],
                                       fake_task_log['period_ending'],
                                       fake_task_log['host'],
                                       state=fake_task_log['state'])
        mock_get.assert_called_once_with(
            self.context,
            fake_task_log['task_name'],
            utils.strtime(fake_task_log['period_beginning']),
            utils.strtime(fake_task_log['period_ending']),
            fake_task_log['host'],
            state=fake_task_log['state'])
        self.compare_obj(task_log, fake_task_log)

    @mock.patch('nova.db.main.api.task_log_begin_task')
    def test_begin_task(self, mock_begin_task):
        task_log = objects.TaskLog(self.context)
        task_log.task_name = fake_task_log['task_name']
        task_log.period_beginning = fake_task_log['period_beginning']
        task_log.period_ending = fake_task_log['period_ending']
        task_log.host = fake_task_log['host']
        task_log.task_items = fake_task_log['task_items']
        task_log.message = fake_task_log['message']
        task_log.begin_task()
        mock_begin_task.assert_called_once_with(
            self.context,
            fake_task_log['task_name'],
            fake_task_log['period_beginning'].replace(
                tzinfo=iso8601.UTC),
            fake_task_log['period_ending'].replace(
                tzinfo=iso8601.UTC),
            fake_task_log['host'],
            task_items=fake_task_log['task_items'],
            message=fake_task_log['message'])

    @mock.patch('nova.db.main.api.task_log_end_task')
    def test_end_task(self, mock_end_task):
        task_log = objects.TaskLog(self.context)
        task_log.task_name = fake_task_log['task_name']
        task_log.period_beginning = fake_task_log['period_beginning']
        task_log.period_ending = fake_task_log['period_ending']
        task_log.host = fake_task_log['host']
        task_log.errors = fake_task_log['errors']
        task_log.message = fake_task_log['message']
        task_log.end_task()
        mock_end_task.assert_called_once_with(
            self.context,
            fake_task_log['task_name'],
            fake_task_log['period_beginning'].replace(
                tzinfo=iso8601.UTC),
            fake_task_log['period_ending'].replace(
                tzinfo=iso8601.UTC),
            fake_task_log['host'],
            errors=fake_task_log['errors'],
            message=fake_task_log['message'])


class TestTaskLog(test_objects._LocalTest, _TestTaskLog):
    pass


class TestRemoteTaskLog(test_objects._RemoteTest, _TestTaskLog):
    pass


class _TestTaskLogList(object):
    @mock.patch('nova.db.main.api.task_log_get_all')
    def test_get_all(self, mock_get_all):
        fake_task_logs = [dict(fake_task_log, id=1), dict(fake_task_log, id=2)]
        mock_get_all.return_value = fake_task_logs
        task_logs = objects.TaskLogList.get_all(
            self.context,
            fake_task_log['task_name'],
            fake_task_log['period_beginning'],
            fake_task_log['period_ending'],
            host=fake_task_log['host'],
            state=fake_task_log['state'])
        mock_get_all.assert_called_once_with(
            self.context,
            fake_task_log['task_name'],
            utils.strtime(fake_task_log['period_beginning']),
            utils.strtime(fake_task_log['period_ending']),
            host=fake_task_log['host'],
            state=fake_task_log['state'])
        for index, task_log in enumerate(task_logs):
            self.compare_obj(task_log, fake_task_logs[index])


class TestTaskLogList(test_objects._LocalTest, _TestTaskLogList):
    pass


class TestRemoteTaskLogList(test_objects._RemoteTest, _TestTaskLogList):
    pass