@ -1,17 +1,15 @@
# (c) 2018, NetApp Inc.
# (c) 2018, NetApp Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from mock import MagicMock
from ansible . module_utils import netapp
from ansible . modules . storage . netapp import netapp_e_host
from ansible . modules . storage . netapp . netapp_e_host import Host
from ansible . modules . storage . netapp . netapp_e_host import Host
from units . modules . utils import AnsibleExitJson , AnsibleFailJson , ModuleTestCase , set_module_args
from units . modules . utils import AnsibleExitJson , AnsibleFailJson , ModuleTestCase , set_module_args
__metaclass__ = type
__metaclass__ = type
try :
from unittest import mock
except ImportError :
import mock
import mock
from units . compat . mock import patch
from ansible . module_utils . _text import to_bytes
class HostTest ( ModuleTestCase ) :
class HostTest ( ModuleTestCase ) :
@ -24,6 +22,7 @@ class HostTest(ModuleTestCase):
}
}
HOST = {
HOST = {
' name ' : ' 1 ' ,
' name ' : ' 1 ' ,
' hostRef ' : ' 123 ' ,
' label ' : ' 1 ' ,
' label ' : ' 1 ' ,
' id ' : ' 0 ' * 30 ,
' id ' : ' 0 ' * 30 ,
' clusterRef ' : 40 * ' 0 ' ,
' clusterRef ' : 40 * ' 0 ' ,
@ -41,6 +40,48 @@ class HostTest(ModuleTestCase):
' initiators ' : [ ] ,
' initiators ' : [ ] ,
' ports ' : [ ] ,
' ports ' : [ ] ,
}
}
EXISTING_HOSTS = [
{ " hostRef " : " 84000000600A098000A4B28D00303D065D430118 " , " clusterRef " : " 0000000000000000000000000000000000000000 " , " label " : " beegfs_storage1 " ,
" hostTypeIndex " : 28 , " ports " : [ ] , " initiators " : [ { " initiatorRef " : " 89000000600A098000A4B28D00303CF55D4300E3 " ,
" nodeName " : { " ioInterfaceType " : " iscsi " ,
" iscsiNodeName " : " iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 " ,
" remoteNodeWWN " : None , " nvmeNodeName " : None } ,
" alias " : { " ioInterfaceType " : " iscsi " , " iscsiAlias " : " " } , " label " : " beegfs_storage1_iscsi_0 " ,
" hostRef " : " 84000000600A098000A4B28D00303D065D430118 " ,
" id " : " 89000000600A098000A4B28D00303CF55D4300E3 " } ] ,
" hostSidePorts " : [ { " type " : " iscsi " , " address " : " iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 " , " label " : " beegfs_storage1_iscsi_0 " } ] ,
" id " : " 84000000600A098000A4B28D00303D065D430118 " , " name " : " beegfs_storage1 " } ,
{ " hostRef " : " 84000000600A098000A4B9D10030370B5D430109 " , " clusterRef " : " 0000000000000000000000000000000000000000 " , " label " : " beegfs_metadata1 " ,
" hostTypeIndex " : 28 , " ports " : [ ] , " initiators " : [ { " initiatorRef " : " 89000000600A098000A4B28D00303CFC5D4300F7 " ,
" nodeName " : { " ioInterfaceType " : " iscsi " ,
" iscsiNodeName " : " iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 " ,
" remoteNodeWWN " : None , " nvmeNodeName " : None } ,
" alias " : { " ioInterfaceType " : " iscsi " , " iscsiAlias " : " " } , " label " : " beegfs_metadata1_iscsi_0 " ,
" hostRef " : " 84000000600A098000A4B9D10030370B5D430109 " ,
" id " : " 89000000600A098000A4B28D00303CFC5D4300F7 " } ] ,
" hostSidePorts " : [ { " type " : " iscsi " , " address " : " iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 " , " label " : " beegfs_metadata1_iscsi_0 " } ] ,
" id " : " 84000000600A098000A4B9D10030370B5D430109 " , " name " : " beegfs_metadata1 " } ,
{ " hostRef " : " 84000000600A098000A4B9D10030370B5D430109 " , " clusterRef " : " 85000000600A098000A4B9D1003637135D483DEB " , " label " : " beegfs_metadata2 " ,
" hostTypeIndex " : 28 , " ports " : [ ] , " initiators " : [ { " initiatorRef " : " 89000000600A098000A4B28D00303CFC5D4300F7 " ,
" nodeName " : { " ioInterfaceType " : " iscsi " ,
" iscsiNodeName " : " iqn.used_elsewhere " ,
" remoteNodeWWN " : None , " nvmeNodeName " : None } ,
" alias " : { " ioInterfaceType " : " iscsi " , " iscsiAlias " : " " } , " label " : " beegfs_metadata2_iscsi_0 " ,
" hostRef " : " 84000000600A098000A4B9D10030370B5D430109 " ,
" id " : " 89000000600A098000A4B28D00303CFC5D4300F7 " } ] ,
" hostSidePorts " : [ { " type " : " iscsi " , " address " : " iqn.used_elsewhere " , " label " : " beegfs_metadata2_iscsi_0 " } ] ,
" id " : " 84000000600A098000A4B9D10030370B5D430120 " , " name " : " beegfs_metadata2 " } ]
HOST_GROUPS = [ { " clusterRef " : " 85000000600A098000A4B9D1003637135D483DEB " , " label " : " test_group " , " isSAControlled " : False ,
" confirmLUNMappingCreation " : False , " protectionInformationCapableAccessMethod " : True , " isLun0Restricted " : False ,
" id " : " 85000000600A098000A4B9D1003637135D483DEB " , " name " : " test_group " } ]
HOST_TYPES = [ { " name " : " FactoryDefault " , " index " : 0 , " code " : " FactoryDefault " } ,
{ " name " : " Windows 2000/Server 2003/Server 2008 Non-Clustered " , " index " : 1 , " code " : " W2KNETNCL " } ,
{ " name " : " Solaris " , " index " : 2 , " code " : " SOL " } ,
{ " name " : " Linux " , " index " : 6 , " code " : " LNX " } ,
{ " name " : " LnxALUA " , " index " : 7 , " code " : " LnxALUA " } ,
{ " name " : " Windows 2000/Server 2003/Server 2008 Clustered " , " index " : 8 , " code " : " W2KNETCL " } ,
{ " name " : " LnxTPGSALUA_SF " , " index " : 27 , " code " : " LnxTPGSALUA_SF " } ,
{ " name " : " LnxDHALUA " , " index " : 28 , " code " : " LnxDHALUA " } ]
REQ_FUNC = ' ansible.modules.storage.netapp.netapp_e_host.request '
REQ_FUNC = ' ansible.modules.storage.netapp.netapp_e_host.request '
def _set_args ( self , args ) :
def _set_args ( self , args ) :
@ -48,140 +89,401 @@ class HostTest(ModuleTestCase):
module_args . update ( args )
module_args . update ( args )
set_module_args ( module_args )
set_module_args ( module_args )
def test_delete_host ( self ) :
def test_host_exists_pass ( self ) :
""" Validate removing a host object """
""" Verify host_exists produces expected results. """
self . _set_args ( {
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
' state ' : ' absent '
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' new_host ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
} )
' ports ' : [ { ' label ' : ' new_host_port_1 ' , ' type ' : ' fc ' , ' port ' : ' 0x08ef08ef08ef08ef ' } ] } )
host = Host ( )
host = Host ( )
with self . assertRaises ( AnsibleExitJson ) as result :
self . assertFalse ( host . host_exists ( ) )
# We expect 2 calls to the API, the first to retrieve the host objects defined,
# the second to remove the host definition.
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' does_not_exist ' , ' host_type ' : ' linux dm-mp ' ,
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , [ self . HOST ] ) , ( 204 , { } ) ] ) as request :
' ports ' : [ { ' label ' : ' beegfs_storage1_iscsi_0 ' , ' type ' : ' iscsi ' ,
host . apply ( )
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
self . assertEquals ( request . call_count , 2 )
host = Host ( )
# We expect the module to make changes
self . assertFalse ( host . host_exists ( ) )
self . assertEquals ( result . exception . args [ 0 ] [ ' changed ' ] , True )
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_storage1 ' , ' host_type ' : ' linux dm-mp ' ,
def test_delete_host_no_changes ( self ) :
' ports ' : [ { ' label ' : ' beegfs_storage1_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.differentiqn.org ' } ] } )
""" Ensure that removing a host that doesn ' t exist works correctly. """
host = Host ( )
self . _set_args ( {
self . assertTrue ( host . host_exists ( ) )
' state ' : ' absent '
} )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
host = Host ( )
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
with self . assertRaises ( AnsibleExitJson ) as result :
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
# We expect a single call to the API: retrieve the defined hosts.
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ ] ) ) :
host = Host ( )
host . apply ( )
self . assertTrue ( host . host_exists ( ) )
# We should not mark changed=True
self . assertEquals ( result . exception . args [ 0 ] [ ' changed ' ] , False )
def test_host_exists ( self ) :
""" Test host_exists method """
self . _set_args ( {
' state ' : ' absent '
} )
host = Host ( )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ self . HOST ] ) ) as request :
host_exists = host . host_exists
self . assertTrue ( host_exists , msg = " This host should exist! " )
def test_host_exists_negative ( self ) :
""" Test host_exists method with no matching hosts to return """
self . _set_args ( {
' state ' : ' absent '
} )
host = Host ( )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ self . HOST_ALT ] ) ) as request :
host_exists = host . host_exists
self . assertFalse ( host_exists , msg = " This host should exist! " )
def test_host_exists_fail ( self ) :
def test_host_exists_fail ( self ) :
""" Ensure we do not dump a stack trace if we fail to make the request """
""" Verify host_exists produces expected exceptions. """
self . _set_args ( {
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' linux dm-mp ' , ' ports ' : [ { ' label ' : ' abc ' , ' type ' : ' iscsi ' , ' port ' : ' iqn:0 ' } ] } )
' state ' : ' absent '
host = Host ( )
} )
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to determine host existence. " ) :
host = Host ( )
with mock . patch ( self . REQ_FUNC , return_value = Exception ( ) ) :
with self . assertRaises ( AnsibleFailJson ) :
host . host_exists ( )
with mock . patch ( self . REQ_FUNC , side_effect = Exception ( " http_error " ) ) as request :
host_exists = host . host_exists
def test_needs_update_pass ( self ) :
""" Verify needs_update produces expected results. """
def test_needs_update_host_type ( self ) :
# No changes
""" Ensure a changed host_type triggers an update """
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( {
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' ,
' state ' : ' present ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
' host_type ' : 27
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
} )
host = Host ( )
host = Host ( )
host . host_exists ( )
host . host_obj = self . HOST
self . assertFalse ( host . needs_update ( ) )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ self . HOST ] ) ) as request :
needs_update = host . needs_update
# Change host type
self . assertTrue ( needs_update , msg = " An update to the host should be required! " )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : False ,
def test_needs_update_cluster ( self ) :
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.not_used ' } ] } )
""" Ensure a changed group_id triggers an update """
host = Host ( )
self . _set_args ( {
host . host_exists ( )
' state ' : ' present ' ,
self . assertTrue ( host . needs_update ( ) )
' host_type ' : self . HOST [ ' hostTypeIndex ' ] ,
' group ' : ' 1 ' ,
# Add port to host
} )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
host = Host ( )
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
host . host_obj = self . HOST
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.not_used ' } ] } )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ self . HOST ] ) ) as request :
host = Host ( )
needs_update = host . needs_update
host . host_exists ( )
self . assertTrue ( needs_update , msg = " An update to the host should be required! " )
self . assertTrue ( host . needs_update ( ) )
def test_needs_update_no_change ( self ) :
# Change port name
""" Ensure no changes do not trigger an update """
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( {
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' state ' : ' present ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_2 ' , ' type ' : ' iscsi ' ,
' host_type ' : self . HOST [ ' hostTypeIndex ' ] ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
} )
host = Host ( )
host = Host ( )
host . host_exists ( )
host . host_obj = self . HOST
self . assertTrue ( host . needs_update ( ) )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , [ self . HOST ] ) ) as request :
needs_update = host . needs_update
# take port from another host by force
self . assertFalse ( needs_update , msg = " An update to the host should be required! " )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
def test_needs_update_ports ( self ) :
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' ,
""" Ensure added ports trigger an update """
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
self . _set_args ( {
host = Host ( )
' state ' : ' present ' ,
host . host_exists ( )
' host_type ' : self . HOST [ ' hostTypeIndex ' ] ,
self . assertTrue ( host . needs_update ( ) )
' ports ' : [ { ' label ' : ' abc ' , ' type ' : ' iscsi ' , ' port ' : ' 0 ' } ] ,
} )
def test_needs_update_fail ( self ) :
host = Host ( )
""" Verify needs_update produces expected exceptions. """
host . host_obj = self . HOST
with self . assertRaisesRegexp ( AnsibleFailJson , " is associated with a different host. " ) :
with mock . patch . object ( host , ' all_hosts ' , [ self . HOST ] ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
needs_update = host . needs_update
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
self . assertTrue ( needs_update , msg = " An update to the host should be required! " )
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
def test_needs_update_changed_ports ( self ) :
host = Host ( )
""" Ensure changed ports trigger an update """
host . host_exists ( )
self . _set_args ( {
host . needs_update ( )
' state ' : ' present ' ,
' host_type ' : self . HOST [ ' hostTypeIndex ' ] ,
def test_valid_host_type_pass ( self ) :
' ports ' : [ { ' label ' : ' abc ' , ' type ' : ' iscsi ' , ' port ' : ' 0 ' } ] ,
""" Validate the available host types. """
} )
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . HOST_TYPES ) ) :
host = Host ( )
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' 0 ' } )
host . host_obj = self . HOST . copy ( )
host = Host ( )
host . host_obj [ ' hostSidePorts ' ] = [ { ' label ' : ' xyz ' , ' type ' : ' iscsi ' , ' port ' : ' 0 ' , ' address ' : ' iqn:0 ' } ]
self . assertTrue ( host . valid_host_type ( ) )
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' 28 ' } )
with mock . patch . object ( host , ' all_hosts ' , [ self . HOST ] ) :
host = Host ( )
needs_update = host . needs_update
self . assertTrue ( host . valid_host_type ( ) )
self . assertTrue ( needs_update , msg = " An update to the host should be required! " )
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' windows ' } )
host = Host ( )
def test_needs_update_changed_negative ( self ) :
self . assertTrue ( host . valid_host_type ( ) )
""" Ensure a ports update with no changes does not trigger an update """
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' linux dm-mp ' } )
self . _set_args ( {
host = Host ( )
' state ' : ' present ' ,
self . assertTrue ( host . valid_host_type ( ) )
' host_type ' : self . HOST [ ' hostTypeIndex ' ] ,
' ports ' : [ { ' label ' : ' abc ' , ' type ' : ' iscsi ' , ' port ' : ' 0 ' } ] ,
def test_valid_host_type_fail ( self ) :
} )
""" Validate the available host types. """
host = Host ( )
with self . assertRaisesRegexp ( AnsibleFailJson , " host_type must be either a host type name or host type index found integer the documentation " ) :
host . host_obj = self . HOST . copy ( )
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' non-host-type ' } )
host . host_obj [ ' hostSidePorts ' ] = [ { ' label ' : ' xyz ' , ' type ' : ' iscsi ' , ' port ' : ' 0 ' , ' address ' : ' iqn:0 ' } ]
host = Host ( )
with mock . patch . object ( host , ' all_hosts ' , [ self . HOST ] ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . HOST_TYPES ) ) :
needs_update = host . needs_update
with self . assertRaisesRegexp ( AnsibleFailJson , " There is no host type with index " ) :
self . assertTrue ( needs_update , msg = " An update to the host should be required! " )
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' 4 ' } )
host = Host ( )
host . valid_host_type ( )
with mock . patch ( self . REQ_FUNC , return_value = Exception ( ) ) :
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to get host types. " ) :
self . _set_args ( { ' state ' : ' present ' , ' host_type ' : ' 4 ' } )
host = Host ( )
host . valid_host_type ( )
def test_group_id_pass ( self ) :
""" Verify group_id produces expected results. """
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . HOST_GROUPS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
self . assertEqual ( host . group_id ( ) , " 0000000000000000000000000000000000000000 " )
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata2 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
self . assertEqual ( host . group_id ( ) , " 85000000600A098000A4B9D1003637135D483DEB " )
def test_group_id_fail ( self ) :
""" Verify group_id produces expected exceptions. """
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to get host groups. " ) :
with mock . patch ( self . REQ_FUNC , return_value = Exception ( ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata2 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False , ' group ' : ' test_group2 ' ,
' ports ' : [
{ ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . group_id ( )
with self . assertRaisesRegexp ( AnsibleFailJson , " No group with the name: " ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . HOST_GROUPS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata2 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False , ' group ' : ' test_group2 ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . group_id ( )
def test_assigned_host_ports_pass ( self ) :
""" Verify assigned_host_ports gives expected results. """
# Add an unused port to host
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.not_used ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
self . assertEquals ( host . assigned_host_ports ( ) , { } )
# Change port name (force)
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_2 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
self . assertEquals ( host . assigned_host_ports ( ) , { ' 84000000600A098000A4B9D10030370B5D430109 ' : [ ' 89000000600A098000A4B28D00303CFC5D4300F7 ' ] } )
# Change port type
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' fc ' , ' port ' : ' 08:ef:7e:24:52:a0 ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
self . assertEquals ( host . assigned_host_ports ( ) , { } )
# take port from another host by force
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.used_elsewhere ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
self . assertEquals ( host . assigned_host_ports ( ) , { ' 84000000600A098000A4B9D10030370B5D430109 ' : [ ' 89000000600A098000A4B28D00303CFC5D4300F7 ' ] } )
# take port from another host by force
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) , ( 200 , { } ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.used_elsewhere ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
self . assertEquals ( host . assigned_host_ports ( apply_unassigning = True ) ,
{ ' 84000000600A098000A4B9D10030370B5D430109 ' : [ ' 89000000600A098000A4B28D00303CFC5D4300F7 ' ] } )
def test_assigned_host_ports_fail ( self ) :
""" Verify assigned_host_ports gives expected exceptions. """
# take port from another
with self . assertRaisesRegexp ( AnsibleFailJson , " There are no host ports available OR there are not enough unassigned host ports " ) :
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_2 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . assigned_host_ports ( apply_unassigning = True )
# take port from another host and fail because force == False
with self . assertRaisesRegexp ( AnsibleFailJson , " There are no host ports available OR there are not enough unassigned host ports " ) :
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.used_elsewhere ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . assigned_host_ports ( apply_unassigning = True )
# take port from another host and fail because force == False
with self . assertRaisesRegexp ( AnsibleFailJson , " There are no host ports available OR there are not enough unassigned host ports " ) :
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata3 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.used_elsewhere ' } ] } )
host = Host ( )
host . host_exists ( )
host . assigned_host_ports ( apply_unassigning = True )
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to unassign host port. " ) :
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) , Exception ( ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata2_iscsi_0 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.used_elsewhere ' } ] } )
host = Host ( )
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . assigned_host_ports ( apply_unassigning = True )
def test_update_host_pass ( self ) :
""" Verify update_host produces expected results. """
# Change host type
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
# Change port iqn
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.not_used ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
# Change port type to fc
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' fc ' , ' port ' : ' 0x08ef08ef08ef08ef ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
# Change port name
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_12 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
# Change group
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , self . EXISTING_HOSTS ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : False , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . group_id = lambda : " 85000000600A098000A4B9D1003637135D483DEB "
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
def test_update_host_fail ( self ) :
""" Verify update_host produces expected exceptions. """
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to update host. " ) :
with mock . patch ( self . REQ_FUNC , side_effect = [ ( 200 , self . EXISTING_HOSTS ) , Exception ( ) ] ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : False , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . build_success_payload = lambda x : { }
host . group_id = lambda : " 85000000600A098000A4B9D1003637135D483DEB "
host . host_exists ( )
self . assertTrue ( host . needs_update ( ) )
host . update_host ( )
def test_create_host_pass ( self ) :
""" Verify create_host produces expected results. """
def _assigned_host_ports ( apply_unassigning = False ) :
return None
with self . assertRaises ( AnsibleExitJson ) :
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , { ' id ' : ' 84000000600A098000A4B9D10030370B5D430109 ' } ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
host = Host ( )
host . host_exists = lambda : False
host . assigned_host_ports = _assigned_host_ports
host . build_success_payload = lambda x : { }
host . group_id = lambda : " 85000000600A098000A4B9D1003637135D483DEB "
host . create_host ( )
def test_create_host_fail ( self ) :
""" Verify create_host produces expected exceptions. """
def _assigned_host_ports ( apply_unassigning = False ) :
return None
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to create host. " ) :
with mock . patch ( self . REQ_FUNC , return_value = Exception ( ) ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
host = Host ( )
host . host_exists = lambda : False
host . assigned_host_ports = _assigned_host_ports
host . build_success_payload = lambda x : { }
host . group_id = lambda : " 85000000600A098000A4B9D1003637135D483DEB "
host . create_host ( )
with self . assertRaisesRegexp ( AnsibleExitJson , " Host already exists. " ) :
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
host = Host ( )
host . host_exists = lambda : True
host . assigned_host_ports = _assigned_host_ports
host . build_success_payload = lambda x : { }
host . group_id = lambda : " 85000000600A098000A4B9D1003637135D483DEB "
host . create_host ( )
def test_remove_host_pass ( self ) :
""" Verify remove_host produces expected results. """
with mock . patch ( self . REQ_FUNC , return_value = ( 200 , None ) ) :
self . _set_args ( { ' state ' : ' absent ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . host_obj = { " id " : " 84000000600A098000A4B9D10030370B5D430109 " }
host . remove_host ( )
def test_remove_host_fail ( self ) :
""" Verify remove_host produces expected exceptions. """
with self . assertRaisesRegexp ( AnsibleFailJson , " Failed to remove host. " ) :
with mock . patch ( self . REQ_FUNC , return_value = Exception ( ) ) :
self . _set_args ( { ' state ' : ' absent ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' linux dm-mp ' , ' force_port ' : False , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_0 ' , ' type ' : ' iscsi ' ,
' port ' : ' iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8 ' } ] } )
host = Host ( )
host . host_obj = { " id " : " 84000000600A098000A4B9D10030370B5D430109 " }
host . remove_host ( )
def test_build_success_payload ( self ) :
""" Validate success payload. """
def _assigned_host_ports ( apply_unassigning = False ) :
return None
self . _set_args ( { ' state ' : ' present ' , ' name ' : ' beegfs_metadata1 ' , ' host_type ' : ' windows ' , ' force_port ' : True , ' group ' : ' test_group ' ,
' ports ' : [ { ' label ' : ' beegfs_metadata1_iscsi_1 ' , ' type ' : ' iscsi ' , ' port ' : ' iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818 ' } ] } )
host = Host ( )
self . assertEquals ( host . build_success_payload ( ) , { ' api_url ' : ' http://localhost/ ' , ' ssid ' : ' 1 ' } )