run acceptance tests as non-root (#16624)

This commit is contained in:
kaisecheng 2024-11-01 19:47:08 +00:00 committed by GitHub
parent 00da72378b
commit 8ce58b8355
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 63 additions and 45 deletions

View file

@ -34,11 +34,11 @@ shared_examples "logstash generate" do |logstash|
GENERATE_TYPES.each do |type|
context "with type #{type}" do
it "successfully generate the plugin skeleton" do
command = logstash.run_command_in_path("bin/logstash-plugin generate --type #{type} --name qatest-generated")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin generate --type #{type} --name qatest-generated")
expect(logstash).to File.directory?("logstash-#{type}-qatest-generated")
end
it "successfully install the plugin" do
command = logstash.run_command_in_path("bin/logstash-plugin install logstash-#{type}-qatest-generated")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-#{type}-qatest-generated")
expect(command).to install_successfully
expect(logstash).to have_installed?("logstash-#{type}-qatest-generated")
end

View file

@ -42,7 +42,7 @@ shared_examples "logstash install" do |logstash|
after(:each) { logstash.delete_file(gem_tmp_path) }
it "successfully install the plugin" do
command = logstash.run_command_in_path("bin/logstash-plugin install #{gem_tmp_path}")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install #{gem_tmp_path}")
expect(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-dns")
expect(logstash).not_to be_running
@ -54,7 +54,7 @@ shared_examples "logstash install" do |logstash|
context "when fetching a gem from rubygems" do
it "successfully install the plugin" do
command = logstash.run_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
expect(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running
@ -64,7 +64,7 @@ shared_examples "logstash install" do |logstash|
end
it "successfully install the plugin when verification is disabled" do
command = logstash.run_command_in_path("bin/logstash-plugin install --no-verify logstash-filter-qatest")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install --no-verify logstash-filter-qatest")
expect(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running
@ -74,7 +74,7 @@ shared_examples "logstash install" do |logstash|
end
it "fails when installing a non logstash plugin" do
command = logstash.run_command_in_path("bin/logstash-plugin install bundler")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install bundler")
expect(command).not_to install_successfully
expect(logstash).not_to be_running
with_running_logstash_service(logstash) do
@ -83,7 +83,7 @@ shared_examples "logstash install" do |logstash|
end
it "allow to install a specific version" do
command = logstash.run_command_in_path("bin/logstash-plugin install --no-verify --version 0.1.0 logstash-filter-qatest")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install --no-verify --version 0.1.0 logstash-filter-qatest")
expect(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest", "0.1.0")
with_running_logstash_service(logstash) do
@ -95,7 +95,7 @@ shared_examples "logstash install" do |logstash|
context "when the plugin doesnt exist" do
it "fails to install and report an error" do
command = logstash.run_command_in_path("bin/logstash-plugin install --no-verify logstash-output-impossible-plugin")
command = logstash.run_sudo_command_in_path("bin/logstash-plugin install --no-verify logstash-output-impossible-plugin")
expect(command.stderr).to match(/Plugin not found, aborting/)
with_running_logstash_service(logstash) do
expect(logstash).to be_running

View file

@ -33,11 +33,11 @@ shared_examples "integration plugins compatible" do |logstash|
context "when the integration is installed" do
before(:each) do
logstash.run_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
end
context "trying to install an inner plugin separately" do
it "fails to install" do
result = logstash.run_command_in_path("bin/logstash-plugin install logstash-input-rabbitmq")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-input-rabbitmq")
expect(result.stderr).to match(/is already provided by/)
end
end
@ -46,11 +46,11 @@ shared_examples "integration plugins compatible" do |logstash|
# Muting test. Tracked in https://github.com/elastic/logstash/issues/10459
xcontext "if an inner plugin is installed" do
before(:each) do
logstash.run_command_in_path("bin/logstash-plugin install logstash-input-rabbitmq")
logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-input-rabbitmq")
end
it "installing the integrations uninstalls the inner plugin" do
logstash.run_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
result = logstash.run_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq")
logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq")
expect(result.stdout).to_not match(/^logstash-input-rabbitmq/)
end
end
@ -70,11 +70,11 @@ shared_examples "integration plugins compatible" do |logstash|
context "when the integration is installed" do
before(:each) do
logstash.run_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
end
context "trying to uninstall an inner plugin" do
it "fails to uninstall it" do
result = logstash.run_command_in_path("bin/logstash-plugin uninstall logstash-input-rabbitmq")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin uninstall logstash-input-rabbitmq")
expect(result.stderr).to match(/is already provided by/)
end
end
@ -94,16 +94,16 @@ shared_examples "integration plugins compatible" do |logstash|
context "when the integration is installed" do
before(:each) do
logstash.run_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-integration-rabbitmq")
end
context "listing an integration" do
let(:result) { logstash.run_command_in_path("bin/logstash-plugin list logstash-integration-rabbitmq") }
let(:result) { logstash.run_sudo_command_in_path("bin/logstash-plugin list logstash-integration-rabbitmq") }
it "shows its inner plugin" do
expect(result.stdout).to match(/logstash-input-rabbitmq/m)
end
end
context "listing an inner plugin" do
let(:result) { logstash.run_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq") }
let(:result) { logstash.run_sudo_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq") }
it "matches the integration that contains it" do
expect(result.stdout).to match(/logstash-integration-rabbitmq/m)
end

View file

@ -35,17 +35,17 @@ shared_examples "logstash list" do |logstash|
context "without a specific plugin" do
it "display a list of plugins" do
result = logstash.run_command_in_path("bin/logstash-plugin list")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list")
expect(result.stdout.split("\n").size).to be > 1
end
it "display a list of installed plugins" do
result = logstash.run_command_in_path("bin/logstash-plugin list --installed")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list --installed")
expect(result.stdout.split("\n").size).to be > 1
end
it "list the plugins with their versions" do
result = logstash.run_command_in_path("bin/logstash-plugin list --verbose")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list --verbose")
stdout = StringIO.new(result.stdout)
stdout.set_encoding(Encoding::UTF_8)
@ -74,12 +74,12 @@ shared_examples "logstash list" do |logstash|
context "with a specific plugin" do
let(:plugin_name) { "logstash-input-stdin" }
it "list the plugin and display the plugin name" do
result = logstash.run_command_in_path("bin/logstash-plugin list #{plugin_name}")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list #{plugin_name}")
expect(result).to run_successfully_and_output(/^#{plugin_name}$/)
end
it "list the plugin with his version" do
result = logstash.run_command_in_path("bin/logstash-plugin list --verbose #{plugin_name}")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin list --verbose #{plugin_name}")
expect(result).to run_successfully_and_output(/^#{plugin_name} \(\d+\.\d+.\d+\)/)
end
end

View file

@ -32,17 +32,17 @@ shared_examples "logstash remove" do |logstash|
context "when the plugin isn't installed" do
it "fails to remove it" do
result = logstash.run_command_in_path("bin/logstash-plugin remove logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin remove logstash-filter-qatest")
expect(result.stderr).to match(/This plugin has not been previously installed/)
end
end
context "when the plugin is installed" do
it "successfully removes it" do
result = logstash.run_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
expect(logstash).to have_installed?("logstash-filter-qatest")
result = logstash.run_command_in_path("bin/logstash-plugin remove logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin remove logstash-filter-qatest")
expect(logstash).not_to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running
with_running_logstash_service(logstash) do

View file

@ -32,17 +32,17 @@ shared_examples "logstash uninstall" do |logstash|
context "when the plugin isn't installed" do
it "fails to uninstall it" do
result = logstash.run_command_in_path("bin/logstash-plugin uninstall logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin uninstall logstash-filter-qatest")
expect(result.stderr).to match(/This plugin has not been previously installed/)
end
end
context "when the plugin is installed" do
it "successfully uninstall it" do
result = logstash.run_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin install logstash-filter-qatest")
expect(logstash).to have_installed?("logstash-filter-qatest")
result = logstash.run_command_in_path("bin/logstash-plugin uninstall logstash-filter-qatest")
result = logstash.run_sudo_command_in_path("bin/logstash-plugin uninstall logstash-filter-qatest")
expect(logstash).not_to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running
with_running_logstash_service(logstash) do

View file

@ -33,8 +33,8 @@ shared_examples "logstash update" do |logstash|
let(:previous_version) { "0.1.0" }
before do
logstash.run_command_in_path("bin/logstash-plugin install --no-verify --version #{previous_version} #{plugin_name}")
logstash.run_command_in_path("bin/logstash-plugin list")
logstash.run_sudo_command_in_path("bin/logstash-plugin install --no-verify --version #{previous_version} #{plugin_name}")
logstash.run_sudo_command_in_path("bin/logstash-plugin list")
expect(logstash).to have_installed?(plugin_name, previous_version)
# Logstash won't update when we have a pinned version in the gemfile so we remove them
logstash.replace_in_gemfile(',[[:space:]]"0.1.0"', "")
@ -42,7 +42,7 @@ shared_examples "logstash update" do |logstash|
context "update a specific plugin" do
it "has executed successfully" do
cmd = logstash.run_command_in_path("bin/logstash-plugin update --no-verify #{plugin_name}")
cmd = logstash.run_sudo_command_in_path("bin/logstash-plugin update --no-verify #{plugin_name}")
expect(cmd.stdout).to match(/Updating #{plugin_name}/)
expect(logstash).to have_installed?(plugin_name, "0.1.1")
expect(logstash).not_to have_installed?(plugin_name, previous_version)
@ -55,7 +55,7 @@ shared_examples "logstash update" do |logstash|
context "update all the plugins" do
it "has executed successfully" do
logstash.run_command_in_path("bin/logstash-plugin update --no-verify")
logstash.run_sudo_command_in_path("bin/logstash-plugin update --no-verify")
expect(logstash).to have_installed?(plugin_name, "0.1.1")
expect(logstash).not_to be_running
with_running_logstash_service(logstash) do

View file

@ -144,6 +144,14 @@ module ServiceTester
client.uninstall(name)
end
def run_sudo_command_in_path(cmd)
client.run_sudo_command_in_path(cmd)
end
def run_sudo_command(cmd)
client.run_sudo_command(cmd)
end
def run_command_in_path(cmd)
client.run_command_in_path(cmd)
end

View file

@ -47,12 +47,16 @@ class Command
end
end
def sudo_exec!(cmd)
def exec!(cmd)
command = Command.new()
command.execute("sudo #{cmd}")
command.execute("#{cmd}")
return command
end
def sudo_exec!(cmd)
exec!("sudo #{cmd}")
end
module ServiceTester
class InstallException < Exception; end
@ -68,16 +72,22 @@ module ServiceTester
service_manager(service, "stop")
end
def run_sudo_command(cmd)
sudo_exec!("JARS_SKIP='true' #{cmd}")
end
def run_command(cmd)
response = nil
response = sudo_exec!("JARS_SKIP='true' #{cmd}")
response
exec!("JARS_SKIP='true' #{cmd}")
end
def replace_in_gemfile(pattern, replace)
gemfile = File.join(LOGSTASH_PATH, "Gemfile")
cmd = "sed -i.sedbak 's/#{pattern}/#{replace}/' #{gemfile}"
run_command(cmd)
run_sudo_command(cmd)
end
def run_sudo_command_in_path(cmd)
run_sudo_command("#{File.join(LOGSTASH_PATH, cmd)}")
end
def run_command_in_path(cmd)
@ -86,10 +96,10 @@ module ServiceTester
def plugin_installed?(plugin_name, version = nil)
if version.nil?
cmd = run_command_in_path("bin/logstash-plugin list")
cmd = run_sudo_command_in_path("bin/logstash-plugin list")
search_token = plugin_name
else
cmd = run_command_in_path("bin/logstash-plugin list --verbose")
cmd = run_sudo_command_in_path("bin/logstash-plugin list --verbose")
search_token = "#{plugin_name} (#{version})"
end