-1

The below test is to check the software information on a list of IP addresses. The program prints the version of software running on all the IPs as expected.

Now, I would like to test whether the software running on all the IPs are identical? How do I do that?

sub test_check_software_info_on_all_the_ips {

    my ($self) = @_;

    $self->{'machine_ip'} = $self->{'queryObj'}->get_machine_ip();

    foreach my $ip ( @{ $self->{'machine_ip'} } ) {

        $self->{'install_info'} = $self->{'queryObj'}->get_install_info($ip);

        INFO( 'Software info of ' . $ip . ' is ' . $self->{'install_info'} );
    }
}

Sample output

20160907T141846   INFO    Software info of 1.1.1.1 is r-2016-08-27-03
20160907T141846   INFO    Software info of 2.2.2.2 is r-2016-08-27-03
20160907T141847   INFO    Software info of 3.3.3.3 is r-2016-08-27-03
20160907T141847   INFO    Software info of 4.4.4.4 is r-2016-08-27-03
Borodin
  • 126,100
  • 9
  • 70
  • 144
nims
  • 103
  • 1
  • 8
  • 3
    So, what have you considered doing? It looks pretty straight-forward. Grab the version from the first; check whether all the others are the same, printing any discrepant versions. Of course, there's a risk that the first machine is the one with the odd version. You could also create a hash with the count of each version (keyed by the version) and when you've finished the loop above, iterate over the keys of that new hash to report versions and counts. You could also create a hash keyed on version with the list of machines using that version. Etc. All good clean basic Perl programming fun. – Jonathan Leffler Sep 07 '16 at 14:48
  • It looks like you are writing object-oriented software, in which case you should be using accessor methods instead of treating `$self` as an ordinary hash reference. – Borodin Sep 07 '16 at 15:02

2 Answers2

0

This will do as you ask

sub check_matching_info {

    my ($self) = @_;

    my $ips = $self->{queryObj}->get_machine_ip;

    my %info;

    for my $ip ( @$ips ) {
        my $info = $self->{queryObj}->get_install_info($ip);
        push @{ $info{$info} }, $ip;
    }

    print keys %info == 1 ? "All IPs have the same install info" : "IPs have different install info";
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
0

As always, everything has already been written for you, you just have to find it. While there are some gems in core available through List::Util, what we want today is not in core, but is in List::MoreUtils.

use List::MoreUtils ('all') ;

sub check_versions_equal
{
  my ($self)= @_ ;
  my @vers= map ( $self->{queryObj}->get_install_info($_) }
            @{$self->{queryObjs}->get_machine_ip ;}
  return true unless @vers ;  # empty list case ;
  my ($v)= @vers ;
  return all { $_ eq $v } @vers ;
}
woolstar
  • 5,063
  • 20
  • 31