cucumber-chef 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,7 +27,7 @@ module Cucumber::Chef::Helpers::TestLab
27
27
  dhcpd_config = File.join("/etc/dhcp/test-lab.conf")
28
28
  File.open(dhcpd_config, 'w') do |f|
29
29
  f.puts(Cucumber::Chef.generate_do_not_edit_warning("DHCPD Configuration"))
30
- $servers.each do |key, value|
30
+ @servers.each do |key, value|
31
31
  f.puts
32
32
  f.puts("host #{key} {")
33
33
  f.puts(" hardware ethernet #{value[:mac]};")
@@ -37,12 +37,12 @@ module Cucumber
37
37
  @stdout, @stderr, @stdin = stdout, stderr, stdin
38
38
  @stdout.sync = true if @stdout.respond_to?(:sync=)
39
39
 
40
- @ssh = Cucumber::Chef::SSH.new(@stdout, @stderr, @stdin)
41
- @ssh.config[:host] = @server.public_ip_address
42
- @ssh.config[:ssh_user] = "ubuntu"
43
- @ssh.config[:identity_file] = Cucumber::Chef::Config[:aws][:identity_file]
40
+ @ssh = ZTK::SSH.new(:stdout => @stdout, :stderr => @stderr, :stdin => @stdin)
41
+ @ssh.config.host_name = @server.public_ip_address
42
+ @ssh.config.user = "ubuntu"
43
+ @ssh.config.keys = Cucumber::Chef::Config[:aws][:identity_file]
44
44
 
45
- @command = Cucumber::Chef::Command.new(@stdout, @stderr, @stdin)
45
+ # @command = Cucumber::Chef::Command.new(@stdout, @stderr, @stdin)
46
46
 
47
47
  @cookbooks_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "chef_repo", "cookbooks"))
48
48
  @roles_path = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "chef_repo", "roles"))
@@ -112,7 +112,7 @@ module Cucumber
112
112
  @stdout.print("Downloading chef-server credentials...")
113
113
  Cucumber::Chef.spinner do
114
114
  local_path = Cucumber::Chef.locate(:directory, ".cucumber-chef")
115
- remote_path = File.join("/", "home", @ssh.config[:ssh_user], ".chef")
115
+ remote_path = File.join("/", "home", @ssh.config.user, ".chef")
116
116
 
117
117
  files = [ "#{Cucumber::Chef::Config[:user]}.pem", "validation.pem" ]
118
118
  files.each do |file|
@@ -128,7 +128,7 @@ module Cucumber
128
128
  @stdout.print("Downloading container SSH credentials...")
129
129
  Cucumber::Chef.spinner do
130
130
  local_path = Cucumber::Chef.locate(:directory, ".cucumber-chef")
131
- remote_path = File.join("/", "home", @ssh.config[:ssh_user], ".ssh")
131
+ remote_path = File.join("/", "home", @ssh.config.user, ".ssh")
132
132
 
133
133
  files = { "id_rsa" => "id_rsa-ubuntu" }
134
134
  files.each do |remote_file, local_file|
@@ -155,7 +155,7 @@ module Cucumber
155
155
  }
156
156
 
157
157
  File.open(knife_rb, 'w') do |f|
158
- f.puts(Cucumber::Chef::Template.render(template_file, context))
158
+ f.puts(ZTK::Template.render(template_file, context))
159
159
  end
160
160
  end
161
161
  @stdout.print("done.\n")
@@ -259,13 +259,13 @@ module Cucumber
259
259
  def wait_for_chef_server
260
260
  @stdout.print("Waiting for Chef-Server...")
261
261
  Cucumber::Chef.spinner do
262
- Cucumber::Chef::TCPSocket.new(@server.public_ip_address, 4000, "GET").wait
262
+ ZTK::TCPSocketCheck.new(:host => @server.public_ip_address, :port => 4000, :data => "GET").wait
263
263
  end
264
264
  @stdout.puts("done.\n")
265
265
 
