Skip to content

Commit

Permalink
run acceptance tests as non-root (#16624)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisecheng authored Nov 1, 2024
1 parent 00da723 commit 8ce58b8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
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")
expect(command).to install_successfully
expect(logstash).to have_installed?("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
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
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
Expand All @@ -54,7 +54,7 @@

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
Expand All @@ -64,7 +64,7 @@
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
Expand All @@ -74,7 +74,7 @@
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
Expand All @@ -83,7 +83,7 @@
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
Expand All @@ -95,7 +95,7 @@

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

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
Expand All @@ -46,11 +46,11 @@
# 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
Expand All @@ -70,11 +70,11 @@

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
Expand All @@ -94,16 +94,16 @@

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
Expand Down
10 changes: 5 additions & 5 deletions qa/acceptance/spec/shared_examples/cli/logstash-plugin/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@

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)
Expand Down Expand Up @@ -74,12 +74,12 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
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"', "")
end

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)
Expand All @@ -55,7 +55,7 @@

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
Expand Down
8 changes: 8 additions & 0 deletions qa/rspec/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ def uninstall
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
Expand Down
30 changes: 20 additions & 10 deletions qa/rspec/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
class Command
def initialize()
@stdout, @stderr, @exit_status = nil
end
end

def stdout
@stdout
end
Expand All @@ -47,12 +47,16 @@ def execute(cmdline)
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

Expand All @@ -68,16 +72,22 @@ def stop_service(service)
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)
Expand All @@ -86,10 +96,10 @@ def run_command_in_path(cmd)

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

Expand Down

0 comments on commit 8ce58b8

Please sign in to comment.