mirror of
https://github.com/inspec/inspec
synced 2024-11-11 07:34:15 +00:00
Merge pull request #1596 from nvwls/gem-binary
Extend `gem` to take an optional `gem_binary`
This commit is contained in:
commit
3f32dbe2d0
8 changed files with 102 additions and 4 deletions
|
@ -10,13 +10,14 @@ Use the `gem` InSpec audit resource to test if a global Gem package is installed
|
|||
|
||||
A `gem` resource block declares a package and (optionally) a package version:
|
||||
|
||||
describe gem('gem_package_name') do
|
||||
describe gem('gem_package_name', 'gem_binary') do
|
||||
it { should be_installed }
|
||||
end
|
||||
|
||||
where
|
||||
|
||||
* `('gem_package_name')` must specify a Gem package, such as `'rubocop'`
|
||||
* `('gem_binary')` can specify the path to a non-default gem binary, defaults to `'gem'`
|
||||
* `be_installed` is a valid matcher for this resource
|
||||
|
||||
## Matchers
|
||||
|
@ -71,3 +72,21 @@ The following examples show how to use this InSpec audit resource.
|
|||
describe gem('rubocop') do
|
||||
it { should_not be_installed }
|
||||
end
|
||||
|
||||
### Verify that a gem package is installed in an omnibus environment
|
||||
|
||||
describe gem('pry', '/opt/ruby-2.3.1/embedded/bin/gem') do
|
||||
it { should be_installed }
|
||||
end
|
||||
|
||||
### Verify that a gem package is installed in a chef omnibus environment
|
||||
|
||||
describe gem('chef-sugar', :chef) do
|
||||
it { should be_installed }
|
||||
end
|
||||
|
||||
### Verify that a gem package is installed in a chef-server omnibus environment
|
||||
|
||||
describe gem('knife-backup', :chef_server) do
|
||||
it { should be_installed }
|
||||
end
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
# encoding: utf-8
|
||||
# author: Christoph Hartmann
|
||||
# author: Dominik Richter
|
||||
# author: Joe Nuspl
|
||||
|
||||
module Inspec::Resources
|
||||
class GemPackage < Inspec.resource(1)
|
||||
|
@ -13,16 +14,32 @@ module Inspec::Resources
|
|||
end
|
||||
"
|
||||
|
||||
def initialize(package_name)
|
||||
attr_reader :gem_binary
|
||||
|
||||
def initialize(package_name, gem_binary = nil)
|
||||
@package_name = package_name
|
||||
@gem_binary = case gem_binary
|
||||
when nil
|
||||
'gem'
|
||||
when :chef
|
||||
if inspec.os.windows?
|
||||
'c:\opscode\chef\embedded\bin\gem'
|
||||
else
|
||||
'/opt/chef/embedded/bin/gem'
|
||||
end
|
||||
when :chef_server
|
||||
'/opt/opscode/embedded/bin/gem'
|
||||
else
|
||||
gem_binary
|
||||
end
|
||||
end
|
||||
|
||||
def info
|
||||
return @info if defined?(@info)
|
||||
|
||||
cmd = inspec.command("gem list --local -a -q \^#{@package_name}\$")
|
||||
cmd = inspec.command("#{@gem_binary} list --local -a -q \^#{@package_name}\$")
|
||||
@info = {
|
||||
installed: cmd.exit_status == 0,
|
||||
installed: cmd.exit_status.zero?,
|
||||
type: 'gem',
|
||||
}
|
||||
return @info unless @info[:installed]
|
||||
|
|
|
@ -170,6 +170,10 @@ class MockLoader
|
|||
'pacman -Qi curl' => cmd.call('pacman-qi-curl'),
|
||||
'brew info --json=v1 curl' => cmd.call('brew-info--json-v1-curl'),
|
||||
'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'),
|
||||
'/opt/ruby-2.3.1/embedded/bin/gem list --local -a -q ^pry$' => cmd.call('gem-list-local-a-q-pry'),
|
||||
'/opt/chef/embedded/bin/gem list --local -a -q ^chef-sugar$' => cmd.call('gem-list-local-a-q-chef-sugar'),
|
||||
'c:\opscode\chef\embedded\bin\gem list --local -a -q ^json$' => cmd.call('gem-list-local-a-q-json'),
|
||||
'/opt/opscode/embedded/bin/gem list --local -a -q ^knife-backup$' => cmd.call('gem-list-local-a-q-knife-backup'),
|
||||
'npm ls -g --json bower' => cmd.call('npm-ls-g--json-bower'),
|
||||
'pip show jinja2' => cmd.call('pip-show-jinja2'),
|
||||
"Get-Package -Name 'Mozilla Firefox' | ConvertTo-Json" => cmd.call('get-package-firefox'),
|
||||
|
|
1
test/unit/mock/cmd/gem-list-local-a-q-chef-sugar
Normal file
1
test/unit/mock/cmd/gem-list-local-a-q-chef-sugar
Normal file
|
@ -0,0 +1 @@
|
|||
chef-sugar (3.4.0)
|
1
test/unit/mock/cmd/gem-list-local-a-q-json
Normal file
1
test/unit/mock/cmd/gem-list-local-a-q-json
Normal file
|
@ -0,0 +1 @@
|
|||
json (1.8.3)
|
1
test/unit/mock/cmd/gem-list-local-a-q-knife-backup
Normal file
1
test/unit/mock/cmd/gem-list-local-a-q-knife-backup
Normal file
|
@ -0,0 +1 @@
|
|||
knife-backup (0.0.12)
|
1
test/unit/mock/cmd/gem-list-local-a-q-pry
Normal file
1
test/unit/mock/cmd/gem-list-local-a-q-pry
Normal file
|
@ -0,0 +1 @@
|
|||
pry (0.10.4)
|
|
@ -1,6 +1,7 @@
|
|||
# encoding: utf-8
|
||||
# author: Christoph Hartmann
|
||||
# author: Dominik Richter
|
||||
# author: Joe Nuspl
|
||||
|
||||
require 'helper'
|
||||
require 'inspec/resource'
|
||||
|
@ -16,5 +17,58 @@ describe 'Inspec::Resources::Gem' do
|
|||
}
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.info).must_equal pkg
|
||||
_(resource.gem_binary).must_equal 'gem'
|
||||
end
|
||||
|
||||
it 'specifying gem binary' do
|
||||
resource = load_resource('gem', 'pry', '/opt/ruby-2.3.1/embedded/bin/gem')
|
||||
pkg = {
|
||||
name: 'pry',
|
||||
version: '0.10.4',
|
||||
type: 'gem',
|
||||
installed: true,
|
||||
}
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.info).must_equal pkg
|
||||
_(resource.gem_binary).must_equal '/opt/ruby-2.3.1/embedded/bin/gem'
|
||||
end
|
||||
|
||||
it 'verify gem in :chef' do
|
||||
resource = load_resource('gem', 'chef-sugar', :chef)
|
||||
pkg = {
|
||||
name: 'chef-sugar',
|
||||
version: '3.4.0',
|
||||
type: 'gem',
|
||||
installed: true,
|
||||
}
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.info).must_equal pkg
|
||||
_(resource.gem_binary).must_equal '/opt/chef/embedded/bin/gem'
|
||||
end
|
||||
|
||||
it 'verify gem in :chef on windows' do
|
||||
resource = MockLoader.new(:windows).load_resource('gem', 'json', :chef)
|
||||
pkg = {
|
||||
name: 'json',
|
||||
version: '1.8.3',
|
||||
type: 'gem',
|
||||
installed: true,
|
||||
}
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.info).must_equal pkg
|
||||
_(resource.gem_binary).must_equal 'c:\opscode\chef\embedded\bin\gem'
|
||||
end
|
||||
|
||||
it 'verify gem in :chef_server' do
|
||||
resource = load_resource('gem', 'knife-backup', :chef_server)
|
||||
pkg = {
|
||||
name: 'knife-backup',
|
||||
version: '0.0.12',
|
||||
type: 'gem',
|
||||
installed: true,
|
||||
}
|
||||
_(resource.installed?).must_equal true
|
||||
_(resource.info).must_equal pkg
|
||||
_(resource.gem_binary).must_equal '/opt/opscode/embedded/bin/gem'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue