Skip to content

Commit

Permalink
aii-core: add more ansible tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stdweird committed Oct 22, 2021
1 parent 90c968f commit 539923d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
32 changes: 20 additions & 12 deletions aii-core/src/main/perl/Shellfe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use File::Basename qw(basename dirname);
use DB_File;
use Readonly;
use Parallel::ForkManager 0.7.6;
use AII::Playbook;

use NCM::Component::metaconfig 18.6.0;

Expand Down Expand Up @@ -535,12 +536,19 @@ sub run_plugin

# This is here because CacheManager and Fetch objects may have
# problems when they get out of scope.
my @modules = $only_modulename ? ($only_modulename) : map {$tree->{$_}->{plugin_modulename} || $_} sort keys %$tree;
my %pmodules;
if ($only_modulename) {
$pmodules{$only_modulename} = $only_modulename;
} else {
%pmodules = map {$_ => $tree->{$_}->{plugin_modulename} || $tree->{$_}->{'ncm-module'} || $_} keys %$tree;
}

# Iterate over module names, handling each
foreach my $modulename (@modules) {
# TODO: when dealing with ansible, this order should be resolved via the dependencies
foreach my $pname (sort keys %pmodules) {
my $modulename = $pmodules{$pname};
if ($modulename !~ m/^[a-zA-Z_]\w+(::[a-zA-Z_]\w+)*$/) {
$self->error ("Invalid Perl identifier $modulename specified as a plug-in. Skipping.");
$self->error ("Invalid Perl identifier $modulename specified as a plug-in for $pname. Skipping.");
$self->{status} = PARTERR_ST;
next;
}
Expand All @@ -553,7 +561,7 @@ sub run_plugin
$self->debug (4, "Loading plugin module $modulename");
eval (USEMODULE . $modulename);
if ($@) {
$self->error ("Couldn't load plugin module $modulename for path $path: $@");
$self->error ("Couldn't load plugin module $modulename for $pname for path $path: $@");
$self->{status} = PARTERR_ST;
next;
}
Expand All @@ -579,19 +587,19 @@ sub run_plugin
$self->plugin_handler($modulename, @_);
});

if ($method eq 'ansible_command') {
my $ansible = $st->{configuration}->{ansible};
# make role for component name, and also pass it via configuration hack
my $role = $ansible->{playbook}->add_role($pname);
# placeholder for current/last active role
$ansible->{role} = $role;
}

# Set active config
if ($plug->can('set_active_config')) {
$plug->set_active_config($st->{configuration});
}

if ($method == 'ansible_command') {
# make role for component name, and also pass it via configuration hack
# AII code does not support aliased components
my $role = $configuration->{ansible}->{playbook}->add_role(component name);
# placeholder for current/last active role
$configuration->{ansible}->{role} = $role;
}

# The plugin method has to return success
my $res = eval { $plug->$method ($st->{configuration}) };
if ($@) {
Expand Down
15 changes: 15 additions & 0 deletions aii-core/src/test/perl/NCM/Component/ansible.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package NCM::Component::ansible;

sub new {
my $class = shift;

return bless {}, $class;
}

sub ansible_command {
my ($self, $configuration) = @_;
$configuration->{ansible}->{role}->add_task("mytask");
return 1; # must return success
}

1;
17 changes: 11 additions & 6 deletions aii-core/src/test/perl/shellfe.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use strict;
use warnings;
use Test::More;
use Test::Quattor qw(metaconfig modulename_exists modulename_not_exists);
use Test::Quattor qw(metaconfig modulename_exists modulename_not_exists ansible);
use AII::Shellfe;
use Cwd;
use CAF::FileReader;
Expand Down Expand Up @@ -50,17 +50,22 @@ is_deeply($cli->_download_options('ccm'), {}, "empty config returns hashref for

# Test metaconfig
my $cfg = get_config_for_profile('metaconfig');
$cli->_metaconfig("somenode", {configuration => $cfg});
$cli->_metaconfig("somenode", {configuration => $cfg, name => 'somename'});

my $fh = get_file(getcwd . "/target/test/cache/metaconfig/metaconfig/etc/something");
is("$fh", "a=1\n\n", "metaconfig option rendered file in cache dir");

# Test ansible
$cfg = get_config_for_profile('metaconfig');
$cli->_ansible("somenode", {configuration => $cfg});
$cfg = get_config_for_profile('ansible');
$cli->_ansible("ansinode", {configuration => $cfg, name => 'ansiname'});

$fh = get_file(getcwd . "/target/test/cache/ansible/ansible/main.yml");
is("$fh", "---\n- hosts: ansinode\n roles:\n - ansible\n - myalias\n", "ansible playbook rendered in cache dir");
$fh = get_file(getcwd . "/target/test/cache/ansible/ansible/roles/ansible.yml");
is("$fh", "---\n- tasks:\n - name: mytask\n", "ansible role1 rendered in cache dir");
$fh = get_file(getcwd . "/target/test/cache/ansible/ansible/roles/myalias.yml");
is("$fh", "---\n- tasks:\n - name: mytask\n", "ansible role2 rendered in cache dir");

$fh = get_file(getcwd . "/target/test/cache/metaconfig/ansible/metaconfig.yml");
is("$fh", "abc", "metaconfig playbook rendered in cache dir");

# test modulename
$cfg = get_config_for_profile('modulename_not_exists');
Expand Down
8 changes: 8 additions & 0 deletions aii-core/src/test/resources/ansible.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object template ansible;

prefix "/software/components/myalias";
"ncm-module" = "ansible";
"data" = 1;

prefix "/software/components/ansible";
"data" = 2;

0 comments on commit 539923d

Please sign in to comment.