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,13 +34,13 @@ shared_examples "logstash generate" do |logstash|
GENERATE_TYPES.each do |type| GENERATE_TYPES.each do |type|
context "with type #{type}" do context "with type #{type}" do
it "successfully generate the plugin skeleton" 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") expect(logstash).to File.directory?("logstash-#{type}-qatest-generated")
end end
it "successfully install the plugin" do 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(command).to install_successfully
expect(logstash).to have_installed?("logstash-#{type}-qatest-generated") expect(logstash).to have_installed?("logstash-#{type}-qatest-generated")
end end
end end
end end

View file

@ -42,7 +42,7 @@ shared_examples "logstash install" do |logstash|
after(:each) { logstash.delete_file(gem_tmp_path) } after(:each) { logstash.delete_file(gem_tmp_path) }
it "successfully install the plugin" do 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(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-dns") expect(logstash).to have_installed?("logstash-filter-dns")
expect(logstash).not_to be_running expect(logstash).not_to be_running
@ -54,7 +54,7 @@ shared_examples "logstash install" do |logstash|
context "when fetching a gem from rubygems" do context "when fetching a gem from rubygems" do
it "successfully install the plugin" 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(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest") expect(logstash).to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running expect(logstash).not_to be_running
@ -64,7 +64,7 @@ shared_examples "logstash install" do |logstash|
end end
it "successfully install the plugin when verification is disabled" do 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(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest") expect(logstash).to have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running expect(logstash).not_to be_running
@ -74,7 +74,7 @@ shared_examples "logstash install" do |logstash|
end end
it "fails when installing a non logstash plugin" do 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(command).not_to install_successfully
expect(logstash).not_to be_running expect(logstash).not_to be_running
with_running_logstash_service(logstash) do with_running_logstash_service(logstash) do
@ -83,7 +83,7 @@ shared_examples "logstash install" do |logstash|
end end
it "allow to install a specific version" do 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(command).to install_successfully
expect(logstash).to have_installed?("logstash-filter-qatest", "0.1.0") expect(logstash).to have_installed?("logstash-filter-qatest", "0.1.0")
with_running_logstash_service(logstash) do with_running_logstash_service(logstash) do
@ -95,7 +95,7 @@ shared_examples "logstash install" do |logstash|
context "when the plugin doesnt exist" do context "when the plugin doesnt exist" do
it "fails to install and report an error" 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/) expect(command.stderr).to match(/Plugin not found, aborting/)
with_running_logstash_service(logstash) do with_running_logstash_service(logstash) do
expect(logstash).to be_running 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 context "when the integration is installed" do
before(:each) 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 end
context "trying to install an inner plugin separately" do context "trying to install an inner plugin separately" do
it "fails to install" 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/) expect(result.stderr).to match(/is already provided by/)
end end
end end
@ -46,11 +46,11 @@ shared_examples "integration plugins compatible" do |logstash|
# Muting test. Tracked in https://github.com/elastic/logstash/issues/10459 # Muting test. Tracked in https://github.com/elastic/logstash/issues/10459
xcontext "if an inner plugin is installed" do xcontext "if an inner plugin is installed" do
before(:each) 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 end
it "installing the integrations uninstalls the inner plugin" do it "installing the integrations uninstalls the inner plugin" 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")
result = logstash.run_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq") result = logstash.run_sudo_command_in_path("bin/logstash-plugin list logstash-input-rabbitmq")
expect(result.stdout).to_not match(/^logstash-input-rabbitmq/) expect(result.stdout).to_not match(/^logstash-input-rabbitmq/)
end end
end end
@ -70,11 +70,11 @@ shared_examples "integration plugins compatible" do |logstash|
context "when the integration is installed" do context "when the integration is installed" do
before(:each) 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 end
context "trying to uninstall an inner plugin" do context "trying to uninstall an inner plugin" do
it "fails to uninstall it" 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/) expect(result.stderr).to match(/is already provided by/)
end end
end end
@ -94,16 +94,16 @@ shared_examples "integration plugins compatible" do |logstash|
context "when the integration is installed" do context "when the integration is installed" do
before(:each) 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 end
context "listing an integration" do 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 it "shows its inner plugin" do
expect(result.stdout).to match(/logstash-input-rabbitmq/m) expect(result.stdout).to match(/logstash-input-rabbitmq/m)
end end
end end
context "listing an inner plugin" do 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 it "matches the integration that contains it" do
expect(result.stdout).to match(/logstash-integration-rabbitmq/m) expect(result.stdout).to match(/logstash-integration-rabbitmq/m)
end end

View file

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

View file

@ -32,17 +32,17 @@ shared_examples "logstash remove" do |logstash|
context "when the plugin isn't installed" do context "when the plugin isn't installed" do
it "fails to remove it" 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/) expect(result.stderr).to match(/This plugin has not been previously installed/)
end end
end end
context "when the plugin is installed" do context "when the plugin is installed" do
it "successfully removes it" 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") 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 have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running expect(logstash).not_to be_running
with_running_logstash_service(logstash) do 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 context "when the plugin isn't installed" do
it "fails to uninstall it" 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/) expect(result.stderr).to match(/This plugin has not been previously installed/)
end end
end end
context "when the plugin is installed" do context "when the plugin is installed" do
it "successfully uninstall it" 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") 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 have_installed?("logstash-filter-qatest")
expect(logstash).not_to be_running expect(logstash).not_to be_running
with_running_logstash_service(logstash) do with_running_logstash_service(logstash) do

View file

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

View file

@ -144,6 +144,14 @@ module ServiceTester
client.uninstall(name) client.uninstall(name)
end 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) def run_command_in_path(cmd)
client.run_command_in_path(cmd) client.run_command_in_path(cmd)
end end

View file

@ -24,8 +24,8 @@ LS_BUILD_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '
class Command class Command
def initialize() def initialize()
@stdout, @stderr, @exit_status = nil @stdout, @stderr, @exit_status = nil
end end
def stdout def stdout
@stdout @stdout
end end
@ -47,12 +47,16 @@ class Command
end end
end end
def sudo_exec!(cmd) def exec!(cmd)
command = Command.new() command = Command.new()
command.execute("sudo #{cmd}") command.execute("#{cmd}")
return command return command
end end
def sudo_exec!(cmd)
exec!("sudo #{cmd}")
end
module ServiceTester module ServiceTester
class InstallException < Exception; end class InstallException < Exception; end
@ -68,16 +72,22 @@ module ServiceTester
service_manager(service, "stop") service_manager(service, "stop")
end end
def run_sudo_command(cmd)
sudo_exec!("JARS_SKIP='true' #{cmd}")
end
def run_command(cmd) def run_command(cmd)
response = nil exec!("JARS_SKIP='true' #{cmd}")
response = sudo_exec!("JARS_SKIP='true' #{cmd}")
response
end end
def replace_in_gemfile(pattern, replace) def replace_in_gemfile(pattern, replace)
gemfile = File.join(LOGSTASH_PATH, "Gemfile") gemfile = File.join(LOGSTASH_PATH, "Gemfile")
cmd = "sed -i.sedbak 's/#{pattern}/#{replace}/' #{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 end
def run_command_in_path(cmd) def run_command_in_path(cmd)
@ -86,10 +96,10 @@ module ServiceTester
def plugin_installed?(plugin_name, version = nil) def plugin_installed?(plugin_name, version = nil)
if 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 search_token = plugin_name
else 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})" search_token = "#{plugin_name} (#{version})"
end end