diff options
author | zsdc <taras@vyos.io> | 2022-03-25 20:58:01 +0200 |
---|---|---|
committer | zsdc <taras@vyos.io> | 2022-03-25 21:42:00 +0200 |
commit | 31448cccedd8f841fb3ac7d0f2e3cdefe08a53ba (patch) | |
tree | 349631a02467dae0158f6f663cc8aa8537974a97 /tests/unittests/config/test_cc_write_files_deferred.py | |
parent | 5c4b3943343a85fbe517e5ec1fc670b3a8566b4b (diff) | |
parent | 8537237d80a48c8f0cbf8e66aa4826bbc882b022 (diff) | |
download | vyos-cloud-init-31448cccedd8f841fb3ac7d0f2e3cdefe08a53ba.tar.gz vyos-cloud-init-31448cccedd8f841fb3ac7d0f2e3cdefe08a53ba.zip |
T2117: Cloud-init updated to 22.1
Merged with 22.1 tag from the upstream Cloud-init repository.
Our modules were slightly modified for compatibility with the new
version.
Diffstat (limited to 'tests/unittests/config/test_cc_write_files_deferred.py')
-rw-r--r-- | tests/unittests/config/test_cc_write_files_deferred.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/unittests/config/test_cc_write_files_deferred.py b/tests/unittests/config/test_cc_write_files_deferred.py new file mode 100644 index 00000000..17203233 --- /dev/null +++ b/tests/unittests/config/test_cc_write_files_deferred.py @@ -0,0 +1,85 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +import shutil +import tempfile + +from cloudinit import log as logging +from cloudinit import util +from cloudinit.config.cc_write_files_deferred import handle +from tests.unittests.helpers import ( + CiTestCase, + FilesystemMockingTestCase, + mock, + skipUnlessJsonSchema, +) + +from .test_cc_write_files import VALID_SCHEMA + +LOG = logging.getLogger(__name__) + + +@skipUnlessJsonSchema() +@mock.patch("cloudinit.config.cc_write_files_deferred.write_files") +class TestWriteFilesDeferredSchema(CiTestCase): + + with_logs = True + + def test_schema_validation_warns_invalid_value( + self, m_write_files_deferred + ): + """If 'defer' is defined, it must be of type 'bool'.""" + + valid_config = { + "write_files": [ + {**VALID_SCHEMA.get("write_files")[0], "defer": True} + ] + } + + invalid_config = { + "write_files": [ + {**VALID_SCHEMA.get("write_files")[0], "defer": str("no")} + ] + } + + cc = self.tmp_cloud("ubuntu") + handle("cc_write_files_deferred", valid_config, cc, LOG, []) + self.assertNotIn( + "Invalid cloud-config provided:", self.logs.getvalue() + ) + handle("cc_write_files_deferred", invalid_config, cc, LOG, []) + self.assertIn("Invalid cloud-config provided:", self.logs.getvalue()) + self.assertIn( + "defer: 'no' is not of type 'boolean'", self.logs.getvalue() + ) + + +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, LOG, []) + self.assertEqual(util.load_file("/tmp/deferred.file"), expected) + with self.assertRaises(FileNotFoundError): + util.load_file("/tmp/not_deferred.file") + + +# vi: ts=4 expandtab |