Skip to content

Instantly share code, notes, and snippets.

@electrofelix
Last active June 15, 2018 09:21
Show Gist options
  • Save electrofelix/ec951282e137a29d6e35bea051a9363f to your computer and use it in GitHub Desktop.
Save electrofelix/ec951282e137a29d6e35bea051a9363f to your computer and use it in GitHub Desktop.
fixtures:
symlinks:
site: "#{source_dir}/puppet/modules"
require 'simplecov-console'
require 'simplecov-json'
SimpleCov.configure do
# add_filter '/spec/'
# add_filter '/puppet/modules/docker'
# add_filter '/puppet/modules/net'
# add_filter '/puppet/modules/user'
#
# track_files('puppet/modules/mod/**/*.pp')
#
coverage_dir 'html'
formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::JSONFormatter,
])
end
#all_files = Dir['puppet/modules/mod/**/*.rb', 'puppet/modules/mod/**/*.pp']
#base_result = {}
#all_files.each do |file|
# absolute = File::expand_path(file)
# lines = File.readlines(absolute, :encoding => 'UTF-8')
# base_result[absolute] = lines.map do |l|
# l.strip!
# l.empty? || l =~ /^end$/ || l[0] == '#' ? nil : 0
# end
#end
#
#SimpleCov.at_exit do
# original = SimpleCov.result.original_result
#
# merged = SimpleCov::RawCoverage.merge_resultsets(original, base_result)
# result = SimpleCov::Result.new(merged)
# result.format!
#end
SimpleCov.start
source "https://rubygems.org"
group :test do
gem "rake", "~> 10.0"
if puppet_gem_version = ENV['PUPPET_GEM_VERSION']
gem "puppet", puppet_gem_version
elsif puppet_git_url = ENV['PUPPET_GIT_URL']
gem "puppet", :git => "#{puppet_git_url}"
else
gem "puppet", "3.8.7"
end
gem "puppet-lint"
gem "puppet-lint-unquoted_string-check"
gem "rspec-puppet", "2.6.11"
gem "puppet-syntax"
gem "puppetlabs_spec_helper"
gem "metadata-json-lint"
gem "rspec", '< 3.2.0' # Support for 1.8.7
gem "rspec-retry"
gem 'simplecov', '>= 0.11.0'
gem 'simplecov-console'
gem 'simplecov-json'
# required for Puppetfile processing by rake
gem 'r10k', '~> 2.5.0'
gem 'ra10ke'
end
group :development do
gem 'rubocop', :require => false # requires at least Ruby 1.9.2
end
# puppet/modules/mod/manifests/server/localuser.pp
define mod::server::localuser(
$ensure = 'present',
$home = undef,
$ssh = {},
$ssh_private_key = undef,
$ssh_public_key = undef,
$type = undef,
) {
$user = $title
if $home != undef {
$user_home = $home
} else {
$user_home = "/home/${user}"
}
group { $user:
ensure => 'present',
} -> user { $user:
ensure => 'present',
home => $user_home,
shell => '/bin/bash',
purge_ssh_keys => true,
managehome => true,
} -> file { "${user_home}/.ssh":
ensure => 'directory',
mode => '0700',
owner => $user,
group => $user,
}
if $ssh_public_key != undef {
ssh_authorized_key { $user:
ensure => present,
user => $user,
key => $ssh_public_key,
type => $type,
require => File["${user_home}/.ssh"],
}
}
if $ssh_private_key {
file { "${user_home}/.ssh/id_rsa":
mode => '0600',
content => $ssh_private_key,
replace => true,
owner => $user,
group => $user,
require => File["${user_home}/.ssh"],
}
}
$ssh_config_entries = dig44($ssh, ['config_entries'], [])
if !empty($ssh_config_entries) {
create_resources(
ssh::config_entry,
$ssh_config_entries.reduce({}) |$cumulate, $entry| {
$key = "${user} ${entry['host']}"
merge($cumulate, {"${key}" => $entry })
},
{
'path' => "${user_home}/.ssh/config",
'owner' => $user,
'group' => $user,
}
)
} else {
file { "${user_home}/.ssh/config":
ensure => absent,
}
}
}
mod 'ghoneycutt-ssh', '3.57.0'
mod 'puppetlabs-stdlib', '4.14.0'
require 'rubygems'
require 'bundler/setup'
require 'ra10ke'
require 'puppetlabs_spec_helper/rake_tasks'
{
'STRICT_VARIABLES' => 'yes',
}.each do |var, default|
ENV[var] = ENV.fetch(var, default)
end
namespace :r10k do
r10k_spec_modpath = File.expand_path(File.join(__FILE__, '..', 'spec/fixtures/modules/r10k'))
r10k_default_modpath = File.expand_path(File.join(__FILE__, '..', 'puppet/r10k'))
task :spec_install do
Rake::Task['r10k:install'].invoke(r10k_spec_modpath)
end
task :install, [:path] do |_, args|
require 'r10k/puppetfile'
require 'puppet_forge'
require 'pathname'
modpath = Pathname.new args[:path] || r10k_default_modpath
curpath = Pathname.new Dir.pwd
puppetfile_dir = File.expand_path(File.join(__FILE__, '..'))
modules_dir = File.join(puppetfile_dir, modpath.relative_path_from(curpath))
puppetfile = R10K::Puppetfile.new(puppetfile_dir, modules_dir)
puppetfile.load!
puts "Processing Puppetfile for fixtures"
puppetfile.modules.each do |mod|
if mod.status == :insync
puts "Skipping #{mod.name} (#{mod.version}) already in sync"
else
if mod.status == :absent
msg = "installed #{mod.name}"
else
msg = "updated #{mod.name} from #{mod.version} to"
end
mod.sync
if mod.status != :insync
puts "Failed to sync #{mod.name}".red
else
puts "Successfully #{msg} #{mod.version}".green
end
end
end
end
task :spec_clean do
require 'fileutils'
if File.exist?(r10k_spec_modpath)
FileUtils.remove_dir(r10k_spec_modpath)
end
end
end
# prepending r10k task to prep
task :spec_prep => ['r10k:spec_install']
Rake::Task[:spec_clean].enhance do
Rake::Task['r10k:spec_clean'].invoke
end
# spec/defines/server_localuser_spec.rb
require 'spec_helper'
describe 'mod::server::localuser' do
let(:title) { 'example' }
context 'with no params provided' do
let(:params) { {} }
it { should compile.with_all_deps }
it { should contain_user("#{title}") }
it { should contain_group("#{title}") }
it { should contain_file("/home/#{title}/.ssh").with_mode('0700') }
it { should contain_file("/home/#{title}/.ssh/config").with_ensure('absent') }
end
context 'with only home specified' do
let(:params) { { :home => '/custom_home' } }
it { should contain_file("/custom_home/.ssh") }
it { should contain_file("/custom_home/.ssh/config").with_ensure('absent') }
end
context 'with ssh public key specified (no private key)' do
let(:params) { { :ssh_public_key => 'public key data' } }
it { should contain_ssh_authorized_key("#{title}") }
it { should_not contain_file("/home/#{title}/.ssh/id_rsa") }
end
context 'with ssh private key specified (no public key)' do
let(:params) { { :ssh_private_key => 'private key data' } }
it { should_not contain_ssh_authorized_key("#{title}") }
it { should contain_file("/home/#{title}/.ssh/id_rsa") }
end
context 'with ssh config options provided' do
let(:params) {
{
:ssh => {
'config_entries' => [
{ 'host' => 'custom-host',
'lines' => [ 'ProxyCommand custom_proxy_command' ] },
]
}
}
}
it do
should contain_concat__fragment("/home/#{title}/.ssh/config Host custom-host").
with_content("Host custom-host\nProxyCommand custom_proxy_command")
end
end
end
# spec/spec_helper.rb
require 'simplecov'
# required to prevent a deprecation warning when loading the spec helper lib
RSpec.configure do |c|
c.mock_with :rspec
end
require 'puppetlabs_spec_helper/module_spec_helper'
if ENV['DEBUG']
Puppet::Util::Log.level = :debug
Puppet::Util::Log.newdestination(:console)
end
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
#c.module_path = File.join(fixture_path, 'modules/site') + ':' + File.join(fixture_path, 'modules/r10k')
c.module_path = File.expand_path(File.join(__FILE__, '../../puppet/modules')) + ':' + File.join(fixture_path, 'modules/r10k')
c.hiera_config = File.join(fixture_path, 'hiera.yaml')
c.parser = 'future'
c.fail_fast = true
c.before :each do
# Avoid accidentally caching facts/env between test cases.
Facter.clear
Facter.clear_messages
end
c.after(:suite) do
RSpec::Puppet::Coverage.report!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment