From 70fb5ae36a10dd6b9079b61aab9d9eb076486d2c Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Sun, 7 Feb 2021 18:50:49 -0500 Subject: [PATCH] [stable-2.10] facts - properly report virtualization facts for Linux guests on bhyve (#73204). (#73233) (cherry picked from commit df451636e74fe6f0021a5555392f84c2bf194432) Co-authored-by: Sam Doran --- changelogs/fragments/73167-bhyve-facts.yml | 2 ++ .../module_utils/facts/virtual/linux.py | 5 ++++ .../module_utils/facts/virtual/__init__.py | 0 .../module_utils/facts/virtual/test_linux.py | 26 +++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 changelogs/fragments/73167-bhyve-facts.yml create mode 100644 test/units/module_utils/facts/virtual/__init__.py create mode 100644 test/units/module_utils/facts/virtual/test_linux.py diff --git a/changelogs/fragments/73167-bhyve-facts.yml b/changelogs/fragments/73167-bhyve-facts.yml new file mode 100644 index 00000000000..d9f6d0fc904 --- /dev/null +++ b/changelogs/fragments/73167-bhyve-facts.yml @@ -0,0 +1,2 @@ +bugfixes: + - facts - properly report virtualization facts for Linux guests running on bhyve (https://github.com/ansible/ansible/issues/73167) diff --git a/lib/ansible/module_utils/facts/virtual/linux.py b/lib/ansible/module_utils/facts/virtual/linux.py index e133df42958..5722e462b39 100644 --- a/lib/ansible/module_utils/facts/virtual/linux.py +++ b/lib/ansible/module_utils/facts/virtual/linux.py @@ -238,6 +238,11 @@ class LinuxVirtual(Virtual): virtual_facts['virtualization_role'] = 'guest' return virtual_facts + if 'BHYVE' in out: + virtual_facts['virtualization_type'] = 'bhyve' + virtual_facts['virtualization_role'] = 'guest' + return virtual_facts + # If none of the above matches, return 'NA' for virtualization_type # and virtualization_role. This allows for proper grouping. virtual_facts['virtualization_type'] = 'NA' diff --git a/test/units/module_utils/facts/virtual/__init__.py b/test/units/module_utils/facts/virtual/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/module_utils/facts/virtual/test_linux.py b/test/units/module_utils/facts/virtual/test_linux.py new file mode 100644 index 00000000000..d534478c5a5 --- /dev/null +++ b/test/units/module_utils/facts/virtual/test_linux.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2020 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.facts.virtual import linux + + +def test_get_virtual_facts_bhyve(mocker): + mocker.patch('os.path.exists', return_value=False) + mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_content', return_value='') + mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_lines', return_value=[]) + + module = mocker.Mock() + module.run_command.return_value = (0, 'BHYVE\n', '') + inst = linux.LinuxVirtual(module) + + facts = inst.get_virtual_facts() + expected = { + 'virtualization_role': 'guest', + 'virtualization_type': 'bhyve', + } + + assert facts == expected