Skip to content

Commit 8047d1c

Browse files
authored
Merge pull request #9294 from joshcooper/PUP-11655
(PUP-11655) Use run_mode for better platform independence
2 parents fa0e42a + d3d73f1 commit 8047d1c

File tree

7 files changed

+183
-93
lines changed

7 files changed

+183
-93
lines changed

lib/puppet/defaults.rb

+5-19
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,15 @@ def self.default_cadir
4747
end
4848

4949
def self.default_basemodulepath
50-
if Puppet::Util::Platform.windows?
51-
path = ['$codedir/modules']
52-
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
53-
if installdir
54-
path << "#{installdir}/puppet/modules"
55-
end
56-
path.join(File::PATH_SEPARATOR)
57-
else
58-
'$codedir/modules:/opt/puppetlabs/puppet/modules'
50+
path = ['$codedir/modules']
51+
if (run_mode_dir = Puppet.run_mode.common_module_dir)
52+
path << run_mode_dir
5953
end
54+
path.join(File::PATH_SEPARATOR)
6055
end
6156

6257
def self.default_vendormoduledir
63-
if Puppet::Util::Platform.windows?
64-
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
65-
if installdir
66-
"#{installdir}\\puppet\\vendor_modules"
67-
else
68-
nil
69-
end
70-
else
71-
'/opt/puppetlabs/puppet/vendor_modules'
72-
end
58+
Puppet.run_mode.vendor_module_dir
7359
end
7460

7561
############################################################################################

lib/puppet/provider/package/gem.rb

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def self.execute_gem_command(command, command_options, custom_environment = {})
8383
custom_environment[:PATH] = windows_path_without_puppet_bin
8484
end
8585

86+
# This uses an unusual form of passing the command and args as [<cmd>, [<arg1>, <arg2>, ...]]
8687
execute(cmd, { :failonfail => true, :combine => true, :custom_environment => custom_environment })
8788
end
8889

lib/puppet/provider/package/puppet_gem.rb

+4-15
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,7 @@
88

99
confine :true => Puppet.runtime[:facter].value(:aio_agent_version)
1010

11-
def self.windows_gemcmd
12-
puppet_dir = ENV.fetch('PUPPET_DIR', nil)
13-
if puppet_dir
14-
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
15-
else
16-
File.join(Gem.default_bindir, 'gem.bat')
17-
end
18-
end
19-
20-
if Puppet::Util::Platform.windows?
21-
commands :gemcmd => windows_gemcmd
22-
else
23-
commands :gemcmd => "/opt/puppetlabs/puppet/bin/gem"
24-
end
11+
commands :gemcmd => Puppet.run_mode.gem_cmd
2512

2613
def uninstall
2714
super
@@ -30,7 +17,9 @@ def uninstall
3017
end
3118

3219
def self.execute_gem_command(command, command_options, custom_environment = {})
33-
custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig' unless Puppet::Util::Platform.windows?
20+
if (pkg_config_path = Puppet.run_mode.pkg_config_path)
21+
custom_environment['PKG_CONFIG_PATH'] = pkg_config_path
22+
end
3423
super(command, command_options, custom_environment)
3524
end
3625
end

lib/puppet/util/run_mode.rb

+40
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ def run_dir
8787
def log_dir
8888
which_dir("/var/log/puppetlabs/puppet", "~/.puppetlabs/var/log")
8989
end
90+
91+
def pkg_config_path
92+
'/opt/puppetlabs/puppet/lib/pkgconfig'
93+
end
94+
95+
def gem_cmd
96+
'/opt/puppetlabs/puppet/bin/gem'
97+
end
98+
99+
def common_module_dir
100+
'/opt/puppetlabs/puppet/modules'
101+
end
102+
103+
def vendor_module_dir
104+
'/opt/puppetlabs/puppet/vendor_modules'
105+
end
90106
end
91107

92108
class WindowsRunMode < RunMode
@@ -114,8 +130,32 @@ def log_dir
114130
which_dir(File.join(windows_common_base("puppet/var/log")), "~/.puppetlabs/var/log")
115131
end
116132

133+
def pkg_config_path
134+
nil
135+
end
136+
137+
def gem_cmd
138+
if (puppet_dir = ENV.fetch('PUPPET_DIR', nil))
139+
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
140+
else
141+
File.join(Gem.default_bindir, 'gem.bat')
142+
end
143+
end
144+
145+
def common_module_dir
146+
"#{installdir}/puppet/modules" if installdir
147+
end
148+
149+
def vendor_module_dir
150+
"#{installdir}\\puppet\\vendor_modules" if installdir
151+
end
152+
117153
private
118154

155+
def installdir
156+
ENV.fetch('FACTER_env_windows_installdir', nil)
157+
end
158+
119159
def windows_common_base(*extra)
120160
[ENV.fetch('ALLUSERSPROFILE', nil), "PuppetLabs"] + extra
121161
end

