From c348a2032a749753f825bf8af36470a6b0f39d87 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 11 Jan 2019 20:25:58 -0800 Subject: [PATCH 01/13] Remove test that uses deprecated `ppa` resource Signed-off-by: Jerry Aldrich --- test/unit/resources/apt_test.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/unit/resources/apt_test.rb b/test/unit/resources/apt_test.rb index 28c7524de..92025b6ca 100644 --- a/test/unit/resources/apt_test.rb +++ b/test/unit/resources/apt_test.rb @@ -54,11 +54,4 @@ describe 'Inspec::Resources::AptRepo' do _(resource.exists?).must_equal false _(resource.enabled?).must_equal false end - - # check ppa resource - it 'check apt on ubuntu' do - resource = MockLoader.new(:ubuntu1504).load_resource('ppa', 'ubuntu-wine/ppa') - _(resource.exists?).must_equal true - _(resource.enabled?).must_equal true - end end From b440e3f1320b32a9d3ab6418f1038509feb1a429 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 11 Jan 2019 21:09:47 -0800 Subject: [PATCH 02/13] Change `DLS` to `DSL` Signed-off-by: Jerry Aldrich --- test/unit/profiles/profile_context_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/profiles/profile_context_test.rb b/test/unit/profiles/profile_context_test.rb index 458fea22a..1df88077d 100644 --- a/test/unit/profiles/profile_context_test.rb +++ b/test/unit/profiles/profile_context_test.rb @@ -110,7 +110,7 @@ describe Inspec::ProfileContext do end end - it 'does not provide the expect keyword in the global DLS' do + it 'does not provide the expect keyword in the global DSL' do load('expect(true).to_eq true').must_raise NoMethodError end From bc56b2275a929a5124c73fdb19685fe64ee27cdb Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 11 Jan 2019 21:16:18 -0800 Subject: [PATCH 03/13] Use mocked file instead of an empty file call Signed-off-by: Jerry Aldrich --- test/unit/profiles/profile_context_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/profiles/profile_context_test.rb b/test/unit/profiles/profile_context_test.rb index 1df88077d..587cd186e 100644 --- a/test/unit/profiles/profile_context_test.rb +++ b/test/unit/profiles/profile_context_test.rb @@ -78,7 +78,7 @@ describe Inspec::ProfileContext do end it 'must provide file resource' do - load('print file("").type').must_output 'unknown' + load('print file("/etc/passwd").type').must_output 'file' end it 'must provide command resource' do From 2f20370426f8cf93cab3a28ee5e6c85b8247663d Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 11 Jan 2019 21:22:30 -0800 Subject: [PATCH 04/13] Change `.must_equal nil` to `.must_be_nil` `.must_equal nil` is deprecated Signed-off-by: Jerry Aldrich --- test/unit/fetchers/url_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/fetchers/url_test.rb b/test/unit/fetchers/url_test.rb index 90194d784..d809b4868 100644 --- a/test/unit/fetchers/url_test.rb +++ b/test/unit/fetchers/url_test.rb @@ -204,7 +204,7 @@ describe Fetchers::Url do describe 'when only password is specified' do let(:config) { { :password => 'dummy'} } it 'returns a hash containing http_basic_authentication setting as nil' do - subject.send(:http_opts)[:http_basic_authentication].must_equal nil + subject.send(:http_opts)[:http_basic_authentication].must_be_nil end end From a0b76defb01e38d5bba6a25ae5bb51add6799fbf Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Fri, 11 Jan 2019 21:35:05 -0800 Subject: [PATCH 05/13] Evaluate `runner.run` in a proc This removes the following line from the test output: ``` Test Summary: 0 successful, 0 failures, 0 skipped ``` Signed-off-by: Jerry Aldrich --- test/unit/runner_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/runner_test.rb b/test/unit/runner_test.rb index 2047c1bc2..f3386fe05 100644 --- a/test/unit/runner_test.rb +++ b/test/unit/runner_test.rb @@ -38,7 +38,7 @@ describe Inspec::Runner do describe 'testing runner.run exit codes' do it 'returns proper exit code when no profile is added' do - runner.run.must_equal 0 + proc { runner.run.must_equal 0 } end end From c1547ee89858945b8dabc415144d8d7743c65d25 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 14:06:37 -0800 Subject: [PATCH 06/13] Remove deprecated methods from shadow test Signed-off-by: Jerry Aldrich --- test/unit/resources/shadow_test.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/unit/resources/shadow_test.rb b/test/unit/resources/shadow_test.rb index 53c145cbe..be90e5e94 100644 --- a/test/unit/resources/shadow_test.rb +++ b/test/unit/resources/shadow_test.rb @@ -11,16 +11,16 @@ describe 'Inspec::Resources::Shadow' do end it 'retrieve users via field' do - _(shadow.user).must_equal %w{root www-data} + _(shadow.users).must_equal %w{root www-data} _(shadow.count).must_equal 2 end it 'retrieve passwords via field' do - _(shadow.password).must_equal %w{x !!} + _(shadow.passwords).must_equal %w{x !!} end it 'retrieve last password change via field' do - _(shadow.last_change).must_equal %w{1 10} + _(shadow.last_changes).must_equal %w{1 10} end it 'retrieve min password days via field' do @@ -40,7 +40,7 @@ describe 'Inspec::Resources::Shadow' do end it 'retrieve dates when account will expire via field' do - _(shadow.expiry_date).must_equal [nil, "60"] + _(shadow.expiry_dates).must_equal [nil, "60"] end it 'access all lines of the file' do @@ -82,8 +82,8 @@ describe 'Inspec::Resources::Shadow' do describe 'multiple filters' do it 'filters with min_days and max_days' do - _(shadow.filter(min_days: 20, max_days: 30).user).must_equal ['www-data'] - _(shadow.filter(last_change: 1, min_days: 2).user).must_equal ['root'] + _(shadow.filter(min_days: 20, max_days: 30).users).must_equal ['www-data'] + _(shadow.filter(last_change: 1, min_days: 2).users).must_equal ['root'] end end From 5ea640a2cdc0b8770e7155947ce874492591455a Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 14:09:13 -0800 Subject: [PATCH 07/13] Remove nil check in chocolatey_package test Signed-off-by: Jerry Aldrich --- test/unit/resources/chocolatey_package_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/resources/chocolatey_package_test.rb b/test/unit/resources/chocolatey_package_test.rb index 5f2661cc6..2d97796e5 100644 --- a/test/unit/resources/chocolatey_package_test.rb +++ b/test/unit/resources/chocolatey_package_test.rb @@ -13,7 +13,7 @@ describe 'Inspec::Resources::ChocoPkg' do pkg = { name: 'git', installed: false, version: nil, type: 'chocolatey' } resource = MockLoader.new(:windows).load_resource('chocolatey_package', 'git') _(resource.installed?).must_equal pkg[:installed] - _(resource.version).must_equal pkg[:version] + _(resource.version).must_be_nil _(resource.info).must_equal pkg end From 3fe70912545c57102bf102d1895575747139c1ab Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 15:26:35 -0800 Subject: [PATCH 08/13] Wrap deprecated ServerSpec matchers in procs Signed-off-by: Jerry Aldrich --- test/unit/resources/user_test.rb | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/unit/resources/user_test.rb b/test/unit/resources/user_test.rb index c413faed3..1a33fb7ef 100644 --- a/test/unit/resources/user_test.rb +++ b/test/unit/resources/user_test.rb @@ -34,14 +34,22 @@ describe 'Inspec::Resources::User' do end # serverspec compatibility tests (do not test matcher) - it 'verify serverspec compatibility' do + it 'returns deprecation notices' do resource = MockLoader.new(:ubuntu1404).load_resource('user', 'root') - _(resource.has_uid?(0)).must_equal true - _(resource.has_home_directory?('/root')).must_equal true - _(resource.has_login_shell?('/bin/bash')).must_equal true - _(resource.minimum_days_between_password_change).must_equal 0 - _(resource.maximum_days_between_password_change).must_equal 99999 - # _(resource.has_authorized_key?('abc')).must_equal true + proc { resource.has_uid?(0).must_equal true } + .must_output nil, "[DEPRECATION] has_uid? is deprecated. \n" + proc { resource.has_home_directory?('/root').must_equal true } + .must_output nil, "[DEPRECATION] has_home_directory? is deprecated. Please use: its('home')\n" + proc { resource.has_login_shell?('/bin/bash').must_equal true } + .must_output nil, "[DEPRECATION] has_login_shell? is deprecated. Please use: its('shell')\n" + proc { resource.minimum_days_between_password_change.must_equal 0 } + .must_output nil, "[DEPRECATION] minimum_days_between_password_change is deprecated. Please use: its('mindays')\n" + proc { resource.maximum_days_between_password_change.must_equal 99999 } + .must_output nil, "[DEPRECATION] maximum_days_between_password_change is deprecated. Please use: its('maxdays')\n" + + assert_output(nil, "[DEPRECATION] has_authorized_key? is deprecated. \n") do + proc { resource.has_authorized_key?('abc') }.must_raise NotImplementedError + end end it 'read user on centos7' do From 4c0a58253980c42ae4b1f1a3e79de21602a11b46 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 15:30:04 -0800 Subject: [PATCH 09/13] Mock mysql related command Signed-off-by: Jerry Aldrich --- test/helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helper.rb b/test/helper.rb index 3b965d39e..35ba1d351 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -508,7 +508,7 @@ class MockLoader '/sbin/service sshd status' => empty.call, 'service apache2 status' => cmd_exit_1.call, 'type "lsof"' => empty.call, - + 'test -f /etc/mysql/debian.cnf && cat /etc/mysql/debian.cnf' => empty.call, # http resource - remote worker' %{bash -c 'type "curl"'} => cmd.call('bash-c-type-curl'), "curl -i -X GET --connect-timeout 60 --max-time 120 'http://www.example.com'" => cmd.call('http-remote-no-options'), From 62cfcf0ebe22621ebd29a23b054a0cb64ee2c828 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 15:39:35 -0800 Subject: [PATCH 10/13] Add mock command for oracledb_session Signed-off-by: Jerry Aldrich --- test/helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/helper.rb b/test/helper.rb index 35ba1d351..f4185488a 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -229,6 +229,7 @@ class MockLoader 'bash -c \'type "Rscript"\'' => empty.call, 'bash -c \'type "perl"\'' => empty.call, 'bash -c \'type "/sbin/auditctl"\'' => empty.call, + 'bash -c \'type "sql"\'' => cmd_exit_1.call, 'type "pwsh"' => empty.call, 'type "netstat"' => empty.call, 'sh -c \'find /etc/apache2/ports.conf -type l -maxdepth 1\'' => empty.call, From 3b8da7957d6795305c4e0c05539c40b6b91fcb6d Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 15:44:36 -0800 Subject: [PATCH 11/13] Mock `missing_file` in NGINX resource tests Signed-off-by: Jerry Aldrich --- test/helper.rb | 1 + test/unit/resources/nginx_conf_test.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/helper.rb b/test/helper.rb index f4185488a..5aba661c9 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -198,6 +198,7 @@ class MockLoader '/fakepath/fakefile' => emptyfile.call, 'C:/fakepath/fakefile' => emptyfile.call, '/etc/cron.d/crondotd' => mockfile.call('crondotd'), + '/missing_file' => emptyfile.call, } # create all mock commands diff --git a/test/unit/resources/nginx_conf_test.rb b/test/unit/resources/nginx_conf_test.rb index cab7a74db..32196370f 100644 --- a/test/unit/resources/nginx_conf_test.rb +++ b/test/unit/resources/nginx_conf_test.rb @@ -13,7 +13,7 @@ describe 'Inspec::Resources::NginxConf' do let(:nginx_conf) { MockLoader.new(:ubuntu1404).load_resource('nginx_conf') } it 'doesnt fail with a missing file' do - nginx_conf = MockLoader.new(:ubuntu1404).load_resource('nginx_conf', '/....missing_file') + nginx_conf = MockLoader.new(:ubuntu1404).load_resource('nginx_conf', '/missing_file') _(nginx_conf.params).must_equal({}) end From f7e8a0c80c9b55e540c7de70e5d4b633655912a8 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 15:50:20 -0800 Subject: [PATCH 12/13] Add deprecation check for `processes.list` Signed-off-by: Jerry Aldrich --- test/unit/resources/processes_test.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/resources/processes_test.rb b/test/unit/resources/processes_test.rb index a5e0c8303..91cafa475 100644 --- a/test/unit/resources/processes_test.rb +++ b/test/unit/resources/processes_test.rb @@ -13,7 +13,10 @@ describe 'Inspec::Resources::Processes' do it 'verify processes resource' do resource = MockLoader.new(:freebsd10).load_resource('processes', 'login -fp apop') - _(resource.list.length).must_equal 2 # until we deprecate + + proc { resource.list.length.must_equal 2 } + .must_output nil, "[DEPRECATION] `processes.list` is deprecated. Please use `processes.entries` instead. It will be removed in version 4.0.\n" + _(resource.entries.length).must_equal 2 _(resource.entries[0].to_h).must_equal({ label: nil, From e80e7d872e06bfb2a2be10fc95f77e4376873f45 Mon Sep 17 00:00:00 2001 From: Jerry Aldrich Date: Sat, 12 Jan 2019 21:48:42 -0800 Subject: [PATCH 13/13] Mock files used for virtualization resource tests Signed-off-by: Jerry Aldrich --- test/unit/resources/virtualization_test.rb | 40 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/test/unit/resources/virtualization_test.rb b/test/unit/resources/virtualization_test.rb index e0003a05e..2a27f8794 100644 --- a/test/unit/resources/virtualization_test.rb +++ b/test/unit/resources/virtualization_test.rb @@ -4,15 +4,45 @@ require 'helper' require 'inspec/resource' describe 'Inspec::Resources::Virtualization' do - let(:resource) { MockLoader.new(:ubuntu).load_resource('virtualization') } + def mock_proc(mocked_files) + proc do |filename| + OpenStruct.new( + :exist? => mocked_files.include?(filename) ? false : true + ) + end + end it 'fails the resource if OS is not Linux' do - resource = MockLoader.new(:windows).load_resource('virtualization') - resource.resource_failed?.must_equal true + mocked_files = [] + mock_loader = MockLoader.new(:windows) + mock_loader.backend.stub :file, mock_proc(mocked_files) do + mock_resource = mock_loader.load_resource('virtualization') + mock_resource.resource_failed?.must_equal true + end end it 'returns nil for all properties if no virutalization platform is found' do - resource.system.must_be_nil - resource.role.must_be_nil + mocked_files = [ + '/proc/xen/capabilities', + '/proc/modules', + '/proc/cpuinfo', + '/sys/devices/virtual/misc/kvm', + '/proc/bc/0', + '/proc/vz', + '/proc/bus/pci/devices', + '/proc/self/status', + '/proc/self/cgroup', + '/.dockerenv', + '/.dockerinit', + '/dev/lxd/sock', + '/var/lib/lxd/devlxd', + ] + + mock_loader = MockLoader.new(:ubuntu) + mock_loader.backend.stub :file, mock_proc(mocked_files) do + mock_resource = mock_loader.load_resource('virtualization') + mock_resource.system.must_be_nil + mock_resource.role.must_be_nil + end end end