Преглед на файлове

Implemented mock data functionality in unit testing. Cleaned up methods

Paul Trost преди 11 години
родител
ревизия
4d113b5527
променени са 3 файла, в които са добавени 24 реда и са изтрити 32 реда
  1. 1 2
      MANIFEST
  2. 14 20
      lib/Disk/SMART.pm
  3. 9 10
      t/01-get_functions.t

+ 1 - 2
MANIFEST

@@ -7,8 +7,7 @@ README
 README.md
 LICENSE
 t/00-load.t
-t/01-instantiation.t
-t/02-get_functions.t
+t/01-get_functions.t
 t/manifest.t
 t/pod-coverage.t
 t/pod.t

+ 14 - 20
lib/Disk/SMART.pm

@@ -44,18 +44,12 @@ sub new {
     my $self = bless {}, $class;
     my $test_data;
 
-    if ( $ENV{'TEST_MOCK_DATA'} ) {
-        $test_data = pop @devices;
-    }
-    
     croak "Valid device identifier not supplied to constructor for $class.\n"
-        if !@devices && !$ENV{'TEST_MOCK_DATA'};
+        if !@devices && !defined $ENV{'TEST_MOCK_DATA'};
     croak "smartctl binary was not found on your system, are you running as root?\n"
-        if !-f $smartctl && !$ENV{'TEST_MOCK_DATA'};
+        if !-f $smartctl && !defined $ENV{'TEST_MOCK_DATA'};
 
-    foreach my $device (@devices) {
-      ($test_data) ? $self->update_data($device, $test_data) : $self->update_data($device);
-    }
+    $self->update_data($_) foreach @devices;
 
     return $self;
 }
@@ -76,6 +70,7 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
 sub get_disk_attributes {
     my ( $self, $device ) = @_;
     $self->_validate_param($device);
+
     return $self->{'devices'}->{$device}->{'attributes'};
 }
 
@@ -93,6 +88,7 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
 sub get_disk_errors {
     my ( $self, $device ) = @_;
     $self->_validate_param($device);
+
     return $self->{'devices'}->{$device}->{'errors'};
 }
 
@@ -110,6 +106,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
 sub get_disk_health {
     my ( $self, $device ) = @_;
     $self->_validate_param($device);
+
     return $self->{'devices'}->{$device}->{'health'};
 }
 
@@ -127,6 +124,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
 sub get_disk_model {
     my ( $self, $device ) = @_;
     $self->_validate_param($device);
+
     return $self->{'devices'}->{$device}->{'model'};
 }
 
@@ -144,6 +142,7 @@ C<DEVICE> - Device identifier of SSD / Hard Drive
 sub get_disk_temp {
     my ( $self, $device ) = @_;
     $self->_validate_param($device);
+
     return @{ $self->{'devices'}->{$device}->{'temp'} };
 }
 
@@ -159,13 +158,11 @@ C<DEVICE> - Device identifier of SSD/ Hard Drive
 =cut
 
 sub update_data {
-    my ( $self, $device, $test_data ) = @_;
-    my $out = $test_data // undef;
-
-    $out = qx($smartctl -a $device) if !defined $test_data;
+    my ( $self, $device ) = @_;
+    my $out = ( defined $ENV{'MOCK_TEST_DATA'} ) ? $ENV{'MOCK_TEST_DATA'} : qx($smartctl -a $device);
     my $retval = $?;
 
-    if ( !$test_data ) {
+    if ( !$ENV{'MOCK_TEST_DATA'} ) {
         croak "Smartctl couldn't poll device $device\n"
             if ( $out !~ /START OF INFORMATION SECTION/ );
     }
@@ -173,11 +170,6 @@ sub update_data {
     chomp($out);
     $self->{'devices'}->{$device}->{'SMART_OUTPUT'} = $out;
     
-    # update_data() can be called at any time with a device name. Let's check
-    # the device name given to make sure it matches what was given during
-    # object construction.
-    $self->_validate_param($device);
-
     $self->_process_disk_attributes($device);
     $self->_process_disk_errors($device);
     $self->_process_disk_health($device);
@@ -189,6 +181,7 @@ sub update_data {
 
 sub _process_disk_attributes {
     my ( $self, $device ) = @_;
+    $self->_validate_param($device);
 
     my $smart_output = $self->{'devices'}->{$device}->{'SMART_OUTPUT'};
     my ($smart_attributes) = $smart_output =~ /(ID# ATTRIBUTE_NAME.*)\nSMART Error/s;
@@ -267,7 +260,8 @@ sub _process_disk_temp {
 
 sub _validate_param {
     my ( $self, $device ) = @_;
-    croak "$device not found in object, you probably didn't enter it right" if ( !exists $self->{'devices'}->{$device} );
+    croak "$device not found in object. Verify you specified the right device identifier.\n" if ( !exists $self->{'devices'}->{$device} );
+
     return;
 }
     

+ 9 - 10
t/01-get_functions.t

@@ -5,7 +5,8 @@ use Test::Fatal;
 use Disk::SMART;
 use Data::Dumper;
 
-my $test_data = 
+
+$ENV{'MOCK_TEST_DATA'} =
 'smartctl 5.41 2011-06-09 r3365 [x86_64-linux-2.6.32-32-pve] (local build)
 Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net
 
@@ -93,11 +94,8 @@ 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.';
 
-
-$ENV{'TEST_MOCK_DATA'} = 1;
-
-my $disk  = '/dev/blah_good';
-my $smart = Disk::SMART->new( $disk, $test_data );
+my $disk  = '/dev/test_good';
+my $smart = Disk::SMART->new($disk);
 
 #Positive testing
 is( $smart->get_disk_temp($disk), 2, 'get_disk_temp() returns drive temperature' );
@@ -106,17 +104,18 @@ is( $smart->get_disk_model($disk), 'ST3250410AS', 'get_disk_model() returns disk
 is( $smart->get_disk_errors($disk), 'No Errors Logged', 'get_disk_errors() returns proper string' );
 is( scalar( keys $smart->get_disk_attributes($disk) ), 18, 'get_disk_attributes() returns hash of device attributes' );
 
-my $new_test_data = $test_data;
-$new_test_data =~ s/ST3250410AS//;
-is( $smart->update_data( $disk, $new_test_data ), 1, 'update_data() updated object with changed device data' );
+$ENV{'MOCK_TEST_DATA'} =~ s/ST3250410AS//;
+is( $smart->update_data($disk), 1, 'update_data() updated object with changed device data' );
 is( $smart->get_disk_model($disk), 'N/A', 'get_disk_model() returns N/A with changed device data' );
 
 #Negative testing
-$disk  = '/dev/blah_bad';
+$disk  = '/dev/test_bad';
 like( exception { $smart->get_disk_temp($disk); },       qr/$disk not found in object/, 'get_disk_temp() returns failure when passed invalid device' );
 like( exception { $smart->get_disk_health($disk); },     qr/$disk not found in object/, 'get_disk_health() returns failure when passed invalid device' );
 like( exception { $smart->get_disk_errors($disk); },     qr/$disk not found in object/, 'get_disk_model() returns failure when passed invalid device' );
 like( exception { $smart->get_disk_errors($disk); },     qr/$disk not found in object/, 'get_disk_errors() returns failure when passed invalid device' );
 like( exception { $smart->get_disk_attributes($disk); }, qr/$disk not found in object/, 'get_disk_attributes() returns failure when passed invalid device' );
+
+$ENV{'MOCK_TEST_DATA'} = undef;
 like( exception { $smart->update_data($disk); },         qr/couldn't poll/,             'update_data() returns falure when passed invalid device' );