Merge pull request #701 from rhansen/group_hosts_by

`group_hosts_by` improvements
This commit is contained in:
Richard Hansen 2024-07-13 04:45:16 -04:00 committed by GitHub
commit 60f931e7da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 430 additions and 514 deletions

View file

@ -80,10 +80,10 @@ jobs:
dnf --refresh install -y 'dnf-command(config-manager)' epel-release && dnf --refresh install -y 'dnf-command(config-manager)' epel-release &&
dnf config-manager --set-enabled crb dnf config-manager --set-enabled crb
- name: install dependencies - name: install dependencies
# The --skip-broken argument works around RedHat UBI's missing packages. # The --skip-broken argument works around missing packages. (They're
# (They're only used for testing, so it's OK to not install them.) # only used for testing, so it's OK to not install them.)
run: | run: |
dnf --refresh --skip-broken install -y \ dnf --refresh install --skip-broken -y \
automake \ automake \
findutils \ findutils \
iproute \ iproute \

File diff suppressed because it is too large Load diff

View file

@ -34,57 +34,77 @@ my @test_cases = (
{ {
desc => 'empty attribute set yields single group with all hosts', desc => 'empty attribute set yields single group with all hosts',
groupby => [qw()], groupby => [qw()],
want => [[$h1, $h2, $h3]], want => [{cfg => {}, hosts => [$h1, $h2, $h3]}],
}, },
{ {
desc => 'common attribute yields single group with all hosts', desc => 'common attribute yields single group with all hosts',
groupby => [qw(common)], groupby => [qw(common)],
want => [[$h1, $h2, $h3]], want => [{cfg => {common => 'common'}, hosts => [$h1, $h2, $h3]}],
}, },
{ {
desc => 'subset share a value', desc => 'subset share a value',
groupby => [qw(h1h2)], groupby => [qw(h1h2)],
want => [[$h1, $h2], [$h3]], want => [
{cfg => {h1h2 => 'h1 and h2'}, hosts => [$h1, $h2]},
{cfg => {h1h2 => 'unique'}, hosts => [$h3]},
],
}, },
{ {
desc => 'all unique', desc => 'all unique',
groupby => [qw(unique)], groupby => [qw(unique)],
want => [[$h1], [$h2], [$h3]], want => [
{cfg => {unique => 'h1'}, hosts => [$h1]},
{cfg => {unique => 'h2'}, hosts => [$h2]},
{cfg => {unique => 'h3'}, hosts => [$h3]},
],
}, },
{ {
desc => 'combination', desc => 'combination',
groupby => [qw(common h1h2)], groupby => [qw(common h1h2)],
want => [[$h1, $h2], [$h3]], want => [
{cfg => {common => 'common', h1h2 => 'h1 and h2'}, hosts => [$h1, $h2]},
{cfg => {common => 'common', h1h2 => 'unique'}, hosts => [$h3]},
],
}, },
{ {
desc => 'falsy values', desc => 'falsy values',
groupby => [qw(falsy)], groupby => [qw(falsy)],
want => [[$h1], [$h2], [$h3]], want => [
{cfg => {falsy => 0}, hosts => [$h1]},
{cfg => {falsy => ''}, hosts => [$h2]},
{cfg => {falsy => undef}, hosts => [$h3]},
],
}, },
{ {
desc => 'set, unset, undef', desc => 'set, unset, undef',
groupby => [qw(maybeunset)], groupby => [qw(maybeunset)],
want => [[$h1], [$h2], [$h3]], want => [
{cfg => {maybeunset => 'unique'}, hosts => [$h1]},
{cfg => {maybeunset => undef}, hosts => [$h2]},
{cfg => {}, hosts => [$h3]},
],
}, },
{ {
desc => 'missing attribute', desc => 'missing attribute',
groupby => [qw(thisdoesnotexist)], groupby => [qw(thisdoesnotexist)],
want => [[$h1, $h2, $h3]], want => [{cfg => {}, hosts => [$h1, $h2, $h3]}],
}, },
); );
for my $tc (@test_cases) { for my $tc (@test_cases) {
my %got = ddclient::group_hosts_by([$h1, $h2, $h3], $tc->{groupby}); my @got = ddclient::group_hosts_by([$h1, $h2, $h3], @{$tc->{groupby}});
# %got is used as a set of sets. Sort everything to make comparison easier. # @got is used as a set of sets. Sort everything to make comparison easier.
my @got = sort({ $_->{hosts} = [sort(@{$_->{hosts}})] for @got;
for (my $i = 0; $i < @$a && $i < @$b; ++$i) { @got = sort({
my $x = $a->[$i] cmp $b->[$i]; for (my $i = 0; $i < @{$a->{hosts}} && $i < @{$b->{hosts}}; ++$i) {
my $x = $a->{hosts}[$i] cmp $b->{hosts}[$i];
return $x if $x != 0; return $x if $x != 0;
} }
return @$a <=> @$b; return @{$a->{hosts}} <=> @{$b->{hosts}};
} map({ [sort(@$_)]; } values(%got))); } @got);
is_deeply(\@got, $tc->{want}, $tc->{desc}) is_deeply(\@got, $tc->{want}, $tc->{desc})
or diag(Data::Dumper->Dump([\@got, $tc->{want}], [qw(got want)])); or diag(Data::Dumper->new([\@got, $tc->{want}],
[qw(got want)])->Sortkeys(1)->Useqq(1)->Dump());
} }
done_testing(); done_testing();