Skip to content

Commit 86e44a0

Browse files
author
Kylo Ginsberg
committed
Merge pull request #2970 from kylog/joshcooper-ticket/master/PUP-2907
ticket/master/pup 2907
2 parents e2549ac + 1888bc0 commit 86e44a0

File tree

7 files changed

+170
-59
lines changed

7 files changed

+170
-59
lines changed

lib/puppet/configurer.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class Puppet::Configurer
99
require 'puppet/configurer/fact_handler'
1010
require 'puppet/configurer/plugin_handler'
11+
require 'puppet/configurer/downloader_factory'
1112

1213
include Puppet::Configurer::FactHandler
13-
include Puppet::Configurer::PluginHandler
1414

1515
# For benchmarking
1616
include Puppet::Util
@@ -44,13 +44,14 @@ def init_storage
4444
end
4545
end
4646

47-
def initialize
47+
def initialize(factory = Puppet::Configurer::DownloaderFactory.new)
4848
Puppet.settings.use(:main, :ssl, :agent)
4949

5050
@running = false
5151
@splayed = false
5252
@environment = Puppet[:environment]
5353
@transaction_uuid = SecureRandom.uuid
54+
@handler = Puppet::Configurer::PluginHandler.new(factory)
5455
end
5556

5657
# Get the remote catalog, yo. Returns nil if no catalog can be found.
@@ -295,4 +296,8 @@ def retrieve_new_catalog(query_options)
295296
Puppet.log_exception(detail, "Could not retrieve catalog from remote server: #{detail}")
296297
return nil
297298
end
299+
300+
def download_plugins(remote_environment_for_plugins)
301+
@handler.download_plugins(remote_environment_for_plugins)
302+
end
298303
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'puppet/configurer'
2+
3+
# Factory for <tt>Puppet::Configurer::Downloader</tt> objects.
4+
#
5+
# Puppet's pluginsync facilities can be used to download modules
6+
# and external facts, each with a different destination directory
7+
# and related settings.
8+
#
9+
# @api private
10+
#
11+
class Puppet::Configurer::DownloaderFactory
12+
def create_plugin_downloader(environment)
13+
plugin_downloader = Puppet::Configurer::Downloader.new(
14+
"plugin",
15+
Puppet[:plugindest],
16+
Puppet[:pluginsource],
17+
Puppet[:pluginsignore],
18+
environment
19+
)
20+
end
21+
22+
def create_plugin_facts_downloader(environment)
23+
source_permissions = Puppet.features.microsoft_windows? ? :ignore : :use
24+
25+
plugin_fact_downloader = Puppet::Configurer::Downloader.new(
26+
"pluginfacts",
27+
Puppet[:pluginfactdest],
28+
Puppet[:pluginfactsource],
29+
Puppet[:pluginsignore],
30+
environment,
31+
source_permissions
32+
)
33+
end
34+
end

lib/puppet/configurer/plugin_handler.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
# Break out the code related to plugins. This module is
22
# just included into the agent, but having it here makes it
33
# easier to test.
4-
module Puppet::Configurer::PluginHandler
4+
require 'puppet/configurer'
5+
6+
class Puppet::Configurer::PluginHandler
7+
def initialize(factory)
8+
@factory = factory
9+
end
10+
511
# Retrieve facts from the central server.
612
def download_plugins(environment)
7-
plugin_downloader = Puppet::Configurer::Downloader.new(
8-
"plugin",
9-
Puppet[:plugindest],
10-
Puppet[:pluginsource],
11-
Puppet[:pluginsignore],
12-
environment
13-
)
13+
plugin_downloader = @factory.create_plugin_downloader(environment)
14+
1415
if Puppet.features.external_facts?
15-
plugin_fact_downloader = Puppet::Configurer::Downloader.new(
16-
"pluginfacts",
17-
Puppet[:pluginfactdest],
18-
Puppet[:pluginfactsource],
19-
Puppet[:pluginsignore],
20-
environment,
21-
:use
22-
)
23-
plugin_fact_downloader.evaluate
16+
plugin_fact_downloader = @factory.create_plugin_facts_downloader(environment)
17+
plugin_fact_downloader.evaluate
2418
end
2519

