Bläddra i källkod

Added root check to constructer, and updated unit test

Paul Trost 10 år sedan
förälder
incheckning
855049c6d1
4 ändrade filer med 16 tillägg och 8 borttagningar
  1. 3 0
      Changes
  2. BIN
      dist/Disk-SMART-0.15.tar.gz
  3. 3 4
      lib/Disk/SMART.pm
  4. 10 4
      t/01-function_tests.t

+ 3 - 0
Changes

@@ -1,5 +1,8 @@
 Revision history for Disk-SMART
 
+0.15	2016-11-05
+	Added check for root as the executing user, updated unit tests
+
 0.14	2015-06-14
 	Fixed to run unit tests without smartctl binary being installed
 

BIN
dist/Disk-SMART-0.15.tar.gz


+ 3 - 4
lib/Disk/SMART.pm

@@ -8,7 +8,7 @@ use Math::Round;
 use File::Which;
 
 {
-    $Disk::SMART::VERSION = '0.14'
+    $Disk::SMART::VERSION = '0.15'
 }
 
 our $smartctl = which('smartctl');
@@ -49,9 +49,9 @@ Returns C<Disk::SMART> object if smartctl is available and can poll the given de
 sub new {
     my ( $class, @devices ) = @_;
     my $self = bless {}, $class;
+    die "$class must be called as root, please run $0 as root or with sudo\n" if $>;
     @devices = @devices ? @devices : $self->get_disk_list();
-
-    croak "Valid device identifier not supplied to constructor for $class.\n"
+    confess "Valid device identifier not supplied to constructor, or no disks detected.\n"
         if !@devices;
 
     $self->update_data(@devices);
@@ -342,7 +342,6 @@ sub _process_disk_temp {
         $temp_c = substr $temp_c, 83, +3;
         $temp_c = _trim($temp_c);
         $temp_f = round( ( $temp_c * 9 ) / 5 + 32 );
-
         $temp_c = int $temp_c;
         $temp_f = int $temp_f;
     }

+ 10 - 4
t/01-function_tests.t

@@ -92,16 +92,22 @@ SMART Selective self-test log data structure revision number 1
     3        0        0  Not_testing
     4        0        0  Not_testing
     5        0        0  Not_testing
-elsius
+Selective self-test flags (0x0):
   After scanning selected spans, do NOT read-scan remainder of disk.
 If Selective self-test is pending on power-up, resume after 0 minute delay.";
 
 my $mock  = Test::MockModule->new('Disk::SMART');
 $mock->mock(
+    'new' => sub {
+        my ( $class, @devices ) = @_;
+        my $self = bless {}, $class;
+        $self->update_data(@devices);
+        return $self;
+    },
     '_get_smart_output' => sub { return $smart_output; },
     'run_short_test'    => 'Completed without error'
 );
-my $disk  = '/dev/sda';
+my $disk = '/dev/sda';    # this is being mocked
 my $smart = Disk::SMART->new($disk);
             
 # Verify functions return correctly with SMART data present
@@ -125,9 +131,9 @@ my @disk_temps = $smart->get_disk_temp($disk);
 is( $disk_temps[0], 'N/A', "get_disk_temp() returns 'N/A' when smartctl doesn't report temperaure" );
 
 #Exception testing
-$disk  = '/dev/test_bad';
+$disk = '/dev/test_bad';
 $mock->mock( 
-    'update_data'       => "Smartctl couldn't poll device",
+    'update_data' => "Smartctl couldn't poll device",
 );
 $mock->unmock('run_short_test');