diff --git a/.fixtures.yml b/.fixtures.yml index 0af53a34..3f031b88 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -7,7 +7,6 @@ fixtures: inifile: repo: "puppetlabs/inifile" ref: "4.2.0" - deploy_pe: 'jarretlavallee/deploy_pe' repositories: stdlib: 'https://github.com/puppetlabs/puppetlabs-stdlib' translate: 'https://github.com/puppetlabs/puppetlabs-translate' @@ -16,3 +15,4 @@ fixtures: provision: 'https://github.com/puppetlabs/provision' puppet_conf: 'https://github.com/puppetlabs/puppet_conf' pe_event_forwarding: 'https://github.com/puppetlabs/puppetlabs-pe_event_forwarding' + deploy_pe: 'https://github.com/jarretlavallee/puppet-deploy_pe' diff --git a/.github/workflows/latest_testing.yml b/.github/workflows/latest_testing.yml new file mode 100644 index 00000000..ce707601 --- /dev/null +++ b/.github/workflows/latest_testing.yml @@ -0,0 +1,119 @@ +name: "PE Latest Testing" + +on: [pull_request] + +jobs: + setup_matrix: + name: "Setup Test Matrix" + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.build-matrix.outputs.matrix }} + + steps: + - name: Checkout Source + uses: actions/checkout@v3 + if: ${{ github.repository_owner == 'puppetlabs' }} + + - name: Activate Ruby 2.7 + uses: ruby/setup-ruby@v1 + if: ${{ github.repository_owner == 'puppetlabs' }} + with: + ruby-version: "2.7" + bundler-cache: true + + - name: Print bundle environment + if: ${{ github.repository_owner == 'puppetlabs' }} + run: | + echo ::group::bundler environment + bundle env + echo ::endgroup:: + - name: Curl Forge for PE versions + id: curl_forge + run: | + echo "forge_response=$(curl https://forge.puppet.com/private/versions/pe)" >> $GITHUB_OUTPUT + - name: Set latest release + id: latest_release + run: | + out=$(jq -c '[.[] | select(.lts == false)][0].latest | {"collection": [.]}' <<<'${{ steps.curl_forge.outputs.forge_response }}') + echo "latest=$out" >> $GITHUB_OUTPUT + - name: Setup Spec Test Matrix + id: set-matrix + run: | + echo "matrix={\"platforms\":[\"centos-7\",\"rhel-8\",\"rhel-9\",\"ubuntu-2004-lts\"]}" >> $GITHUB_OUTPUT + - name: Setup Acceptance Test Matrix + id: build-matrix + run: | + out=$(echo '${{ steps.set-matrix.outputs.matrix }}' | jq -c --argjson latest '${{ steps.latest_release.outputs.latest }}' '.collection += $latest.collection') + echo "matrix=$out" >> $GITHUB_OUTPUT + Integration: + name: "${{matrix.platforms}}, ${{matrix.collection}}" + needs: + - setup_matrix + if: ${{ needs.setup_matrix.outputs.matrix != '{}' }} + + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: ${{fromJson(needs.setup_matrix.outputs.matrix)}} + + steps: + - name: Checkout Source + uses: actions/checkout@v3 + + - name: Activate Ruby 2.7 + uses: ruby/setup-ruby@v1 + with: + ruby-version: "2.7" + bundler-cache: true + + - name: Print bundle environment + run: | + echo ::group::bundler environment + bundle env + echo ::endgroup:: + - name: Create the fixtures directory + run: | + echo ::group::Create the fixtures directory + bundle exec rake spec_prep + echo ::endgroup:: + - name: Provision test environment + run: | + bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::provision_machines using='provision_service' image='${{ matrix.platforms }}' + echo ::group::=== REQUEST === + cat request.json || true + echo + echo ::endgroup:: + echo ::group::=== INVENTORY === + if [ -f 'spec/fixtures/litmus_inventory.yaml' ]; + then + FILE='spec/fixtures/litmus_inventory.yaml' + elif [ -f 'inventory.yaml' ]; + then + FILE='inventory.yaml' + fi + sed -e 's/password: .*/password: "[redacted]"/' < $FILE || true + echo ::endgroup:: + echo INVENTORY_PATH=$FILE >> $GITHUB_ENV + - name: Install server + run: | + bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::server_setup puppet_version='${{ matrix.collection }}' -i ./$INVENTORY_PATH --stream + - name: Install module + run: | + bundle exec rake 'litmus:install_module' + - name: Set up Splunk instance + run: | + bundle exec rake acceptance:setup_splunk_instance + - name: Run acceptance tests + run: | + bundle exec rake acceptance:run_tests + - name: Remove test environment + if: ${{ always() }} + continue-on-error: true + run: | + if [[ -f inventory.yaml || -f spec/fixtures/litmus_inventory.yaml ]]; then + bundle exec rake 'litmus:tear_down' + echo ::group::=== REQUEST === + cat request.json || true + echo + echo ::endgroup:: + fi diff --git a/.github/workflows/lts_testing.yml b/.github/workflows/lts_testing.yml new file mode 100644 index 00000000..7f6ff885 --- /dev/null +++ b/.github/workflows/lts_testing.yml @@ -0,0 +1,119 @@ +name: "PE LTS Testing" + +on: [pull_request] + +jobs: + setup_matrix: + name: "Setup Test Matrix" + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.build-matrix.outputs.matrix }} + + steps: + - name: Checkout Source + uses: actions/checkout@v3 + if: ${{ github.repository_owner == 'puppetlabs' }} + + - name: Activate Ruby 2.7 + uses: ruby/setup-ruby@v1 + if: ${{ github.repository_owner == 'puppetlabs' }} + with: + ruby-version: "2.7" + bundler-cache: true + + - name: Print bundle environment + if: ${{ github.repository_owner == 'puppetlabs' }} + run: | + echo ::group::bundler environment + bundle env + echo ::endgroup:: + - name: Curl Forge for PE versions + id: curl_forge + run: | + echo "forge_response=$(curl https://forge.puppet.com/private/versions/pe)" >> $GITHUB_OUTPUT + - name: Set LTS release + id: lts_release + run: | + out=$(jq -c '[.[] | select(.lts == true)][0].latest | {"collection": [.]}' <<<'${{ steps.curl_forge.outputs.forge_response }}') + echo "lts=$out" >> $GITHUB_OUTPUT + - name: Setup Spec Test Matrix + id: set-matrix + run: | + echo "matrix={\"platforms\":[\"centos-7\",\"rhel-8\",\"rhel-9\",\"ubuntu-2004-lts\"]}" >> $GITHUB_OUTPUT + - name: Setup Acceptance Test Matrix + id: build-matrix + run: | + out=$(echo '${{ steps.set-matrix.outputs.matrix }}' | jq -c --argjson lts '${{ steps.lts_release.outputs.lts }}' '.collection += $lts.collection') + echo "matrix=$out" >> $GITHUB_OUTPUT + Integration: + name: "${{matrix.platforms}}, ${{matrix.collection}}" + needs: + - setup_matrix + if: ${{ needs.setup_matrix.outputs.matrix != '{}' }} + + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: ${{fromJson(needs.setup_matrix.outputs.matrix)}} + + steps: + - name: Checkout Source + uses: actions/checkout@v3 + + - name: Activate Ruby 2.7 + uses: ruby/setup-ruby@v1 + with: + ruby-version: "2.7" + bundler-cache: true + + - name: Print bundle environment + run: | + echo ::group::bundler environment + bundle env + echo ::endgroup:: + - name: Create the fixtures directory + run: | + echo ::group::Create the fixtures directory + bundle exec rake spec_prep + echo ::endgroup:: + - name: Provision test environment + run: | + bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::provision_machines using='provision_service' image='${{ matrix.platforms }}' + echo ::group::=== REQUEST === + cat request.json || true + echo + echo ::endgroup:: + echo ::group::=== INVENTORY === + if [ -f 'spec/fixtures/litmus_inventory.yaml' ]; + then + FILE='spec/fixtures/litmus_inventory.yaml' + elif [ -f 'inventory.yaml' ]; + then + FILE='inventory.yaml' + fi + sed -e 's/password: .*/password: "[redacted]"/' < $FILE || true + echo ::endgroup:: + echo INVENTORY_PATH=$FILE >> $GITHUB_ENV + - name: Install server + run: | + bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::server_setup puppet_version='${{ matrix.collection }}' -i ./$INVENTORY_PATH --stream + - name: Install module + run: | + bundle exec rake 'litmus:install_module' + - name: Set up Splunk instance + run: | + bundle exec rake acceptance:setup_splunk_instance + - name: Run acceptance tests + run: | + bundle exec rake acceptance:run_tests + - name: Remove test environment + if: ${{ always() }} + continue-on-error: true + run: | + if [[ -f inventory.yaml || -f spec/fixtures/litmus_inventory.yaml ]]; then + bundle exec rake 'litmus:tear_down' + echo ::group::=== REQUEST === + cat request.json || true + echo + echo ::endgroup:: + fi diff --git a/.github/workflows/integration_test.yml b/.github/workflows/nightly_testing.yml similarity index 50% rename from .github/workflows/integration_test.yml rename to .github/workflows/nightly_testing.yml index 512eaa2c..c43ea01a 100644 --- a/.github/workflows/integration_test.yml +++ b/.github/workflows/nightly_testing.yml @@ -1,23 +1,60 @@ -name: "Integration Testing" +name: "Puppet Server Nightly Testing" on: [pull_request] jobs: + setup_matrix: + name: "Setup Test Matrix" + runs-on: ubuntu-20.04 + outputs: + matrix: ${{ steps.build-matrix.outputs.matrix }} + + steps: + - name: Checkout Source + uses: actions/checkout@v3 + if: ${{ github.repository_owner == 'puppetlabs' }} + + - name: Activate Ruby 2.7 + uses: ruby/setup-ruby@v1 + if: ${{ github.repository_owner == 'puppetlabs' }} + with: + ruby-version: "2.7" + bundler-cache: true + + - name: Print bundle environment + if: ${{ github.repository_owner == 'puppetlabs' }} + run: | + echo ::group::bundler environment + bundle env + echo ::endgroup:: + - name: Get Test Matrix + id: get-matrix + run: | + bundle exec matrix_from_metadata_v2 + - name: Set nightly releases + id: nightly_release + run: | + out=$(echo '${{ steps.get-matrix.outputs.matrix }}' | jq -c '.collection') + echo "latest=$out" >> $GITHUB_OUTPUT + - name: Setup Spec Test Matrix + id: set-matrix + run: | + echo "matrix={\"platforms\":[\"centos-7\",\"rhel-8\",\"rhel-9\",\"ubuntu-2004-lts\"]}" >> $GITHUB_OUTPUT + - name: Setup Acceptance Test Matrix + id: build-matrix + run: | + out=$(echo '${{ steps.set-matrix.outputs.matrix }}' | jq -c --argjson nightly '${{ steps.nightly_release.outputs.latest }}' '.collection += $nightly') + echo "matrix=$out" >> $GITHUB_OUTPUT Integration: + name: "${{matrix.platforms}}, ${{matrix.collection}}" + needs: + - setup_matrix + if: ${{ needs.setup_matrix.outputs.matrix != '{}' }} + runs-on: ubuntu-20.04 strategy: fail-fast: false - matrix: - platforms: - - centos-7 - puppet_version: - - puppet6-nightly - - puppet7-nightly - - puppet8-nightly - - 2021.7.3 - - env: - CLOUD_CI: 'true' + matrix: ${{fromJson(needs.setup_matrix.outputs.matrix)}} steps: - name: Checkout Source @@ -47,9 +84,11 @@ jobs: echo echo ::endgroup:: echo ::group::=== INVENTORY === - if [ -f 'spec/fixtures/litmus_inventory.yaml' ]; then + if [ -f 'spec/fixtures/litmus_inventory.yaml' ]; + then FILE='spec/fixtures/litmus_inventory.yaml' - elif [ -f 'inventory.yaml' ]; then + elif [ -f 'inventory.yaml' ]; + then FILE='inventory.yaml' fi sed -e 's/password: .*/password: "[redacted]"/' < $FILE || true @@ -57,28 +96,24 @@ jobs: echo INVENTORY_PATH=$FILE >> $GITHUB_ENV - name: Install server run: | - bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::server_setup puppet_version='${{ matrix.puppet_version }}' -i ./$INVENTORY_PATH - + bundle exec bolt --modulepath spec/fixtures/modules plan run splunk_hec::acceptance::server_setup puppet_version='${{ matrix.collection }}' -i ./$INVENTORY_PATH --stream - name: Install module run: | bundle exec rake 'litmus:install_module' - - name: Set up Splunk instance run: | bundle exec rake acceptance:setup_splunk_instance - - - name: Run tests + - name: Run acceptance tests run: | bundle exec rake acceptance:run_tests - - name: Remove test environment if: ${{ always() }} continue-on-error: true run: | if [[ -f inventory.yaml || -f spec/fixtures/litmus_inventory.yaml ]]; then - buildevents cmd $TRACE_ID $STEP_ID 'rake litmus:tear_down' -- bundle exec rake 'litmus:tear_down' + bundle exec rake 'litmus:tear_down' echo ::group::=== REQUEST === cat request.json || true echo echo ::endgroup:: - fi \ No newline at end of file + fi diff --git a/metadata.json b/metadata.json index 452bcfb4..7173a1c3 100644 --- a/metadata.json +++ b/metadata.json @@ -1,6 +1,6 @@ { "name": "puppetlabs-splunk_hec", - "version": "1.4.0", + "version": "2.0.0", "author": "puppetlabs", "summary": "Puppet report processor using Splunk HEC", "license": "Apache-2.0", @@ -10,11 +10,11 @@ "dependencies": [ { "name": "puppetlabs-puppet_metrics_collector", - "version_requirement": ">= 6.0.0 < 8.0" + "version_requirement": ">= 6.0.0 < 9.0" }, { "name": "puppetlabs-pe_event_forwarding", - "version_requirement": ">= 1.1.0 < 2.0.0" + "version_requirement": ">= 1.1.0 < 3.0.0" } ], "operatingsystem_support": [ @@ -34,7 +34,8 @@ "operatingsystem": "RedHat", "operatingsystemrelease": [ "7", - "8" + "8", + "9" ] }, { @@ -46,8 +47,9 @@ { "operatingsystem": "Ubuntu", "operatingsystemrelease": [ + "18.04", "20.04", - "18.04" + "22.04" ] }, { diff --git a/plans/acceptance/oss_server_setup.pp b/plans/acceptance/oss_server_setup.pp index b3dc93b9..43a27979 100644 --- a/plans/acceptance/oss_server_setup.pp +++ b/plans/acceptance/oss_server_setup.pp @@ -10,7 +10,7 @@ Optional[String] $collection = 'puppet7' ) { # get server - $server = get_targets('*').filter |$node| { $node.vars['role'] == 'server' } + $server = get_targets('*').filter |$n| { $n.vars['role'] == 'server' } $localhost = get_targets('localhost') # get facts @@ -18,7 +18,7 @@ $platform = $puppetserver_facts['platform'] # machines are not yet ready at time of installing the puppetserver, so we wait 15s - run_command('sleep 15s', $localhost) + run_command('sleep 15', $localhost) # install puppetserver and start on master run_task( diff --git a/plans/acceptance/pe_server_setup.pp b/plans/acceptance/pe_server_setup.pp index 6fd49986..bdc2fdd6 100644 --- a/plans/acceptance/pe_server_setup.pp +++ b/plans/acceptance/pe_server_setup.pp @@ -11,12 +11,12 @@ # @param [Optional[Hash]] pe_settings # Sets PE settings including password plan splunk_hec::acceptance::pe_server_setup( - Optional[String] $version = '2021.7.2', - Optional[Hash] $pe_settings = { password => 'puppetlabspie' } + Optional[String] $version = '2021.7.5', + Optional[Hash] $pe_settings = { password => 'puppetlabspie', configure_tuning => false } ) { # machines are not yet ready at time of installing the puppetserver, so we wait 15s $localhost = get_targets('localhost') - run_command('sleep 15s', $localhost) + run_command('sleep 15', $localhost) #identify pe server node $puppet_server = get_targets('*').filter |$n| { $n.vars['role'] == 'server' } diff --git a/plans/acceptance/server_setup.pp b/plans/acceptance/server_setup.pp index 77984683..045c9620 100644 --- a/plans/acceptance/server_setup.pp +++ b/plans/acceptance/server_setup.pp @@ -9,11 +9,11 @@ # @param [Optional[String]] puppet_version # Sets the version of Puppet Server to install plan splunk_hec::acceptance::server_setup( - Optional[String] $puppet_version = '2021.7.2', + Optional[String] $puppet_version = '2021.7.5', ) { # machines are not yet ready at time of installing the puppetserver, so we wait 15s $localhost = get_targets('localhost') - run_command('sleep 15s', $localhost) + run_command('sleep 15', $localhost) if $puppet_version =~ /puppet/ { run_plan( diff --git a/provision.yaml b/provision.yaml index 798f2b46..c740ce98 100644 --- a/provision.yaml +++ b/provision.yaml @@ -1,10 +1,14 @@ acceptance: provisioner: abs - images: ['ubuntu-1804-x86_64'] + images: + - centos-7-x86_64 + - redhat-8-x86_64 + - redhat-9-x86_64 + - ubuntu-2004-x86_64 vars: | role: server fips_acceptance: provisioner: abs images: - redhat-fips-7-x86_64 - - ubuntu-1804-x86_64 + - ubuntu-2004-x86_64 diff --git a/rakelib/helpers.rake b/rakelib/helpers.rake index 750628fe..1c512dc6 100644 --- a/rakelib/helpers.rake +++ b/rakelib/helpers.rake @@ -17,7 +17,7 @@ namespace :acceptance do provision_list = ENV['PROVISION_LIST'] || 'acceptance' Rake::Task['litmus:provision_list'].invoke(provision_list) - inventory_hash = LitmusHelpers.inventory_hash_from_inventory_file + inventory_hash = inventory_hash_from_inventory_file begin # If a fips node is present, assign the correct roles to the fips node and the splunk node fips_node = inventory_hash['groups'].detect {|g| g['name'] == 'ssh_nodes'}['targets'].detect {|t| t['facts']['platform'].match(/fips/)} @@ -38,7 +38,13 @@ namespace :acceptance do desc 'clone puppetlabs-pe_event_forwarding module to test host' task :upload_pe_event_forwarding_module do - puppetserver.bolt_upload_file('./spec/fixtures/modules/pe_event_forwarding', '/etc/puppetlabs/code/environments/production/modules') + puppetserver.each do |target| + message = "Installing puppetlabs-pe_event_forwarding module on #{target.uri} !" + spinner = start_spinner(message) + target.run_shell('rm /etc/puppetlabs/code/environments/production/modules/pe_event_forwarding -rf', expect_failures: true) + target.bolt_upload_file('./spec/fixtures/modules/pe_event_forwarding', '/etc/puppetlabs/code/environments/production/modules') + stop_spinner(spinner) + end end desc 'Sets up PE on the server' @@ -48,60 +54,72 @@ namespace :acceptance do target_nodes = find_targets(inventory_hash, 'ssh_nodes') config = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } - params = {} - params.merge(puppet_version: ENV['PUPPET_VERSION']) unless ENV['PUPPET_VERSION'].nil? + message = "Installing Puppet Enterprise on targets in litmus_inventory.yaml !" + install_spinner = start_spinner(message) bolt_result = run_plan('splunk_hec::acceptance::server_setup', params, config: config, inventory: inventory_hash.clone) + stop_spinner(install_spinner) + puts bolt_result['status'] end desc 'Sets up the Splunk instance' - task :setup_splunk_instance do + task :setup_splunk_targets do + inventory_hash = LitmusHelpers.inventory_hash_from_inventory_file splunk_setup_target = begin splunk_node rescue TargetNotFoundError puppetserver end - puts("Starting the Splunk instance at the puppetserver (#{splunk_setup_target.uri})") - splunk_setup_target.bolt_upload_file('./spec/support/acceptance/splunk', '/tmp/splunk') - puts splunk_setup_target.bolt_run_script('spec/support/acceptance/start_splunk_instance.sh').stdout.chomp - # HEC token is hard coded because it will always be the same in the splunk container - instance, hec_token = "#{splunk_setup_target.uri}:8088", 'abcd1234' - - # Update the inventory file - puts('Updating the inventory.yaml file with the Splunk HEC credentials') - inventory_hash = LitmusHelpers.inventory_hash_from_inventory_file - splunk_group = inventory_hash['groups'].find { |g| g['name'] =~ %r{splunk} } - unless splunk_group - splunk_group = { 'name' => 'splunk_nodes' } - inventory_hash['groups'].push(splunk_group) - end - splunk_group['targets'] = [{ + splunk_setup_target.each_with_index do |splunk_target, i| + message = "Starting the Splunk instance at the puppetserver (#{splunk_target.uri})" + splunk_spinner = start_spinner(message) + splunk_target.bolt_upload_file('./spec/support/acceptance/splunk', '/tmp/splunk') + result = splunk_target.bolt_run_script('spec/support/acceptance/start_splunk_instance.sh').stdout.chomp + stop_spinner(splunk_spinner) + puts result + + # HEC token is hard coded because it will always be the same in the splunk container + instance, hec_token = "#{splunk_target.uri}:8088", 'abcd1234' + + # Update the inventory file + message = "Updating the inventory.yaml file with the Splunk HEC credentials for #{splunk_target.uri}" + inventory_spinner = start_spinner(message) + splunk_group = inventory_hash['groups'].find { |g| g['name'] =~ %r{splunk} } + unless splunk_group + splunk_group = { 'name' => 'splunk_nodes' } + inventory_hash['groups'].push(splunk_group) + splunk_group['targets'] = [] + end + splunk_group['targets'][i] = { 'uri' => instance, - 'config' => { - 'transport' => 'remote', - 'remote' => { - 'hec_token' => hec_token, + 'config' => { + 'transport' => 'remote', + 'remote' => { + 'hec_token' => hec_token, + } + }, + 'facts' => { + 'platform' => 'splunk_hec', + 'provisioner' => 'docker', + 'container_name' => 'splunk_enterprise_1' + }, + 'vars' => { + 'role' => ['splunk_instance'], } - }, - 'facts' => { - 'platform' => 'splunk_hec', - 'provisioner' => 'docker', - 'container_name' => 'splunk_enterprise_1' - }, - 'vars' => { - 'roles' => ['splunk_instance'], } - }] + stop_spinner(inventory_spinner) + end write_to_inventory_file(inventory_hash, 'spec/fixtures/litmus_inventory.yaml') end desc 'Installs the module on the puppetserver' task :install_module do - puppetserver.run_shell("rm -rf '/etc/puppetlabs/puppet/splunk_hec.yaml'") - Rake::Task['litmus:install_module'].invoke(puppetserver.uri) - puppetserver.bolt_upload_file('./spec/support/acceptance/splunk_hec.yaml', '/etc/puppetlabs/puppet/splunk_hec.yaml') + puppetserver.each do |target| + Rake::Task['litmus:install_module'].invoke(target.uri) + Rake::Task['litmus:install_module'].reenable + end end desc 'Runs the tests' @@ -120,7 +138,7 @@ namespace :acceptance do tasks = [ :provision_vms, :setup_pe, - :setup_splunk_instance, + :setup_splunk_targets, :install_module, :upload_pe_event_forwarding_module, ] @@ -136,20 +154,38 @@ namespace :acceptance do desc 'Tear down the setup' task :tear_down do puts("Tearing down the test infrastructure ...\n") - Rake::Task['litmus:tear_down'].invoke(puppetserver.uri) - begin - Rake::Task['litmus:tear_down'].invoke(splunk_node.uri) - rescue TargetNotFoundError - # This error means splunk server container was run on the puppetserver node. - end + Rake::Task['litmus:tear_down'].invoke FileUtils.rm_f('spec/fixtures/litmus_inventory.yaml') end - desc 'Task for CI' + desc 'Task to run rspec tests against multiple targets' task :ci_run_tests do + include ::BoltSpec::Run + inventory_hash = inventory_hash_from_inventory_file + + # Run the tests + config = { 'modulepath' => File.join(Dir.pwd, 'spec', 'fixtures', 'modules') } + puppetserver.each do |server| + message = "Running rspec tests against #{server.uri} !" + spec_spinner = start_spinner(message) + params = { 'sut' => server.uri, 'format' => 'documentation' } + bolt_result = run_task('provision::run_tests', 'localhost', params, config: config) + stop_spinner(spec_spinner) + puts "Finished running rspec tests against #{server.uri} !\n" + if bolt_result[0]['value'].has_key?('_error') + test_result = bolt_result[0]['value']['_error']['msg'].to_json + else + test_result = bolt_result[0]['value']['result'].to_json + end + puts JSON.parse(test_result) + end + end + + desc 'Task for CI' + task :ci_tests do begin Rake::Task['acceptance:setup'].invoke - Rake::Task['acceptance:run_tests'].invoke + Rake::Task['acceptance:ci_run_tests'].invoke ensure Rake::Task['acceptance:tear_down'].invoke end diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index 03036cda..4285fc22 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -3,6 +3,7 @@ describe 'Verify the minimum install' do let(:earliest) { Time.now.utc } + let(:server) { ENV['TARGET_HOST'] } before(:all) do server_agent_run(setup_manifest) @@ -11,7 +12,7 @@ context 'with a basic test' do it 'Successfully sends a report to splunk' do before_run = earliest - trigger_puppet_run(puppetserver) + trigger_puppet_run(server) after_run = Time.now.utc report_count = report_count(get_splunk_report(before_run, after_run)) expect(report_count).to be 1 @@ -19,7 +20,7 @@ it 'Successfully sends facts to Splunk' do before_run = earliest - trigger_puppet_run(puppetserver) + trigger_puppet_run(server) after_run = Time.now.utc report_count = report_count(get_splunk_report(before_run, after_run, 'puppet:facts')) expect(report_count).to be >= 1 @@ -28,13 +29,13 @@ it 'Records events with record_event set to true' it 'Successfully sends data to an http endpoint' do - run_shell('cat /etc/puppetlabs/code/environments/production/modules/splunk_hec/examples/orchestrator_metrics.json | puppet splunk_hec --sourcetype puppet:summary --saved_report') + server.run_shell('cat /etc/puppetlabs/code/environments/production/modules/splunk_hec/examples/orchestrator_metrics.json | puppet splunk_hec --sourcetype puppet:summary --saved_report') end it 'Fails when given a bad endpoint' do server_agent_run(setup_manifest(url: 'notanendpoint/nicetry')) cmd = 'cat /etc/puppetlabs/code/environments/production/modules/splunk_hec/examples/foo.json | puppet splunk_hec --sourcetype puppet:summary --saved_report' - results = puppetserver.run_shell(cmd, expect_failures: true).to_s + results = server.run_shell(cmd, expect_failures: true).to_s expect(results).to match %r{exit_code=1} end diff --git a/spec/acceptance/events_processor_spec.rb b/spec/acceptance/events_processor_spec.rb index a979c4d2..9fbfb5d8 100644 --- a/spec/acceptance/events_processor_spec.rb +++ b/spec/acceptance/events_processor_spec.rb @@ -3,6 +3,7 @@ describe 'Event Forwarding' do is_pe = puppet_user == 'pe-puppet' let(:earliest) { Time.now.utc } + let(:server) { ENV['TARGET_HOST'] } context 'With event forwarding enabled', if: is_pe do before(:all) do @@ -12,21 +13,21 @@ context 'with orchestrator event_types set' do let(:report) do before_run = Time.now.utc - puppetserver.run_shell("LC_ALL=en_US.UTF-8 puppet task run facts --nodes #{host_name}") - puppetserver.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") + server.run_shell("LC_ALL=en_US.UTF-8 puppet task run facts --nodes #{host_name}") + server.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") after_run = Time.now.utc get_splunk_report(before_run, after_run, 'puppet:jobs') end it 'does not send report on first run' do - puppetserver.run_shell('rm /etc/puppetlabs/pe_event_forwarding/pe_event_forwarding_indexes.yaml', expect_failures: true) + server.run_shell('rm /etc/puppetlabs/pe_event_forwarding/pe_event_forwarding_indexes.yaml', expect_failures: true) count = report_count(report) expect(count).to be 0 end it 'Successfully sends an orchestrator event to splunk' do # ensure the indexes.yaml file is created - puppetserver.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") + server.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") count = report_count(report) expect(count).to be 1 end @@ -59,21 +60,21 @@ context 'with pe_console event_types set' do let(:report) do before_run = Time.now.utc - puppetserver.run_shell("LC_ALL=en_US.UTF-8 puppet task run facts --nodes #{host_name}") - puppetserver.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") + server.run_shell("LC_ALL=en_US.UTF-8 puppet task run facts --nodes #{host_name}") + server.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") after_run = Time.now.utc get_splunk_report(before_run, after_run, 'puppet:activities_console') end it 'does not send report on first run' do - puppetserver.run_shell('rm /etc/puppetlabs/pe_event_forwarding/pe_event_forwarding_indexes.yaml', expect_failures: true) + server.run_shell('rm /etc/puppetlabs/pe_event_forwarding/pe_event_forwarding_indexes.yaml', expect_failures: true) count = report_count(report) expect(count).to be 0 end it 'Successfully sends a pe_console event to splunk' do # ensure the indexes.yaml file is created - puppetserver.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") + server.run_shell("#{EVENT_FORWARDING_CONFDIR}/collect_api_events.rb") count = report_count(report) expect(count).to be 1 end diff --git a/spec/spec_helper_acceptance_local.rb b/spec/spec_helper_acceptance_local.rb index 96a70b67..98fa9171 100644 --- a/spec/spec_helper_acceptance_local.rb +++ b/spec/spec_helper_acceptance_local.rb @@ -7,10 +7,12 @@ EVENT_FORWARDING_CONFDIR = '/etc/puppetlabs/pe_event_forwarding'.freeze DIR_TEST_COMMAND = '[[ -d /etc/puppetlabs/code/environments/production/modules/pe_event_forwarding ]] '\ - '&& rm -r /etc/puppetlabs/code/environments/production/modules/pe_event_forwarding'.freeze + '&& rm /etc/puppetlabs/code/environments/production/modules/pe_event_forwarding -rf'.freeze EVENT_FORWARDING_LOCAL_PATH = './spec/fixtures/modules/pe_event_forwarding'.freeze EVENT_FORWARDING_REMOTE_PATH = '/etc/puppetlabs/code/environments/production/modules'.freeze +TARGET_SERVER = ENV['TARGET_HOST'] + RSpec.configure do |config| include TargetHelpers @@ -19,9 +21,9 @@ # Puppet runs (one triggered by service vs one we trigger) shell_command = 'puppet resource service puppet ensure=stopped; '\ 'puppet module install puppetlabs-inifile --version 5.1.0' - puppetserver.run_shell(shell_command) - puppetserver.run_shell(DIR_TEST_COMMAND, expect_failures: true) - puppetserver.bolt_upload_file(EVENT_FORWARDING_LOCAL_PATH, EVENT_FORWARDING_REMOTE_PATH) + TARGET_SERVER.run_shell(shell_command) + TARGET_SERVER.run_shell(DIR_TEST_COMMAND, expect_failures: true) + TARGET_SERVER.bolt_upload_file(EVENT_FORWARDING_LOCAL_PATH, EVENT_FORWARDING_REMOTE_PATH) end end @@ -35,8 +37,8 @@ def set_sitepp_content(manifest) } HERE - puppetserver.write_file(content, '/etc/puppetlabs/code/environments/production/manifests/site.pp') - puppetserver.run_shell("chown #{puppet_user}:#{puppet_user} /etc/puppetlabs/code/environments/production/manifests/site.pp") + TARGET_SERVER.write_file(content, '/etc/puppetlabs/code/environments/production/manifests/site.pp') + TARGET_SERVER.run_shell("chown #{puppet_user}:#{puppet_user} /etc/puppetlabs/code/environments/production/manifests/site.pp") end def trigger_puppet_run(target, acceptable_exit_codes: [0, 2]) @@ -65,14 +67,14 @@ def to_manifest(*declarations) end def host_name - @puppetserver_hostname ||= puppetserver.run_shell('facter fqdn').stdout.chomp + @puppetserver_hostname ||= TARGET_SERVER.run_shell('facter fqdn').stdout.chomp end def report_dir cmd = "reportdir=`puppet config print reportdir --section server` \n"\ "hostname=`facter fqdn` \n"\ 'echo \"$reportdir/$hostname\"' - @report_dir ||= puppetserver.run_shell(cmd).stdout.chomp + @report_dir ||= TARGET_SERVER.run_shell(cmd).stdout.chomp end def setup_manifest(disabled: false, ignore_store: true, ssl_ca: nil, url: nil, with_event_forwarding: false) @@ -125,7 +127,7 @@ def add_service_resource end def add_event_forwarding - token = puppetserver.run_shell('puppet access show').stdout.chomp + token = TARGET_SERVER.run_shell('puppet access show').stdout.chomp params = { pe_token: token, disabled: true @@ -135,7 +137,7 @@ def add_event_forwarding def configure_ssl(cert_store: false) inventory_hash = LitmusHelpers.inventory_hash_from_inventory_file - image = puppetserver.facts_from_node(inventory_hash, puppetserver.uri) + image = LitmusHelpers.facts_from_node(inventory_hash, TARGET_SERVER) cmd = if cert_store if image['platform'].include?('ubuntu') 'cp $(puppet config print localcacert) /usr/local/share/ca-certificates/ && update-ca-certificates' @@ -145,7 +147,7 @@ def configure_ssl(cert_store: false) else 'cp $(puppet config print localcacert) /etc/puppetlabs/puppet/splunk_hec/' end - puppetserver.run_shell(cmd) + TARGET_SERVER.run_shell(cmd) end def puppet_user @@ -154,7 +156,7 @@ def puppet_user def query_puppet_user service_name = '' - puppetserver.run_shell('[ -f /opt/puppetlabs/server/pe_version ]', expect_failures: true) do |result| + TARGET_SERVER.run_shell('[ -f /opt/puppetlabs/server/pe_version ]', expect_failures: true) do |result| service_name = (result.exit_code == 0) ? 'pe-puppet' : 'puppet' end service_name @@ -170,7 +172,7 @@ def get_splunk_report(earliest, latest, sourcetype = 'puppet:summary') begin splunk_runner = splunk_node rescue - splunk_runner = puppetserver + splunk_runner = TARGET_SERVER end response = splunk_runner.run_shell(query_command).stdout JSON.parse("[#{response.split.join(',')}]") @@ -182,14 +184,14 @@ def report_count(report) def log_count(message, log) cmd = "grep '#{message}' #{log} -c" - puppetserver.run_shell(cmd, expect_failures: true).stdout.chomp.to_i + TARGET_SERVER.run_shell(cmd, expect_failures: true).stdout.chomp.to_i end def server_agent_run(manifest) set_sitepp_content(manifest) - trigger_puppet_run(puppetserver) + trigger_puppet_run(TARGET_SERVER) end def console_host_fqdn - @console_host_fqdn ||= puppetserver.run_shell('hostname -A').stdout.strip + @console_host_fqdn ||= TARGET_SERVER.run_shell('hostname -A').stdout.strip end diff --git a/spec/support/acceptance/helpers.rb b/spec/support/acceptance/helpers.rb index 2d8529d8..aca99c97 100644 --- a/spec/support/acceptance/helpers.rb +++ b/spec/support/acceptance/helpers.rb @@ -34,9 +34,9 @@ def bolt_config # most recently set the TARGET_HOST variable. PuppetLitmus.instance_methods.each do |name| m = PuppetLitmus.instance_method(name) - define_method(name) do |*args, &block| + define_method(name) do |*args, **kwargs, &block| ENV['TARGET_HOST'] = uri - m.bind(self).call(*args, &block) + m.bind(self).call(*args, **kwargs, &block) end end end @@ -50,7 +50,7 @@ def puppetserver module_function :puppetserver def splunk_instance - target('Splunk instance', 'acceptance:setup_splunk_instance', 'splunk_instance') + target('Splunk instance', 'acceptance:setup_splunk_targets', 'splunk_instance') end module_function :splunk_instance @@ -63,17 +63,16 @@ def target(name, setup_task, role) @targets ||= {} unless @targets[name] - # Find the target + # Find the targets inventory_hash = LitmusHelpers.inventory_hash_from_inventory_file - targets = LitmusHelpers.find_targets(inventory_hash, nil) - target_uri = targets.find do |target| - vars = LitmusHelpers.vars_from_node(inventory_hash, target) || {} - (vars['role'] || []) == role - end - unless target_uri + targets = LitmusHelpers.nodes_with_role(role, inventory_hash) + unless targets raise TargetNotFoundError, "none of the targets in 'inventory.yaml' have the '#{role}' role set. Did you forget to run 'rake #{setup_task}'?" end - @targets[name] = Target.new(target_uri) + @targets[name] = [] + targets.each_with_index do |target,i| + @targets[name][i] = Target.new(target) + end end @targets[name]