@ -20,6 +20,8 @@
from __future__ import ( absolute_import , division , print_function )
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
__metaclass__ = type
import contextlib
import ansible
import ansible
from io import BytesIO
from io import BytesIO
import json
import json
@ -60,8 +62,7 @@ class TestGalaxy(unittest.TestCase):
cls . temp_dir = tempfile . mkdtemp ( prefix = ' ansible-test_galaxy- ' )
cls . temp_dir = tempfile . mkdtemp ( prefix = ' ansible-test_galaxy- ' )
os . chdir ( cls . temp_dir )
os . chdir ( cls . temp_dir )
if os . path . exists ( " ./delete_me " ) :
shutil . rmtree ( " ./delete_me " , ignore_errors = True )
shutil . rmtree ( " ./delete_me " )
# creating framework for a role
# creating framework for a role
gc = GalaxyCLI ( args = [ " ansible-galaxy " , " init " , " --offline " , " delete_me " ] )
gc = GalaxyCLI ( args = [ " ansible-galaxy " , " init " , " --offline " , " delete_me " ] )
@ -88,29 +89,22 @@ class TestGalaxy(unittest.TestCase):
def makeTar ( cls , output_file , source_dir ) :
def makeTar ( cls , output_file , source_dir ) :
''' used for making a tarfile from a role directory '''
''' used for making a tarfile from a role directory '''
# adding directory into a tar file
# adding directory into a tar file
try :
with tarfile . open ( output_file , " w:gz " ) as tar :
tar = tarfile . open ( output_file , " w:gz " )
tar . add ( source_dir , arcname = os . path . basename ( source_dir ) )
tar . add ( source_dir , arcname = os . path . basename ( source_dir ) )
except AttributeError : # tarfile obj. has no attribute __exit__ prior to python 2. 7
pass
finally : # ensuring closure of tarfile obj
tar . close ( )
@classmethod
@classmethod
def tearDownClass ( cls ) :
def tearDownClass ( cls ) :
''' After tests are finished removes things created in setUpClass '''
''' After tests are finished removes things created in setUpClass '''
# deleting the temp role directory
# deleting the temp role directory
if os . path . exists ( cls . role_dir ) :
shutil . rmtree ( cls . role_dir , ignore_errors = True )
shutil . rmtree ( cls . role_dir )
with contextlib . suppress ( FileNotFoundError ) :
if os . path . exists ( cls . role_req ) :
os . remove ( cls . role_req )
os . remove ( cls . role_req )
if os . path . exists ( cls . role_ta r) :
with contextlib . suppress ( FileNotFoundErro r) :
os . remove ( cls . role_tar )
os . remove ( cls . role_tar )
if os . path . isdir ( cls . role_path ) :
shutil . rmtree ( cls . role_path , ignore_errors = True )
shutil . rmtree ( cls . role_path )
os . chdir ( ' / ' )
os . chdir ( ' / ' )
shutil . rmtree ( cls . temp_dir )
shutil . rmtree ( cls . temp_dir , ignore_errors = True )
def setUp ( self ) :
def setUp ( self ) :
# Reset the stored command line args
# Reset the stored command line args
@ -137,8 +131,7 @@ class TestGalaxy(unittest.TestCase):
role_info = { ' name ' : ' some_role_name ' ,
role_info = { ' name ' : ' some_role_name ' ,
' galaxy_info ' : galaxy_info }
' galaxy_info ' : galaxy_info }
display_result = gc . _display_role_info ( role_info )
display_result = gc . _display_role_info ( role_info )
if display_result . find ( ' \n \t galaxy_info: ' ) == - 1 :
self . assertNotEqual ( display_result . find ( ' \n \t galaxy_info: ' ) , - 1 , ' Expected galaxy_info to be indented once ' )
self . fail ( ' Expected galaxy_info to be indented once ' )
def test_run ( self ) :
def test_run ( self ) :
''' verifies that the GalaxyCLI object ' s api is created and that execute() is called. '''
''' verifies that the GalaxyCLI object ' s api is created and that execute() is called. '''
@ -277,8 +270,6 @@ class ValidRoleTests(object):
# Make temp directory for testing
# Make temp directory for testing
cls . test_dir = tempfile . mkdtemp ( )
cls . test_dir = tempfile . mkdtemp ( )
if not os . path . isdir ( cls . test_dir ) :
os . makedirs ( cls . test_dir )
cls . role_dir = os . path . join ( cls . test_dir , role_name )
cls . role_dir = os . path . join ( cls . test_dir , role_name )
cls . role_name = role_name
cls . role_name = role_name
@ -298,8 +289,7 @@ class ValidRoleTests(object):
@classmethod
@classmethod
def tearDownClass ( cls ) :
def tearDownClass ( cls ) :
if os . path . isdir ( cls . test_dir ) :
shutil . rmtree ( cls . test_dir , ignore_errors = True )
shutil . rmtree ( cls . test_dir )
def test_metadata ( self ) :
def test_metadata ( self ) :
with open ( os . path . join ( self . role_dir , ' meta ' , ' main.yml ' ) , ' r ' ) as mf :
with open ( os . path . join ( self . role_dir , ' meta ' , ' main.yml ' ) , ' r ' ) as mf :