machines 0.5.1

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.
Files changed (180) hide show
  1. data/.gitignore +11 -0
  2. data/EXAMPLES.md +18 -0
  3. data/Gemfile +4 -0
  4. data/Guardfile +14 -0
  5. data/INSTALL.md +25 -0
  6. data/LICENSE +23 -0
  7. data/README.md +271 -0
  8. data/Rakefile +60 -0
  9. data/TODO.md +92 -0
  10. data/bin/machines +6 -0
  11. data/lib/machines/app_settings.rb +54 -0
  12. data/lib/machines/base.rb +13 -0
  13. data/lib/machines/checks.rb +63 -0
  14. data/lib/machines/cloud_machine.rb +33 -0
  15. data/lib/machines/command.rb +86 -0
  16. data/lib/machines/commandline.rb +148 -0
  17. data/lib/machines/configuration.rb +49 -0
  18. data/lib/machines/core.rb +117 -0
  19. data/lib/machines/database.rb +17 -0
  20. data/lib/machines/file_operations.rb +104 -0
  21. data/lib/machines/help.rb +30 -0
  22. data/lib/machines/installation.rb +151 -0
  23. data/lib/machines/log_command.rb +22 -0
  24. data/lib/machines/logger.rb +65 -0
  25. data/lib/machines/machinesfile.rb +25 -0
  26. data/lib/machines/named_buffer.rb +9 -0
  27. data/lib/machines/questions.rb +15 -0
  28. data/lib/machines/services.rb +24 -0
  29. data/lib/machines/upload.rb +29 -0
  30. data/lib/machines/version.rb +4 -0
  31. data/lib/machines.rb +19 -0
  32. data/lib/packages/abiword.rb +11 -0
  33. data/lib/packages/amazon_mp3.rb +4 -0
  34. data/lib/packages/awstats.rb +16 -0
  35. data/lib/packages/base.rb +14 -0
  36. data/lib/packages/chrome.rb +12 -0
  37. data/lib/packages/cruisecontrol.rb +22 -0
  38. data/lib/packages/dependencies.rb +10 -0
  39. data/lib/packages/docky.rb +36 -0
  40. data/lib/packages/dotfiles.rb +26 -0
  41. data/lib/packages/file_roller.rb +12 -0
  42. data/lib/packages/finalise.rb +4 -0
  43. data/lib/packages/firefox.rb +4 -0
  44. data/lib/packages/gedit.rb +11 -0
  45. data/lib/packages/git.rb +4 -0
  46. data/lib/packages/gmate.rb +33 -0
  47. data/lib/packages/gnome.rb +10 -0
  48. data/lib/packages/gnumeric.rb +11 -0
  49. data/lib/packages/hosts.rb +13 -0
  50. data/lib/packages/load_machines.rb +38 -0
  51. data/lib/packages/monit.rb +10 -0
  52. data/lib/packages/mysql.rb +46 -0
  53. data/lib/packages/nginx.rb +22 -0
  54. data/lib/packages/nginx_logrotate.rb +26 -0
  55. data/lib/packages/openbox.rb +35 -0
  56. data/lib/packages/passenger.rb +14 -0
  57. data/lib/packages/passenger_nginx.rb +8 -0
  58. data/lib/packages/postfix.rb +10 -0
  59. data/lib/packages/questions.rb +5 -0
  60. data/lib/packages/rbenv.rb +27 -0
  61. data/lib/packages/rvm.rb +20 -0
  62. data/lib/packages/save_machines.rb +4 -0
  63. data/lib/packages/slim.rb +6 -0
  64. data/lib/packages/sqlserver.rb +5 -0
  65. data/lib/packages/subtle.rb +29 -0
  66. data/lib/packages/sudo_mods.rb +6 -0
  67. data/lib/packages/time.rb +6 -0
  68. data/lib/packages/time_daily.rb +5 -0
  69. data/lib/packages/timezone.rb +10 -0
  70. data/lib/packages/unison.rb +5 -0
  71. data/lib/packages/virtualbox.rb +11 -0
  72. data/lib/packages/virtualbox_guest.rb +7 -0
  73. data/lib/packages/webapps.rb +36 -0
  74. data/lib/template/Machinesfile +48 -0
  75. data/lib/template/certificates/example.com.crt +0 -0
  76. data/lib/template/certificates/example.com.key +0 -0
  77. data/lib/template/certificates/selfsigned.crt +14 -0
  78. data/lib/template/certificates/selfsigned.key +16 -0
  79. data/lib/template/config.yml +98 -0
  80. data/lib/template/logrotate/app.erb +10 -0
  81. data/lib/template/logrotate/nginx.erb +12 -0
  82. data/lib/template/machines.yml +179 -0
  83. data/lib/template/misc/awstats.conf.erb +7 -0
  84. data/lib/template/misc/ntp.conf +7 -0
  85. data/lib/template/monit/conf.d/delayed_job.erb +11 -0
  86. data/lib/template/monit/conf.d/mysql.erb +7 -0
  87. data/lib/template/monit/conf.d/nginx +5 -0
  88. data/lib/template/monit/conf.d/postfix +7 -0
  89. data/lib/template/monit/conf.d/ssh +6 -0
  90. data/lib/template/monit/conf.d/system.erb +14 -0
  91. data/lib/template/monit/monitrc.erb +10 -0
  92. data/lib/template/monit/upstart.conf +16 -0
  93. data/lib/template/mysql/dbmaster.cnf +7 -0
  94. data/lib/template/mysql/dbslave.cnf +3 -0
  95. data/lib/template/nginx/app_server.conf.erb +87 -0
  96. data/lib/template/nginx/nginx.conf.erb +46 -0
  97. data/lib/template/nginx/upstart.conf.erb +21 -0
  98. data/lib/template/packages/custom.rb +17 -0
  99. data/lib/template/packages/productivity.rb +18 -0
  100. data/lib/template/slim/themes/dark/background.jpg +0 -0
  101. data/lib/template/slim/themes/dark/panel.png +0 -0
  102. data/lib/template/slim/themes/dark/slim.theme +39 -0
  103. data/lib/template/users/phil/dotfiles/bash_aliases +45 -0
  104. data/lib/template/users/phil/dotfiles/config/Trolltech.conf +4 -0
  105. data/lib/template/users/phil/dotfiles/config/gtk-3.0/settings.ini +9 -0
  106. data/lib/template/users/phil/dotfiles/config/openbox/autostart.sh +14 -0
  107. data/lib/template/users/phil/dotfiles/config/openbox/rc.xml +482 -0
  108. data/lib/template/users/phil/dotfiles/config/terminator/config +10 -0
  109. data/lib/template/users/phil/dotfiles/fonts.conf +15 -0
  110. data/lib/template/users/phil/dotfiles/gitconfig +27 -0
  111. data/lib/template/users/phil/dotfiles/gtkrc-2.0 +16 -0
  112. data/lib/template/users/phil/dotfiles/local/share/applications/mimeapps.list +4 -0
  113. data/lib/template/users/phil/dotfiles/unison/default.prf +33 -0
  114. data/lib/template/users/www/authorized_keys +0 -0
  115. data/lib/template/users/www/dotfiles/bash_aliases +40 -0
  116. data/lib/template/webapps.yml +75 -0
  117. data/machines.gemspec +44 -0
  118. data/spec/acceptance/dev_machine_spec.rb +22 -0
  119. data/spec/lib/machines/app_settings_spec.rb +106 -0
  120. data/spec/lib/machines/checks_spec.rb +105 -0
  121. data/spec/lib/machines/cloud_machine_spec.rb +36 -0
  122. data/spec/lib/machines/command_spec.rb +184 -0
  123. data/spec/lib/machines/commandline_spec.rb +299 -0
  124. data/spec/lib/machines/configuration_spec.rb +61 -0
  125. data/spec/lib/machines/core_spec.rb +299 -0
  126. data/spec/lib/machines/database_spec.rb +51 -0
  127. data/spec/lib/machines/file_operations_spec.rb +124 -0
  128. data/spec/lib/machines/help_spec.rb +22 -0
  129. data/spec/lib/machines/installation_spec.rb +176 -0
  130. data/spec/lib/machines/log_command_spec.rb +16 -0
  131. data/spec/lib/machines/logger_spec.rb +70 -0
  132. data/spec/lib/machines/machinesfile_spec.rb +34 -0
  133. data/spec/lib/machines/questions_spec.rb +73 -0
  134. data/spec/lib/machines/services_spec.rb +26 -0
  135. data/spec/lib/machines/upload_spec.rb +86 -0
  136. data/spec/lib/packages/abiword_spec.rb +20 -0
  137. data/spec/lib/packages/amazon_mp3_spec.rb +17 -0
  138. data/spec/lib/packages/awstats_spec.rb +26 -0
  139. data/spec/lib/packages/base_spec.rb +21 -0
  140. data/spec/lib/packages/chrome_spec.rb +30 -0
  141. data/spec/lib/packages/cruisecontrol_spec.rb +33 -0
  142. data/spec/lib/packages/dependencies_spec.rb +20 -0
  143. data/spec/lib/packages/docky_spec.rb +32 -0
  144. data/spec/lib/packages/dotfiles_spec.rb +44 -0
  145. data/spec/lib/packages/file_roller_spec.rb +69 -0
  146. data/spec/lib/packages/firefox_spec.rb +16 -0
  147. data/spec/lib/packages/gedit_spec.rb +20 -0
  148. data/spec/lib/packages/git_spec.rb +16 -0
  149. data/spec/lib/packages/gmate_spec.rb +39 -0
  150. data/spec/lib/packages/gnome_spec.rb +22 -0
  151. data/spec/lib/packages/gnumeric_spec.rb +21 -0
  152. data/spec/lib/packages/hosts_spec.rb +41 -0
  153. data/spec/lib/packages/load_machines_spec.rb +118 -0
  154. data/spec/lib/packages/monit_spec.rb +34 -0
  155. data/spec/lib/packages/mysql_spec.rb +69 -0
  156. data/spec/lib/packages/nginx_logrotate_spec.rb +80 -0
  157. data/spec/lib/packages/nginx_spec.rb +46 -0
  158. data/spec/lib/packages/openbox_spec.rb +41 -0
  159. data/spec/lib/packages/passenger_nginx_spec.rb +20 -0
  160. data/spec/lib/packages/passenger_spec.rb +26 -0
  161. data/spec/lib/packages/postfix_spec.rb +19 -0
  162. data/spec/lib/packages/questions_spec.rb +29 -0
  163. data/spec/lib/packages/rbenv_spec.rb +32 -0
  164. data/spec/lib/packages/rvm_spec.rb +31 -0
  165. data/spec/lib/packages/save_machines_spec.rb +51 -0
  166. data/spec/lib/packages/slim_spec.rb +22 -0
  167. data/spec/lib/packages/sqlserver_spec.rb +17 -0
  168. data/spec/lib/packages/timezone_spec.rb +27 -0
  169. data/spec/lib/packages/unison_spec.rb +17 -0
  170. data/spec/lib/packages/virtualbox_guest_spec.rb +25 -0
  171. data/spec/lib/packages/virtualbox_spec.rb +23 -0
  172. data/spec/lib/packages/webapps_spec.rb +70 -0
  173. data/spec/spec_helper.rb +103 -0
  174. data/spec/support/coverage.rb +8 -0
  175. data/spec/support/fake_out.rb +22 -0
  176. data/spec/support/fakefs_additions.rb +10 -0
  177. data/spec/support/minitest.rb +69 -0
  178. data/spec/support/vm_control.rb +54 -0
  179. data/tmp/.gitkeep +0 -0
  180. metadata +581 -0
