Update inspec for os[:family] change in Train

Signed-off-by: Steven Danna <steve@chef.io>
This commit is contained in:
Steven Danna 2016-08-03 18:18:24 +01:00
parent b16ea5f89f
commit 57d7275857
No known key found for this signature in database
GPG key ID: 94DFB46E861A7DAE
19 changed files with 105 additions and 95 deletions

View file

@ -8,6 +8,13 @@ if Gem::Version.new(RUBY_VERSION) <= Gem::Version.new('1.9.3')
gem 'net-ssh', '~> 2.9'
end
#
# This has to be merged someone concurrently with a a version bump in
# train to avoid breaking users.
#
gem 'train', github: 'chef/train'
# TODO: ffi 1.9.11 is currently erroneous on windows tests
gem 'ffi', '= 1.9.10'

View file

@ -82,7 +82,7 @@ module Inspec
end
if !profile.metadata.supports_transport?(@backend)
os_info = @backend.os[:family].to_s
os_info = @backend.os[:name].to_s
fail "This OS/platform (#{os_info}) is not supported by this profile."
end

View file

@ -10,8 +10,7 @@ module Inspec::Resources
attr_reader :service, :conf_dir, :conf_path, :user
def initialize
case inspec.os[:family]
when 'ubuntu', 'debian'
if inspec.os.debian?
@service = 'apache2'
@conf_dir = '/etc/apache2/'
@conf_path = File.join @conf_dir, 'apache2.conf'

View file

@ -80,7 +80,7 @@ module Inspec::Resources
if @content =~ /^LIST_RULES:/
# do not warn on centos 5
unless inspec.os[:family] == 'centos' && inspec.os[:release].to_i == 5
unless inspec.os[:name] == 'centos' && inspec.os[:release].to_i == 5
warn '[WARN] this version of auditd is outdated. Updating it allows for using more precise matchers.'
end
@legacy = AuditdRulesLegacy.new(@content)

View file

@ -45,7 +45,7 @@ module Inspec::Resources
def exist?
# silent for mock resources
return false if inspec.os[:family].to_s == 'unknown'
return false if inspec.os[:name].to_s == 'unknown'
if inspec.os.linux?
res = inspec.backend.run_command("bash -c 'type \"#{@command}\"'")
@ -54,7 +54,7 @@ module Inspec::Resources
elsif inspec.os.unix?
res = inspec.backend.run_command("type \"#{@command}\"")
else
warn "`command(#{@command}).exist?` is not suported on your OS: #{inspec.os[:family]}"
warn "`command(#{@command}).exist?` is not suported on your OS: #{inspec.os[:name]}"
return false
end
res.exit_status.to_i == 0

View file

@ -21,28 +21,40 @@ class GrubConfig < Inspec.resource(1) # rubocop:disable Metrics/ClassLength
end
"
class UnknownGrubConfig < StandardError; end
def initialize(path = nil, kernel = nil)
family = inspec.os[:family]
case family
when 'redhat', 'fedora', 'centos'
release = inspec.os[:release].to_f
supported = true
if release < 7
@conf_path = path || '/etc/grub.conf'
@version = 'legacy'
else
@conf_path = path || '/boot/grub/grub.cfg'
@defaults_path = '/etc/default/grub'
@version = 'grub2'
end
when 'ubuntu'
config_for_platform(path)
@kernel = kernel || 'default'
rescue UnknownGrubConfig
return skip_resource 'The `grub_config` resource is not supported on your OS yet.'
end
def config_for_platform(path)
os = inspec.os
if os.redhat? || os[:name] == 'fedora'
config_for_redhatish(path)
elsif os.debian?
@conf_path = path || '/boot/grub/grub.cfg'
@defaults_path = '/etc/default/grub'
@version = 'grub2'
elsif os[:name] == 'amazon' # rubocop:disable Style/GuardClause
@conf_path = path || '/etc/grub.conf'
@version = 'legacy'
else
fail UnknownGrubConfig
end
end
def config_for_redhatish(path)
if inspec.os[:release].to_f < 7
@conf_path = path || '/etc/grub.conf'
@version = 'legacy'
else
@conf_path = path || '/boot/grub/grub.cfg'
@defaults_path = '/etc/default/grub'
@version = 'grub2'
supported = true
end
@kernel = kernel || 'default'
return skip_resource 'The `grub_config` resource is not supported on your OS yet.' if supported.nil?
end
def method_missing(name)

View file

@ -24,7 +24,7 @@ module Inspec::Resources
# default lsmod command
lsmod_cmd = 'lsmod'
# special care for CentOS 5 and sudo
lsmod_cmd = '/sbin/lsmod' if inspec.os[:family] == 'centos' && inspec.os[:release].to_i == 5
lsmod_cmd = '/sbin/lsmod' if inspec.os[:name] == 'centos' && inspec.os[:release].to_i == 5
# get list of all modules
cmd = inspec.command(lsmod_cmd)

