<commit_before>test_name "Master can startup even if agent run before ssl files inited"

skip_test 'Skipping for PE since it pre-configures a CRL file and Puppet ' \
  'Server does not yet update it at startup - SERVER-346 and SERVER-911' \
  if options[:type] == 'pe'

puppetservice=options['puppetservice']
ssldir = master.puppet['ssldir']
backup_ssldir = master.tmpdir("agent_run_before_master_init_ssldir_backup")

# We don't set up and test PuppetDB integration on all OSes in our FOSS tests
# so here we check if PuppetDB is enabled so we can disable as needed.
reports_termini = on(master, puppet("config print --section master reports")).stdout.chomp
puppetdb_enabled = reports_termini =~ /puppetdb/
route_file_exists = master.file_exist?("/etc/puppetlabs/puppet/routes.yaml")


step "Backup original SSL configuration so can be restored when test finishes" do
  on(master, "cp -pR #{ssldir} #{backup_ssldir}")
end

teardown do
  step 'Stop the server so the original SSL config can be restored' do
    # This is done as a 'service stop' as opposed to a resource 'ensure=stopped'
    # to ensure that the OS isn't trying to restart a failing service in the
    # background (e.g, on a system using systemd Restart=on-failure) and
    # potentially recreating part of the SSL state before the original state
    # can be restored.  'ensure=stopped' doesn't terminate the systemd
    # Restart=on-failure loop.
    on(master, "service #{puppetservice} stop")
  end

  # Re-enable PuppetDB facts terminus
  if route_file_exists
    on(master, puppet("config set --section master route_file /etc/puppetlabs/puppet/routes.yaml"))
  end
  if puppetdb_enabled
    on(master, puppet("config set --section master reports #{reports_termini}"))
    on(master, puppet("config set --section master storeconfigs true"))
  end

  step 'Restore the original server SSL config' do
    on(master, "rm -rf #{ssldir}")
    on(master, "mv #{backup_ssldir}/#{File.basename(ssldir)} #{File.dirname(ssldir)}")
  end
  step 'Restart the server with original SSL config before ending the test' do
    on(master, puppet("resource service #{puppetservice} ensure=running"))
  end

end

step 'Disable reporting to PuppetDB if needed while we munge certs' do
  if route_file_exists
    on(master, puppet("config set --section master route_file /tmp/nonexistent.yaml"))
  end
  if puppetdb_enabled
    on(master, puppet("config set --section master reports store"))
    on(master, puppet("config set --section master storeconfigs false"))
  end
end

step 'Ensure puppetserver has been stopped before nuking SSL directory' do<commit_msg>Revert "Merge pull request #1622 from justinstoller/PE-23484-fixup"<commit_after>test_name "Master can startup even if agent run before ssl files inited"

skip_test 'Skipping for PE since it pre-configures a CRL file and Puppet ' \
  'Server does not yet update it at startup - SERVER-346 and SERVER-911' \
  if options[:type] == 'pe'

puppetservice=options['puppetservice']
ssldir = master.puppet['ssldir']
backup_ssldir = master.tmpdir("agent_run_before_master_init_ssldir_backup")

step "Backup original SSL configuration so can be restored when test finishes" do
  on(master, "cp -pR #{ssldir} #{backup_ssldir}")
end

teardown do
  step 'Stop the server so the original SSL config can be restored' do
    # This is done as a 'service stop' as opposed to a resource 'ensure=stopped'
    # to ensure that the OS isn't trying to restart a failing service in the
    # background (e.g, on a system using systemd Restart=on-failure) and
    # potentially recreating part of the SSL state before the original state
    # can be restored.  'ensure=stopped' doesn't terminate the systemd
    # Restart=on-failure loop.
    on(master, "service #{puppetservice} stop")
  end

  # Re-enable PuppetDB facts terminus
  on(master, puppet("config set --section master route_file /etc/puppetlabs/puppet/routes.yaml"))
  on(master, puppet("config set --section master reports puppetdb"))
  on(master, puppet("config set --section master storeconfigs true"))

  step 'Restore the original server SSL config' do
    on(master, "rm -rf #{ssldir}")
    on(master, "mv #{backup_ssldir}/#{File.basename(ssldir)} #{File.dirname(ssldir)}")
  end
  step 'Restart the server with original SSL config before ending the test' do
    on(master, puppet("resource service #{puppetservice} ensure=running"))
  end

end

step 'Disable reporting to PuppetDB while we munge certs' do
  on(master, puppet("config set --section master route_file /tmp/nonexistent.yaml"))
  on(master, puppet("config set --section master reports store"))
  on(master, puppet("config set --section master storeconfigs false"))
end

step 'Ensure puppetserver has been stopped before nuking SSL directory' do
{'fraction_non_alphanumeric': 0.09357952161141418, 'fraction_numerical': 0.0044062106588334034, 'mean_word_length': 4.479310344827586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268167', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1460, 'n_words': 548}