implement os mock in unit tests

This commit is contained in:
Christoph Hartmann 2015-09-22 18:31:21 +02:00
parent 098e84132f
commit 39b697ccb7
27 changed files with 129 additions and 97 deletions

View file

@ -14,75 +14,109 @@ end
require 'vulcano/backend'
require 'vulcano/resource'
# loads a resource class and instantiates the class with the given arguments
def loadResource (resource, *args)
scriptpath = ::File.realpath(::File.dirname(__FILE__))
class MockLoader
# pass the os identifier to emulate a specific operating system
def initialize(os = nil)
# collects emulation operating systems
@operating_systems = {
windows: { family: 'windows', release: nil, arch: nil },
debian6: { family: 'debian', release: '6', arch: 'x86_64' },
debian7: { family: 'debian', release: '7', arch: 'x86_64' },
debian8: { family: 'debian', release: '8', arch: 'x86_64' },
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' },
ubuntu1204: { family: 'ubuntu', release: '12.04', arch: 'x86_64' },
ubuntu1404: { family: 'ubuntu', release: '14.04', arch: 'x86_64' },
osx104: { family: 'darwin', release: '10.10.4', arch: nil, name: 'mac_os_x' },
freebsd9: { family: 'freebsd', release: '9', arch: 'amd64' },
freebsd10: { family: 'freebsd', release: '10', arch: 'amd64' },
}
# create mock backend
conf = Vulcano::Backend.target_config({
quiet: true
})
backend_class = Vulcano::Backend.registry['mock']
@backend = backend_class.new(conf)
# selects operating system
if !os.nil?
@os = @operating_systems[os]
else
@os = @operating_systems[:ubuntu1404]
end
end
# create all mock files
local = Vulcano::Backend.registry['local'].new({})
mockfile = lambda { |x|
path = ::File.join(scriptpath, '/unit/mock/files', x)
local.file(path)
}
@backend.files = {
'/proc/net/bonding/bond0' => mockfile.('bond0'),
'/etc/ssh/ssh_config' => mockfile.('ssh_config'),
'/etc/ssh/sshd_config' => mockfile.('sshd_config'),
'/etc/passwd' => mockfile.('passwd'),
'/etc/ntp.conf' => mockfile.('ntp.conf'),
'/etc/login.defs' => mockfile.('login.defs'),
'/etc/security/limits.conf' => mockfile.('limits.conf'),
'/etc/inetd.conf' => mockfile.('inetd.conf'),
'/etc/group' => mockfile.('group'),
'/etc/audit/auditd.conf' => mockfile.('auditd.conf'),
'/etc/mysql/my.cnf' => mockfile.('mysql.conf'),
'/etc/mysql/mysql2.conf' => mockfile.('mysql2.conf'),
'kitchen.yml' => mockfile.('kitchen.yml'),
'example.csv' => mockfile.('example.csv'),
'policyfile.lock.json' => mockfile.('policyfile.lock.json'),
}
# loads a resource class and instantiates the class with the given arguments
def load_resource(resource, *args)
scriptpath = ::File.realpath(::File.dirname(__FILE__))
# create all mock commands
cmd = lambda {|x|
stdout = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x))
@backend.mock_command( stdout, '', 0 )
}
@backend.commands = {
'ps aux' => cmd.call('ps-aux'),
'type win_secpol.cfg' => cmd.call('secedit-export'),
'secedit /export /cfg win_secpol.cfg' => cmd.call('success'),
'del win_secpol.cfg' => cmd.call('success'),
'su - root -c \'echo $PATH\'' => cmd.call('PATH'),
'(Get-Item \'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule\').GetValue(\'Start\')' => cmd.call('reg_schedule'),
'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'),
'/sbin/auditctl -l' => cmd.call('auditctl'),
'yum -v repolist all' => cmd.call('yum-repolist-all'),
'dpkg -s curl' => cmd.call('dpkg-s-curl'),
'rpm -qia curl' => cmd.call('rpm-qia-curl'),
'pacman -Qi curl' => cmd.call('packman-qi-curl'),
'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'),
'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'),
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
'lsmod' => cmd.call('lsmod'),
'/sbin/sysctl -q -n net.ipv4.conf.all.forwarding' => cmd.call('sbin_sysctl'),
}
# create mock backend
conf = Vulcano::Backend.target_config({ quiet: true })
backend_class = Vulcano::Backend.registry['mock']
@backend = backend_class.new(conf)
# load resource
@rclass = Vulcano::Resource.registry[resource]
# create all mock files
local = Vulcano::Backend.registry['local'].new({})
mockfile = lambda { |x|
path = ::File.join(scriptpath, '/unit/mock/files', x)
local.file(path)
}
@backend.files = {
'/proc/net/bonding/bond0' => mockfile.call('bond0'),
'/etc/ssh/ssh_config' => mockfile.call('ssh_config'),
'/etc/ssh/sshd_config' => mockfile.call('sshd_config'),
'/etc/passwd' => mockfile.call('passwd'),
'/etc/ntp.conf' => mockfile.call('ntp.conf'),
'/etc/login.defs' => mockfile.call('login.defs'),
'/etc/security/limits.conf' => mockfile.call('limits.conf'),
'/etc/inetd.conf' => mockfile.call('inetd.conf'),
'/etc/group' => mockfile.call('group'),
'/etc/audit/auditd.conf' => mockfile.call('auditd.conf'),
'/etc/mysql/my.cnf' => mockfile.call('mysql.conf'),
'/etc/mysql/mysql2.conf' => mockfile.call('mysql2.conf'),
'kitchen.yml' => mockfile.call('kitchen.yml'),
'example.csv' => mockfile.call('example.csv'),
'policyfile.lock.json' => mockfile.call('policyfile.lock.json'),
}
# merge arguments
args = [@backend] | args
# create all mock commands
cmd = lambda {|x|
stdout = ::File.read(::File.join(scriptpath, '/unit/mock/cmd/'+x))
@backend.mock_command(stdout, '', 0)
}
@backend.commands = {
'ps aux' => cmd.call('ps-aux'),
'type win_secpol.cfg' => cmd.call('secedit-export'),
'secedit /export /cfg win_secpol.cfg' => cmd.call('success'),
'del win_secpol.cfg' => cmd.call('success'),
'su - root -c \'echo $PATH\'' => cmd.call('PATH'),
'(Get-Item \'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule\').GetValue(\'Start\')' => cmd.call('reg_schedule'),
'Auditpol /get /subcategory:\'User Account Management\' /r' => cmd.call('auditpol'),
'/sbin/auditctl -l' => cmd.call('auditctl'),
'yum -v repolist all' => cmd.call('yum-repolist-all'),
'dpkg -s curl' => cmd.call('dpkg-s-curl'),
'rpm -qia curl' => cmd.call('rpm-qia-curl'),
'pacman -Qi curl' => cmd.call('packman-qi-curl'),
'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'),
'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'),
"New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name dhcp| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json" => cmd.call('get-service-dhcp'),
"Get-WindowsFeature | Where-Object {$_.Name -eq 'dhcp' -or $_.DisplayName -eq 'dhcp'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" => cmd.call('get-windows-feature'),
'lsmod' => cmd.call('lsmod'),
'/sbin/sysctl -q -n net.ipv4.conf.all.forwarding' => cmd.call('sbin_sysctl'),
}
# initialize resource with backend and parameters
@resource = @rclass.new(*args)
# set os emulation
@backend.os = @os
# load resource
@rclass = Vulcano::Resource.registry[resource]
# merge arguments
args = [@backend] | args
# initialize resource with backend and parameters
@resource = @rclass.new(*args)
end
end
def load_resource(*args)
m = MockLoader.new(:ubuntu1404)
m.send('load_resource', *args)
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::AuditPolicy' do
describe 'audit_policy' do
let(:resource) { loadResource('audit_policy') }
let(:resource) { load_resource('audit_policy') }
it 'check audit policy parsing' do
_(resource.send('User Account Management')).must_equal 'Success'

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::AuditDaemonConf' do
describe 'audit_daemon_conf' do
let(:resource) { loadResource('audit_daemon_conf') }
let(:resource) { load_resource('audit_daemon_conf') }
it 'check audit daemon config parsing' do
_(resource.space_left_action).must_equal 'SYSLOG'

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::AuditDaemonRules' do
describe 'audit_daemon_rules' do
let(:resource) { loadResource('audit_daemon_rules') }
let(:resource) { load_resource('audit_daemon_rules') }
it 'check audit policy parsing' do
_(resource.send('LIST_RULES')).must_equal [

View file

@ -4,10 +4,10 @@ require 'helper'
require 'vulcano/resource'
describe 'Vulcano::Resources::Bond' do
let(:resource) { load_resource('bond', 'bond0') }
describe 'parse bond config' do
let(:resource) { loadResource('bond', 'bond0') }
it 'bond must be available' do
resource.exist?.must_equal true

View file

@ -5,8 +5,8 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::CSV' do
describe 'cars' do
let(:resource) { load_resource('csv', 'example.csv') }
let(:resource) { loadResource('csv', 'example.csv') }
it 'verify csv parsing' do
_(resource.params).wont_equal nil

View file

@ -5,7 +5,6 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
describe 'gem' do
let(:resource) { loadResource('gem', 'rubocop') }
it 'verify gem package detail parsing' do
pkg = {
@ -17,6 +16,7 @@ describe 'Vulcano::Resources::Passwd' do
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end
let(:resource) { load_resource('gem', 'rubocop') }
end
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::EtcGroup' do
describe 'etc_group' do
let(:resource) { loadResource('etc_group') }
let(:resource) { load_resource('etc_group') }
it 'verify /etc/group config parsing' do
_(resource.gids).must_equal %w{ 0 33 }

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::InetdConf' do
describe 'inetd_config' do
let(:resource) { loadResource('inetd_config') }
let(:resource) { load_resource('inetd_config') }
it 'verify limits.conf config parsing' do
_(resource.send('shell')).must_equal nil

View file

@ -5,8 +5,8 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::JSON' do
describe 'json' do
resource = load_resource('json', 'policyfile.lock.json')
let(:resource) { loadResource('json', 'policyfile.lock.json') }
it 'verify json parsing' do
_(resource.params).wont_equal nil

View file

@ -5,17 +5,17 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::KernelModule' do
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'bridge')
resource = load_resource('kernel_module', 'bridge')
_(resource.loaded?).must_equal true
end
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'bridges')
resource = load_resource('kernel_module', 'bridges')
_(resource.loaded?).must_equal false
end
it 'verify kernel_module parsing' do
resource = loadResource('kernel_module', 'dhcp')
resource = load_resource('kernel_module', 'dhcp')
_(resource.loaded?).must_equal false
end
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::LimitsConf' do
describe 'limits_conf' do
let(:resource) { loadResource('limits_conf') }
let(:resource) { load_resource('limits_conf') }
it 'verify limits.conf config parsing' do
_(resource.send('*')).must_equal [['soft', 'core', '0'], ['hard', 'rss', '10000']]

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::LoginDef' do
describe 'login_defs' do
let(:resource) { loadResource('login_defs') }
let(:resource) { load_resource('login_defs') }
it 'verify login.def config parsing' do
_(resource.UMASK).must_equal '022'

