File opensuse-3000-spacewalk-runner-parse-command-247.patch of Package salt

From b8dd66a71d051a55e68778c83b69250c88d4286a Mon Sep 17 00:00:00 2001
From: Alexander Graul <agraul@suse.com>
Date: Fri, 3 Jul 2020 14:07:55 +0200
Subject: [PATCH] openSUSE-3000 spacewalk runner parse command (#247)

* Accept nested namespaces in spacewalk.api

salt-run $server spacewalk.api allows users to run arbitrary Spacewalk
API functions through Salt. These are passed in a namespace.method
notation and may use nested namespaces. Previously only methods in a
top-level namespace were supported.

Fixes https://github.com/saltstack/salt/issues/57442

Co-authored-by: Wayne Werner <wwerner@saltstack.com>

* Add spacewalk runner command parsing tests

Co-authored-by: Wayne Werner <wwerner@saltstack.com>
---
 changelog/57442.fixed                |  1 +
 salt/runners/spacewalk.py            |  6 +++-
 tests/unit/runners/test_spacewalk.py | 50 ++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 changelog/57442.fixed
 create mode 100644 tests/unit/runners/test_spacewalk.py

diff --git a/changelog/57442.fixed b/changelog/57442.fixed
new file mode 100644
index 0000000000..81f394880f
--- /dev/null
+++ b/changelog/57442.fixed
@@ -0,0 +1 @@
+Accept nested namespaces in spacewalk.api runner function.
diff --git a/salt/runners/spacewalk.py b/salt/runners/spacewalk.py
index 07ca9bd711..df4e568a28 100644
--- a/salt/runners/spacewalk.py
+++ b/salt/runners/spacewalk.py
@@ -172,7 +172,11 @@ def api(server, command, *args, **kwargs):
         log.error(err_msg)
         return {call: err_msg}
 
-    namespace, method = command.split('.')
+    namespace, _, method = command.rpartition(".")
+    if not namespace:
+        return {
+            call: "Error: command must use the following format: 'namespace.method'"
+        }
     endpoint = getattr(getattr(client, namespace), method)
 
     try:
diff --git a/tests/unit/runners/test_spacewalk.py b/tests/unit/runners/test_spacewalk.py
new file mode 100644
index 0000000000..5b64069cc9
--- /dev/null
+++ b/tests/unit/runners/test_spacewalk.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+"""
+Unit tests for Spacewalk runner
+"""
+import salt.runners.spacewalk as spacewalk
+from tests.support.mock import Mock, call, patch
+from tests.support.unit import TestCase
+
+
+class SpacewalkTest(TestCase):
+    """Test the Spacewalk runner"""
+
+    def test_api_command_must_have_namespace(self):
+        _get_session_mock = Mock(return_value=(None, None))
+
+        with patch.object(spacewalk, "_get_session", _get_session_mock):
+            result = spacewalk.api("mocked.server", "badMethod")
+            assert result == {
+                "badMethod ()": "Error: command must use the following format: 'namespace.method'"
+            }
+
+    def test_api_command_accepts_single_namespace(self):
+        client_mock = Mock()
+        _get_session_mock = Mock(return_value=(client_mock, "key"))
+        getattr_mock = Mock(return_value="mocked_getattr_return")
+
+        with patch.object(spacewalk, "_get_session", _get_session_mock):
+            with patch.object(spacewalk, "getattr", getattr_mock):
+                spacewalk.api("mocked.server", "system.listSystems")
+                getattr_mock.assert_has_calls(
+                    [
+                        call(client_mock, "system"),
+                        call("mocked_getattr_return", "listSystems"),
+                    ]
+                )
+
+    def test_api_command_accepts_nested_namespace(self):
+        client_mock = Mock()
+        _get_session_mock = Mock(return_value=(client_mock, "key"))
+        getattr_mock = Mock(return_value="mocked_getattr_return")
+
+        with patch.object(spacewalk, "_get_session", _get_session_mock):
+            with patch.object(spacewalk, "getattr", getattr_mock):
+                spacewalk.api("mocked.server", "channel.software.listChildren")
+                getattr_mock.assert_has_calls(
+                    [
+                        call(client_mock, "channel.software"),
+                        call("mocked_getattr_return", "listChildren"),
+                    ]
+                )
-- 
2.27.0
openSUSE Build Service is sponsored by