parse_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2010, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the Willow Garage nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 ##\author Kevin Watts
00036 
00037 from __future__ import with_statement
00038 
00039 PKG = 'pr2_computer_monitor'
00040 
00041 import roslib; roslib.load_manifest(PKG)
00042 import unittest
00043 
00044 import pr2_computer_monitor
00045 
00046 import os, sys
00047 
00048 TEXT_PATH = 'test/sample_output/nvidia_smi_out.txt'
00049 TEXT_HIGH_TEMP_PATH = 'test/sample_output/nvidia_smi_high_temp.txt'
00050 
00051 
00052 ##\brief Parses launch, tests.xml and configs.xml files in qualification
00053 class TestNominalParser(unittest.TestCase):
00054     def setUp(self):
00055         with open(os.path.join(roslib.packages.get_pkg_dir('pr2_computer_monitor'), TEXT_PATH), 'r') as f:
00056             self.data = f.read()
00057 
00058         with open(os.path.join(roslib.packages.get_pkg_dir('pr2_computer_monitor'), TEXT_HIGH_TEMP_PATH), 'r') as f:
00059             self.high_temp_data = f.read()
00060 
00061     def test_parse(self):
00062         gpu_stat = pr2_computer_monitor.parse_smi_output(self.data)
00063         
00064         # Check valid
00065         self.assert_(self.data, "Unable to read sample output, no test to run")
00066 
00067         # Check all string fields of message
00068         self.assert_(gpu_stat.pci_device_id, "No PCI Device ID found")
00069         self.assert_(gpu_stat.pci_location, "No PCI Location found")
00070         self.assert_(gpu_stat.display, "No Display found")
00071         self.assert_(gpu_stat.driver_version, "No Driver Version found")
00072         self.assert_(gpu_stat.product_name, "No Product Name found")
00073 
00074         self.assert_(gpu_stat.temperature > 40 and gpu_stat.temperature < 90, "Invalid temperature readings. Temperature: %d" % gpu_stat.temperature)
00075         self.assert_(gpu_stat.fan_speed > 0 and gpu_stat.fan_speed < 471, "Invalid fan speed readings. Fan Speed %f" % gpu_stat.fan_speed)
00076 
00077         diag_stat = pr2_computer_monitor.gpu_status_to_diag(gpu_stat)
00078         
00079         self.assert_(diag_stat.level == 0, "Diagnostics reports an error for nominal input. Message: %s" % diag_stat.message)
00080 
00081     def test_high_temp_parse(self):
00082         gpu_stat = pr2_computer_monitor.parse_smi_output(self.high_temp_data)
00083         
00084         # Check valid
00085         self.assert_(self.high_temp_data, "Unable to read sample output, no test to run")
00086 
00087         # Check all string fields of message
00088         self.assert_(gpu_stat.pci_device_id, "No PCI Device ID found")
00089         self.assert_(gpu_stat.pci_location, "No PCI Location found")
00090         self.assert_(gpu_stat.display, "No Display found")
00091         self.assert_(gpu_stat.driver_version, "No Driver Version found")
00092         self.assert_(gpu_stat.product_name, "No Product Name found")
00093 
00094         self.assert_(gpu_stat.temperature > 90, "Invalid temperature readings. Temperature: %d" % gpu_stat.temperature)
00095         self.assert_(gpu_stat.fan_speed > 0 and gpu_stat.fan_speed < 471, "Invalid fan speed readings. Fan Speed %s" % gpu_stat.fan_speed)
00096 
00097         diag_stat = pr2_computer_monitor.gpu_status_to_diag(gpu_stat)
00098         
00099         self.assert_(diag_stat.level == 1, "Diagnostics didn't report warning for high temp input. Level %d, Message: %s" % (diag_stat.level, diag_stat.message))
00100 
00101 
00102     def test_empty_parse(self):
00103         gpu_stat = pr2_computer_monitor.parse_smi_output('')
00104         
00105         self.assert_(gpu_stat.temperature == 0, "Invalid temperature reading. Should be 0. Reading: %d" % gpu_stat.temperature)
00106         
00107         diag_stat = pr2_computer_monitor.gpu_status_to_diag(gpu_stat)
00108         
00109         self.assert_(diag_stat.level == 2, "Diagnostics didn't reports an error for empty input. Level: %d, Message: %s" % (diag_stat.level, diag_stat.message))
00110 
00111         
00112 
00113 if __name__ == '__main__':
00114     if len(sys.argv) > 1 and sys.argv[1] == '-v':
00115         # Use to run tests verbosly
00116         suite = unittest.TestSuite()
00117         suite.addTest(TestNominalParser('test_parse'))
00118         suite.addTest(TestNominalParser('test_empty_parse'))
00119         suite.addTest(TestNominalParser('test_high_temp_parse'))
00120         
00121         unittest.TextTestRunner(verbosity = 2).run(suite)
00122     else:
00123         import rostest
00124         rostest.unitrun(PKG, 'parse_nominal', TestNominalParser)
00125 
00126 


pr2_computer_monitor
Author(s): Kevin Watts (watts@willowgarage.com)
autogenerated on Thu Sep 3 2015 11:31:16