felix-vlad 1.2.0.3

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,211 @@
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_raise(ArgumentError) { @vlad.host "", :app, :db }
30
+ assert_raise(ArgumentError) { @vlad.host nil, :web }
31
+ end
32
+
33
+ def test_host_multiple_hosts
34
+ @vlad.host "test.example.com", :app, :db
35
+ @vlad.host "yarr.example.com", :app, :db, :no_release => true
36
+
37
+ expected = {
38
+ "test.example.com" => {},
39
+ "yarr.example.com" => {:no_release => true}
40
+ }
41
+
42
+ assert_equal expected, @vlad.roles[:app]
43
+ assert_equal expected, @vlad.roles[:db]
44
+ assert_not_equal(@vlad.roles[:db]["test.example.com"].object_id,
45
+ @vlad.roles[:app]["test.example.com"].object_id)
46
+ end
47
+
48
+ def test_hosts_for_array_of_roles
49
+ util_set_hosts
50
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for([:app, :db])
51
+ end
52
+
53
+ def test_hosts_for_one_role
54
+ util_set_hosts
55
+ @vlad.host "app2.example.com", :app
56
+ assert_equal %w[app.example.com app2.example.com], @vlad.hosts_for(:app)
57
+ end
58
+
59
+ def test_hosts_for_multiple_roles
60
+ util_set_hosts
61
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for(:app, :db)
62
+ end
63
+
64
+ def test_hosts_for_unique
65
+ util_set_hosts
66
+ @vlad.host "app.example.com", :web
67
+ assert_equal %w[app.example.com db.example.com], @vlad.hosts_for(:app, :db, :web)
68
+ end
69
+
70
+ def test_initialize
71
+ @vlad.set_defaults # ensure these three are virginal
72
+ assert_raise(Vlad::ConfigurationError) { @vlad.code_repo }
73
+ assert_raise(Vlad::ConfigurationError) { @vlad.deploy_to }
74
+ assert_raise(Vlad::ConfigurationError) { @vlad.domain }
75
+ end
76
+
77
+ def test_role
78
+ @vlad.role :app, "test.example.com"
79
+ expected = {"test.example.com" => {}}
80
+ assert_equal expected, @vlad.roles[:app]
81
+ end
82
+
83
+ def test_role_multiple_hosts
84
+ @vlad.role :app, "test.example.com"
85
+ @vlad.role :app, "yarr.example.com", :no_release => true
86
+ expected = {
87
+ "test.example.com" => {},
88
+ "yarr.example.com" => {:no_release => true}
89
+ }
90
+ assert_equal expected, @vlad.roles[:app]
91
+ end
92
+
93
+ def test_role_multiple_roles
94
+ @vlad.role :app, "test.example.com", :primary => true
95
+ @vlad.role :db, "yarr.example.com", :no_release => true
96
+ expected_db = { "yarr.example.com" => {:no_release => true} }
97
+ assert_equal expected_db, @vlad.roles[:db]
98
+ expected_app = { "test.example.com" => {:primary => true} }
99
+ assert_equal expected_app, @vlad.roles[:app]
100
+ end
101
+
102
+ def test_remote_task
103
+ t = @vlad.remote_task(:test_task) { 5 }
104
+ assert_equal @task_count + 1, Rake.application.tasks.size
105
+ assert_equal Hash.new, t.options
106
+ end
107
+
108
+ def test_remote_task_all_hosts_by_default
109
+ util_set_hosts
110
+ t = @vlad.remote_task(:test_task) { 5 }
111
+ assert_equal %w[app.example.com db.example.com], t.target_hosts
112
+ end
113
+
114
+ def test_remote_task_environment_override
115
+ old_env_hosts = ENV["HOSTS"]
116
+ ENV["HOSTS"] = 'other1.example.com, other2.example.com'
117
+ util_set_hosts
118
+ t = @vlad.remote_task(:test_task) { 5 }
119
+ assert_equal %w[other1.example.com other2.example.com], t.target_hosts
120
+ ensure
121
+ ENV["HOSTS"] = old_env_hosts
122
+ end
123
+
124
+ def test_remote_task_body_set
125
+ set(:some_variable, 5)
126
+ @vlad.host 'www.example.com', :app
127
+ @vlad.remote_task(:some_task) do $some_task_result = some_variable end
128
+
129
+ Rake::Task['some_task'].execute nil
130
+ assert_equal @vlad.fetch(:some_variable), $some_task_result
131
+ end
132
+
133
+ def test_remote_task_with_options
134
+ t = @vlad.remote_task :test_task, :roles => [:app, :db] do
135
+ fail "should not run"
136
+ end
137
+ assert_equal({:roles => [:app, :db]}, t.options)
138
+ end
139
+
140
+ def test_remote_task_before_host_declaration
141
+ t = @vlad.remote_task :test_task, :roles => :web do 5 end
142
+ @vlad.host 'www.example.com', :web
143
+ assert_equal %w[www.example.com], t.target_hosts
144
+ end
145
+
146
+ def test_remote_task_role_override
147
+ host "db1", :db
148
+ host "db2", :db
149
+ host "db3", :db
150
+ host "master", :master_db
151
+
152
+ remote_task(:migrate_the_db, :roles => [:db]) { flunk "bad!" }
153
+ task = Rake::Task["migrate_the_db"]
154
+ assert_equal %w[db1 db2 db3], task.target_hosts
155
+
156
+ task.options[:roles] = :master_db
157
+ assert_equal %w[master], task.target_hosts
158
+
159
+ task.options[:roles] = [:master_db]
160
+ assert_equal %w[master], task.target_hosts
161
+ end
162
+
163
+ def test_set
164
+ set :test, 5
165
+ assert_equal 5, @vlad.test
166
+ end
167
+
168
+ def test_set_lazy_block_evaluation
169
+ set(:test) { fail "lose" }
170
+ assert_raise(RuntimeError) { @vlad.test }
171
+ end
172
+
173
+ def test_set_with_block
174
+ x = 1
175
+ set(:test) { x += 2 }
176
+
177
+ assert_equal 3, @vlad.test
178
+ assert_equal 3, @vlad.test
179
+ end
180
+
181
+ def test_set_with_reference
182
+ @vlad.instance_eval do
183
+ set(:var_one) { var_two }
184
+ set(:var_two) { var_three }
185
+ set(:var_three) { 5 }
186
+ end
187
+
188
+ assert_equal 5, @vlad.var_one
189
+ end
190
+
191
+ def test_set_with_block_and_value
192
+ e = assert_raise(ArgumentError) do
193
+ set(:test, 5) { 6 }
194
+ end
195
+ assert_equal "cannot provide both a value and a block", e.message
196
+ end
197
+
198
+ def test_set_with_nil
199
+ set(:test, nil)
200
+ assert_equal nil, @vlad.test
201
+ end
202
+
203
+ def test_set_with_reserved_name
204
+ $TESTING = false
205
+ e = assert_raise(ArgumentError) { set(:all_hosts, []) }
206
+ assert_equal "cannot set reserved name: 'all_hosts'", e.message
207
+ ensure
208
+ $TESTING = true
209
+ end
210
+ end
211
+
@@ -0,0 +1,39 @@
1
+ require 'test/vlad_test_case'
2
+ require 'vlad'
3
+ require 'vlad/git'
4
+
5
+ class TestVladGit < VladTestCase
6
+ def setup
7
+ super
8
+ @scm = Vlad::Git.new
9
+ set :code_repo, "git@myhost:/home/john/project1"
10
+ end
11
+
12
+ def test_checkout
13
+ # Checkout to the current directory (which is what the :update task passes)
14
+ cmd = @scm.checkout 'master', '.'
15
+ assert_equal 'rm -rf repo && git clone git@myhost:/home/john/project1 repo && cd repo && git checkout -f -b deployed-master master', cmd
16
+
17
+ # Mimic :update task
18
+ # 'head' should become HEAD
19
+ cmd = @scm.checkout 'head', '.'
20
+ assert_equal 'rm -rf repo && git clone git@myhost:/home/john/project1 repo && cd repo && git checkout -f -b deployed-HEAD HEAD', cmd
21
+
22
+ # Checkout to a relative path
23
+ cmd = @scm.checkout 'master', 'some/relative/path'
24
+ assert_equal 'rm -rf some/relative/path && git clone git@myhost:/home/john/project1 some/relative/path && cd some/relative/path && git checkout -f -b deployed-master master', cmd
25
+ end
26
+
27
+ def test_export
28
+ cmd = @scm.export 'master', 'the/release/path'
29
+ assert_equal 'mkdir -p the/release/path && git archive --format=tar master | (cd the/release/path && tar xf -)', cmd
30
+ end
31
+
32
+ def test_revision
33
+ ['head', 'HEAD'].each do |head|
34
+ cmd = @scm.revision(head)
35
+ expected = "`git rev-parse HEAD`"
36
+ assert_equal expected, cmd
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/vlad_test_case'
2
+ require 'vlad'
3
+ require 'vlad/mercurial'
4
+
5
+ class TestVladMercurial < Test::Unit::TestCase
6
+ def setup
7
+ @scm = Vlad::Mercurial.new
8
+ set :code_repo, "http://repo/project"
9
+ end
10
+
11
+ def test_checkout
12
+ cmd = @scm.checkout 'head', '/the/place'
13
+ assert_equal 'hg pull -r tip -R /the/place http://repo/project', cmd
14
+ end
15
+
16
+ def test_export
17
+ cmd = @scm.export 'head', '/the/place'
18
+ assert_equal 'hg archive -r tip -R http://repo/project /the/place', cmd
19
+ end
20
+
21
+ def test_revision
22
+ cmd = @scm.revision('tip')
23
+ expected = "`hg identify -R http://repo/project | cut -f1 -d\\ `"
24
+ assert_equal expected, cmd
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ require 'test/vlad_test_case'
2
+ require 'vlad'
3
+ require 'vlad/perforce'
4
+
5
+ class TestVladPerforce < VladTestCase
6
+ def setup
7
+ super
8
+ @scm = Vlad::Perforce.new
9
+ end
10
+
11
+ def test_checkout
12
+ cmd = @scm.checkout 'head', '/the/place'
13
+ assert_equal 'p4 sync ...#head', cmd
14
+ end
15
+
16
+ def test_checkout_revision
17
+ cmd = @scm.checkout 555, '/the/place'
18
+ assert_equal 'p4 sync ...@555', cmd
19
+ end
20
+
21
+ def test_export
22
+ cmd = @scm.export 'head', '/the/place'
23
+ assert_equal '(cd /the/place && p4 sync ...#head)', cmd
24
+ end
25
+
26
+ def test_revision
27
+ cmd = @scm.revision('head')
28
+ assert_equal '`p4 changes -s submitted -m 1 ...#head | cut -f 2 -d\\ `', cmd
29
+ end
30
+
31
+ def test_rev_no
32
+ assert_equal "@555", @scm.rev_no(555)
33
+ assert_equal "#head", @scm.rev_no('head')
34
+ assert_equal "#head", @scm.rev_no('HEAD')
35
+ assert_equal "@666", @scm.rev_no("@666")
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ require 'test/vlad_test_case'
2
+ require 'vlad'
3
+ require 'vlad/subversion'
4
+
5
+ class TestVladSubversion < VladTestCase
6
+ def setup
7
+ super
8
+ @scm = Vlad::Subversion.new
9
+ set :code_repo, "svn+ssh://repo/myproject"
10
+ end
11
+
12
+ def test_checkout
13
+ cmd = @scm.checkout 'HEAD', '/the/place'
14
+ assert_equal 'svn co -r HEAD svn+ssh://repo/myproject /the/place', cmd
15
+ end
16
+
17
+ def test_export
18
+ cmd = @scm.export 'HEAD', '/the/place'
19
+ assert_equal 'svn export -r HEAD svn+ssh://repo/myproject /the/place', cmd
20
+ end
21
+
22
+ def test_revision
23
+ cmd = @scm.revision('HEAD')
24
+ expected = "`svn info svn+ssh://repo/myproject | grep 'Revision:' | cut -f2 -d\\ `"
25
+ assert_equal expected, cmd
26
+ end
27
+ end
@@ -0,0 +1,71 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require 'vlad'
4
+
5
+ class StringIO
6
+ def readpartial(size) read end # suck!
7
+ end
8
+
9
+ module Process
10
+ def self.expected status
11
+ @@expected ||= []
12
+ @@expected << status
13
+ end
14
+
15
+ def self.waitpid2(pid)
16
+ [ @@expected.shift ]
17
+ end
18
+ end
19
+
20
+
21
+ class Rake::RemoteTask
22
+ attr_accessor :commands, :action, :input, :output, :error
23
+
24
+ Status = Struct.new :exitstatus
25
+
26
+ class Status
27
+ def success?() exitstatus == 0 end
28
+ end
29
+
30
+ def system *command
31
+ @commands << command
32
+ self.action ? self.action[command.join(' ')] : true
33
+ end
34
+
35
+ def popen4 *command
36
+ @commands << command
37
+
38
+ @input = StringIO.new
39
+ out = StringIO.new @output.shift.to_s
40
+ err = StringIO.new @error.shift.to_s
41
+
42
+ raise if block_given?
43
+
44
+ status = self.action ? self.action[command.join(' ')] : 0
45
+ Process.expected Status.new(status)
46
+
47
+ return 42, @input, out, err
48
+ end
49
+
50
+ def select reads, writes, errs, timeout
51
+ [reads, writes, errs]
52
+ end
53
+
54
+ end
55
+
56
+ class VladTestCase < Test::Unit::TestCase
57
+ undef_method :default_test
58
+
59
+ def setup
60
+ @vlad = Rake::RemoteTask
61
+ @vlad.reset
62
+ Rake.application.clear
63
+ @task_count = Rake.application.tasks.size
64
+ @vlad.set :domain, "example.com"
65
+ end
66
+
67
+ def util_set_hosts
68
+ @vlad.host "app.example.com", :app
69
+ @vlad.host "db.example.com", :db
70
+ end
71
+ end
@@ -0,0 +1,64 @@
1
+ #!/bin/bash
2
+
3
+ killall mongrel svnserve 2> /dev/null
4
+
5
+ rm -rf ~/demo
6
+ mkdir ~/demo
7
+ cd ~/demo
8
+
9
+ pause() {
10
+ echo -n "waiting... hit return... "
11
+ read
12
+ echo
13
+ }
14
+
15
+ echo "Starting demo from scratch"
16
+ echo " This step creates a subversion code repo and imports a new rails app"
17
+ echo " It modifies the Rakefile to load Vlad and creates config/deploy.rb"
18
+ echo
19
+ pause
20
+
21
+ svnadmin create svnrepo
22
+ echo "anon-access = write" >> svnrepo/conf/svnserve.conf
23
+
24
+ svnserve -d --foreground -r svnrepo --listen-host localhost &
25
+
26
+ rails mydemoapp
27
+
28
+ cd mydemoapp
29
+
30
+ echo "require 'rubygems'
31
+ require 'vlad'
32
+ Vlad.load" >> Rakefile
33
+
34
+ echo "set :code_repo, 'svn://localhost/blah'
35
+ set :domain, 'localhost'
36
+ set :deploy_to, '/Users/ryan/demo/website'
37
+ set :web_command, 'sudo apachectl'" > config/deploy.rb
38
+
39
+ svn import -m Added . svn://localhost/blah
40
+
41
+ echo
42
+ echo "Here are the tasks available:"
43
+ echo
44
+
45
+ rake -T vlad
46
+
47
+ echo
48
+ echo "The next step deploys and fires up the application"
49
+ echo
50
+ pause
51
+
52
+ rake -t vlad:setup vlad:update vlad:start
53
+
54
+ open http://localhost:8000/
55
+
56
+ echo
57
+ echo "done! check it out"
58
+ echo
59
+ pause
60
+
61
+ rake vlad:stop
62
+
63
+ kill %1
64
+