View file

@ -3,7 +3,7 @@ require 'helper'
describe 'Vulcano::Resources::MysqlConf' do
describe 'mysql_conf' do
let(:resource) { loadResource('mysql_conf', '/etc/mysql/my.cnf') }
let(:resource) { load_resource('mysql_conf', '/etc/mysql/my.cnf') }
it 'verify mysql.conf config parsing' do
_(resource.client['port']).must_equal '3306'

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
describe 'npm' do
let(:resource) { loadResource('npm', 'bower') }
let(:resource) { load_resource('npm', 'bower') }
it 'verify npm package detail parsing' do
pkg = {

View file

@ -5,7 +5,6 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::NtpConf' do
describe 'ntp_conf' do
let(:resource) { loadResource('ntp_conf') }
it 'verify ntp config parsing' do
_(resource.driftfile).must_equal '/var/lib/ntp/ntp.drift'
@ -13,6 +12,7 @@ describe 'Vulcano::Resources::NtpConf' do
0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org
}
end
let(:resource) { load_resource('ntp_conf') }
end
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
describe 'oneget' do
let(:resource) { loadResource('oneget', 'Mozilla Firefox') }
let(:resource) { load_resource('oneget', 'Mozilla Firefox') }
it 'verify oneget package detail parsing' do
pkg = {

View file

@ -5,11 +5,11 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::OsEnv' do
describe 'os_env' do
let(:resource) { loadResource('os_env', 'PATH') }
it 'verify ntp config parsing' do
_(resource.split).must_equal %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}
end
let(:resource) { load_resource('os_env', 'PATH') }
end
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
describe 'passwd' do
let(:resource) { loadResource('passwd') }
let(:resource) { load_resource('passwd') }
it 'verify passwd parsing' do
_(resource.usernames).must_equal %w{root www-data}

