summaryrefslogtreecommitdiff
path: root/nova/tests/unit/privsep/test_utils.py
blob: 887e6dfa8b05886726df366a3ab22f0900e08cdd (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
# Copyright 2011 Justin Santa Barbara
#
#    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 errno
import os
from unittest import mock

import nova.privsep.utils
from nova import test


class SupportDirectIOTestCase(test.NoDBTestCase):
    def setUp(self):
        super(SupportDirectIOTestCase, self).setUp()
        # O_DIRECT is not supported on all Python runtimes, so on platforms
        # where it's not supported (e.g. Mac), we can still test the code-path
        # by stubbing out the value.
        if not hasattr(os, 'O_DIRECT'):
            # `mock` seems to have trouble stubbing an attr that doesn't
            # originally exist, so falling back to stubbing out the attribute
            # directly.
            os.O_DIRECT = 16384
            self.addCleanup(delattr, os, 'O_DIRECT')
        self.einval = OSError()
        self.einval.errno = errno.EINVAL
        self.enoent = OSError()
        self.enoent.errno = errno.ENOENT
        self.test_path = os.path.join('.', '.directio.test.123')
        self.io_flags = os.O_CREAT | os.O_WRONLY | os.O_DIRECT

        open_patcher = mock.patch('os.open')
        write_patcher = mock.patch('os.write')
        close_patcher = mock.patch('os.close')
        unlink_patcher = mock.patch('os.unlink')
        random_string_patcher = mock.patch(
            'nova.privsep.utils.generate_random_string', return_value='123')
        self.addCleanup(open_patcher.stop)
        self.addCleanup(write_patcher.stop)
        self.addCleanup(close_patcher.stop)
        self.addCleanup(unlink_patcher.stop)
        self.addCleanup(random_string_patcher.stop)
        self.mock_open = open_patcher.start()
        self.mock_write = write_patcher.start()
        self.mock_close = close_patcher.start()
        self.mock_unlink = unlink_patcher.start()
        random_string_patcher.start()

    def test_supports_direct_io(self):
        self.mock_open.return_value = 3

        self.assertTrue(nova.privsep.utils.supports_direct_io('.'))

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_called_once_with(3, mock.ANY)
        # ensure unlink(filepath) will actually remove the file by deleting
        # the remaining link to it in close(fd)
        self.mock_close.assert_called_once_with(3)
        self.mock_unlink.assert_called_once_with(self.test_path)

    def test_supports_direct_io_with_exception_in_write(self):
        self.mock_open.return_value = 3
        self.mock_write.side_effect = ValueError()

        self.assertRaises(ValueError, nova.privsep.utils.supports_direct_io,
                          '.')

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_called_once_with(3, mock.ANY)
        # ensure unlink(filepath) will actually remove the file by deleting
        # the remaining link to it in close(fd)
        self.mock_close.assert_called_once_with(3)
        self.mock_unlink.assert_called_once_with(self.test_path)

    def test_supports_direct_io_with_exception_in_open(self):
        self.mock_open.side_effect = ValueError()

        self.assertRaises(ValueError, nova.privsep.utils.supports_direct_io,
                          '.')

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_not_called()
        self.mock_close.assert_not_called()
        self.mock_unlink.assert_called_once_with(self.test_path)

    def test_supports_direct_io_with_oserror_in_write(self):
        self.mock_open.return_value = 3
        self.mock_write.side_effect = self.einval

        self.assertFalse(nova.privsep.utils.supports_direct_io('.'))

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_called_once_with(3, mock.ANY)
        # ensure unlink(filepath) will actually remove the file by deleting
        # the remaining link to it in close(fd)
        self.mock_close.assert_called_once_with(3)
        self.mock_unlink.assert_called_once_with(self.test_path)

    def test_supports_direct_io_with_oserror_in_open_einval(self):
        self.mock_open.side_effect = self.einval

        self.assertFalse(nova.privsep.utils.supports_direct_io('.'))

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_not_called()
        self.mock_close.assert_not_called()
        self.mock_unlink.assert_called_once_with(self.test_path)

    def test_supports_direct_io_with_oserror_in_open_enoent(self):
        self.mock_open.side_effect = self.enoent

        self.assertFalse(nova.privsep.utils.supports_direct_io('.'))

        self.mock_open.assert_called_once_with(self.test_path, self.io_flags)
        self.mock_write.assert_not_called()
        self.mock_close.assert_not_called()
        self.mock_unlink.assert_called_once_with(self.test_path)