From 5ae113be26b2ad1af7a8eff6e7d33ce8bd398c5a Mon Sep 17 00:00:00 2001 From: 6065meet <6065meet@gmail.com> Date: Tue, 18 Aug 2026 22:20:46 +0530 Subject: [PATCH] Fix CodeDeploy install when service is missing --- awscli/customizations/codedeploy/systems.py | 15 ++-- .../customizations/codedeploy/test_systems.py | 90 +++++++++++++++++-- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/awscli/customizations/codedeploy/systems.py b/awscli/customizations/codedeploy/systems.py index 19e24f6fbdeb..bb2aae4dc577 100644 --- a/awscli/customizations/codedeploy/systems.py +++ b/awscli/customizations/codedeploy/systems.py @@ -66,7 +66,8 @@ def install(self, params): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ) (output, error) = process.communicate() not_found = ( @@ -102,7 +103,8 @@ def install(self, params): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ) (output, error) = process.communicate() if "Running" not in output: @@ -118,7 +120,8 @@ def uninstall(self, params): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ) (output, error) = process.communicate() not_found = ( @@ -139,7 +142,8 @@ def _remove_agent(self): 'call', 'uninstall', '/nointeractive' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ) (output, error) = process.communicate() if process.returncode != 0: @@ -202,7 +206,8 @@ def _stop_agent(self, params): process = subprocess.Popen( ['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ) (output, error) = process.communicate() if process.returncode != 0 and params.not_found_msg not in error: diff --git a/tests/unit/customizations/codedeploy/test_systems.py b/tests/unit/customizations/codedeploy/test_systems.py index c4e50ff9db84..c3343c9e4bd7 100644 --- a/tests/unit/customizations/codedeploy/test_systems.py +++ b/tests/unit/customizations/codedeploy/test_systems.py @@ -88,7 +88,8 @@ def test_install(self): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate(), mock.call( @@ -98,7 +99,8 @@ def test_install(self): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) @@ -134,7 +136,8 @@ def test_uninstall(self): '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate(), mock.call( @@ -144,11 +147,38 @@ def test_uninstall(self): 'call', 'uninstall', '/nointeractive' ], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) + def test_install_service_not_found(self): + not_found = ( + "Cannot find any service with service name 'codedeployagent'" + ) + stop_process = mock.MagicMock() + stop_process.communicate.return_value = ('', not_found) + stop_process.returncode = 1 + + get_process = mock.MagicMock() + get_process.communicate.return_value = ('Running', '') + get_process.returncode = 0 + + self.popen.side_effect = [stop_process, get_process] + self.windows.install(self.params) + + def test_uninstall_service_not_found(self): + not_found = ( + "Cannot find any service with service name 'codedeployagent'" + ) + process = mock.MagicMock() + process.communicate.return_value = ('', not_found) + process.returncode = 1 + self.popen.return_value = process + self.windows.uninstall(self.params) + self.assertEqual(self.popen.call_count, 1) + class TestLinux(unittest.TestCase): def setUp(self): @@ -248,7 +278,8 @@ def test_install(self): mock.call( ['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) @@ -274,7 +305,8 @@ def test_uninstall(self): mock.call( ['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) @@ -282,6 +314,26 @@ def test_uninstall(self): mock.call(['dpkg', '-r', 'codedeploy-agent']) ]) + def test_install_service_not_found(self): + process = mock.MagicMock() + process.communicate.return_value = ( + '', 'codedeploy-agent: unrecognized service' + ) + process.returncode = 1 + self.popen.return_value = process + self.ubuntu.install(self.params) + + def test_uninstall_service_not_found(self): + process = mock.MagicMock() + process.communicate.return_value = ( + '', 'codedeploy-agent: unrecognized service' + ) + process.returncode = 1 + self.popen.return_value = process + self.ubuntu.uninstall(self.params) + self.assertEqual(self.popen.call_count, 1) + + class TestRHEL(TestLinux): def setUp(self): super(self.__class__, self).setUp() @@ -316,7 +368,8 @@ def test_install(self): mock.call( ['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) @@ -341,7 +394,8 @@ def test_uninstall(self): mock.call( ['service', 'codedeploy-agent', 'stop'], stdout=subprocess.PIPE, - stderr=subprocess.PIPE + stderr=subprocess.PIPE, + text=True ), mock.call().communicate() ]) @@ -349,5 +403,25 @@ def test_uninstall(self): mock.call(['yum', '-y', 'erase', 'codedeploy-agent']) ]) + def test_install_service_not_found(self): + process = mock.MagicMock() + process.communicate.return_value = ( + '', 'Redirecting to /bin/systemctl stop codedeploy-agent.service' + ) + process.returncode = 1 + self.popen.return_value = process + self.rhel.install(self.params) + + def test_uninstall_service_not_found(self): + process = mock.MagicMock() + process.communicate.return_value = ( + '', 'Redirecting to /bin/systemctl stop codedeploy-agent.service' + ) + process.returncode = 1 + self.popen.return_value = process + self.rhel.uninstall(self.params) + self.assertEqual(self.popen.call_count, 1) + + if __name__ == "__main__": unittest.main()