View file

@ -5,13 +5,13 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Passwd' do
describe 'pip' do
let(:resource) { loadResource('pip', 'jinja2') }
it 'verify pip package detail parsing' do
pkg = {:name=>"Jinja2", :installed=>true, :version=>"2.8", :type=>"pip"}
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end
let(:resource) { load_resource('pip', 'jinja2') }
end
end

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::Processes' do
describe 'processes' do
let(:resource) { loadResource('processes', '/bin/bash') }
let(:resource) { load_resource('processes', '/bin/bash') }
it 'verify processes resource' do
_(resource.list).must_equal [{

View file

@ -5,8 +5,8 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::RegistryKey' do
describe 'registry_key' do
let(:resource) { loadResource('registry_key', 'Task Scheduler', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule') }
let(:resource_without_name) { loadResource('registry_key', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule') }
let(:resource) { load_resource('registry_key', 'Task Scheduler', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule') }
let(:resource_without_name) { load_resource('registry_key', 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule') }
it 'read reg key with human readable name' do
_(resource.Start).must_equal 2

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::SecurityPolicy' do
describe 'security_policy' do
let(:resource) { loadResource('security_policy') }
let(:resource) { load_resource('security_policy') }
it 'verify processes resource' do
_(resource.MaximumPasswordAge).must_equal 42

View file

@ -6,8 +6,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::SshConf' do
describe 'ssh_config' do
let(:resource) { loadResource('ssh_config') }
let(:resource) { load_resource('ssh_config') }
it 'check ssh config parsing' do
_(resource.Host).must_equal '*'
@ -19,8 +18,7 @@ describe 'Vulcano::Resources::SshConf' do
end
describe 'sshd_config' do
let(:resource) { loadResource('sshd_config') }
let(:resource) { load_resource('sshd_config') }
it 'check protocol version' do
_(resource.Port).must_equal '22'

View file

@ -6,7 +6,7 @@ require 'vulcano/resource'
describe 'Vulcano:Resources::Feature' do
describe 'feature' do
let(:resource) { loadResource('windows_feature', 'dhcp') }
let(:resource) { MockLoader.new(:windows).load_resource('windows_feature', 'dhcp') }
# TODO: set windows as mock os
it 'verify windows feature parsing' do

View file

@ -5,8 +5,8 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::YAML' do
describe 'yaml' do
let(:resource) { load_resource('yaml', 'kitchen.yml') }
let(:resource) { loadResource('yaml', 'kitchen.yml') }
it 'verify yaml parsing' do
_(resource.params).wont_be_nil

View file

@ -5,7 +5,7 @@ require 'vulcano/resource'
describe 'Vulcano::Resources::YumRepo' do
describe 'yum' do
let(:resource) { loadResource('yum') }
let(:resource) { MockLoader.new(:centos7).load_resource('yum') }
it 'get repository details' do
_(resource.repositories).must_equal [{