Пример #1
0
class TestUbuntu(TestLinux):
    def setUp(self):
        super(self.__class__, self).setUp()
        self.ubuntu = Ubuntu(self.params)

    def test_config_dir(self):
        self.assertEqual(self.config_dir, self.ubuntu.CONFIG_DIR)

    def test_config_file(self):
        self.assertEqual(self.config_file, self.ubuntu.CONFIG_FILE)

    def test_config_path(self):
        self.assertEqual(self.config_path, self.ubuntu.CONFIG_PATH)

    def test_installer(self):
        self.assertEqual(self.installer, self.ubuntu.INSTALLER)

    @mock.patch('os.geteuid', create=True)
    def test_validate_administrator_throws(self, geteuid):
        geteuid.return_value = 1
        with self.assertRaisesRegex(RuntimeError,
                                    'You must run this command as sudo.'):
            self.ubuntu.validate_administrator()

    def test_install(self):
        process = mock.MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.install(self.params)
        self.popen.assert_has_calls([
            mock.call(['service', 'codedeploy-agent', 'stop'],
                      stdout=subprocess.PIPE,
                      stderr=subprocess.PIPE),
            mock.call().communicate()
        ])
        self.check_call.assert_has_calls([
            mock.call(['apt-get', '-y', 'update']),
            mock.call(['apt-get', '-y', 'install', 'ruby2.0']),
            mock.call(['chmod', '+x', './{0}'.format(self.installer)]),
            mock.call(['./{0}'.format(self.installer), 'auto'],
                      env=self.environment)
        ])
        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.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.uninstall(self.params)
        self.popen.assert_has_calls([
            mock.call(['service', 'codedeploy-agent', 'stop'],
                      stdout=subprocess.PIPE,
                      stderr=subprocess.PIPE),
            mock.call().communicate()
        ])
        self.check_call.assert_has_calls(
            [mock.call(['dpkg', '-r', 'codedeploy-agent'])])