View file

@ -12,7 +12,7 @@ module Inspec::Resources
def initialize
# set OS-dependent filenames and paths
case inspec.os[:family]
when 'ubuntu', 'debian'
when 'debian'
init_ubuntu
when 'redhat', 'fedora'
init_redhat

View file

@ -23,7 +23,7 @@ module Inspec::Resources
@package_name = package_name
# verify that this resource is only supported on Windows
return skip_resource 'The `oneget` resource is not supported on your OS.' if inspec.os[:family] != 'windows'
return skip_resource 'The `oneget` resource is not supported on your OS.' if !inspec.os.windows?
end
def info

View file

@ -59,7 +59,7 @@ module Inspec::Resources
out = inspec.command(command)
unless out.exit_status == 0
skip_resource "Can't read environment variables on #{os[:family]}. "\
skip_resource "Can't read environment variables on #{os[:name]}. "\
"Tried `#{command}` which returned #{out.exit_status}"
end

View file

@ -29,7 +29,7 @@ module Inspec::Resources
os = inspec.os
if os.debian?
@pkgman = Deb.new(inspec)
elsif os.redhat? || os.suse?
elsif %w{redhat suse amazon fedora}.include?(os[:family])
@pkgman = Rpm.new(inspec)
elsif ['arch'].include?(os[:family])
@pkgman = Pacman.new(inspec)

View file

@ -57,9 +57,7 @@ module Inspec::Resources
def pip_cmd
# Pip is not on the default path for Windows, therefore we do some logic
# to find the binary on Windows
family = inspec.os[:family]
case family
when 'windows'
if inspec.os.windows?
# we need to detect the pip command on Windows
cmd = inspec.command('New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Pip -Value (Invoke-Command -ScriptBlock {where.exe pip}) -PassThru | Add-Member -MemberType NoteProperty -Name Python -Value (Invoke-Command -ScriptBlock {where.exe python}) -PassThru | ConvertTo-Json')
begin

View file

@ -10,33 +10,27 @@ module Inspec::Resources
attr_reader :service, :data_dir, :conf_dir, :conf_path
def initialize
case inspec.os[:family]
when 'ubuntu', 'debian'
os = inspec.os
if os.debian?
@service = 'postgresql'
@data_dir = '/var/lib/postgresql'
@version = inspec.command('ls /etc/postgresql/').stdout.chomp
@conf_dir = "/etc/postgresql/#{@version}/main"
@conf_path = File.join @conf_dir, 'postgresql.conf'
when 'arch'
@service = 'postgresql'
@data_dir = '/var/lib/postgres/data'
@conf_dir = '/var/lib/postgres/data'
@conf_path = File.join @conf_dir, 'postgresql.conf'
when 'centos', 'redhat'
elsif os.redhat?
@service = 'postgresql'
@version = inspec.command('ls /var/lib/pgsql/').stdout.chomp
@data_dir = "/var/lib/pgsql/#{@version}/data"
@conf_dir = "/var/lib/pgsql/#{@version}/data"
@conf_path = File.join @conf_dir, 'postgresql.conf'
elsif os[:name] == 'arch'
@service = 'postgresql'
@data_dir = '/var/lib/postgres/data'
@conf_dir = '/var/lib/postgres/data'
else
@service = 'postgresql'
@data_dir = '/var/lib/postgresql'
@conf_dir = '/var/lib/pgsql/data'
@conf_path = File.join @conf_dir, 'postgresql.conf'
end
@conf_path = File.join @conf_dir, 'postgresql.conf'
end
def to_s

View file

