Skip to content

Commit

Permalink
Merge pull request #6011 from r-richardson/json_array_settings_166154
Browse files Browse the repository at this point in the history
Consistently handle settings with multiple values
  • Loading branch information
mergify[bot] authored Oct 18, 2024
2 parents 9ff0bc1 + b7ab64d commit 5cfb4dc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/OpenQA/Schema/ResultSet/Jobs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package OpenQA::Schema::ResultSet::Jobs;
use Mojo::Base 'DBIx::Class::ResultSet', -signatures;
use DBIx::Class::Timestamps 'now';
use Date::Format 'time2str';
use Encode qw(decode_utf8);
use File::Basename 'basename';
use IPC::Run;
use OpenQA::App;
Expand Down Expand Up @@ -112,7 +113,7 @@ sub create_from_settings {
my ($self, $settings, $scheduled_product_id) = @_;

my %settings = %$settings;
my %new_job_args = (TEST => $settings{TEST});
my %new_job_args;

my $result_source = $self->result_source;
my $schema = $result_source->schema;
Expand Down Expand Up @@ -171,6 +172,12 @@ sub create_from_settings {
}
}

for my $key (keys %settings) {
my $value = $settings{$key};
$settings{$key} = decode_utf8 encode_json $value if (ref $value eq 'ARRAY' || ref $value eq 'HASH');
}
$new_job_args{TEST} = $settings{TEST};

# move important keys from the settings directly to the job
for my $key (OpenQA::Schema::Result::Jobs::MAIN_SETTINGS) {
if (my $value = delete $settings{$key}) { $new_job_args{$key} = $value }
Expand Down
15 changes: 14 additions & 1 deletion t/10-jobs.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use FindBin;
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
use Mojo::Base -signatures;
use autodie ':all';
use Encode;
use File::Copy;
use OpenQA::Jobs::Constants;
use OpenQA::Test::Case;
Expand All @@ -26,6 +25,8 @@ use Mojo::JSON qw(decode_json encode_json);
use OpenQA::Test::Utils qw(perform_minion_jobs);
use OpenQA::Test::TimeLimit '30';

binmode(STDOUT, ":encoding(UTF-8)");

my $schema_name = OpenQA::Test::Database::generate_schema_name;
my $schema = OpenQA::Test::Case->new->init_data(
fixtures_glob => '01-jobs.pl 05-job_modules.pl 06-job_dependencies.pl',
Expand Down Expand Up @@ -1006,4 +1007,16 @@ subtest 'get all setting values for a job/key in a sorted array' => sub {
is_deeply $job_settings->all_values_sorted(99926, 'WORKER_CLASS'), [qw(bar baz foo)], 'all values returned';
};

subtest 'handling of array and unicode settings' => sub {
my %s = %settings;
$s{TEST} = 'array_setting_test';
$s{ARRAY_SETTING} = ['value1', 'ä', ''];
my $job = _job_create(\%s);
my $entry = $job->settings->find({key => 'ARRAY_SETTING'});
ok $entry, 'ARRAY_SETTING exists in job settings';
my $v = $entry->value;
is ref($v), '', 'ARRAY_SETTING value is stored as a string';
is $v, '["value1","ä","…"]', 'retrieved correctly encoded JSON array as string';
};

done_testing();

0 comments on commit 5cfb4dc

Please sign in to comment.