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

import shutil
import tempfile

from cloudinit import util
from tests.unittests.helpers import TestCase, populate_dir


class TestPathPrefix2Dict(TestCase):
    def setUp(self):
        super(TestPathPrefix2Dict, self).setUp()
        self.tmp = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.tmp)

    def test_required_only(self):
        dirdata = {"f1": b"f1content", "f2": b"f2content"}
        populate_dir(self.tmp, dirdata)

        ret = util.pathprefix2dict(self.tmp, required=["f1", "f2"])
        self.assertEqual(dirdata, ret)

    def test_required_missing(self):
        dirdata = {"f1": b"f1content"}
        populate_dir(self.tmp, dirdata)
        kwargs = {"required": ["f1", "f2"]}
        self.assertRaises(ValueError, util.pathprefix2dict, self.tmp, **kwargs)

    def test_no_required_and_optional(self):
        dirdata = {"f1": b"f1c", "f2": b"f2c"}
        populate_dir(self.tmp, dirdata)

        ret = util.pathprefix2dict(
            self.tmp, required=None, optional=["f1", "f2"]
        )
        self.assertEqual(dirdata, ret)

    def test_required_and_optional(self):
        dirdata = {"f1": b"f1c", "f2": b"f2c"}
        populate_dir(self.tmp, dirdata)

        ret = util.pathprefix2dict(self.tmp, required=["f1"], optional=["f2"])
        self.assertEqual(dirdata, ret)


# vi: ts=4 expandtab