summaryrefslogtreecommitdiff
path: root/tests/unittests/test_vmware
diff options
context:
space:
mode:
authorBrett Holman <bholman.devel@gmail.com>2021-12-03 13:11:46 -0700
committerGitHub <noreply@github.com>2021-12-03 13:11:46 -0700
commit039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51 (patch)
tree5f1b09486ccaf98ee8159de58d9a2a1ef0af5dc1 /tests/unittests/test_vmware
parentffa6fc88249aa080aa31811a45569a45e567418a (diff)
downloadvyos-cloud-init-039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51.tar.gz
vyos-cloud-init-039c40f9b3d88ee8158604bb18ca4bf2fb5d5e51.zip
Reorganize unit test locations under tests/unittests (#1126)
This attempts to standardize unit test file location under test/unittests/ such that any source file located at cloudinit/path/to/file.py may have a corresponding unit test file at test/unittests/path/to/test_file.py. Noteworthy Comments: ==================== Four different duplicate test files existed: test_{gpg,util,cc_mounts,cc_resolv_conf}.py Each of these duplicate file pairs has been merged together. This is a break in git history for these files. The test suite appears to have a dependency on test order. Changing test order causes some tests to fail. This should be rectified, but for now some tests have been modified in tests/unittests/config/test_set_passwords.py. A helper class name starts with "Test" which causes pytest to try executing it as a test case, which then throws warnings "due to Class having __init__()". Silence by changing the name of the class. # helpers.py is imported in many test files, import paths change cloudinit/tests/helpers.py -> tests/unittests/helpers.py # Move directories: cloudinit/distros/tests -> tests/unittests/distros cloudinit/cmd/devel/tests -> tests/unittests/cmd/devel cloudinit/cmd/tests -> tests/unittests/cmd/ cloudinit/sources/helpers/tests -> tests/unittests/sources/helpers cloudinit/sources/tests -> tests/unittests/sources cloudinit/net/tests -> tests/unittests/net cloudinit/config/tests -> tests/unittests/config cloudinit/analyze/tests/ -> tests/unittests/analyze/ # Standardize tests already in tests/unittests/ test_datasource -> sources test_distros -> distros test_vmware -> sources/vmware test_handler -> config # this contains cloudconfig module tests test_runs -> runs
Diffstat (limited to 'tests/unittests/test_vmware')
-rw-r--r--tests/unittests/test_vmware/__init__.py0
-rw-r--r--tests/unittests/test_vmware/test_custom_script.py109
-rw-r--r--tests/unittests/test_vmware/test_guestcust_util.py98
3 files changed, 0 insertions, 207 deletions
diff --git a/tests/unittests/test_vmware/__init__.py b/tests/unittests/test_vmware/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/tests/unittests/test_vmware/__init__.py
+++ /dev/null
diff --git a/tests/unittests/test_vmware/test_custom_script.py b/tests/unittests/test_vmware/test_custom_script.py
deleted file mode 100644
index f89f8157..00000000
--- a/tests/unittests/test_vmware/test_custom_script.py
+++ /dev/null
@@ -1,109 +0,0 @@
-# Copyright (C) 2015 Canonical Ltd.
-# Copyright (C) 2017-2019 VMware INC.
-#
-# Author: Maitreyee Saikia <msaikia@vmware.com>
-#
-# This file is part of cloud-init. See LICENSE file for license information.
-
-import os
-import stat
-from cloudinit import util
-from cloudinit.sources.helpers.vmware.imc.config_custom_script import (
- CustomScriptConstant,
- CustomScriptNotFound,
- PreCustomScript,
- PostCustomScript,
-)
-from cloudinit.tests.helpers import CiTestCase, mock
-
-
-class TestVmwareCustomScript(CiTestCase):
- def setUp(self):
- self.tmpDir = self.tmp_dir()
- # Mock the tmpDir as the root dir in VM.
- self.execDir = os.path.join(self.tmpDir, ".customization")
- self.execScript = os.path.join(self.execDir,
- ".customize.sh")
-
- def test_prepare_custom_script(self):
- """
- This test is designed to verify the behavior based on the presence of
- custom script. Mainly needed for scenario where a custom script is
- expected, but was not properly copied. "CustomScriptNotFound" exception
- is raised in such cases.
- """
- # Custom script does not exist.
- preCust = PreCustomScript("random-vmw-test", self.tmpDir)
- self.assertEqual("random-vmw-test", preCust.scriptname)
- self.assertEqual(self.tmpDir, preCust.directory)
- self.assertEqual(self.tmp_path("random-vmw-test", self.tmpDir),
- preCust.scriptpath)
- with self.assertRaises(CustomScriptNotFound):
- preCust.prepare_script()
-
- # Custom script exists.
- custScript = self.tmp_path("test-cust", self.tmpDir)
- util.write_file(custScript, "test-CR-strip\r\r")
- with mock.patch.object(CustomScriptConstant,
- "CUSTOM_TMP_DIR",
- self.execDir):
- with mock.patch.object(CustomScriptConstant,
- "CUSTOM_SCRIPT",
- self.execScript):
- postCust = PostCustomScript("test-cust",
- self.tmpDir,
- self.tmpDir)
- self.assertEqual("test-cust", postCust.scriptname)
- self.assertEqual(self.tmpDir, postCust.directory)
- self.assertEqual(custScript, postCust.scriptpath)
- postCust.prepare_script()
-
- # Custom script is copied with exec privilege
- self.assertTrue(os.path.exists(self.execScript))
- st = os.stat(self.execScript)
- self.assertTrue(st.st_mode & stat.S_IEXEC)
- with open(self.execScript, "r") as f:
- content = f.read()
- self.assertEqual(content, "test-CR-strip")
- # Check if all carraige returns are stripped from script.
- self.assertFalse("\r" in content)
-
- def test_execute_post_cust(self):
- """
- This test is designed to verify the behavior after execute post
- customization.
- """
- # Prepare the customize package
- postCustRun = self.tmp_path("post-customize-guest.sh", self.tmpDir)
- util.write_file(postCustRun, "This is the script to run post cust")
- userScript = self.tmp_path("test-cust", self.tmpDir)
- util.write_file(userScript, "This is the post cust script")
-
- # Mock the cc_scripts_per_instance dir and marker file.
- # Create another tmp dir for cc_scripts_per_instance.
- ccScriptDir = self.tmp_dir()
- ccScript = os.path.join(ccScriptDir, "post-customize-guest.sh")
- markerFile = os.path.join(self.tmpDir, ".markerFile")
- with mock.patch.object(CustomScriptConstant,
- "CUSTOM_TMP_DIR",
- self.execDir):
- with mock.patch.object(CustomScriptConstant,
- "CUSTOM_SCRIPT",
- self.execScript):
- with mock.patch.object(CustomScriptConstant,
- "POST_CUSTOM_PENDING_MARKER",
- markerFile):
- postCust = PostCustomScript("test-cust",
- self.tmpDir,
- ccScriptDir)
- postCust.execute()
- # Check cc_scripts_per_instance and marker file
- # are created.
- self.assertTrue(os.path.exists(ccScript))
- with open(ccScript, "r") as f:
- content = f.read()
- self.assertEqual(content,
- "This is the script to run post cust")
- self.assertTrue(os.path.exists(markerFile))
-
-# vi: ts=4 expandtab
diff --git a/tests/unittests/test_vmware/test_guestcust_util.py b/tests/unittests/test_vmware/test_guestcust_util.py
deleted file mode 100644
index c8b59d83..00000000
--- a/tests/unittests/test_vmware/test_guestcust_util.py
+++ /dev/null
@@ -1,98 +0,0 @@
-# Copyright (C) 2019 Canonical Ltd.
-# Copyright (C) 2019 VMware INC.
-#
-# Author: Xiaofeng Wang <xiaofengw@vmware.com>
-#
-# This file is part of cloud-init. See LICENSE file for license information.
-
-from cloudinit import subp
-from cloudinit.sources.helpers.vmware.imc.config import Config
-from cloudinit.sources.helpers.vmware.imc.config_file import ConfigFile
-from cloudinit.sources.helpers.vmware.imc.guestcust_util import (
- get_tools_config,
- set_gc_status,
-)
-from cloudinit.tests.helpers import CiTestCase, mock
-
-
-class TestGuestCustUtil(CiTestCase):
- def test_get_tools_config_not_installed(self):
- """
- This test is designed to verify the behavior if vmware-toolbox-cmd
- is not installed.
- """
- with mock.patch.object(subp, 'which', return_value=None):
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'), 'defaultVal')
-
- def test_get_tools_config_internal_exception(self):
- """
- This test is designed to verify the behavior if internal exception
- is raised.
- """
- with mock.patch.object(subp, 'which', return_value='/dummy/path'):
- with mock.patch.object(subp, 'subp',
- return_value=('key=value', b''),
- side_effect=subp.ProcessExecutionError(
- "subp failed", exit_code=99)):
- # verify return value is 'defaultVal', not 'value'.
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'),
- 'defaultVal')
-
- def test_get_tools_config_normal(self):
- """
- This test is designed to verify the value could be parsed from
- key = value of the given [section]
- """
- with mock.patch.object(subp, 'which', return_value='/dummy/path'):
- # value is not blank
- with mock.patch.object(subp, 'subp',
- return_value=('key = value ', b'')):
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'),
- 'value')
- # value is blank
- with mock.patch.object(subp, 'subp',
- return_value=('key = ', b'')):
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'),
- '')
- # value contains =
- with mock.patch.object(subp, 'subp',
- return_value=('key=Bar=Wark', b'')):
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'),
- 'Bar=Wark')
-
- # value contains specific characters
- with mock.patch.object(subp, 'subp',
- return_value=('[a] b.c_d=e-f', b'')):
- self.assertEqual(
- get_tools_config('section', 'key', 'defaultVal'),
- 'e-f')
-
- def test_set_gc_status(self):
- """
- This test is designed to verify the behavior of set_gc_status
- """
- # config is None, return None
- self.assertEqual(set_gc_status(None, 'Successful'), None)
-
- # post gc status is NO, return None
- cf = ConfigFile("tests/data/vmware/cust-dhcp-2nic.cfg")
- conf = Config(cf)
- self.assertEqual(set_gc_status(conf, 'Successful'), None)
-
- # post gc status is YES, subp is called to execute command
- cf._insertKey("MISC|POST-GC-STATUS", "YES")
- conf = Config(cf)
- with mock.patch.object(subp, 'subp',
- return_value=('ok', b'')) as mockobj:
- self.assertEqual(
- set_gc_status(conf, 'Successful'), ('ok', b''))
- mockobj.assert_called_once_with(
- ['vmware-rpctool', 'info-set guestinfo.gc.status Successful'],
- rcs=[0])
-
-# vi: ts=4 expandtab