Пример #2
0
class TestUbuntu(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.environ_patcher = patch('os.environ')
        self.environ = self.environ_patcher.start()
        self.environ.copy.return_value = dict()

        self.config_dir = '/etc/codedeploy-agent/conf'
        self.config_file = 'codedeploy.onpremises.yml'
        self.config_path = '{0}/{1}'.format(self.config_dir, self.config_file)
        self.installer = 'install'
        self.bucket = 'bucket'
        self.key = 'key'
        self.region = 'us-east-1'

        self.access_key_id = 'ACCESSKEYID'
        self.secret_access_key = 'SECRETACCESSKEY'
        self.session_token = 'SESSION_TOKEN'
        self.credentials = MagicMock()
        self.credentials.access_key = self.access_key_id
        self.credentials.secret_key = self.secret_access_key
        self.credentials.token = self.session_token

        self.environment = dict({
            'AWS_REGION': self.region,
            'AWS_ACCESS_KEY_ID': self.access_key_id,
            'AWS_SECRET_ACCESS_KEY': self.secret_access_key,
            'AWS_SESSION_TOKEN': self.session_token
        })

        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.session.get_credentials.return_value = self.credentials

        self.params = Namespace()
        self.params.session = self.session
        self.params.region = self.region
        self.params.bucket = self.bucket
        self.params.key = self.key

        self.ubuntu = Ubuntu(self.params)

    def tearDown(self):
        self.popen_patcher.stop()
        self.check_call_patcher.stop()
        self.open_patcher.stop()
        self.environ_patcher.stop()

    def test_config_dir(self):
        self.assertEquals(self.config_dir, self.ubuntu.CONFIG_DIR)

    def test_config_file(self):
        self.assertEquals(self.config_file, self.ubuntu.CONFIG_FILE)

    def test_config_path(self):
        self.assertEquals(self.config_path, self.ubuntu.CONFIG_PATH)

    def test_installer(self):
        self.assertEquals(self.installer, self.ubuntu.INSTALLER)

    @patch('os.geteuid', create=True)
    def test_validate_administrator_throws(self, geteuid):
        geteuid.return_value = 1
        with self.assertRaisesRegexp(
                RuntimeError, 'You must run this command as sudo.'):
            self.ubuntu.validate_administrator()

    def test_install(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.install(self.params)
        self.popen.assert_has_calls([
            call(
                ['service', 'codedeploy-agent', 'stop'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            ),
            call().communicate()
        ])
        self.check_call.assert_has_calls([
            call(['apt-get', '-y', 'update']),
            call(['apt-get', '-y', 'install', 'ruby2.0']),
            call(['chmod', '+x', './{0}'.format(self.installer)]),
            call(
                ['./{0}'.format(self.installer), 'auto'],
                env=self.environment
            )
        ])
        self.open.assert_called_with(self.installer, 'wb')
        self.open().write.assert_called_with(self.body)

    def test_uninstall(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.uninstall(self.params)
        self.popen.assert_has_calls([
            call(
                ['service', 'codedeploy-agent', 'stop'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            ),
            call().communicate()
        ])
        self.check_call.assert_has_calls([
            call(['dpkg', '-r', 'codedeploy-agent'])
        ])
Пример #3
0
class TestUbuntu(TestLinux):
    def setUp(self):
        super(self.__class__, self).setUp()
        self.ubuntu = Ubuntu(self.params)

    def test_config_dir(self):
        self.assertEquals(self.config_dir, self.ubuntu.CONFIG_DIR)

    def test_config_file(self):
        self.assertEquals(self.config_file, self.ubuntu.CONFIG_FILE)

    def test_config_path(self):
        self.assertEquals(self.config_path, self.ubuntu.CONFIG_PATH)

    def test_installer(self):
        self.assertEquals(self.installer, self.ubuntu.INSTALLER)

    @patch('os.geteuid', create=True)
    def test_validate_administrator_throws(self, geteuid):
        geteuid.return_value = 1
        with self.assertRaisesRegexp(
                RuntimeError, 'You must run this command as sudo.'):
            self.ubuntu.validate_administrator()

    def test_install(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.install(self.params)
        self.popen.assert_has_calls([
            call(
                ['service', 'codedeploy-agent', 'stop'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            ),
            call().communicate()
        ])
        self.check_call.assert_has_calls([
            call(['apt-get', '-y', 'update']),
            call(['apt-get', '-y', 'install', 'ruby2.0']),
            call(['chmod', '+x', './{0}'.format(self.installer)]),
            call(
                ['./{0}'.format(self.installer), 'auto'],
                env=self.environment
            )
        ])
        self.open.assert_called_with(self.installer, 'wb')
        self.open().write.assert_called_with(self.body)

    def test_uninstall(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.uninstall(self.params)
        self.popen.assert_has_calls([
            call(
                ['service', 'codedeploy-agent', 'stop'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            ),
            call().communicate()
        ])
        self.check_call.assert_has_calls([
            call(['dpkg', '-r', 'codedeploy-agent'])
        ])
Пример #4
0
class TestUbuntu(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.environ_patcher = patch('os.environ')
        self.environ = self.environ_patcher.start()
        self.environ.copy.return_value = dict()

        self.config_dir = '/etc/codedeploy-agent/conf'
        self.config_file = 'codedeploy.onpremises.yml'
        self.config_path = '{0}/{1}'.format(self.config_dir, self.config_file)
        self.installer = 'install'
        self.bucket = 'bucket'
        self.key = 'key'
        self.region = 'us-east-1'

        self.access_key_id = 'ACCESSKEYID'
        self.secret_access_key = 'SECRETACCESSKEY'
        self.session_token = 'SESSION_TOKEN'
        self.credentials = MagicMock()
        self.credentials.access_key = self.access_key_id
        self.credentials.secret_key = self.secret_access_key
        self.credentials.token = self.session_token

        self.environment = dict({
            'AWS_REGION': self.region,
            'AWS_ACCESS_KEY_ID': self.access_key_id,
            'AWS_SECRET_ACCESS_KEY': self.secret_access_key,
            'AWS_SESSION_TOKEN': self.session_token
        })

        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.session.get_credentials.return_value = self.credentials

        self.params = Namespace()
        self.params.session = self.session
        self.params.region = self.region
        self.params.bucket = self.bucket
        self.params.key = self.key

        self.ubuntu = Ubuntu(self.params)

    def tearDown(self):
        self.popen_patcher.stop()
        self.check_call_patcher.stop()
        self.open_patcher.stop()
        self.environ_patcher.stop()

    def test_config_dir(self):
        self.assertEquals(self.config_dir, self.ubuntu.CONFIG_DIR)

    def test_config_file(self):
        self.assertEquals(self.config_file, self.ubuntu.CONFIG_FILE)

    def test_config_path(self):
        self.assertEquals(self.config_path, self.ubuntu.CONFIG_PATH)

    def test_installer(self):
        self.assertEquals(self.installer, self.ubuntu.INSTALLER)

    @patch('os.geteuid', create=True)
    def test_validate_administrator_throws(self, geteuid):
        geteuid.return_value = 1
        with self.assertRaisesRegexp(RuntimeError,
                                     'You must run this command as sudo.'):
            self.ubuntu.validate_administrator()

    def test_install(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.install(self.params)
        self.popen.assert_has_calls([
            call(['service', 'codedeploy-agent', 'stop'],
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE),
            call().communicate()
        ])
        self.check_call.assert_has_calls([
            call(['apt-get', '-y', 'update']),
            call(['apt-get', '-y', 'install', 'ruby2.0']),
            call(['chmod', '+x', './{0}'.format(self.installer)]),
            call(['./{0}'.format(self.installer), 'auto'],
                 env=self.environment)
        ])
        self.open.assert_called_with(self.installer, 'wb')
        self.open().write.assert_called_with(self.body)

    def test_uninstall(self):
        process = MagicMock()
        process.communicate.return_value = ('', '')
        process.returncode = 0
        self.popen.return_value = process
        self.ubuntu.uninstall(self.params)
        self.popen.assert_has_calls([
            call(['service', 'codedeploy-agent', 'stop'],
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE),
            call().communicate()
        ])
        self.check_call.assert_has_calls(
            [call(['dpkg', '-r', 'codedeploy-agent'])])