266
266
  @stdout.print("Waiting for Chef-WebUI...")
267
267
  Cucumber::Chef.spinner do
268
- Cucumber::Chef::TCPSocket.new(@server.public_ip_address, 4040, "GET").wait
268
+ ZTK::TCPSocketCheck.new(:host => @server.public_ip_address, :port => 4040, :data => "GET").wait
269
269
  end
270
270
  @stdout.puts("done.\n")
271
271
  end
@@ -283,7 +283,7 @@ module Cucumber
283
283
 
284
284
  @stdout.print("Waiting for SSHD...")
285
285
  Cucumber::Chef.spinner do
286
- Cucumber::Chef::TCPSocket.new(@server.public_ip_address, 22).wait
286
+ ZTK::TCPSocketCheck.new(:host => @server.public_ip_address, :port => 22).wait
287
287
  end
288
288
  @stdout.puts("done.\n")
289
289
 
@@ -20,10 +20,10 @@
20
20
  ################################################################################
21
21
 
22
22
  When /^I enable the running of MiniTest suites for "(.*?)"$/ do |name|
23
- enable_minitest(name)
23
+ $drb_test_lab.enable_minitest(name)
24
24
  end
25
25
 
26
26
  Then /^the tests should run and pass on "(.*?)"$/ do |name|
27
- results = run_minitests(name)
27
+ results = $drb_test_lab.run_minitests(name)
28
28
  results.last.scan(/assertions.*(\d).*failures.*(\d).*errors.*(\d).*skips/).flatten.map { |v| v.to_i }.should == [0,0,0]
29
29
  end
@@ -44,21 +44,21 @@ And /^"([^\"]*)" has a MAC address of "([^\"]*)"$/ do |name, mac|
44
44
  end
45
45
 
46
46
  And /^"([^\"]*)" has been provisioned$/ do |name|
47
- server_create(name, @servers[name])
47
+ $drb_test_lab.server_create(name, @servers[name])
48
48
  end
49
49
 
50
50
  And /^the "([^\"]*)" role has been added to the "([^\"]*)" run list$/ do |role, name|
51
- chef_set_client_attributes(@servers[name], :run_list => ["role[#{role}]"])
51
+ $drb_test_lab.chef_set_client_attributes(@servers[name], :run_list => ["role[#{role}]"])
52
52
  end
53
53
 
54
54
  And /^the "([^\"]*)" recipe has been added to the "([^\"]*)" run list$/ do |recipe, name|
55
- chef_set_client_attributes(@servers[name], :run_list => ["recipe[#{recipe}]"])
55
+ $drb_test_lab.chef_set_client_attributes(@servers[name], :run_list => ["recipe[#{recipe}]"])
56
56
  end
57
57
 
58
58
  And /^"([^\"]*)" is in the "([^\"]*)" environment$/ do |name, environment|
59
- chef_set_client_config(:environment => environment)
59
+ $drb_test_lab.chef_set_client_config(:environment => environment)
60
60
  end
61
61
 
62
62
  And /^the chef-client has been run on "([^\"]*)"$/ do |name|
63
- chef_run_client(name)
63
+ $drb_test_lab.chef_run_client(name)
64
64
  end
