예제 #1
0
 def setUp(self):
     """Test case setup function called automatically prior to each test."""
     self.helper = FatDisk()
     self.maxDiff = None
     super(TestFatDisk, self).setUp()
예제 #2
0
파일: test_fatdisk.py 프로젝트: leroux/cot
class TestFatDisk(HelperUT):

    """Test cases for FatDisk helper class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        self.helper = FatDisk()
        super(TestFatDisk, self).setUp()

    def test_get_version(self):
        """Validate .version getter."""
        self.fake_output = "fatdisk, version 1.0.0-beta"
        self.assertEqual(StrictVersion("1.0.0"), self.helper.version)

    def test_install_helper_already_present(self):
        """Trying to re-install is a no-op."""
        self.helper.install_helper()
        self.assertEqual([], self.last_argv)
        self.assertLogged(**self.ALREADY_INSTALLED)

    def test_install_helper_apt_get(self):
        """Test installation via 'apt-get'."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = True
        Helper._apt_updated = False
        self.system = 'Linux'
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'apt-get', '-q', 'update'],
            ['sudo', 'apt-get', '-q', 'install', 'make'],
            ['sudo', 'apt-get', '-q', 'install', 'gcc'],
            ['./RUNME'],
            ['sudo', 'cp', 'fatdisk', '/usr/local/bin/fatdisk'],
        ], self.last_argv)
        self.assertTrue(Helper._apt_updated)
        # Make sure we don't call apt-get update again unnecessarily.
        self.last_argv = []
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'apt-get', '-q', 'install', 'make'],
            ['sudo', 'apt-get', '-q', 'install', 'gcc'],
            ['./RUNME'],
            ['sudo', 'cp', 'fatdisk', '/usr/local/bin/fatdisk'],
        ], self.last_argv)

    def test_install_helper_port(self):
        """Test installation via 'port'."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = True
        Helper._port_updated = False
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'port', 'selfupdate'],
            ['sudo', 'port', 'install', 'fatdisk'],
        ], self.last_argv)
        self.assertTrue(Helper._port_updated)
        # Make sure we don't call port selfupdate again unnecessarily.
        self.last_argv = []
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'port', 'install', 'fatdisk'],
        ], self.last_argv)

    def test_install_helper_yum(self):
        """Test installation via 'yum'."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = True
        self.system = 'Linux'
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'yum', '--quiet', 'install', 'make'],
            ['sudo', 'yum', '--quiet', 'install', 'gcc'],
            ['./RUNME'],
            ['sudo', 'cp', 'fatdisk', '/usr/local/bin/fatdisk'],
        ], self.last_argv)

    def test_install_helper_linux_need_make_no_package_manager(self):
        """Linux installation requires yum or apt-get if 'make' missing."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = False
        self.system = 'Linux'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()

    def test_install_helper_linux_need_compiler_no_package_manager(self):
        """Linux installation requires yum or apt-get if 'gcc' missing."""
        def new_stub_find_executable(self, name):
            """Stub for Helper.find_executable - returns a fixed response."""
            logger.info("stub_find_executable({0})".format(name))
            if name == 'make':
                return "/bin/make"
            else:
                return None
        Helper.find_executable = new_stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = False
        self.system = 'Linux'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()

    def test_install_helper_unsupported(self):
        """No support for installation under Windows."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        self.system = 'Windows'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()
예제 #3
0
class TestFatDisk(HelperTestCase):
    """Test cases for FatDisk helper class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        self.helper = FatDisk()
        self.maxDiff = None
        super(TestFatDisk, self).setUp()

    @mock.patch('COT.helpers.helper.check_output',
                return_value="fatdisk, version 1.0.0-beta")
    def test_get_version(self, *_):
        """Validate .version getter."""
        self.helper._installed = True
        self.assertEqual(StrictVersion("1.0.0"), self.helper.version)

    @mock.patch('COT.helpers.helper.check_output')
    @mock.patch('subprocess.check_call')
    def test_install_already_present(self,
                                     mock_check_call,
                                     mock_check_output,
                                     *_):
        """Trying to re-install is a no-op."""
        self.helper._installed = True
        self.helper.install()
        mock_check_output.assert_not_called()
        mock_check_call.assert_not_called()

    @mock.patch('platform.system', return_value='Linux')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('os.path.exists', return_value=False)
    @mock.patch('os.makedirs', side_effect=OSError)
    @mock.patch('distutils.spawn.find_executable', return_value="/foo")
    @mock.patch('shutil.copy', return_value=True)
    @mock.patch('COT.helpers.helper.check_output', return_value="")
    @mock.patch('subprocess.check_call')
    def test_install_apt_get(self,
                             mock_check_call,
                             mock_check_output,
                             mock_copy,
                             *_):
        """Test installation via 'apt-get'."""
        self.enable_apt_install()
        helpers['dpkg']._installed = True
        for name in ['make', 'clang', 'gcc', 'g++']:
            helpers[name]._installed = False

        self.helper.install()
        self.assertSubprocessCalls(
            mock_check_output,
            [
                ['dpkg', '-s', 'make'],
                ['dpkg', '-s', 'gcc'],
            ])
        self.assertSubprocessCalls(
            mock_check_call,
            [
                ['apt-get', '-q', 'update'],
                ['apt-get', '-q', 'install', 'make'],
                ['apt-get', '-q', 'install', 'gcc'],
                ['./RUNME'],
                ['sudo', 'mkdir', '-p', '-m', '755', '/usr/local/bin'],
            ])
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/usr/local/bin', mock_copy.call_args[0][1])
        self.assertAptUpdated()

        # Make sure we don't call apt-get update/install again unnecessarily.
        mock_check_output.reset_mock()
        mock_check_call.reset_mock()
        mock_check_output.return_value = 'install ok installed'
        # fakeout!
        helpers['make']._installed = False
        self.helper._installed = False

        os.environ['PREFIX'] = '/opt/local'
        os.environ['DESTDIR'] = '/home/cot'

        self.helper.install()
        self.assertSubprocessCalls(
            mock_check_output,
            [
                ['dpkg', '-s', 'make'],
            ])
        self.assertSubprocessCalls(
            mock_check_call,
            [
                ['./RUNME'],
                ['sudo', 'mkdir', '-p', '-m', '755',
                 '/home/cot/opt/local/bin'],
            ])
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/home/cot/opt/local/bin', mock_copy.call_args[0][1])

    def test_install_brew(self, *_):
        """Test installation via 'brew'."""
        self.brew_install_test(['glennmatthews/fatdisk/fatdisk', '--devel'])

    def test_install_port(self, *_):
        """Test installation via 'port'."""
        self.port_install_test('fatdisk')

    @mock.patch('platform.system', return_value='Linux')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('os.path.exists', return_value=False)
    @mock.patch('os.makedirs', side_effect=OSError)
    @mock.patch('distutils.spawn.find_executable', return_value='/foo')
    @mock.patch('shutil.copy', return_value=True)
    @mock.patch('subprocess.check_call')
    def test_install_yum(self,
                         mock_check_call,
                         mock_copy,
                         *_):
        """Test installation via 'yum'."""
        self.enable_yum_install()
        for name in ['make', 'clang', 'gcc', 'g++']:
            helpers[name]._installed = False

        self.helper.install()
        self.assertSubprocessCalls(
            mock_check_call,
            [
                ['yum', '--quiet', 'install', 'make'],
                ['yum', '--quiet', 'install', 'gcc'],
                ['./RUNME'],
                ['sudo', 'mkdir', '-p', '-m', '755', '/usr/local/bin'],
            ])
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/usr/local/bin', mock_copy.call_args[0][1])

    @mock.patch('platform.system', return_value='Linux')
    @mock.patch('distutils.spawn.find_executable', return_value=None)
    def test_install_linux_need_make_no_package_manager(self, *_):
        """Linux installation requires yum or apt-get if 'make' missing."""
        self.select_package_manager(None)
        for name in ['make', 'clang', 'gcc', 'g++']:
            helpers[name]._installed = False
        with self.assertRaises(NotImplementedError):
            self.helper.install()

    @staticmethod
    def _find_make_only(name):
        """Stub for distutils.spawn.find_executable - only finds 'make'."""
        if name == 'make':
            return "/bin/make"
        else:
            return None

    @mock.patch('platform.system', return_value='Linux')
    @mock.patch('COT.helpers.helper.Helper')
    @mock.patch('distutils.spawn.find_executable')
    def test_install_linux_need_compiler_no_package_manager(self,
                                                            mock_find_exec,
                                                            *_):
        """Linux installation requires yum or apt-get if 'gcc' missing."""
        self.select_package_manager(None)
        for name in ['clang', 'gcc', 'g++']:
            helpers[name]._installed = False
        mock_find_exec.side_effect = self._find_make_only
        with self.assertRaises(NotImplementedError):
            self.helper.install()

    @mock.patch('platform.system', return_value='Darwin')
    @mock.patch('COT.helpers.fatdisk.FatDisk.installable',
                new_callable=mock.PropertyMock, return_value=True)
    def test_install_helper_mac_no_package_manager(self, *_):
        """Mac installation requires port."""
        self.select_package_manager(None)
        self.assertRaises(RuntimeError, self.helper.install)
예제 #4
0
class TestFatDisk(HelperUT):
    """Test cases for FatDisk helper class."""

    def setUp(self):
        """Test case setup function called automatically prior to each test."""
        self.helper = FatDisk()
        super(TestFatDisk, self).setUp()

    def test_get_version(self):
        """Validate .version getter."""
        self.fake_output = "fatdisk, version 1.0.0-beta"
        self.assertEqual(StrictVersion("1.0.0"), self.helper.version)

    def test_install_helper_already_present(self):
        """Trying to re-install is a no-op."""
        self.helper.install_helper()
        self.assertEqual([], self.last_argv)
        self.assertLogged(**self.ALREADY_INSTALLED)

    @mock.patch('shutil.copy')
    @mock.patch('os.path.isdir')
    @mock.patch('os.path.exists')
    @mock.patch('os.makedirs')
    def test_install_helper_apt_get(self,
                                    mock_makedirs,
                                    mock_exists,
                                    mock_isdir,
                                    mock_copy):
        """Test installation via 'apt-get'."""
        mock_isdir.return_value = False
        mock_exists.return_value = False
        mock_makedirs.side_effect = OSError
        mock_copy.return_value = True
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = True
        Helper._apt_updated = False
        self.fake_output = 'is not installed and no information is available'
        self.system = 'Linux'
        os.environ['PREFIX'] = '/usr/local'
        if 'DESTDIR' in os.environ:
            del os.environ['DESTDIR']
        self.helper.install_helper()
        self.assertEqual([
            ['dpkg', '-s', 'make'],
            ['sudo', 'apt-get', '-q', 'update'],
            ['sudo', 'apt-get', '-q', 'install', 'make'],
            ['dpkg', '-s', 'gcc'],
            ['sudo', 'apt-get', '-q', 'install', 'gcc'],
            ['./RUNME'],
            ['sudo', 'mkdir', '-p', '--mode=755', '/usr/local/bin'],
        ], self.last_argv)
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/usr/local/bin', mock_copy.call_args[0][1])
        self.assertTrue(Helper._apt_updated)
        # Make sure we don't call apt-get update/install again unnecessarily.
        self.last_argv = []
        self.fake_output = 'install ok installed'
        os.environ['PREFIX'] = '/opt/local'
        os.environ['DESTDIR'] = '/home/cot'
        self.helper.install_helper()
        self.assertEqual([
            ['dpkg', '-s', 'make'],
            ['dpkg', '-s', 'gcc'],
            ['./RUNME'],
            ['sudo', 'mkdir', '-p', '--mode=755', '/home/cot/opt/local/bin'],
        ], self.last_argv)
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/home/cot/opt/local/bin', mock_copy.call_args[0][1])

    def test_install_helper_port(self):
        """Test installation via 'port'."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = True
        Helper._port_updated = False
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'port', 'selfupdate'],
            ['sudo', 'port', 'install', 'fatdisk'],
        ], self.last_argv)
        self.assertTrue(Helper._port_updated)
        # Make sure we don't call port selfupdate again unnecessarily.
        self.last_argv = []
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'port', 'install', 'fatdisk'],
        ], self.last_argv)

    @mock.patch('shutil.copy')
    @mock.patch('os.path.isdir')
    @mock.patch('os.path.exists')
    @mock.patch('os.makedirs')
    def test_install_helper_yum(self,
                                mock_makedirs,
                                mock_exists,
                                mock_isdir,
                                mock_copy):
        """Test installation via 'yum'."""
        mock_isdir.return_value = False
        mock_exists.return_value = False
        mock_makedirs.side_effect = OSError
        mock_copy.return_value = True
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = True
        self.system = 'Linux'
        os.environ['PREFIX'] = '/usr/local'
        if 'DESTDIR' in os.environ:
            del os.environ['DESTDIR']
        self.helper.install_helper()
        self.assertEqual([
            ['sudo', 'yum', '--quiet', 'install', 'make'],
            ['sudo', 'yum', '--quiet', 'install', 'gcc'],
            ['./RUNME'],
            ['sudo', 'mkdir', '-p', '--mode=755', '/usr/local/bin'],
        ], self.last_argv)
        self.assertTrue(re.search("/fatdisk$", mock_copy.call_args[0][0]))
        self.assertEqual('/usr/local/bin', mock_copy.call_args[0][1])

    def test_install_helper_linux_need_make_no_package_manager(self):
        """Linux installation requires yum or apt-get if 'make' missing."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = False
        self.system = 'Linux'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()

    def test_install_helper_linux_need_compiler_no_package_manager(self):
        """Linux installation requires yum or apt-get if 'gcc' missing."""
        def new_stub_find_executable(self, name):
            """Stub for Helper.find_executable - returns a fixed response."""
            logger.info("stub_find_executable({0})".format(name))
            if name == 'make':
                return "/bin/make"
            else:
                return None
        Helper.find_executable = new_stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        Helper.PACKAGE_MANAGERS['apt-get'] = False
        Helper.PACKAGE_MANAGERS['yum'] = False
        self.system = 'Linux'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()

    def test_install_helper_unsupported(self):
        """No support for installation under Windows."""
        Helper.find_executable = self.stub_find_executable
        Helper.PACKAGE_MANAGERS['port'] = False
        self.system = 'Windows'
        with self.assertRaises(NotImplementedError):
            self.helper.install_helper()