mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Adding a test for GalaxyCLI and two methods on which it depends. Tes… (#16651)
* Adding a test for GalaxyCLI and two methods on which it depends. Tests execute_info method. * Adding a test for GalaxyCLI and two methods on which it depends. Tests execute_remove method. * Update test_galaxy.py * Revising test for GalaxyCLI execute_remove. * Removing mocks
This commit is contained in:
parent
54fc1a10fe
commit
9c925eeeab
1 changed files with 79 additions and 0 deletions
|
@ -23,6 +23,12 @@ from ansible.compat.six import PY3
|
||||||
from ansible.compat.tests import unittest
|
from ansible.compat.tests import unittest
|
||||||
|
|
||||||
from nose.plugins.skip import SkipTest
|
from nose.plugins.skip import SkipTest
|
||||||
|
import ansible
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import tarfile
|
||||||
|
|
||||||
|
from mock import patch
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
raise SkipTest('galaxy is not ported to be py3 compatible yet')
|
raise SkipTest('galaxy is not ported to be py3 compatible yet')
|
||||||
|
@ -30,6 +36,56 @@ if PY3:
|
||||||
from ansible.cli.galaxy import GalaxyCLI
|
from ansible.cli.galaxy import GalaxyCLI
|
||||||
|
|
||||||
class TestGalaxy(unittest.TestCase):
|
class TestGalaxy(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
'''creating prerequisites for installing a role; setUpClass occurs ONCE whereas setUp occurs with every method tested.'''
|
||||||
|
# class data for easy viewing: role_dir, role_tar, role_name, role_req, role_path
|
||||||
|
|
||||||
|
if os.path.exists("./delete_me"):
|
||||||
|
shutil.rmtree("./delete_me")
|
||||||
|
|
||||||
|
# creating framework for a role
|
||||||
|
gc = GalaxyCLI(args=["init"])
|
||||||
|
with patch('sys.argv', ["-c", "delete_me"]):
|
||||||
|
gc.parse()
|
||||||
|
gc.run()
|
||||||
|
cls.role_dir = "./delete_me"
|
||||||
|
cls.role_name = "delete_me"
|
||||||
|
cls.role_path = "/etc/ansible/roles"
|
||||||
|
|
||||||
|
# creating a tar file name for class data
|
||||||
|
cls.role_tar = './delete_me.tar.gz'
|
||||||
|
cls.makeTar(cls.role_tar, cls.role_dir)
|
||||||
|
|
||||||
|
# creating a temp file with installation requirements
|
||||||
|
cls.role_req = './delete_me_requirements.yml'
|
||||||
|
fd = open(cls.role_req, "w")
|
||||||
|
fd.write("- 'src': '%s'\n 'name': '%s'\n 'path': '%s'" % (cls.role_tar, cls.role_name, cls.role_path))
|
||||||
|
fd.close()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def makeTar(cls, output_file, source_dir):
|
||||||
|
''' used for making a tarfile from a role directory '''
|
||||||
|
# adding directory into a tar file
|
||||||
|
try:
|
||||||
|
tar = tarfile.open(output_file, "w:gz")
|
||||||
|
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
|
||||||
|
def tearDownClass(cls):
|
||||||
|
'''After tests are finished removes things created in setUpClass'''
|
||||||
|
# deleting the temp role directory
|
||||||
|
if os.path.exists(cls.role_dir):
|
||||||
|
shutil.rmtree(cls.role_dir)
|
||||||
|
if os.path.exists(cls.role_req):
|
||||||
|
os.remove(cls.role_req)
|
||||||
|
if os.path.exists(cls.role_tar):
|
||||||
|
os.remove(cls.role_tar)
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.default_args = []
|
self.default_args = []
|
||||||
|
|
||||||
|
@ -51,3 +107,26 @@ class TestGalaxy(unittest.TestCase):
|
||||||
display_result = gc._display_role_info(role_info)
|
display_result = gc._display_role_info(role_info)
|
||||||
if display_result.find('\t\tgalaxy_tags:') > -1:
|
if display_result.find('\t\tgalaxy_tags:') > -1:
|
||||||
self.fail('Expected galaxy_tags to be indented twice')
|
self.fail('Expected galaxy_tags to be indented twice')
|
||||||
|
|
||||||
|
def test_execute_remove(self):
|
||||||
|
# installing role
|
||||||
|
gc = GalaxyCLI(args=["install"])
|
||||||
|
with patch('sys.argv', ["--offline", "-r", self.role_req]):
|
||||||
|
galaxy_parser = gc.parse()
|
||||||
|
gc.run()
|
||||||
|
|
||||||
|
# checking that installation worked
|
||||||
|
role_file = os.path.join(self.role_path, self.role_name)
|
||||||
|
self.assertTrue(os.path.exists(role_file))
|
||||||
|
|
||||||
|
# removing role
|
||||||
|
gc.args = ["remove"]
|
||||||
|
with patch('sys.argv', ["-c", self.role_name]):
|
||||||
|
galaxy_parser = gc.parse()
|
||||||
|
super(GalaxyCLI, gc).run()
|
||||||
|
gc.api = ansible.galaxy.api.GalaxyAPI(gc.galaxy)
|
||||||
|
completed_task = gc.execute_remove()
|
||||||
|
|
||||||
|
# testing role was removed
|
||||||
|
self.assertTrue(completed_task == 0)
|
||||||
|
self.assertTrue(not os.path.exists(role_file))
|
||||||
|
|
Loading…
Reference in a new issue