2620
plugin_downloader.evaluate

spec/integration/configurer_spec.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@
66
describe Puppet::Configurer do
77
include PuppetSpec::Files
88

9-
describe "when downloading plugins" do
10-
it "should use the :pluginsignore setting, split on whitespace, for ignoring remote files" do
11-
Puppet.settings.stubs(:use)
12-
resource = Puppet::Type.type(:notify).new :name => "yay"
13-
Puppet::Type.type(:file).expects(:new).at_most(2).with do |args|
14-
args[:ignore] == Puppet[:pluginsignore].split(/\s+/)
15-
end.returns resource
16-
17-
configurer = Puppet::Configurer.new
18-
configurer.stubs(:download_plugins?).returns true
19-
configurer.download_plugins(Puppet::Node::Environment.remote(:testing))
20-
end
21-
end
22-
239
describe "when running" do
2410
before(:each) do
2511
@catalog = Puppet::Resource::Catalog.new("testing", Puppet.lookup(:environments).get(Puppet[:environment]))
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#! /usr/bin/env ruby
2+
require 'spec_helper'
3+
require 'puppet/configurer'
4+
5+
describe Puppet::Configurer::DownloaderFactory do
6+
let(:factory) { Puppet::Configurer::DownloaderFactory.new }
7+
let(:environment) { Puppet::Node::Environment.create(:myenv, []) }
8+
9+
let(:plugin_downloader) do
10+
factory.create_plugin_downloader(environment)
11+
end
12+
13+
let(:facts_downloader) do
14+
factory.create_plugin_facts_downloader(environment)
15+
end
16+
17+
def ignores_source_permissions(downloader)
18+
expect(downloader.file[:source_permissions]).to eq(:ignore)
19+
end
20+
21+
def uses_source_permissions(downloader)
22+
expect(downloader.file[:source_permissions]).to eq(:use)
23+
end
24+
25+
context "when creating a plugin downloader for modules" do
26+
it 'is named "plugin"' do
27+
expect(plugin_downloader.name).to eq('plugin')
28+
end
29+
30+
it 'downloads files into Puppet[:plugindest]' do
31+
plugindest = File.expand_path("/tmp/pdest")
32+
Puppet[:plugindest] = plugindest
33+
34+
expect(plugin_downloader.file[:path]).to eq(plugindest)
35+
end
36+
37+
it 'downloads files from Puppet[:pluginsource]' do
38+
Puppet[:pluginsource] = 'puppet:///myotherplugins'
39+
40+
expect(plugin_downloader.file[:source]).to eq([Puppet[:pluginsource]])
41+
end
42+
43+
it 'ignores files from Puppet[:pluginsignore]' do
44+
Puppet[:pluginsignore] = 'pignore'
45+
46+
expect(plugin_downloader.file[:ignore]).to eq(['pignore'])
47+
end
48+
49+
it 'splits Puppet[:pluginsignore] on whitespace' do
50+
Puppet[:pluginsignore] = ".svn CVS .git"
51+
52+
expect(plugin_downloader.file[:ignore]).to eq(%w[.svn CVS .git])
53+
end
54+
55+
it "ignores source permissions" do
56+
ignores_source_permissions(plugin_downloader)
57+
end
58+
end
59+
60+
context "when creating a plugin downloader for external facts" do
61+
it 'is named "pluginfacts"' do
62+
expect(facts_downloader.name).to eq('pluginfacts')
63+
end
64+
65+
it 'downloads files into Puppet[:pluginfactdest]' do
66+
plugindest = File.expand_path("/tmp/pdest")
67+
Puppet[:pluginfactdest] = plugindest
68+
69+
expect(facts_downloader.file[:path]).to eq(plugindest)
70+
end
71+
72+
it 'downloads files from Puppet[:pluginfactsource]' do
73+
Puppet[:pluginfactsource] = 'puppet:///myotherfacts'
74+
75+
expect(facts_downloader.file[:source]).to eq([Puppet[:pluginfactsource]])
76+
end
77+
78+
it 'ignores files from Puppet[:pluginsignore]' do
79+
Puppet[:pluginsignore] = 'pignore'
80+
81+
expect(facts_downloader.file[:ignore]).to eq(['pignore'])
82+
end
83+
84+
context "on POSIX", :if => Puppet.features.posix? do
85+
it "uses source permissions" do
86+
uses_source_permissions(facts_downloader)
87+
end
88+
end
89+
90+
context "on Windows", :if => Puppet.features.microsoft_windows? do
91+
it "ignores source permissions during external fact pluginsync" do
92+
ignores_source_permissions(facts_downloader)
93+
end
94+
end
95+
end
96+
end

