Update methods to mock os.path.exists

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/84341/head
Abhijeet Kasurde 2 days ago
parent 6da46f3df4
commit 8d059c93a4

@ -50,6 +50,32 @@ def mock_dev_kvm(filename):
return filename in ("/dev/kvm",) return filename in ("/dev/kvm",)
def mock_filepath_rhv(filename):
return filename in (
"/sys/devices/virtual/dmi/id/sys_vendor",
"/sys/devices/virtual/dmi/id/product_family",
)
def mock_filepath_rhev(filename):
return filename in (
"/sys/devices/virtual/dmi/id/sys_vendor",
"/sys/devices/virtual/dmi/id/product_name",
)
def mock_filepath_product_name(filename):
return filename in ("/sys/devices/virtual/dmi/id/product_name",)
def mock_filepath_bios_vendor(filename):
return filename in ("/sys/devices/virtual/dmi/id/bios_vendor",)
def mock_filepath_sys_vendor(filename):
return filename in ("/sys/devices/virtual/dmi/id/sys_vendor",)
def mock_get_file_content_xen(filename): def mock_get_file_content_xen(filename):
return "Xen" if filename == "/sys/devices/virtual/dmi/id/bios_vendor" else None return "Xen" if filename == "/sys/devices/virtual/dmi/id/bios_vendor" else None
@ -58,10 +84,6 @@ def mock_get_file_content_kvm(filename):
return "KVM" if filename == "/sys/devices/virtual/dmi/id/product_name" else None return "KVM" if filename == "/sys/devices/virtual/dmi/id/product_name" else None
def mock_sys_vendor_exists(filename):
return True if filename in ("/sys/devices/virtual/dmi/id/sys_vendor",) else False
def mock_get_file_content_ovirt(filename): def mock_get_file_content_ovirt(filename):
return "oVirt" if filename == "/sys/devices/virtual/dmi/id/sys_vendor" else None return "oVirt" if filename == "/sys/devices/virtual/dmi/id/sys_vendor" else None
@ -352,6 +374,7 @@ def test_get_virtual_facts_procxen_guest(mocker):
def test_get_virtual_facts_kvm_guest(mocker): def test_get_virtual_facts_kvm_guest(mocker):
mocker.patch("os.path.exists", side_effect=mock_filepath_product_name)
mocker.patch( mocker.patch(
"ansible.module_utils.facts.virtual.linux.get_file_content", "ansible.module_utils.facts.virtual.linux.get_file_content",
side_effect=mock_get_file_content_kvm, side_effect=mock_get_file_content_kvm,
@ -370,7 +393,7 @@ def test_get_virtual_facts_kvm_guest(mocker):
def test_get_virtual_facts_ovirt_guest(mocker): def test_get_virtual_facts_ovirt_guest(mocker):
mocker.patch("os.path.exists", side_effect=mock_sys_vendor_exists) mocker.patch("os.path.exists", side_effect=mock_filepath_sys_vendor)
mocker.patch( mocker.patch(
"ansible.module_utils.facts.virtual.linux.get_file_content", "ansible.module_utils.facts.virtual.linux.get_file_content",
side_effect=mock_get_file_content_ovirt, side_effect=mock_get_file_content_ovirt,
@ -389,66 +412,78 @@ def test_get_virtual_facts_ovirt_guest(mocker):
@pytest.mark.parametrize( @pytest.mark.parametrize(
("mock_method", "expected_hypervisor"), ("mock_method", "mock_filepath_method", "expected_hypervisor"),
[ [
pytest.param( pytest.param(
mock_get_file_content_rhv, mock_get_file_content_rhv,
mock_filepath_rhv,
"RHV", "RHV",
id="RHV", id="RHV",
), ),
pytest.param( pytest.param(
mock_get_file_content_rhev, mock_get_file_content_rhev,
mock_filepath_rhev,
"RHEV", "RHEV",
id="RHEV", id="RHEV",
), ),
pytest.param( pytest.param(
mock_get_file_content_vmware, mock_get_file_content_vmware,
mock_filepath_product_name,
"VMware", "VMware",
id="VMware", id="VMware",
), ),
pytest.param( pytest.param(
mock_get_file_content_openstack, mock_get_file_content_openstack,
mock_filepath_product_name,
"openstack", "openstack",
id="OpenStack", id="OpenStack",
), ),
pytest.param( pytest.param(
mock_get_file_content_xen_bios_vendor, mock_get_file_content_xen_bios_vendor,
mock_filepath_bios_vendor,
"xen", "xen",
id="Xen", id="Xen",
), ),
pytest.param( pytest.param(
mock_get_file_content_virtualbox, mock_get_file_content_virtualbox,
mock_filepath_bios_vendor,
"virtualbox", "virtualbox",
id="VirtualBox", id="VirtualBox",
), ),
pytest.param( pytest.param(
mock_get_file_content_nutanix, mock_get_file_content_nutanix,
mock_filepath_sys_vendor,
"kvm", "kvm",
id="Nutanix", id="Nutanix",
), ),
pytest.param( pytest.param(
mock_get_file_content_kubevirt, mock_get_file_content_kubevirt,
mock_filepath_sys_vendor,
"KubeVirt", "KubeVirt",
id="KubeVirt", id="KubeVirt",
), ),
pytest.param( pytest.param(
mock_get_file_content_microsoft, mock_get_file_content_microsoft,
mock_filepath_sys_vendor,
"VirtualPC", "VirtualPC",
id="VirtualPC", id="VirtualPC",
), ),
pytest.param( pytest.param(
mock_get_file_content_parallel, mock_get_file_content_parallel,
mock_filepath_sys_vendor,
"parallels", "parallels",
id="Parallels", id="Parallels",
), ),
pytest.param( pytest.param(
mock_get_file_content_openstack_sys, mock_get_file_content_openstack_sys,
mock_filepath_sys_vendor,
"openstack", "openstack",
id="OpenStack Foundation", id="OpenStack Foundation",
), ),
], ],
) )
def test_get_virtual_facts_guest(mocker, mock_method, expected_hypervisor): def test_get_virtual_facts_guest(mocker, mock_method, mock_filepath_method, expected_hypervisor):
mocker.patch("os.path.exists", side_effect=mock_filepath_method)
mocker.patch( mocker.patch(
"ansible.module_utils.facts.virtual.linux.get_file_content", "ansible.module_utils.facts.virtual.linux.get_file_content",
side_effect=mock_method, side_effect=mock_method,
@ -666,6 +701,7 @@ def test_get_virtual_facts_dev_kvm(mocker):
def test_get_virtual_facts_na(mocker): def test_get_virtual_facts_na(mocker):
mocker.patch("os.path.exists", return_value=False)
module = mocker.Mock() module = mocker.Mock()
module.run_command.return_value = (0, "", "") module.run_command.return_value = (0, "", "")
inst = linux.LinuxVirtual(module) inst = linux.LinuxVirtual(module)

Loading…
Cancel
Save