From b6cd1be8e322dbd58a6f3e3438ae3ead50ee501b Mon Sep 17 00:00:00 2001 From: Vivian Thiebaut Date: Thu, 17 Aug 2023 15:51:51 -0400 Subject: [PATCH 1/3] Fix cannot unpack non-iterable NoneType object error in OS check --- src/ssh/HISTORY.md | 4 ++++ src/ssh/azext_ssh/target_os_utils.py | 8 +++++--- src/ssh/setup.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ssh/HISTORY.md b/src/ssh/HISTORY.md index 4c9dd593ce1..523e2c40535 100644 --- a/src/ssh/HISTORY.md +++ b/src/ssh/HISTORY.md @@ -1,5 +1,9 @@ Release History =============== +2.0.2 +----- +* [Bug Fix] Fix logic that checks for the OS of the targer machine to avoid "cannot unpack non-iterable NoneType object" error + 2.0.1 ----- * [Bug fix] For connections to arc resources, stop attempting to create new service configuration if user has no permission to read service configuration. diff --git a/src/ssh/azext_ssh/target_os_utils.py b/src/ssh/azext_ssh/target_os_utils.py index d3cee78ddde..02abacbd7b6 100644 --- a/src/ssh/azext_ssh/target_os_utils.py +++ b/src/ssh/azext_ssh/target_os_utils.py @@ -80,10 +80,12 @@ def _get_arc_server_os(cmd, resource_group_name, vm_name): try: arc_server = ArcServerShow(cli_ctx=cmd.cli_ctx)(command_args=get_args) except Exception: - return None + return None, None if arc_server and arc_server.get('osName', None): - os_type = arc_server['osName'] + os_type = arc_server.get('osName') + elif arc_server and arc_server.get('properties', None): + os_type = arc_server.get('properties').get('osType', None) if arc_server and arc_server.get('properties'): agent_version = arc_server.get('properties').get('agentVersion') @@ -104,7 +106,7 @@ def _get_connected_vmware_os(cmd, resource_group_name, vm_name): try: vmware = VMwarevSphereShow(cli_ctx=cmd.cli_ctx)(command_args=get_args) except Exception: - return None + return None, None if vmware and vmware.get("osProfile") and vmware.get("osProfile").get("osType"): os_type = vmware.get("osProfile").get("osType") diff --git a/src/ssh/setup.py b/src/ssh/setup.py index e201529af83..11e6271c020 100644 --- a/src/ssh/setup.py +++ b/src/ssh/setup.py @@ -7,7 +7,7 @@ from setuptools import setup, find_packages -VERSION = "2.0.1" +VERSION = "2.0.2" CLASSIFIERS = [ 'Development Status :: 4 - Beta', From 36e0e8c76d248165ac44443b13724d5d4515a0f4 Mon Sep 17 00:00:00 2001 From: Vivian Thiebaut Date: Fri, 18 Aug 2023 16:49:36 -0400 Subject: [PATCH 2/3] Add unit tests to functions that get os and agent version from arc server and connected vmware --- .../tests/latest/test_target_os_utils.py | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/ssh/azext_ssh/tests/latest/test_target_os_utils.py diff --git a/src/ssh/azext_ssh/tests/latest/test_target_os_utils.py b/src/ssh/azext_ssh/tests/latest/test_target_os_utils.py new file mode 100644 index 00000000000..03a4a80b5f0 --- /dev/null +++ b/src/ssh/azext_ssh/tests/latest/test_target_os_utils.py @@ -0,0 +1,91 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +import unittest +from unittest import mock + +from azext_ssh import target_os_utils + +class TargetOSUtilsTest(unittest.TestCase): + + @mock.patch('azext_ssh.aaz.latest.hybrid_compute.machine.Show') + def test_get_arc_os(self, mock_get_arc): + cmd = mock.Mock() + cmd.cli_ctx = mock.Mock() + + showclass = mock.Mock() + showclass.return_value = { + "properties": { + "osType": "os_type", + "agentVersion": "arc_agent_version" + } + } + + mock_get_arc.return_value = showclass + + os, agent = target_os_utils._get_arc_server_os(cmd, "rg", "vm") + + self.assertEqual(os, "os_type") + self.assertEqual(agent, "arc_agent_version") + + + + @mock.patch('azext_ssh.aaz.latest.hybrid_compute.machine.Show', autospec=True) + def test_get_arc_os_exception(self, mock_get_arc): + cmd = mock.Mock() + cmd.cli_ctx = mock.Mock() + + mock_get_arc.return_value.side_effect = mock.Mock(side_effect=Exception('Test')) + + os, agent = target_os_utils._get_arc_server_os(cmd, "rg", "vm") + + self.assertEqual(os, None) + self.assertEqual(agent, None) + + + @mock.patch('azext_ssh.aaz.latest.connected_v_mwarev_sphere.virtual_machine.Show') + def test_get_vmware_os(self, mock_get_vmware): + cmd = mock.Mock() + cmd.cli_ctx = mock.Mock() + + showclass = mock.Mock() + showclass.return_value = { + "osProfile":{ + "osType": "os_type" + }, + "properties":{ + "guestAgentProfile": { + "agentVersion": "agent_version" + } + } + } + + mock_get_vmware.return_value = showclass + + os, agent = target_os_utils._get_connected_vmware_os(cmd, "rg", "vm") + + self.assertEqual(os, "os_type") + self.assertEqual(agent, "agent_version") + + + + @mock.patch('azext_ssh.aaz.latest.connected_v_mwarev_sphere.virtual_machine.Show', autospec=True) + def test_get_vmware_os_exception(self, mock_get_vmware): + cmd = mock.Mock() + cmd.cli_ctx = mock.Mock() + + mock_get_vmware.return_value.side_effect = mock.Mock(side_effect=Exception('Test')) + + os, agent = target_os_utils._get_connected_vmware_os(cmd, "rg", "vm") + + self.assertEqual(os, None) + self.assertEqual(agent, None) + + + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 23dee88641745de59ad438b0a638acb560d8a49b Mon Sep 17 00:00:00 2001 From: Vivian Thiebaut Date: Mon, 25 Sep 2023 16:32:17 -0400 Subject: [PATCH 3/3] Fix typo in history --- src/ssh/HISTORY.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ssh/HISTORY.md b/src/ssh/HISTORY.md index 523e2c40535..0bc9ae91bd3 100644 --- a/src/ssh/HISTORY.md +++ b/src/ssh/HISTORY.md @@ -2,13 +2,12 @@ Release History =============== 2.0.2 ----- -* [Bug Fix] Fix logic that checks for the OS of the targer machine to avoid "cannot unpack non-iterable NoneType object" error +* [Bug Fix] Fix logic that checks for the OS of the target machine to avoid "cannot unpack non-iterable NoneType object" error 2.0.1 ----- * [Bug fix] For connections to arc resources, stop attempting to create new service configuration if user has no permission to read service configuration. - 2.0.0 ----- * [BREAKING CHANGE] Update Microsoft.HybridConnectivity SDK to stable version, which adds functionality to enable SSH connections on specified ports in your Arc Server using an API, instead of enabling ports for connection locally in the Arc Agent running in the target machine. New connections might fail after updating the extension, since the port for connection will need to be enabled at the HybridConnectivity Resource Provider at the first connection attempt. This change doesn't affect those who use this extension to connect to Azure Virtual Machines.