Skip to content

Commit

Permalink
Use chef-ingredient ~> 1.0 to perform installs from packages.chef.io.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvonne Lam committed Apr 22, 2016
1 parent 8be0a6b commit c3d17d7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 57 deletions.
4 changes: 2 additions & 2 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

default['supermarket_omnibus']['package_url'] = nil
default['supermarket_omnibus']['package_version'] = :latest
default['supermarket_omnibus']['package_repo'] = 'chef-stable'
default['supermarket_omnibus']['package_repo'] = 'stable'
# use the following to consume nightly builds of packagecloud:
# default['supermarket_omnibus']['package_repo'] = 'chef-current'
# default['supermarket_omnibus']['package_repo'] = 'current'
30 changes: 6 additions & 24 deletions libraries/supermarket_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,6 @@ def merged_supermarket_config
not_if "grep #{node['hostname']} /etc/hosts"
end

directory '/etc/supermarket' do
owner 'root'
group 'root'
mode '0755'
end

file '/etc/supermarket/supermarket.json' do
owner 'root'
group 'root'
mode '0644'
content JSON.pretty_generate(merged_supermarket_config)
sensitive true
notifies :reconfigure, 'chef_ingredient[supermarket]'
end

if node['supermarket_omnibus']['package_url']
pkgname = ::File.basename(node['supermarket_omnibus']['package_url'])
cache_path = ::File.join(Chef::Config[:file_cache_path], pkgname).gsub(/~/, '-') # rubocop:disable Performance/StringReplacement
Expand All @@ -91,24 +76,21 @@ def merged_supermarket_config
end
end

case node['platform_family']
when 'debian'
node.default['apt-chef']['repo_name'] = node['supermarket_omnibus']['package_repo']
when 'rhel'
node.default['yum-chef']['repositoryid'] = node['supermarket_omnibus']['package_repo']
end

chef_ingredient 'supermarket' do
channel node['supermarket_omnibus']['package_repo'].to_sym
config JSON.pretty_generate(merged_supermarket_config)
ctl_command '/opt/supermarket/bin/supermarket-ctl'
sensitive true

# Prefer package_url if set over custom repository
# If set, prefer package_url to packages.chef.io.
if node['supermarket_omnibus']['package_url']
Chef::Log.info "Using Supermarket package source: #{node['supermarket_omnibus']['package_url']}"
package_source cache_path
else
Chef::Log.info "Using CHEF's public repository #{node['supermarket_omnibus']['package_repo']}"
Chef::Log.info "Using CHEF's public channel #{node['supermarket_omnibus']['package_repo']}"
version node['supermarket_omnibus']['package_version']
end
action [:install, :reconfigure]
end
end
end
Expand Down
58 changes: 27 additions & 31 deletions spec/unit/libraries/supermarket_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Copyright (c) 2015 Irving Popovetsky, All Rights Reserved.

require 'spec_helper'
require 'mixlib/install'

describe 'supermarket-omnibus-cookbook::default' do
context 'When all attributes are default, it should fail because of nil checks' do
Expand Down Expand Up @@ -41,14 +42,11 @@
end

