summaryrefslogtreecommitdiff
path: root/nova/tests/unit/objects/test_console_auth_token.py
blob: 9a0901e12a7f9a076a128b846d2eeb7cd7ed361c (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
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
#    Copyright 2016 Intel Corp.
#    Copyright 2016 Hewlett Packard Enterprise Development Company LP
#
#    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 copy
from unittest import mock
import urllib.parse as urlparse

from oslo_db.exception import DBDuplicateEntry
from oslo_utils.fixture import uuidsentinel
from oslo_utils import timeutils

from nova import exception
from nova.objects import console_auth_token as token_obj
from nova.tests.unit import fake_console_auth_token as fakes
from nova.tests.unit.objects import test_objects


class _TestConsoleAuthToken(object):

    @mock.patch('nova.db.main.api.console_auth_token_create')
    def _test_authorize(self, console_type, mock_create):
        # the expires time is calculated from the current time and
        # a ttl value in the object. Fix the current time so we can
        # test expires is calculated correctly as expected
        self.addCleanup(timeutils.clear_time_override)
        timeutils.set_time_override()
        ttl = 10
        expires = timeutils.utcnow_ts() + ttl

        db_dict = copy.deepcopy(fakes.fake_token_dict)
        db_dict['expires'] = expires
        db_dict['console_type'] = console_type
        mock_create.return_value = db_dict

        create_dict = copy.deepcopy(fakes.fake_token_dict)
        create_dict['expires'] = expires
        create_dict['console_type'] = console_type
        del create_dict['id']
        del create_dict['created_at']
        del create_dict['updated_at']

        expected = copy.deepcopy(fakes.fake_token_dict)
        del expected['token_hash']
        del expected['expires']
        expected['token'] = fakes.fake_token
        expected['console_type'] = console_type

        obj = token_obj.ConsoleAuthToken(
            context=self.context,
            console_type=console_type,
            host=fakes.fake_token_dict['host'],
            port=fakes.fake_token_dict['port'],
            internal_access_path=fakes.fake_token_dict['internal_access_path'],
            instance_uuid=fakes.fake_token_dict['instance_uuid'],
            access_url_base=fakes.fake_token_dict['access_url_base'],
        )
        with mock.patch('uuid.uuid4', return_value=fakes.fake_token):
            token = obj.authorize(ttl)

        mock_create.assert_called_once_with(self.context, create_dict)
        self.assertEqual(token, fakes.fake_token)
        self.compare_obj(obj, expected)

        url = obj.access_url
        if console_type != 'novnc':
            expected_url = '%s?token=%s' % (
                fakes.fake_token_dict['access_url_base'],
                fakes.fake_token)
        else:
            path = urlparse.urlencode({'path': '?token=%s' % fakes.fake_token})
            expected_url = '%s?%s' % (
                fakes.fake_token_dict['access_url_base'],
                path)
        self.assertEqual(expected_url, url)

    def test_authorize(self):
        self._test_authorize(fakes.fake_token_dict['console_type'])

    def test_authorize_novnc(self):
        self._test_authorize('novnc')

    @mock.patch('nova.db.main.api.console_auth_token_create')
    def test_authorize_duplicate_token(self, mock_create):
        mock_create.side_effect = DBDuplicateEntry()

        obj = token_obj.ConsoleAuthToken(
            context=self.context,
            console_type=fakes.fake_token_dict['console_type'],
            host=fakes.fake_token_dict['host'],
            port=fakes.fake_token_dict['port'],
            internal_access_path=fakes.fake_token_dict['internal_access_path'],
            instance_uuid=fakes.fake_token_dict['instance_uuid'],
            access_url_base=fakes.fake_token_dict['access_url_base'],
        )

        self.assertRaises(exception.TokenInUse,
                          obj.authorize,
                          100)

    @mock.patch('nova.db.main.api.console_auth_token_create')
    def test_authorize_instance_not_found(self, mock_create):
        mock_create.side_effect = exception.InstanceNotFound(
            instance_id=fakes.fake_token_dict['instance_uuid'])

        obj = token_obj.ConsoleAuthToken(
            context=self.context,
            console_type=fakes.fake_token_dict['console_type'],
            host=fakes.fake_token_dict['host'],
            port=fakes.fake_token_dict['port'],
            internal_access_path=fakes.fake_token_dict['internal_access_path'],
            instance_uuid=fakes.fake_token_dict['instance_uuid'],
            access_url_base=fakes.fake_token_dict['access_url_base'],
        )

        self.assertRaises(exception.InstanceNotFound,
                          obj.authorize,
                          100)

    @mock.patch('nova.db.main.api.console_auth_token_create')
    def test_authorize_object_already_created(self, mock_create):
        # the expires time is calculated from the current time and
        # a ttl value in the object. Fix the current time so we can
        # test expires is calculated correctly as expected
        self.addCleanup(timeutils.clear_time_override)
        timeutils.set_time_override()
        ttl = 10
        expires = timeutils.utcnow_ts() + ttl

        db_dict = copy.deepcopy(fakes.fake_token_dict)
        db_dict['expires'] = expires
        mock_create.return_value = db_dict

        obj = token_obj.ConsoleAuthToken(
            context=self.context,
            console_type=fakes.fake_token_dict['console_type'],
            host=fakes.fake_token_dict['host'],
            port=fakes.fake_token_dict['port'],
            internal_access_path=fakes.fake_token_dict['internal_access_path'],
            instance_uuid=fakes.fake_token_dict['instance_uuid'],
            access_url_base=fakes.fake_token_dict['access_url_base'],
        )
        obj.authorize(100)
        self.assertRaises(exception.ObjectActionError,
                          obj.authorize,
                          100)

    @mock.patch('nova.db.main.api.console_auth_token_destroy_all_by_instance')
    def test_clean_console_auths_for_instance(self, mock_destroy):
        token_obj.ConsoleAuthToken.clean_console_auths_for_instance(
            self.context, uuidsentinel.instance)
        mock_destroy.assert_called_once_with(
            self.context, uuidsentinel.instance)

    @mock.patch('nova.db.main.api.console_auth_token_destroy_expired')
    def test_clean_expired_console_auths(self, mock_destroy):
        token_obj.ConsoleAuthToken.clean_expired_console_auths(self.context)
        mock_destroy.assert_called_once_with(self.context)

    @mock.patch('nova.db.main.api.console_auth_token_destroy_expired_by_host')
    def test_clean_expired_console_auths_for_host(self, mock_destroy):
        token_obj.ConsoleAuthToken.clean_expired_console_auths_for_host(
            self.context, 'fake-host')
        mock_destroy.assert_called_once_with(
            self.context, 'fake-host')


class TestConsoleAuthToken(test_objects._LocalTest,
                           _TestConsoleAuthToken):
    pass


class TestRemoteConsoleAuthToken(test_objects._RemoteTest,
                                 _TestConsoleAuthToken):
    pass