def runTest(self):
        commandUtil = CommandUtil()
		
		self.assertFalse(commandUtil.execute_command('non existent command'))
		self.assertTrue(commandUtil.execute_command('ls'))
		
		self.assertFalse(commandUtil.get_command_output('non existent command'), [])
		self.assertNotEqual(commandUtil.get_command_output('ls'), [])

		self.assertTrue(commandUtil.execute_command('echo "Asking a yes no question (y/n)?" & read choice'))
		self.assertTrue(commandUtil.get_command_output('echo "Asking a yes no question (y/n)?" & read choice'))
    def get_system_manager():
        '''
        Detecting which system manager is running
        ''' 
        sysmanager = None
        commandUtil = CommandUtil()

        output = commandUtil.get_command_output('ls -l /proc/1/exe')
        line = output[0]

        if line.endswith('/sbin/init'):
            sysmanager = System_Manager.SYSV
        elif line.endswith('/sbin/upstart'):
            sysmanager = System_Manager.UPSTART
        elif line.endswith('/lib/systemd/systemd'):
            sysmanager = System_Manager.SYSTEMD

        return sysmanager
class GemUtil(object):
    '''
    Utility class to configure Ruby GEMs.
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.logger = logging.getLogger('GemUtil')
        # Embebed Chef installation
        self.command = "/opt/chef/embedded/bin/gem"
        self.rubyEmbeddedInChef = True
        self.sys_gemrc = "/opt/chef/embedded/etc/gemrc"

        if not os.path.isfile(self.command):
            # Linux distribution Chef installation
            self.command = "/usr/bin/gem"
            self.sys_gemrc = "/etc/gemrc"
            self.rubyEmbeddedInChef = False

        if self.rubyEmbeddedInChef and not os.path.isdir(
                "/opt/chef/embedded/etc"):
            os.mkdir("/opt/chef/embedded/etc", 0755)

        self.commandUtil = CommandUtil()
        self.pm = PackageManager()

    def get_gem_sources_list(self):
        ''' Getting gem sources '''

        lst = []

        output = self.commandUtil.get_command_output(
            '{} source --list --config-file "{}"'.format(
                self.command, self.sys_gemrc))

        for line in output:
            if line.startswith('http'):
                lst.append(line.strip())

        return lst

    def clear_cache_gem_sources(self):
        ''' Cleaning up gem sources cache '''

        return self.commandUtil.execute_command(
            '{} source -c --config-file "{}"'.format(self.command,
                                                     self.sys_gemrc))

    def remove_all_gem_sources(self):
        ''' Removing all gem sources '''

        sources = self.get_gem_sources_list()
        for source in sources:
            print "removing %s", source
            self.commandUtil.execute_command(
                '{} source -r "{}" --config-file "{}"'.format(
                    self.command, source, self.sys_gemrc))

        if os.path.exists(self.sys_gemrc):
            os.remove(self.sys_gemrc)

    # Adding only gecoscc.ini source ("gem_repo")
    # The gem add command adds https://rubygems.org source
    # by default. We must manually delete it.
    def add_gem_only_one_source(self, url):
        ''' Adding only gecoscc.ini gem source '''

        sources = self.get_gem_sources_list()

        self.logger.debug("adding gem source from gecoscc.ini: %s", url)
        res = self.add_gem_source(url)

        self.logger.debug("deleting all the other sources")
        for source in sources:
            if source != url:
                self.logger.debug("removing %s from gem sources", source)
                res &= self.commandUtil.execute_command(
                    '{} source -r "{}" --config-file "{}"'.format(
                        self.command, source, self.sys_gemrc))

        return res

    def add_gem_source(self, url):
        ''' Adding gem sources '''

        return self.commandUtil.execute_command(
            '{} source -a "{}" --config-file "{}"'.format(
                self.command, url, self.sys_gemrc))

    def remove_gem_source(self, url):
        ''' Removing gem source '''

        return self.commandUtil.execute_command(
            '{} source -r "{}" --config-file "{}"'.format(
                self.command, url, self.sys_gemrc))

    def is_gem_intalled(self, gem_name):
        ''' Is gem installed? '''

        output = self.commandUtil.get_command_output('{} list'.format(
            self.command))
        res = False
        if output != False:
            for line in output:
                if line.startswith(gem_name + ' '):
                    res = True

        return res

    def install_gem(self, gem_name):
        ''' Installing gem '''

        if not self.rubyEmbeddedInChef:
            # Try to install the GEM by using the package manager
            package_name = gem_name
            if not package_name.startswith('ruby-'):
                package_name = 'ruby-{}'.format(package_name)

            if (self.pm.exists_package(package_name)
                    and not self.pm.is_package_installed(package_name)):
                self.pm.install_package(package_name)

            if self.is_gem_intalled(gem_name):
                # GEM installed successfully by using the package manager
                return True

            # GEM is not installed successfully
            if not self.pm.is_package_installed('build-essential'):
                # We will need 'build-essential' package to build GEMs
                self.pm.install_package('build-essential')

        return self.commandUtil.execute_command(
            '{} install "{}"'.format(self.command, gem_name), os.environ)

    def uninstall_gem(self, gem_name):
        ''' Uninstall gem '''

        if not self.rubyEmbeddedInChef:
            # Try to uninstall the GEM by using the package manager
            package_name = gem_name
            if not package_name.startswith('ruby-'):
                package_name = 'ruby-{}'.format(package_name)

            if (self.pm.exists_package(package_name)
                    and self.pm.is_package_installed(package_name)):
                self.pm.remove_package(package_name)

            if not self.is_gem_intalled(gem_name):
                # GEM uninstalled successfully by using the package manager
                return True

        return self.commandUtil.execute_command('{} uninstall "{}"'.format(
            self.command, gem_name))