|
2 | 2 |
|
3 | 3 | require 'spec_helper' |
4 | 4 | require 'puppet/type' |
| 5 | +require 'puppet/resource_api' |
5 | 6 | require 'puppet/provider/dsc_base_provider/dsc_base_provider' |
6 | 7 | require 'json' |
7 | 8 |
|
8 | 9 | RSpec.describe Puppet::Provider::DscBaseProvider do |
9 | 10 | subject(:provider) { described_class.new } |
10 | 11 |
|
11 | | - let(:context) { instance_double(Puppet::ResourceApi::PuppetContext) } |
12 | | - let(:type) { instance_double(Puppet::ResourceApi::TypeDefinition) } |
| 12 | + let(:context) { instance_double(Puppet::ResourceApi::BaseContext, 'context') } |
| 13 | + let(:type) { instance_double(Puppet::ResourceApi::TypeDefinition, 'typedef') } |
13 | 14 | let(:ps_manager) { instance_double(Pwsh::Manager) } |
14 | 15 | let(:execute_response) { { stdout: nil, stderr: nil, exitcode: 0 } } |
15 | 16 |
|
16 | 17 | # Reset the caches after each run |
17 | 18 | after do |
18 | | - described_class.instance_variable_set(:@cached_canonicalized_resource, []) |
19 | | - described_class.instance_variable_set(:@cached_query_results, []) |
20 | | - described_class.instance_variable_set(:@cached_test_results, []) |
21 | | - described_class.instance_variable_set(:@logon_failures, []) |
| 19 | + provider.instance_variable_set(:@cached_canonicalized_resource, []) |
| 20 | + provider.instance_variable_set(:@cached_query_results, []) |
| 21 | + provider.instance_variable_set(:@cached_test_results, []) |
| 22 | + provider.instance_variable_set(:@logon_failures, []) |
22 | 23 | end |
23 | 24 |
|
24 | 25 | describe '.initialize' do |
|
28 | 29 | end |
29 | 30 |
|
30 | 31 | it 'initializes the cached_canonicalized_resource instance variable' do |
31 | | - expect(described_class.instance_variable_get(:@cached_canonicalized_resource)).to eq([]) |
| 32 | + expect(provider.instance_variable_get(:@cached_canonicalized_resource)).to eq([]) |
32 | 33 | end |
33 | 34 |
|
34 | 35 | it 'initializes the cached_query_results instance variable' do |
35 | | - expect(described_class.instance_variable_get(:@cached_query_results)).to eq([]) |
| 36 | + expect(provider.instance_variable_get(:@cached_query_results)).to eq([]) |
36 | 37 | end |
37 | 38 |
|
38 | 39 | it 'initializes the cached_test_results instance variable' do |
39 | | - expect(described_class.instance_variable_get(:@cached_test_results)).to eq([]) |
| 40 | + expect(provider.instance_variable_get(:@cached_test_results)).to eq([]) |
40 | 41 | end |
41 | 42 |
|
42 | 43 | it 'initializes the logon_failures instance variable' do |
43 | | - expect(described_class.instance_variable_get(:@logon_failures)).to eq([]) |
| 44 | + expect(provider.instance_variable_get(:@logon_failures)).to eq([]) |
44 | 45 | end |
45 | 46 | end |
46 | 47 |
|
47 | 48 | describe '.cached_test_results' do |
48 | 49 | let(:cache_value) { %w[foo bar] } |
49 | 50 |
|
| 51 | + before do |
| 52 | + provider.instance_variable_set(:@cached_test_results, cache_value) |
| 53 | + end |
| 54 | + |
50 | 55 | it 'returns the value of the @cached_test_results instance variable' do |
51 | | - described_class.instance_variable_set(:@cached_test_results, cache_value) |
52 | 56 | expect(provider.cached_test_results).to eq(cache_value) |
53 | 57 | end |
54 | 58 | end |
|
237 | 241 |
|
238 | 242 | describe '.get' do |
239 | 243 | after do |
240 | | - described_class.instance_variable_set(:@cached_canonicalized_resource, []) |
| 244 | + provider.instance_variable_set(:@cached_canonicalized_resource, []) |
241 | 245 | end |
242 | 246 |
|
243 | 247 | it 'checks the cached results, returning if one exists for the specified names' do |
244 | | - described_class.instance_variable_set(:@cached_canonicalized_resource, []) |
| 248 | + provider.instance_variable_set(:@cached_canonicalized_resource, []) |
245 | 249 | allow(context).to receive(:debug) |
246 | 250 | expect(provider).to receive(:fetch_cached_hashes).with([], [{ name: 'foo' }]).and_return([{ name: 'foo', property: 'bar' }]) |
247 | 251 | expect(provider).not_to receive(:invoke_get_method) |
248 | 252 | expect(provider.get(context, [{ name: 'foo' }])).to eq([{ name: 'foo', property: 'bar' }]) |
249 | 253 | end |
250 | 254 |
|
251 | 255 | it 'adds mandatory properties to the name hash when calling invoke_get_method' do |
252 | | - described_class.instance_variable_set(:@cached_canonicalized_resource, [{ name: 'foo', property: 'bar', dsc_some_parameter: 'baz' }]) |
| 256 | + provider.instance_variable_set(:@cached_canonicalized_resource, [{ name: 'foo', property: 'bar', dsc_some_parameter: 'baz' }]) |
253 | 257 | allow(context).to receive(:debug) |
254 | 258 | expect(provider).to receive(:fetch_cached_hashes).with([], [{ name: 'foo' }]).and_return([]) |
255 | 259 | expect(provider).to receive(:namevar_attributes).and_return([:name]).exactly(3).times |
|
530 | 534 | end |
531 | 535 |
|
532 | 536 | after do |
533 | | - described_class.instance_variable_set(:@cached_query_results, nil) |
| 537 | + provider.instance_variable_set(:@cached_query_results, []) |
534 | 538 | end |
535 | 539 |
|
536 | 540 | context 'when the invocation script returns data without errors' do |
|
557 | 561 |
|
558 | 562 | it 'caches the result' do |
559 | 563 | expect { result }.not_to raise_error |
560 | | - expect(described_class.instance_variable_get(:@cached_query_results)).to eq([result]) |
| 564 | + expect(provider.instance_variable_get(:@cached_query_results)).to eq([result]) |
561 | 565 | end |
562 | 566 |
|
563 | 567 | it 'removes unrelated properties from the result' do |
|
719 | 723 | end |
720 | 724 |
|
721 | 725 | after do |
722 | | - described_class.instance_variable_set(:@logon_failures, []) |
| 726 | + provider.instance_variable_set(:@logon_failures, []) |
723 | 727 | end |
724 | 728 |
|
725 | 729 | it 'errors specifically for a logon failure and returns nil' do |
|
728 | 732 |
|
729 | 733 | it 'caches the logon failure' do |
730 | 734 | expect { result }.not_to raise_error |
731 | | - expect(described_class.instance_variable_get(:@logon_failures)).to eq([credential_hash]) |
| 735 | + expect(provider.instance_variable_get(:@logon_failures)).to eq([credential_hash]) |
732 | 736 | end |
733 | 737 |
|
734 | 738 | it 'caches the query results' do |
735 | 739 | expect { result }.not_to raise_error |
736 | | - expect(described_class.instance_variable_get(:@cached_query_results)).to eq([name_hash]) |
| 740 | + expect(provider.instance_variable_get(:@cached_query_results)).to eq([name_hash]) |
737 | 741 | end |
738 | 742 | end |
739 | 743 |
|
|
981 | 985 | end |
982 | 986 |
|
983 | 987 | describe '.invoke_test_method' do |
984 | | - subject(:result) { provider.invoke_test_method(context, name, expect(subject).to) } |
| 988 | + subject(:result) { provider.invoke_test_method(context, name, should_hash) } |
985 | 989 |
|
986 | 990 | let(:name) { { name: 'foo', dsc_name: 'bar' } } |
987 | | - let(:should) { name.merge(dsc_ensure: 'present') } |
988 | | - let(:test_properties) { expect(subject).to.reject { |k, _v| k == :name } } |
| 991 | + let(:should_hash) { name.merge(dsc_ensure: 'present') } |
| 992 | + let(:test_properties) { should_hash.reject { |k, _v| k == :name } } |
989 | 993 | let(:invoke_dsc_resource_data) { nil } |
990 | 994 |
|
991 | 995 | before do |
|
995 | 999 | end |
996 | 1000 |
|
997 | 1001 | after do |
998 | | - described_class.instance_variable_set(:@cached_test_results, []) |
| 1002 | + provider.instance_variable_set(:@cached_test_results, []) |
999 | 1003 | end |
1000 | 1004 |
|
1001 | 1005 | context 'when something went wrong calling Invoke-DscResource' do |
|
1043 | 1047 |
|
1044 | 1048 | describe '.instantiated_variables' do |
1045 | 1049 | after do |
1046 | | - described_class.instance_variable_set(:@instantiated_variables, []) |
| 1050 | + provider.instance_variable_set(:@instantiated_variables, []) |
1047 | 1051 | end |
1048 | 1052 |
|
1049 | 1053 | it 'sets the instantiated_variables instance variable to {} if not initialized' do |
1050 | 1054 | expect(provider.instantiated_variables).to eq({}) |
1051 | 1055 | end |
1052 | 1056 |
|
1053 | 1057 | it 'returns the instantiated_variables instance variable if already initialized' do |
1054 | | - described_class.instance_variable_set(:@instantiated_variables, { foo: 'bar' }) |
| 1058 | + provider.instance_variable_set(:@instantiated_variables, { foo: 'bar' }) |
1055 | 1059 | expect(provider.instantiated_variables).to eq({ foo: 'bar' }) |
1056 | 1060 | end |
1057 | 1061 | end |
1058 | 1062 |
|
1059 | 1063 | describe '.clear_instantiated_variables!' do |
1060 | 1064 | after do |
1061 | | - described_class.instance_variable_set(:@instantiated_variables, []) |
| 1065 | + provider.instance_variable_set(:@instantiated_variables, []) |
1062 | 1066 | end |
1063 | 1067 |
|
1064 | 1068 | it 'sets the instantiated_variables instance variable to {}' do |
1065 | | - described_class.instance_variable_set(:@instantiated_variables, { foo: 'bar' }) |
| 1069 | + provider.instance_variable_set(:@instantiated_variables, { foo: 'bar' }) |
1066 | 1070 | expect { provider.clear_instantiated_variables! }.not_to raise_error |
1067 | | - expect(described_class.instance_variable_get(:@instantiated_variables)).to eq({}) |
| 1071 | + expect(provider.instance_variable_get(:@instantiated_variables)).to eq({}) |
1068 | 1072 | end |
1069 | 1073 | end |
1070 | 1074 |
|
|
1087 | 1091 | end |
1088 | 1092 |
|
1089 | 1093 | after do |
1090 | | - described_class.instance_variable_set(:@logon_failures, []) |
| 1094 | + provider.instance_variable_set(:@logon_failures, []) |
1091 | 1095 | end |
1092 | 1096 |
|
1093 | 1097 | it 'returns false if there have been no failed logons with the username/password combination' do |
1094 | | - described_class.instance_variable_set(:@logon_failures, [bad_credential_hash]) |
| 1098 | + provider.instance_variable_set(:@logon_failures, [bad_credential_hash]) |
1095 | 1099 | expect(provider.logon_failed_already?(good_credential_hash)).to be(false) |
1096 | 1100 | end |
1097 | 1101 |
|
1098 | 1102 | it 'returns true if the username/password specified are found in the logon_failures instance variable' do |
1099 | | - described_class.instance_variable_set(:@logon_failures, [good_credential_hash, bad_credential_hash]) |
| 1103 | + provider.instance_variable_set(:@logon_failures, [good_credential_hash, bad_credential_hash]) |
1100 | 1104 | expect(provider.logon_failed_already?(bad_credential_hash)).to be(true) |
1101 | 1105 | end |
1102 | 1106 | end |
|
1437 | 1441 | context 'when the resource does not have the dscmeta_resource_implementation key' do |
1438 | 1442 | let(:test_resource) { {} } |
1439 | 1443 |
|
1440 | | - it 'returns nil' do |
1441 | | - expect(result).to be_nil |
| 1444 | + it 'sets $UnmungedPSModulePath to the current PSModulePath' do |
| 1445 | + # since https://github.com/puppetlabs/ruby-pwsh/pull/261 we load vendored path for MOF resources as well |
| 1446 | + expect(result).to match(/\$UnmungedPSModulePath = .+GetEnvironmentVariable.+PSModulePath.+machine/) |
1442 | 1447 | end |
1443 | 1448 | end |
1444 | 1449 |
|
1445 | 1450 | context "when the resource's dscmeta_resource_implementation is not 'Class'" do |
1446 | 1451 | let(:test_resource) { { dscmeta_resource_implementation: 'MOF' } } |
1447 | 1452 |
|
1448 | | - it 'returns nil' do |
1449 | | - expect(result).to be_nil |
| 1453 | + # since https://github.com/puppetlabs/ruby-pwsh/pull/261 we load vendored path for MOF resources as well |
| 1454 | + it 'sets $UnmungedPSModulePath to the current PSModulePath' do |
| 1455 | + expect(result).to match(/\$UnmungedPSModulePath = .+GetEnvironmentVariable.+PSModulePath.+machine/) |
1450 | 1456 | end |
1451 | 1457 | end |
1452 | 1458 |
|
|
1510 | 1516 | end |
1511 | 1517 |
|
1512 | 1518 | after do |
1513 | | - described_class.instance_variable_set(:@instantiated_variables, []) |
| 1519 | + provider.instance_variable_set(:@instantiated_variables, []) |
1514 | 1520 | end |
1515 | 1521 |
|
1516 | 1522 | 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 |
|
1543 | 1549 | subject(:result) { provider.prepare_cim_instances(test_resource) } |
1544 | 1550 |
|
1545 | 1551 | after do |
1546 | | - described_class.instance_variable_set(:@instantiated_variables, []) |
| 1552 | + provider.instance_variable_set(:@instantiated_variables, []) |
1547 | 1553 | end |
1548 | 1554 |
|
1549 | 1555 | context 'when a cim instance is passed without nested cim instances' do |
|
1652 | 1658 |
|
1653 | 1659 | describe '.format_ciminstance' do |
1654 | 1660 | after do |
1655 | | - described_class.instance_variable_set(:@instantiated_variables, []) |
| 1661 | + provider.instance_variable_set(:@instantiated_variables, []) |
1656 | 1662 | end |
1657 | 1663 |
|
1658 | 1664 | it 'defines and returns a new cim instance as a PowerShell variable, passing the class name and property hash' do |
|
1668 | 1674 | end |
1669 | 1675 |
|
1670 | 1676 | it 'interpolates variables in the case of a cim instance containing a nested instance' do |
1671 | | - described_class.instance_variable_set(:@instantiated_variables, { 'SomeVariable' => { 'bar' => 'ope' } }) |
| 1677 | + provider.instance_variable_set(:@instantiated_variables, { 'SomeVariable' => { 'bar' => 'ope' } }) |
1672 | 1678 | property_hash = { 'foo' => { 'bar' => 'ope' } } |
1673 | 1679 | expect(provider.format_ciminstance('foo', 'SomeClass', property_hash)).to match(/@\{'foo' = \$SomeVariable\}/) |
1674 | 1680 | end |
|
0 commit comments