Skip to content
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

Adds some basic tests for functions #90

Merged
merged 3 commits into from Apr 14, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion .fixtures.yml
Expand Up @@ -11,4 +11,9 @@ fixtures:
debug: 'https://github.com/nwops/puppet-debug'
format: 'https://github.com/voxpupuli/puppet-format'
symlinks:
"peadm": "#{source_dir}"
"peadm": "#{source_dir}"
out: "#{source_dir}/.bundle/gems/bolt-1.49.0/bolt-modules/out"
ctrl: "#{source_dir}/.bundle/gems/bolt-1.49.0/bolt-modules/ctrl"
file: "#{source_dir}/.bundle/gems/bolt-1.49.0/bolt-modules/file"
system: "#{source_dir}/.bundle/gems/bolt-1.49.0/bolt-modules/system"
boltlib: "#{source_dir}/.bundle/gems/bolt-1.49.0/bolt-modules/boltlib"
6 changes: 3 additions & 3 deletions .rubocop.yml
Expand Up @@ -4,7 +4,7 @@ require:
- rubocop-i18n
AllCops:
DisplayCopNames: true
TargetRubyVersion: '2.1'
TargetRubyVersion: '2.4'
Include:
- "./**/*.rb"
Exclude:
Expand Down Expand Up @@ -67,7 +67,7 @@ Style/TrailingCommaInArguments:
Description: Prefer always trailing comma on multiline argument lists. This makes
diffs, and re-ordering nicer.
EnforcedStyleForMultiline: comma
Style/TrailingCommaInLiteral:
Style/TrailingCommaInArrayLiteral:
Description: Prefer always trailing comma on multiline literals. This makes diffs,
and re-ordering nicer.
EnforcedStyleForMultiline: comma
Expand All @@ -90,7 +90,7 @@ Style/StringMethods:
Enabled: true
Layout/EndOfLine:
Enabled: false
Layout/IndentHeredoc:
Layout/HeredocIndentation:
Enabled: false
Metrics/AbcSize:
Enabled: false
Expand Down
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

2 changes: 1 addition & 1 deletion documentation/provision.md
Expand Up @@ -6,7 +6,7 @@ The peadm provisioning plan creates base reference implementation. Once a base s

## Reference Architectures

When provisioning a new PE stack using peadm, there are several different host parameters which can be specified. At a minimum, you must always specify the Puppet master. Depending on which architecture you are deploying, other host parameters may be needed as well. The following is a list of the architectures peadm can provision.
When provisioning a new PE stack using peadm, there are several different host parameters which can be specified. At a minimum, you must always specify the master parameter. Depending on which architecture you are deploying, other host parameters may be needed as well. The following is a list of the architectures peadm can provision and the required parameters.

* Standard
- master
Expand Down
2 changes: 2 additions & 0 deletions examples/ruby_task.rb
@@ -1,4 +1,6 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

#
# rubocop:disable Style/GlobalVars
require 'json'
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/functions/peadm/file_content_upload.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'tempfile'

Puppet::Functions.create_function(:'peadm::file_content_upload') do
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/functions/peadm/node_manager_yaml_location.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Functions.create_function(:'peadm::node_manager_yaml_location') do
dispatch :nm_yaml_location do
end
Expand Down
2 changes: 2 additions & 0 deletions spec/acceptance/peadm_spec.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'
# @summary: default test does nothing
def test_peadm
Expand Down
2 changes: 2 additions & 0 deletions spec/functions/convert_hash_spec.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::convert_hash' do
Expand Down
2 changes: 2 additions & 0 deletions spec/functions/convert_status_spec.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::convert_status' do
Expand Down
2 changes: 2 additions & 0 deletions spec/functions/determine_status_spec.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::determine_status' do
Expand Down
14 changes: 14 additions & 0 deletions spec/functions/fail_on_transport_spec.rb
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::fail_on_transport' do
let(:nodes) do
'some_value_goes_here'
end
let(:transport) do
'some_value_goes_here'
end