@ -102,7 +102,7 @@ module Inspec::Resources
def select_service_mgmt # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
os = inspec.os
family = os[:family]
platform = os[:name]
# Ubuntu
# @see: https://wiki.ubuntu.com/SystemdForUpstartUsers
@ -112,46 +112,46 @@ module Inspec::Resources
# Ubuntu < 15.04 : Upstart
# Upstart runs with PID 1 as /sbin/init.
# Systemd runs with PID 1 as /lib/systemd/systemd.
if %w{ubuntu}.include?(family)
version = inspec.os[:release].to_f
if %w{ubuntu}.include?(platform)
version = os[:release].to_f
if version < 15.04
Upstart.new(inspec, service_ctl)
else
Systemd.new(inspec, service_ctl)
end
elsif %w{debian}.include?(family)
version = inspec.os[:release].to_i
elsif %w{debian}.include?(platform)
version = os[:release].to_i
if version > 7
Systemd.new(inspec, service_ctl)
else
SysV.new(inspec, service_ctl || '/usr/sbin/service')
end
elsif %w{redhat fedora centos}.include?(family)
version = inspec.os[:release].to_i
if (%w{ redhat centos }.include?(family) && version >= 7) || (family == 'fedora' && version >= 15)
elsif %w{redhat fedora centos}.include?(platform)
version = os[:release].to_i
if (%w{ redhat centos }.include?(platform) && version >= 7) || (platform == 'fedora' && version >= 15)
Systemd.new(inspec, service_ctl)
else
SysV.new(inspec, service_ctl || '/sbin/service')
end
elsif %w{wrlinux}.include?(family)
elsif %w{wrlinux}.include?(platform)
SysV.new(inspec, service_ctl)
elsif %w{darwin}.include?(family)
elsif %w{mac_os_x}.include?(platform)
LaunchCtl.new(inspec, service_ctl)
elsif os.windows?
WindowsSrv.new(inspec)
elsif %w{freebsd}.include?(family)
elsif %w{freebsd}.include?(platform)
BSDInit.new(inspec, service_ctl)
elsif %w{arch}.include?(family)
elsif %w{arch}.include?(platform)
Systemd.new(inspec, service_ctl)
elsif %w{suse opensuse}.include?(family)
if inspec.os[:release].to_i >= 12
elsif %w{suse opensuse}.include?(platform)
if os[:release].to_i >= 12
Systemd.new(inspec, service_ctl)
else
SysV.new(inspec, service_ctl || '/sbin/service')
end
elsif %w{aix}.include?(family)
elsif %w{aix}.include?(platform)
SrcMstr.new(inspec)
elsif %w{amazon}.include?(family)
elsif %w{amazon}.include?(platform)
Upstart.new(inspec, service_ctl)
elsif os.solaris?
Svcs.new(inspec)
@ -359,7 +359,7 @@ module Inspec::Resources
enabled = !config[/^\s*start on/].nil?
# implement fallback for Ubuntu 10.04
if inspec.os[:family] == 'ubuntu' &&
if inspec.os[:name] == 'ubuntu' &&
inspec.os[:release].to_f >= 10.04 &&
inspec.os[:release].to_f < 12.04 &&
status.exit_status == 0

View file

@ -42,7 +42,7 @@ module Inspec::Resources
@cache = nil
# verify that this resource is only supported on Windows
return skip_resource 'The `windows_feature` resource is not supported on your OS.' if inspec.os[:family] != 'windows'
return skip_resource 'The `windows_feature` resource is not supported on your OS.' if !inspec.os.windows?
end
# returns true if the package is installed

View file

@ -38,25 +38,25 @@ TMP_CACHE = {}
class MockLoader
# collects emulation operating systems
OPERATING_SYSTEMS = {
arch: { family: 'arch', release: nil, arch: nil },
centos5: { family: 'redhat', release: '5.11', arch: 'x86_64' },
centos6: { family: 'redhat', release: '6.6', arch: 'x86_64' },
centos7: { family: 'redhat', release: '7.1.1503', arch: 'x86_64' },
debian6: { family: 'debian', release: '6', arch: 'x86_64' },
debian7: { family: 'debian', release: '7', arch: 'x86_64' },
debian8: { family: 'debian', release: '8', arch: 'x86_64' },
freebsd9: { family: 'freebsd', release: '9', arch: 'amd64' },
freebsd10: { family: 'freebsd', release: '10', arch: 'amd64' },
osx104: { family: 'darwin', release: '10.10.4', arch: nil, name: 'mac_os_x' },
ubuntu1204: { family: 'ubuntu', release: '12.04', arch: 'x86_64' },
ubuntu1404: { family: 'ubuntu', release: '14.04', arch: 'x86_64' },
ubuntu1504: { family: 'ubuntu', release: '15.04', arch: 'x86_64' },
windows: { family: 'windows', release: '6.2.9200', arch: 'x86_64' },
wrlinux: { family: 'wrlinux', release: '7.0(3)I2(2)', arch: 'x86_64' },
solaris11: { family: "solaris", release: '11', arch: 'i386'},
solaris10: { family: "solaris", release: '10', arch: 'i386'},
hpux: {family: 'hpux', release: 'B.11.31', arch: 'ia64'},
undefined: { family: nil, release: nil, arch: nil },
arch: { name: 'arch', family: 'arch', release: nil, arch: nil },
centos5: { name: 'centos', family: 'redhat', release: '5.11', arch: 'x86_64' },
centos6: { name: 'centos', family: 'redhat', release: '6.6', arch: 'x86_64' },
centos7: { name: 'centos', family: 'redhat', release: '7.1.1503', arch: 'x86_64' },
debian6: { name: 'debian', family: 'debian', release: '6', arch: 'x86_64' },
debian7: { name: 'debian', family: 'debian', release: '7', arch: 'x86_64' },
debian8: { name: 'debian', family: 'debian', release: '8', arch: 'x86_64' },
freebsd9: { name: 'freebsd', family: 'freebsd', release: '9', arch: 'amd64' },
freebsd10: { name: 'freebsd', family: 'freebsd', release: '10', arch: 'amd64' },
osx104: { name: 'mac_os_x',family: 'darwin', release: '10.10.4', arch: nil },
ubuntu1204: { name: 'ubuntu', family: 'debian', release: '12.04', arch: 'x86_64' },
ubuntu1404: { name: 'ubuntu', family: 'debian', release: '14.04', arch: 'x86_64' },
ubuntu1504: { name: 'ubuntu', family: 'debian', release: '15.04', arch: 'x86_64' },
windows: { name: 'windows', family: 'windows', release: '6.2.9200', arch: 'x86_64' },
wrlinux: { name: 'wrlinux', family: 'redhat', release: '7.0(3)I2(2)', arch: 'x86_64' },
solaris11: { name: "solaris", family: 'solaris', release: '11', arch: 'i386'},
solaris10: { name: "solaris", family: 'solaris', release: '10', arch: 'i386'},
hpux: { name: 'hpux', family: 'hpux', release: 'B.11.31', arch: 'ia64'},
undefined: { name: nil, family: nil, release: nil, arch: nil },
}
# pass the os identifier to emulate a specific operating system

