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

Update validator to check for cache only for specific TEST_SUIT_ID #70

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion lib/rspec_tracer/remote_cache/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class AwsError < StandardError; end
def initialize
@s3_bucket, @s3_path = setup_s3
@aws_cli = RSpecTracer.use_local_aws ? 'awslocal' : 'aws'
@use_test_suite_id_cache = ENV['USE_TEST_SUITE_ID_CACHE'] == 'true'
@test_suite_id = ENV['TEST_SUITE_ID'] if @use_test_suite_id_cache
end

def branch_refs?(branch_name)
Expand Down Expand Up @@ -60,7 +62,7 @@ def upload_branch_refs(branch_name, file_name)
end

def cache_files_list(ref)
prefix = "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/"
prefix = @use_test_suite_id_cache && !@test_suite_id.nil? ? "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/#{@test_suite_id}/" : "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/"
Copy link

Choose a reason for hiding this comment

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

The dynamic construction of the prefix for cache file listing is a key part of the proposed enhancement. However, executing shell commands with interpolated variables (#{@aws_cli} s3 ls #{prefix} --recursive) poses a security risk if the variables are not properly sanitized. Consider using Ruby's Open3 library to execute shell commands, which provides more control and helps prevent code injection vulnerabilities.

- `#{@aws_cli} s3 ls #{prefix} --recursive`.chomp.split("\n")
+ require 'open3'
+ stdout, stderr, status = Open3.capture3("#{@aws_cli} s3 ls #{prefix} --recursive")
+ raise AwsError, stderr unless status.success?
+ stdout.chomp.split("\n")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
prefix = @use_test_suite_id_cache && !@test_suite_id.nil? ? "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/#{@test_suite_id}/" : "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/"
prefix = @use_test_suite_id_cache && !@test_suite_id.nil? ? "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/#{@test_suite_id}/" : "s3://#{@s3_bucket}/#{@s3_path}/#{ref}/"


`#{@aws_cli} s3 ls #{prefix} --recursive`.chomp.split("\n")
end
Expand Down
40 changes: 31 additions & 9 deletions lib/rspec_tracer/remote_cache/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ class Validator
def initialize
@test_suite_id = ENV['TEST_SUITE_ID']
@test_suites = ENV['TEST_SUITES']
@use_test_suite_id_cache = ENV['USE_TEST_SUITE_ID_CACHE']
EdwinPhilip marked this conversation as resolved.
Show resolved Hide resolved

if @test_suite_id.nil? ^ @test_suites.nil?
raise(
ValidationError,
'Both the enviornment variables TEST_SUITE_ID and TEST_SUITES are not set'
'Both the environment variables TEST_SUITE_ID and TEST_SUITES are not set'
)
end

setup
end

def valid?(ref, cache_files)
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))

return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count

cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))

cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
if @use_test_suite_id_cache
test_suite_id_specific_validation(ref, cache_files)
else
general_validation(ref, cache_files)
end
end

private
Expand All @@ -36,7 +35,7 @@ def setup
@last_run_files_count = 1
@last_run_files_regex = '/%<ref>s/last_run.json$'
@cached_files_count = CACHE_FILES_PER_TEST_SUITE
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json'
@cached_files_regex = '/%<ref>s/[0-9a-f]{32}/.+.json$'
else
@test_suites = @test_suites.to_i
@test_suites_regex = (1..@test_suites).to_a.join('|')
Expand All @@ -47,6 +46,29 @@ def setup
@cached_files_regex = "/%<ref>s/(#{@test_suites_regex})/[0-9a-f]{32}/.+.json$"
end
end

def general_validation(ref, cache_files)
last_run_regex = Regexp.new(format(@last_run_files_regex, ref: ref))

return false if cache_files.count { |file| file.match?(last_run_regex) } != @last_run_files_count

cache_regex = Regexp.new(format(@cached_files_regex, ref: ref))

cache_files.count { |file| file.match?(cache_regex) } == @cached_files_count
end

def test_suite_id_specific_validation(ref, cache_files)
# Here, we ensure that the regex is dynamically adjusted for the specific test suite
# Adjusting for specific test_suite_id in the regex patterns
last_run_regex = Regexp.new("/#{ref}/#{@test_suite_id}/last_run.json$")
cache_regex = Regexp.new("/#{ref}/#{@test_suite_id}/[0-9a-f]{32}/.+.json$")

# Validate presence of the last run file for the specific test suite
return false unless cache_files.any? { |file| file.match?(last_run_regex) }

# Check if any cache files for the specific test suite are present
cache_files.any? { |file| file.match?(cache_regex) }
end
end
end
end