summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Patterson <cpatterson@microsoft.com>2021-12-14 11:53:56 -0500
committerGitHub <noreply@github.com>2021-12-14 10:53:56 -0600
commit3da3bdae5e5b41bb3a9f6b23c8573c8fbc23e629 (patch)
treedea3aa2f6aeceeadee748719a8e4fa88dfa05eeb
parent9a6e65a2a575055aadc1802004dbe3f343a54b89 (diff)
downloadvyos-cloud-init-3da3bdae5e5b41bb3a9f6b23c8573c8fbc23e629.tar.gz
vyos-cloud-init-3da3bdae5e5b41bb3a9f6b23c8573c8fbc23e629.zip
tests/cmd/query: fix test run as root and add coverage for defaults (#1156)
test_handle_args_error_on_invalid_vaname_paths() would fail when run as root due to invocation of load_userdata() on the default user/vendor data locations under the instance link. - Mock load_userdata() for this test case to avoid loads. - Update _setup_paths() to configure cloud_dir in temporary location. - Add new test case to verify that the default locations are loaded when unspecified. LP: #1825027 Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
-rw-r--r--tests/unittests/cmd/test_query.py44
1 files changed, 41 insertions, 3 deletions
diff --git a/tests/unittests/cmd/test_query.py b/tests/unittests/cmd/test_query.py
index b3f1d98d..b7d02d13 100644
--- a/tests/unittests/cmd/test_query.py
+++ b/tests/unittests/cmd/test_query.py
@@ -2,8 +2,9 @@
import errno
import gzip
-from io import BytesIO
import json
+import os
+from io import BytesIO
from textwrap import dedent
import pytest
@@ -51,8 +52,14 @@ class TestQuery:
vendor_data = None
run_dir = tmpdir.join('run_dir')
run_dir.ensure_dir()
+
+ cloud_dir = tmpdir.join('cloud_dir')
+ cloud_dir.ensure_dir()
+
return (
- Paths({'run_dir': run_dir.strpath}),
+ Paths(
+ {'cloud_dir': cloud_dir.strpath, 'run_dir': run_dir.strpath}
+ ),
run_dir,
user_data,
vendor_data
@@ -106,7 +113,9 @@ class TestQuery:
with mock.patch(
"cloudinit.cmd.query.addLogHandlerCLI", return_value=""
):
- assert 1 == query.handle_args('anyname', args)
+ with mock.patch('cloudinit.cmd.query.load_userdata') as m_lud:
+ m_lud.return_value = "ud"
+ assert 1 == query.handle_args('anyname', args)
assert expected_error in caplog.text
def test_handle_args_error_on_missing_instance_data(self, caplog, tmpdir):
@@ -208,6 +217,35 @@ class TestQuery:
assert ud_expected == cmd_output['userdata']
assert vd_expected == cmd_output['vendordata']
+ def test_handle_args_user_vendor_data_defaults_to_instance_link(
+ self, capsys, tmpdir
+ ):
+ """When no instance_data argument, root uses sensitive json."""
+ paths, run_dir, _, _ = self._setup_paths(tmpdir)
+ sensitive_file = run_dir.join(INSTANCE_JSON_SENSITIVE_FILE)
+ sensitive_file.write('{"my-var": "it worked"}')
+
+ ud_path = os.path.join(paths.instance_link, "user-data.txt")
+ write_file(ud_path, "instance_link_ud")
+ vd_path = os.path.join(paths.instance_link, "vendor-data.txt")
+ write_file(vd_path, "instance_link_vd")
+
+ args = self.args(
+ debug=False, dump_all=True, format=None, instance_data=None,
+ list_keys=False, user_data=None,
+ vendor_data=None, varname=None)
+ with mock.patch('cloudinit.cmd.query.read_cfg_paths') as m_paths:
+ m_paths.return_value = paths
+ with mock.patch('os.getuid', return_value=0):
+ assert 0 == query.handle_args('anyname', args)
+ expected = (
+ '{\n "my-var": "it worked",\n '
+ '"userdata": "instance_link_ud",\n '
+ '"vendordata": "instance_link_vd"\n}\n'
+ )
+ out, _ = capsys.readouterr()
+ assert expected == out
+
def test_handle_args_root_uses_instance_sensitive_data(
self, capsys, tmpdir
):