Skip to content

Commit

Permalink
ProfileCache: prepare_profile_cache checks for valid object template …
Browse files Browse the repository at this point in the history
…before attempting compilation
  • Loading branch information
stdweird committed Feb 3, 2017
1 parent 53a832d commit cd5279d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
40 changes: 39 additions & 1 deletion build-scripts/src/main/perl/Test/Quattor/Panc.pm
Expand Up @@ -22,7 +22,7 @@ use Carp qw(carp croak);
use File::Path qw(mkpath);
use Cwd qw(getcwd);

our @EXPORT = qw(panc panc_annotations
our @EXPORT = qw(panc panc_annotations is_object_template
set_panc_options reset_panc_options get_panc_options
set_panc_includepath get_panc_includepath);

Expand Down Expand Up @@ -126,6 +126,44 @@ sub get_panc_includepath
}
}


=pod
=head2 is_object_template
Given profile name (and optional resourcesdir for relative profile filename),
test if the profile is a valid object template.
=cut

sub is_object_template
{
my ($profile, $resourcesdir) = @_;

$profile .= ".pan" if ($profile !~ m/\.pan$/ );
$profile = "$resourcesdir/$profile" if $resourcesdir && $profile !~ m/^\//;

if (! -f $profile) {
$object->error("Profile $profile does not exist.");
return;
};

my $ok;
open(my $TPL, '<', $profile) or croak("is_object_template failed to open $profile: $!");
while (my $line = <$TPL>) {
chomp($line);
next if ($line =~ m/^\s*($|#)/); # ignore whitespace/comments
$ok = $line =~ m/^\s*object\s*template/;
last;
};
close($TPL);

if (! $ok) {
$object->error("Profile $profile is not valid object template");
};
return $ok;
}

=pod
=head2 Compile pan object template into JSON profile
Expand Down
8 changes: 7 additions & 1 deletion build-scripts/src/main/perl/Test/Quattor/ProfileCache.pm
Expand Up @@ -19,7 +19,7 @@ use File::Path qw(mkpath);
use Cwd qw(getcwd);

use Test::Quattor::Object;
use Test::Quattor::Panc qw(panc set_panc_includepath get_panc_includepath);
use Test::Quattor::Panc qw(panc is_object_template set_panc_includepath get_panc_includepath);

use EDG::WP4::CCM::CacheManager;
use EDG::WP4::CCM::Fetch;
Expand Down Expand Up @@ -200,6 +200,12 @@ sub prepare_profile_cache

my $dirs = get_profile_cache_dirs();

# Failure
if (!is_object_template($profile, $dirs->{resources})) {
note("Profile $profile is not a valid object template");
return 1; # failure
}

my $cachename = profile_cache_name($profile);

my $cache = "$dirs->{cache}/$cachename";
Expand Down
9 changes: 8 additions & 1 deletion build-scripts/src/test/perl/panc.t
Expand Up @@ -5,7 +5,7 @@ use Test::More;
use Cwd;
use Test::Quattor::Panc qw(set_panc_options reset_panc_options
get_panc_options get_panc_includepath set_panc_includepath
panc panc_annotations);
panc panc_annotations is_object_template);


set_panc_options(debug => undef);
Expand All @@ -21,6 +21,13 @@ is_deeply(get_panc_includepath(), \@dirs, "Includepath retrieved");
reset_panc_options();
is_deeply(get_panc_options(), {}, "Options reset");

ok(is_object_template('ok.pan', 'src/test/resources/panc'),
"is_object_template returns true for valid object template");
ok(is_object_template('src/test/resources/panc/ok'),
"is_object_template returns true for valid object template (semi-relative path without extension)");
ok(!is_object_template('notok.pan', 'src/test/resources/panc'),
"is_object_template returns false for invalid object template");

# Test compilation
my $currentdir = getcwd();
is(panc('quattor.pan', 'src/test/resources', 'target/pancout'),
Expand Down
3 changes: 3 additions & 0 deletions build-scripts/src/test/resources/panc/notok.pan
@@ -0,0 +1,3 @@
#

unique template notok;
3 changes: 3 additions & 0 deletions build-scripts/src/test/resources/panc/ok.pan
@@ -0,0 +1,3 @@
# comment

object template ok;

0 comments on commit cd5279d

Please sign in to comment.