Skip to content

Commit

Permalink
Merge pull request #171 from devtom30/2.3.x-macOS104boottimeRefacto_PR2
Browse files Browse the repository at this point in the history
Mac OS X boottime refactoring
  • Loading branch information
g-bougard authored Oct 5, 2016
2 parents 9a499fc + a591b7d commit a2f11cb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
10 changes: 1 addition & 9 deletions lib/FusionInventory/Agent/Task/Inventory/MacOS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@ sub doInventory {
$name = "Mac OS X";
}

my $boottime = getFirstMatch(
command => "sysctl -n kern.boottime",
pattern => qr/sec = (\d+)/
);
if (!$boottime) {
$boottime = getFirstLine(
command => "sysctl -n kern.boottime"
);
}
my $boottime = getBootTime();

$inventory->setHardware({
OSNAME => $name,
Expand Down
12 changes: 2 additions & 10 deletions lib/FusionInventory/Agent/Task/Inventory/MacOS/Uptime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;

use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::MacOS;

sub isEnabled {
return getFirstLine(command => 'sysctl -n kern.boottime');
Expand All @@ -21,17 +22,8 @@ sub doInventory {
});
}

sub _getBootTime {
my $boottime = getFirstMatch(
pattern => qr/(\d+)$/,
@_,
);

return $boottime;
}

sub _getUptime {
my $boottime = _getBootTime(@_);
my $boottime = return FusionInventory::Agent::Tools::MacOS::getBootTime(@_);
return unless $boottime;

my $uptime = time() - $boottime;
Expand Down
15 changes: 15 additions & 0 deletions lib/FusionInventory/Agent/Tools/MacOS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use FusionInventory::Agent::Tools;
our @EXPORT = qw(
getSystemProfilerInfos
getIODevices
getBootTime
);

memoize('getSystemProfilerInfos');
Expand Down Expand Up @@ -402,6 +403,20 @@ sub getIODevices {
return @devices;
}

sub getBootTime {
my (%params) = @_;
if (!$params{string} && !$params{command}) {
$params{command} = 'sysctl -n kern.boottime';
}

my $boottime = getFirstMatch(
pattern => qr/(\d+)$/,
%params
);

return $boottime;
}

1;
__END__
Expand Down
10 changes: 9 additions & 1 deletion t/agent/tools/macos.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use warnings;

use Test::Deep;
use Test::More;
use English;

use FusionInventory::Agent::Tools::MacOS;
use FusionInventory::Agent::Task::Inventory::MacOS::Softwares;
Expand Down Expand Up @@ -3384,7 +3385,7 @@ my $versionNumbersComparisons = [
plan tests =>
scalar (keys %system_profiler_tests) +
scalar @ioreg_tests
+ 15
+ 16
+ scalar (@$versionNumbersComparisons);

foreach my $test (keys %system_profiler_tests) {
Expand Down Expand Up @@ -3570,3 +3571,10 @@ my $subArray = FusionInventory::Agent::Tools::MacOS::_findElementAndReturnParent
ok ($subArray);
my $size = scalar(@$subArray);
ok ($size == 291, 'must be 291 and is ' . $size);
SKIP : {
skip 'MacOS specific test', 1 unless $OSNAME eq 'darwin';
my $boottime = getBootTime();
ok ($boottime);
}
3 changes: 2 additions & 1 deletion t/tasks/inventory/macos/uptime.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use Test::More;
use Test::NoWarnings;

use FusionInventory::Agent::Task::Inventory::MacOS::Uptime;
use FusionInventory::Agent::Tools::MacOS;

my %tests = (
'1325070226' => '1325070226',
Expand All @@ -16,6 +17,6 @@ my %tests = (
plan tests => (scalar keys %tests) + 1;

foreach my $test (keys %tests) {
my $r = FusionInventory::Agent::Task::Inventory::MacOS::Uptime::_getBootTime(string => $test);
my $r = FusionInventory::Agent::Tools::MacOS::getBootTime(string => $test);
ok($r eq $tests{$test});
}

0 comments on commit a2f11cb

Please sign in to comment.