summaryrefslogtreecommitdiff
path: root/cloudinit/tests/test_temp_utils.py
blob: ffbb92cd99a464f95f70b9e94af41b22060a143d (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
# This file is part of cloud-init. See LICENSE file for license information.

"""Tests for cloudinit.temp_utils"""

from cloudinit.temp_utils import mkdtemp, mkstemp
from cloudinit.tests.helpers import CiTestCase, wrap_and_call


class TestTempUtils(CiTestCase):

    def test_mkdtemp_default_non_root(self):
        """mkdtemp creates a dir under /tmp for the unprivileged."""
        calls = []

        def fake_mkdtemp(*args, **kwargs):
            calls.append(kwargs)
            return '/fake/return/path'

        retval = wrap_and_call(
            'cloudinit.temp_utils',
            {'os.getuid': 1000,
             'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
             '_TMPDIR': {'new': None},
             'os.path.isdir': True},
            mkdtemp)
        self.assertEqual('/fake/return/path', retval)
        self.assertEqual([{'dir': '/tmp'}], calls)

    def test_mkdtemp_default_non_root_needs_exe(self):
        """mkdtemp creates a dir under /var/tmp/cloud-init when needs_exe."""
        calls = []

        def fake_mkdtemp(*args, **kwargs):
            calls.append(kwargs)
            return '/fake/return/path'

        retval = wrap_and_call(
            'cloudinit.temp_utils',
            {'os.getuid': 1000,
             'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
             '_TMPDIR': {'new': None},
             'os.path.isdir': True},
            mkdtemp, needs_exe=True)
        self.assertEqual('/fake/return/path', retval)
        self.assertEqual([{'dir': '/var/tmp/cloud-init'}], calls)

    def test_mkdtemp_default_root(self):
        """mkdtemp creates a dir under /run/cloud-init for the privileged."""
        calls = []

        def fake_mkdtemp(*args, **kwargs):
            calls.append(kwargs)
            return '/fake/return/path'

        retval = wrap_and_call(
            'cloudinit.temp_utils',
            {'os.getuid': 0,
             'tempfile.mkdtemp': {'side_effect': fake_mkdtemp},
             '_TMPDIR': {'new': None},
             'os.path.isdir': True},
            mkdtemp)
        self.assertEqual('/fake/return/path', retval)
        self.assertEqual([{'dir': '/run/cloud-init/tmp'}], calls)

    def test_mkstemp_default_non_root(self):
        """mkstemp creates secure tempfile under /tmp for the unprivileged."""
        calls = []

        def fake_mkstemp(*args, **kwargs):
            calls.append(kwargs)
            return '/fake/return/path'

        retval = wrap_and_call(
            'cloudinit.temp_utils',
            {'os.getuid': 1000,
             'tempfile.mkstemp': {'side_effect': fake_mkstemp},
             '_TMPDIR': {'new': None},
             'os.path.isdir': True},
            mkstemp)
        self.assertEqual('/fake/return/path', retval)
        self.assertEqual([{'dir': '/tmp'}], calls)

    def test_mkstemp_default_root(self):
        """mkstemp creates a secure tempfile in /run/cloud-init for root."""
        calls = []

        def fake_mkstemp(*args, **kwargs):
            calls.append(kwargs)
            return '/fake/return/path'

        retval = wrap_and_call(
            'cloudinit.temp_utils',
            {'os.getuid': 0,
             'tempfile.mkstemp': {'side_effect': fake_mkstemp},
             '_TMPDIR': {'new': None},
             'os.path.isdir': True},
            mkstemp)
        self.assertEqual('/fake/return/path', retval)
        self.assertEqual([{'dir': '/run/cloud-init/tmp'}], calls)

# vi: ts=4 expandtab