-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite code for job settings expansion
* Move expand placeholders function to its own module. * rewrite the loop. See: https://progress.opensuse.org/issues/20464
- Loading branch information
Showing
6 changed files
with
292 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (C) 2019 SUSE LLC | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
|
||
package OpenQA::Settings; | ||
|
||
use strict; | ||
use warnings; | ||
|
||
# replace %NAME% with $settings{NAME} | ||
sub expand_placeholders { | ||
my ($settings) = @_; | ||
|
||
my $error_string; | ||
|
||
for my $value (values %$settings) { | ||
next unless defined $value; | ||
|
||
# loop the value until there is no %KEY% in it | ||
while ($value =~ /%(\w+)%/) { | ||
my $key = $1; | ||
last unless defined $settings->{$key}; | ||
|
||
my $replaced_value = $settings->{$key}; | ||
|
||
# used to record already replaced value. If the value has been in this array which means there is a circular reference, then return an error message. | ||
my @replaced_array = (); | ||
|
||
while ($replaced_value =~ /%(\w+)%/) { | ||
last unless defined $settings->{$1}; | ||
if (grep { $_ eq $1 } @replaced_array) { | ||
return "The key $1 is a circular reference, value is " . $settings->{$1}; | ||
} | ||
else { | ||
$replaced_value = $settings->{$1}; | ||
push @replaced_array, $1; | ||
} | ||
} | ||
$value =~ s/(%$key%)/$replaced_value/; | ||
} | ||
} | ||
return undef; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#! /usr/bin/perl | ||
|
||
# Copyright (C) 2019 SUSE LLC | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
|
||
use strict; | ||
use warnings; | ||
|
||
BEGIN { | ||
unshift @INC, 'lib'; | ||
} | ||
|
||
use FindBin; | ||
use lib "$FindBin::Bin/lib"; | ||
use Test::More; | ||
use OpenQA::Settings; | ||
|
||
my $settings = { | ||
BUILD_SDK => '%BUILD_HA%', | ||
BETA => 1, | ||
ISO_MAXSIZE => '4700372992', | ||
SHUTDOWN_NEEDS_AUTH => 1, | ||
HDD_1 => 'SLES-%VERSION%-%ARCH%-%BUILD%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
PUBLISH_HDD_1 => 'SLES-%VERSION%-%ARCH%-%BUILD%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
ANOTHER_JOB => 'SLES-%VERSION%-%ARCH%-%BUILD%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
ARCH => 'x86_64', | ||
BACKEND => 'qemu', | ||
BUILD => '1234', | ||
BUILD_SLE => '%BUILD%', | ||
MACHINE => '64bit', | ||
PATCH => 1, | ||
UPGRADE => 1, | ||
ISO => 'SLE-%VERSION%-%FLAVOR%-%MACHINE%-Build%BUILD%-Media1.iso', | ||
WORKER_CLASS => 'qemu_x86_64', | ||
VERSION => '15-SP1', | ||
FLAVOR => 'Installer-DVD', | ||
ADDONURL_SDK => 'ftp://openqa.suse.de/SLE-%VERSION%-SDK-POOL-%ARCH%-Build%BUILD_SDK%-Media1/', | ||
DEPENDENCY_RESOLVER_FLAG => 1, | ||
DESKTOP => 'textmode', | ||
DEV_IMAGE => 1, | ||
HDDSIZEGB => 50, | ||
INSTALLONLY => 1, | ||
PATTERNS => 'base,minimal', | ||
PUBLISH_PFLASH_VARS => | ||
'SLES-%VERSION%-%ARCH%-%BUILD%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed-uefi-vars.qcow2', | ||
SEPARATE_HOME => 0, | ||
BUILD_HA => '%BUILD%', | ||
BUILD_SES => '%BUILD%', | ||
WORKAROUND_MODULES => 'base,desktop,serverapp,script,sdk', | ||
}; | ||
|
||
subtest expand_placeholders => sub { | ||
my $str = OpenQA::Settings::expand_placeholders($settings); | ||
my $match_settings = { | ||
BUILD_SDK => '1234', | ||
BETA => 1, | ||
ISO_MAXSIZE => '4700372992', | ||
SHUTDOWN_NEEDS_AUTH => 1, | ||
HDD_1 => 'SLES-15-SP1-x86_64-1234@64bit-minimal_with_sdk1234_installed.qcow2', | ||
PUBLISH_HDD_1 => 'SLES-15-SP1-x86_64-1234@64bit-minimal_with_sdk1234_installed.qcow2', | ||
ANOTHER_JOB => 'SLES-15-SP1-x86_64-1234@64bit-minimal_with_sdk1234_installed.qcow2', | ||
ARCH => 'x86_64', | ||
BACKEND => 'qemu', | ||
BUILD => '1234', | ||
BUILD_SLE => '1234', | ||
MACHINE => '64bit', | ||
PATCH => 1, | ||
UPGRADE => 1, | ||
ISO => 'SLE-15-SP1-Installer-DVD-64bit-Build1234-Media1.iso', | ||
WORKER_CLASS => 'qemu_x86_64', | ||
VERSION => '15-SP1', | ||
FLAVOR => 'Installer-DVD', | ||
ADDONURL_SDK => 'ftp://openqa.suse.de/SLE-15-SP1-SDK-POOL-x86_64-Build1234-Media1/', | ||
DEPENDENCY_RESOLVER_FLAG => 1, | ||
DESKTOP => 'textmode', | ||
DEV_IMAGE => 1, | ||
HDDSIZEGB => 50, | ||
INSTALLONLY => 1, | ||
PATTERNS => 'base,minimal', | ||
PUBLISH_PFLASH_VARS => 'SLES-15-SP1-x86_64-1234@64bit-minimal_with_sdk1234_installed-uefi-vars.qcow2', | ||
SEPARATE_HOME => 0, | ||
BUILD_HA => '1234', | ||
BUILD_SES => '1234', | ||
WORKAROUND_MODULES => 'base,desktop,serverapp,script,sdk', | ||
}; | ||
is_deeply($settings, $match_settings, "Settings replaced"); | ||
}; | ||
|
||
subtest circular_reference => sub { | ||
my $circular_settings = { | ||
BUILD_SDK => '%BUILD_HA%', | ||
ISO_MAXSIZE => '4700372992', | ||
HDD_1 => 'SLES-%VERSION%-%ARCH%-%BUILD_HA%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
PUBLISH_HDD_1 => 'SLES-%VERSION%-%ARCH%-%BUILD_HA%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
ANOTHER_JOB => 'SLES-%VERSION%-%ARCH%-%BUILD_HA%@%MACHINE%-minimal_with_sdk%BUILD_SDK%_installed.qcow2', | ||
ARCH => 'x86_64', | ||
BACKEND => 'qemu', | ||
BUILD => '%BUILD_HA%', | ||
BUILD_HA => '%BUILD%', | ||
VERSION => '15-SP1', | ||
MACHINE => '64bit', | ||
}; | ||
my $str = OpenQA::Settings::expand_placeholders($circular_settings); | ||
like($str, qr/The key (\w+) is a circular reference, value is %\w+%/, "circular reference exit successfully"); | ||
}; | ||
|
||
done_testing; |
Oops, something went wrong.