From 57ccd89b6af7d3551915df56f548b7617dfcebf9 Mon Sep 17 00:00:00 2001
From: Chad Smith <chad.smith@canonical.com>
Date: Mon, 10 Jan 2022 15:05:52 -0700
Subject: tests: mock LXD datasource detection in ds-identify on LXD containers
 (#1178)

On LXD containers /dev/lxd/sock will always exist.

Mock dscheck_LXD to return 1 (NOT_FOUND) to avoid leaking into
test environment and returning LXD as detected.

We have integration tests covering proper LXD datasource detection
so we don't need a Unit test validating the [ -S /dev/lxd/sock] that
is in ds-identify.
---
 tests/unittests/test_ds_identify.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/tests/unittests/test_ds_identify.py b/tests/unittests/test_ds_identify.py
index f2d2b494..0b0de395 100644
--- a/tests/unittests/test_ds_identify.py
+++ b/tests/unittests/test_ds_identify.py
@@ -92,6 +92,7 @@ MOCK_VIRT_IS_CONTAINER_OTHER = {
     "RET": "container-other",
     "ret": 0,
 }
+MOCK_NOT_LXD_DATASOURCE = {"name": "dscheck_LXD", "ret": 1}
 MOCK_VIRT_IS_KVM = {"name": "detect_virt", "RET": "kvm", "ret": 0}
 MOCK_VIRT_IS_VMWARE = {"name": "detect_virt", "RET": "vmware", "ret": 0}
 # currenty' SmartOS hypervisor "bhyve" is unknown by systemd-detect-virt.
@@ -100,6 +101,8 @@ MOCK_VIRT_IS_XEN = {"name": "detect_virt", "RET": "xen", "ret": 0}
 MOCK_UNAME_IS_PPC64 = {"name": "uname", "out": UNAME_PPC64EL, "ret": 0}
 MOCK_UNAME_IS_FREEBSD = {"name": "uname", "out": UNAME_FREEBSD, "ret": 0}
 
+DEFAULT_MOCKS = [MOCK_NOT_LXD_DATASOURCE]
+
 shell_true = 0
 shell_false = 1
 
@@ -230,6 +233,13 @@ class DsIdentifyBase(CiTestCase):
                 xwargs[k] = data[k]
             if k in kwargs:
                 xwargs[k] = kwargs[k]
+        if "mocks" not in xwargs:
+            xwargs["mocks"] = DEFAULT_MOCKS
+        else:
+            mocked_funcs = [m["name"] for m in xwargs["mocks"]]
+            for default_mock in DEFAULT_MOCKS:
+                if default_mock["name"] not in mocked_funcs:
+                    xwargs["mocks"].append(default_mock)
 
         return self.call(**xwargs)
 
-- 
cgit v1.2.3