Skip to content

Commit a8f19aa

Browse files
author
Clinton Wolfe
committed
Basic test for installing chef gem
1 parent 5d99098 commit a8f19aa

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

Berksfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
metadata

Vagrantfile

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ Vagrant.configure('2') do |config|
66
config.vm.box = 'Berkshelf-CentOS-6.3-x86_64-minimal'
77
config.vm.box_url = 'https://dl.dropbox.com/u/31081437/Berkshelf-CentOS-6.3-x86_64-minimal.box'
88

9-
config.vm.provision :chef_solo do |chef|
10-
chef.json = {
11-
}
9+
config.berkshelf.enable = true
1210

11+
config.vm.provision :chef_solo do |chef|
1312
chef.run_list = [
1413
'recipe[attribute-validator::install]'
1514
]
15+
16+
if ENV['CAV_VAGRANTFILE_FIXTURE']
17+
fixture_file = File.join(Dir.pwd, 'test', 'fixtures', ENV['CAV_VAGRANTFILE_FIXTURE'])
18+
instance_eval(File.read(fixture_file))
19+
end
20+
1621
end
1722
end

recipes/install.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
# Copyright (C) 2013 Clinton Wolfe
66
#
77

8-
# TODO
8+
chef_gem "chef-attribute-validator" do
9+
action :nothing
10+
end
11+
12+
resources('chef_gem[chef-attribute-validator]').run_action(:install)

test/unit/gem_install_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative './spec_helper.rb'
2+
3+
RSpec.configure do |c|
4+
include ServerSpecHelper
5+
6+
c.before :all do
7+
setup_serverspec_vagrant_ssh(c)
8+
end
9+
end
10+
11+
12+
13+
describe 'the VM should get the chef-attribute-validation gem' do
14+
describe file('/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-attribute-validator-0.2.0') do
15+
it {should be_directory }
16+
end
17+
end

test/unit/spec_helper.rb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'serverspec'
2+
require 'pathname'
3+
require 'net/ssh'
4+
5+
include Serverspec::Helper::Exec
6+
include Serverspec::Helper::Ssh
7+
include Serverspec::Helper::DetectOS
8+
9+
module ServerSpecHelper
10+
def setup_serverspec_vagrant_ssh(rspec_conf)
11+
12+
rspec_conf.sudo_password = ENV['SUDO_PASSWORD']
13+
14+
# Guess hostname from file location
15+
block = self.class.metadata[:example_group_block]
16+
file = block.source_location.first
17+
host = File.basename(Pathname.new(file).dirname)
18+
19+
# Reconnect to VM using Vagrant credentials if it is a different hostname
20+
if rspec_conf.host != host # TODO: this is always false, slowwwww tests :(
21+
rspec_conf.ssh.close if rspec_conf.ssh
22+
rspec_conf.host = host
23+
options = Net::SSH::Config.for(rspec_conf.host)
24+
user = options[:user] || Etc.getlogin
25+
`vagrant up default`
26+
config = `vagrant ssh-config default`
27+
if config != ''
28+
# Disabling AssignmentInCondition here because we are doing regex
29+
# matches, whose result is both testable and needed.
30+
# rubocop:disable AssignmentInCondition
31+
config.each_line do |line|
32+
if match = /HostName (.*)/.match(line)
33+
rspec_conf.host = match[1]
34+
elsif match = /User (.*)/.match(line)
35+
user = match[1]
36+
elsif match = /IdentityFile (.*)/.match(line)
37+
options[:keys] = [match[1].gsub(/"/, '')]
38+
elsif match = /Port (.*)/.match(line)
39+
options[:port] = match[1]
40+
end
41+
end
42+
# rubocop:enable AssignmentInCondition
43+
end
44+
rspec_conf.ssh = Net::SSH.start(rspec_conf.host, user, options)
45+
end
46+
end
47+
end
48+

0 commit comments

Comments
 (0)