View file

@ -1,6 +1,6 @@
# encoding: utf-8
if os[:family] == 'ubuntu'
if os[:name] == 'ubuntu'
describe apt('ppa:nginx/stable') do
it { should exist }

View file

@ -77,7 +77,7 @@ describe Inspec::ProfileContext do
include DescribeOneTest
it 'must provide os resource' do
load('print os[:family]').must_output 'ubuntu'
load('print os[:family]').must_output 'debian'
end
it 'must provide file resource' do
@ -215,7 +215,7 @@ describe Inspec::ProfileContext do
describe 'adds a check via describe' do
let(:check) {
profile.load(format(context_format,
"describe os[:family] { it { must_equal 'ubuntu' } }"
"describe os[:family] { it { must_equal 'debian' } }"
))
get_checks[0]
}
@ -225,7 +225,7 @@ describe Inspec::ProfileContext do
end
it 'registers the check with the describe argument' do
check[1].must_equal %w{ubuntu}
check[1].must_equal %w{debian}
end
it 'registers the check with the provided proc' do
@ -236,7 +236,7 @@ describe Inspec::ProfileContext do
describe 'adds a check via expect' do
let(:check) {
profile.load(format(context_format,
"expect(os[:family]).to eq('ubuntu')"
"expect(os[:family]).to eq('debian')"
))
get_checks[0]
}
@ -246,7 +246,7 @@ describe Inspec::ProfileContext do
end
it 'registers the check with the describe argument' do
check[1].must_equal %w{ubuntu}
check[1].must_equal %w{debian}
end
it 'registers the check with the provided proc' do
@ -258,7 +258,7 @@ describe Inspec::ProfileContext do
let(:check) {
profile.load(format(context_format,
"describe 'the actual test' do
expect(os[:family]).to eq('ubuntu')
expect(os[:family]).to eq('debian')
end"
))
get_checks[0]

View file

@ -8,7 +8,7 @@ require 'inspec/resource'
describe 'Inspec::Resources::Os' do
it 'verify os parsing on CentOS' do
resource = MockLoader.new(:centos7).load_resource('os')
_(resource.name).must_equal nil
_(resource.name).must_equal 'centos'
_(resource.family).must_equal 'redhat'
_(resource.release).must_equal '7.1.1503'
_(resource.arch).must_equal 'x86_64'
@ -16,7 +16,7 @@ describe 'Inspec::Resources::Os' do
it 'read env variable on Windows' do
resource = MockLoader.new(:windows).load_resource('os')
_(resource.name).must_equal nil
_(resource.name).must_equal 'windows'
_(resource.family).must_equal 'windows'
_(resource.release).must_equal '6.2.9200'
_(resource.arch).must_equal 'x86_64'
@ -24,7 +24,7 @@ describe 'Inspec::Resources::Os' do
it 'verify os parsing on Debian' do
resource = MockLoader.new(:debian8).load_resource('os')
_(resource.name).must_equal nil
_(resource.name).must_equal 'debian'
_(resource.family).must_equal 'debian'
_(resource.release).must_equal '8'
_(resource.arch).must_equal 'x86_64'
@ -32,8 +32,8 @@ describe 'Inspec::Resources::Os' do
it 'verify os parsing on Ubuntu' do
resource = MockLoader.new(:ubuntu1504).load_resource('os')
_(resource.name).must_equal nil
_(resource.family).must_equal 'ubuntu'
_(resource.name).must_equal 'ubuntu'
_(resource.family).must_equal 'debian'
_(resource.release).must_equal '15.04'
_(resource.arch).must_equal 'x86_64'
end