Skip to content

Commit 4ca0ad5

Browse files
authored
Merge pull request #56 from Dakta/BKR-496
(BKR-496) Move create_tmpdir_on from beaker
2 parents d70dc66 + 2888407 commit 4ca0ad5

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
test_name "dsl::helpers::host_helpers #create_tmpdir_on" do
2+
step "#create_tmpdir_on returns a temporary directory on the remote system" do
3+
tmpdir = create_tmpdir_on default
4+
assert_match %r{/}, tmpdir
5+
assert_equal 0, on(default, "touch #{tmpdir}/testfile").exit_code
6+
end
7+
8+
step "#create_tmpdir_on uses the specified path prefix when provided" do
9+
tmpdir = create_tmpdir_on(default, "mypathprefix")
10+
assert_match %r{/mypathprefix}, tmpdir
11+
assert_equal 0, on(default, "touch #{tmpdir}/testfile").exit_code
12+
end
13+
14+
step "#create_tmpdir_on fails if a non-existent user is specified" do
15+
assert_raises Beaker::Host::CommandFailure do
16+
tmpdir = create_tmpdir_on default, '', 'fakeuser'
17+
end
18+
end
19+
20+
step "#create_tmpdir_on sets the user if specified" do
21+
default.user_present('tmpdirtestuser')
22+
tmpdir = create_tmpdir_on(default, nil, 'tmpdirtestuser', nil)
23+
assert_match /tmpdirtestuser/, on(default, "ls -ld #{tmpdir}").output
24+
default.user_absent('tmpdirtestuser')
25+
end
26+
27+
step "#create_tmpdir_on fails if a non-existent group is specified" do
28+
assert_raises Beaker::Host::CommandFailure do
29+
tmpdir = create_tmpdir_on default, '', nil, 'fakegroup'
30+
end
31+
end
32+
33+
step "#create_tmpdir_on sets the group if specified" do
34+
default.group_present('tmpdirtestgroup')
35+
tmpdir = create_tmpdir_on(default, nil, nil, 'tmpdirtestgroup')
36+
assert_match /testgroup/, on(default, "ls -ld #{tmpdir}").output
37+
default.group_absent('tmpdirtestgroup')
38+
end
39+
40+
step "#create_tmpdir_on operates on all hosts if given a hosts array" do
41+
tmpdirs = create_tmpdir_on hosts
42+
hosts.zip(tmpdirs).each do |(host, tmpdir)|
43+
assert_match %r{/}, tmpdir
44+
assert_equal 0, on(host, "touch #{tmpdir}/testfile").exit_code
45+
end
46+
end
47+
end

lib/beaker-puppet/helpers/puppet_helpers.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,55 @@ def sign_certificate
865865
sign_certificate_for(default)
866866
end
867867

868+
# Create a temp directory on remote host, optionally owned by specified user and group.
869+
#
870+
# @param [Host, Array<Host>, String, Symbol] hosts One or more hosts to act upon,
871+
# or a role (String or Symbol) that identifies one or more hosts.
872+
# @param [String] path_prefix A remote path prefix for the new temp directory.
873+
# @param [String] user The name of user that should own the temp directory. If
874+
# not specified, uses default permissions from tmpdir creation.
875+
# @param [String] group The name of group that should own the temp directory.
876+
# If not specified, uses default permissions from tmpdir creation.
877+
#
878+
# @return [String, Array<String>] Returns the name of the newly-created dir, or
879+
# an array of names of newly-created dirs per-host
880+
#
881+
# @note While tempting, this method should not be "optimized" to coalesce calls to
882+
# chown user:group when both options are passed, as doing so will muddy the spec.
883+
def create_tmpdir_on(hosts, path_prefix = '', user = nil, group = nil)
884+
block_on hosts do | host |
885+
# create the directory
886+
dir = host.tmpdir(path_prefix)
887+
# only chown if explicitly passed; don't make assumptions about perms
888+
# only `chown user` for cleaner codepaths
889+
if user
890+
# ensure user exists
891+
if not host.user_get(user).success?
892+
# clean up
893+
host.rm_rf("#{dir}")
894+
raise "User #{user} does not exist on #{host}."
895+
end
896+
# chown only user
897+
host.chown(user, dir)
898+
# on host, "chown #{user} #{dir}"
899+
end
900+
# only chgrp if explicitly passed; don't make assumptions about perms
901+
if group
902+
# ensure group exists
903+
if not host.group_get(group).success?
904+
# clean up
905+
# on host, "rmdir #{dir}"
906+
host.rm_rf(dir)
907+
raise "Group #{group} does not exist on #{host}."
908+
end
909+
# chgrp
910+
# on host, "chgrp #{group} #{dir}"
911+
host.chgrp(group, dir)
912+
end
913+
dir
914+
end
915+
end
916+
868917
# Create a temp directory on remote host with a user. Default user
869918
# is puppet master user.
870919
#

