-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from glennsarti/refactor-factor
(GH-213) Gather facts using the Sidecar
- Loading branch information
Showing
13 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module PuppetLanguageServerSidecar | ||
module FacterHelper | ||
def self.current_environment | ||
begin | ||
env = Puppet.lookup(:environments).get!(Puppet.settings[:environment]) | ||
return env unless env.nil? | ||
rescue Puppet::Environments::EnvironmentNotFound | ||
PuppetLanguageServerSidecar.log_message(:warning, "[FacterHelper::current_environment] Unable to load environment #{Puppet.settings[:environment]}") | ||
rescue StandardError => e | ||
PuppetLanguageServerSidecar.log_message(:warning, "[FacterHelper::current_environment] Error loading environment #{Puppet.settings[:environment]}: #{e}") | ||
end | ||
Puppet.lookup(:current_environment) | ||
end | ||
|
||
def self.retrieve_facts(_cache, _options = {}) | ||
require 'puppet/indirector/facts/facter' | ||
|
||
PuppetLanguageServerSidecar.log_message(:debug, '[FacterHelper::retrieve_facts] Starting') | ||
facts = PuppetLanguageServer::Sidecar::Protocol::Facts.new | ||
begin | ||
req = Puppet::Indirector::Request.new(:facts, :find, 'language_server', nil, environment: current_environment) | ||
result = Puppet::Node::Facts::Facter.new.find(req) | ||
facts.from_h!(result.values) | ||
rescue StandardError => e | ||
PuppetLanguageServerSidecar.log_message(:error, "[FacterHelper::_load_facts] Error loading facts #{e.message} #{e.backtrace}") | ||
rescue LoadError => e | ||
PuppetLanguageServerSidecar.log_message(:error, "[FacterHelper::_load_facts] Error loading facts (LoadError) #{e.message} #{e.backtrace}") | ||
end | ||
|
||
PuppetLanguageServerSidecar.log_message(:debug, "[FacterHelper::retrieve_facts] Finished loading #{facts.keys.count} facts") | ||
facts | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions
5
.../languageserver-sidecar/fixtures/real_agent/cache/lib/facter/fixture_agent_custom_fact.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Facter.add('fixture_agent_custom_fact') do | ||
setcode do | ||
'fixture_agent_custom_fact_value' | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
...s/valid_environment_workspace/site/private/facts.d/fixture_environment_external_fact.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
fixture_environment_external_fact: "fixture_environment_external_fact_value" |
5 changes: 5 additions & 0 deletions
5
...es/valid_environment_workspace/site/private/lib/facter/fixture_environment_custom_fact.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Facter.add('fixture_environment_custom_fact') do | ||
setcode do | ||
'fixture_environment_custom_fact_value' | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
...eserver-sidecar/fixtures/valid_module_workspace/facts.d/fixture_module_external_fact.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
fixture_module_external_fact: "fixture_module_external_fact_value" |
5 changes: 5 additions & 0 deletions
5
...geserver-sidecar/fixtures/valid_module_workspace/lib/facter/fixture_module_custom_fact.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Facter.add('fixture_module_custom_fact') do | ||
setcode do | ||
'fixture_module_custom_fact_value' | ||
end | ||
end |
90 changes: 90 additions & 0 deletions
90
spec/languageserver-sidecar/integration/puppet-languageserver-sidecar/facter_helper_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require 'spec_helper' | ||
require 'open3' | ||
|
||
describe 'PuppetLanguageServerSidecar::FacterHelper' do | ||
let(:subject) { PuppetLanguageServerSidecar::FacterHelper } | ||
|
||
def run_sidecar(cmd_options) | ||
# Use a new array so we don't affect the original cmd_options) | ||
cmd = cmd_options.dup | ||
|
||
# Append the puppet test-fixtures | ||
cmd << '--puppet-settings' | ||
cmd << "--vardir,#{File.join($fixtures_dir, 'real_agent', 'cache')},--confdir,#{File.join($fixtures_dir, 'real_agent', 'confdir')}" | ||
|
||
cmd.unshift('puppet-languageserver-sidecar') | ||
cmd.unshift('ruby') | ||
stdout, _stderr, status = Open3.capture3(*cmd) | ||
|
||
raise "Expected exit code of 0, but got #{status.exitstatus} #{_stderr}" unless status.exitstatus.zero? | ||
return stdout.bytes.pack('U*') | ||
end | ||
|
||
let(:default_fact_names) { ['hostname', 'fixture_agent_custom_fact'] } | ||
let(:module_fact_names) { ['fixture_module_custom_fact', 'fixture_module_external_fact'] } | ||
let(:environment_fact_names) { ['fixture_environment_custom_fact', 'fixture_environment_external_fact'] } | ||
|
||
describe 'when running facts action' do | ||
let (:cmd_options) { ['--action', 'facts'] } | ||
|
||
it 'should return a deserializable facts object with all default facts' do | ||
result = run_sidecar(cmd_options) | ||
deserial = PuppetLanguageServer::Sidecar::Protocol::Facts.new | ||
expect { deserial.from_json!(result) }.to_not raise_error | ||
|
||
default_fact_names.each do |name| | ||
expect(deserial).to include(name) | ||
end | ||
|
||
module_fact_names.each do |name| | ||
expect(deserial).not_to include(name) | ||
end | ||
end | ||
end | ||
|
||
context 'given a workspace containing a module' do | ||
# Test fixtures used is fixtures/valid_module_workspace | ||
let(:workspace) { File.join($fixtures_dir, 'valid_module_workspace') } | ||
|
||
describe 'when running facts action' do | ||
let (:cmd_options) { ['--action', 'facts', '--local-workspace', workspace] } | ||
|
||
it 'should return a deserializable facts object with default facts and workspace facts' do | ||
result = run_sidecar(cmd_options) | ||
deserial = PuppetLanguageServer::Sidecar::Protocol::Facts.new | ||
expect { deserial.from_json!(result) }.to_not raise_error | ||
|
||
default_fact_names.each do |name| | ||
expect(deserial).to include(name) | ||
end | ||
|
||
module_fact_names.each do |name| | ||
expect(deserial).to include(name) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'given a workspace containing an environment.conf' do | ||
# Test fixtures used is fixtures/valid_environment_workspace | ||
let(:workspace) { File.join($fixtures_dir, 'valid_environment_workspace') } | ||
|
||
describe 'when running facts action' do | ||
let (:cmd_options) { ['--action', 'facts', '--local-workspace', workspace] } | ||
|
||
it 'should return a deserializable facts object with default facts and workspace facts' do | ||
result = run_sidecar(cmd_options) | ||
deserial = PuppetLanguageServer::Sidecar::Protocol::Facts.new | ||
expect { deserial.from_json!(result) }.to_not raise_error | ||
|
||
default_fact_names.each do |name| | ||
expect(deserial).to include(name) | ||
end | ||
|
||
environment_fact_names.each do |name| | ||
expect(deserial).to include(name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters