Skip to content

Commit b416cf4

Browse files
committed
Merge pull request #1649 from RexOps/groups
Fix hostgroup membership list
2 parents e6937a6 + a64cc00 commit b416cf4

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Revision history for Rex
1212
- Fix parsing FreeBSD memory details
1313
- Recognize laundry memory on FreeBSD
1414
- Fix command existence checks without which
15+
- Fix hostgroup membership list
1516

1617
[DOCUMENTATION]
1718

lib/Rex/Group.pm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use attributes;
1515
use Rex::Group::Entry::Server;
1616

1717
use vars qw(%groups);
18-
use List::Util 1.45 qw(uniq);
18+
use List::Util 1.45 qw(any uniq);
1919
use Data::Dumper;
2020

2121
sub new {
@@ -24,8 +24,16 @@ sub new {
2424
my $self = {@_};
2525

2626
bless( $self, $proto );
27+
2728
for my $srv ( @{ $self->{servers} } ) {
2829
$srv->append_to_group( $self->{name} );
30+
31+
for my $group ( keys %groups ) {
32+
33+
if ( any { $_ eq $srv } $groups{$group}->get_servers ) {
34+
$srv->append_to_group($group);
35+
}
36+
}
2937
}
3038

3139
return $self;

t/group.t

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use warnings;
55

66
our $VERSION = '9999.99.99_99'; # VERSION
77

8-
use Test::More tests => 99;
8+
use Test::More tests => 102;
99
use Test::Warnings;
1010
use Test::Exception;
11+
use Test::Deep;
1112

1213
use Rex -feature => '0.31';
1314
use Rex::Group;
@@ -83,3 +84,32 @@ is( *{$function_ref}->(), $function_result, 'calling custom function' );
8384

8485
dies_ok( sub { Rex::Commands::evaluate_hostname('s[78]') },
8586
'die on invalid hostgroup expression' );
87+
88+
# group membership
89+
90+
group first => qw(one two);
91+
group second => qw(two three);
92+
93+
my $expected_groups_for = {
94+
one => [qw(first)],
95+
two => [qw(first second)],
96+
three => [qw(second)],
97+
};
98+
99+
my $groups_of;
100+
101+
for my $group (qw(first second)) {
102+
my @hosts = Rex::Group->get_group($group);
103+
104+
for my $host (@hosts) {
105+
$groups_of->{ $host->to_s } = [ $host->groups ];
106+
}
107+
}
108+
109+
for my $host ( keys %{$expected_groups_for} ) {
110+
cmp_bag(
111+
$groups_of->{$host},
112+
$expected_groups_for->{$host},
113+
"correct host groups for host $host"
114+
);
115+
}

0 commit comments

Comments
 (0)