Skip to content

Commit d934419

Browse files
qa: use clean expansion of LS tarball per fixture instance (#17082) (#17095)
* qa: use clean expansion of LS tarball per fixture instance Because QA tests can _modify_ the Logstash installation (e.g. those that invoke the plugin manager), it is important that the service wrapper begins with a clean expansion of the logstash tarball. * qa: enable safe reuse of ls_home in ls_to_ls tests (cherry picked from commit d20eb4d) Co-authored-by: Ry Biesemeyer <[email protected]>
1 parent 905e28b commit d934419

File tree

4 files changed

+78
-22
lines changed

4 files changed

+78
-22
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def qaBuildPath = "${buildDir}/qa/integration"
418418
def qaVendorPath = "${qaBuildPath}/vendor"
419419

420420
tasks.register("installIntegrationTestGems") {
421-
dependsOn unpackTarDistribution
421+
dependsOn assembleTarDistribution
422422
def gemfilePath = file("${projectDir}/qa/integration/Gemfile")
423423
inputs.files gemfilePath
424424
inputs.files file("${projectDir}/qa/integration/integration_tests.gemspec")

qa/integration/framework/settings.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
require 'yaml'
19+
require 'delegate'
1920

2021
# All settings for a test, global and per test
2122
class TestSettings
@@ -76,4 +77,23 @@ def feature_config_dir
7677
feature = feature_flag
7778
feature.empty? ? nil : File.join(FIXTURES_DIR, feature)
7879
end
80+
81+
def override(kv_map)
82+
Override.new(self, kv_map)
83+
end
84+
85+
class Override < SimpleDelegator# quacks like TestSettings
86+
def initialize(test_settings, overrides)
87+
super(test_settings)
88+
@overrides = overrides
89+
end
90+
91+
def is_set?(key)
92+
@overrides.include?(key) || super
93+
end
94+
95+
def get(key)
96+
@overrides.fetch(key) { super }
97+
end
98+
end
7999
end

qa/integration/services/logstash_service.rb

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,58 @@ def initialize(settings, api_port = 9600)
5353
if @settings.is_set?("ls_home_abs_path")
5454
@logstash_home = @settings.get("ls_home_abs_path")
5555
else
56-
# use the LS which was just built in source repo
57-
ls_version_file = YAML.load_file(LS_VERSION_FILE)
58-
ls_file = "logstash-" + ls_version_file["logstash"]
59-
# First try without the snapshot if it's there
60-
@logstash_home = File.expand_path(File.join(LS_BUILD_DIR, ls_file), __FILE__)
61-
@logstash_home += "-SNAPSHOT" unless Dir.exist?(@logstash_home)
62-
63-
puts "Using #{@logstash_home} as LS_HOME"
64-
@logstash_bin = File.join("#{@logstash_home}", LS_BIN)
65-
raise "Logstash binary not found in path #{@logstash_home}" unless File.file? @logstash_bin
56+
@logstash_home = clean_expand_built_tarball
6657
end
6758

59+
puts "Using #{@logstash_home} as LS_HOME"
60+
@logstash_bin = File.join("#{@logstash_home}", LS_BIN)
61+
raise "Logstash binary not found in path #{@logstash_home}" unless File.file? @logstash_bin
62+
6863
@default_settings_file = File.join(@logstash_home, LS_CONFIG_FILE)
6964
@monitoring_api = MonitoringAPI.new(api_port)
7065
end
7166

67+
##
68+
# @return [String] the path to a CLEAN expansion of the locally-built tarball
69+
def clean_expand_built_tarball
70+
build_dir = File.expand_path(LS_BUILD_DIR, __FILE__) # source of tarball
71+
target_dir = File.join(build_dir, "qa-fixture")
72+
73+
# find the built tarball matching the current version, preferring non-SNAPSHOT
74+
ls_version = YAML.load_file(LS_VERSION_FILE).fetch("logstash")
75+
candidates = %W(
76+
logstash-#{ls_version}.tar.gz
77+
logstash-#{ls_version}-SNAPSHOT.tar.gz
78+
)
79+
80+
candidates.each do |tarball_candidate|
81+
tarball_candidate_path = File.join(build_dir, tarball_candidate)
82+
if File.exist?(tarball_candidate_path)
83+
expected_untar_directory = File.basename(tarball_candidate, ".tar.gz")
84+
result_logstash_home = File.join(target_dir, expected_untar_directory)
85+
86+
if Dir.exist?(result_logstash_home)
87+
puts "expunging(#{result_logstash_home})"
88+
# FileUtils#rm_rf cannot be used here because it silently fails to remove the bundled jdk on MacOS
89+
expunge_result = `rm -rf #{Shellwords.escape(result_logstash_home)} 2>&1`
90+
fail("ERROR EXPUNGING: #{expunge_result}") unless $?.success?
91+
end
92+
93+
puts "expanding(#{tarball_candidate_path})"
94+
FileUtils.mkdir_p(target_dir) unless Dir.exist?(target_dir)
95+
FileUtils.chdir(target_dir) do
96+
expand_result = `tar -xzf #{Shellwords.escape(tarball_candidate_path)} 2>&1`
97+
fail("ERROR EXPANDING: #{expand_result}") unless $?.success?
98+
end
99+
100+
return result_logstash_home
101+
end
102+
end
103+
104+
fail("failed to find any matching build tarballs (looked for `#{candidates}` in `#{build_dir}`)")
105+
end
106+
private :clean_expand_built_tarball
107+
72108
def alive?
73109
if @process.nil? || @process.exited?
74110
raise "Logstash process is not up because of an error, or it stopped"

qa/integration/specs/logstash_to_logstash_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@
3636
@fixture.teardown
3737
}
3838

39-
def change_logstash_setting(logstash_service, name, value)
40-
settings = {}.tap do |settings|
41-
settings[name] = value
42-
end
43-
IO.write(logstash_service.application_settings_file, settings.to_yaml)
44-
end
45-
4639
def get_temp_path_dir
4740
tmp_path = Stud::Temporary.pathname
4841
tmp_data_path = File.join(tmp_path, "data")
@@ -51,10 +44,17 @@ def get_temp_path_dir
5144
end
5245

5346
def run_logstash_instance(config_name, options = {})
54-
api_port = 9600 + rand(1000)
55-
logstash_service = LogstashService.new(@fixture.settings, api_port)
56-
change_logstash_setting(logstash_service, "api.http.port", api_port)
57-
logstash_service.spawn_logstash("-f", config_to_temp_file(@fixture.config(config_name, options)), "--path.data", get_temp_path_dir)
47+
@next_api_port_offset = (@next_api_port_offset||0).next.modulo(1000) # cycle through 1000 possibles
48+
api_port = 9600 + @next_api_port_offset
49+
50+
# to avoid LogstashService's clean-from-tarball default behaviour, we need
51+
# to tell it where our LOGSTASH_HOME is in the existing service
52+
existing_fixture_logstash_home = @fixture.get_service("logstash").logstash_home
53+
logstash_service = LogstashService.new(@fixture.settings.override("ls_home_abs_path" => existing_fixture_logstash_home), api_port)
54+
55+
logstash_service.spawn_logstash("--path.config", config_to_temp_file(@fixture.config(config_name, options)),
56+
"--path.data", get_temp_path_dir,
57+
"--api.http.port", api_port.to_s)
5858
wait_for_logstash(logstash_service)
5959
logstash_service
6060
end

0 commit comments

Comments
 (0)