lastobelus-vlad 1.4.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.
@@ -0,0 +1,257 @@
1
+ require 'test/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
+ x = 5
36
+ task = @vlad.remote_task(:some_task) { x += some_variable }
37
+ task.execute nil
38
+ assert_equal 1, task.some_variable
39
+ assert_equal 7, x
40
+ end
41
+
42
+ def test_set_false
43
+ set :can_set_nil, nil
44
+ set :lies_are, false
45
+
46
+ assert_equal nil, task.can_set_nil
47
+
48
+ assert_equal false, task.lies_are
49
+ assert_equal false, Rake::RemoteTask.fetch(:lies_are)
50
+ end
51
+
52
+
53
+ def test_fetch_false
54
+ assert_equal false, Rake::RemoteTask.fetch(:unknown, false)
55
+ end
56
+
57
+ def test_execute_exposes_target_host
58
+ host "app.example.com", :app
59
+ task = remote_task(:target_task) { set(:test_target_host, target_host) }
60
+ task.execute nil
61
+ assert_equal "app.example.com", Rake::RemoteTask.fetch(:test_target_host)
62
+ end
63
+
64
+ def test_execute_with_no_hosts
65
+ @vlad.host "app.example.com", :app
66
+ t = @vlad.remote_task(:flunk, :roles => :db) { flunk "should not have run" }
67
+ e = assert_raises(Vlad::ConfigurationError) { t.execute nil }
68
+ assert_equal "No target hosts specified on task flunk for roles [:db]",
69
+ e.message
70
+ end
71
+
72
+ def test_execute_with_no_roles
73
+ t = @vlad.remote_task(:flunk, :roles => :junk) { flunk "should not have run" }
74
+ e = assert_raises(Vlad::ConfigurationError) { t.execute nil }
75
+ assert_equal "No target hosts specified on task flunk for roles [:junk]",
76
+ e.message
77
+ end
78
+
79
+ def test_execute_with_roles
80
+ util_set_hosts
81
+ set :some_variable, 1
82
+ x = 5
83
+ task = @vlad.remote_task(:some_task, :roles => :db) { x += some_variable }
84
+ task.execute nil
85
+ assert_equal 1, task.some_variable
86
+ assert_equal 6, x
87
+ end
88
+
89
+ def test_rsync
90
+ util_setup_task
91
+ @task.target_host = "app.example.com"
92
+
93
+ assert_silent do
94
+ @task.rsync 'localfile', 'host:remotefile'
95
+ end
96
+
97
+ commands = @task.commands
98
+
99
+ assert_equal 1, commands.size, 'not enough commands'
100
+ assert_equal(%w[rsync -azP --delete localfile host:remotefile],
101
+ commands.first)
102
+ end
103
+
104
+ def test_rsync_fail
105
+ util_setup_task
106
+ @task.target_host = "app.example.com"
107
+ @task.action = lambda { false }
108
+
109
+ e = assert_raises Vlad::CommandFailedError do
110
+ assert_silent do
111
+ @task.rsync 'local', 'host:remote'
112
+ end
113
+ end
114
+ exp = "execution failed: rsync -azP --delete local host:remote"
115
+ assert_equal exp, e.message
116
+ end
117
+
118
+ def test_rsync_deprecation
119
+ util_setup_task
120
+ @task.target_host = "app.example.com"
121
+
122
+ out, err = capture_io do
123
+ @task.rsync 'localfile', 'remotefile'
124
+ end
125
+
126
+ commands = @task.commands
127
+
128
+ assert_equal 1, commands.size, 'not enough commands'
129
+ assert_equal(%w[rsync -azP --delete localfile app.example.com:remotefile],
130
+ commands.first)
131
+
132
+ assert_equal("rsync deprecation: pass target_host:remote_path explicitly\n",
133
+ err)
134
+ assert_empty out
135
+ # flunk "not yet"
136
+ end
137
+
138
+ def test_get
139
+ util_setup_task
140
+ @task.target_host = "app.example.com"
141
+
142
+ assert_silent do
143
+ @task.get 'tmp', "remote1", "remote2"
144
+ end
145
+
146
+ commands = @task.commands
147
+
148
+ expected = %w[rsync -azP --delete app.example.com:remote1 app.example.com:remote2 tmp]
149
+
150
+ assert_equal 1, commands.size
151
+ assert_equal expected, commands.first
152
+ end
153
+
154
+ def test_put
155
+ util_setup_task
156
+ @task.target_host = "app.example.com"
157
+
158
+ assert_silent do
159
+ @task.put 'dest' do
160
+ "whatever"
161
+ end
162
+ end
163
+
164
+ commands = @task.commands
165
+
166
+ expected = %w[rsync -azP --delete HAPPY app.example.com:dest]
167
+ commands.first[3] = 'HAPPY'
168
+
169
+ assert_equal 1, commands.size
170
+ assert_equal expected, commands.first
171
+ end
172
+
173
+ def test_run
174
+ util_setup_task
175
+ @task.output << "file1\nfile2\n"
176
+ @task.target_host = "app.example.com"
177
+ result = nil
178
+
179
+ out, err = capture_io do
180
+ result = @task.run("ls")
181
+ end
182
+
183
+ commands = @task.commands
184
+
185
+ assert_equal 1, commands.size, 'not enough commands'
186
+ assert_equal ["ssh", "app.example.com", "ls"],
187
+ commands.first, 'app'
188
+ assert_equal "file1\nfile2\n", result
189
+
190
+ assert_equal "file1\nfile2\n", out
191
+ assert_equal '', err
192
+ end
193
+
194
+ def test_run_failing_command
195
+ util_set_hosts
196
+ util_setup_task
197
+ @task.input = StringIO.new "file1\nfile2\n"
198
+ @task.target_host = 'app.example.com'
199
+ @task.action = lambda { 1 }
200
+
201
+ e = assert_raises(Vlad::CommandFailedError) { @task.run("ls") }
202
+ assert_equal "execution failed with status 1: ssh app.example.com ls", e.message
203
+
204
+ assert_equal 1, @task.commands.size
205
+ end
206
+
207
+ def test_run_sudo
208
+ util_setup_task
209
+ @task.output << "file1\nfile2\n"
210
+ @task.error << 'Password:'
211
+ @task.target_host = "app.example.com"
212
+ def @task.sudo_password() "my password" end # gets defined by set
213
+ result = nil
214
+
215
+ out, err = capture_io do
216
+ result = @task.run("sudo ls")
217
+ end
218
+
219
+ commands = @task.commands
220
+
221
+ assert_equal 1, commands.size, 'not enough commands'
222
+ assert_equal ['ssh', 'app.example.com', 'sudo ls'],
223
+ commands.first
224
+
225
+ assert_equal "my password\n", @task.input.string
226
+
227
+ # WARN: Technically incorrect, the password line should be
228
+ # first... this is an artifact of changes to the IO code in run
229
+ # and the fact that we have a very simplistic (non-blocking)
230
+ # testing model.
231
+ assert_equal "file1\nfile2\nPassword:\n", result
232
+
233
+ assert_equal "file1\nfile2\n", out
234
+ assert_equal "Password:\n", err
235
+ end
236
+
237
+ def test_sudo
238
+ util_setup_task
239
+ @task.target_host = "app.example.com"
240
+ @task.sudo "ls"
241
+
242
+ commands = @task.commands
243
+
244
+ assert_equal 1, commands.size, 'wrong number of commands'
245
+ assert_equal ["ssh", "app.example.com", "sudo -p Password: ls"],
246
+ commands.first, 'app'
247
+ end
248
+
249
+ def util_setup_task(options = {})
250
+ @task = @vlad.remote_task :test_task, options
251
+ @task.commands = []
252
+ @task.output = []
253
+ @task.error = []
254
+ @task.action = nil
255
+ @task
256
+ end
257
+ end
data/test/test_vlad.rb ADDED
@@ -0,0 +1,210 @@
1
+ require 'test/vlad_test_case'
2
+ require 'vlad'
3
+
4
+ $TESTING = true
5
+
6
+ class TestVlad < VladTestCase
7
+ def test_all_hosts
8
+ util_set_hosts
9
+ assert_equal %w[app.example.com db.example.com], @vlad.all_hosts
10
+ end
11
+
12
+ def test_fetch
13
+ set :foo, 5
14
+ assert_equal 5, @vlad.fetch(:foo)
15
+ end
16
+
17
+ def test_fetch_with_default
18
+ assert_equal 5, @vlad.fetch(:not_here, 5)
19
+ end
20
+
21
+ def test_host
22
+ @vlad.host "test.example.com", :app, :db
23
+ expected = {"test.example.com" => {}}
24
+ assert_equal expected, @vlad.roles[:app]
25
+ assert_equal expected, @vlad.roles[:db]
26
+ end
27
+
28
+ def test_host_invalid
29
+ assert_raises(ArgumentError) { @vlad.host nil, :web }
30
+ end
31
+
32
+ def test_host_multiple_hosts
33
+ @vlad.host "test.example.com", :app, :db
34
+ @vlad.host "yarr.example.com", :app, :db, :no_release => true
35
+
36
+ expected = {
37
+ "test.example.com" => {},
38
+ "yarr.example.com" => {:no_release => true}
39
+ }
40
+
41
+ assert_equal expected, @vlad.roles[:app]
42
+ assert_equal expected, @vlad.roles[:db]
43
+ refute_equal(@vlad.roles[:db]["test.example.com"].object_id,
44
+ @vlad.roles[:app]["test.example.com"].object_id)
45
+ end
46
+
47
+ def test_hosts_for_array_of_roles
48
+ util_set_hosts
49
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for([:app, :db])
50
+ end
51
+
52
+ def test_hosts_for_one_role
53
+ util_set_hosts
54
+ @vlad.host "app2.example.com", :app
55
+ assert_equal %w[app.example.com app2.example.com], @vlad.hosts_for(:app)
56
+ end
57
+
58
+ def test_hosts_for_multiple_roles
59
+ util_set_hosts
60
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for(:app, :db)
61
+ end
62
+
63
+ def test_hosts_for_unique
64
+ util_set_hosts
65
+ @vlad.host "app.example.com", :web
66
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for(:app, :db, :web)
67
+ end
68
+
69
+ def test_initialize
70
+ @vlad.set_defaults # ensure these three are virginal
71
+ assert_raises(Vlad::ConfigurationError) { @vlad.repository }
72
+ assert_raises(Vlad::ConfigurationError) { @vlad.deploy_to }
73
+ assert_raises(Vlad::ConfigurationError) { @vlad.domain }
74
+ end
75
+
76
+ def test_role
77
+ @vlad.role :app, "test.example.com"
78
+ expected = {"test.example.com" => {}}
79
+ assert_equal expected, @vlad.roles[:app]
80
+ end
81
+
82
+ def test_role_multiple_hosts
83
+ @vlad.role :app, "test.example.com"
84
+ @vlad.role :app, "yarr.example.com", :no_release => true
85
+ expected = {
86
+ "test.example.com" => {},
87
+ "yarr.example.com" => {:no_release => true}
88
+ }
89
+ assert_equal expected, @vlad.roles[:app]
90
+ end
91
+
92
+ def test_role_multiple_roles
93
+ @vlad.role :app, "test.example.com", :primary => true
94
+ @vlad.role :db, "yarr.example.com", :no_release => true
95
+ expected_db = { "yarr.example.com" => {:no_release => true} }
96
+ assert_equal expected_db, @vlad.roles[:db]
97
+ expected_app = { "test.example.com" => {:primary => true} }
98
+ assert_equal expected_app, @vlad.roles[:app]
99
+ end
100
+
101
+ def test_remote_task
102
+ t = @vlad.remote_task(:test_task) { 5 }
103
+ assert_equal @task_count + 1, Rake.application.tasks.size
104
+ assert_equal({ :roles => [] }, t.options)
105
+ end
106
+
107
+ def test_remote_task_all_hosts_by_default
108
+ util_set_hosts
109
+ t = @vlad.remote_task(:test_task) { 5 }
110
+ assert_equal %w[app.example.com db.example.com], t.target_hosts
111
+ end
112
+
113
+ def test_remote_task_environment_override
114
+ old_env_hosts = ENV["HOSTS"]
115
+ ENV["HOSTS"] = 'other1.example.com, other2.example.com'
116
+ util_set_hosts
117
+ t = @vlad.remote_task(:test_task) { 5 }
118
+ assert_equal %w[other1.example.com other2.example.com], t.target_hosts
119
+ ensure
120
+ ENV["HOSTS"] = old_env_hosts
121
+ end
122
+
123
+ def test_remote_task_body_set
124
+ set(:some_variable, 5)
125
+ @vlad.host 'www.example.com', :app
126
+ @vlad.remote_task(:some_task) do $some_task_result = some_variable end
127
+
128
+ Rake::Task['some_task'].execute nil
129
+ assert_equal @vlad.fetch(:some_variable), $some_task_result
130
+ end
131
+
132
+ def test_remote_task_with_options
133
+ t = @vlad.remote_task :test_task, :roles => [:app, :db] do
134
+ fail "should not run"
135
+ end
136
+ assert_equal({:roles => [:app, :db]}, t.options)
137
+ end
138
+
139
+ def test_remote_task_before_host_declaration
140
+ t = @vlad.remote_task :test_task, :roles => :web do 5 end
141
+ @vlad.host 'www.example.com', :web
142
+ assert_equal %w[www.example.com], t.target_hosts
143
+ end
144
+
145
+ def test_remote_task_role_override
146
+ host "db1", :db
147
+ host "db2", :db
148
+ host "db3", :db
149
+ host "master", :master_db
150
+
151
+ remote_task(:migrate_the_db, :roles => [:db]) { flunk "bad!" }
152
+ task = Rake::Task["migrate_the_db"]
153
+ assert_equal %w[db1 db2 db3], task.target_hosts
154
+
155
+ task.options[:roles] = :master_db
156
+ assert_equal %w[master], task.target_hosts
157
+
158
+ task.options[:roles] = [:master_db]
159
+ assert_equal %w[master], task.target_hosts
160
+ end
161
+
162
+ def test_set
163
+ set :win, 5
164
+ assert_equal 5, @vlad.win
165
+ end
166
+
167
+ def test_set_lazy_block_evaluation
168
+ set(:lose) { raise "lose" }
169
+ assert_raises(RuntimeError) { @vlad.lose }
170
+ end
171
+
172
+ def test_set_with_block
173
+ x = 1
174
+ set(:win) { x += 2 }
175
+
176
+ assert_equal 3, @vlad.win
177
+ assert_equal 3, @vlad.win
178
+ end
179
+
180
+ def test_set_with_reference
181
+ @vlad.instance_eval do
182
+ set(:var_one) { var_two }
183
+ set(:var_two) { var_three }
184
+ set(:var_three) { 5 }
185
+ end
186
+
187
+ assert_equal 5, @vlad.var_one
188
+ end
189
+
190
+ def test_set_with_block_and_value
191
+ e = assert_raises(ArgumentError) do
192
+ set(:loser, 5) { 6 }
193
+ end
194
+ assert_equal "cannot provide both a value and a block", e.message
195
+ end
196
+
197
+ def test_set_with_nil
198
+ set(:win, nil)
199
+ assert_equal nil, @vlad.win
200
+ end
201
+
202
+ def test_set_with_reserved_name
203
+ $TESTING = false
204
+ e = assert_raises(ArgumentError) { set(:all_hosts, []) }
205
+ assert_equal "cannot set reserved name: 'all_hosts'", e.message
206
+ ensure
207
+ $TESTING = true
208
+ end
209
+ end
210
+