Skip to content

Commit

Permalink
Check existing job groups in openqa-load-templates
Browse files Browse the repository at this point in the history
The `openqa-load-templates` script now checks if a job groups is already present when importing new template data. If a group already exists, there will be no attempt of creating it, but associated templates will still be loaded if the `--update` option is provided.
If the `--update` option is not provided, and a duplicate job group is found, the import will fail.

Related Issue: https://progress.opensuse.org/issues/174808
  • Loading branch information
Robert Richardson committed Jan 7, 2025
1 parent f9ab890 commit 16ace53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 22 additions & 8 deletions script/openqa-load-templates
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,30 @@ sub post_entry ($table, $entry) {
if ($table eq 'JobGroups') {
# Create the group first
my $job_groups_url = $url->clone->path($options{apibase} . '/job_groups');
my $res = $client->post($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error $res unless $res->is_success;

my $existing_group_res = $client->get($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error $existing_group_res unless $existing_group_res->is_success;
my $group_exists = 0;
my $json = $existing_group_res->json;
for my $group (@$json) {
next unless $group->{name} eq $entry->{group_name};
$group_exists = 1
if $options{update}
or die "Job group '$entry->{group_name}' already exists. Use --update to modify existing entries.";
}
unless ($group_exists) {
my $create_res = $client->post($job_groups_url, form => {name => $entry->{group_name}})->res;
print_error($create_res) unless $create_res->is_success;
}
# Post the job template YAML
my $job_templates_url = $url->clone->path($options{apibase} . '/job_templates_scheduling');
$res
= $client->post($job_templates_url,
form => {name => $entry->{group_name}, template => $entry->{template}, schema => 'JobTemplates-01.yaml'})
->res;
print_error $res unless $res->is_success;
my $yaml_res = $client->post(
$job_templates_url,
form => {
name => $entry->{group_name},
template => $entry->{template},
schema => 'JobTemplates-01.yaml'
})->res;
print_error($yaml_res) unless $yaml_res->is_success;
return 1;
}

Expand Down
4 changes: 3 additions & 1 deletion t/40-script_load_dump_templates.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ my $base_args = "--host $host --apikey $apikey --apisecret $apisecret";
$args = "$base_args $filename";
my $expected = qr/JobGroups.+=> \{ added => 1, of => 1 \}/;
test_once $args, $expected, 'Admin may load templates', 0, 'successfully loaded templates';
test_once $args, qr/group with existing name/, 'Duplicate job group', 255, 'failed on duplicate job group';
test_once $args, qr/Use --update to modify/, 'Duplicate job group', 255, 'failed on duplicate job group';
$args = "$base_args --update $filename";
test_once $args, $expected, 'Update with existing job group', 0, 'updated template with existing job group';

subtest 'test changing existing entries' => sub {
# delete job group so that we can load the template again without running into duplicate job group error
Expand Down

0 comments on commit 16ace53

Please sign in to comment.