vlad 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,261 +0,0 @@
1
- require 'vlad_test_case'
2
- require 'vlad'
3
-
4
- class TestRakeRemoteTask < VladTestCase
5
- # TODO: move to minitest
6
- def assert_silent
7
- out, err = capture_io do
8
- yield
9
- end
10
-
11
- assert_empty err
12
- assert_empty out
13
- end
14
-
15
- def test_enhance
16
- util_set_hosts
17
- body = Proc.new { 5 }
18
- task = @vlad.remote_task(:some_task => :foo, &body)
19
- action = Rake::RemoteTask::Action.new(task, body)
20
- assert_equal [action], task.remote_actions
21
- assert_equal task, action.task
22
- assert_equal ["foo"], task.prerequisites
23
- end
24
-
25
- def test_enhance_with_no_task_body
26
- util_set_hosts
27
- util_setup_task
28
- assert_equal [], @task.remote_actions
29
- assert_equal [], @task.prerequisites
30
- end
31
-
32
- def test_execute
33
- util_set_hosts
34
- set :some_variable, 1
35
- set :can_set_nil, nil
36
- set :lies_are, false
37
- x = 5
38
- task = @vlad.remote_task(:some_task) { x += some_variable }
39
- task.execute nil
40
- assert_equal 1, task.some_variable
41
- assert_equal 7, x
42
- assert task.can_set_nil.nil?
43
- assert_equal false, task.lies_are
44
- end
45
-
46
- def test_set_false
47
- set :can_set_nil, nil
48
- set :lies_are, false
49
-
50
- assert_equal nil, task.can_set_nil
51
-
52
- assert_equal false, task.lies_are
53
- assert_equal false, Rake::RemoteTask.fetch(:lies_are)
54
- end
55
-
56
-
57
- def test_fetch_false
58
- assert_equal false, Rake::RemoteTask.fetch(:unknown, false)
59
- end
60
-
61
- def test_execute_exposes_target_host
62
- host "app.example.com", :app
63
- task = remote_task(:target_task) { set(:test_target_host, target_host) }
64
- task.execute nil
65
- assert_equal "app.example.com", Rake::RemoteTask.fetch(:test_target_host)
66
- end
67
-
68
- def test_execute_with_no_hosts
69
- @vlad.host "app.example.com", :app
70
- t = @vlad.remote_task(:flunk, :roles => :db) { flunk "should not have run" }
71
- e = assert_raises(Vlad::ConfigurationError) { t.execute nil }
72
- assert_equal "No target hosts specified on task flunk for roles [:db]",
73
- e.message
74
- end
75
-
76
- def test_execute_with_no_roles
77
- t = @vlad.remote_task(:flunk, :roles => :junk) { flunk "should not have run" }
78
- e = assert_raises(Vlad::ConfigurationError) { t.execute nil }
79
- assert_equal "No target hosts specified on task flunk for roles [:junk]",
80
- e.message
81
- end
82
-
83
- def test_execute_with_roles
84
- util_set_hosts
85
- set :some_variable, 1
86
- x = 5
87
- task = @vlad.remote_task(:some_task, :roles => :db) { x += some_variable }
88
- task.execute nil
89
- assert_equal 1, task.some_variable
90
- assert_equal 6, x
91
- end
92
-
93
- def test_rsync
94
- util_setup_task
95
- @task.target_host = "app.example.com"
96
-
97
- assert_silent do
98
- @task.rsync 'localfile', 'host:remotefile'
99
- end
100
-
101
- commands = @task.commands
102
-
103
- assert_equal 1, commands.size, 'not enough commands'
104
- assert_equal(%w[rsync -azP --delete localfile host:remotefile],
105
- commands.first)
106
- end
107
-
108
- def test_rsync_fail
109
- util_setup_task
110
- @task.target_host = "app.example.com"
111
- @task.action = lambda { false }
112
-
113
- e = assert_raises Vlad::CommandFailedError do
114
- assert_silent do
115
- @task.rsync 'local', 'host:remote'
116
- end
117
- end
118
- exp = "execution failed: rsync -azP --delete local host:remote"
119
- assert_equal exp, e.message
120
- end
121
-
122
- def test_rsync_deprecation
123
- util_setup_task
124
- @task.target_host = "app.example.com"
125
-
126
- out, err = capture_io do
127
- @task.rsync 'localfile', 'remotefile'
128
- end
129
-
130
- commands = @task.commands
131
-
132
- assert_equal 1, commands.size, 'not enough commands'
133
- assert_equal(%w[rsync -azP --delete localfile app.example.com:remotefile],
134
- commands.first)
135
-
136
- assert_equal("rsync deprecation: pass target_host:remote_path explicitly\n",
137
- err)
138
- assert_empty out
139
- # flunk "not yet"
140
- end
141
-
142
- def test_get
143
- util_setup_task
144
- @task.target_host = "app.example.com"
145
-
146
- assert_silent do
147
- @task.get 'tmp', "remote1", "remote2"
148
- end
149
-
150
- commands = @task.commands
151
-
152
- expected = %w[rsync -azP --delete app.example.com:remote1 app.example.com:remote2 tmp]
153
-
154
- assert_equal 1, commands.size
155
- assert_equal expected, commands.first
156
- end
157
-
158
- def test_put
159
- util_setup_task
160
- @task.target_host = "app.example.com"
161
-
162
- assert_silent do
163
- @task.put 'dest' do
164
- "whatever"
165
- end
166
- end
167
-
168
- commands = @task.commands
169
-
170
- expected = %w[rsync -azP --delete HAPPY app.example.com:dest]
171
- commands.first[3] = 'HAPPY'
172
-
173
- assert_equal 1, commands.size
174
- assert_equal expected, commands.first
175
- end
176
-
177
- def test_run
178
- util_setup_task
179
- @task.output << "file1\nfile2\n"
180
- @task.target_host = "app.example.com"
181
- result = nil
182
-
183
- out, err = capture_io do
184
- result = @task.run("ls")
185
- end
186
-
187
- commands = @task.commands
188
-
189
- assert_equal 1, commands.size, 'not enough commands'
190
- assert_equal ["ssh", "app.example.com", "ls"],
191
- commands.first, 'app'
192
- assert_equal "file1\nfile2\n", result
193
-
194
- assert_equal "file1\nfile2\n", out
195
- assert_equal '', err
196
- end
197
-
198
- def test_run_failing_command
199
- util_set_hosts
200
- util_setup_task
201
- @task.input = StringIO.new "file1\nfile2\n"
202
- @task.target_host = 'app.example.com'
203
- @task.action = lambda { 1 }
204
-
205
- e = assert_raises(Vlad::CommandFailedError) { @task.run("ls") }
206
- assert_equal "execution failed with status 1: ssh app.example.com ls", e.message
207
-
208
- assert_equal 1, @task.commands.size
209
- end
210
-
211
- def test_run_sudo
212
- util_setup_task
213
- @task.output << "file1\nfile2\n"
214
- @task.error << 'Password:'
215
- @task.target_host = "app.example.com"
216
- def @task.sudo_password() "my password" end # gets defined by set
217
- result = nil
218
-
219
- out, err = capture_io do
220
- result = @task.run("sudo ls")
221
- end
222
-
223
- commands = @task.commands
224
-
225
- assert_equal 1, commands.size, 'not enough commands'
226
- assert_equal ['ssh', 'app.example.com', 'sudo ls'],
227
- commands.first
228
-
229
- assert_equal "my password\n", @task.input.string
230
-
231
- # WARN: Technically incorrect, the password line should be
232
- # first... this is an artifact of changes to the IO code in run
233
- # and the fact that we have a very simplistic (non-blocking)
234
- # testing model.
235
- assert_equal "file1\nfile2\nPassword:\n", result
236
-
237
- assert_equal "file1\nfile2\n", out
238
- assert_equal "Password:\n", err
239
- end
240
-
241
- def test_sudo
242
- util_setup_task
243
- @task.target_host = "app.example.com"
244
- @task.sudo "ls"
245
-
246
- commands = @task.commands
247
-
248
- assert_equal 1, commands.size, 'wrong number of commands'
249
- assert_equal ["ssh", "app.example.com", "sudo -p Password: ls"],
250
- commands.first, 'app'
251
- end
252
-
253
- def util_setup_task(options = {})
254
- @task = @vlad.remote_task :test_task, options
255
- @task.commands = []
256
- @task.output = []
257
- @task.error = []
258
- @task.action = nil
259
- @task
260
- end
261
- end