Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff_package_list in Rex::Pkg::Redhat, for multiple pks with same name #1620

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Revision history for Rex

[BUG FIXES]
- Return only the first found command
- Fix on_change in update_system command on Redhat

[DOCUMENTATION]

Expand Down
53 changes: 53 additions & 0 deletions lib/Rex/Pkg/Redhat.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,59 @@ sub get_installed {
return @pkg;
}

sub diff_package_list {
my ( $self, $list1, $list2 ) = @_;

my @old_installed = @{$list1};
my @new_installed = @{$list2};

my @modifications;

my %old_installed;
foreach my $old_pkg (@old_installed) {
my $name = $old_pkg->{name};
my $version = "$old_pkg->{version} $old_pkg->{release}";
$old_installed{$name}{$version} = $old_pkg;
}

my %new_installed;
foreach my $new_pkg (@new_installed) {
my $name = $new_pkg->{name};
my $version = "$new_pkg->{version} $new_pkg->{release}";
if ( $old_installed{$name} and $old_installed{$name}{$version} ) {
delete $old_installed{$name}{$version};
if ( !keys %{ $old_installed{$name} } ) {
delete $old_installed{$name};
}
next;
}
$new_installed{$name}{$version} = $new_pkg;
}

foreach my $new_name ( keys %new_installed ) {
if ( $old_installed{$new_name} ) {
foreach my $pkg ( values %{ $new_installed{$new_name} } ) {
push @modifications, { %{$pkg}, action => 'updated' };
}
}
else {
foreach my $pkg ( values %{ $new_installed{$new_name} } ) {
push @modifications, { %{$pkg}, action => 'installed' };
}
}
}

foreach my $old_name ( keys %old_installed ) {
if ( !$new_installed{$old_name} ) {
foreach my $pkg ( values %{ $old_installed{$old_name} } ) {
push @modifications, { %{$pkg}, action => 'removed' };
}
}
}

return @modifications;
}

sub add_repository {
my ( $self, %data ) = @_;

Expand Down
93 changes: 92 additions & 1 deletion t/package.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use warnings;

our $VERSION = '9999.99.99_99'; # VERSION

use Test::More tests => 3;
use Test::More tests => 4;
use Test::Warnings;
use Test::Deep;
use Test::Exception;

use Rex::Pkg::Test;
use Rex::Pkg::Redhat;

my $pkg = Rex::Pkg::Test->new;

Expand Down Expand Up @@ -62,4 +63,94 @@ subtest 'local package installation' => sub {
lives_ok { $pkg->update('test_package') }, 'update test package';
};

subtest 'redhat package list diffs' => sub {
plan tests => 1;

my $rh_pkg = Rex::Pkg::Redhat->new;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: the other instantiation of a Rex::Pkg class is outside the test cases, and this introduces a different convention.

Long run I'm fine with both, depending where the evolution of t/package.t takes us over time. Normally I would lean towards consistency, while it's also fine for now to not know exactly which approach will serve us best later (so we allow ourselves to decide later.)

It may not be necessary to discover the answer or fix it now, I just thought it's worth mentioning while reading the changes carefully.


## no critic (ProhibitDuplicateLiteral)

my @before = (
{
arch => 'x86_64',
epoch => '0',
name => 'lzo',
release => '8.el7',
version => '2.06',
},
{
arch => 'x86_64',
epoch => '0',
name => 'postgresql-server',
release => '1.el7',
version => '9.2.18',
},
{
arch => 'x86_64',
epoch => '0',
name => 'kernel',
release => '427.26.1.el9_4',
version => '5.14.0',
},
);

my @after = (
{
arch => 'x86_64',
epoch => '0',
name => 'postgresql-server',
release => '1.el7',
version => '9.2.19',
},
{
arch => 'x86_64',
epoch => '0',
name => 'kernel',
release => '427.28.1.el9_4',
version => '5.14.0',
},
{
arch => 'x86_64',
epoch => '0',
name => 'kernel',
release => '427.26.1.el9_4',
version => '5.14.0',
},
);

my @expected = (
{
action => 'updated',
arch => 'x86_64',
epoch => '0',
name => 'postgresql-server',
release => '1.el7',
version => '9.2.19',
},
{
action => 'installed',
arch => 'x86_64',
epoch => '0',
name => 'kernel',
release => '427.28.1.el9_4',
version => '5.14.0',
},
{
action => 'removed',
arch => 'x86_64',
epoch => '0',
name => 'lzo',
release => '8.el7',
version => '2.06',
},
);

## use critic

my @mods = $rh_pkg->diff_package_list( \@before, \@after );

cmp_bag( \@mods, \@expected,
'expected package modifications on Red Hat compatible distros' );
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more note overall: we seem to aim fixing on_change for pkg calls, and I found no tests for that. I agree it may be tricky to add, though I'd like to think of a good way to include that too (=a test to prove we can detect on_change bugs in the future, and then add code to prove we fixed the known cases for now.)


1;
Loading