From 39b697ccb7d4801a7a881bcb2bde4f08ecc7c46a Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 22 Sep 2015 18:31:21 +0200 Subject: [PATCH 1/3] implement os mock in unit tests --- test/helper.rb | 164 +++++++++++++-------- test/unit/resource_audit_policy_test.rb | 2 +- test/unit/resource_auditd_conf_test.rb | 2 +- test/unit/resource_auditd_rules_test.rb | 2 +- test/unit/resource_bond_test.rb | 2 +- test/unit/resource_csv_test.rb | 2 +- test/unit/resource_gem_test.rb | 2 +- test/unit/resource_group_test.rb | 2 +- test/unit/resource_inetd_conf_test.rb | 2 +- test/unit/resource_json_test.rb | 2 +- test/unit/resource_kernel_module_test.rb | 6 +- test/unit/resource_limits_conf_test.rb | 2 +- test/unit/resource_login_def_test.rb | 2 +- test/unit/resource_mysql_conf_test.rb | 2 +- test/unit/resource_npm_test.rb | 2 +- test/unit/resource_ntp_conf_test.rb | 2 +- test/unit/resource_oneget_test.rb | 2 +- test/unit/resource_os_env_test.rb | 2 +- test/unit/resource_passwd_test.rb | 2 +- test/unit/resource_pip_test.rb | 2 +- test/unit/resource_processes_test.rb | 2 +- test/unit/resource_registry_key_test.rb | 4 +- test/unit/resource_security_policy_test.rb | 2 +- test/unit/resource_ssh_conf_test.rb | 6 +- test/unit/resource_windows_feature.rb | 2 +- test/unit/resource_yaml_test.rb | 2 +- test/unit/resource_yum_test.rb | 2 +- 27 files changed, 129 insertions(+), 97 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 61fd8e7a4..a292f69a9 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -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 diff --git a/test/unit/resource_audit_policy_test.rb b/test/unit/resource_audit_policy_test.rb index 5a2124e92..b60f6817c 100644 --- a/test/unit/resource_audit_policy_test.rb +++ b/test/unit/resource_audit_policy_test.rb @@ -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' diff --git a/test/unit/resource_auditd_conf_test.rb b/test/unit/resource_auditd_conf_test.rb index 13df111db..cb30a918e 100644 --- a/test/unit/resource_auditd_conf_test.rb +++ b/test/unit/resource_auditd_conf_test.rb @@ -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' diff --git a/test/unit/resource_auditd_rules_test.rb b/test/unit/resource_auditd_rules_test.rb index be111916e..4de98c9fa 100644 --- a/test/unit/resource_auditd_rules_test.rb +++ b/test/unit/resource_auditd_rules_test.rb @@ -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 [ diff --git a/test/unit/resource_bond_test.rb b/test/unit/resource_bond_test.rb index 043a6a494..999f7c02c 100644 --- a/test/unit/resource_bond_test.rb +++ b/test/unit/resource_bond_test.rb @@ -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 diff --git a/test/unit/resource_csv_test.rb b/test/unit/resource_csv_test.rb index 771c72f92..0152b6b85 100644 --- a/test/unit/resource_csv_test.rb +++ b/test/unit/resource_csv_test.rb @@ -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 diff --git a/test/unit/resource_gem_test.rb b/test/unit/resource_gem_test.rb index 6dd6edd48..9aaca434c 100644 --- a/test/unit/resource_gem_test.rb +++ b/test/unit/resource_gem_test.rb @@ -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 diff --git a/test/unit/resource_group_test.rb b/test/unit/resource_group_test.rb index 1eeb3bce8..3af646897 100644 --- a/test/unit/resource_group_test.rb +++ b/test/unit/resource_group_test.rb @@ -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 } diff --git a/test/unit/resource_inetd_conf_test.rb b/test/unit/resource_inetd_conf_test.rb index cd93abe4d..6a106fcff 100644 --- a/test/unit/resource_inetd_conf_test.rb +++ b/test/unit/resource_inetd_conf_test.rb @@ -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 diff --git a/test/unit/resource_json_test.rb b/test/unit/resource_json_test.rb index 8e8007cfc..19d66be22 100644 --- a/test/unit/resource_json_test.rb +++ b/test/unit/resource_json_test.rb @@ -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 diff --git a/test/unit/resource_kernel_module_test.rb b/test/unit/resource_kernel_module_test.rb index 95346fb7a..4450934aa 100644 --- a/test/unit/resource_kernel_module_test.rb +++ b/test/unit/resource_kernel_module_test.rb @@ -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 diff --git a/test/unit/resource_limits_conf_test.rb b/test/unit/resource_limits_conf_test.rb index 375851065..df2ec867e 100644 --- a/test/unit/resource_limits_conf_test.rb +++ b/test/unit/resource_limits_conf_test.rb @@ -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']] diff --git a/test/unit/resource_login_def_test.rb b/test/unit/resource_login_def_test.rb index d0623415e..6014bed72 100644 --- a/test/unit/resource_login_def_test.rb +++ b/test/unit/resource_login_def_test.rb @@ -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' diff --git a/test/unit/resource_mysql_conf_test.rb b/test/unit/resource_mysql_conf_test.rb index 11bd614e4..66181cd9b 100644 --- a/test/unit/resource_mysql_conf_test.rb +++ b/test/unit/resource_mysql_conf_test.rb @@ -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' diff --git a/test/unit/resource_npm_test.rb b/test/unit/resource_npm_test.rb index 97daa5553..835881f3d 100644 --- a/test/unit/resource_npm_test.rb +++ b/test/unit/resource_npm_test.rb @@ -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 = { diff --git a/test/unit/resource_ntp_conf_test.rb b/test/unit/resource_ntp_conf_test.rb index ef64a0b6f..0b12141a9 100644 --- a/test/unit/resource_ntp_conf_test.rb +++ b/test/unit/resource_ntp_conf_test.rb @@ -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 diff --git a/test/unit/resource_oneget_test.rb b/test/unit/resource_oneget_test.rb index 875b1dc83..90364182c 100644 --- a/test/unit/resource_oneget_test.rb +++ b/test/unit/resource_oneget_test.rb @@ -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 = { diff --git a/test/unit/resource_os_env_test.rb b/test/unit/resource_os_env_test.rb index cb5119174..c377b2cc4 100644 --- a/test/unit/resource_os_env_test.rb +++ b/test/unit/resource_os_env_test.rb @@ -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 diff --git a/test/unit/resource_passwd_test.rb b/test/unit/resource_passwd_test.rb index cb55f2478..d3df2c405 100644 --- a/test/unit/resource_passwd_test.rb +++ b/test/unit/resource_passwd_test.rb @@ -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} diff --git a/test/unit/resource_pip_test.rb b/test/unit/resource_pip_test.rb index bd8515fce..ccff80ad0 100644 --- a/test/unit/resource_pip_test.rb +++ b/test/unit/resource_pip_test.rb @@ -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 diff --git a/test/unit/resource_processes_test.rb b/test/unit/resource_processes_test.rb index 32f7beacb..452ed51e6 100644 --- a/test/unit/resource_processes_test.rb +++ b/test/unit/resource_processes_test.rb @@ -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 [{ diff --git a/test/unit/resource_registry_key_test.rb b/test/unit/resource_registry_key_test.rb index e6e0d8eb6..ceb468367 100644 --- a/test/unit/resource_registry_key_test.rb +++ b/test/unit/resource_registry_key_test.rb @@ -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 diff --git a/test/unit/resource_security_policy_test.rb b/test/unit/resource_security_policy_test.rb index 80972a2fb..ae8b41ef2 100644 --- a/test/unit/resource_security_policy_test.rb +++ b/test/unit/resource_security_policy_test.rb @@ -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 diff --git a/test/unit/resource_ssh_conf_test.rb b/test/unit/resource_ssh_conf_test.rb index 0076315e4..2b29a86f4 100644 --- a/test/unit/resource_ssh_conf_test.rb +++ b/test/unit/resource_ssh_conf_test.rb @@ -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' diff --git a/test/unit/resource_windows_feature.rb b/test/unit/resource_windows_feature.rb index 988f7a252..cc0733473 100644 --- a/test/unit/resource_windows_feature.rb +++ b/test/unit/resource_windows_feature.rb @@ -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 diff --git a/test/unit/resource_yaml_test.rb b/test/unit/resource_yaml_test.rb index a35630772..429671db4 100644 --- a/test/unit/resource_yaml_test.rb +++ b/test/unit/resource_yaml_test.rb @@ -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 diff --git a/test/unit/resource_yum_test.rb b/test/unit/resource_yum_test.rb index a5adfb9e1..d480280f3 100644 --- a/test/unit/resource_yum_test.rb +++ b/test/unit/resource_yum_test.rb @@ -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 [{ From bdb9a59264073f49a9c9889bcb43b2cb0b0e7626 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 22 Sep 2015 18:33:05 +0200 Subject: [PATCH 2/3] simplify unit tests --- test/unit/resource_audit_policy_test.rb | 6 +- test/unit/resource_auditd_conf_test.rb | 10 +- test/unit/resource_auditd_rules_test.rb | 22 ++-- test/unit/resource_bond_test.rb | 32 +++-- test/unit/resource_csv_test.rb | 11 +- test/unit/resource_gem_test.rb | 21 ++-- test/unit/resource_group_test.rb | 46 ++++--- test/unit/resource_inetd_conf_test.rb | 10 +- test/unit/resource_json_test.rb | 13 +- test/unit/resource_kernel_parameter_test.rb | 9 +- test/unit/resource_limits_conf_test.rb | 8 +- test/unit/resource_login_def_test.rb | 12 +- test/unit/resource_mysql_conf_test.rb | 10 +- test/unit/resource_npm_test.rb | 20 ++- test/unit/resource_ntp_conf_test.rb | 13 +- test/unit/resource_oneget_test.rb | 20 ++- test/unit/resource_os_env_test.rb | 7 +- test/unit/resource_passwd_test.rb | 29 ++--- test/unit/resource_pip_test.rb | 11 +- test/unit/resource_processes_test.rb | 32 +++-- test/unit/resource_registry_key_test.rb | 12 +- test/unit/resource_security_policy_test.rb | 12 +- test/unit/resource_service_test.rb | 23 ++-- test/unit/resource_yaml_test.rb | 15 +-- test/unit/resource_yum_test.rb | 130 ++++++++++---------- 25 files changed, 236 insertions(+), 298 deletions(-) diff --git a/test/unit/resource_audit_policy_test.rb b/test/unit/resource_audit_policy_test.rb index b60f6817c..256c0edf2 100644 --- a/test/unit/resource_audit_policy_test.rb +++ b/test/unit/resource_audit_policy_test.rb @@ -4,11 +4,9 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::AuditPolicy' do - describe 'audit_policy' do let(:resource) { load_resource('audit_policy') } - it 'check audit policy parsing' do - _(resource.send('User Account Management')).must_equal 'Success' - end + it 'check audit policy parsing' do + _(resource.send('User Account Management')).must_equal 'Success' end end diff --git a/test/unit/resource_auditd_conf_test.rb b/test/unit/resource_auditd_conf_test.rb index cb30a918e..dab2208f7 100644 --- a/test/unit/resource_auditd_conf_test.rb +++ b/test/unit/resource_auditd_conf_test.rb @@ -4,13 +4,11 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::AuditDaemonConf' do - describe 'audit_daemon_conf' do let(:resource) { load_resource('audit_daemon_conf') } - it 'check audit daemon config parsing' do - _(resource.space_left_action).must_equal 'SYSLOG' - _(resource.action_mail_acct).must_equal 'root' - _(resource.tcp_listen_queue).must_equal '5' - end + it 'check audit daemon config parsing' do + _(resource.space_left_action).must_equal 'SYSLOG' + _(resource.action_mail_acct).must_equal 'root' + _(resource.tcp_listen_queue).must_equal '5' end end diff --git a/test/unit/resource_auditd_rules_test.rb b/test/unit/resource_auditd_rules_test.rb index 4de98c9fa..35077cec5 100644 --- a/test/unit/resource_auditd_rules_test.rb +++ b/test/unit/resource_auditd_rules_test.rb @@ -4,19 +4,17 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::AuditDaemonRules' do - describe 'audit_daemon_rules' do let(:resource) { load_resource('audit_daemon_rules') } - it 'check audit policy parsing' do - _(resource.send('LIST_RULES')).must_equal [ - 'exit,always syscall=rmdir,unlink', - 'exit,always auid=1001 (0x3e9) syscall=open', - 'exit,always watch=/etc/group perm=wa', - 'exit,always watch=/etc/passwd perm=wa', - 'exit,always watch=/etc/shadow perm=wa', - 'exit,always watch=/etc/sudoers perm=wa', - 'exit,always watch=/etc/secret_directory perm=r' - ] - end + it 'check audit policy parsing' do + _(resource.send('LIST_RULES')).must_equal [ + 'exit,always syscall=rmdir,unlink', + 'exit,always auid=1001 (0x3e9) syscall=open', + 'exit,always watch=/etc/group perm=wa', + 'exit,always watch=/etc/passwd perm=wa', + 'exit,always watch=/etc/shadow perm=wa', + 'exit,always watch=/etc/sudoers perm=wa', + 'exit,always watch=/etc/secret_directory perm=r', + ] end end diff --git a/test/unit/resource_bond_test.rb b/test/unit/resource_bond_test.rb index 999f7c02c..0564bed21 100644 --- a/test/unit/resource_bond_test.rb +++ b/test/unit/resource_bond_test.rb @@ -6,26 +6,22 @@ require 'vulcano/resource' describe 'Vulcano::Resources::Bond' do let(:resource) { load_resource('bond', 'bond0') } - describe 'parse bond config' do + it 'bond must be available' do + resource.exist?.must_equal true + end + it 'eth0 is part of bond' do + _(resource.has_interface?('eth0')).must_equal true + _(resource.has_interface?('eth1')).must_equal false + _(resource.has_interface?('eth2')).must_equal true + end - it 'bond must be available' do - resource.exist?.must_equal true - end + it 'get all interfaces' do + _(resource.interfaces).must_equal %w{eth0 eth2} + end - it 'eth0 is part of bond' do - _(resource.has_interface?('eth0')).must_equal true - _(resource.has_interface?('eth1')).must_equal false - _(resource.has_interface?('eth2')).must_equal true - end - - it 'get all interfaces' do - _(resource.interfaces).must_equal %w{eth0 eth2} - end - - it 'get proc content' do - _(resource.content).wont_equal nil - _(resource.content).wont_equal '' - end + it 'get proc content' do + _(resource.content).wont_equal nil + _(resource.content).wont_equal '' end end diff --git a/test/unit/resource_csv_test.rb b/test/unit/resource_csv_test.rb index 0152b6b85..2633341d2 100644 --- a/test/unit/resource_csv_test.rb +++ b/test/unit/resource_csv_test.rb @@ -4,14 +4,11 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::CSV' do - describe 'cars' do let(:resource) { load_resource('csv', 'example.csv') } - - it 'verify csv parsing' do - _(resource.params).wont_equal nil - _(resource.send('1.addressable')).must_equal 'astrolabe' - _(resource.send('addressable')).must_equal %w{ast astrolabe berkshelf} - end + it 'verify csv parsing' do + _(resource.params).wont_equal nil + _(resource.send('1.addressable')).must_equal 'astrolabe' + _(resource.send('addressable')).must_equal %w{ast astrolabe berkshelf} end end diff --git a/test/unit/resource_gem_test.rb b/test/unit/resource_gem_test.rb index 9aaca434c..0941cf717 100644 --- a/test/unit/resource_gem_test.rb +++ b/test/unit/resource_gem_test.rb @@ -4,19 +4,16 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Passwd' do - describe 'gem' do - - it 'verify gem package detail parsing' do - pkg = { - name: 'rubocop', - version: '0.33.0', - type: 'gem', - installed: true, - } - _(resource.installed?).must_equal true - _(resource.info).must_equal pkg - end let(:resource) { load_resource('gem', 'rubocop') } + it 'verify gem package detail parsing' do + pkg = { + name: 'rubocop', + version: '0.33.0', + type: 'gem', + installed: true, + } + _(resource.installed?).must_equal true + _(resource.info).must_equal pkg end end diff --git a/test/unit/resource_group_test.rb b/test/unit/resource_group_test.rb index 3af646897..31d4cd3fc 100644 --- a/test/unit/resource_group_test.rb +++ b/test/unit/resource_group_test.rb @@ -4,34 +4,32 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::EtcGroup' do - describe 'etc_group' do let(:resource) { load_resource('etc_group') } - it 'verify /etc/group config parsing' do - _(resource.gids).must_equal %w{ 0 33 } - _(resource.groups).must_equal %w{ root www-data } - _(resource.users).must_equal %w{ www-data root } - end + it 'verify /etc/group config parsing' do + _(resource.gids).must_equal %w{ 0 33 } + _(resource.groups).must_equal %w{ root www-data } + _(resource.users).must_equal %w{ www-data root } + end - it 'verify group filter with no users' do - root_filter = resource.where(name: 'root') - _(root_filter.gids).must_equal ['0'] - _(root_filter.groups).must_equal ['root'] - _(root_filter.users).must_equal [] - end + it 'verify group filter with no users' do + root_filter = resource.where(name: 'root') + _(root_filter.gids).must_equal ['0'] + _(root_filter.groups).must_equal ['root'] + _(root_filter.users).must_equal [] + end - it 'verify group filter with users' do - www_filter = resource.where(name: 'www-data') - _(www_filter.gids).must_equal ['33'] - _(www_filter.groups).must_equal ['www-data'] - _(www_filter.users).must_equal ['www-data', 'root'] - end + it 'verify group filter with users' do + www_filter = resource.where(name: 'www-data') + _(www_filter.gids).must_equal ['33'] + _(www_filter.groups).must_equal ['www-data'] + _(www_filter.users).must_equal ['www-data', 'root'] + end - it 'verify group filter with wrong group' do - wrong_filter = resource.where(name: 'wrong_group') - _(wrong_filter.gids).must_equal [] - _(wrong_filter.groups).must_equal [] - _(wrong_filter.users).must_equal [] - end + it 'verify group filter with wrong group' do + wrong_filter = resource.where(name: 'wrong_group') + _(wrong_filter.gids).must_equal [] + _(wrong_filter.groups).must_equal [] + _(wrong_filter.users).must_equal [] end end diff --git a/test/unit/resource_inetd_conf_test.rb b/test/unit/resource_inetd_conf_test.rb index 6a106fcff..08aaae4de 100644 --- a/test/unit/resource_inetd_conf_test.rb +++ b/test/unit/resource_inetd_conf_test.rb @@ -4,13 +4,11 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::InetdConf' do - describe 'inetd_config' do let(:resource) { load_resource('inetd_config') } - it 'verify limits.conf config parsing' do - _(resource.send('shell')).must_equal nil - _(resource.send('login')).must_equal nil - _(resource.send('ftp')).must_equal %w{stream tcp nowait root /usr/sbin/in.ftpd in.ftpd} - end + it 'verify limits.conf config parsing' do + _(resource.send('shell')).must_equal nil + _(resource.send('login')).must_equal nil + _(resource.send('ftp')).must_equal %w{stream tcp nowait root /usr/sbin/in.ftpd in.ftpd} end end diff --git a/test/unit/resource_json_test.rb b/test/unit/resource_json_test.rb index 19d66be22..dad505710 100644 --- a/test/unit/resource_json_test.rb +++ b/test/unit/resource_json_test.rb @@ -4,15 +4,12 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::JSON' do - describe 'json' do resource = load_resource('json', 'policyfile.lock.json') - - it 'verify json parsing' do - _(resource.params).wont_equal nil - _(resource.send('name')).must_equal 'demo' - _(resource.send('run_list')).must_equal %w{apache2 omnibus} - _(resource.send('cookbook_locks.omnibus.version')).must_equal '2.2.0' - end + it 'verify json parsing' do + _(resource.params).wont_equal nil + _(resource.send('name')).must_equal 'demo' + _(resource.send('run_list')).must_equal %w{apache2 omnibus} + _(resource.send('cookbook_locks.omnibus.version')).must_equal '2.2.0' end end diff --git a/test/unit/resource_kernel_parameter_test.rb b/test/unit/resource_kernel_parameter_test.rb index 9a6462bb2..df6a9efb2 100644 --- a/test/unit/resource_kernel_parameter_test.rb +++ b/test/unit/resource_kernel_parameter_test.rb @@ -4,12 +4,9 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::KernelParameter' do - describe 'kernel_parameter' do - let(:resource) { loadResource('kernel_parameter', 'net.ipv4.conf.all.forwarding') } - - it 'verify kernel_parameter parsing' do - _(resource.value).must_equal 1 - end + let(:resource) { load_resource('kernel_parameter', 'net.ipv4.conf.all.forwarding') } + it 'verify kernel_parameter parsing' do + _(resource.value).must_equal 1 end end diff --git a/test/unit/resource_limits_conf_test.rb b/test/unit/resource_limits_conf_test.rb index df2ec867e..a5d457581 100644 --- a/test/unit/resource_limits_conf_test.rb +++ b/test/unit/resource_limits_conf_test.rb @@ -4,12 +4,10 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::LimitsConf' do - describe 'limits_conf' do let(:resource) { load_resource('limits_conf') } - it 'verify limits.conf config parsing' do - _(resource.send('*')).must_equal [['soft', 'core', '0'], ['hard', 'rss', '10000']] - _(resource.send('ftp')).must_equal [["hard", "nproc", "0"]] - end + it 'verify limits.conf config parsing' do + _(resource.send('*')).must_equal [['soft', 'core', '0'], ['hard', 'rss', '10000']] + _(resource.send('ftp')).must_equal [["hard", "nproc", "0"]] end end diff --git a/test/unit/resource_login_def_test.rb b/test/unit/resource_login_def_test.rb index 6014bed72..f09c9a048 100644 --- a/test/unit/resource_login_def_test.rb +++ b/test/unit/resource_login_def_test.rb @@ -4,14 +4,12 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::LoginDef' do - describe 'login_defs' do let(:resource) { load_resource('login_defs') } - it 'verify login.def config parsing' do - _(resource.UMASK).must_equal '022' - _(resource.PASS_MIN_DAYS).must_equal '0' - _(resource.PASS_WARN_AGE).must_equal '7' - _(resource.USERDEL_CMD).must_equal nil - end + it 'verify login.def config parsing' do + _(resource.UMASK).must_equal '022' + _(resource.PASS_MIN_DAYS).must_equal '0' + _(resource.PASS_WARN_AGE).must_equal '7' + _(resource.USERDEL_CMD).must_equal nil end end diff --git a/test/unit/resource_mysql_conf_test.rb b/test/unit/resource_mysql_conf_test.rb index 66181cd9b..a6a07139f 100644 --- a/test/unit/resource_mysql_conf_test.rb +++ b/test/unit/resource_mysql_conf_test.rb @@ -2,13 +2,11 @@ require 'helper' describe 'Vulcano::Resources::MysqlConf' do - describe 'mysql_conf' do let(:resource) { load_resource('mysql_conf', '/etc/mysql/my.cnf') } - it 'verify mysql.conf config parsing' do - _(resource.client['port']).must_equal '3306' - _(resource.mysqld['user']).must_equal 'mysql' - _(resource.mysqld['key_buffer_size']).must_equal '16M' - end + it 'verify mysql.conf config parsing' do + _(resource.client['port']).must_equal '3306' + _(resource.mysqld['user']).must_equal 'mysql' + _(resource.mysqld['key_buffer_size']).must_equal '16M' end end diff --git a/test/unit/resource_npm_test.rb b/test/unit/resource_npm_test.rb index 835881f3d..d58e91877 100644 --- a/test/unit/resource_npm_test.rb +++ b/test/unit/resource_npm_test.rb @@ -4,18 +4,16 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Passwd' do - describe 'npm' do let(:resource) { load_resource('npm', 'bower') } - it 'verify npm package detail parsing' do - pkg = { - name: 'bower', - version: '1.4.1', - type: 'npm', - installed: true, - } - _(resource.installed?).must_equal true - _(resource.info).must_equal pkg - end + it 'verify npm package detail parsing' do + pkg = { + name: 'bower', + version: '1.4.1', + type: 'npm', + installed: true, + } + _(resource.installed?).must_equal true + _(resource.info).must_equal pkg end end diff --git a/test/unit/resource_ntp_conf_test.rb b/test/unit/resource_ntp_conf_test.rb index 0b12141a9..df94a592d 100644 --- a/test/unit/resource_ntp_conf_test.rb +++ b/test/unit/resource_ntp_conf_test.rb @@ -4,15 +4,12 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::NtpConf' do - describe 'ntp_conf' do - - it 'verify ntp config parsing' do - _(resource.driftfile).must_equal '/var/lib/ntp/ntp.drift' - _(resource.server).must_equal %w{ - 0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org - } - end let(:resource) { load_resource('ntp_conf') } + it 'verify ntp config parsing' do + _(resource.driftfile).must_equal '/var/lib/ntp/ntp.drift' + _(resource.server).must_equal %w{ + 0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org + } end end diff --git a/test/unit/resource_oneget_test.rb b/test/unit/resource_oneget_test.rb index 90364182c..77d968bc0 100644 --- a/test/unit/resource_oneget_test.rb +++ b/test/unit/resource_oneget_test.rb @@ -4,18 +4,16 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Passwd' do - describe 'oneget' do let(:resource) { load_resource('oneget', 'Mozilla Firefox') } - it 'verify oneget package detail parsing' do - pkg = { - name: 'Mozilla Firefox 40.0.3 (x86 en-US)', - version: '40.0.3', - type: 'oneget', - installed: true, - } - _(resource.installed?).must_equal true - _(resource.info).must_equal pkg - end + it 'verify oneget package detail parsing' do + pkg = { + name: 'Mozilla Firefox 40.0.3 (x86 en-US)', + version: '40.0.3', + type: 'oneget', + installed: true, + } + _(resource.installed?).must_equal true + _(resource.info).must_equal pkg end end diff --git a/test/unit/resource_os_env_test.rb b/test/unit/resource_os_env_test.rb index c377b2cc4..619dd39c1 100644 --- a/test/unit/resource_os_env_test.rb +++ b/test/unit/resource_os_env_test.rb @@ -4,12 +4,9 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::OsEnv' do - describe 'os_env' do - - 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') } + it 'verify ntp config parsing' do + _(resource.split).must_equal %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin} end end diff --git a/test/unit/resource_passwd_test.rb b/test/unit/resource_passwd_test.rb index d3df2c405..85af4cc8d 100644 --- a/test/unit/resource_passwd_test.rb +++ b/test/unit/resource_passwd_test.rb @@ -4,25 +4,22 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Passwd' do - describe 'passwd' do let(:resource) { load_resource('passwd') } - it 'verify passwd parsing' do - _(resource.usernames).must_equal %w{root www-data} - _(resource.uids).must_equal %w{0 33} - end + it 'verify passwd parsing' do + _(resource.usernames).must_equal %w{root www-data} + _(resource.uids).must_equal %w{0 33} + end - it 'verify root passwd resource' do - root = resource.uid(0) - _(root.username).must_equal 'root' - _(root.count).must_equal 1 - end - - it 'verify www-data resource' do - www = resource.uid(33) - _(www.username).must_equal 'www-data' - _(www.count).must_equal 1 - end + it 'verify root passwd resource' do + root = resource.uid(0) + _(root.username).must_equal 'root' + _(root.count).must_equal 1 + end + it 'verify www-data resource' do + www = resource.uid(33) + _(www.username).must_equal 'www-data' + _(www.count).must_equal 1 end end diff --git a/test/unit/resource_pip_test.rb b/test/unit/resource_pip_test.rb index ccff80ad0..418e0fcd5 100644 --- a/test/unit/resource_pip_test.rb +++ b/test/unit/resource_pip_test.rb @@ -4,14 +4,11 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Passwd' do - describe 'pip' do - - 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') } + 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 end diff --git a/test/unit/resource_processes_test.rb b/test/unit/resource_processes_test.rb index 452ed51e6..688a06bef 100644 --- a/test/unit/resource_processes_test.rb +++ b/test/unit/resource_processes_test.rb @@ -4,25 +4,23 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Processes' do - describe 'processes' do let(:resource) { load_resource('processes', '/bin/bash') } - it 'verify processes resource' do - _(resource.list).must_equal [{ - user: 'root', - pid: '1', - cpu: '0.0', - mem: '0.0', - vsz: '18084', - rss: '3228', - tty: '?', - stat: 'Ss', - start: '14:15', - time: '0:00', - command: '/bin/bash' - }] + it 'verify processes resource' do + _(resource.list).must_equal [{ + user: 'root', + pid: '1', + cpu: '0.0', + mem: '0.0', + vsz: '18084', + rss: '3228', + tty: '?', + stat: 'Ss', + start: '14:15', + time: '0:00', + command: '/bin/bash' + }] - _(resource.list.length).must_equal 1 - end + _(resource.list.length).must_equal 1 end end diff --git a/test/unit/resource_registry_key_test.rb b/test/unit/resource_registry_key_test.rb index ceb468367..ce5e8b506 100644 --- a/test/unit/resource_registry_key_test.rb +++ b/test/unit/resource_registry_key_test.rb @@ -4,16 +4,14 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::RegistryKey' do - describe 'registry_key' do 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 - end + it 'read reg key with human readable name' do + _(resource.Start).must_equal 2 + end - it 'read reg key without human readable name' do - _(resource_without_name.Start).must_equal 2 - end + it 'read reg key without human readable name' do + _(resource_without_name.Start).must_equal 2 end end diff --git a/test/unit/resource_security_policy_test.rb b/test/unit/resource_security_policy_test.rb index ae8b41ef2..6d07c0390 100644 --- a/test/unit/resource_security_policy_test.rb +++ b/test/unit/resource_security_policy_test.rb @@ -4,14 +4,12 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::SecurityPolicy' do - describe 'security_policy' do let(:resource) { load_resource('security_policy') } - it 'verify processes resource' do - _(resource.MaximumPasswordAge).must_equal 42 - _(resource.send('MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Setup\RecoveryConsole\SecurityLevel')).must_equal '4,0' - _(resource.SeUndockPrivilege).must_equal '*S-1-5-32-544' - _(resource.SeRemoteInteractiveLogonRight).must_equal '*S-1-5-32-544,*S-1-5-32-555' - end + it 'verify processes resource' do + _(resource.MaximumPasswordAge).must_equal 42 + _(resource.send('MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Setup\RecoveryConsole\SecurityLevel')).must_equal '4,0' + _(resource.SeUndockPrivilege).must_equal '*S-1-5-32-544' + _(resource.SeRemoteInteractiveLogonRight).must_equal '*S-1-5-32-544,*S-1-5-32-555' end end diff --git a/test/unit/resource_service_test.rb b/test/unit/resource_service_test.rb index c070df8f7..1999911f7 100644 --- a/test/unit/resource_service_test.rb +++ b/test/unit/resource_service_test.rb @@ -4,18 +4,15 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::Service' do - describe 'service' do + # TODO: make OS dependent + let(:resource) { load_resource('service', 'dhcp') } - # TODO: make OS dependent - let(:resource) { loadResource('service', 'dhcp') } - - # windows - # it 'verify service parsing' do - # pkg = { name: 'dhcp', description: 'DHCP Client', installed: true, running: true, enabled: true, type: 'windows' } - # _(resource.info).must_equal pkg - # _(resource.installed?).must_equal true - # _(resource.enabled?).must_equal true - # _(resource.running?).must_equal true - # end - end + # windows + # it 'verify service parsing' do + # pkg = { name: 'dhcp', description: 'DHCP Client', installed: true, running: true, enabled: true, type: 'windows' } + # _(resource.info).must_equal pkg + # _(resource.installed?).must_equal true + # _(resource.enabled?).must_equal true + # _(resource.running?).must_equal true + # end end diff --git a/test/unit/resource_yaml_test.rb b/test/unit/resource_yaml_test.rb index 429671db4..37a816289 100644 --- a/test/unit/resource_yaml_test.rb +++ b/test/unit/resource_yaml_test.rb @@ -4,16 +4,13 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::YAML' do - describe 'yaml' do let(:resource) { load_resource('yaml', 'kitchen.yml') } - - it 'verify yaml parsing' do - _(resource.params).wont_be_nil - _(resource.send('driver.name')).must_equal 'vagrant' - _(resource.send('driver.customize.memory')).must_equal 1024 - _(resource.send('platforms.1.name')).must_equal 'centos-6.7' - _(resource.send('platforms.name')).must_equal %w{centos-5.11 centos-6.7} - end + it 'verify yaml parsing' do + _(resource.params).wont_be_nil + _(resource.send('driver.name')).must_equal 'vagrant' + _(resource.send('driver.customize.memory')).must_equal 1024 + _(resource.send('platforms.1.name')).must_equal 'centos-6.7' + _(resource.send('platforms.name')).must_equal %w{centos-5.11 centos-6.7} end end diff --git a/test/unit/resource_yum_test.rb b/test/unit/resource_yum_test.rb index d480280f3..dff243ecf 100644 --- a/test/unit/resource_yum_test.rb +++ b/test/unit/resource_yum_test.rb @@ -4,79 +4,77 @@ require 'helper' require 'vulcano/resource' describe 'Vulcano::Resources::YumRepo' do - describe 'yum' do let(:resource) { MockLoader.new(:centos7).load_resource('yum') } - it 'get repository details' do - _(resource.repositories).must_equal [{ - 'id'=>'base/7/x86_64', - 'name'=>'CentOS-7 - Base', - 'status'=>'enabled', - 'revision'=>'1427842153', - 'updated'=>'Tue Mar 31 22:50:46 2015', - 'pkgs'=>'8652', - 'size'=>'6.3 G', - 'mirrors'=>'http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock', - 'baseurl'=>'http://ftp.hosteurope.de/mirror/centos.org/7.1.1503/os/x86_64/ (9 more)', - 'expire'=>'21600 second(s) (last: Sun Sep 6 10:20:46 2015)', - 'filename'=>'/etc/yum.repos.d/CentOS-Base.repo' - }, { - 'id'=>'base-debuginfo/x86_64', - 'name'=>'CentOS-7 - Debuginfo', - 'status'=>'disabled', - 'baseurl'=>'http://debuginfo.centos.org/7/x86_64/', - 'expire'=>'21600 second(s) (last: Unknown)', - 'filename'=>'/etc/yum.repos.d/CentOS-Debuginfo.repo' - }, { - 'id'=>'extras/7/x86_64', - 'name'=>'CentOS-7 - Extras', - 'status'=>'enabled', - 'revision'=>'1441314199', - 'updated'=>'Thu Sep 3 21:03:33 2015', - 'pkgs'=>'181', - 'size'=>'742 M', - 'mirrors'=>'http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock', - 'baseurl'=>'http://mirror.informatik.hs-fulda.de/centos7.1.1503/extras/x86_64/ (9 more)', - 'expire'=>'21600 second(s) (last: Sun Sep 6 10:20:48 2015)', - 'filename'=>'/etc/yum.repos.d/CentOS-Base.repo' - }] - _(resource.repos.length).must_equal 3 - end + it 'get repository details' do + _(resource.repositories).must_equal [{ + 'id'=>'base/7/x86_64', + 'name'=>'CentOS-7 - Base', + 'status'=>'enabled', + 'revision'=>'1427842153', + 'updated'=>'Tue Mar 31 22:50:46 2015', + 'pkgs'=>'8652', + 'size'=>'6.3 G', + 'mirrors'=>'http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock', + 'baseurl'=>'http://ftp.hosteurope.de/mirror/centos.org/7.1.1503/os/x86_64/ (9 more)', + 'expire'=>'21600 second(s) (last: Sun Sep 6 10:20:46 2015)', + 'filename'=>'/etc/yum.repos.d/CentOS-Base.repo', + }, { + 'id'=>'base-debuginfo/x86_64', + 'name'=>'CentOS-7 - Debuginfo', + 'status'=>'disabled', + 'baseurl'=>'http://debuginfo.centos.org/7/x86_64/', + 'expire'=>'21600 second(s) (last: Unknown)', + 'filename'=>'/etc/yum.repos.d/CentOS-Debuginfo.repo', + }, { + 'id'=>'extras/7/x86_64', + 'name'=>'CentOS-7 - Extras', + 'status'=>'enabled', + 'revision'=>'1441314199', + 'updated'=>'Thu Sep 3 21:03:33 2015', + 'pkgs'=>'181', + 'size'=>'742 M', + 'mirrors'=>'http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock', + 'baseurl'=>'http://mirror.informatik.hs-fulda.de/centos7.1.1503/extras/x86_64/ (9 more)', + 'expire'=>'21600 second(s) (last: Sun Sep 6 10:20:48 2015)', + 'filename'=>'/etc/yum.repos.d/CentOS-Base.repo', + }] + _(resource.repos.length).must_equal 3 + end - it 'get repository details' do - _(resource.repos).must_equal %w{base/7/x86_64 base-debuginfo/x86_64 extras/7/x86_64} - end + it 'get repository details' do + _(resource.repos).must_equal %w{base/7/x86_64 base-debuginfo/x86_64 extras/7/x86_64} + end - # its('epel') { should exist } - # its('epel') { should be_enabled } - it 'test its syntax repo' do - _(resource.extras.exist?).must_equal true - _(resource.extras.enabled?).must_equal true - end + # its('epel') { should exist } + # its('epel') { should be_enabled } + it 'test its syntax repo' do + _(resource.extras.exist?).must_equal true + _(resource.extras.enabled?).must_equal true + end - it 'test enabled extra repo' do - extras = resource.repo('extras/7/x86_64') - _(extras.exist?).must_equal true - _(extras.enabled?).must_equal true - end + it 'test enabled extra repo' do + extras = resource.repo('extras/7/x86_64') + _(extras.exist?).must_equal true + _(extras.enabled?).must_equal true + end - it 'test enabled extra repo with short name' do - extras = resource.repo('extras') - _(extras.exist?).must_equal true - _(extras.enabled?).must_equal true - end + it 'test enabled extra repo with short name' do + extras = resource.repo('extras') + _(extras.exist?).must_equal true + _(extras.enabled?).must_equal true + end - it 'test disabled extra-source repo' do - extras = resource.repo('base-debuginfo/x86_64') - _(extras.exist?).must_equal true - _(extras.enabled?).must_equal false - end + it 'test disabled extra-source repo' do + extras = resource.repo('base-debuginfo/x86_64') + _(extras.exist?).must_equal true + _(extras.enabled?).must_equal false + end - # test serverspec syntax - let(:serverspec) { loadResource('yumrepo', 'extras') } - it 'test enabled extra repo (serverspec backwards comptability)' do - _(serverspec.exists?).must_equal true - _(serverspec.enabled?).must_equal true - end + # test serverspec syntax + let(:serverspec) { load_resource('yumrepo', 'extras') } + it 'test enabled extra repo (serverspec backwards comptability)' do + _(serverspec.exists?).must_equal true + _(serverspec.enabled?).must_equal true end end From 48f0615d17932ff562945ed431cb30c0fa423793 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 22 Sep 2015 19:35:38 +0200 Subject: [PATCH 3/3] simplify os default setting --- test/helper.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index a292f69a9..83d9ff312 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -34,11 +34,7 @@ class MockLoader } # selects operating system - if !os.nil? - @os = @operating_systems[os] - else - @os = @operating_systems[:ubuntu1404] - end + @os = @operating_systems[os || :ubuntu1404] end # loads a resource class and instantiates the class with the given arguments