summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/objects/test_node_history.py
blob: 780fe7067cec5fcb757d2162cc512861b8faf1f0 (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
#    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 types
from unittest import mock

from testtools.matchers import HasLength

from ironic.common import exception
from ironic import objects
from ironic.tests.unit.db import base as db_base
from ironic.tests.unit.db import utils as db_utils
from ironic.tests.unit.objects import utils as obj_utils


class TestNodeHistoryObject(db_base.DbTestCase, obj_utils.SchemasTestMixIn):

    def setUp(self):
        super(TestNodeHistoryObject, self).setUp()
        self.fake_history = db_utils.get_test_history()

    def test_get_by_id(self):
        with mock.patch.object(self.dbapi, 'get_node_history_by_id',
                               autospec=True) as mock_get:
            id_ = self.fake_history['id']
            mock_get.return_value = self.fake_history

            history = objects.NodeHistory.get_by_id(self.context, id_)

            mock_get.assert_called_once_with(id_)
            self.assertIsInstance(history, objects.NodeHistory)
            self.assertEqual(self.context, history._context)

    def test_get_by_uuid(self):
        uuid = self.fake_history['uuid']
        with mock.patch.object(self.dbapi, 'get_node_history_by_uuid',
                               autospec=True) as mock_get:
            mock_get.return_value = self.fake_history

            history = objects.NodeHistory.get_by_uuid(self.context, uuid)

            mock_get.assert_called_once_with(uuid)
            self.assertIsInstance(history, objects.NodeHistory)
            self.assertEqual(self.context, history._context)

    @mock.patch('ironic.objects.NodeHistory.get_by_uuid',
                spec_set=types.FunctionType)
    @mock.patch('ironic.objects.NodeHistory.get_by_id',
                spec_set=types.FunctionType)
    def test_get(self, mock_get_by_id, mock_get_by_uuid):
        id_ = self.fake_history['id']
        uuid = self.fake_history['uuid']

        objects.NodeHistory.get(self.context, id_)
        mock_get_by_id.assert_called_once_with(self.context, id_)
        self.assertFalse(mock_get_by_uuid.called)

        objects.NodeHistory.get(self.context, uuid)
        mock_get_by_uuid.assert_called_once_with(self.context, uuid)

        # Invalid identifier (not ID or UUID)
        self.assertRaises(exception.InvalidIdentity,
                          objects.NodeHistory.get,
                          self.context, 'not-valid-identifier')

    def test_list(self):
        with mock.patch.object(self.dbapi, 'get_node_history_list',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = [self.fake_history]
            history = objects.NodeHistory.list(
                self.context, limit=4, sort_key='uuid', sort_dir='asc')

            mock_get_list.assert_called_once_with(
                limit=4, marker=None, sort_key='uuid', sort_dir='asc')
            self.assertThat(history, HasLength(1))
            self.assertIsInstance(history[0], objects.NodeHistory)
            self.assertEqual(self.context, history[0]._context)

    def test_list_none(self):
        with mock.patch.object(self.dbapi, 'get_node_history_list',
                               autospec=True) as mock_get_list:
            mock_get_list.return_value = []
            history = objects.NodeHistory.list(
                self.context, limit=4, sort_key='uuid', sort_dir='asc')

            mock_get_list.assert_called_once_with(
                limit=4, marker=None, sort_key='uuid', sort_dir='asc')
            self.assertEqual([], history)

    def test_list_by_node_id(self):
        with mock.patch.object(self.dbapi, 'get_node_history_by_node_id',
                               autospec=True) as mock_get_list_by_node_id:
            mock_get_list_by_node_id.return_value = [self.fake_history]
            node_id = self.fake_history['node_id']
            history = objects.NodeHistory.list_by_node_id(
                self.context, node_id, limit=10, sort_dir='desc')

            mock_get_list_by_node_id.assert_called_once_with(
                node_id, limit=10, marker=None, sort_key=None, sort_dir='desc')
            self.assertThat(history, HasLength(1))
            self.assertIsInstance(history[0], objects.NodeHistory)
            self.assertEqual(self.context, history[0]._context)

    def test_create(self):
        with mock.patch.object(self.dbapi, 'create_node_history',
                               autospec=True) as mock_db_create:
            mock_db_create.return_value = self.fake_history
            new_history = objects.NodeHistory(
                self.context, **self.fake_history)
            new_history.create()

            mock_db_create.assert_called_once_with(self.fake_history)

    def test_destroy(self):
        uuid = self.fake_history['uuid']
        with mock.patch.object(self.dbapi, 'get_node_history_by_uuid',
                               autospec=True) as mock_get:
            mock_get.return_value = self.fake_history
            with mock.patch.object(self.dbapi, 'destroy_node_history_by_uuid',
                                   autospec=True) as mock_db_destroy:
                history = objects.NodeHistory.get_by_uuid(self.context, uuid)
                history.destroy()

                mock_db_destroy.assert_called_once_with(uuid)