def test_from_string_conversion_failure(self): config["jobtype_ignore_id_mapping_errors"] = True system = System() username = os.urandom(8).encode("hex") with patch.object(logger, "error") as error: value = system._get_uid_gid_value( username, None, "foo_bar", "getpwnam", "pwd" ) error.assert_called_with( "Failed to convert %s to a %s", username, "bar") self.assertIsNone(value) config["jobtype_ignore_id_mapping_errors"] = False username = os.urandom(8).encode("hex") with patch.object(logger, "error") as error: with self.assertRaises(KeyError): value = system._get_uid_gid_value( username, None, "foo_bar", "getpwnam", "pwd" ) error.assert_called_with( "Failed to convert %s to a %s", username, "bar") self.assertIsNone(value)
def test_removes_directories(self): directories = [ tempfile.mkdtemp(), tempfile.mkdtemp(), tempfile.mkdtemp(), tempfile.mkdtemp(), "THIS PATH DOES NOT EXIST" ] system = System() system._remove_directories(directories) for directory in directories: # This is here because the default behavior for the # underlying remove_directory() function is to ignore # ENOENT if directory == "THIS PATH DOES NOT EXIST": continue if not isdir(directory): continue # Maybe the directory was not removed? If not # then we should expect it to be in atexit for function, args, keywords in atexit._exithandlers: if directory in args: break else: self.fail("Directory %s not removed" % directory)
def test_does_nothing_if_empty_tempdirs(self): system = System() system._tempdirs = set() with patch.object(reactor, "callInThread") as callInThread: system._remove_tempdirs() self.assertEqual(callInThread.call_count, 0)
def test_insufficient_disk_space(self): tempdir = tempfile.mkdtemp() self.addCleanup(os.removedirs, tempdir) system = System() free_space = psutil.disk_usage(tempdir).free with self.assertRaises(InsufficientSpaceError): system._ensure_free_space_in_temp_dir(tempdir, free_space * 2)
def test_log_to_logpool(self): system = System() system.uuid = uuid4() with patch.object(logpool, "log") as log: system._log("Hello, World.") self.assertEqual(log.call_count, 1) log.assert_called_with(system.uuid, "jobtype", "Hello, World.")
def test_get_pwd_from_string(self): system = System() username = user.username() self.assertEqual( pwd.getpwnam(username).pw_uid, system._get_uid_gid_value( username, None, None, "getpwnam", "pwd" ) )
def test_calls_removes_directories(self): system = System() system._tempdirs = set([None]) with patch.object(reactor, "callInThread") as callInThread: system._remove_tempdirs() self.assertEqual(callInThread.call_count, 1) self.assertEqual(system._tempdirs, set()) callInThread.assert_called_with(system._remove_directories, set([None]))
def test_module_not_implemented(self): system = System() with patch.object(logger, "warning") as warning: system._get_uid_gid_value( "", None, "function", NotImplemented, "module") warning.assert_called_with( "This platform does not implement the %r module, skipping %s()", "module", "function" )
def test_cleans_up_disk_space(self): tempdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tempdir) system = System() paths = [ join(tempdir, "a.dat"), join(tempdir, "b.dat"), join(tempdir, "c.dat"), join(tempdir, "d.dat") ] # Create 5MB files on disk in the above directories size_per_file = 5242880 for path in paths: with open(path, "wb") as output: output.write("0" * size_per_file) free_space = psutil.disk_usage(tempdir).free space_to_free = free_space + (size_per_file * len(paths) / 2) system._ensure_free_space_in_temp_dir(tempdir, space_to_free) self.assertLessEqual(len(os.listdir(tempdir)), 2)
def test_get_grp_from_integer(self): system = System() success = True for group_id in os.getgroups(): try: self.assertEqual( group_id, system._get_uid_gid_value( group_id, None, None, "getgrnam", "grp" ) ) success = True # There's numerous ways this could happen including # the user being a part of the group however the group # itself is undefined. except KeyError: pass self.failUnless(success, "Expected at least one successful resolution")
def test_from_integer_grp_no_such_value(self): config["jobtype_ignore_id_mapping_errors"] = False system = System() all_ids = set(group.gr_gid for group in grp.getgrall()) bad_id = 0 while True: if bad_id not in all_ids: break bad_id += 1 with self.assertRaises(KeyError): system._get_uid_gid_value( bad_id, None, None, "getgrgid", "grp" ) config["jobtype_ignore_id_mapping_errors"] = True value = system._get_uid_gid_value( bad_id, None, None, "getgrgid", "grp" ) self.assertIsNone(value)
def test_from_integer_pwd_no_such_value(self): config["jobtype_ignore_id_mapping_errors"] = False system = System() all_ids = set(user.pw_uid for user in pwd.getpwall()) bad_id = 0 while True: if bad_id not in all_ids: break bad_id += 1 with self.assertRaises(KeyError): system._get_uid_gid_value( bad_id, None, None, "getpwuid", "pwd" ) config["jobtype_ignore_id_mapping_errors"] = True value = system._get_uid_gid_value( bad_id, None, None, "getpwuid", "pwd" ) self.assertIsNone(value)
def test_log_assertion(self): system = System() for entry in ("", 1, None, [], tuple(), dict(), 1.0): system.uuid = entry with self.assertRaises(AssertionError): system._log("")
def test_value_type(self): system = System() for entry in (None, [], tuple(), dict(), 1.0): with self.assertRaises(TypeError): system._get_uid_gid_value(entry, None, None, None, None)
def test_has_enough_free_space(self): tempdir = tempfile.mkdtemp() self.addCleanup(os.removedirs, tempdir) system = System() free_space = psutil.disk_usage(tempdir).free system._ensure_free_space_in_temp_dir(tempdir, free_space - 2048)
def test_from_integer_no_such_module(self): system = System() with self.assertRaises(ValueError): system._get_uid_gid_value( 0, None, None, "getpwnam", "foo" )
def test_remove_directories_exception(self): system = System() for entry in ("", 1, None, 1.0): with self.assertRaises(AssertionError): system._remove_directories(entry)
def test_remote_tempdirs_assertion(self): system = System() for entry in ("", 1, None, [], tuple(), dict()): system._tempdirs = entry with self.assertRaises(AssertionError): system._remove_tempdirs()