Skip to content

Commit

Permalink
Bug fixes on pyhpeimc.objects classes. Adding regression tests for
Browse files Browse the repository at this point in the history
bugs to test_pyhpeimc_objects in tests folder
  • Loading branch information
netmanchris committed Aug 25, 2017
1 parent 106992a commit c34a056
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ What's Changed:
capabilties
1.0.39 - Fixed pyhpeimc.objects broken class objects.
1.0.40 - Fixed getvlan() addvlan() and delvlan() methods on pyhpeimc.objects IMCDev class
1.0.41 - Bugfixes for pyhpeimc.objects classes. Adding additional tests.
32 changes: 26 additions & 6 deletions pyhpeimc/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def getipmacarp(self):
Function operates on the IMCDev object and updates the ipmacarp attribute
:return:
"""
self.ipmacarp = get_ip_mac_arp_list(self.devid, self.auth, self.url)
self.ipmacarp = get_ip_mac_arp_list(self.auth, self.url, devid = self.devid)


class IMCInterface:
Expand Down Expand Up @@ -168,14 +168,34 @@ def __init__(self, ip_address, ifindex, auth, url):
self.accessinterfaces = get_device_access_interfaces(self.auth, self.url, devip = self.ip)
self.pvid = get_access_interface_vlan(self.ifIndex, self.accessinterfaces)

def down(self):
"""
Function operates on the IMCInterface object and configures the interface into an
administratively down state and refreshes contents of self.adminstatus
:return:
"""
set_interface_down(self.ifIndex, self.auth, self.url, devip=self.ip)
self.adminstatus = get_interface_details(self.ifIndex, self.auth, self.url, devip=self.ip)[
'adminStatusDesc']

def up(self):
"""
Function operates on the IMCInterface object and configures the interface into an
administratively up state and refreshes contents of self.adminstatus
:return:
"""
set_interface_up(self.ifIndex, self.auth, self.url, devip=self.ip)
self.adminstatus = get_interface_details(self.ifIndex, self.auth, self.url, devip=self.ip)[
'adminStatusDesc']


# TODO refactor deallocateIp method for human consumption
# TODO Add real_time_locate functionality to nextfreeip method to search IP address before offering

class IPScope:
"""
Class instantiates an object to gather and manipulate attributes and methods of a IP
scope as configured in the HPE IMC Platform Terminal Access module.
scope as configured in the HPE IMC Platform Terminal Access module. Note: IPScope must already exist.
"""

def __init__(self, netaddr, auth, url):
Expand All @@ -199,16 +219,16 @@ def allocate_ip(self, hostipaddress, name, description):
:param description: str of a description of the target host ip record
:return:
"""
add_scope_ip(hostipaddress, name, description, self.id, self.auth, self.url)
add_scope_ip(hostipaddress, name, description, self.auth, self.url, scopeid=self.id)

def deallocate_ip(self, hostid):
def deallocate_ip(self, hostipaddress):
"""
Object method takes in input of hostid,removes them from the parent ip scope.
Object method takes in input of hostip address,removes them from the parent ip scope.
:param hostid: str of the hostid of the target host ip record
:return:
"""
remove_scope_ip(hostid, self.auth, self.url)
delete_host_from_segment(hostipaddress, self.netaddr, self.auth, self.url)

