Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHELC-1339] Port update_rhsm_custom_facts to Action framework #1299

Merged
merged 12 commits into from
Aug 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright(C) 2023 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__metaclass__ = type

import logging

from convert2rhel import actions, subscription


loggerinst = logging.getLogger(__name__)


class RHSMCustomFactsConfig(actions.Action):

id = "RHSM_CUSTOM_FACTS_CONFIG"

dependencies = ()

def run(self):
super(RHSMCustomFactsConfig, self).run()

ret_code, output = subscription.update_rhsm_custom_facts()

Venefilyn marked this conversation as resolved.
Show resolved Hide resolved
if not output:
return None

if ret_code != 0:
self.add_message(
level="WARNING",
id="FAILED_TO_UPDATE_RHSM_CUSTOM_FACTS",
title="Failed to update RHSM custom facts",
description="Failed to update the RHSM custom facts with return code: {0} and output: {1}.".format(
ret_code, output
),
)
2 changes: 2 additions & 0 deletions convert2rhel/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,10 +917,12 @@ def update_rhsm_custom_facts():
ret_code,
output,
)
return ret_code, output
else:
loggerinst.info("RHSM custom facts uploaded successfully.")
else:
loggerinst.info("Skipping updating RHSM custom facts.")
return None, None


def get_rhsm_facts():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright(C) 2023 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__metaclass__ = type

import pytest
import six

from convert2rhel import actions, subscription, utils
from convert2rhel.actions.post_conversion.rhsm_custom_facts_config import RHSMCustomFactsConfig
from convert2rhel.unit_tests import RunSubprocessMocked


six.add_move(six.MovedModule("mock", "mock", "unittest.mock"))
from six.moves import mock


@pytest.fixture
def rhsm_custom_facts_config_instance():
return RHSMCustomFactsConfig()


def test_rhsm_custom_facts_config(rhsm_custom_facts_config_instance, monkeypatch):
# need to mock runsubprocess
monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked())
monkeypatch.setattr(
subscription, "update_rhsm_custom_facts", mock.Mock(return_value=(1, "Unable to update RHSM custom facts"))
)

expected = {
actions.ActionMessage(
level="WARNING",
title="Failed to update RHSM custom facts",
id="FAILED_TO_UPDATE_RHSM_CUSTOM_FACTS",
description="Failed to update the RHSM custom facts with return code: 1 and output: Unable to update RHSM custom facts.",
)
}

rhsm_custom_facts_config_instance.run()

assert expected.issuperset(rhsm_custom_facts_config_instance.messages)
assert expected.issubset(rhsm_custom_facts_config_instance.messages)


def test_rhsm_custom_facts_config_no_output(rhsm_custom_facts_config_instance, monkeypatch, caplog):
monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked())
monkeypatch.setattr(subscription, "update_rhsm_custom_facts", mock.Mock(return_value=(1, "")))
rhsm_custom_facts_config_instance.run()

expected = []

assert rhsm_custom_facts_config_instance.messages == expected
Loading