<commit_before>require 'pxp-agent/config_helper.rb'
require 'pcp/client'
require 'pcp/simple_logger'
require 'net/http'
require 'openssl'
require 'socket'
require 'json'
require 'securerandom'

# This file contains general test helper methods for pxp-agent acceptance tests

# The standard path for git checkouts is where pcp-broker will be
GIT_CLONE_FOLDER = '/opt/puppet-git-repos'

# Some helpers for working with the log file
PXP_LOG_DIR_CYGPATH = '/cygdrive/c/ProgramData/PuppetLabs/pxp-agent/var/log'
PXP_LOG_DIR_POSIX = '/var/log/puppetlabs/pxp-agent'

def logdir(host)
  windows?(host)?
    PXP_LOG_DIR_CYGPATH :
    PXP_LOG_DIR_POSIX
end

# @param host the beaker host you want the path to the log file on
# @return The path to the log file on the host. For Windows, a cygpath is returned
def logfile(host)
  logdir(host)+'/pxp-agent.log'
end

def reset_logfile(host)
  old_logfile = logfile(host)
  new_logfile = old_logfile+'.'+SecureRandom.uuid
  logger.notify "---- Saving pxp-agent logfile to #{new_logfile}"
  retry_on(host, "test ! -f #{old_logfile} || mv #{old_logfile} #{new_logfile}")
end

# A general helper for waiting for a file to contain some string, and failing the test if it doesn't appear
# @param host the host to check the file on
# @param file path to file to check
# @param expected the string or pattern expected in the file
# @param seconds number of seconds to retry (one retry per second). Default 30.
def expect_file_on_host_to_contain(host, file, expected, seconds=30)
  # If the expected file entry does not appear in the file within 30 seconds, then do an explicit assertion
  # that the file should contain the expected text, so we get a test fail (not an unhandled error), and the
  # log contents will appear in the test failure output
  begin
    retry_on(host, "grep '#{expected}' #{file}", {:max_retries => 30,
                                                     :retry_interval => 1})
  rescue
    on(host, "cat #{file}") do |result|
      assert_match(expected, result.stdout,
                  "Expected text '#{expected}' did not appear in file #{file}")
    end
  end
end

# Some helpers for working with a pcp-broker 'lein tk' instance
def run_pcp_broker(host, instance=0)
  host[:pcp_broker_instance] = instance
  on(host, "cd #{GIT_CLONE_FOLDER}/pcp-broker#{instance}; export LEIN_ROOT=ok; \
     lein with-profile internal-mirrors tk </dev/null >/var/log/puppetlabs/pcp-broker.log 2>&1 &")
  assert(port_open_within?(host, PCP_BROKER_PORTS[instance], 60),
         "pcp-broker port #{PCP_BROKER_PORTS[instance].to_s} not open within 1 minutes of starting the broker")
  broker_state = nil
  attempts = 0
  until broker_state == "running" or attempts == 100 do
    broker_state = get_pcp_broker_status(host)
    if broker_state != "running"
      attempts += 1
      sleep 0.5
    end
  end
  assert_equal("running", broker_state, "Shortly after startup, pcp-broker should report its state as being 'running'")
end

def kill_all_pcp_brokers(host)
  on(host, "ps -C java -f | grep pcp-broker | sed 's/[^0-9]*//' | cut -d\\  -f1") do |result|
    pids = result.stdout.chomp.split("\n")
    pids.each do |pid|
      on(host, "kill -9 #{pid}")
    end
  end
end

def block_pcp_broker(host, instance)
  on(host, "iptables -A INPUT -p tcp --destination-port #{PCP_BROKER_PORTS[instance].to_s} -j DROP; iptables -S")
end

def unblock_pcp_broker(host, instance)
  host[:pcp_broker_instance] = instance
  on(host, "iptables -D INPUT -p tcp --destination-port #{PCP_BROKER_PORTS[instance].to_s} -j DROP; iptables -S")
end<commit_msg>(maint) Ensure all broker logs are unique so they can be preserved<commit_after>require 'pxp-agent/config_helper.rb'
require 'pcp/client'
require 'pcp/simple_logger'
require 'net/http'
require 'openssl'
require 'socket'
require 'json'
require 'securerandom'