@@ -0,0 +1,75 @@
1
+ # BE CAREFUL: THIS FILE IS ALSO WRITTEN TO BY MACHINES
2
+ # This config file is used to read and also write application settings.
3
+ # You can replace these comments with your own. Comment lines will be retained upto
4
+ # the ---.
5
+ #
6
+ # TODO: Move passwords to separate GPG encrypted file
7
+ #
8
+ # password: Password for application database - Leave blank to generate one
9
+ # database: Database name to write to database.yml - Leave blank to use application name
10
+ # username: Username to write to database.yml - Leave blank to use application name
11
+ # The name of the webapp will be used as the name of the destination folder of the clone
12
+ # Any environments can be used here. A machine has a given environment.
13
+ # Any of the keys can be used within or outside the environment
14
+
15
+ ---
16
+ webapps:
17
+ condensd:
18
+ title: 'Condensd'
19
+ scm: git://github.com/PhilT/fountain.git
20
+ branch: condensd
21
+ ssl: false
22
+ write_yml: true
23
+ development:
24
+ server_name: condensd.dev
25
+ password: condensd
26
+ staging:
27
+ server_name: staging.condensd.com
28
+ password:
29
+ monitor_delayed_job: true
30
+ monit_web: true
31
+ production:
32
+ server_name: condensd.com
33
+ password:
34
+ monitor_delayed_job: true
35
+ monit_web: true
36
+
37
+ fountain:
38
+ title: 'Fountain'
39
+ scm: git://github.com/PhilT/fountain.git
40
+ database: fountain
41
+ username: fountain
42
+ ssl: false
43
+ write_yml: true
44
+ development:
45
+ server_name: fountainwiki.dev
46
+ password: fountain
47
+ staging:
48
+ server_name: staging.fountainwiki.com
49
+ username: fountain
50
+ password:
51
+ monitor_delayed_job: true
52
+ production:
53
+ server_name: fountainwiki.com
54
+ password:
55
+ monitor_delayed_job: true
56
+
57
+ velocitytracking:
58
+ title: 'Velocity'
59
+ scm: git://github.com/PhilT/velocity.git
60
+ database: velocity
61
+ username: velocity
62
+ ssl: false
63
+ write_yml: true
64
+ development:
65
+ server_name: velocitytracking.dev
66
+ password: velocity
67
+ staging:
68
+ server_name: staging.velocitytracking.com
69
+ password:
70
+ cert: selfsigned
71
+ production:
72
+ server_name: velocitytracking.com
73
+ password:
74
+ cert: velocitytracking.com
75
+
data/machines.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ require 'base64'
2
+ $LOAD_PATH << 'lib'
3
+ require 'machines/version.rb'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'machines'
7
+ s.version = Machines::VERSION
8
+ s.authors = 'Phil Thompson'
9
+ s.email = Base64.decode64("cGhpbEBlbGVjdHJpY3Zpc2lvbnMuY29t\n")
10
+ s.homepage = 'http://github.com/PhilT/machines'
11
+ s.summary = 'Simple configuration of development, staging and production computers or images for cloud machines'
12
+ s.description = 'Install and configure Ubuntu desktops, laptops, servers and cloud instances. Install software, configure settings, preferences, keys, projects, applications, scripts, etc...'
13
+ s.required_rubygems_version = '>= 1.3.6'
14
+ s.platform = Gem::Platform::RUBY
15
+ s.rubyforge_project = 'machines'
16
+
17
+ %w(activesupport app_conf highline i18n net-ssh net-scp).each do |name|
18
+ s.add_dependency name
19
+ end
20
+
21
+ %w(
22
+ bluecloth
23
+ guard
24
+ guard-bundler
25
+ guard-minitest
26
+ fakefs
27
+ fog
28
+ minitest
29
+ mocha
30
+ rake
31
+ rev
32
+ simplecov
33
+ yard
34
+ ).each do |name|
35
+ s.add_development_dependency name
36
+ end
37
+
38
+ s.files = `git ls-files`.split("\n")
39
+ s.test_files = `git ls-files -- spec/*`.split("\n")
40
+
41
+ s.executables = ['machines']
42
+ s.require_path = 'lib'
43
+ end
44
+
@@ -0,0 +1,22 @@
1
+ require './spec/support/vm_control'
2
+ require 'app_conf'
3
+
4
+ @vm = VmControl.new
5
+ system 'cd tmp && rm -rf acceptance_project'
6
+ system 'reset && clear'
7
+
8
+ begin
9
+ system 'cd tmp && ruby -I../lib ../bin/machines new acceptance_project'
10
+ system 'cd tmp/acceptance_project && ruby -I../../lib ../../bin/machines dryrun testvm'
11
+ system 'cat tmp/acceptance_project/log/output.log'
12
+
13
+ @vm.restore
14
+ @vm.start
15
+
16
+ system 'cd tmp/acceptance_project && ruby -I../../lib ../../bin/machines build testvm'
17
+ puts 'cat tmp/acceptance_project/log/output.log to see full log'
18
+
19
+ ensure
20
+ @vm.kill
21
+ end
22
+
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'AppSettings' do
4
+ include Machines::AppSettings
5
+
6
+ describe 'load_and_generate_passwords_for_webapps' do
7
+ it 'generates passwords and saves to webapps.yml' do
8
+ File.open('webapps.yml', 'w') {|f| f.puts "###\n\n---\nwebapps:\n my_app:\n development:\n password: \n" }
9
+ stubs(:generate_password).returns 'random'
10
+ load_and_generate_passwords_for_webapps
11
+ File.read('webapps.yml').must_equal "###\n\n---\nwebapps:\n my_app:\n development:\n password: random\n"
12
+ end
13
+ end
14
+
15
+ describe 'load_app_settings' do
16
+ before do
17
+ $conf.environment = :test
18
+ $conf.appsroot = '/home/user'
19
+ @settings = <<-EOF
20
+ ---
21
+ webapps:
22
+ app:
23
+ scm: scm://project.git
24
+ test:
25
+ setting: setting
26
+ password: secure
27
+ cert: signed
28
+ EOF
29
+ File.open('webapps.yml', 'w') {|f| f.puts @settings.gsub(" cert: signed\n", '') }
30
+ end
31
+
32
+ it 'loads the app settings for selected apps' do
33
+ load_app_settings ['app']
34
+ $conf.webapps.must_equal({
35
+ 'app' => AppBuilder.new(
36
+ :scm => 'scm://project.git',
37
+ :name => 'app',
38
+ :path => '/home/user/app',
39
+ :root => '/home/user/app/current/public',
40
+ :setting => 'setting',
41
+ :password => 'secure'
42
+ )
43
+ })
44
+ end
45
+
46
+ it 'handles ssl settings' do
47
+ File.open('webapps.yml', 'w') {|f| f.puts @settings }
48
+ load_app_settings ['app']
49
+ $conf.webapps.must_equal({
50
+ 'app' => AppBuilder.new(
51
+ :name => 'app',
52
+ :scm => 'scm://project.git',
53
+ :path => '/home/user/app',
54
+ :root => '/home/user/app/current/public',
55
+ :setting => 'setting',
56
+ :password => 'secure',
57
+ :ssl_key => 'signed.key',
58
+ :ssl_crt => 'signed.crt',
59
+ :cert => 'signed',
60
+ :ssl => true
61
+ )
62
+ })
63
+ end
64
+
65
+ it 'does not fail when settings not included for specified environment' do
66
+ File.open('webapps.yml', 'w') {|f| f.puts "---\nwebapps:\n app:\n path: path\n" }
67
+ load_app_settings(['app'])
68
+ end
69
+
70
+ it 'loads settings for all apps when none specified' do
71
+ settings = <<-EOF
72
+ ---
73
+ webapps:
74
+ app:
75
+ scm: scm://project.git
76
+ test:
77
+ setting: setting
78
+ password: secure
79
+ other:
80
+ scm: scm://other_project
81
+ test:
82
+ setting: other_setting
83
+ password: secure
84
+ EOF
85
+ File.open('webapps.yml', 'w') {|f| f.puts settings }
86
+ load_app_settings nil
87
+ $conf.webapps.must_equal({
88
+ 'app' => AppBuilder.new(
89
+ :name => 'app',
90
+ :path => '/home/user/app',
91
+ :scm => 'scm://project.git',
92
+ :root => '/home/user/app/current/public',
93
+ :setting => 'setting',
94
+ :password => 'secure'),
95
+ 'other' => AppBuilder.new(
96
+ :name => 'other',
97
+ :root => '/home/user/other/current/public',
98
+ :scm => 'scm://other_project',
99
+ :path => '/home/user/other',
100
+ :setting => 'other_setting',
101
+ :password => 'secure')
102
+ })
103
+ end
104
+ end
105
+ end
106
+
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Checks' do
4
+ describe 'echo_result' do
5
+ it do
6
+ echo_result.must_equal '&& echo CHECK PASSED || echo CHECK FAILED'
7
+ end
8
+ end
9
+
10
+ describe 'package' do
11
+ it 'exists' do
12
+ check_package('package1').must_equal "dpkg --get-selections | grep package1.*install #{echo_result}"
13
+ end
14
+
15
+ it 'does not exist' do
16
+ check_package('package1', false).must_equal "dpkg --get-selections | grep package1.*deinstall #{echo_result}"
17
+ end
18
+ end
19
+
20
+ describe 'check_gem' do
21
+ it do
22
+ check_gem('gem').must_equal "gem search gem --installed #{echo_result}"
23
+ end
24
+
25
+ it do
26
+ check_gem('gem', '1.2.3').must_equal "gem search gem -v 1.2.3 --installed #{echo_result}"
27
+ end
28
+ end
29
+
30
+ describe 'check_file' do
31
+ it do
32
+ check_file('file').must_equal "test -s file #{echo_result}"
33
+ end
34
+
35
+ it do
36
+ check_file('file', false).must_equal "test ! -s file #{echo_result}"
37
+ end
38
+ end
39
+
40
+ describe 'check_link' do
41
+ it do
42
+ check_link('link').must_equal "test -L link #{echo_result}"
43
+ end
44
+ end
45
+
46
+ describe 'check_dir' do
47
+ it do
48
+ check_dir('dir').must_equal "test -d dir #{echo_result}"
49
+ end
50
+
51
+ it do
52
+ check_dir('dir', false).must_equal "test ! -d dir #{echo_result}"
53
+ end
54
+ end
55
+
56
+ describe 'check_perms' do
57
+ it do
58
+ check_perms('764', 'path').must_equal "ls -la path | grep rwxrw-r-- #{echo_result}"
59
+ end
60
+
61
+ it do
62
+ check_perms('235', 'path').must_equal "ls -la path | grep -w--wxr-x #{echo_result}"
63
+ end
64
+ end
65
+
66
+ describe 'check_owner' do
67
+ it do
68
+ check_owner('owner', 'path').must_equal "ls -la path | grep \"owner.*owner\" #{echo_result}"
69
+ end
70
+ end
71
+
72
+ describe 'check_string' do
73
+ it do
74
+ check_string('string', 'file').must_equal "grep \"string\" file #{echo_result}"
75
+ end
76
+ end
77
+
78
+ describe 'check_daemon' do
79
+ it do
80
+ check_daemon('daemon').must_equal "ps aux | grep daemon | grep -v grep #{echo_result}"
81
+ end
82
+
83
+ it do
84
+ check_daemon('daemon', false).must_equal "ps aux | grep daemon | grep -v grep | grep -v daemon #{echo_result}"
85
+ end
86
+ end
87
+
88
+ describe 'check_init_d' do
89
+ it do
90
+ check_init_d('name').must_equal "test -L /etc/rc0.d/K20name #{echo_result}"
91
+ end
92
+ end
93
+
94
+ describe 'check_command' do
95
+ it do
96
+ check_command('command', 'match').must_equal "command | grep match #{echo_result}"
97
+ end
98
+
99
+ it do
100
+ check_command('command').must_equal "command #{echo_result}"
101
+ end
102
+
103
+ end
104
+ end
105
+
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'fog'
3
+
4
+ describe 'CloudMachine' do
5
+ include Machines::CloudMachine
6
+
7
+ before do
8
+ Fog.mock!
9
+ $conf.from_hash(:cloud => {:provider => 'AWS', :aws_access_key_id => '123', :aws_secret_access_key => '456'})
10
+ $conf.from_hash(:machine => {:cloud => {:flavor_id => 't1-micro', :image_id => 'ami-11f0cc65', :region => 'eu-west-1'}})
11
+ end
12
+
13
+ it 'displays an error message when fog gem is not available' do
14
+ stubs(:require)
15
+ expects(:require).with('fog').raises LoadError
16
+ lambda do
17
+ begin
18
+ connect_to_cloud
19
+ rescue LoadError
20
+ end
21
+ end.must_output "fog gem required to use cloud features.
22
+ Please \"gem install fog\".
23
+ "
24
+ end
25
+
26
+ it 'sets correct options and connects' do
27
+ connect_to_cloud
28
+ $conf.cloud.connection.must_be_kind_of Fog::Compute::AWS::Mock
29
+ end
30
+
31
+ it 'creates a server' do
32
+ connect_to_cloud
33
+ create_server
34
+ end
35
+ end
36
+
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ describe Machines::Command do
4
+ subject { Machines::Command.new('command', 'check') }
5
+
6
+ describe 'initialize' do
7
+ it 'sets line, command, check' do
8
+ subject.command.must_equal 'command'
9
+ subject.check.must_equal 'check'
10
+ end
11
+ end
12
+
13
+ describe 'run' do
14
+ before(:each) do
15
+ $conf.commands = [subject]
16
+ @mock_ssh = mock 'Net::SSH'
17
+ @mock_ssh.stubs(:exec!).returns 'result'
18
+ @mock_ssh.stubs(:exec!).with('export TERM=linux && command').returns "result"
19
+ @mock_ssh.stubs(:exec!).with('export TERM=linux && check').returns "CHECK PASSED"
20
+ Machines::Command.ssh = @mock_ssh
21
+ end
22
+
23
+ it 'does not execute command when logging only' do
24
+ @mock_ssh.expects(:exec!).never
25
+ $conf.log_only = true
26
+ subject.run
27
+ end
28
+
29
+ describe 'check_result' do
30
+ it 'returns NOT CHECKED when nothing to execute' do
31
+ subject = Machines::Command.new('command', nil)
32
+ $conf.commands = [subject]
33
+ @mock_ssh.expects(:exec!).with(nil).never
34
+ subject.run
35
+
36
+ $console.next.must_equal "100% RUN command\r"
37
+ $console.next.must_equal colored("100% RUN command\n", :warning)
38
+
39
+ $file.next.must_equal colored("RUN command\n", :highlight)
40
+ $file.next.must_equal "result\n"
41
+ $file.next.must_equal colored("NOT CHECKED\n", :warning)
42
+ end
43
+ end
44
+
45
+ describe 'logs' do
46
+ before(:each) do
47
+ $conf.log_only = false
48
+ end
49
+
50
+ it 'to screen using newline instead of return when logging only' do
51
+ $conf.log_only = true
52
+ subject.run
53
+
54
+ $console.next.must_equal "100% RUN command\n"
55
+ end
56
+
57
+ it 'defaults screen logging to return' do
58
+ $conf.log_only = nil
59
+ subject.run
60
+
61
+ $console.next.must_equal "100% RUN command\r"
62
+ end
63
+
64
+ it 'successful command to screen and file' do
65
+ subject.run
66
+
67
+ $console.next.must_equal "100% RUN command\r"
68
+ $console.next.must_equal colored("100% RUN command\n", :success)
69
+
70
+ $file.next.must_equal colored("RUN command\n", :highlight)
71
+ $file.next.must_equal "result\n"
72
+ $file.next.must_equal colored("CHECK PASSED\n", :success)
73
+ end
74
+
75
+ it 'successful sudo command to screen and file' do
76
+ $conf.password = 'userpass'
77
+ @mock_ssh.stubs(:exec!).with("echo userpass | sudo -S bash -c 'export TERM=linux && check'").returns 'CHECK PASSED'
78
+ subject.use_sudo
79
+ subject.run
80
+
81
+ $console.next.must_equal "100% SUDO command\r"
82
+ $console.next.must_equal colored("100% SUDO command\n", :success)
83
+
84
+ $file.next.must_equal colored("SUDO command\n", :highlight)
85
+ $file.next.must_equal "result\n"
86
+ $file.next.must_equal colored("CHECK PASSED\n", :success)
87
+ end
88
+
89
+ it 'unsuccesful command to screen and file' do
90
+ @mock_ssh.stubs(:exec!).with('export TERM=linux && check').returns "CHECK FAILED"
91
+
92
+ subject.run
93
+
94
+ $console.next.must_equal "100% RUN command\r"
95
+ $console.next.must_equal colored("100% RUN command\n", :failure)
96
+
97
+ $file.next.must_equal colored("RUN command\n", :highlight)
98
+ $file.next.must_equal "result\n"
99
+ $file.next.must_equal colored("CHECK FAILED\n", :failure)
100
+ end
101
+
102
+ it 'ensure failures are always logged even when exceptions raised' do
103
+ @mock_ssh.expects(:exec!).with('export TERM=linux && check').raises Exception
104
+
105
+ lambda {subject.run}.must_raise Exception
106
+
107
+ $console.next.must_equal "100% RUN command\r"
108
+ $console.next.must_equal colored("100% RUN command\n", :failure)
109
+
110
+ $file.next.must_equal colored("RUN command\n", :highlight)
111
+ $file.next.must_equal "result\n"
112
+ $file.next.must_equal colored("Exception\n", :failure)
113
+ end
114
+
115
+ it 'ensure logging failures do not stop app exiting gracefully' do
116
+ @mock_ssh.expects(:exec!).with('export TERM=linux && check').raises Exception
117
+ Machines::Command.file.stubs(:log)
118
+ Machines::Command.file.expects(:log).with('Exception', :color => :failure).raises ArgumentError
119
+ begin
120
+ subject.run
121
+ rescue Exception
122
+ rescue ArgumentError
123
+ flunk 'Expecting Exception to be raised but not ArgumentError'
124
+ end
125
+ end
126
+
127
+ it 'ensure console failures do not stop app exiting gracefully' do
128
+ @mock_ssh.expects(:exec!).with('export TERM=linux && check').raises Exception
129
+ Machines::Command.console.stubs(:log)
130
+ Machines::Command.console.expects(:log).with('100% RUN command', :color => :failure).raises ArgumentError
131
+ begin
132
+ subject.run
133
+ rescue Exception
134
+ rescue ArgumentError
135
+ flunk 'Expecting Exception to be raised but not ArgumentError'
136
+ end
137
+ end
138
+ end
139
+
140
+ it 'wraps command execution in sudo with a password' do
141
+ $conf.password = 'userpass'
142
+ @mock_ssh.expects(:exec!).with("echo userpass | sudo -S bash -c 'export TERM=linux && command'").returns "result"
143
+
144
+ subject.use_sudo
145
+ subject.run
146
+ end
147
+
148
+ it 'wraps command execution in sudo with no password' do
149
+ @mock_ssh.expects(:exec!).with("sudo -S bash -c 'export TERM=linux && command'").returns "result"
150
+
151
+ subject.use_sudo
152
+ subject.run
153
+ end
154
+
155
+ it 'wraps check execution in sudo with a password' do
156
+ $conf.password = 'userpass'
157
+ @mock_ssh.expects(:exec!).with("echo userpass | sudo -S bash -c 'export TERM=linux && check'").returns 'CHECK PASSED'
158
+
159
+ subject.use_sudo
160
+ subject.run
161
+ end
162
+
163
+ it 'wraps check execution in sudo with no password' do
164
+ @mock_ssh.expects(:exec!).with("sudo -S bash -c 'export TERM=linux && check'").returns 'CHECK PASSED'
165
+
166
+ subject.use_sudo
167
+ subject.run
168
+ end
169
+ end
170
+
171
+ describe 'progress' do
172
+ it 'returns correct percentage' do
173
+ $conf.commands = 200.times.map { Machines::Command.new('command', 'check') }
174
+ $conf.commands[10].send('progress').must_equal ' 6% '
175
+ end
176
+ end
177
+
178
+ describe 'info' do
179
+ it 'returns the command' do
180
+ subject.info.must_equal 'RUN command'
181
+ end
182
+ end
183
+ end
184
+