Пример #1
0
  def testCanAddUser(self):
    """Test that we can correctly add a user to a database."""
    new_user = user_db.User(user='******', password='******', uid=1000, gid=1000,
                            gecos='test', home='/dev/null', shell='/bin/false')
    self.assertFalse(self._user_db.UserExists(new_user.user))
    self._user_db.AddUser(new_user)
    self.assertTrue(self._user_db.UserExists(new_user.user))

    # New instances should just see the new user.
    new_db = user_db.UserDB(self.tempdir)
    self.assertTrue(new_db.UserExists(new_user.user))
 def testInstallUser(self):
   """Test that we can install a user correctly."""
   db = self._ParseSpec(MINIMAL_ACCOUNTS_DB)
   mock_user_db = mock.MagicMock()
   db.InstallUser(MINIMAL_DB_USER.name, mock_user_db)
   installed_user = user_db.User(
       user=MINIMAL_DB_USER.name, password=MINIMAL_DB_USER.password,
       uid=MINIMAL_DB_USER.uid, gid=MINIMAL_DB_GROUP.gid,
       gecos=MINIMAL_DB_USER.description, home=MINIMAL_DB_USER.home,
       shell=MINIMAL_DB_USER.shell)
   self.assertEqual([mock.call.AddUser(installed_user)],
                    mock_user_db.mock_calls)
Пример #3
0
    def InstallUser(self,
                    username,
                    sysroot_user_db,
                    uid=None,
                    shell=None,
                    homedir=None,
                    primary_group=None):
        """Install a user in |sysroot_user_db|.

    Args:
      username: name of user to install.
      sysroot_user_db: user_db.UserDB instance representing the installed users
          of a particular sysroot.
      uid: ebuild specified uid.
      shell: ebuild specified shell.
      homedir: ebuild specified home directory.
      primary_group: ebuild specified primary group for user.
    """
        if not username in self.users:
            raise ValueError('Cannot add unknown user "%s"' % username)
        user = self.users[username]

        if user.is_defunct:
            raise ValueError('Refusing to install defunct user: "******"' %
                             username)

        def RaiseIfNotCompatible(user_specified, db_specified, fieldname):
            if user_specified is not None and user_specified != db_specified:
                raise ValueError(
                    'Accounts database %s (%s) for user %s differs from '
                    'requested %s (%s)' % (fieldname, db_specified, user.name,
                                           fieldname, user_specified))

        RaiseIfNotCompatible(uid, user.uid, 'UID')
        RaiseIfNotCompatible(shell, user.shell, 'shell')
        RaiseIfNotCompatible(homedir, user.home, 'homedir')
        RaiseIfNotCompatible(primary_group, user.group_name, 'group')

        if not user.group_name in self.groups:
            raise ValueError(
                'Refusing to install user %s with unknown group %s' %
                (user.name, user.group_name))

        installable_user = user_db.User(user=user.name,
                                        password=user.password,
                                        uid=user.uid,
                                        gid=self.groups[user.group_name].gid,
                                        gecos=user.description,
                                        home=user.home,
                                        shell=user.shell)
        sysroot_user_db.AddUser(installable_user)