xit { is_expected.to run.with_params(nodes, transport).and_return('some_value') }
end
17 changes: 17 additions & 0 deletions spec/functions/file_or_content_spec.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::file_or_content' do
let(:param_name) do
'some_value_goes_here'
end
let(:file) do
'some_value_goes_here'
end
let(:content) do
'some_value_goes_here'
end

xit { is_expected.to run.with_params(param_name, file, content).and_return('some_value') }
end
11 changes: 11 additions & 0 deletions spec/functions/flatten_compact_spec.rb
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::flatten_compact' do
let(:input) do
[1, 2, 3, nil, 4, 5, 6, nil, 'ds', '']
end

it { is_expected.to run.with_params(input).and_return([1, 2, 3, 4, 5, 6, 'ds', '']) }
end
32 changes: 32 additions & 0 deletions spec/functions/generate_pe_conf_spec.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'peadm::generate_pe_conf' do
let(:settings) do
{ 'console_admin_password' => 'puppetlabs' }
end
let(:output) do
JSON.pretty_generate('console_admin_password' => 'puppetlabs',
'puppet_enterprise::profile::console::java_args' => {
'Xms' => '256m',
'Xmx' => '768m'
},
'puppet_enterprise::profile::master::java_args' => {
'Xms' => '512m',
'Xmx' => '2048m'
},
'puppet_enterprise::profile::orchestrator::java_args' => {
'Xms' => '256m',
'Xmx' => '768m'
},
'puppet_enterprise::profile::puppetdb::java_args' => {
'Xms' => '256m',
'Xmx' => '768m'
})
end

it do
is_expected.to run.with_params(settings).and_return(%r{puppetlabs})
end
end
16 changes: 16 additions & 0 deletions spec/functions/get_targets_spec.rb
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'spec_helper'
# TODO: test the error case, however due to an issue with boltspec
# and functions we cannot do this right now.
# https://github.com/puppetlabs/bolt/issues/1688
describe 'peadm::get_targets' do
let(:spec) do
'some_value_goes_here'
end
let(:count) do
'some_value_goes_here'
end

xit { is_expected.to run.with_params(spec, count).and_return('some_value') }
end
16 changes: 16 additions & 0 deletions spec/functions/oid_spec.rb
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'spec_helper'
# TODO: test the error case, however due to an issue with boltspec
# and functions we cannot do this right now.
# https://github.com/puppetlabs/bolt/issues/1688
describe 'peadm::oid' do
it { is_expected.to run.with_params('peadm_role').and_return('1.3.6.1.4.1.34380.1.1.9812') }
it { is_expected.to run.with_params('peadm_availability_group').and_return('1.3.6.1.4.1.34380.1.1.9813') }
it { is_expected.to run.with_params('pp_application').and_return('1.3.6.1.4.1.34380.1.1.8') }
it { is_expected.to run.with_params('pp_cluster').and_return('1.3.6.1.4.1.34380.1.1.16') }
it do
is_expected.to run.with_params('bogus')
.and_raise_error(Puppet::PreformattedError, %r{No peadm OID for bogus})
end
end
14 changes: 14 additions & 0 deletions spec/functions/target_name_spec.rb
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require 'spec_helper'

# TODO: test the error case, however due to an issue with boltspec
# and functions we cannot do this right now.
# https://github.com/puppetlabs/bolt/issues/1688
describe 'peadm::target_name' do
let(:target) do
['test-vm.puppet.vm']
end

xit { is_expected.to run.with_params(target).and_return('some_value') }
end
80 changes: 80 additions & 0 deletions spec/functions/validate_architecture_spec.rb
@@ -0,0 +1,80 @@
# frozen_string_literal: true

require 'spec_helper'
# TODO: test the error case, however due to an issue with boltspec
# and functions we cannot do this right now.
# https://github.com/puppetlabs/bolt/issues/1688

