Skip to content

Commit

Permalink
Merge pull request #283 from puppetlabs/maint-run_all_specs
Browse files Browse the repository at this point in the history
(maint) - Run all unit test files during rake spec
  • Loading branch information
david22swan authored Jan 30, 2024
2 parents b53094e + f6d7bfa commit 36ed427
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require 'rspec/core/rake_task'
require 'yard'

RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/unit/*_spec.rb'
t.pattern = 'spec/unit/**/*_spec.rb'
end
task default: :spec

Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def interpolate_variables(string)
# @param resource [Hash] a hash with the information needed to run `Invoke-DscResource`
# @return [String] A multi-line string which sets the PSModulePath at the system level
def munge_psmodulepath(resource)
vendor_path = resource[:vendored_modules_path].tr('/', '\\')
vendor_path = resource[:vendored_modules_path]&.tr('/', '\\')
<<~MUNGE_PSMODULEPATH.strip
$UnmungedPSModulePath = [System.Environment]::GetEnvironmentVariable('PSModulePath','machine')
$MungedPSModulePath = $env:PSModulePath + ';#{vendor_path}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

require 'spec_helper'
require 'puppet/type'
require 'puppet/resource_api'
require 'puppet/provider/dsc_base_provider/dsc_base_provider'
require 'json'

RSpec.describe Puppet::Provider::DscBaseProvider do
subject(:provider) { described_class.new }

let(:context) { instance_double(Puppet::ResourceApi::PuppetContext) }
let(:type) { instance_double(Puppet::ResourceApi::TypeDefinition) }
let(:context) { instance_double(Puppet::ResourceApi::BaseContext, 'context') }
let(:type) { instance_double(Puppet::ResourceApi::TypeDefinition, 'typedef') }
let(:ps_manager) { instance_double(Pwsh::Manager) }
let(:execute_response) { { stdout: nil, stderr: nil, exitcode: 0 } }

# Reset the caches after each run
after do
described_class.instance_variable_set(:@cached_canonicalized_resource, [])
described_class.instance_variable_set(:@cached_query_results, [])
described_class.instance_variable_set(:@cached_test_results, [])
described_class.instance_variable_set(:@logon_failures, [])
provider.instance_variable_set(:@cached_canonicalized_resource, [])
provider.instance_variable_set(:@cached_query_results, [])
provider.instance_variable_set(:@cached_test_results, [])
provider.instance_variable_set(:@logon_failures, [])
end

describe '.initialize' do
Expand All @@ -28,27 +29,30 @@
end

it 'initializes the cached_canonicalized_resource instance variable' do
expect(described_class.instance_variable_get(:@cached_canonicalized_resource)).to eq([])
expect(provider.instance_variable_get(:@cached_canonicalized_resource)).to eq([])
end

it 'initializes the cached_query_results instance variable' do
expect(described_class.instance_variable_get(:@cached_query_results)).to eq([])
expect(provider.instance_variable_get(:@cached_query_results)).to eq([])
end

it 'initializes the cached_test_results instance variable' do
expect(described_class.instance_variable_get(:@cached_test_results)).to eq([])
expect(provider.instance_variable_get(:@cached_test_results)).to eq([])
end

it 'initializes the logon_failures instance variable' do
expect(described_class.instance_variable_get(:@logon_failures)).to eq([])
expect(provider.instance_variable_get(:@logon_failures)).to eq([])
end
end

describe '.cached_test_results' do
let(:cache_value) { %w[foo bar] }

before do
provider.instance_variable_set(:@cached_test_results, cache_value)
end

it 'returns the value of the @cached_test_results instance variable' do
described_class.instance_variable_set(:@cached_test_results, cache_value)
expect(provider.cached_test_results).to eq(cache_value)
end
end
Expand Down Expand Up @@ -237,19 +241,19 @@

describe '.get' do
after do
described_class.instance_variable_set(:@cached_canonicalized_resource, [])
provider.instance_variable_set(:@cached_canonicalized_resource, [])
end

