Top Level Namespace

Includes:
Chef::Mixin::ShellOut

Defined Under Namespace

Modules: Cuken, SshForever, Vagrant Classes: Chef, NoInspectMatch

Constant Summary collapse

CHEF_CLIENT =
File.join(CHEF_PROJECT_ROOT, "chef", "bin", "chef-client")

Instance Method Summary collapse

Instance Method Details

#call_as_admin(&block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cuken/cucumber/chef/request_steps.rb', line 84

def call_as_admin(&block)
  orig_rest = @rest
  orig_node_name = Chef::Config[:node_name]
  orig_client_key = Chef::Config[:client_key]
  begin
    @rest = admin_rest
    Chef::Config[:node_name] = @rest.auth_credentials.client_name
    Chef::Config[:client_key] = @rest.auth_credentials.key_file
    yield
  ensure
    @rest = orig_rest
    Chef::Config[:node_name] = orig_node_name
    Chef::Config[:client_key] = orig_client_key
  end
end

#chef_client_command_stringObject



26
27
28
29
30
31
32
# File 'lib/cuken/cucumber/chef/run_client_steps.rb', line 26

def chef_client_command_string
  @log_level ||= ENV["LOG_LEVEL"] ? ENV["LOG_LEVEL"] : "error"
  @chef_args ||= ""
  @config_file ||= File.expand_path(File.join(configdir, 'client.rb'))

  "#{File.join(File.dirname(__FILE__), "..", "..", "chef", "bin", "chef-client")} -l #{@log_level} -c #{@config_file} #{@chef_args}"
end

#chef_test_dpkg_installed?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/cuken/cucumber/chef/packages.rb', line 5

def chef_test_dpkg_installed?
  shell_out("dpkg -l chef-integration-test").status.success?
end

#compare_manifests(manifest1, manifest2) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cuken/cucumber/chef/cookbook_steps.rb', line 23

def compare_manifests(manifest1, manifest2)
  Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
    next unless manifest1[segment]
    manifest2.should have_key(segment)

    manifest2_records_by_path = manifest2[segment].inject({}) {|memo,manifest2_record| memo[manifest2_record[:path]] = manifest2_record; memo}
    manifest1[segment].each do |manifest1_record|
      path = manifest1_record[:path]

      manifest2_records_by_path.should have_key(path)
      manifest1_record.should == manifest2_records_by_path[path]
    end
  end
end

#count_structure_levels(obj) ⇒ Object

Walk array/hash to determine maximum depth. A scalar (anything but an Array or Hash) has depth 0.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cuken/cucumber/chef/response_steps.rb', line 4

def count_structure_levels(obj)
  if obj.respond_to?(:keys)
    # empty hash also has depth 0.
    max_depth = 0
    obj.keys.each do |key|
      child_levels = 1 + count_structure_levels(obj[key])
      max_depth = [max_depth, child_levels].max
    end
    max_depth
  elsif obj.is_a?(Array)
    # empty array also has depth 0.
    max_depth = 0
    obj.each do |child|
      child_levels = 1 + count_structure_levels(child)
      max_depth = [max_depth, child_levels].max
    end
    max_depth
  else
    0
  end
end

#debian_compatible?Boolean

Returns:

  • (Boolean)


1
2
3
# File 'lib/cuken/cucumber/chef/packages.rb', line 1

def debian_compatible?
  (ohai[:platform] == 'debian') || (ohai[:platform] == "ubuntu")
end

#dpkg_should_be_installed(pkg_name) ⇒ Object



127
128
129
# File 'lib/cuken/cucumber/chef/packages.rb', line 127

def dpkg_should_be_installed(pkg_name)
  shell_out!("dpkg -l #{pkg_name}")
end

#gem_installed?(gem_name, version) ⇒ Boolean

Given /^I have a clone of typo in the data/tmp dir$/ do

cmd = "git clone #{datadir}/typo.bundle #{tmpdir}/gitrepo/typo"
`#{cmd}`

end

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/cuken/cucumber/chef/deploy_steps.rb', line 8

def gem_installed?(gem_name, version)
  cmd = "gem list -i #{gem_name} -v #{version}"
  `#{cmd}`=~ /true/ ? true : false
end

#get_fixture(stash_name, stash_key) ⇒ Object



338
339
340
341
342
343
344
345
# File 'lib/cuken/cucumber/chef/fixture_steps.rb', line 338