# This file contains general test helper methods for pxp-agent acceptance tests

# The standard path for git checkouts is where pcp-broker will be
GIT_CLONE_FOLDER = '/opt/puppet-git-repos'

# Some helpers for working with the log file
PXP_LOG_DIR_CYGPATH = '/cygdrive/c/ProgramData/PuppetLabs/pxp-agent/var/log'
PXP_LOG_DIR_POSIX = '/var/log/puppetlabs/pxp-agent'

def logdir(host)
  windows?(host)?
    PXP_LOG_DIR_CYGPATH :
    PXP_LOG_DIR_POSIX
end

# @param host the beaker host you want the path to the log file on
# @return The path to the log file on the host. For Windows, a cygpath is returned
def logfile(host)
  logdir(host)+'/pxp-agent.log'
end

def reset_logfile(host)
  old_logfile = logfile(host)
  new_logfile = old_logfile+'.'+SecureRandom.uuid
  logger.notify "---- Saving pxp-agent logfile to #{new_logfile}"
  retry_on(host, "test ! -f #{old_logfile} || mv #{old_logfile} #{new_logfile}")
end

# A general helper for waiting for a file to contain some string, and failing the test if it doesn't appear
# @param host the host to check the file on
# @param file path to file to check
# @param expected the string or pattern expected in the file
# @param seconds number of seconds to retry (one retry per second). Default 30.
def expect_file_on_host_to_contain(host, file, expected, seconds=30)
  # If the expected file entry does not appear in the file within 30 seconds, then do an explicit assertion
  # that the file should contain the expected text, so we get a test fail (not an unhandled error), and the
  # log contents will appear in the test failure output
  begin
    retry_on(host, "grep '#{expected}' #{file}", {:max_retries => 30,
                                                     :retry_interval => 1})
  rescue
    on(host, "cat #{file}") do |result|
      assert_match(expected, result.stdout,
                  "Expected text '#{expected}' did not appear in file #{file}")
    end
  end
end

# Some helpers for working with a pcp-broker 'lein tk' instance
def run_pcp_broker(host, instance=0)
  host[:pcp_broker_instance] = instance
  on(host, "cd #{GIT_CLONE_FOLDER}/pcp-broker#{instance}; export LEIN_ROOT=ok; \
     lein with-profile internal-mirrors tk </dev/null >/var/log/puppetlabs/pcp-broker.log.#{SecureRandom.uuid} 2>&1 &")
  assert(port_open_within?(host, PCP_BROKER_PORTS[instance], 60),
         "pcp-broker port #{PCP_BROKER_PORTS[instance].to_s} not open within 1 minutes of starting the broker")
  broker_state = nil
  attempts = 0
  until broker_state == "running" or attempts == 100 do
    broker_state = get_pcp_broker_status(host)
    if broker_state != "running"
      attempts += 1
      sleep 0.5
    end
  end
  assert_equal("running", broker_state, "Shortly after startup, pcp-broker should report its state as being 'running'")
end

def kill_all_pcp_brokers(host)
  on(host, "ps -C java -f | grep pcp-broker | sed 's/[^0-9]*//' | cut -d\\  -f1") do |result|
    pids = result.stdout.chomp.split("\n")
    pids.each do |pid|
      on(host, "kill -9 #{pid}")
    end
  end
end

def block_pcp_broker(host, instance)
  on(host, "iptables -A INPUT -p tcp --destination-port #{PCP_BROKER_PORTS[instance].to_s} -j DROP; iptables -S")
end

def unblock_pcp_broker(host, instance)
  host[:pcp_broker_instance] = instance
  on(host, "iptables -D INPUT -p tcp --destination-port #{PCP_BROKER_PORTS[instance].to_s} -j DROP; iptables -S")
end
{'fraction_non_alphanumeric': 0.108298755186722, 'fraction_numerical': 0.007192254495159059, 'mean_word_length': 3.846514745308311, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1068385', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2484, 'n_words': 796}