spec/unit/configurer/plugin_handler_spec.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33
require 'puppet/configurer'
44
require 'puppet/configurer/plugin_handler'
55

6-
class PluginHandlerTester
7-
include Puppet::Configurer::PluginHandler
8-
attr_accessor :environment
9-
end
10-
116
describe Puppet::Configurer::PluginHandler do
12-
before do
13-
@pluginhandler = PluginHandlerTester.new
7+
let(:factory) { Puppet::Configurer::DownloaderFactory.new }
8+
let(:pluginhandler) { Puppet::Configurer::PluginHandler.new(factory) }
9+
let(:environment) { Puppet::Node::Environment.create(:myenv, []) }
1410

11+
before :each do
1512
# PluginHandler#load_plugin has an extra-strong rescue clause
1613
# this mock is to make sure that we don't silently ignore errors
1714
Puppet.expects(:err).never
1815
end
1916

20-
it "should use an Agent Downloader, with the name, source, destination, ignore, and environment set correctly, to download plugins when downloading is enabled" do
21-
environment = Puppet::Node::Environment.create(:myenv, [])
22-
Puppet.features.stubs(:external_facts?).returns(:true)
23-
plugindest = File.expand_path("/tmp/pdest")
24-
Puppet[:pluginsource] = "psource"
25-
Puppet[:plugindest] = plugindest
26-
Puppet[:pluginsignore] = "pignore"
27-
Puppet[:pluginfactsource] = "psource"
28-
Puppet[:pluginfactdest] = plugindest
17+
it "downloads plugins and facts" do
18+
Puppet.features.stubs(:external_facts?).returns(true)
19+
20+
plugin_downloader = stub('plugin-downloader', :evaluate => [])
21+
facts_downloader = stub('facts-downloader', :evaluate => [])
22+
23+
factory.expects(:create_plugin_downloader).returns(plugin_downloader)
24+
factory.expects(:create_plugin_facts_downloader).returns(facts_downloader)
25+
26+
pluginhandler.download_plugins(environment)
27+
end
28+
29+
it "skips facts if not enabled" do
30+
Puppet.features.stubs(:external_facts?).returns(false)
2931

30-
downloader = mock 'downloader'
31-
Puppet::Configurer::Downloader.expects(:new).with("pluginfacts", plugindest, "psource", "pignore", environment, :use).returns downloader
32-
Puppet::Configurer::Downloader.expects(:new).with("plugin", plugindest, "psource", "pignore", environment).returns downloader
32+
plugin_downloader = stub('plugin-downloader', :evaluate => [])
3333

34-
downloader.stubs(:evaluate).returns([])
35-
downloader.expects(:evaluate).twice
34+
factory.expects(:create_plugin_downloader).returns(plugin_downloader)
35+
factory.expects(:create_plugin_facts_downloader).never
3636

37-
@pluginhandler.download_plugins(environment)
37+
pluginhandler.download_plugins(environment)
3838
end
3939
end

spec/unit/configurer_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
Puppet[:report] = true
1313
end
1414

15-
it "should include the Plugin Handler module" do
16-
Puppet::Configurer.ancestors.should be_include(Puppet::Configurer::PluginHandler)
17-
end
18-
1915
it "should include the Fact Handler module" do
2016
Puppet::Configurer.ancestors.should be_include(Puppet::Configurer::FactHandler)
2117
end

0 commit comments

Comments
 (0)