def gethosts(self):
"""
Expand Down
52 changes: 52 additions & 0 deletions pyhpeimc/plat/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,58 @@ def set_inteface_up(ifindex, auth, url, devid=None, devip=None):
return "Error:\n" + str(error) + " set_inteface_up: An Error has occured"


def set_interface_up(ifindex, auth, url, devid=None, devip=None):
"""
function takest devid and ifindex of specific device and interface and issues a RESTFUL call
to "undo shut" the specified interface on the target device.
:param devid: int or str value of the target device
:param devip: ipv4 address of the target devices
:param ifindex: int or str value of the target interface
:param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class
:param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass
:return: HTTP status code 204 with no values.
:rype: int
>>> from pyhpeimc.auth import *
>>> from pyhpeimc.plat.device import *
>>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")
>>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devid = '10')
204
>>> int_up_response = set_interface_up( '9', auth.creds, auth.url, devid = '10')
>>> int_down_response = set_interface_down( '9', auth.creds, auth.url, devid = '10')
204
>>> int_up_response = set_interface_up('9', auth.creds, auth.url, devip = '10.101.0.221')
>>> assert type(int_up_response) is int
>>> assert int_up_response is 204
"""
if devip is not None:
devid = get_dev_details(devip, auth, url)['id']
set_int_up_url = "/imcrs/plat/res/device/" + str(devid) + "/interface/" + str(ifindex) + "/up"
f_url = url + set_int_up_url
try:
response = requests.put(f_url, auth=auth, headers=HEADERS)
if response.status_code == 204:
return response.status_code
except requests.exceptions.RequestException as error:
return "Error:\n" + str(error) + " set_interface_up: An Error has occured"


def _make_cmd_list(cmd_list):
"""
Helper function to easily create the proper json formated string from a list of strs
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across test_machine.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.0.40',
version='1.0.41',

description='Python binding for HPE IMC REST API Interface',
long_description=long_description,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

# Switches
CW3_Switch = '10.101.0.227'
CW3_Interface = '9'
CW5_Switch = '10.101.0.221'
CW5_Interface = '9'
CW7_Switch = None
Cisco_Switch = None
Juniper_Switch = None
Expand Down
174 changes: 174 additions & 0 deletions tests/test_pyhpeimc_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
"""
This module is used for testing the functions within the pyhpeimc.plat.alarms module.
"""

from unittest import TestCase
from pyhpeimc.objects import *
from test_machine import *



# TODO Remarked out failing tests


class TestCreateIMCDev(TestCase):
"""
Test Case for pyhpeimc objects IMCDev class
"""

def test_create_IMCDev_type(self):
"""
test case for creating IMCDev object
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
self.assertIs(type(dev1), IMCDev)

def test_create_IMCDev_type(self):
"""
test case for creating IMCDev object
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
self.assertIs(type(dev1), IMCDev)

def test_get_vlans(self):
"""
test case for getvlans method on IMCDev class object
:return:
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
dev1.getvlans()
self.assertIs(type(dev1.vlans), list)

def test_add_vlan(self):
"""
test case for add_vlans method on IMCDev object
:return:
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
dev1.delvlan('500')
dev1.addvlan('500','testvlan')
dev1.getvlans()
vlans = [vlan['vlanId'] for vlan in dev1.vlans]
self.assertIn('500',vlans)
dev1.delvlan('500')

def test_del_vlan(self):
"""
test case for del_vlan method on IMCDev object
:return:
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
dev1.addvlan('500', 'testvlan')
dev1.delvlan('500')
dev1.getvlans()
vlans = [vlan['vlanId'] for vlan in dev1.vlans]
self.assertNotIn('500', vlans)

def test_get_ipmacarp(self):
"""
test case for get_ipmacarp method on IMCDev object
:return:
"""
dev1 = IMCDev(CW5_Switch, auth.creds, auth.url)
dev1.getipmacarp()
self.assertIs(type(dev1.ipmacarp), list)


class TestIMCInterface(TestCase):
"""
Test Case for pyhpeimc objects IMCInterface class objects
"""

def test_create_IMCDev_type(self):
"""
test case for creating IMCInterface object
"""
Interface1 = IMCInterface(CW5_Switch,CW5_Interface, auth.creds, auth.url)
self.assertIs(type(Interface1), IMCInterface)

def test_down_interface(self):
"""
test case for down method on IMCInterface object
:return:
"""
Interface1 = IMCInterface(CW5_Switch, CW5_Interface, auth.creds, auth.url)
Interface1.down()
self.assertEquals(Interface1.adminstatus, 'Down' )

def test_up_interface(self):
"""
test case for up method on IMCInterface object
:return:
"""
Interface1 = IMCInterface(CW5_Switch, CW5_Interface, auth.creds, auth.url)
Interface1.up()
self.assertEquals(Interface1.adminstatus, 'Up')


class TestIPScope(TestCase):
"""
Test Case for pyhpeimc objects IPScope class
"""

def test_create_IPScope_type(self):
"""
test case for creating IMCInterface object
"""
new_scope = add_ip_scope('cyoung', 'test group', auth.creds, auth.url,
network_address=term_access_ipam_network_scope)
Scope1 = IPScope(term_access_ipam_network_scope, auth.creds, auth.url)
self.assertIs(type(Scope1), IPScope)
delete_ip_scope(term_access_ipam_network_scope, auth.creds, auth.url)

def test_get_hosts(self):
"""
test case for get_hosts method of IPScope class object
:return:
"""
new_scope = add_ip_scope('cyoung', 'test group', auth.creds, auth.url,
network_address=term_access_ipam_network_scope)
Scope1 = IPScope(term_access_ipam_network_scope, auth.creds, auth.url)
Scope1.gethosts()
self.assertIs(type(Scope1.hosts), list)
delete_ip_scope(term_access_ipam_network_scope, auth.creds, auth.url)


def test_allocate_host_ip(self):
"""
test case for allocate_ip method of IPScope class object
:return:
"""
new_scope = add_ip_scope('cyoung', 'test group', auth.creds, auth.url,
network_address=term_access_ipam_network_scope)
Scope1 = IPScope(term_access_ipam_network_scope, auth.creds, auth.url)
Scope1.allocate_ip(term_access_ipam_host,'test_host', 'my_test')
Scope1.gethosts()
hosts = [ host['ip'] for host in Scope1.hosts]
self.assertIn(term_access_ipam_host, hosts)
delete_ip_scope(term_access_ipam_network_scope, auth.creds, auth.url)

def test_deallocate_host_ip(self):
"""
test case for allocate_ip method of IPScope class object
:return:
"""
new_scope = add_ip_scope('cyoung', 'test group', auth.creds, auth.url,
network_address=term_access_ipam_network_scope)
Scope1 = IPScope(term_access_ipam_network_scope, auth.creds, auth.url)
Scope1.allocate_ip(term_access_ipam_host, 'test_host', 'my_test')
Scope1.gethosts()
hosts = [host['ip'] for host in Scope1.hosts]
self.assertIn(term_access_ipam_host, hosts)
Scope1.deallocate_ip(term_access_ipam_host)
Scope1.gethosts()
hosts = [host['ip'] for host in Scope1.hosts]
self.assertNotIn(term_access_ipam_host, hosts)
delete_ip_scope(term_access_ipam_network_scope, auth.creds, auth.url)


6 changes: 3 additions & 3 deletions tests/test_pyhpeimc_plat_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3094,7 +3094,7 @@ def test_set_inteface_up(self):
raise SkipTest
if CW3_Switch is None:
raise SkipTest
set_interface_down('9', auth.creds, auth.url, devip=CW3_Switch)
set_interface_down(CW3_Interface, auth.creds, auth.url, devip=CW3_Switch)
int_up_response = set_inteface_up('1', auth.creds, auth.url, devip=CW3_Switch)
self.assertIs(type(int_up_response), int)
self.assertIs(int_up_response, 204)
Expand All @@ -3107,8 +3107,8 @@ def test_set_inteface_up(self):
raise SkipTest
if CW5_Switch is None:
raise SkipTest
set_interface_down('9', auth.creds, auth.url, devip=CW5_Switch)
int_up_response = set_inteface_up('9', auth.creds, auth.url, devip=CW5_Switch)
set_interface_down(CW5_Interface, auth.creds, auth.url, devip=CW5_Switch)
int_up_response = set_inteface_up(CW5_Interface, auth.creds, auth.url, devip=CW5_Switch)
self.assertIs(type(int_up_response), int)
self.assertIs(int_up_response, 204)

Expand Down

0 comments on commit c34a056

Please sign in to comment.