it 'checks the cached results, returning if one exists for the specified names' do
described_class.instance_variable_set(:@cached_canonicalized_resource, [])
provider.instance_variable_set(:@cached_canonicalized_resource, [])
allow(context).to receive(:debug)
expect(provider).to receive(:fetch_cached_hashes).with([], [{ name: 'foo' }]).and_return([{ name: 'foo', property: 'bar' }])
expect(provider).not_to receive(:invoke_get_method)
expect(provider.get(context, [{ name: 'foo' }])).to eq([{ name: 'foo', property: 'bar' }])
end

it 'adds mandatory properties to the name hash when calling invoke_get_method' do
described_class.instance_variable_set(:@cached_canonicalized_resource, [{ name: 'foo', property: 'bar', dsc_some_parameter: 'baz' }])
provider.instance_variable_set(:@cached_canonicalized_resource, [{ name: 'foo', property: 'bar', dsc_some_parameter: 'baz' }])
allow(context).to receive(:debug)
expect(provider).to receive(:fetch_cached_hashes).with([], [{ name: 'foo' }]).and_return([])
expect(provider).to receive(:namevar_attributes).and_return([:name]).exactly(3).times
Expand Down Expand Up @@ -530,7 +534,7 @@
end

after do
described_class.instance_variable_set(:@cached_query_results, nil)
provider.instance_variable_set(:@cached_query_results, [])
end

context 'when the invocation script returns data without errors' do
Expand All @@ -557,7 +561,7 @@

it 'caches the result' do
expect { result }.not_to raise_error
expect(described_class.instance_variable_get(:@cached_query_results)).to eq([result])
expect(provider.instance_variable_get(:@cached_query_results)).to eq([result])
end

it 'removes unrelated properties from the result' do
Expand Down Expand Up @@ -719,7 +723,7 @@
end

after do
described_class.instance_variable_set(:@logon_failures, [])
provider.instance_variable_set(:@logon_failures, [])
end

it 'errors specifically for a logon failure and returns nil' do
Expand All @@ -728,12 +732,12 @@

it 'caches the logon failure' do
expect { result }.not_to raise_error
expect(described_class.instance_variable_get(:@logon_failures)).to eq([credential_hash])
expect(provider.instance_variable_get(:@logon_failures)).to eq([credential_hash])
end

it 'caches the query results' do
expect { result }.not_to raise_error
expect(described_class.instance_variable_get(:@cached_query_results)).to eq([name_hash])
expect(provider.instance_variable_get(:@cached_query_results)).to eq([name_hash])
end
end

Expand Down Expand Up @@ -981,11 +985,11 @@
end

describe '.invoke_test_method' do
subject(:result) { provider.invoke_test_method(context, name, expect(subject).to) }
subject(:result) { provider.invoke_test_method(context, name, should_hash) }

let(:name) { { name: 'foo', dsc_name: 'bar' } }
let(:should) { name.merge(dsc_ensure: 'present') }
let(:test_properties) { expect(subject).to.reject { |k, _v| k == :name } }
let(:should_hash) { name.merge(dsc_ensure: 'present') }
let(:test_properties) { should_hash.reject { |k, _v| k == :name } }
let(:invoke_dsc_resource_data) { nil }

before do
Expand All @@ -995,7 +999,7 @@
end

after do
described_class.instance_variable_set(:@cached_test_results, [])
provider.instance_variable_set(:@cached_test_results, [])
end

context 'when something went wrong calling Invoke-DscResource' do
Expand Down Expand Up @@ -1043,28 +1047,28 @@

describe '.instantiated_variables' do
after do
described_class.instance_variable_set(:@instantiated_variables, [])
provider.instance_variable_set(:@instantiated_variables, [])
end

it 'sets the instantiated_variables instance variable to {} if not initialized' do
expect(provider.instantiated_variables).to eq({})
end

it 'returns the instantiated_variables instance variable if already initialized' do
described_class.instance_variable_set(:@instantiated_variables, { foo: 'bar' })
provider.instance_variable_set(:@instantiated_variables, { foo: 'bar' })
expect(provider.instantiated_variables).to eq({ foo: 'bar' })
end
end

