Merge branch 'master' into kpaulisse-remove-time-based-tests
This commit is contained in:
Коммит
6e66c629cd
2
.version
2
.version
|
@ -1 +1 @@
|
|||
0.5.4
|
||||
0.5.7
|
||||
|
|
|
@ -8,6 +8,22 @@
|
|||
</tr>
|
||||
</thead><tbody>
|
||||
<tr valign=top>
|
||||
<td>0.5.6</td>
|
||||
<td>2016-11-16</td>
|
||||
<td>
|
||||
<ul>
|
||||
<li><a href="https://github.com/github/octocatalog-diff/pull/20">https://github.com/github/octocatalog-diff/pull/20</a>: Use modulepath from environment.conf to inform lookup directories for <code>--compare-file-text</code> feature</li>
|
||||
</ul>
|
||||
</td>
|
||||
</tr>
|
||||
<tr valign=top>
|
||||
<td>0.5.5</td>
|
||||
<td>-</td>
|
||||
<td>
|
||||
Unreleased internal version
|
||||
</td>
|
||||
</tr>
|
||||
<tr valign=top>
|
||||
<td>0.5.4</td>
|
||||
<td>2016-11-07</td>
|
||||
<td>
|
||||
|
|
|
@ -31,3 +31,28 @@ The [example configuration file](/examples/octocatalog-diff.cfg.rb) contains an
|
|||
# settings[:bootstrap_script] = '/etc/puppetlabs/repo-bootstrap.sh' # Absolute path
|
||||
# settings[:bootstrap_script] = 'script/bootstrap' # Relative path
|
||||
```
|
||||
|
||||
## Bootstrap environment
|
||||
|
||||
When the bootstrap script runs, a limited set of environment variables are passed from the shell running octocatalog-diff. Only these variables are set:
|
||||
|
||||
- `HOME`
|
||||
- `PATH`
|
||||
- `PWD` (set to the base directory of your Puppet checkout)
|
||||
- `BASEDIR` (as explicitly set with `--basedir` CLI option or `settings[:basedir]` setting)
|
||||
|
||||
If you wish to set additional environment variables for your bootstrap script, you may do so via the `--bootstrap-environment VAR=value` command line flag, or by defining `settings[:bootstrap_environment] = { 'VAR' => 'value' }` in your configuration file.
|
||||
|
||||
As an example, consider that your bootstrap script is written in Python, and needs the `PYTHONPATH` variable set to `/usr/local/lib/python-custom`. Even if this environment variable is set when octocatalog-diff is run, it will not be available to the bootstrap script. You may supply it via the command line:
|
||||
|
||||
```
|
||||
octocatalog-diff --bootstrap-environment PYTHONPATH=/usr/local/lib/python-custom ...
|
||||
```
|
||||
|
||||
Or you may specify it in your configuration file:
|
||||
|
||||
```
|
||||
settings[:bootstrap_environment] = {
|
||||
'PYTHONPATH' => '/usr/local/lib/python-custom'
|
||||
}
|
||||
```
|
||||
|
|
|
@ -37,3 +37,5 @@ bin/octocatalog-diff --hiera-config hiera.yaml --hiera-path-strip /etc/puppetlab
|
|||
:yaml:
|
||||
:datadir: /var/tmp/puppet-compile-dir-92347829847/environments/%{environment}/hieradata
|
||||
```
|
||||
|
||||
:warning: Be sure that you do NOT include a trailing slash on `--hiera-path-strip` or `settings[:hiera_path_strip]`.
|
||||
|
|
|
@ -67,3 +67,37 @@ For example, when compiling the catalog for `some-node.github.net`, Puppet will
|
|||
```
|
||||
|
||||
Sometimes the ENC script requires credentials or makes other assumptions about the system on which it is running. To be able to run the ENC script on systems other than your Puppet master, you will need to ensure that any such credentials are supplied and other assumptions are met.
|
||||
|
||||
## Environment
|
||||
|
||||
When the ENC is executed, the following environment variables are set to match the environment of the shell in which octocatalog-diff executes:
|
||||
|
||||
- `HOME`
|
||||
- `PATH`
|
||||
- `PWD` (set to the temporary directory as previously described)
|
||||
|
||||
No other environment variables are passed from the shell. If you wish to pass additional environment variables, you must explicitly list them with the `--pass-env-vars` CLI flag or `settings[:pass_env_vars]` array in your configuration file.
|
||||
|
||||
As an example, consider that your ENC is written in Python, and needs the `PYTHONPATH` variable set to `/usr/local/lib/python-custom`. Even if this environment variable is set when octocatalog-diff is run, it will not be available to the ENC script. You may pass the variable via the command line:
|
||||
|
||||
```
|
||||
octocatalog-diff --pass-env-vars PYTHONPATH ...
|
||||
```
|
||||
|
||||
Or you may specify it in your configuration file:
|
||||
|
||||
```
|
||||
settings[:pass_env_vars] = [ 'PYTHONPATH' ]
|
||||
```
|
||||
|
||||
If you wish to specify multiple environment variables to pass:
|
||||
|
||||
```
|
||||
octocatalog-diff --pass-env-vars PYTHONPATH,SECONDVAR,THIRDVAR ...
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```
|
||||
settings[:pass_env_vars] = [ 'PYTHONPATH', 'SECONDVAR', 'THIRDVAR' ]
|
||||
```
|
||||
|
|
|
@ -56,6 +56,7 @@ module OctocatalogDiff
|
|||
# In this case, you desire to strip `/etc/puppetlabs/code` from the beginning of the path,
|
||||
# in order that octocatalog-diff can find your hiera datafiles in the compilation
|
||||
# location, which is {temporary directory}/environments/production/hieradata.
|
||||
# If you use this, be sure that you do NOT include a trailing slash!
|
||||
#
|
||||
# More: https://github.com/github/octocatalog-diff/blob/master/doc/configuration-hiera.md
|
||||
##############################################################################################
|
||||
|
|
|
@ -197,7 +197,7 @@ module OctocatalogDiff
|
|||
|
||||
# Munge datadir in hiera config file
|
||||
obj = YAML.load_file(file_src)
|
||||
%w(yaml json).each do |key|
|
||||
(obj[:backends] || %w(yaml json)).each do |key|
|
||||
next unless obj.key?(key.to_sym)
|
||||
if options[:hiera_path_strip].is_a?(String)
|
||||
next if obj[key.to_sym][:datadir].nil?
|
||||
|
|
|
@ -21,6 +21,45 @@ module OctocatalogDiff
|
|||
end
|
||||
end
|
||||
|
||||
# Internal method: Locate a file that is referenced at puppet:///modules/xxx/yyy using the
|
||||
# module path that is specified within the environment.conf file (assuming the default 'modules'
|
||||
# directory doesn't exist or the module isn't found in there). If the file can't be found then
|
||||
# this returns nil which may trigger an error.
|
||||
# @param src [String] A file reference: puppet:///modules/xxx/yyy
|
||||
# @param modulepaths [Array] Cached module path
|
||||
# @return [String] File system path to referenced file
|
||||
def self.file_path(src, modulepaths)
|
||||
unless src =~ %r{^puppet:///modules/([^/]+)/(.+)}
|
||||
raise ArgumentError, "Bad parameter source #{src}"
|
||||
end
|
||||
|
||||
path = File.join(Regexp.last_match(1), 'files', Regexp.last_match(2))
|
||||
modulepaths.each do |mp|
|
||||
file = File.join(mp, path)
|
||||
return file if File.exist?(file)
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
# Internal method: Parse environment.conf to find the modulepath
|
||||
# @param compilation_dir [String] Compilation directory
|
||||
# @return [Array] Module paths
|
||||
def self.module_path(compilation_dir)
|
||||
environment_conf = File.join(compilation_dir, 'environment.conf')
|
||||
unless File.file?(environment_conf)
|
||||
return [File.join(compilation_dir, 'modules')]
|
||||
end
|
||||
|
||||
# This doesn't support multi-line, continuations with backslash, etc.
|
||||
# Does it need to??
|
||||
if File.read(environment_conf) =~ /^modulepath\s*=\s*(.+)/
|
||||
Regexp.last_match(1).split(/:/).map(&:strip).reject { |x| x =~ /^\$/ }.map { |x| File.join(compilation_dir, x) }
|
||||
else
|
||||
[File.join(compilation_dir, 'modules')]
|
||||
end
|
||||
end
|
||||
|
||||
# Internal method: Static method to convert file resources. The compilation directory is
|
||||
# required, or else this is a no-op. The passed-in array of resources is modified by this method.
|
||||
# @param resources [Array<Hash>] Array of catalog resources
|
||||
|
@ -34,18 +73,15 @@ module OctocatalogDiff
|
|||
# that compilation_dir/environments/production is pointing at the right place). Otherwise, try to find
|
||||
# compilation_dir/modules. If neither of those exist, this code can't run.
|
||||
env_dir = File.join(compilation_dir, 'environments', 'production')
|
||||
unless File.directory?(File.join(env_dir, 'modules'))
|
||||
return unless File.directory?(File.join(compilation_dir, 'modules'))
|
||||
env_dir = compilation_dir
|
||||
end
|
||||
modulepaths = module_path(env_dir) + module_path(compilation_dir)
|
||||
modulepaths.select! { |x| File.directory?(x) }
|
||||
return if modulepaths.empty?
|
||||
|
||||
# Modify the resources
|
||||
# At least one existing module path was found! Run the code to modify the resources.
|
||||
resources.map! do |resource|
|
||||
if resource_convertible?(resource)
|
||||
# Parse the 'source' parameter into a file on disk
|
||||
src = resource['parameters']['source']
|
||||
raise "Bad parameter source #{src}" unless src =~ %r{^puppet:///modules/([^/]+)/(.+)}
|
||||
path = File.join(env_dir, 'modules', Regexp.last_match(1), 'files', Regexp.last_match(2))
|
||||
path = file_path(resource['parameters']['source'], modulepaths)
|
||||
raise Errno::ENOENT, "Unable to resolve '#{resource['parameters']['source']}'!" if path.nil?
|
||||
|
||||
if File.file?(path)
|
||||
# If the file is found, read its content. If the content is all ASCII, substitute it into
|
||||
|
@ -60,7 +96,10 @@ module OctocatalogDiff
|
|||
# However, the fact that we found *something* at this location indicates that the catalog
|
||||
# is probably correct. Hence, the very general .exist? check.
|
||||
else
|
||||
raise Errno::ENOENT, "Unable to find '#{src}' at #{path}!"
|
||||
# This is probably a bug
|
||||
# :nocov:
|
||||
raise "Unable to find '#{resource['parameters']['source']}' at #{path}!"
|
||||
# :nocov:
|
||||
end
|
||||
end
|
||||
resource
|
||||
|
|
|
@ -68,13 +68,17 @@ module OctocatalogDiff
|
|||
|
||||
# Returns a hash of parameters for each supported version of the Puppet Server Catalog API.
|
||||
# @return [Hash] Hash of parameters
|
||||
#
|
||||
# Note: The double escaping of the facts here is implemented to correspond to a long standing
|
||||
# bug in the Puppet code. See https://github.com/puppetlabs/puppet/pull/1818 and
|
||||
# https://docs.puppet.com/puppet/latest/http_api/http_catalog.html#parameters for explanation.
|
||||
def puppet_catalog_api
|
||||
{
|
||||
2 => {
|
||||
url: "https://#{@options[:puppet_master]}/#{@options[:branch]}/catalog/#{@node}",
|
||||
parameters: {
|
||||
'facts_format' => 'pson',
|
||||
'facts' => @facts.fudge_timestamp.without('trusted').to_pson,
|
||||
'facts' => CGI.escape(@facts.fudge_timestamp.without('trusted').to_pson),
|
||||
'transaction_uuid' => SecureRandom.uuid
|
||||
}
|
||||
},
|
||||
|
@ -83,7 +87,7 @@ module OctocatalogDiff
|
|||
parameters: {
|
||||
'environment' => @options[:branch],
|
||||
'facts_format' => 'pson',
|
||||
'facts' => @facts.fudge_timestamp.without('trusted').to_pson,
|
||||
'facts' => CGI.escape(@facts.fudge_timestamp.without('trusted').to_pson),
|
||||
'transaction_uuid' => SecureRandom.uuid
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
{
|
||||
"document_type": "Catalog",
|
||||
"tags": ["settings","test"],
|
||||
"name": "my.rspec.node",
|
||||
"version": "production",
|
||||
"environment": "production",
|
||||
"resources": [
|
||||
{
|
||||
"type": "Stage",
|
||||
"title": "main",
|
||||
"tags": ["stage"],
|
||||
"exported": false,
|
||||
"parameters": {
|
||||
"name": "main"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "Class",
|
||||
"title": "Settings",
|
||||
"tags": ["class","settings"],
|
||||
"exported": false
|
||||
},
|
||||
{
|
||||
"type": "File",
|
||||
"title": "/tmp/foo",
|
||||
"tags": ["file","class"],
|
||||
"file": "/x/modules/modulestest/manifests/init.pp",
|
||||
"line": 37,
|
||||
"exported": false,
|
||||
"parameters": {
|
||||
"backup": false,
|
||||
"mode": "0440",
|
||||
"owner": "root",
|
||||
"group": "root",
|
||||
"source": "puppet:///modules/modulestest/tmp/modulestest"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "File",
|
||||
"title": "/tmp/foobaz",
|
||||
"tags": ["file","class"],
|
||||
"file": "/x/modules/modulestest/manifests/init.pp",
|
||||
"line": 37,
|
||||
"exported": false,
|
||||
"parameters": {
|
||||
"backup": false,
|
||||
"ensure": "directory",
|
||||
"mode": "0755",
|
||||
"owner": "root",
|
||||
"group": "root",
|
||||
"source": "puppet:///modules/modulestest/foo"
|
||||
}
|
||||
}
|
||||
],
|
||||
"classes": [
|
||||
"test"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"apt_update_last_success":1458162123,
|
||||
"architecture":"amd64",
|
||||
"datacenter":"xyz",
|
||||
"fqdn":"rspec-node.xyz.github.net",
|
||||
"math":"1+2=3",
|
||||
"percent":"25%20=5",
|
||||
"_timestamp":"2014-12-02 14:56:20 -0600"
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#--- !ruby/object:Puppet::Node::Facts
|
||||
name: rspec-node.xyz.github.net
|
||||
values:
|
||||
apt_update_last_success: 1458162123
|
||||
architecture: amd64
|
||||
datacenter: xyz
|
||||
fqdn: rspec-node.xyz.github.net
|
||||
math: "1+2=3"
|
||||
percent: "25%20=5"
|
||||
"_timestamp": 2014-12-02 12:56:20.865795 -08:00
|
||||
expiration: 2014-12-02 13:11:20.521667 -08:00
|
|
@ -0,0 +1,20 @@
|
|||
---
|
||||
:backends:
|
||||
- eyaml
|
||||
- yaml
|
||||
- json
|
||||
:yaml:
|
||||
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
|
||||
:eyaml:
|
||||
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
|
||||
:json:
|
||||
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
|
||||
:hierarchy:
|
||||
- servers/%{::fqdn}
|
||||
- datacenter/%{::datacenter}
|
||||
- platform/%{::virtual}
|
||||
- os/%{::operatingsystem}/%{::lsbdistcodename}
|
||||
- os/%{::operatingsystem}
|
||||
- common
|
||||
:merge_behavior: deeper
|
||||
:logger: console
|
|
@ -0,0 +1 @@
|
|||
modulepath=modules:site:$basemodulepath
|
|
@ -0,0 +1,4 @@
|
|||
node default {
|
||||
include modulestest
|
||||
include sitetest
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
# Hi
|
|
@ -0,0 +1 @@
|
|||
Modules Test
|
|
@ -0,0 +1,11 @@
|
|||
class modulestest {
|
||||
file { '/tmp/modulestest':
|
||||
source => 'puppet:///modules/modulestest/tmp/modulestest',
|
||||
}
|
||||
|
||||
file { '/tmp/foobaz':
|
||||
ensure => directory,
|
||||
source => 'puppet:///modules/modulestest/foo',
|
||||
recurse => true,
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Site Test
|
|
@ -0,0 +1,5 @@
|
|||
class sitetest {
|
||||
file { '/tmp/sitetest':
|
||||
source => 'puppet:///modules/sitetest/tmp/sitetest',
|
||||
}
|
||||
}
|
|
@ -118,7 +118,7 @@ describe 'convert file resources' do
|
|||
expect(result[:exitcode]).to eq(-1)
|
||||
expect(result[:exception]).to be_a_kind_of(OctocatalogDiff::CatalogDiff::Cli::Catalogs::CatalogError)
|
||||
expect(result[:exception].message).to match(/failed to compile with Errno::ENOENT/)
|
||||
expect(result[:exception].message).to match(%r{Unable to find 'puppet:///modules/test/foo-new' at})
|
||||
expect(result[:exception].message).to match(%r{Unable to resolve 'puppet:///modules/test/foo-new'})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
require_relative 'integration_helper'
|
||||
require OctocatalogDiff::Spec.require_path('/catalog')
|
||||
|
||||
describe 'multiple module paths' do
|
||||
# Make sure the catalog compiles correctly, without using any of the file
|
||||
# conversion resources. If the catalog doesn't compile correctly this could
|
||||
# indicate a problem that lies somewhere other than the comparison code.
|
||||
describe 'catalog only' do
|
||||
before(:all) do
|
||||
@result = OctocatalogDiff::Integration.integration(
|
||||
spec_fact_file: 'facts.yaml',
|
||||
spec_repo: 'modulepath',
|
||||
argv: [
|
||||
'--catalog-only',
|
||||
'-n', 'rspec-node.github.net',
|
||||
'--no-compare-file-text'
|
||||
]
|
||||
)
|
||||
@catalog = OctocatalogDiff::Catalog.new(
|
||||
backend: :json,
|
||||
json: @result[:output]
|
||||
)
|
||||
end
|
||||
|
||||
it 'should compile' do
|
||||
expect(@result[:exitcode]).not_to eq(-1), OctocatalogDiff::Integration.format_exception(@result)
|
||||
end
|
||||
|
||||
it 'should be a valid catalog' do
|
||||
pending 'catalog failed to compile' if @result[:exitcode] == -1
|
||||
expect(@catalog.valid?).to eq(true)
|
||||
end
|
||||
|
||||
it 'should have expected resources in catalog' do
|
||||
pending 'catalog was invalid' unless @catalog.valid?
|
||||
expect(@catalog.resources).to be_a_kind_of(Array)
|
||||
|
||||
mf = @catalog.resource(type: 'File', title: '/tmp/modulestest')
|
||||
expect(mf).to be_a_kind_of(Hash)
|
||||
expect(mf['parameters']).to eq('source' => 'puppet:///modules/modulestest/tmp/modulestest')
|
||||
|
||||
sf = @catalog.resource(type: 'File', title: '/tmp/sitetest')
|
||||
expect(sf).to be_a_kind_of(Hash)
|
||||
expect(sf['parameters']).to eq('source' => 'puppet:///modules/sitetest/tmp/sitetest')
|
||||
end
|
||||
end
|
||||
|
||||
# Test the file comparison feature itself here in its various iterations.
|
||||
describe 'file comparison feature' do
|
||||
before(:each) do
|
||||
@from_dir = Dir.mktmpdir
|
||||
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath'), @from_dir
|
||||
|
||||
@to_dir = Dir.mktmpdir
|
||||
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath'), @to_dir
|
||||
|
||||
file1 = File.join(@to_dir, 'modulepath', 'modules', 'modulestest', 'files', 'tmp', 'modulestest')
|
||||
File.open(file1, 'w') { |f| f.write("New content of modulestest\n") }
|
||||
|
||||
file2 = File.join(@to_dir, 'modulepath', 'site', 'sitetest', 'files', 'tmp', 'sitetest')
|
||||
File.open(file2, 'w') { |f| f.write("New content of sitetest\n") }
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
OctocatalogDiff::Spec.clean_up_tmpdir(@from_dir)
|
||||
OctocatalogDiff::Spec.clean_up_tmpdir(@to_dir)
|
||||
end
|
||||
|
||||
let(:module_answer) do
|
||||
['~',
|
||||
"File\f/tmp/modulestest\fparameters\fcontent",
|
||||
"Modules Test\n",
|
||||
"New content of modulestest\n"]
|
||||
end
|
||||
|
||||
let(:site_answer) do
|
||||
[
|
||||
'~',
|
||||
"File\f/tmp/sitetest\fparameters\fcontent",
|
||||
"Site Test\n",
|
||||
"New content of sitetest\n"
|
||||
]
|
||||
end
|
||||
|
||||
context 'with environment.conf' do
|
||||
# The environment.conf is a fixture within the repository so there is no need
|
||||
# to create it or manipulate it.
|
||||
before(:each) do
|
||||
@result = OctocatalogDiff::Integration.integration(
|
||||
spec_fact_file: 'facts.yaml',
|
||||
argv: [
|
||||
'-n', 'rspec-node.github.net',
|
||||
'--bootstrapped-from-dir', File.join(@from_dir, 'modulepath'),
|
||||
'--bootstrapped-to-dir', File.join(@to_dir, 'modulepath')
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
it 'should compile catalogs and compute differences' do
|
||||
expect(@result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(@result)
|
||||
expect(@result[:diffs]).to be_a_kind_of(Array)
|
||||
expect(@result[:diffs].size).to eq(2)
|
||||
expect(OctocatalogDiff::Spec.array_contains_partial_array?(@result[:diffs], module_answer)).to eq(true)
|
||||
expect(OctocatalogDiff::Spec.array_contains_partial_array?(@result[:diffs], site_answer)).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'without environment.conf in one directory' do
|
||||
before(:each) do
|
||||
FileUtils.rm_f File.join(@from_dir, 'modulepath', 'environment.conf')
|
||||
FileUtils.mv File.join(@from_dir, 'modulepath', 'site', 'sitetest'), File.join(@from_dir, 'modulepath', 'modules')
|
||||
@result = OctocatalogDiff::Integration.integration(
|
||||
spec_fact_file: 'facts.yaml',
|
||||
argv: [
|
||||
'-n', 'rspec-node.github.net',
|
||||
'--bootstrapped-from-dir', File.join(@from_dir, 'modulepath'),
|
||||
'--bootstrapped-to-dir', File.join(@to_dir, 'modulepath')
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
it 'should compile catalogs and compute differences' do
|
||||
expect(@result[:exitcode]).to eq(2), OctocatalogDiff::Integration.format_exception(@result)
|
||||
expect(@result[:diffs]).to be_a_kind_of(Array)
|
||||
expect(@result[:diffs].size).to eq(2)
|
||||
expect(OctocatalogDiff::Spec.array_contains_partial_array?(@result[:diffs], module_answer)).to eq(true)
|
||||
expect(OctocatalogDiff::Spec.array_contains_partial_array?(@result[:diffs], site_answer)).to eq(true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -45,7 +45,7 @@ module OctocatalogDiff
|
|||
end
|
||||
|
||||
def self.facts_match(body)
|
||||
facts = JSON.parse(parse_body(body)['facts'])
|
||||
facts = JSON.parse(CGI.unescape(parse_body(body)['facts']))
|
||||
facts.delete('_timestamp')
|
||||
desired_facts = {
|
||||
'name' => 'rspec-node.xyz.github.net',
|
||||
|
|
|
@ -231,6 +231,27 @@ describe OctocatalogDiff::CatalogUtil::BuildDir do
|
|||
expect(logger_str.string).to match(%r{WARNING: Hiera datadir for yaml.+/environments/production/aksdfjlkfjk})
|
||||
end
|
||||
end
|
||||
|
||||
context 'using other backends' do
|
||||
it 'should rewrite all datadir' do
|
||||
options = default_options.merge(
|
||||
hiera_config: OctocatalogDiff::Spec.fixture_path('repos/default/config/hiera-other-backends.yaml'),
|
||||
hiera_path: 'hieradata'
|
||||
)
|
||||
logger, logger_str = OctocatalogDiff::Spec.setup_logger
|
||||
testobj = OctocatalogDiff::CatalogUtil::BuildDir.new(options, logger)
|
||||
hiera_yaml = File.join(testobj.tempdir, 'hiera.yaml')
|
||||
expect(File.file?(hiera_yaml)).to eq(true)
|
||||
hiera_cfg = YAML.load_file(hiera_yaml)
|
||||
expect(hiera_cfg[:backends]).to eq(%w(eyaml yaml json))
|
||||
expect(hiera_cfg[:yaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
|
||||
expect(hiera_cfg[:eyaml]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
|
||||
expect(hiera_cfg[:json]).to eq(datadir: File.join(testobj.tempdir, 'environments', 'production', 'hieradata'))
|
||||
expect(logger_str.string).not_to match(/Hiera datadir for yaml doesn't seem to exist/)
|
||||
expect(logger_str.string).not_to match(/Hiera datadir for eyaml doesn't seem to exist/)
|
||||
expect(logger_str.string).not_to match(/Hiera datadir for json doesn't seem to exist/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#install_fact_file' do
|
||||
|
|
|
@ -11,6 +11,89 @@ describe OctocatalogDiff::CatalogUtil::FileResources do
|
|||
OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path(path)))
|
||||
end
|
||||
|
||||
describe '#file_path' do
|
||||
it 'should raise ArgumentError for unexpected format of file name' do
|
||||
src = 'asldfkjwoeifjslakfj'
|
||||
expect do
|
||||
OctocatalogDiff::CatalogUtil::FileResources.file_path(src, [])
|
||||
end.to raise_error(ArgumentError, /Bad parameter source/)
|
||||
end
|
||||
|
||||
it 'should return path if file is found' do
|
||||
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(true)
|
||||
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
|
||||
expect(result).to eq('/a/foo/files/bar')
|
||||
end
|
||||
|
||||
it 'should return nil if file is not found' do
|
||||
allow(File).to receive(:exist?).with('/a/foo/files/bar').and_return(false)
|
||||
result = OctocatalogDiff::CatalogUtil::FileResources.file_path('puppet:///modules/foo/bar', ['/a'])
|
||||
expect(result).to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#module_path' do
|
||||
it 'should return "modules" only when environment.conf is missing' do
|
||||
allow(File).to receive(:file?).with('/a/environment.conf').and_return(false)
|
||||
result = OctocatalogDiff::CatalogUtil::FileResources.module_path('/a')
|
||||
expect(result).to eq(['/a/modules'])
|
||||
end
|
||||
|
||||
it 'should return "modules" if environment.conf has no modulepath' do
|
||||
allow(File).to receive(:file?).with('/a/environment.conf').and_return(true)
|
||||
allow(File).to receive(:read).with('/a/environment.conf').and_return('foo')
|
||||
result = OctocatalogDiff::CatalogUtil::FileResources.module_path('/a')
|
||||
expect(result).to eq(['/a/modules'])
|
||||
end
|
||||
|
||||
it 'should return proper entries from environment.conf modulepath' do
|
||||
allow(File).to receive(:file?).with('/a/environment.conf').and_return(true)
|
||||
allow(File).to receive(:read).with('/a/environment.conf').and_return('modulepath=modules:site:$basemoduledir')
|
||||
result = OctocatalogDiff::CatalogUtil::FileResources.module_path('/a')
|
||||
expect(result).to eq(['/a/modules', '/a/site'])
|
||||
end
|
||||
end
|
||||
|
||||
context 'with mixed files and directories' do
|
||||
describe '#convert_file_resources' do
|
||||
before(:each) do
|
||||
@tmpdir = Dir.mktmpdir
|
||||
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath/manifests'), @tmpdir
|
||||
FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/modulepath/modules'), @tmpdir
|
||||
Dir.mkdir File.join(@tmpdir, 'environments')
|
||||
File.symlink @tmpdir, File.join(@tmpdir, 'environments', 'production')
|
||||
File.open(File.join(@tmpdir, 'manifests', 'site.pp'), 'w') { |f| f.write "include modulestest\n" }
|
||||
|
||||
@obj = catalog_from_fixture('catalogs/catalog-modules-test.json')
|
||||
@obj.compilation_dir = @tmpdir
|
||||
@resources_save = @obj.resources.dup
|
||||
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(@obj)
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
FileUtils.remove_entry_secure @tmpdir if File.directory?(@tmpdir)
|
||||
end
|
||||
|
||||
it 'should populate content of a file' do
|
||||
r = @obj.resources.select { |x| x['type'] == 'File' && x['title'] == '/tmp/foo' }
|
||||
expect(r).to be_a_kind_of(Array)
|
||||
expect(r.size).to eq(1)
|
||||
expect(r.first).to be_a_kind_of(Hash)
|
||||
expect(r.first['parameters'].key?('source')).to eq(false)
|
||||
expect(r.first['parameters']['content']).to eq("Modules Test\n")
|
||||
end
|
||||
|
||||
it 'should leave a directory unmodified' do
|
||||
r = @obj.resources.select { |x| x['type'] == 'File' && x['title'] == '/tmp/foobaz' }
|
||||
expect(r).to be_a_kind_of(Array)
|
||||
expect(r.size).to eq(1)
|
||||
expect(r.first).to be_a_kind_of(Hash)
|
||||
expect(r.first['parameters'].key?('content')).to eq(false)
|
||||
expect(r.first['parameters']['source']).to eq('puppet:///modules/modulestest/foo')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#convert_file_resources' do
|
||||
before(:each) do
|
||||
@tmpdir = Dir.mktmpdir
|
||||
|
@ -97,7 +180,7 @@ describe OctocatalogDiff::CatalogUtil::FileResources do
|
|||
# Perform test
|
||||
expect do
|
||||
OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
|
||||
end.to raise_error(Errno::ENOENT, %r{Unable to find 'puppet:///modules/this/does/not/exist'})
|
||||
end.to raise_error(Errno::ENOENT, %r{Unable to resolve 'puppet:///modules/this/does/not/exist'})
|
||||
end
|
||||
|
||||
it 'should return original if compilation_dir is not a string' do
|
||||
|
|
|
@ -11,7 +11,7 @@ describe OctocatalogDiff::Catalog::PuppetMaster do
|
|||
node: 'foo',
|
||||
branch: 'foobranch',
|
||||
puppet_master: 'fake-puppetmaster.non-existent-domain.com',
|
||||
fact_file: OctocatalogDiff::Spec.fixture_path('facts/facts.yaml')
|
||||
fact_file: OctocatalogDiff::Spec.fixture_path('facts/facts_esc.yaml')
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -99,9 +99,12 @@ describe OctocatalogDiff::Catalog::PuppetMaster do
|
|||
end
|
||||
|
||||
it 'should post the correct facts to HTTParty' do
|
||||
answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('facts/facts.json')))
|
||||
answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('facts/facts_esc.json')))
|
||||
answer.delete('_timestamp')
|
||||
result = JSON.parse(@post_data['facts'])['values']
|
||||
# An extra 'unescape' is here because the facts are double escaped.
|
||||
# See https://docs.puppet.com/puppet/latest/http_api/http_catalog.html#parameters
|
||||
# and https://github.com/puppetlabs/puppet/pull/1818
|
||||
result = JSON.parse(CGI.unescape(@post_data['facts']))['values']
|
||||
expect(result).to eq(answer)
|
||||
end
|
||||
|
||||
|
@ -118,8 +121,8 @@ describe OctocatalogDiff::Catalog::PuppetMaster do
|
|||
it 'should log correctly' do
|
||||
logs = @logger_str.string
|
||||
expect(logs).to match(/Start retrieving facts for foo from OctocatalogDiff::Catalog::PuppetMaster/)
|
||||
expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts.yaml})
|
||||
expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts.yaml})
|
||||
expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts_esc.yaml})
|
||||
expect(logs).to match(%r{Retrieving facts from.*fixtures/facts/facts_esc.yaml})
|
||||
|
||||
answer = Regexp.new("Retrieve catalog from #{api_url[api_version]} environment foobranch")
|
||||
expect(logs).to match(answer)
|
||||
|
|
Загрузка…
Ссылка в новой задаче