| 1 |
import unittest |
| 2 |
import ConfigParser |
| 3 |
import json |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import time |
| 7 |
import re |
| 8 |
|
| 9 |
testroot = os.path.dirname(__file__) |
| 10 |
sys.path.insert(0, os.path.dirname(testroot)) |
| 11 |
|
| 12 |
import hpilo |
| 13 |
import warnings |
| 14 |
warnings.filterwarnings("ignore", category=hpilo.IloWarning) |
| 15 |
|
| 16 |
class FirmwareCache(object): |
| 17 |
def __init__(self): |
| 18 |
self.cachefile = os.path.join(testroot, '.firmware_version_cache') |
| 19 |
self.cache = {} |
| 20 |
if os.path.exists(self.cachefile): |
| 21 |
with open(self.cachefile) as fd: |
| 22 |
self.cache = json.load(fd) |
| 23 |
|
| 24 |
def __getitem__(self, ilo): |
| 25 |
if ilo.hostname not in self.cache: |
| 26 |
self.cache[ilo.hostname] = ilo.get_fw_version() |
| 27 |
with open(self.cachefile, 'w') as fd: |
| 28 |
json.dump(self.cache, fd) |
| 29 |
return self.cache[ilo.hostname] |
| 30 |
firmware_cache = FirmwareCache() |
| 31 |
|
| 32 |
class IloTestCaseMeta(type): |
| 33 |
def __new__(cls, name, bases, attrs): |
| 34 |
attrs['ilos'] = {} |
| 35 |
config = ConfigParser.ConfigParser() |
| 36 |
config.read(os.path.expanduser(os.path.join('~', '.ilo.conf'))) |
| 37 |
login = config.get('ilo', 'login') |
| 38 |
password = config.get('ilo', 'password') |
| 39 |
methods = [] |
| 40 |
for attr in list(attrs.keys()): |
| 41 |
if attr.startswith('test_') and callable(attrs[attr]): |
| 42 |
attrs['_' + attr] = attrs.pop(attr) |
| 43 |
methods.append(attr[5:]) |
| 44 |
|
| 45 |
for section in config.sections(): |
| 46 |
if not section.startswith('test '): |
| 47 |
continue |
| 48 |
key = section.split()[1] |
| 49 |
hostname = config.get(section, 'ilo') |
| 50 |
ilo = hpilo.Ilo(hostname, login, password) |
| 51 |
ilo.firmware_version = firmware_cache[ilo] |
| 52 |
if not ilo.protocol: |
| 53 |
ilo.protocol = hpilo.ILO_RAW if ilo.firmware_version['management_processor'].lower() in ('ilo', 'ilo2') else hpilo.ILO_HTTP |
| 54 |
|
| 55 |
ilo.save_response = os.path.join(testroot, 'hpilo_test_debug_output') |
| 56 |
attrs['ilos'][key] = ilo |
| 57 |
for method in methods: |
| 58 |
fname = re.sub('[^a-zA-Z0-9_]', '_', 'test_%s_%s' % (key, method)) |
| 59 |
attrs[fname] = eval("lambda self: self._test_%s(self.ilos['%s'])" % (method, key)) |
| 60 |
return super(IloTestCaseMeta, cls).__new__(cls, name, bases, attrs) |
| 61 |
|
| 62 |
class IloTestCase(unittest.TestCase): |
| 63 |
__metaclass__ = IloTestCaseMeta |
| 64 |
maxDiff = None |
| 65 |
|
| 66 |
def require_ilo(self, ilo, *ilos): |
| 67 |
for ilov in ilos: |
| 68 |
version = None |
| 69 |
if ':' in ilov: |
| 70 |
ilov, version = ilov.split(':') |
| 71 |
if ilo.firmware_version['management_processor'].lower() == ilov: |
| 72 |
if not version or ilo.firmware_version['firmware_version'] >= version: |
| 73 |
return True |
| 74 |
|
| 75 |
raise unittest.SkipTest("This test requires %s, not %s:%s" % ('|'.join(ilos), |
| 76 |
ilo.firmware_version['management_processor'].lower(), ilo.firmware_version['firmware_version'])) |
| 77 |
|
| 78 |
def reset_delay(self, ilo): |
| 79 |
time.sleep(30) |
| 80 |
while True: |
| 81 |
try: |
| 82 |
ilo.get_fw_version() |
| 83 |
return |
| 84 |
except hpilo.IloCommunicationError: |
| 85 |
time.sleep(10) |