@ -23,6 +23,8 @@ import io
import inspect
import inspect
from shutil import copyfile , move
from shutil import copyfile , move
import difflib
import difflib
import tempfile
import shutil
class AnsibleFailJson ( Exception ) :
class AnsibleFailJson ( Exception ) :
@ -169,25 +171,29 @@ class TestInterfacesFileModule(unittest.TestCase):
}
}
for testname , options_list in testcases . items ( ) :
for testname , options_list in testcases . items ( ) :
for testfile in self . getTestFiles ( ) :
for testfile in self . getTestFiles ( ) :
path = os . path . join ( fixture_path , testfile )
with tempfile . NamedTemporaryFile ( ) as temp_file :
lines , ifaces = interfaces_file . read_interfaces_file ( module , path )
src_path = os . path . join ( fixture_path , testfile )
backupp = module . backup_local ( path )
path = temp_file . name
options = options_list [ 0 ]
shutil . copy ( src_path , path )
for state in [ ' present ' , ' absent ' ] :
lines , ifaces = interfaces_file . read_interfaces_file ( module , path )
fail_json_iterations = [ ]
backupp = module . backup_local ( path )
options [ ' state ' ] = state
options = options_list [ 0 ]
try :
for state in [ ' present ' , ' absent ' ] :
_ , lines = interfaces_file . setInterfaceOption ( module , lines , options [ ' iface ' ] , options [ ' option ' ] , options [ ' value ' ] , options [ ' state ' ] )
fail_json_iterations = [ ]
except AnsibleFailJson as e :
options [ ' state ' ] = state
fail_json_iterations . append ( " fail_json message: %s \n options: \n %s " %
try :
( str ( e ) , json . dumps ( options , sort_keys = True , indent = 4 , separators = ( ' , ' , ' : ' ) ) ) )
_ , lines = interfaces_file . setInterfaceOption ( module , lines ,
interfaces_file . write_changes ( module , [ d [ ' line ' ] for d in lines if ' line ' in d ] , path )
options [ ' iface ' ] , options [ ' option ' ] , options [ ' value ' ] , options [ ' state ' ] )
except AnsibleFailJson as e :
self . compareStringWithFile ( " \n ===== \n " . join ( fail_json_iterations ) , " %s _ %s .exceptions.txt " % ( testfile , testname ) )
fail_json_iterations . append ( " fail_json message: %s \n options: \n %s " %
( str ( e ) , json . dumps ( options , sort_keys = True , indent = 4 , separators = ( ' , ' , ' : ' ) ) ) )
self . compareInterfacesLinesToFile ( lines , testfile , " %s _ %s " % ( testfile , testname ) )
interfaces_file . write_changes ( module , [ d [ ' line ' ] for d in lines if ' line ' in d ] , path )
self . compareInterfacesToFile ( ifaces , testfile , " %s _ %s .json " % ( testfile , testname ) )
self . compareFileToBackup ( path , backupp )
self . compareStringWithFile ( " \n ===== \n " . join ( fail_json_iterations ) , " %s _ %s .exceptions.txt " % ( testfile , testname ) )
self . compareInterfacesLinesToFile ( lines , testfile , " %s _ %s " % ( testfile , testname ) )
self . compareInterfacesToFile ( ifaces , testfile , " %s _ %s .json " % ( testfile , testname ) )
self . compareFileToBackup ( path , backupp )
def test_change_method ( self ) :
def test_change_method ( self ) :
testcases = {
testcases = {
@ -202,21 +208,24 @@ class TestInterfacesFileModule(unittest.TestCase):
}
}
for testname , options_list in testcases . items ( ) :
for testname , options_list in testcases . items ( ) :
for testfile in self . getTestFiles ( ) :
for testfile in self . getTestFiles ( ) :
path = os . path . join ( fixture_path , testfile )
with tempfile . NamedTemporaryFile ( ) as temp_file :
lines , ifaces = interfaces_file . read_interfaces_file ( module , path )
src_path = os . path . join ( fixture_path , testfile )
backupp = module . backup_local ( path )
path = temp_file . name
options = options_list [ 0 ]
shutil . copy ( src_path , path )
fail_json_iterations = [ ]
lines , ifaces = interfaces_file . read_interfaces_file ( module , path )
try :
backupp = module . backup_local ( path )
_ , lines = interfaces_file . setInterfaceOption ( module , lines , options [ ' iface ' ] , options [ ' option ' ] , options [ ' value ' ] , options [ ' state ' ] )
options = options_list [ 0 ]
except AnsibleFailJson as e :
fail_json_iterations = [ ]
fail_json_iterations . append ( " fail_json message: %s \n options: \n %s " %
try :
( str ( e ) , json . dumps ( options , sort_keys = True , indent = 4 , separators = ( ' , ' , ' : ' ) ) ) )
_ , lines = interfaces_file . setInterfaceOption ( module , lines , options [ ' iface ' ] , options [ ' option ' ] , options [ ' value ' ] , options [ ' state ' ] )
interfaces_file . write_changes ( module , [ d [ ' line ' ] for d in lines if ' line ' in d ] , path )
except AnsibleFailJson as e :
fail_json_iterations . append ( " fail_json message: %s \n options: \n %s " %
( str ( e ) , json . dumps ( options , sort_keys = True , indent = 4 , separators = ( ' , ' , ' : ' ) ) ) )
interfaces_file . write_changes ( module , [ d [ ' line ' ] for d in lines if ' line ' in d ] , path )
self . compareStringWithFile ( " \n ===== \n " . join ( fail_json_iterations ) , " %s _ %s .exceptions.txt " % ( testfile , testname ) )
self . compareStringWithFile ( " \n ===== \n " . join ( fail_json_iterations ) , " %s _ %s .exceptions.txt " % ( testfile , testname ) )
self . compareInterfacesLinesToFile ( lines , testfile , " %s _ %s " % ( testfile , testname ) )
self . compareInterfacesLinesToFile ( lines , testfile , " %s _ %s " % ( testfile , testname ) )
self . compareInterfacesToFile ( ifaces , testfile , " %s _ %s .json " % ( testfile , testname ) )
self . compareInterfacesToFile ( ifaces , testfile , " %s _ %s .json " % ( testfile , testname ) )
# Restore backup
# Restore backup
move ( backupp , path )
move ( backupp , path )