Skip to content

(PUP-12046) Send facts for catalog download #9424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions lib/puppet/face/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@

deactivate_action(:destroy)
deactivate_action(:search)
find = get_action(:find)
find.summary "Retrieve the catalog for the node from which the command is run."
find.arguments "<certname>"
find.returns <<-'EOT'
A serialized catalog. When used from the Ruby API, returns a
Puppet::Resource::Catalog object.
EOT
action(:find) do
summary _("Retrieve the catalog for the node from which the comamand is run.")
arguments "<certname>, <facts>"
option("--facts_for_catalog") do
summary _("Not yet implemented for the CLI; facts will be collected internally.")
end
returns <<-'EOT'
A serialized catalog. When used from the Ruby API, returns a
Puppet::Resource::Catalog object.
EOT

when_invoked do |*args|
# Default the key to Puppet[:certname] if none is supplied
if args.length == 1
key = Puppet[:certname]
else
key = args.shift
end
call_indirection_method :find, key, args.first
end
end

action(:apply) do
summary "Find and apply a catalog."
Expand Down Expand Up @@ -135,9 +149,11 @@
when_invoked do |_options|
Puppet::Resource::Catalog.indirection.terminus_class = :rest
Puppet::Resource::Catalog.indirection.cache_class = nil
facts = Puppet::Face[:facts, '0.0.1'].find(Puppet[:certname])
catalog = nil
retrieval_duration = thinmark do
catalog = Puppet::Face[:catalog, '0.0.1'].find(Puppet[:certname])
catalog = Puppet::Face[:catalog, '0.0.1'].find(Puppet[:certname],
{ facts_for_catalog: facts })
end
catalog.retrieval_duration = retrieval_duration
catalog.write_class_file
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/interface/action_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Puppet::Interface::ActionManager
# @dsl Faces
def action(name, &block)
@actions ||= {}
Puppet.warning _("Redefining action %{name} for %{self}") % { name: name, self: self } if action?(name)
Puppet.debug _("Redefining action %{name} for %{self}") % { name: name, self: self } if action?(name)

action = Puppet::Interface::ActionBuilder.build(self, name, &block)

Expand Down
39 changes: 39 additions & 0 deletions spec/unit/face/catalog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'
require 'puppet/face'
require 'puppet/indirector/facts/facter'
require 'puppet/indirector/facts/rest'

describe Puppet::Face[:catalog, '0.0.1'] do

describe '#download' do
let(:model) { Puppet::Node::Facts }
let(:test_data) { model.new('puppet.node.test', {test_fact: 'catalog_face_request_test_value'}) }
let(:catalog) { Puppet::Resource::Catalog.new('puppet.node.test', Puppet::Node::Environment.remote(Puppet[:environment].to_sym)) }

before(:each) do
Puppet[:facts_terminus] = :memory
Puppet::Node::Facts.indirection.save(test_data)
allow(Puppet::Face[:catalog, "0.0.1"]).to receive(:save).once

Puppet.settings.parse_config(<<-CONF)
[main]
server=puppet.server.test
certname=puppet.node.test
CONF

# Faces start in :user run mode
Puppet.settings.preferred_run_mode = :user
end

it "adds facts to the catalog request" do
stub_request(:post, 'https://puppet.server.test:8140/puppet/v3/catalog/puppet.node.test?environment=*root*')
.with(
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
body: hash_including(facts: URI.encode_www_form_component(Puppet::Node::Facts.indirection.find('puppet.node.test').to_json))
).to_return(:status => 200, :body => catalog.render(:json), :headers => {'Content-Type' => 'application/json'})
subject.download
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, stub_request doesn't verify the request was made, only that if a request is made that it matches at least one stub, which could be in this file or in spec_helper. Might be good to do something like https://github.com/bblimke/webmock?tab=readme-ov-file#setting-expectations-in-rspec-on-the-stub

end
end
end


Loading