describe '.clear_instantiated_variables!' do
after do
described_class.instance_variable_set(:@instantiated_variables, [])
provider.instance_variable_set(:@instantiated_variables, [])
end

it 'sets the instantiated_variables instance variable to {}' do
described_class.instance_variable_set(:@instantiated_variables, { foo: 'bar' })
provider.instance_variable_set(:@instantiated_variables, { foo: 'bar' })
expect { provider.clear_instantiated_variables! }.not_to raise_error
expect(described_class.instance_variable_get(:@instantiated_variables)).to eq({})
expect(provider.instance_variable_get(:@instantiated_variables)).to eq({})
end
end

Expand All @@ -1087,16 +1091,16 @@
end

after do
described_class.instance_variable_set(:@logon_failures, [])
provider.instance_variable_set(:@logon_failures, [])
end

it 'returns false if there have been no failed logons with the username/password combination' do
described_class.instance_variable_set(:@logon_failures, [bad_credential_hash])
provider.instance_variable_set(:@logon_failures, [bad_credential_hash])
expect(provider.logon_failed_already?(good_credential_hash)).to be(false)
end

it 'returns true if the username/password specified are found in the logon_failures instance variable' do
described_class.instance_variable_set(:@logon_failures, [good_credential_hash, bad_credential_hash])
provider.instance_variable_set(:@logon_failures, [good_credential_hash, bad_credential_hash])
expect(provider.logon_failed_already?(bad_credential_hash)).to be(true)
end
end
Expand Down Expand Up @@ -1437,16 +1441,18 @@
context 'when the resource does not have the dscmeta_resource_implementation key' do
let(:test_resource) { {} }

it 'returns nil' do
expect(result).to be_nil
it 'sets $UnmungedPSModulePath to the current PSModulePath' do
# since https://github.com/puppetlabs/ruby-pwsh/pull/261 we load vendored path for MOF resources as well
expect(result).to match(/\$UnmungedPSModulePath = .+GetEnvironmentVariable.+PSModulePath.+machine/)
end
end

context "when the resource's dscmeta_resource_implementation is not 'Class'" do
let(:test_resource) { { dscmeta_resource_implementation: 'MOF' } }

it 'returns nil' do
expect(result).to be_nil
# since https://github.com/puppetlabs/ruby-pwsh/pull/261 we load vendored path for MOF resources as well
it 'sets $UnmungedPSModulePath to the current PSModulePath' do
expect(result).to match(/\$UnmungedPSModulePath = .+GetEnvironmentVariable.+PSModulePath.+machine/)
end
end

Expand Down Expand Up @@ -1510,7 +1516,7 @@
end

after do
described_class.instance_variable_set(:@instantiated_variables, [])
provider.instance_variable_set(:@instantiated_variables, [])
end

it 'writes the ruby representation of the credentials as the value of a key named for the new variable into the instantiated_variables cache' do
Expand Down Expand Up @@ -1543,7 +1549,7 @@
subject(:result) { provider.prepare_cim_instances(test_resource) }

after do
described_class.instance_variable_set(:@instantiated_variables, [])
provider.instance_variable_set(:@instantiated_variables, [])
end

context 'when a cim instance is passed without nested cim instances' do
Expand Down Expand Up @@ -1652,7 +1658,7 @@

describe '.format_ciminstance' do
after do
described_class.instance_variable_set(:@instantiated_variables, [])
provider.instance_variable_set(:@instantiated_variables, [])
end

it 'defines and returns a new cim instance as a PowerShell variable, passing the class name and property hash' do
Expand All @@ -1668,7 +1674,7 @@
end

it 'interpolates variables in the case of a cim instance containing a nested instance' do
described_class.instance_variable_set(:@instantiated_variables, { 'SomeVariable' => { 'bar' => 'ope' } })
provider.instance_variable_set(:@instantiated_variables, { 'SomeVariable' => { 'bar' => 'ope' } })
property_hash = { 'foo' => { 'bar' => 'ope' } }
expect(provider.format_ciminstance('foo', 'SomeClass', property_hash)).to match(/@\{'foo' = \$SomeVariable\}/)
end
Expand Down

0 comments on commit 36ed427

Please sign in to comment.