spec/beaker-puppet/helpers/puppet_helpers_spec.rb

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,98 @@ def logger
2929
let( :db ) { make_host( 'db', :roles => %w( database agent ) ) }
3030
let( :hosts ) { [ master, agent, dash, db, custom ] }
3131

32+
describe '#create_tmpdir_on' do
33+
let(:host) { {'user' => 'puppet', 'group' => 'muppets'} }
34+
let(:result_success) { double.as_null_object }
35+
let(:result_failure) { double.as_null_object }
36+
let(:tmpdir) { '/tmp/beaker.XXXXXX/' }
37+
38+
before :each do
39+
allow( host ).to receive( :tmpdir ).and_return( tmpdir )
40+
allow( host ).to receive( :result ).and_return( result_success )
41+
allow( result_success ).to receive( :success? ).and_return( true )
42+
allow( result_success ).to receive( :stdout ).and_return( 'puppet' )
43+
allow( result_failure ).to receive( :success? ).and_return( false )
44+
end
45+
46+
context 'with the path_prefix argument' do
47+
it 'passes path_prefix to host.tmpdir' do
48+
expect( host ).to receive( :tmpdir ).with( 'beaker' )
49+
subject.create_tmpdir_on( host, 'beaker' )
50+
end
51+
end
52+
53+
context 'with the user argument' do
54+
it 'calls chown when a user is specified' do
55+
expect( host ).to receive( :user_get ).and_return( result_success )
56+
expect( host ).to receive( :chown ).with( host['user'], tmpdir )
57+
58+
subject.create_tmpdir_on( host, 'beaker', host['user'] )
59+
end
60+
61+
it 'does not call chown when a user is not specified' do
62+
expect( host ).to_not receive( :chown )
63+
64+
subject.create_tmpdir_on( host, 'beaker' )
65+
end
66+
67+
it 'does not call chown and cleans up when the user does not exist on the host' do
68+
expect( host ).to receive( :user_get ).and_return( result_failure )
69+
expect( host ).to receive( :rm_rf ).with( tmpdir )
70+
71+
expect{
72+
subject.create_tmpdir_on( host, 'beaker', 'invalid.user' )
73+
}.to raise_error( RuntimeError, /User invalid.user does not exist on / )
74+
end
75+
end
76+
77+
context 'with the group argument' do
78+
it 'calls chgrp when a group is specified' do
79+
expect( host ).to receive( :group_get ).and_return( result_success )
80+
expect( host ).to receive( :chgrp ).with( host['group'], tmpdir )
81+
82+
subject.create_tmpdir_on( host, 'beaker', nil, host['group'] )
83+
end
84+
85+
it 'does not call chgrp when a group is not specified' do
86+
expect( subject ).to_not receive( :chgrp )
87+
88+
subject.create_tmpdir_on( host, 'beaker' )
89+
end
90+
91+
it 'does not call chgrp and cleans up when the group does not exist on the host' do
92+
expect( host ).to receive( :group_get ).and_return( result_failure )
93+
expect( host ).to receive( :rm_rf ).with( tmpdir )
94+
95+
expect{
96+
subject.create_tmpdir_on( host, 'beaker', nil, 'invalid.group' )
97+
}.to raise_error( RuntimeError, /Group invalid.group does not exist on / )
98+
end
99+
end
100+
101+
context 'with user and group arguments' do
102+
# don't coalesce the group into chown, i.e. `chown user:group`
103+
# this keeps execution paths simple, clean, and discrete
104+
it 'calls chown and chgrp separately' do
105+
expect( host ).to receive( :user_get ).and_return( result_success )
106+
expect( host ).to receive( :group_get ).and_return( result_success )
107+
expect( host ).to receive( :chown ).with( host['user'], tmpdir )
108+
expect( host ).to receive( :chgrp ).with( host['group'], tmpdir )
109+
110+
subject.create_tmpdir_on( host, 'beaker', host['user'], host['group'] )
111+
end
112+
113+
it 'does not pass group to chown' do
114+
allow( host ).to receive( :user_get ).and_return( result_success )
115+
allow( host ).to receive( :chgrp ).with( host['group'], tmpdir )
116+
117+
expect( host ).to receive( :group_get ).and_return( result_success )
118+
expect( host ).to receive( :chown ).with( host['user'], tmpdir )
119+
120+
subject.create_tmpdir_on( host, 'beaker', host['user'], host['group'] )
121+
end
122+
end
123+
end
32124

33125
describe '#create_tmpdir_for_user' do
34126
let(:host) { {} }

0 commit comments

Comments
 (0)