Skip to content

Commit

Permalink
Consistently handle settings with multiple values
Browse files Browse the repository at this point in the history
Using JSON syntax to consistently store array values.

Related ticket: https://progress.opensuse.org/issues/166154
  • Loading branch information
r-richardson committed Oct 17, 2024
1 parent 726e974 commit 10474bd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
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;
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
34 changes: 34 additions & 0 deletions t/10-jobs.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 +1008,36 @@ 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 settings' => sub {
my %_settings = %settings;
$_settings{TEST} = 'array_setting_test';
$_settings{ARRAY_SETTING} = ["value1", "value2", "value3"];
my $job = _job_create(\%_settings);

my $array_setting_entry = $job->settings->find({key => 'ARRAY_SETTING'});
ok($array_setting_entry, 'ARRAY_SETTING exists in job settings');

my $array_setting_value = $array_setting_entry->value;
is(ref($array_setting_value), '', 'ARRAY_SETTING value is stored as a string');

my $decoded_value = decode_json($array_setting_value);

is_deeply($decoded_value, ["value1", "value2", "value3"], 'ARRAY_SETTING value stored and retrieved correctly');
};

subtest 'handling of Umlaute in settings' => sub {
my %_settings = %settings;
$_settings{TEST} = 'umlaut_test';
$_settings{UNICODE} = ['ä', ''];

my $job = _job_create(\%_settings);

my $umlaute_entry = $job->settings->find({key => 'UNICODE'});
ok($umlaute_entry, 'Umlaut value exists in job settings');

my $umlaute_value = $umlaute_entry->value;
is($umlaute_value, '["ä","…"]', 'Umlaute values stored and retrieved correctly');
};


done_testing();
36 changes: 36 additions & 0 deletions t/ui/18-tests-details.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ sub prepare_database {

my $assets = $schema->resultset('Assets');
$assets->find({type => 'iso', name => 'openSUSE-13.1-DVD-i586-Build0091-Media.iso'})->update({size => 0});

my $umlaute_job = $jobs->create_from_settings(
{
TEST => 'umlaute_test',
UMLAUTS => 'äöüßÄÖÜ',
MULTI_VALUE => ["Value1", "Value2", "Value3"],
MULTI_VALUE_UMLAUT => ['VäÄlüÜöÖ', 'Scharfesß'],
},
undef, # scheduled_product_id
);
$umlaute_job->update({id => 99999});
}

prepare_database;
Expand Down Expand Up @@ -332,6 +343,7 @@ subtest 'running job' => sub {
isnt($test_modules_table, undef, 'results table shown') or return undef;
is(scalar @{$driver->find_child_elements($test_modules_table, 'tbody tr')}, 0, 'no results shown so far');
};

subtest 'test module table is populated (without reload) when test modules become available' => sub {
$job_modules->search({job_id => 99961})->update({job_id => 99963});
update_status sub { scalar @{$driver->find_elements('#results .result')} >= 1 }, 'test modules shown';
Expand All @@ -352,6 +364,30 @@ subtest 'running job' => sub {
ok $step_detail_element, 'step detail still present' or return;
like $step_detail_element->get_text, qr/some text result/, 'text result finally shown';
};

subtest 'Multi value settings are displayed correctly' => sub {
# For multi-value settings we expect a JSON formated string containing all elements
$driver->get('/tests/99999');
$driver->find_element_by_link_text('Settings')->click();
wait_for_ajax(msg => 'settings tab loaded');

my $umlaute_value = $driver->find_element("//td[text()='UMLAUTS']/following-sibling::td", 'xpath');
is($umlaute_value->get_text(), 'äöüßÄÖÜ', 'Umlauts displayed correctly');

my @multi_value_element = $driver->find_elements("//td[text()='MULTI_VALUE']/following-sibling::td", 'xpath');

my $expected_json_string = ('["Value1","Value2","Value3"]');
is($multi_value_element[0]->get_text, $expected_json_string, "Multi-value settings are rendered as JSON");
is($multi_value_element[1], undef);

my @multi_value_element_with_umlauts
= $driver->find_elements("//td[text()='MULTI_VALUE_UMLAUT']/following-sibling::td", 'xpath');

my $expected_umlauts = ('["VäÄlüÜöÖ","Scharfesß"]');
is($multi_value_element_with_umlauts[0]->get_text,
$expected_umlauts, "MULTI_VALUE_UMLAUT value displayed correctly");
is($multi_value_element_with_umlauts[1], undef);
};
};

subtest 'render bugref links in thumbnail text windows' => sub {
Expand Down

0 comments on commit 10474bd

Please sign in to comment.