spec/unit/provider/package/puppet_gem_spec.rb

+23-42
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,26 @@
2020
let(:provider_gem_cmd) { '/opt/puppetlabs/puppet/bin/gem' }
2121
end
2222

23-
custom_environment = {"HOME"=>ENV["HOME"]}
24-
custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig'
25-
26-
let(:execute_options) { {:failonfail => true, :combine => true, :custom_environment => custom_environment} }
23+
let(:execute_options) do
24+
{
25+
failonfail: true,
26+
combine: true,
27+
custom_environment: {
28+
'HOME'=>ENV['HOME'],
29+
'PKG_CONFIG_PATH' => '/opt/puppetlabs/puppet/lib/pkgconfig'
30+
}
31+
}
32+
end
2733

2834
before :each do
2935
resource.provider = provider
30-
allow(described_class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
31-
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
32-
end
33-
34-
35-
describe '.windows_gemcmd' do
36-
context 'when PUPPET_DIR is not set' do
37-
before do
38-
# allow(ENV).to receive(:fetch, anything).and_call_original
39-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil)
40-
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
41-
end
42-
43-
it 'uses Gem.default_bindir' do
44-
expected_path = File.join('default_gem_bin', 'gem.bat')
45-
expect(described_class.windows_gemcmd).to eql(expected_path)
46-
end
47-
end
48-
49-
context 'when PUPPET_DIR is set' do
50-
before do
51-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir')
52-
end
53-
54-
it 'uses Gem.default_bindir' do
55-
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
56-
expect(described_class.windows_gemcmd).to eql(expected_path)
57-
end
36+
if Puppet::Util::Platform.windows?
37+
# provider is loaded before we can stub, so stub the class we're testing
38+
allow(provider.class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
39+
else
40+
allow(provider.class).to receive(:which).with(provider_gem_cmd).and_return(provider_gem_cmd)
5841
end
42+
allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true)
5943
end
6044

6145
context "when installing" do
@@ -64,45 +48,43 @@
6448
end
6549

6650
it "should use the path to the gem command" do
67-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
68-
expect(described_class).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
51+
expect(described_class).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
6952
provider.install
7053
end
7154

7255
it "should not append install_options by default" do
73-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{install --no-rdoc --no-ri myresource}).and_return('')
56+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{install --no-rdoc --no-ri myresource}], anything).and_return('')
7457
provider.install
7558
end
7659

7760
it "should allow setting an install_options parameter" do
7861
resource[:install_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
79-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{install --force --bindir=/usr/bin --no-rdoc --no-ri myresource}).and_return('')
62+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{install --force --bindir=/usr/bin --no-rdoc --no-ri myresource}], anything).and_return('')
8063
provider.install
8164
end
8265
end
8366

8467
context "when uninstalling" do
8568
it "should use the path to the gem command" do
86-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
87-
expect(described_class).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
69+
expect(described_class).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
8870
provider.uninstall
8971
end
9072

9173
it "should not append uninstall_options by default" do
92-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource}).and_return('')
74+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource}], anything).and_return('')
9375
provider.uninstall
9476
end
9577

9678
it "should allow setting an uninstall_options parameter" do
9779
resource[:uninstall_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
98-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource --force --bindir=/usr/bin}).and_return('')
80+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource --force --bindir=/usr/bin}], anything).and_return('')
9981
provider.uninstall
10082
end
10183

10284
it 'should invalidate the rubygems cache' do
10385
gem_source = double('gem_source')
10486
allow(Puppet::Util::Autoload).to receive(:gem_source).and_return(gem_source)
105-
expect(described_class).to receive(:execute_gem_command).with(provider_gem_cmd, %w{uninstall --executables --all myresource}).and_return('')
87+
expect(described_class).to receive(:execute).with([provider_gem_cmd, %w{uninstall --executables --all myresource}], anything).and_return('')
10688
expect(gem_source).to receive(:clear_paths)
10789
provider.uninstall
10890
end
@@ -125,5 +107,4 @@
125107
it { is_expected.to be > 100 }
126108
end
127109
end
128-
129110
end

spec/unit/provider/package/puppetserver_gem_spec.rb

+19-17
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,57 @@
1616

1717
let(:provider_gem_cmd) { '/opt/puppetlabs/bin/puppetserver' }
1818

19-
custom_environment = { HOME: ENV['HOME'] }
20-
21-
let(:execute_options) { { failonfail: true, combine: true, custom_environment: custom_environment } }
19+
let(:execute_options) do
20+
{ failonfail: true, combine: true, custom_environment: { 'HOME' => ENV['HOME'] } }
21+
end
2222

