From 51ebf2d8d5c02c7cd314edd9e6507163a8073785 Mon Sep 17 00:00:00 2001 From: Gabriel Nagy Date: Thu, 11 Mar 2021 14:17:09 +0200 Subject: (maint) Switch to rspec-mocks --- .rubocop.yml | 2 + spec/integration/provider/cron/crontab_spec.rb | 16 ++++---- spec/lib/puppet_spec/compiler.rb | 4 +- spec/lib/puppet_spec/files.rb | 2 +- spec/spec_helper.rb | 1 + spec/unit/provider/cron/crontab_spec.rb | 38 +++++++++---------- spec/unit/provider/cron/filetype_spec.rb | 52 ++++++++++++-------------- spec/unit/provider/cron/parsed_spec.rb | 45 +++++++++------------- spec/unit/type/cron_spec.rb | 6 +-- 9 files changed, 77 insertions(+), 89 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 3500164..b410b2b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -77,6 +77,8 @@ Style/SymbolArray: EnforcedStyle: brackets RSpec/NamedSubject: Enabled: false +RSpec/SubjectStub: + Enabled: false RSpec/MessageSpies: EnforcedStyle: receive Style/Documentation: diff --git a/spec/integration/provider/cron/crontab_spec.rb b/spec/integration/provider/cron/crontab_spec.rb index 989eae6..a419774 100644 --- a/spec/integration/provider/cron/crontab_spec.rb +++ b/spec/integration/provider/cron/crontab_spec.rb @@ -7,17 +7,17 @@ describe Puppet::Type.type(:cron).provider(:crontab), unless: Puppet.features.mi include PuppetSpec::Compiler before :each do - Puppet::Type.type(:cron).stubs(:defaultprovider).returns described_class - described_class.stubs(:suitable?).returns true - Puppet::FileBucket::Dipper.any_instance.stubs(:backup) # rubocop:disable RSpec/AnyInstance + allow(Puppet::Type.type(:cron)).to receive(:defaultprovider).and_return described_class + allow(described_class).to receive(:suitable?).and_return true + allow_any_instance_of(Puppet::FileBucket::Dipper).to receive(:backup) # rubocop:disable RSpec/AnyInstance # I don't want to execute anything - described_class.stubs(:filetype).returns Puppet::Util::FileType::FileTypeFlat - described_class.stubs(:default_target).returns crontab_user1 + allow(described_class).to receive(:filetype).and_return Puppet::Util::FileType::FileTypeFlat + allow(described_class).to receive(:default_target).and_return crontab_user1 # I don't want to stub Time.now to get a static header because I don't know # where Time.now is used elsewhere, so just go with a very simple header - described_class.stubs(:header).returns "# HEADER: some simple\n# HEADER: header\n" + allow(described_class).to receive(:header).and_return "# HEADER: some simple\n# HEADER: header\n" FileUtils.cp(my_fixture('crontab_user1'), crontab_user1) FileUtils.cp(my_fixture('crontab_user2'), crontab_user2) end @@ -113,9 +113,9 @@ describe Puppet::Type.type(:cron).provider(:crontab), unless: Puppet.features.mi MANIFEST apply_compiled_manifest(manifest) do |res| if res.ref == 'Cron[Entirely new resource]' - res.expects(:err).with(regexp_matches(%r{no command})) + expect(res).to receive(:err).with(%r{no command}) else - res.expects(:err).never + expect(res).not_to receive(:err) end end end diff --git a/spec/lib/puppet_spec/compiler.rb b/spec/lib/puppet_spec/compiler.rb index fb04a7b..44274bf 100644 --- a/spec/lib/puppet_spec/compiler.rb +++ b/spec/lib/puppet_spec/compiler.rb @@ -33,7 +33,7 @@ module PuppetSpec::Compiler if Puppet.version.to_f < 5.0 args << 'apply' # rubocop:disable RSpec/AnyInstance - Puppet::Transaction::Persistence.any_instance.stubs(:save) + allow_any_instance_of(Puppet::Transaction::Persistence).to receive(:save) # rubocop:enable RSpec/AnyInstance end catalog = compile_to_ral(manifest) @@ -51,7 +51,7 @@ module PuppetSpec::Compiler def apply_with_error_check(manifest) apply_compiled_manifest(manifest) do |res| - res.expects(:err).never + expect(res).not_to receive(:err) end end diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb index c8315c9..af0e936 100644 --- a/spec/lib/puppet_spec/files.rb +++ b/spec/lib/puppet_spec/files.rb @@ -10,7 +10,7 @@ module PuppetSpec::Files $global_tempfiles ||= [] $global_tempfiles.each do |path| begin - Dir.unstub(:entries) + allow(Dir).to receive(:entries).and_call_original FileUtils.rm_rf path, secure: true rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions # nothing to do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index feb5720..29615cd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,6 +31,7 @@ default_facts.each do |fact, value| end RSpec.configure do |c| + c.mock_with :rspec c.default_facts = default_facts c.before :each do # set to strictest setting for testing diff --git a/spec/unit/provider/cron/crontab_spec.rb b/spec/unit/provider/cron/crontab_spec.rb index 796a4c2..6e28393 100644 --- a/spec/unit/provider/cron/crontab_spec.rb +++ b/spec/unit/provider/cron/crontab_spec.rb @@ -125,7 +125,7 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:resources) { { 'test' => resource } } before :each do - subject.stubs(:prefetch_all_targets).returns([record]) + allow(subject).to receive(:prefetch_all_targets).and_return([record]) end # this would be a more fitting test, but I haven't yet @@ -141,17 +141,17 @@ describe Puppet::Type.type(:cron).provider(:crontab) do # end it "does not base the new resource's provider on the existing record" do - subject.expects(:new).with(record).never - subject.stubs(:new) + expect(subject).not_to receive(:new).with(record) + allow(subject).to receive(:new) subject.prefetch(resources) end end context 'when prefetching an entry now managed for another user' do let(:resource) do - s = stub(:resource) - s.stubs(:[]).with(:user).returns 'root' - s.stubs(:[]).with(:target).returns 'root' + s = instance_double('Puppet::Type::Crontab') + allow(s).to receive(:[]).with(:user).and_return 'root' + allow(s).to receive(:[]).with(:target).and_return 'root' s end @@ -159,16 +159,16 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:resources) { { 'test' => resource } } before :each do - subject.stubs(:prefetch_all_targets).returns([record]) + allow(subject).to receive(:prefetch_all_targets).and_return([record]) end it 'tries and use the match method to find a more fitting record' do - subject.expects(:match).with(record, resources) + expect(subject).to receive(:match).with(record, resources) subject.prefetch(resources) end it 'does not match a provider to the resource' do - resource.expects(:provider=).never + expect(resource).not_to receive(:provider=) subject.prefetch(resources) end @@ -205,19 +205,17 @@ describe Puppet::Type.type(:cron).provider(:crontab) do context '#enumerate_crontabs' do before(:each) do - File.expects(:readable?).with(subject.crontab_dir).returns(true) - Dir.expects(:foreach).with(subject.crontab_dir).multiple_yields(*files) + allow(File).to receive(:readable?).with(subject.crontab_dir).and_return(true) end context 'only a hidden file' do - let(:files) { ['.keep_cronbase-0'] } + let(:file) { '.keep_cronbase-0' } before(:each) do - files.each do |filename| - path = File.join(subject.crontab_dir, filename) - File.expects(:file?).with(path).returns(true) - File.expects(:writable?).with(path).returns(true) - end + path = File.join(subject.crontab_dir, file) + allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(file) + allow(File).to receive(:file?).with(path).and_return(true) + allow(File).to receive(:writable?).with(path).and_return(true) end it 'ignores .keep_* files' do @@ -229,10 +227,12 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:files) { ['myuser', '.keep_cronbase-0'] } before(:each) do + allow(Dir).to receive(:foreach).with(subject.crontab_dir).and_yield(files[0]).and_yield(files[1]) + files.each do |filename| path = File.join(subject.crontab_dir, filename) - File.expects(:file?).with(path).returns(true) - File.expects(:writable?).with(path).returns(true) + allow(File).to receive(:file?).with(path).and_return(true) + allow(File).to receive(:writable?).with(path).and_return(true) end end diff --git a/spec/unit/provider/cron/filetype_spec.rb b/spec/unit/provider/cron/filetype_spec.rb index bf96f91..bd579c1 100644 --- a/spec/unit/provider/cron/filetype_spec.rb +++ b/spec/unit/provider/cron/filetype_spec.rb @@ -17,59 +17,53 @@ describe Puppet::Provider::Cron::FileType do # make Puppet::Util::SUIDManager return something deterministic, not the # uid of the user running the tests, except where overridden below. before :each do - Puppet::Util::SUIDManager.stubs(:uid).returns 1234 + allow(Puppet::Util::SUIDManager).to receive(:uid).and_return 1234 end describe '#read' do before(:each) do - Puppet::Util.stubs(:uid).with(uid).returns 9000 + allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000 end it 'runs crontab -l as the target user' do - Puppet::Util::Execution - .expects(:execute) - .with(['crontab', '-l'], user_options) - .returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) - + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) expect(cron.read).to eq(crontab) end it 'does not switch user if current user is the target user' do - Puppet::Util.expects(:uid).with(uid).twice.returns 9000 - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - Puppet::Util::Execution - .expects(:execute).with(['crontab', '-l'], options) - .returns(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) + expect(Puppet::Util).to receive(:uid).with(uid).twice.and_return 9000 + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], options).and_return(Puppet::Util::Execution::ProcessOutput.new(crontab, 0)) expect(cron.read).to eq(crontab) end it 'treats an absent crontab as empty' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, absent_crontab) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, absent_crontab) expect(cron.read).to eq('') end it "treats a nonexistent user's crontab as empty" do - Puppet::Util.expects(:uid).with(uid).returns nil + expect(Puppet::Util).to receive(:uid).with(uid).and_return nil expect(cron.read).to eq('') end it 'returns empty if the user is not authorized to use cron' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-l'], user_options).raises(Puppet::ExecutionFailure, unauthorized_crontab) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-l'], user_options).and_raise(Puppet::ExecutionFailure, unauthorized_crontab) expect(cron.read).to eq('') end end describe '#remove' do it 'runs crontab -r as the target user' do - Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], user_options) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], user_options) cron.remove end it 'does not switch user if current user is the target user' do - Puppet::Util.expects(:uid).with(uid).returns 9000 - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - Puppet::Util::Execution.expects(:execute).with(['crontab', '-r'], options) + expect(Puppet::Util).to receive(:uid).with(uid).and_return 9000 + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', '-r'], options) cron.remove end end @@ -79,30 +73,30 @@ describe Puppet::Provider::Cron::FileType do let!(:tmp_cron_path) { tmp_cron.path } before :each do - Puppet::Util.stubs(:uid).with(uid).returns 9000 - Tempfile.expects(:new).with("puppet_#{name}", encoding: Encoding.default_external).returns tmp_cron + allow(Puppet::Util).to receive(:uid).with(uid).and_return 9000 + allow(Tempfile).to receive(:new).with("puppet_#{name}", encoding: Encoding.default_external).and_return tmp_cron end after :each do - File.unstub(:chown) + allow(File).to receive(:chown).and_call_original end it 'runs crontab as the target user on a temporary file' do - File.expects(:chown).with(9000, nil, tmp_cron_path) - Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], user_options) + expect(File).to receive(:chown).with(9000, nil, tmp_cron_path) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], user_options) - tmp_cron.expects(:print).with("foo\n") + expect(tmp_cron).to receive(:print).with("foo\n") cron.write "foo\n" expect(Puppet::FileSystem).not_to exist(tmp_cron_path) end it 'does not switch user if current user is the target user' do - Puppet::Util::SUIDManager.expects(:uid).returns 9000 - File.expects(:chown).with(9000, nil, tmp_cron_path) - Puppet::Util::Execution.expects(:execute).with(['crontab', tmp_cron_path], options) + expect(Puppet::Util::SUIDManager).to receive(:uid).and_return 9000 + expect(File).to receive(:chown).with(9000, nil, tmp_cron_path) + expect(Puppet::Util::Execution).to receive(:execute).with(['crontab', tmp_cron_path], options) - tmp_cron.expects(:print).with("foo\n") + expect(tmp_cron).to receive(:print).with("foo\n") cron.write "foo\n" expect(Puppet::FileSystem).not_to exist(tmp_cron_path) diff --git a/spec/unit/provider/cron/parsed_spec.rb b/spec/unit/provider/cron/parsed_spec.rb index b115f9d..e4fa4cd 100644 --- a/spec/unit/provider/cron/parsed_spec.rb +++ b/spec/unit/provider/cron/parsed_spec.rb @@ -63,22 +63,22 @@ describe Puppet::Type.type(:cron).provider(:crontab) do describe 'when determining the correct filetype' do it 'uses the suntab filetype on Solaris' do - Facter.stubs(:value).with(:osfamily).returns 'Solaris' + allow(Facter).to receive(:value).with(:osfamily).and_return 'Solaris' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeSuntab) end it 'uses the aixtab filetype on AIX' do - Facter.stubs(:value).with(:osfamily).returns 'AIX' + allow(Facter).to receive(:value).with(:osfamily).and_return 'AIX' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeAixtab) end it 'uses the crontab filetype on other platforms' do - Facter.stubs(:value).with(:osfamily).returns 'Not a real operating system family' + allow(Facter).to receive(:value).with(:osfamily).and_return 'Not a real operating system family' expect(described_class.filetype).to eq(Puppet::Provider::Cron::FileType::FileTypeCrontab) end end - # I'd use ENV.expects(:[]).with('USER') but this does not work because + # I'd use expect(ENV).to receive(:[]).with('USER') but this does not work because # ENV["USER"] is evaluated at load time. describe 'when determining the default target' do it "should use the current user #{ENV['USER']}", if: ENV['USER'] do @@ -94,16 +94,13 @@ describe Puppet::Type.type(:cron).provider(:crontab) do let(:tabs) { [described_class.default_target] + ['foo', 'bar'] } before(:each) do - File.expects(:readable?).returns true - File.stubs(:file?).returns true - File.stubs(:writable?).returns true - end - after(:each) do - File.unstub :readable?, :file?, :writable? - Dir.unstub :foreach + allow(File).to receive(:readable?).and_return true + allow(File).to receive(:file?).and_return true + allow(File).to receive(:writable?).and_return true end + it 'adds all crontabs as targets' do - Dir.expects(:foreach).multiple_yields(*tabs) + expect(Dir).to receive(:foreach).and_yield(tabs[0]).and_yield(tabs[1]).and_yield(tabs[2]) expect(described_class.targets).to eq(tabs) end end @@ -185,36 +182,30 @@ describe Puppet::Type.type(:cron).provider(:crontab) do describe '.instances' do before :each do - described_class.stubs(:default_target).returns 'foobar' + allow(described_class).to receive(:targets).and_return ['foobar'] end describe 'on linux' do before(:each) do - Facter.stubs(:value).with(:osfamily).returns 'Linux' - Facter.stubs(:value).with(:operatingsystem) + allow(Facter).to receive(:value).with(:osfamily).and_return 'Linux' + allow(Facter).to receive(:value).with(:operatingsystem) end it 'contains no resources for a user who has no crontab' do - Puppet::Util.stubs(:uid).returns(10) + allow(Puppet::Util).to receive(:uid).and_return(10) - Puppet::Util::Execution - .expects(:execute) - .with('crontab -u foobar -l', failonfail: true, combine: true) - .returns('') - - expect(described_class.instances.select do |resource| - resource.get('target') == 'foobar' - end).to be_empty + expect(Puppet::Util::Execution).to receive(:execute).with('crontab -u foobar -l', failonfail: true, combine: true).and_return('') + expect(described_class.instances).to be_empty end it 'contains no resources for a user who is absent' do - Puppet::Util.stubs(:uid).returns(nil) + allow(Puppet::Util).to receive(:uid).and_return(nil) expect(described_class.instances).to be_empty end it 'is able to create records from not-managed records' do - described_class.stubs(:target_object).returns File.new(my_fixture('simple')) + allow(described_class).to receive(:target_object).and_return File.new(my_fixture('simple')) parameters = described_class.instances.map do |p| h = { name: p.get(:name) } Puppet::Type.type(:cron).validproperties.each do |property| @@ -250,7 +241,7 @@ describe Puppet::Type.type(:cron).provider(:crontab) do end it 'is able to parse puppet managed cronjobs' do - described_class.stubs(:target_object).returns File.new(my_fixture('managed')) + allow(described_class).to receive(:target_object).and_return File.new(my_fixture('managed')) expect(described_class.instances.map do |p| h = { name: p.get(:name) } Puppet::Type.type(:cron).validproperties.each do |property| diff --git a/spec/unit/type/cron_spec.rb b/spec/unit/type/cron_spec.rb index 1ce66a4..78deabf 100644 --- a/spec/unit/type/cron_spec.rb +++ b/spec/unit/type/cron_spec.rb @@ -3,12 +3,12 @@ require 'spec_helper' describe Puppet::Type.type(:cron), unless: Puppet.features.microsoft_windows? do let(:simple_provider) do provider_class = described_class.provide(:simple) { mk_resource_methods } - provider_class.stubs(:suitable?).returns true + allow(provider_class).to receive(:suitable?).and_return true provider_class end before :each do - described_class.stubs(:defaultprovider).returns simple_provider + allow(described_class).to receive(:defaultprovider).and_return simple_provider end after :each do @@ -574,7 +574,7 @@ describe Puppet::Type.type(:cron), unless: Puppet.features.microsoft_windows? do end it 'defaults to user => root if Etc.getpwuid(Process.uid) returns nil (#12357)' do - Etc.expects(:getpwuid).returns(nil) + expect(Etc).to receive(:getpwuid).and_return(nil) entry = described_class.new(name: 'test_entry', ensure: :present) expect(entry.value(:user)).to eql 'root' end -- cgit v1.2.3 From 9596250a8c0d4263c8ecc7b4c84e4df4ee57b968 Mon Sep 17 00:00:00 2001 From: Gabriel Nagy Date: Thu, 11 Mar 2021 14:21:44 +0200 Subject: (MODULES-10953) Update metadata.json and pdk version To avoid having to update this everytime we release a new agent platform, it should be enough to specify the supported OS, without specific versions. It is assumed that for each OS in metadata.json, the versions supported are the same as what the agent itself supports. --- Gemfile | 2 +- metadata.json | 52 +++++++++++----------------------------------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/Gemfile b/Gemfile index 110fa1a..de6992f 100644 --- a/Gemfile +++ b/Gemfile @@ -50,7 +50,7 @@ end group :release do gem "puppet-blacksmith", '~> 3.4', require: false - gem "pdk", platforms: [:ruby] + gem "pdk", '~> 2.0', platforms: [:ruby] end puppet_version = ENV['PUPPET_GEM_VERSION'] diff --git a/metadata.json b/metadata.json index abcf936..60d9c0c 100644 --- a/metadata.json +++ b/metadata.json @@ -12,70 +12,40 @@ ], "operatingsystem_support": [ { - "operatingsystem": "CentOS", - "operatingsystemrelease": [ - "7" - ] + "operatingsystem": "CentOS" }, { - "operatingsystem": "OracleLinux", - "operatingsystemrelease": [ - "7" - ] + "operatingsystem": "OracleLinux" }, { - "operatingsystem": "RedHat", - "operatingsystemrelease": [ - "7" - ] + "operatingsystem": "RedHat" }, { - "operatingsystem": "Scientific", - "operatingsystemrelease": [ - "7" - ] + "operatingsystem": "Scientific" }, { - "operatingsystem": "Debian", - "operatingsystemrelease": [ - "8" - ] + "operatingsystem": "Debian" }, { - "operatingsystem": "Ubuntu", - "operatingsystemrelease": [ - "16.04" - ] + "operatingsystem": "Ubuntu" }, { - "operatingsystem": "Fedora", - "operatingsystemrelease": [ - "25" - ] + "operatingsystem": "Fedora" }, { - "operatingsystem": "Darwin", - "operatingsystemrelease": [ - "16" - ] + "operatingsystem": "Darwin" }, { - "operatingsystem": "SLES", - "operatingsystemrelease": [ - "12" - ] + "operatingsystem": "SLES" }, { - "operatingsystem": "Solaris", - "operatingsystemrelease": [ - "11" - ] + "operatingsystem": "Solaris" } ], "requirements": [ { "name": "puppet", - "version_requirement": ">= 6.0.0 < 7.0.0" + "version_requirement": ">= 6.0.0 < 8.0.0" } ], "pdk-version": "1.14.0", -- cgit v1.2.3