Skip to content

Commit

Permalink
Allow cloning example test distribution from UI for running example test
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Sep 19, 2024
1 parent 17d8212 commit 1fd66cc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
19 changes: 19 additions & 0 deletions assets/javascripts/create_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,22 @@ function createTests(form) {
}
});
}

function cloneTests(link) {
const loadingIndication = document.createElement('span');
loadingIndication.append('Cloning test distribution …');
link.parentNode.replaceWith(loadingIndication);
$.ajax({
url: document.getElementById('flash-messages').dataset.cloneUrl,
method: 'POST',
success: function (response) {
location.reload();
},
error: function (xhr, ajaxOptions, thrownError) {
const retryButton = '<br/><a class="btn btn-primary" href="#" onclick="cloneTests(this)">Retry</a>';
const error = xhr.responseJSON?.error ?? xhr.responseText ?? thrownError;
loadingIndication.parentNode.classList.replace('alert-primary', 'alert-danger');
loadingIndication.innerHTML = `Unable to clone: ${error} ${retryButton}`;
}
});
}
1 change: 1 addition & 0 deletions etc/openqa/openqa.ini
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,4 @@ concurrent = 0
[test_preset bar]
title = Some preset
distri = does-not-exist
casedir = http://foo.git
1 change: 1 addition & 0 deletions lib/OpenQA/WebAPI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ sub startup ($self) {

$r->get('/tests')->name('tests')->to('test#list');
$r->get('/tests/create')->name('tests_create')->to('test#create');
$op_auth->post('/tests/clone')->name('tests_clone')->to('test#clone');
# we have to set this and some later routes up differently on Mojo
# < 9 and Mojo >= 9.11
if ($Mojolicious::VERSION > 9.10) {
Expand Down
26 changes: 23 additions & 3 deletions lib/OpenQA/WebAPI/Controller/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,21 @@ sub _load_test_preset ($self, $preset_key) {

sub _load_scenario_definitions ($self, $preset) {
return undef if exists $preset->{scenario_definitions};
return undef unless my $casedir = testcasedir($preset->{distri}, $preset->{version});
return undef unless my $distri = $preset->{distri};
return undef unless my $casedir = testcasedir($distri, $preset->{version});
my $defs_yaml = eval { path($casedir, 'scenario-definitions.yaml')->slurp('UTF-8') };
$preset->{scenario_definitions} = $defs_yaml;
return $self->stash(flash_error => "Unable to read scenario definitions for the specified preset: $@") if $@;
if (my $error = $@) {
return $self->stash(flash_error => "Unable to read scenario definitions for the specified preset: $error")
unless $error =~ /no.*file/i;
my $info = Mojo::ByteStream->new(
qq(You first need to <a href="#" onclick="cloneTests(this)">clone the $distri test distribution</a>.));
return $self->stash(flash_info => $info);
}
my $defs = eval { load_yaml(string => $defs_yaml) };
return $self->stash(flash_error => "Unable to parse scenario definitions for the specified preset: $@") if $@;
my $e = join("\n", @{$self->app->validate_yaml($defs, 'JobScenarios-01.yaml')});
return $self->stash(flash_error => "Unable to validate scenarios definitions of the specified preset:\n$e") if $e;
$preset->{scenario_definitions} = $defs_yaml;
return undef unless my @products = values %{$defs->{products}};
return undef unless my @job_templates = keys %{$defs->{job_templates}};
$preset->{$_} //= $products[0]->{$_} for qw(distri version flavor arch);
Expand All @@ -162,6 +169,19 @@ sub create ($self) {
$self->stash(preset => ($preset // {}));
}

sub clone ($self) {
my $preset = $self->_load_test_preset($self->param('preset'));
return $self->render(status => 400, text => 'unable to find preset') unless defined $preset;
return $self->render(status => 400, text => 'preset has no distri') unless my $distri = $preset->{distri};
return $self->render(status => 400, text => 'preset has no casedir') unless my $casedir = $preset->{casedir};
$self->gru->enqueue_and_keep_track(
task_name => 'git_clone',
task_description => 'cloning test distribution',
task_args => {testcasedir($distri, $preset->{version}) => $casedir}
)->then(sub ($result) { $self->render(json => $result) })

Check warning on line 181 in lib/OpenQA/WebAPI/Controller/Test.pm

View check run for this annotation

Codecov / codecov/patch

lib/OpenQA/WebAPI/Controller/Test.pm#L180-L181

Added lines #L180 - L181 were not covered by tests
->catch(sub ($error, @) { $self->reply->gru_result($error, 400) });
}

sub get_match_param {
my ($self) = @_;

Expand Down
11 changes: 8 additions & 3 deletions t/ui/29-create_tests.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ subtest 'preset not found' => sub {
like $flash_messages->get_text, qr/'foo' does not exist/i, 'error if preset does not exist';
};

subtest 'preset information can be loaded from INI file, error about non-existing scenario definitions' => sub {
subtest 'preset information can be loaded from INI file, note about non-existing scenario definitions' => sub {
$driver->get("$url/tests/create?preset=bar");
my $flash_messages = $driver->find_element_by_id('flash-messages')->get_text;
unlike $flash_messages, qr/does not exist/i, 'preset defined in INI file is available';

like $flash_messages,
qr|can't open.*tests/does-not-exist/scenario-definitions\.yaml|i,
'error about missing scneario definitions, senario definitions looked up under expected location';
qr|You first need to clone the does-not-exist test distribution|i,
'note about cloning test distribution shown';

$driver->find_element_by_link_text('clone the does-not-exist test distribution')->click;
my $error_message = wait_for_element selector => '#flash-messages .alert-danger', description => 'error message';
like $error_message->get_text, qr/No Minion worker available/i, 'expected error shown';
};

kill_driver;
Expand Down
2 changes: 1 addition & 1 deletion templates/webapi/test/create.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<h2><%= title %></h2>

<div id="flash-messages">
<div id="flash-messages" data-clone-url="<%= url_for('tests_clone')->query(preset => param('preset')) %>">
%= include 'layouts/flash_messages';
</div>

Expand Down

0 comments on commit 1fd66cc

Please sign in to comment.