it 'creates the template with the correct values' do
expect(chef_run).to create_file('/etc/supermarket/supermarket.json').with(
content: JSON.pretty_generate(chef_server_url: 'https://chefserver.mycorp.com',
chef_oauth2_app_id: 'blahblah',
chef_oauth2_secret: 'bob_lawblaw',
chef_oauth2_verify_ssl: false),
owner: 'root',
group: 'root',
mode: '0644',
expect(chef_run).to install_chef_ingredient('supermarket').with(
config: JSON.pretty_generate(chef_server_url: 'https://chefserver.mycorp.com',
chef_oauth2_app_id: 'blahblah',
chef_oauth2_secret: 'bob_lawblaw',
chef_oauth2_verify_ssl: false),
sensitive: true
)
end
Expand All @@ -57,7 +55,7 @@
context 'When additional config is provided' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(platform: 'redhat', version: '6.5', step_into: %w(chef_ingredient supermarket_server)) do |node|
node.set['supermarket_omnibus']['package_repo'] = 'chef-current'
node.set['supermarket_omnibus']['package_repo'] = 'current'
node.set['supermarket_omnibus']['chef_server_url'] = 'https://chefserver.mycorp.com'
node.set['supermarket_omnibus']['chef_oauth2_app_id'] = 'blahblah'
node.set['supermarket_omnibus']['chef_oauth2_secret'] = 'bob_lawblaw'
Expand All @@ -71,24 +69,21 @@
end

it 'creates the template with the correct values' do
expect(chef_run).to create_file('/etc/supermarket/supermarket.json').with(
content: JSON.pretty_generate(chef_oauth2_mode: 'blah',
chef_server_url: 'https://chefserver.mycorp.com',
chef_oauth2_app_id: 'blahblah',
chef_oauth2_secret: 'bob_lawblaw',
chef_oauth2_verify_ssl: false),
owner: 'root',
group: 'root',
mode: '0644',
expect(chef_run).to install_chef_ingredient('supermarket').with(
config: JSON.pretty_generate(chef_oauth2_mode: 'blah',
chef_server_url: 'https://chefserver.mycorp.com',
chef_oauth2_app_id: 'blahblah',
chef_oauth2_secret: 'bob_lawblaw',
chef_oauth2_verify_ssl: false),
sensitive: true
)
end
end

context 'When a repository chef-current is specified' do
context 'When a repository current is specified' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(platform: 'redhat', version: '6.5', step_into: %w(chef_ingredient supermarket_server)) do |node|
node.set['supermarket_omnibus']['package_repo'] = 'chef-current'
node.set['supermarket_omnibus']['package_repo'] = 'current'
node.set['supermarket_omnibus']['chef_server_url'] = 'https://chefserver.mycorp.com'
node.set['supermarket_omnibus']['chef_oauth2_app_id'] = 'blahblah'
node.set['supermarket_omnibus']['chef_oauth2_secret'] = 'bob_loblaw'
Expand All @@ -97,16 +92,21 @@
end

before do
artifact_info = instance_double('artifact info',
url: 'https://packages.chef.io/current/el/6/supermarket-300.30.3-1.el6.x86_64.rpm',
sha256: 'f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b')
installer = instance_double('installer',
artifact_info: artifact_info)
allow_any_instance_of(Chef::Provider::ChefIngredient).to receive(:installer).and_return(installer)
stub_command('grep Fauxhai /etc/hosts').and_return('33.33.33.11 Fauxhai')
end

it 'includes the yum-chef::default recipe with the chef-current repositoryid' do
expect(chef_run).to include_recipe('yum-chef::default')
expect(chef_run.node['yum-chef']['repositoryid']) == 'chef-current'
it 'tells chef-ingredient to install the supermarket package from the current channel' do
expect(chef_run).to install_chef_ingredient('supermarket').with(channel: :current)
end

it 'creates a package_repository named "chef-current"' do
expect(chef_run).to create_yum_repository('chef-current')
it 'fetches the supermarket package places it on the filesystem' do
expect(chef_run).to create_remote_file("#{Chef::Config[:file_cache_path]}/supermarket-300.30.3-1.el6.x86_64.rpm")
end

it 'converges successfully' do
Expand All @@ -117,7 +117,7 @@
context 'When a package_url is specified, packages.chef.io should not be used' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(platform: 'redhat', version: '6.5', step_into: 'supermarket_server') do |node|
node.set['supermarket_omnibus']['package_url'] = 'https://packages.chef.io/stable/el/6/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm'
node.set['supermarket_omnibus']['package_url'] = 'https://somethingelse.chef.io/stable/el/6/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm'
node.set['supermarket_omnibus']['chef_server_url'] = 'https://chefserver.mycorp.com'
node.set['supermarket_omnibus']['chef_oauth2_app_id'] = 'blahblah'
node.set['supermarket_omnibus']['chef_oauth2_secret'] = 'bob_loblaw'
Expand All @@ -138,10 +138,6 @@
.with(package_source: ::File.join(Chef::Config[:file_cache_path], 'supermarket-1.10.1-alpha.0-1.el5.x86_64.rpm'))
end

it 'does not create a package_repository named "chef-stable"' do
expect(chef_run).to_not create_yum_repository('chef-stable')
end

it 'converges successfully' do
chef_run # This should not raise an error
end
Expand All @@ -150,7 +146,7 @@
context 'When a package_url is specified, the Rpm provider should be used on RHEL systems' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(platform: 'redhat', version: '6.5', step_into: %w(chef_ingredient supermarket_server)) do |node|
node.set['supermarket_omnibus']['package_url'] = 'https://web-dl.packagecloud.io/chef/stable/packages/el/6/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm'
node.set['supermarket_omnibus']['package_url'] = 'https://somethingelse.chef.io/chef/stable/packages/el/6/supermarket-1.10.1~alpha.0-1.el5.x86_64.rpm'
node.set['supermarket_omnibus']['chef_server_url'] = 'https://chefserver.mycorp.com'
node.set['supermarket_omnibus']['chef_oauth2_app_id'] = 'blahblah'
node.set['supermarket_omnibus']['chef_oauth2_secret'] = 'bob_loblaw'
Expand Down

0 comments on commit c3d17d7

Please sign in to comment.