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

import shutil
import tempfile

import pytest

from cloudinit import log as logging
from cloudinit import util
from cloudinit.config.cc_write_files_deferred import handle
from cloudinit.config.schema import (
    SchemaValidationError,
    get_schema,
    validate_cloudconfig_schema,
)
from tests.unittests.helpers import (
    FilesystemMockingTestCase,
    skipUnlessJsonSchema,
)

LOG = logging.getLogger(__name__)


class TestWriteFilesDeferred(FilesystemMockingTestCase):

    with_logs = True

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

    def test_filtering_deferred_files(self):
        self.patchUtils(self.tmp)
        expected = "hello world\n"
        config = {
            "write_files": [
                {
                    "path": "/tmp/deferred.file",
                    "defer": True,
                    "content": expected,
                },
                {"path": "/tmp/not_deferred.file"},
            ]
        }
        cc = self.tmp_cloud("ubuntu")
        handle("cc_write_files_deferred", config, cc, [])
        self.assertEqual(util.load_file("/tmp/deferred.file"), expected)
        with self.assertRaises(FileNotFoundError):
            util.load_file("/tmp/not_deferred.file")


class TestWriteFilesDeferredSchema:
    @pytest.mark.parametrize(
        "config, error_msg",
        [
            # Allow undocumented keys client keys without error
            (
                {"write_files": [{"defer": "no"}]},
                "write_files.0.defer: 'no' is not of type 'boolean'",
            ),
        ],
    )
    @skipUnlessJsonSchema()
    def test_schema_validation(self, config, error_msg):
        with pytest.raises(SchemaValidationError, match=error_msg):
            validate_cloudconfig_schema(config, get_schema(), strict=True)


# vi: ts=4 expandtab