def get_fixture(stash_name, stash_key)
  fixy = @fixtures[stash_name][stash_key]
  if fixy.kind_of?(Proc)
    fixy.call
  else
    fixy
  end
end

#get_knife_configObject

return a set of knife command line parameters that are based on the current Chef::Rest config being used by the feature tests



64
65
66
67
68
69
70
# File 'lib/cuken/cucumber/chef/search_steps.rb', line 64

def get_knife_config
  [
    "--user",       @rest.auth_credentials.client_name,
    "--server-url", @rest.url,
    "--key",        @rest.auth_credentials.key_file
  ].join(" ")
end

#noinspect_match(expected_regex) ⇒ Object



228
229
230
# File 'lib/cuken/cucumber/chef/run_client_steps.rb', line 228

def noinspect_match(expected_regex)
  NoInspectMatch.new(expected_regex)
end


202
203
204
205
206
207
# File 'lib/cuken/cucumber/chef/run_client_steps.rb', line 202

def print_output
  puts "--- run stdout:"
  puts @stdout
  puts "--- run stderr:"
  puts @stderr
end

#purge_chef_integration_debsObject



9
10
11
12
13
14
# File 'lib/cuken/cucumber/chef/packages.rb', line 9

def purge_chef_integration_debs
  if debian_compatible? && chef_test_dpkg_installed?
    shell_out!("dpkg -r chef-integration-test")
    shell_out("dpkg --clear-avail")
  end
end

#remove_integration_test_apt_sourceObject



22
23
24
25
26
# File 'lib/cuken/cucumber/chef/packages.rb', line 22

def remove_integration_test_apt_source
  FileUtils.rm("/etc/apt/sources.list.d/chef-integration-test.list")  
rescue Errno::ENOENT
  Chef::Log.info("Attempted to remove integration test from /etc/apt/sources.list.d but it didn't exist")
end

#sign_request(http_method, path, private_key, user_id, body = "") ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/cuken/cucumber/chef/fixture_steps.rb', line 326

def sign_request(http_method, path, private_key, user_id, body = "")
  timestamp = Time.now.utc.iso8601
  sign_obj = Mixlib::Auth::SignedHeaderAuth.signing_object(
                                                     :http_method=>http_method,
                                                     :path=>path,
                                                     :body=>body,
                                                     :user_id=>user_id,
                                                     :timestamp=>timestamp)
  signed =  sign_obj.sign(private_key).merge({:host => "localhost"})
  signed.inject({}){|memo, kv| memo["#{kv[0].to_s.upcase}"] = kv[1];memo}
end

#tcp_test_port(hostname, port) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/cuken/cucumber/chef/packages.rb', line 135

def tcp_test_port(hostname, port)
  tcp_socket = TCPSocket.new(hostname, port)
  true
rescue Errno::ETIMEDOUT
  false
rescue Errno::ECONNREFUSED
  false
ensure
  tcp_socket && tcp_socket.close
end

#upload_to_sandbox(sandbox_filename, sandbox_file_checksum, url) ⇒ Object

Upload the given file to the sandbox which was created by ‘when I create a sandbox named’



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cuken/cucumber/chef/sandbox_steps.rb', line 5

def upload_to_sandbox(sandbox_filename, sandbox_file_checksum, url)
  
  checksum64 = Base64.encode64([sandbox_file_checksum].pack("H*")).strip
  timestamp = Time.now.utc.iso8601
  file_contents = File.read(sandbox_filename)
  # TODO - 5/28/2010, cw: make signing and sending the request streaming
  sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(
                                                                     :http_method => :put,
                                                                     :path => URI.parse(url).path,
                                                                     :body => file_contents,
                                                                     :timestamp => timestamp,
                                                                     :user_id => rest.client_name
                                                                     )
  headers = { 
    'content-type' => 'application/x-binary', 
    'content-md5' => checksum64, 
    :accept => 'application/json'
  }
  headers.merge!(sign_obj.sign(OpenSSL::PKey::RSA.new(rest.signing_key)))

  # Don't set inflated_response as S3 (for the Platform) doesn't return JSON.
  # Only Open Source does.
  self.inflated_response = nil
  self.exception = nil
  self.api_response = RestClient::Request.execute(
    :method => :put, 
    :url => url, 
    :headers => headers, 
    :payload => file_contents
  )
end