@@ -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