From be8fa870a55a694c9840d7411285e1b535e5e931 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Wed, 13 May 2020 16:56:42 -0400 Subject: [PATCH 1/2] cc_snap: apply validation to snap.commands properties --- cloudinit/config/cc_snap.py | 14 ++++++++++++++ cloudinit/config/tests/test_snap.py | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cloudinit/config/cc_snap.py b/cloudinit/config/cc_snap.py index fbe7787a56a..b7a9c5822a2 100644 --- a/cloudinit/config/cc_snap.py +++ b/cloudinit/config/cc_snap.py @@ -85,6 +85,14 @@ 01: ['snap', 'install', 'vlc'] 02: snap install vlc 03: 'snap install vlc' + """), dedent("""\ + # You can use a list of commands + snap: + commands: + - ['install', 'vlc'] + - ['snap', 'install', 'vlc'] + - snap install vlc + - 'snap install vlc' """)], 'frequency': PER_INSTANCE, 'type': 'object', @@ -110,6 +118,12 @@ 'additionalItems': False, # Reject non-string & non-list 'minItems': 1, 'minProperties': 1, + 'additionalProperties': { + 'oneOf': [ + {'type': 'string'}, + {'type': 'array'}, + ], + }, }, 'squashfuse_in_container': { 'type': 'boolean' diff --git a/cloudinit/config/tests/test_snap.py b/cloudinit/config/tests/test_snap.py index 70676942414..e40bd2a7eac 100644 --- a/cloudinit/config/tests/test_snap.py +++ b/cloudinit/config/tests/test_snap.py @@ -310,6 +310,22 @@ def test_schema_when_commands_are_list_or_dict(self, _): {'snap': {'commands': {'01': 'also valid'}}}, schema) self.assertEqual('', self.logs.getvalue()) + @mock.patch('cloudinit.config.cc_snap.run_commands') + def test_schema_when_commands_values_are_invalid_type(self, _): + """Warnings when snap:commands values are invalid type (e.g. int)""" + validate_cloudconfig_schema( + {'snap': {'commands': [123]}}, schema) + validate_cloudconfig_schema( + {'snap': {'commands': {'01': 123}}}, schema) + self.assertEqual( + "WARNING: Invalid config:\n" + "snap.commands.0: 123 is not valid under any of the given" + " schemas\n" + "WARNING: Invalid config:\n" + "snap.commands.01: 123 is not valid under any of the given" + " schemas\n", + self.logs.getvalue()) + @mock.patch('cloudinit.config.cc_snap.add_assertions') def test_warn_schema_assertions_is_not_list_or_dict(self, _): """Warn when snap:assertions config is not a list or dict.""" From 6127caf7e956a3f4ba2467662be3c2f3f755efa6 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Wed, 13 May 2020 17:20:43 -0400 Subject: [PATCH 2/2] cc_snap: command list items must be strings --- cloudinit/config/cc_snap.py | 2 +- cloudinit/config/tests/test_snap.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cloudinit/config/cc_snap.py b/cloudinit/config/cc_snap.py index b7a9c5822a2..3bf2e250b9e 100644 --- a/cloudinit/config/cc_snap.py +++ b/cloudinit/config/cc_snap.py @@ -121,7 +121,7 @@ 'additionalProperties': { 'oneOf': [ {'type': 'string'}, - {'type': 'array'}, + {'type': 'array', 'items': {'type': 'string'}}, ], }, }, diff --git a/cloudinit/config/tests/test_snap.py b/cloudinit/config/tests/test_snap.py index e40bd2a7eac..2be301861bc 100644 --- a/cloudinit/config/tests/test_snap.py +++ b/cloudinit/config/tests/test_snap.py @@ -326,6 +326,22 @@ def test_schema_when_commands_values_are_invalid_type(self, _): " schemas\n", self.logs.getvalue()) + @mock.patch('cloudinit.config.cc_snap.run_commands') + def test_schema_when_commands_list_values_are_invalid_type(self, _): + """Warnings when snap:commands list values are wrong type (e.g. int)""" + validate_cloudconfig_schema( + {'snap': {'commands': [["snap", "install", 123]]}}, schema) + validate_cloudconfig_schema( + {'snap': {'commands': {'01': ["snap", "install", 123]}}}, schema) + self.assertEqual( + "WARNING: Invalid config:\n" + "snap.commands.0: ['snap', 'install', 123] is not valid under any" + " of the given schemas\n", + "WARNING: Invalid config:\n" + "snap.commands.0: ['snap', 'install', 123] is not valid under any" + " of the given schemas\n", + self.logs.getvalue()) + @mock.patch('cloudinit.config.cc_snap.add_assertions') def test_warn_schema_assertions_is_not_list_or_dict(self, _): """Warn when snap:assertions config is not a list or dict."""