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

Added random sampling in acceptance tests. Minor refactoring #4

Merged
merged 1 commit into from
Jun 1, 2024
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
10 changes: 5 additions & 5 deletions manifests/cis.pp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
recurse => true,
source => 'puppet:///modules/cis_security_hardening_windows/user_grouppolicy/',
replace => false,
notify => [Exec['grouppolicy dir attributes']],
notify => Exec['grouppolicy dir attributes'],
}
# Ensure that the GroupPolicy directory is hidden as per default
exec { 'grouppolicy dir attributes':
Expand Down Expand Up @@ -85,17 +85,17 @@
}

# Create final enforced_rules by removing any excluded rules using description only
$enforced_rules = $total_rules.filter |$rule, $value| {
$enforced_rules = $total_rules.filter | String $rule, Hash $value| {
!($rule in $cis_exclude_rules_real)
}

# Remove the rule title from the hashes so the registry resource can apply them
$enforced_rules.each | String $title, Hash $rule = {} | {
$rule.each |String $key, Hash $value = {} | {
$regpath = regsubst($key, /\\[^\\]+$/,'')
if (!defined(Registry_key[$regpath]) and ($value['ensure'] !=absent)) {
$regpath = regsubst($key, /\\[^\\]+$/, '')
if !defined(Registry_key[$regpath]) and $value['ensure'] != 'absent' {
registry_key { $regpath:
ensure => $value['ensure'],
ensure => 'present',
}
}
registry_value {
Expand Down
6 changes: 3 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@

# Apply any misc registry hash values
$misc_registry.each | String $key, Hash $value = {} | {
$regpath = regsubst($key, /\\[^\\]+$/,'')
if (!defined(Registry_key[$regpath]) and ($value['ensure'] !=absent)) {
$regpath = regsubst($key, /\\[^\\]+$/, '')
if !defined(Registry_key[$regpath]) and $value['ensure'] != 'absent' {
registry_key { $regpath:
ensure => $value['ensure'],
ensure => 'present',
}
}
registry_value { $key:
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"pdk-version": "3.0.1",
"template-url": "pdk-default#3.0.0",
"template-ref": "tags/3.0.0-0-g5bfc1c0"
}
}
6 changes: 3 additions & 3 deletions spec/acceptance/cis_security_hardening_windows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

describe 'cis_security_hardening_windows acceptance testing' do
context 'Configure the master and run puppet on agents' do
puts "\e[0;32m \nAdding agents and class to nodeset on Master \e[0m\n"
print_stage('Adding agents and class to nodeset on Master')
agents.each do |agent|
pp = <<-SITE_PP
node '#{agent.node_name}' {
Expand All @@ -31,7 +31,7 @@

# Copy environment specific overrides for acceptance testing
if File.file?("#{PROJECT_ROOT}/spec/acceptance/overrides.yaml")
puts "\e[0;32m \nCopying environment specific hiera overrides from spec/acceptance/overrides.yaml to master\e[0m\n"
print_stage('Copying environment specific hiera overrides from spec/acceptance/overrides.yaml to master')
scp_to(master, "#{PROJECT_ROOT}/spec/acceptance/overrides.yaml", "/etc/puppetlabs/code/environments/#{ENVIRONMENT}/data/overrides.yaml")
end

Expand All @@ -40,7 +40,7 @@
on(master, "chmod -R g+rX,o-rwX #{env_path}")

# Run puppet on agents
puts "\e[0;32m \nRunning Puppet on agents \e[0m\n"
print_stage('Running Puppet on agents')
include_examples 'run idempotently'
end

Expand Down
12 changes: 10 additions & 2 deletions spec/acceptance/shared_examples/windows_tests.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
shared_examples 'windows tests' do |agent:, _agent_ip:|
# Number of combined registry entries to select
reg_entries_to_test = 50

# Users tests
describe user('user') do
it { is_expected.to exist }
Expand Down Expand Up @@ -43,8 +46,13 @@
%r{Remote Desktop Services UserMode Port Redirector},
]

# Iterate over combined data
registry_combined_data.each do |title, hash|
# Convert registry_combined_data to an array and randomly select 50 entries
random_registry_entries = registry_combined_data.to_a.sample(reg_entries_to_test)

print_stage("Verifying registry with a random sample of #{reg_entries_to_test} entries")

# Iterate over the randomly selected entries
random_registry_entries.each do |title, hash|
# Skip the iteration if the title matches any pattern in the exclusion list due to remote requirements for testing and complex data values
next if exclusion_patterns.any? { |pattern| title.match?(pattern) }

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def print_stage(h)
puts "\e[0;32m---------------------------------------------------------------------------------\e[0m"
puts "\e[0;36m#{h}\e[0m"
puts "\e[0;32m---------------------------------------------------------------------------------\e[0m"
puts "\n"
end

# As each dependency is installed from fixtures, add the latest version to an array (uses the 5th line of output so that only primary dependencies are written to metadata.json
Expand Down