def test_get_file_returns_tempfile(self): package = 'package' file = 'dummyfile' with PackageUnpacker() as package_unpacker: self.useFixture( MockSomethingFixture(package_unpacker, 'unpack_package', lambda package: None)) self.useFixture( MockSomethingFixture(os.path, 'exists', lambda file: True)) tempfile = package_unpacker.get_file(package, file) self.assertEquals( tempfile, os.path.join(package_unpacker.get_path(package), file))
def test_additional_option_checks(self): self.useFixture(MockSomethingFixture(os.path, 'abspath', lambda x: x)) self.useFixture(MockSomethingFixture(os, "makedirs", lambda x: x)) self.assertRaises( IncompatibleOptions, additional_option_checks, Args(directory="/foo/bar", device="/testdevice", board="testboard")) sys.argv.append("--mmc") self.assertRaises( IncompatibleOptions, additional_option_checks, Args(directory="/foo/bar", device="testdevice", board="testboard")) sys.argv.remove("--mmc")
def test_prep_media_path(self): self.useFixture(MockSomethingFixture(os.path, 'abspath', lambda x: x)) self.useFixture(MockSomethingFixture(os, "makedirs", lambda x: x)) self.assertEqual( "testdevice", prep_media_path( Args(directory=None, device="testdevice", board="testboard"))) self.assertEqual( "/foo/bar/testdevice", prep_media_path( Args(directory="/foo/bar", device="testdevice", board="testboard")))
def test_get_file_no_clash(self): # Test that PackageUnpacker, asked to get the same file path # from 2 different packages, return reference to *different* # temporary files package1 = 'package1' package2 = 'package2' file = 'dummyfile' with PackageUnpacker() as package_unpacker: self.useFixture( MockSomethingFixture(package_unpacker, 'unpack_package', lambda package: None)) self.useFixture( MockSomethingFixture(os.path, 'exists', lambda file: True)) tempfile1 = package_unpacker.get_file(package1, file) tempfile2 = package_unpacker.get_file(package2, file) self.assertNotEquals(tempfile1, tempfile2)
def test_package_installation_refused(self): self.useFixture( MockSomethingFixture(sys, 'stdout', open('/dev/null', 'w'))) # We need this since we are getting user input via raw_input # and we need a 'n' to mimic a refused package installation. self.useFixture(MockSomethingFixture(sys, 'stdin', StringIO('n'))) self.useFixture(MockCmdRunnerPopenFixture(self.output_string)) CommandNotFound = try_import('CommandNotFound.CommandNotFound') if CommandNotFound is None: self.assertRaises(UnableToFindPackageProvidingCommand, install_package_providing, 'mkfs.vfat') else: self.assertRaises(SystemExit, install_package_providing, 'mkfs.vfat')
def mock_install_package_providing(self): def mock_func(command): self.install_pkg_providing_called = True self.useFixture( MockSomethingFixture(utils, 'install_package_providing', mock_func))
def test_verify_files_returns_files(self): self.useFixture( MockSomethingFixture(cmd_runner, 'Popen', self.MockCmdRunnerPopen())) hash_filename = "dummy-file.txt" signature_filename = hash_filename + ".asc" verified_files, _, _ = verify_file_integrity([signature_filename]) self.assertEqual(self.filenames_in_shafile, verified_files)
def test_get_file_raises(self): package = 'package' file = 'dummyfile' with PackageUnpacker() as package_unpacker: self.useFixture( MockSomethingFixture(package_unpacker, 'unpack_package', lambda package: None)) self.assertRaises(AssertionError, package_unpacker.get_file, package, file)
def test_tuple_with_sudo(self): fixture = self.useFixture(MockCmdRunnerPopenFixture()) self.useFixture(MockSomethingFixture(os, 'getuid', lambda: 1000)) cmd_runner.run(( 'foo', 'bar', ), as_root=True).wait() self.assertEqual(['%s foo bar' % sudo_args], fixture.mock.commands_executed)
def test_package_installation_accepted(self): self.useFixture( MockSomethingFixture(sys, 'stdout', open('/dev/null', 'w'))) # We need this since we are getting user input via raw_input # and we need a 'Y' to proceed with the operations. self.useFixture(MockSomethingFixture(sys, 'stdin', StringIO('Y'))) fixture = self.useFixture(MockCmdRunnerPopenFixture( self.output_string)) try: install_package_providing('mkfs.vfat') except UnableToFindPackageProvidingCommand as inst: self.assertEqual("CommandNotFound python module does not exist.", inst.args[0]) else: self.assertEqual([ 'apt-get -s install dosfstools', '%s apt-get --yes install dosfstools' % sudo_args ], fixture.mock.commands_executed)
def test_verify_files(self): fixture = self.useFixture(MockCmdRunnerPopenFixture()) self.useFixture( MockSomethingFixture(tempfile, 'NamedTemporaryFile', self.FakeTempFile)) hash_filename = "dummy-file.txt" signature_filename = hash_filename + ".asc" verify_file_integrity([signature_filename]) self.assertEqual([ 'gpg --status-file=%s --verify %s' % (self.FakeTempFile.name, signature_filename), 'sha1sum -c %s' % hash_filename ], fixture.mock.commands_executed)
def test_check_file_integrity_and_print_errors(self): self.useFixture( MockSomethingFixture(cmd_runner, 'Popen', self.MockCmdRunnerPopen())) hash_filename = "dummy-file.txt" signature_filename = hash_filename + ".asc" result, verified_files = check_file_integrity_and_log_errors( [signature_filename], self.filenames_in_shafile[0], [self.filenames_in_shafile[1]]) self.assertEqual(self.filenames_in_shafile, verified_files) # The sha1sums are faked as passing and all commands return 0, so # it should look like GPG passed self.assertTrue(result)
def test_check_file_integrity_and_print_errors_fail_gpg(self): logging.getLogger().setLevel(100) # Disable logging messages to screen self.useFixture( MockSomethingFixture(cmd_runner, 'Popen', self.MockCmdRunnerPopen_wait_fails())) hash_filename = "dummy-file.txt" signature_filename = hash_filename + ".asc" result, verified_files = check_file_integrity_and_log_errors( [signature_filename], self.filenames_in_shafile[0], [self.filenames_in_shafile[1]]) self.assertEqual([], verified_files) # The sha1sums are faked as passing and all commands return 1, so # it should look like GPG failed self.assertFalse(result) logging.getLogger().setLevel(logging.WARNING)
def setUp(self): super(TestEnsureCommand, self).setUp() self.useFixture( MockSomethingFixture(sys, 'stdout', open('/dev/null', 'w')))
def test_run_as_root_as_root(self): fixture = self.useFixture(MockCmdRunnerPopenFixture()) self.useFixture(MockSomethingFixture(os, 'getuid', lambda: 0)) cmd_runner.run(['foo', 'bar'], as_root=True).wait() self.assertEqual(['foo bar'], fixture.mock.commands_executed)