Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid authored and joseivanlopez committed Sep 21, 2021
1 parent b2ba866 commit 9861f56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib/y2users/linux/users_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def add_user(user, issues)
# @param issues [Y2Issues::List] a collection for adding an issue if something goes wrong
def create_user(user, issues)
try_create_user(user, issues)
clear_home(user, issues) if user.empty_home
rescue Cheetah::ExecutionFailed => e
issues << Y2Issues::Issue.new(
format(_("The user '%{username}' could not be created"), username: user.name)
Expand Down Expand Up @@ -179,6 +180,16 @@ def try_create_user(user, issues)
log.warn("User '#{user.name}' created without home '#{user.home}'")
end

def clear_home(user, issues)
Yast::Execute.on_target!("/usr/bin/find", *[user.home, "-mindepth", "1", "-delete"])
# Pathname.new(user.home).children.each { |f| f.rm_rf }
rescue Cheetah::ExecutionFailed => e
issues << Y2Issues::Issue.new(
format(_("Something was wrong while cleaning up '%s'"), user.home)
)
log.error(_("Something was wrong while cleaning up '#{user.home}'"))
end

# Executes the commands for setting the password and all its associated
# attributes for the given user
#
Expand Down
31 changes: 31 additions & 0 deletions test/lib/y2users/linux/writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@
end
end

# FIXME: improve it
RSpec.shared_examples "using an empty home" do
context "when home must be empty" do
before do
config.users.by_id(user.id).empty_home = true
end

it "removes all the home content after useradd execution" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
expect(args).to include(user.home)
end

expect(Yast::Execute).to receive(:on_target!).with(/find/, any_args) do |*args|
expect(args).to include(user.home)
expect(args).to include("-delete")
end

writer.write
end
end

context "when home must not be empty" do
it "does not clear recently created user home" do
expect(Yast::Execute).to_not receive(:on_target!).with(/find/, any_args)

writer.write
end
end
end

RSpec.shared_examples "setting password attributes" do
context "setting some password attributes" do
before do
Expand Down Expand Up @@ -508,6 +538,7 @@
include_examples "setting password attributes"
include_examples "writing authorized keys"
include_examples "using btrfs subvolume"
include_examples "using an empty home"

it "executes useradd with all the parameters, including creation of home directory" do
expect(Yast::Execute).to receive(:on_target!).with(/useradd/, any_args) do |*args|
Expand Down

0 comments on commit 9861f56

Please sign in to comment.