From 95fcfa463c091d64f68fbd95e815d237937038a3 Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <Victor.Zhestkov@suse.com>
Date: Fri, 28 Oct 2022 13:19:46 +0300
Subject: [PATCH] Align Amazon EC2 (Nitro) grains with upstream PR
 (bsc#1203685)

* Set virtual to Nitro for Amazon EC2 kvm instances

* Add few mocks to prevent false failing

possible in some specific environments

* Add one more possible test case returning Nitro
---
 salt/grains/core.py                    |  8 +++++++-
 tests/pytests/unit/grains/test_core.py | 27 +++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/salt/grains/core.py b/salt/grains/core.py
index b543144da2..48436e36c2 100644
--- a/salt/grains/core.py
+++ b/salt/grains/core.py
@@ -864,6 +864,10 @@ def _virtual(osdata):
                 grains["virtual"] = "container"
                 grains["virtual_subtype"] = "LXC"
                 break
+            elif "amazon" in output:
+                grains["virtual"] = "Nitro"
+                grains["virtual_subtype"] = "Amazon EC2"
+                break
         elif command == "virt-what":
             for line in output.splitlines():
                 if line in ("kvm", "qemu", "uml", "xen"):
@@ -1176,7 +1180,7 @@ def _virtual(osdata):
         grains["virtual"] = "virtual"
 
     # Try to detect if the instance is running on Amazon EC2
-    if grains["virtual"] in ("qemu", "kvm", "xen"):
+    if grains["virtual"] in ("qemu", "kvm", "xen", "amazon"):
         dmidecode = salt.utils.path.which("dmidecode")
         if dmidecode:
             ret = __salt__["cmd.run_all"](
@@ -1184,6 +1188,8 @@ def _virtual(osdata):
             )
             output = ret["stdout"]
             if "Manufacturer: Amazon EC2" in output:
+                if grains["virtual"] != "xen":
+                    grains["virtual"] = "Nitro"
                 grains["virtual_subtype"] = "Amazon EC2"
                 product = re.match(
                     r".*Product Name: ([^\r\n]*).*", output, flags=re.DOTALL
diff --git a/tests/pytests/unit/grains/test_core.py b/tests/pytests/unit/grains/test_core.py
index e640a07f76..46a0591e27 100644
--- a/tests/pytests/unit/grains/test_core.py
+++ b/tests/pytests/unit/grains/test_core.py
@@ -2785,6 +2785,11 @@ def test_virtual_set_virtual_ec2():
             "/usr/bin/systemd-detect-virt",
             None,
             None,
+            # Check with systemd-detect-virt returning amazon and no dmidecode available
+            None,
+            "/usr/bin/systemd-detect-virt",
+            None,
+            None,
         ]
     )
     cmd_run_all_mock = MagicMock(
@@ -2843,9 +2848,22 @@ def test_virtual_set_virtual_ec2():
             },
             # Check with systemd-detect-virt when no dmidecode available
             {"retcode": 0, "stderr": "", "stdout": "kvm"},
+            # Check with systemd-detect-virt returning amazon and no dmidecode available
+            {"retcode": 0, "stderr": "", "stdout": "amazon"},
         ]
     )
 
+    def _mock_is_file(filename):
+        if filename in (
+            "/proc/1/cgroup",
+            "/proc/cpuinfo",
+            "/sys/devices/virtual/dmi/id/product_name",
+            "/proc/xen/xsd_kva",
+            "/proc/xen/capabilities",
+        ):
+            return False
+        return True
+
     with patch("salt.utils.path.which", which_mock), patch.dict(
         core.__salt__,
         {
@@ -2854,6 +2872,8 @@ def test_virtual_set_virtual_ec2():
             "cmd.retcode": salt.modules.cmdmod.retcode,
             "smbios.get": salt.modules.smbios.get,
         },
+    ), patch("os.path.isfile", _mock_is_file), patch(
+        "os.path.isdir", return_value=False
     ):
 
         virtual_grains = core._virtual(osdata.copy())
@@ -2863,7 +2883,7 @@ def test_virtual_set_virtual_ec2():
 
         virtual_grains = core._virtual(osdata.copy())
 
-        assert virtual_grains["virtual"] == "kvm"
+        assert virtual_grains["virtual"] == "Nitro"
         assert virtual_grains["virtual_subtype"] == "Amazon EC2 (m5.large)"
 
         virtual_grains = core._virtual(osdata.copy())
@@ -2871,6 +2891,11 @@ def test_virtual_set_virtual_ec2():
         assert virtual_grains["virtual"] == "kvm"
         assert "virtual_subtype" not in virtual_grains
 
+        virtual_grains = core._virtual(osdata.copy())
+
+        assert virtual_grains["virtual"] == "Nitro"
+        assert virtual_grains["virtual_subtype"] == "Amazon EC2"
+
 
 @pytest.mark.skip_on_windows
 def test_linux_proc_files_with_non_utf8_chars():
-- 
2.38.0


