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

(maint) Add testing around exec via Puppet #583

Merged
merged 3 commits into from
Jun 29, 2017
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
24 changes: 14 additions & 10 deletions acceptance/lib/pxp-agent/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,6 @@ def rpc_request(broker, targets,
end
end

message = PCP::Message.new({
:message_type => blocking ? 'http://puppetlabs.com/rpc_blocking_request' : 'http://puppetlabs.com/rpc_non_blocking_request',
:targets => targets
})

message_data = {
:transaction_id => transaction_id,
:module => pxp_module,
Expand All @@ -305,13 +300,22 @@ def rpc_request(broker, targets,
if !blocking then
message_data[:notify_outcome] = false
end
message.data = message_data.to_json

message_expiry = 10 # Seconds for the PCP message to be considered failed
rpc_action_expiry = 60 # Seconds for the entire RPC action to be considered failed
message.expires(message_expiry)
targets.each do |target|
message = PCP::Message.new({
:message_type => blocking ? 'http://puppetlabs.com/rpc_blocking_request' : 'http://puppetlabs.com/rpc_non_blocking_request',
:targets => [target]
})

client.send(message)
message.data = message_data.to_json

message_expiry = 10 # Seconds for the PCP message to be considered failed
message.expires(message_expiry)
Copy link
Contributor

@mruzicka mruzicka Jun 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we get rid of the message_expiry variable altogether?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's no longer needed, so we could remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tempted to ignore it for now though.


client.send(message)
end

rpc_action_expiry = 60 # Seconds for the entire RPC action to be considered failed

begin
Timeout::timeout(rpc_action_expiry) do
Expand Down
87 changes: 87 additions & 0 deletions acceptance/tests/pxp-module-puppet/run_puppet_exec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'pxp-agent/test_helper.rb'
require 'puppet/acceptance/environment_utils'

test_name 'C98107 - Run puppet with an exec' do

extend Puppet::Acceptance::EnvironmentUtils

env_name = test_file_name = File.basename(__FILE__, '.*')
environment_name = mk_tmp_environment(env_name)

step 'On master, create a new environment that execs hostname' do
site_manifest = "#{environmentpath}/#{environment_name}/manifests/site.pp"
create_remote_file(master, site_manifest, <<-SITEPP)
node default {
exec { "hostname":
path => ["/bin", "/usr/bin", "C:/cygwin32/bin", "C:/cygwin64/bin"],
Copy link
Contributor

@joshcooper joshcooper Jul 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikaelSmith this is failing CI when running 32-bit puppet/ruby on 32-bit windows (using the win-10-ent-i386 template), because the path needs to be C:/cygwin/bin. I think the C:/cygwin32 would only happen when using 32-bit cygwin on 64-bit windows, which shouldn't happen in our current set of windows templates.

Note the puppet-agent pipelines alternate architectures, which is why it was failing yesterday, but not today: https://jenkins-master-prod-1.delivery.puppetlabs.net/view/puppet-agent/view/master/view/Suite/job/platform_puppet-agent_intn-van-sys_suite-daily-pxp-agent-master/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logoutput => true,
}
}
SITEPP
on(master, "chmod 644 #{site_manifest}")
end

step 'Ensure each agent host has pxp-agent running and associated' do
agents.each do |agent|
on agent, puppet('resource service pxp-agent ensure=stopped')
create_remote_file(agent, pxp_agent_config_file(agent), pxp_config_json_using_puppet_certs(master, agent).to_s)
on agent, puppet('resource service pxp-agent ensure=running')

assert(is_associated?(master, "pcp://#{agent}/agent"),
"Agent #{agent} with PCP identity pcp://#{agent}/agent should be associated with pcp-broker")
end
end

def last_run_report(h)
if h['platform'] =~ /windows/
'C:/ProgramData/PuppetLabs/puppet/cache'
else
'/opt/puppetlabs/puppet/cache'
end + '/state/last_run_report.yaml'
end

step "Send an rpc_blocking_request to all agents" do
target_identities = []
agents.each do |agent|
target_identities << "pcp://#{agent}/agent"
end
responses = nil # Declare here so not local to begin/rescue below
begin
responses = rpc_blocking_request(master, target_identities,
'pxp-module-puppet', 'run',
{:env => [], :flags => ['--environment', environment_name]})
rescue => exception
fail("Exception occurred when trying to run Puppet on all agents: #{exception.message}")
end
agents.each_with_index do |agent|
step "Check Run Puppet response for #{agent}" do
identity = "pcp://#{agent}/agent"
assert(responses[identity][:data].has_key?("results"),
"Agent #{agent} received a response for rpc_blocking_request but it is missing a 'results' entry: #{responses[identity]}")
action_result = responses[identity][:data]["results"]
# The test's pass/fail criteria is only the value of 'status'. However, if something goes wrong and Puppet needs to default
# the environment to 'production' and results in 'unchanged' then it's better to fail specifically on the environment.
assert(action_result.has_key?('environment'), "Results for pxp-module-puppet run on #{agent} should contain an 'environment' field")
assert_equal(environment_name, action_result['environment'], "Result of pxp-module-puppet run on #{agent} should run with the "\
"#{environment_name} environment")
assert(action_result.has_key?('status'), "Results for pxp-module-puppet run on #{agent} should contain a 'status' field")
assert_equal('changed', action_result['status'], "Result of pxp-module-puppet run on #{agent} should be 'changed'")
end

step "Check run report for #{agent}" do
result = on(agent, "cat #{last_run_report(agent)}")

# Parse the last run report, ignoring object tags
data = YAML.parse(result.stdout)
data.root.each do |o|
o.tag = nil if o.respond_to?(:tag=)
end
data = data.to_ruby

hostname = on(agent, 'hostname').stdout.chomp
expected = data['logs'].select {|log| log['source'] =~ /Exec\[hostname\]/}.select {|log| log['message'] == hostname}
assert_equal(1, expected.count, 'puppet failed to exec hostname')
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@
transaction_ids = start_puppet_non_blocking_request(master, target_identities)
end

teardown do
# Make sure we stop sleep processes when the test finishes, to avoid leaving stranded processes running.
stop_sleep_process(agents)
end

agents.each do |agent|
step "Kill the first agent run on #{agent}" do
lockfile = nil
Expand All @@ -75,6 +70,10 @@
else
on agent, "kill -9 #{pid}"
end

# Also halt the sleep process here, as on Windows the parent process won't exit while the child process
# has an open handle shared with it (the stdout pipe).
stop_sleep_process(agent)
end
end

Expand Down