describe 'peadm::validate_architecture' do
let(:pre_condition) do
'type TargetSpec = Variant[String[1], Target, Array[TargetSpec]]'
end
let(:master_host) do
'puppet-std.puppet.vm'
end
let(:master_replica_host) do
'pup-replica.puppet.vm'
end
let(:puppetdb_database_host) do
'pup-db.puppet.vm'
end
let(:puppetdb_database_replica_host) do
'pup-db-replica.puppet.vm'
end
let(:compiler_hosts) do
'pup-c1.puppet.vm'
end

it {
is_expected.to run.with_params(master_host)
.and_return('high-availability' => false, 'architecture' => 'standard')
}
it {
is_expected.to run.with_params(master_host, master_replica_host)
.and_return('high-availability' => true, 'architecture' => 'standard')
}

it do
is_expected.to run.with_params(
master_host,
master_replica_host,
nil,
nil,
compiler_hosts,
)
.and_return('high-availability' => true, 'architecture' => 'large')
end

it do
is_expected.to run.with_params(
master_host,
nil,
nil,
nil,
compiler_hosts,
)
.and_return('high-availability' => false, 'architecture' => 'large')
end

it do
is_expected.to run.with_params(
master_host,
master_replica_host,
puppetdb_database_host,
puppetdb_database_replica_host,
compiler_hosts,
)
.and_return('high-availability' => true, 'architecture' => 'extra-large')
end

it do
is_expected.to run.with_params(
master_host,
nil,
puppetdb_database_host,
nil,
compiler_hosts,
)
.and_return('high-availability' => false, 'architecture' => 'extra-large')
end
end
2 changes: 2 additions & 0 deletions spec/plans/status_disable.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'
# https://github.com/puppetlabs/bolt/blob/master/lib/bolt_spec/plans.rb

Expand Down
6 changes: 4 additions & 2 deletions spec/spec_helper.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'

Expand All @@ -7,12 +9,12 @@

default_facts = {
puppetversion: Puppet.version,
facterversion: Facter.version,
facterversion: Facter.version
}

default_fact_files = [
File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')),
File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')),
File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml'))
]

default_fact_files.each do |f|
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper_acceptance_local.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppet_litmus'
require 'singleton'

Expand Down
20 changes: 20 additions & 0 deletions spec/spec_helper_local.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppet'

if Gem::Version.new(Puppet.version) >= Gem::Version.new('6.0.0')
Expand All @@ -12,3 +14,21 @@
warn '=== bolt tests will not run; ensure bolt gem is installed (requires Puppet 6+)'
end
end

# Bolt has some hidden puppet modules inside the bolt codebase that need to be added here
# for proper testing and availablity with other tooling. The code below will
# locate the bolt gem dir and create fixtures for each of the bolt modules.

spec = Gem::Specification.latest_specs.find { |s| s.name.eql?('bolt') }
if spec
bolt_modules = File.join(spec.full_gem_path, 'bolt-modules')
Dir.glob(File.join(bolt_modules, '*')).each do |dir|
mod_name = File.basename(dir)
mod_path = File.expand_path(File.join(__dir__, 'fixtures', 'modules', mod_name))
FileUtils.ln_sf(dir, mod_path) unless File.exist?(mod_path)
end

RSpec.configure do |c|
c.basemodulepath = bolt_modules
end
end
4 changes: 3 additions & 1 deletion tasks/rbac_token.rb
@@ -1,4 +1,6 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

#
# rubocop:disable Style/GlobalVars
require 'net/https'
Expand All @@ -16,7 +18,7 @@
'login' => 'admin',
'password' => $params['password'],
'lifetime' => '1y',
'label' => 'provision-time token',
'label' => 'provision-time token'
}.to_json

http = Net::HTTP.new(uri.host, uri.port)
Expand Down
5 changes: 3 additions & 2 deletions tasks/read_file.rb
@@ -1,4 +1,5 @@
#!/opt/puppetlabs/puppet/bin/ruby
# frozen_string_literal: true

require 'json'

Expand All @@ -8,11 +9,11 @@
rescue StandardError => err
result = {
'content' => nil,
'error' => err.message,
'error' => err.message
}
else
result = {
'content' => content,
'content' => content
}
ensure
puts result.to_json
Expand Down