def setUp(self): self.popen_patcher = mock.patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = mock.patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.systems.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy' self.config_file = 'conf.onpremises.yml' self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file) self.installer = 'codedeploy-agent.msi' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.body = 'install-script' self.reader = mock.MagicMock() self.reader.read.return_value = self.body self.s3 = mock.MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = mock.MagicMock() self.session.create_client.return_value = self.s3 self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key self.windows = Windows(self.params)
def validate_instance(params): if platform.system() == 'Linux': if 'Ubuntu' in platform.linux_distribution()[0]: params.system = Ubuntu(params) elif platform.system() == 'Windows': params.system = Windows(params) if 'system' not in params: raise RuntimeError( 'Only Ubuntu Server and Windows Server operating systems are ' 'supported.') try: urlopen('http://169.254.169.254/latest/meta-data/', timeout=1) raise RuntimeError('Amazon EC2 instances are not supported.') except (URLError, timeout): pass
def validate_instance(params): if platform.system() == 'Linux': if 'Ubuntu' in platform.linux_distribution()[0]: params.system = Ubuntu(params) if 'Red Hat Enterprise Linux Server' in platform.linux_distribution( )[0]: params.system = RHEL(params) elif platform.system() == 'Windows': params.system = Windows(params) if 'system' not in params: raise RuntimeError(System.UNSUPPORTED_SYSTEM_MSG) try: urlopen('http://169.254.169.254/latest/meta-data/', timeout=1) raise RuntimeError('Amazon EC2 instances are not supported.') except (URLError, timeout): pass
def setUp(self): self.popen_patcher = patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = patch( 'awscli.customizations.codedeploy.systems.open', mock_open(), create=True ) self.open = self.open_patcher.start() self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy' self.config_file = 'conf.onpremises.yml' self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file) self.installer = 'codedeploy-agent.msi' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.body = 'install-script' self.reader = MagicMock() self.reader.read.return_value = self.body self.s3 = MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = MagicMock() self.session.create_client.return_value = self.s3 self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key self.windows = Windows(self.params)
class TestWindows(unittest.TestCase): def setUp(self): self.popen_patcher = patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = patch( 'awscli.customizations.codedeploy.systems.open', mock_open(), create=True ) self.open = self.open_patcher.start() self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy' self.config_file = 'conf.onpremises.yml' self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file) self.installer = 'codedeploy-agent.msi' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.body = 'install-script' self.reader = MagicMock() self.reader.read.return_value = self.body self.s3 = MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = MagicMock() self.session.create_client.return_value = self.s3 self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key self.windows = Windows(self.params) def tearDown(self): self.popen_patcher.stop() self.check_call_patcher.stop() self.open_patcher.stop() def test_config_dir(self): self.assertEqual(self.config_dir, self.windows.CONFIG_DIR) def test_config_file(self): self.assertEquals(self.config_file, self.windows.CONFIG_FILE) def test_config_path(self): self.assertEquals(self.config_path, self.windows.CONFIG_PATH) def test_installer(self): self.assertEquals(self.installer, self.windows.INSTALLER) def test_install(self): process = MagicMock() process.communicate.side_effect = [('', ''), ('Running', '')] process.returncode = 0 self.popen.return_value = process self.windows.install(self.params) self.popen.assert_has_calls([ call( [ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE ), call().communicate(), call( [ 'powershell.exe', '-Command', 'Get-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE ), call().communicate() ]) self.check_call.assert_has_calls([ call( [ r'.\{0}'.format(self.installer), '/quiet', '/l', r'.\codedeploy-agent-install-log.txt' ], shell=True ), call([ 'powershell.exe', '-Command', 'Restart-Service', '-Name', 'codedeployagent' ]) ]) self.open.assert_called_with(self.installer, 'wb') self.open().write.assert_called_with(self.body) def test_uninstall(self): process = MagicMock() process.communicate.side_effect = [('', ''), ('', '')] process.returncode = 0 self.popen.return_value = process self.windows.uninstall(self.params) self.popen.assert_has_calls([ call( [ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE ), call().communicate(), call( [ 'wmic', 'product', 'where', 'name="CodeDeploy Host Agent"', 'call', 'uninstall', '/nointeractive' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE ), call().communicate() ])
class TestWindows(unittest.TestCase): def setUp(self): self.popen_patcher = mock.patch('subprocess.Popen') self.popen = self.popen_patcher.start() self.check_call_patcher = mock.patch('subprocess.check_call') self.check_call = self.check_call_patcher.start() self.open_patcher = mock.patch( 'awscli.customizations.codedeploy.systems.open', mock.mock_open(), create=True) self.open = self.open_patcher.start() self.config_dir = r'C:\ProgramData\Amazon\CodeDeploy' self.config_file = 'conf.onpremises.yml' self.config_path = r'{0}\{1}'.format(self.config_dir, self.config_file) self.installer = 'codedeploy-agent.msi' self.bucket = 'bucket' self.key = 'key' self.region = 'us-east-1' self.body = 'install-script' self.reader = mock.MagicMock() self.reader.read.return_value = self.body self.s3 = mock.MagicMock() self.s3.get_object.return_value = {'Body': self.reader} self.session = mock.MagicMock() self.session.create_client.return_value = self.s3 self.params = Namespace() self.params.session = self.session self.params.region = self.region self.params.bucket = self.bucket self.params.key = self.key self.windows = Windows(self.params) def tearDown(self): self.popen_patcher.stop() self.check_call_patcher.stop() self.open_patcher.stop() def test_config_dir(self): self.assertEqual(self.config_dir, self.windows.CONFIG_DIR) def test_config_file(self): self.assertEqual(self.config_file, self.windows.CONFIG_FILE) def test_config_path(self): self.assertEqual(self.config_path, self.windows.CONFIG_PATH) def test_installer(self): self.assertEqual(self.installer, self.windows.INSTALLER) def test_install(self): process = mock.MagicMock() process.communicate.side_effect = [('', ''), ('Running', '')] process.returncode = 0 self.popen.return_value = process self.windows.install(self.params) self.popen.assert_has_calls([ mock.call([ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate(), mock.call([ 'powershell.exe', '-Command', 'Get-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ]) self.check_call.assert_has_calls([ mock.call([ r'.\{0}'.format(self.installer), '/quiet', '/l', r'.\codedeploy-agent-install-log.txt' ], shell=True), mock.call([ 'powershell.exe', '-Command', 'Restart-Service', '-Name', 'codedeployagent' ]) ]) self.open.assert_called_with(self.installer, 'wb') self.open().write.assert_called_with(self.body) def test_uninstall(self): process = mock.MagicMock() process.communicate.side_effect = [('', ''), ('', '')] process.returncode = 0 self.popen.return_value = process self.windows.uninstall(self.params) self.popen.assert_has_calls([ mock.call([ 'powershell.exe', '-Command', 'Stop-Service', '-Name', 'codedeployagent' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate(), mock.call([ 'wmic', 'product', 'where', 'name="CodeDeploy Host Agent"', 'call', 'uninstall', '/nointeractive' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE), mock.call().communicate() ])