2323
before :each do
2424
resource.provider = provider
2525
allow(Puppet::Util).to receive(:which).with(provider_gem_cmd).and_return(provider_gem_cmd)
26+
allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true)
2627
end
2728

2829
describe "#install" do
2930
it "uses the path to the gem command" do
30-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
31-
expect(Puppet::Util::Execution).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
31+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
3232
provider.install
3333
end
3434

3535
it "appends version if given" do
3636
resource[:ensure] = ['1.2.1']
37-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install -v 1.2.1 --no-document myresource}).and_return('')
37+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install -v 1.2.1 --no-document myresource}], anything).and_return('')
3838
provider.install
3939
end
4040

4141
context "with install_options" do
4242
it "does not append the parameter by default" do
43-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document myresource}).and_return('')
43+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document myresource}], anything).and_return('')
4444
provider.install
4545
end
4646

4747
it "allows setting the parameter" do
4848
resource[:install_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
49-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --force --bindir=/usr/bin --no-document myresource}).and_return('')
49+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --force --bindir=/usr/bin --no-document myresource}], anything).and_return('')
5050
provider.install
5151
end
5252
end
5353

5454
context "with source" do
5555
it "correctly sets http source" do
5656
resource[:source] = 'http://rubygems.com'
57-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document --source http://rubygems.com myresource}).and_return('')
57+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document --source http://rubygems.com myresource}], anything).and_return('')
5858
provider.install
5959
end
6060

6161
it "correctly sets local file source" do
6262
resource[:source] = 'paint-2.2.0.gem'
63-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document paint-2.2.0.gem}).and_return('')
63+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document paint-2.2.0.gem}], anything).and_return('')
6464
provider.install
6565
end
6666

6767
it "correctly sets local file source with URI scheme" do
6868
resource[:source] = 'file:///root/paint-2.2.0.gem'
69-
expect(described_class).to receive(:puppetservercmd).with(%w{gem install --no-document /root/paint-2.2.0.gem}).and_return('')
69+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem install --no-document /root/paint-2.2.0.gem}], anything).and_return('')
7070
provider.install
7171
end
7272

@@ -84,20 +84,19 @@
8484

8585
describe "#uninstall" do
8686
it "uses the path to the gem command" do
87-
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
88-
expect(Puppet::Util::Execution).to receive(:execute).with(be_a(Array), execute_options) { |args| expect(args[0]).to eq(provider_gem_cmd) }.and_return('')
87+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, be_an(Array)], be_a(Hash)).and_return('')
8988
provider.uninstall
9089
end
9190

9291
context "with uninstall_options" do
9392
it "does not append the parameter by default" do
94-
expect(described_class).to receive(:puppetservercmd).with(%w{gem uninstall --executables --all myresource}).and_return('')
93+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem uninstall --executables --all myresource}], anything).and_return('')
9594
provider.uninstall
9695
end
9796

9897
it "allows setting the parameter" do
9998
resource[:uninstall_options] = [ '--force', {'--bindir' => '/usr/bin' } ]
100-
expect(described_class).to receive(:puppetservercmd).with(%w{gem uninstall --executables --all myresource --force --bindir=/usr/bin}).and_return('')
99+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem uninstall --executables --all myresource --force --bindir=/usr/bin}], anything).and_return('')
101100
provider.uninstall
102101
end
103102
end
@@ -106,14 +105,17 @@
106105
describe ".gemlist" do
107106
context "listing installed packages" do
108107
it "uses the puppet_gem provider_command to list local gems" do
108+
allow(Puppet::Type::Package::ProviderPuppet_gem).to receive(:provider_command).and_return('/opt/puppetlabs/puppet/bin/gem')
109+
allow(described_class).to receive(:validate_command).with('/opt/puppetlabs/puppet/bin/gem')
110+
109111
expected = { name: 'world_airports', provider: :puppetserver_gem, ensure: ['1.1.3'] }
110-
expect(described_class).to receive(:execute_rubygems_list_command).with(['gem', 'list', '--local']).and_return(File.read(my_fixture('gem-list-local-packages')))
112+
expect(Puppet::Util::Execution).to receive(:execute).with(['/opt/puppetlabs/puppet/bin/gem', %w[list --local]], anything).and_return(File.read(my_fixture('gem-list-local-packages')))
111113
expect(described_class.gemlist({ local: true })).to include(expected)
112114
end
113115
end
114116

115117
it "appends the gem source if given" do
116-
expect(described_class).to receive(:puppetservercmd).with(%w{gem list --remote --source https://rubygems.com}).and_return('')
118+
expect(Puppet::Util::Execution).to receive(:execute).with([provider_gem_cmd, %w{gem list --remote --source https://rubygems.com}], anything).and_return('')
117119
described_class.gemlist({ source: 'https://rubygems.com' })
118120
end
119121
end

0 commit comments

Comments
 (0)