@@ -1,81 +1,23 @@
1
- Given /^I have no public keys set$/ do
2
- @auth_methods = %w(password)
3
- end
4
-
5
- Then /^I can ssh to "([^\"]*)" with the following credentials:$/ do |hostname, table|
6
- @auth_methods ||= %w(publickey password)
7
-
8
- credentials = table.hashes
9
- credentials.each do |creds|
10
- lambda {
11
- Net::SSH.start(session["hostname"], creds["username"], :password => creds["password"], :auth_methods => @auth_methods)
12
- }.should_not raise_error(Net::SSH::AuthenticationFailed)
13
- end
14
- end
15
-
16
- Then /^I can ssh to the following hosts with these credentials:$/ do |table|
17
- @keys ||= []
18
- @auth_methods ||= %w(password)
19
- session_details = table.hashes
20
-
21
- session_details.each do |session|
22
- # initialize a list of keys and auth methods for just this session, as
23
- # session can have session-specific keys mixed with global keys
24
- session_keys = Array.new(@keys)
25
- session_auth_methods = Array.new(@auth_methods)
26
-
27
- # you can pass in a keyfile in the session details, so we need to
28
- if session["keyfile"]
29
- session_keys << session["keyfile"]
30
- session_auth_methods << "publickey"
31
- end
32
-
33
- lambda {
34
- @connection = Net::SSH.start(session["hostname"],
35
- session["username"],
36
- :password => session["password"],
37
- :auth_methods => session_auth_methods,
38
- :keys => session_keys)
39
- }.should_not raise_error
40
- end
41
- end
42
-
43
- Given /^I have the following public keys:$/ do |table|
44
- @keys = []
45
- public_key_paths = table.hashes
1
+ When /^I ssh to "([^\"]*)" with the following credentials:$/ do |hostname, table|
2
+ session = table.hashes.first
3
+ lambda {
46
4
 
47
- public_key_paths.each do |key|
48
- File.exist?(key["keyfile"]).should be_true
49
- FileUtils.chmod(0600, key["keyfile"])
50
- @keys << key["keyfile"]
51
- end
5
+ @connection = ZTK::SSH.new
52
6
 
53
- @auth_methods ||= %w(password)
54
- @auth_methods << "publickey"
55
- end
7
+ @connection.config.proxy_host_name = $test_lab.labs_running.first.public_ip_address
8
+ @connection.config.proxy_user = "ubuntu"
9
+ @connection.config.proxy_keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{@connection.config.proxy_user}")
56
10
 
57
- When /^I ssh to "([^\"]*)" with the following credentials:$/ do |hostname, table|
58
- @keys = []
59
- @auth_methods ||= %w(password)
60
- session = table.hashes.first
61
- session_keys = Array.new(@keys)
62
- session_auth_methods = Array.new(@auth_methods)
63
- if session["keyfile"]
64
- session_keys << session["keyfile"]
65
- session_auth_methods << "publickey"
66
- end
11
+ hostname and (@connection.config.host_name = hostname)
12
+ session["username"] and (@connection.config.user = session["username"])
13
+ session["password"] and (@connection.config.password = session["password"])
14
+ session["keyfile"] and (@connection.config.keys = session["keyfile"])
67
15
 
68
- lambda {
69
- @connection = Net::SSH.start(hostname,
70
- session["username"],
71
- :password => session["password"],
72
- :auth_methods => session_auth_methods,
73
- :keys => session_keys)
74
16
  }.should_not raise_error
75
17
  end
76
18
 
77
19
  And /^I run "([^\"]*)"$/ do |command|
78
- @output = @connection.exec!(command)
20
+ @output = @connection.exec(command, :silence => true).output
79
21
  end
80
22
 
81
23
  Then /^I should( not)? see "([^\"]*)" in the output$/ do |boolean, string|
@@ -100,14 +42,14 @@ Then /^(path|directory|file|symlink) "([^\"]*)" should exist$/ do |type, path|
100
42
  command = "ls %s" % [
101
43
  parent
102
44
  ]
103
- @output = @connection.exec!(command)
45
+ @output = @connection.exec(command).output
104
46
  @output.should =~ /#{child}/
105
47
 
106
48
  # if a specific type (directory|file) was specified, test for it
107
49
  command = "stat -c %%F %s" % [
108
50
  path
109
51
  ]
110
- @output = @connection.exec!(command)
52
+ @output = @connection.exec(command).output
111
53
  types = {
112
54
  "file" => /regular file/,
113
55
  "directory" => /directory/,
@@ -132,7 +74,7 @@ Then /^(?:path|directory|file) "([^\"]*)" should be owned by "([^\"]*)"$/ do |pa
132
74
  command = "stat -c %%U:%%G %s" % [
133
75
  path
134
76
  ]
135
- @output = @connection.exec!(command)
77
+ @output = @connection.exec(command).output
136
78
  @output.should =~ /#{owner}/
137
79
  end
138
80
 
@@ -144,7 +86,7 @@ Then /^file "([^\"]*)" should( not)? contain/ do |path, boolean, content|
144
86
 
145
87
  # turn the command-line output and the expectation string into Arrays and strip
146
88
  # leading and trailing cruft from members
147
- @output = @connection.exec!(command).split("\n").map{ |i| i.strip }
89
+ @output = @connection.exec(command).output.split("\n").map{ |i| i.strip }
148
90
  content = content.split("\n").map{ |i| i.strip }
149
91
 
150
92
  # assume no match
@@ -179,14 +121,14 @@ end
179
121
 
180
122
  Then /^package "([^\"]*)" should be installed$/ do |package|
181
123
  command = ""
182
- if (dpkg = @connection.exec!("which dpkg 2> /dev/null")).length > 0
124
+ if (dpkg = @connection.exec("which dpkg 2> /dev/null").output).length > 0
183
125
  command = "#{dpkg.chomp} --get-selections"
184
- elsif (yum = @connection.exec!("which yum 2> /dev/null")).length > 0
126
+ elsif (yum = @connection.exec("which yum 2> /dev/null").output).length > 0
185
127
  command = "#{yum.chomp} -q list installed"
186
128
  # could easily add more cases here, if I knew what they were :)
187
129
  end
188
130
 
189
- @output = @connection.exec!(command)
131
+ @output = @connection.exec(command)
190
132
  @output.should =~ /#{package}/
191
133
  end
192
134
 
@@ -196,7 +138,7 @@ end
196
138
  # * service "foo" is running
197
139
  # * application "foo" is running
198
140
  # * process "foo" is running
199
- #
141
+ #
200
142
  # basically because I couldn't decide what they should be called. Maybe there's
201
143
  # an Official Cucumber-chef Opinion on this. Still, Rubular is fun :)
202
144
 
@@ -204,7 +146,7 @@ end
204
146
  # works
205
147
  Then /^(?:(?:service|application|process)? )?"([^\"]*)" should( not)? be running$/ do |service, boolean|
206
148
  command = "ps ax"
207
- @output = @connection.exec!(command)
149
+ @output = @connection.exec(command).output
208
150
  if (!boolean)
209
151
  @output.should =~ /#{service}/
210
152
  else
@@ -4,9 +4,9 @@ CUCUMBER_CHEF_BOOTSTRAP_DONE="/.cucumber-chef-bootstrap-finished"
4
4
 
5
5
  export DEBIAN_FRONTEND=noninteractive
6
6
 
7
- sed -i "s/127.0.0.1 localhost/127.0.0.1 <%= @config[:hostname] %> <%= @config[:hostname].split(".")[0] %> localhost/" /etc/hosts
8
- echo "<%= @config[:hostname] %>" | tee /etc/hostname
9
- hostname <%= @config[:hostname] %>
7
+ sed -i "s/127.0.0.1 localhost/127.0.0.1 <%= @hostname %> <%= @hostname.split(".")[0] %> localhost/" /etc/hosts
8
+ echo "<%= @hostname %>" | tee /etc/hostname
9
+ hostname <%= @hostname %>
10
10
 
11
11
  echo "deb http://apt.opscode.com/ `lsb_release -cs`-0.10 main" | tee /etc/apt/sources.list.d/opscode.list
12
12
  mkdir -p /etc/apt/trusted.gpg.d
@@ -19,9 +19,9 @@ chown -R ${SUDO_USER}:${SUDO_USER} ${HOME}/.gnupg
19
19
  apt-get -q -y upgrade
20
20
 
21
21
  cat <<EOF | debconf-set-selections
22
- chef chef/chef_server_url string http://<%= @config[:chef_server] %>:4000
23
- chef-solr chef-solr/amqp_password password <%= @config[:amqp_password] %>
24
- chef-server-webui chef-server-webui/admin_password password <%= @config[:admin_password] %>
22
+ chef chef/chef_server_url string http://<%= @chef_server %>:4000
23
+ chef-solr chef-solr/amqp_password password <%= @amqp_password %>
24
+ chef-server-webui chef-server-webui/admin_password password <%= @admin_password %>
25
25
  EOF
26
26
 
27
27
  apt-get -q -y --force-yes install chef chef-server
@@ -56,7 +56,7 @@ EOF
56
56
  chmod +x ${KNIFE_CONFIG_EXP_FILE}
57
57
  ${KNIFE_CONFIG_EXP_FILE}
58
58
 
59
- knife client create <%= @config[:user] %> -d -a -f ${HOME}/.chef/<%= @config[:user] %>.pem
59
+ knife client create <%= @user %> -d -a -f ${HOME}/.chef/<%= @user %>.pem
60
60
 
61
61
  chown -R ${SUDO_USER}:${SUDO_USER} ${HOME}
62
62
 
@@ -93,7 +93,7 @@ if [ ! -f /etc/chef/client.pem ]; then
93
93
  fi
94
94
 
95
95
  cat <<EOF > /etc/chef/first-boot.json
96
- <%= @config[:attributes].to_json %>
96
+ <%= @attributes.to_json %>
97
97
  EOF
98
98
 
99
99
  touch ${CUCUMBER_CHEF_BOOTSTRAP_DONE}
@@ -2,16 +2,16 @@ current_dir = File.dirname(__FILE__)
2
2
 
3
3
  log_level :debug
4
4
  log_location STDOUT
5
- node_name "<%= @config[:user] %>"
6
- client_key "#{current_dir}/<%= @config[:user] %>.pem"
5
+ node_name "<%= @user %>"
6
+ client_key "#{current_dir}/<%= @user %>.pem"
7
7
  validation_client_name "chef-validator"
8
8
  validation_key "#{current_dir}/validation.pem"
9
- chef_server_url "http://<%= @config[:chef_server] %>:4000"
9
+ chef_server_url "http://<%= @chef_server %>:4000"
10
10
  cache_type "BasicFile"
11
11
  cookbook_path ['#{current_dir}/../cookbooks']
12
12
 
13
13
  cache_options(:path => "#{current_dir}/checksums")
14
- <% if @config[:librarian_chef] -%>
14
+ <% if @librarian_chef -%>
15
15
 
16
16
  require 'librarian/chef/integration/knife'
17
17
  cookbook_path Librarian::Chef.install_path, "#{current_dir}/../site-cookbooks"
@@ -2,7 +2,6 @@ require 'rspec/expectations'
2
2
  require 'cucumber/chef'
3
3
  require 'cucumber/chef/helpers'
4
4
  require 'cucumber/chef/steps'
5
- require 'cucumber/chef/version'
6
5
 
7
6
  class CustomWorld
8
7
  include Cucumber::Chef
@@ -13,19 +12,54 @@ World do
13
12
  CustomWorld.new
14
13
  end
15
14
 
16
- $servers = Hash.new(nil)
15
+ ################################################################################
17
16
 
18
- Before do
19
- knife_rb = Cucumber::Chef.locate(:file, ".chef", "knife.rb")
17
+ $logger = ZTK::Logger.new(Cucumber::Chef.log_file)
18
+ Cucumber::Chef.is_rc? and ($logger.level = ZTK::Logger::DEBUG)
19
+
20
+ message = "cucumber-chef v#{Cucumber::Chef::VERSION}"
21
+ print(" * #{message}")
22
+ $logger.info { message }
23
+
24
+ Cucumber::Chef::Config.load
25
+ if ($test_lab = Cucumber::Chef::TestLab.new) && ($test_lab.labs_running.count > 0)
26
+
27
+ # fire up our drb server
28
+ ssh = ZTK::SSH.new
29
+ ssh.config.host_name = $test_lab.labs_running.first.public_ip_address
30
+ ssh.config.user = "ubuntu"
31
+ ssh.config.keys = Cucumber::Chef.locate(:file, ".cucumber-chef", "id_rsa-#{ssh.config.user}")
32
+ ssh.exec("nohup sudo cc-server #{Cucumber::Chef.external_ip}")
33
+ Cucumber::Chef.spinner do
34
+ ZTK::TCPSocketCheck.new(:host => $test_lab.labs_running.first.public_ip_address, :port => 8787, :data => "\n\n").wait
35
+ end
36
+
37
+ # load our test lab knife config
38
+ knife_rb = Cucumber::Chef.locate(:file, ".cucumber-chef", "knife.rb")
20
39
  Chef::Config.from_file(knife_rb)
21
40
 
22
- $servers_bin ||= (Cucumber::Chef.locate(:file, ENV['HOME'], "servers.bin") rescue File.expand_path(File.join(ENV['HOME'], "servers.bin")))
41
+ # initialize our drb object
42
+ $drb_test_lab ||= DRbObject.new_with_uri("druby://#{$test_lab.labs_running.first.public_ip_address}:8787")
43
+ $drb_test_lab and DRb.start_service
44
+ $drb_test_lab.servers = Hash.new(nil)
45
+
46
+ else
47
+ puts("No running cucumber-chef test labs to connect to!")
48
+ exit(1)
49
+ end
50
+
51
+ puts(" - connected to test lab")
52
+
53
+ ################################################################################
54
+
55
+ Before do
56
+ $servers_bin ||= (File.join(Cucumber::Chef.locate(:directory, ".cucumber-chef"), "servers.bin") rescue File.expand_path(File.join(ENV['HOME'], "servers.bin")))
23
57
 
24
58
  # cleanup previous lxc containers if asked
25
- if (ENV['DESTROY'] == "1")
59
+ if ENV['DESTROY']
26
60
  log("servers", "are being destroyed")
27
- servers.each do |name|
28
- server_destroy(name)
61
+ $drb_test_lab.servers.each do |name|
62
+ $drb_test_lab.server_destroy(name)
29
63
  end
30
64
  File.exists?($servers_bin) && File.delete($servers_bin)
31
65
  else
@@ -33,24 +67,33 @@ Before do
33
67
  end
34
68
 
35
69
  if File.exists?($servers_bin)
36
- $servers = Marshal.load(IO.read($servers_bin))
70
+ $drb_test_lab.servers = (Marshal.load(IO.read($servers_bin)) rescue Hash.new(nil))
37
71
  end
38
72
 
39
- chef_set_client_config(:chef_server_url => "http://192.168.255.254:4000",
40
- :validation_client_name => "chef-validator")
73
+ $drb_test_lab.chef_set_client_config(:chef_server_url => "http://192.168.255.254:4000",
74
+ :validation_client_name => "chef-validator")
41
75
  end
42
76
 
43
- After do |scenario|
44
- @connection.close if @connection
77
+ ################################################################################
45
78
 
79
+ After do |scenario|
46
80
  File.open($servers_bin, 'w') do |f|
47
- f.puts(Marshal.dump($servers))
81
+ f.puts(Marshal.dump($drb_test_lab.servers))
48
82
  end
49
83
 
50
84
  Kernel.exit if scenario.failed?
51
85
 
52
- # cleanup non-persistent lxc containers on exit
53
- $servers.select{ |name, attributes| !attributes[:persist] }.each do |name, attributes|
54
- server_destroy(name)
86
+ # cleanup non-persistent lxc containers between tests
87
+ $drb_test_lab.servers.select{ |name, attributes| !attributes[:persist] }.each do |name, attributes|
88
+ $drb_test_lab.server_destroy(name)
55
89
  end
90
+
56
91
  end
92
+
93
+ ################################################################################
94
+
95
+ Kernel.at_exit do
96
+ $drb_test_lab.shutdown
97
+ end
98
+
99
+ ################################################################################