Ejemplo n.º 1
0
    def __init__(self, command_args, global_args, molecule=False):
        """
        Initialize commands

        :param command_args: arguments of the command
        :param global_args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = docopt(self.__doc__, argv=command_args)
        self.args['<command>'] = self.__class__.__name__.lower()
        self.command_args = command_args

        self.static = False

        # give us the option to reuse an existing molecule instance
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # init doesn't need to load molecule files
        if self.__class__.__name__ == 'Init':
            return

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True

        # Add or update the group_vars if needed.
        self.molecule._add_or_update_group_vars()
Ejemplo n.º 2
0
class TestStringMethods(testtools.TestCase):

    OUTPUT_MIXED_FAILED = """
    TASK: [cisco.zuul | Adjust setup script] **************************************
    ok: [aio-01-ubuntu]

    TASK: [cisco.zuul | file ] ****************************************************
    changed: [aio-01-ubuntu]

    TASK: [cisco.zuul | Fetch Zuul portal dependencies] ***************************
    ok: [aio-01-ubuntu]

    TASK: [cisco.zuul | start zuul service] ***************************************
    changed: [aio-01-ubuntu]

    TASK: [cisco.zuul | start zuul merger service] ********************************
    changed: [aio-01-ubuntu]

    NOTIFIED: [cisco.zuul | restart apache2] **************************************
    changed: [aio-01-ubuntu]

    PLAY RECAP ********************************************************************
    aio-01-ubuntu              : ok=36   changed=29   unreachable=0    failed=0
    """

    OUTPUT_MIXED_SUCCESS = """
    TASK: [cisco.zuul | Adjust setup script] **************************************
    ok: [aio-01-ubuntu]

    TASK: [cisco.zuul | Fetch Zuul portal dependencies] ***************************
    ok: [aio-01-ubuntu]

    NOTIFIED: [cisco.zuul | restart apache2] **************************************
    changed: [aio-01-ubuntu]

    PLAY RECAP ********************************************************************
    aio-01-ubuntu              : ok=36   changed=0    unreachable=0    failed=0
    """

    def setUp(self):
        super(TestStringMethods, self).setUp()
        with patch('molecule.core.Molecule._main') as mocked:
            mocked.return_value = None
            self.molecule = Molecule(None)

    def test_parse_provisioning_output_failure_00(self):
        res = self.molecule._parse_provisioning_output(
            TestStringMethods.OUTPUT_MIXED_FAILED)
        self.assertFalse(res)

    def test_parse_provisioning_output_success_00(self):
        res = self.molecule._parse_provisioning_output(
            TestStringMethods.OUTPUT_MIXED_SUCCESS)
        self.assertTrue(res)
Ejemplo n.º 3
0
class AbstractCommand:
    def __init__(self, command_args, global_args, molecule=False):
        """
        Initialize commands

        :param command_args: arguments of the command
        :param global_args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = docopt(self.__doc__, argv=command_args)
        self.args['<command>'] = self.__class__.__name__.lower()
        self.command_args = command_args

        self.static = False

        # give us the option to reuse an existing molecule instance
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # init doesn't need to load molecule files
        if self.__class__.__name__ == 'Init':
            return

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True

        # Add or update the group_vars if needed.
        self.molecule._add_or_update_group_vars()

    def disabled(self, cmd):
        """
        Prints 'command disabled' message and exits program.

        :param cmd: Name of the disabled command to print.
        :return: None
        """
        fmt = [colorama.Fore.RED, cmd, colorama.Fore.RESET]
        errmsg = "{}The `{}` command isn't supported with static inventory.{}"
        utilities.logger.error(errmsg.format(*fmt))
        sys.exit(1)

    def execute(self):
        raise NotImplementedError
Ejemplo n.º 4
0
class AbstractCommand:
    def __init__(self, command_args, global_args, molecule=False):
        """
        Initialize commands

        :param command_args: arguments of the command
        :param global_args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = docopt(self.__doc__, argv=command_args)
        self.args['<command>'] = self.__class__.__name__.lower()
        self.command_args = command_args

        self.static = False

        # give us the option to reuse an existing molecule instance
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # init doesn't need to load molecule files
        if self.__class__.__name__ == 'Init':
            return

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True

        # Add or update the group_vars if needed.
        self.molecule._add_or_update_group_vars()

    def disabled(self, cmd):
        """
        Prints 'command disabled' message and exits program.

        :param cmd: Name of the disabled command to print.
        :return: None
        """
        fmt = [Fore.RED, cmd, Fore.RESET]
        errmsg = "{}The `{}` command isn't supported with static inventory.{}"
        print(errmsg.format(*fmt))
        sys.exit(1)

    def execute(self):
        raise NotImplementedError
Ejemplo n.º 5
0
class Commands(object):
    def __init__(self, args):
        self.args = args

    def main(self):
        self.molecule = Molecule(self.args)
        self.molecule.main()

        # assume static inventory if no vagrant config block is defined
        if self.molecule._config.config.get('vagrant') is None:
            self.commands = StaticCommands(self.molecule)
            return

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._config.config['vagrant'].get('instances') is None:
            self.commands = StaticCommands(self.molecule)
            return

        self.commands = BaseCommands(self.molecule)

    def destroy(self):
        self.commands.destroy()

    def create(self):
        self.commands.create()

    def converge(self):
        self.commands.converge()

    def idempotence(self):
        self.commands.idempotence()

    def verify(self):
        self.commands.verify()

    def test(self):
        self.commands.test()

    def list(self):
        self.commands.list()

    def status(self):
        self.commands.status()

    def login(self):
        self.commands.login()

    def init(self):
        self.commands.init()
Ejemplo n.º 6
0
    def __init__(self, command_args, global_args, molecule=False):
        """
        Initialize commands

        :param command_args: arguments of the command
        :param global_args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = docopt(self.__doc__, argv=command_args)
        self.args['<command>'] = self.__class__.__name__.lower()
        self.command_args = command_args

        self.static = False

        # give us the option to reuse an existing molecule instance
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # init doesn't need to load molecule files
        if self.__class__.__name__ == 'Init':
            return

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True

        # Add or update the group_vars if needed.
        self.molecule._add_or_update_group_vars()
Ejemplo n.º 7
0
class AbstractCommand:
    def __init__(self, args, molecule=False):
        """
        Initialize commands

        :param args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = args
        self.static = False

        # only create a molecule instance if one doesn't exist
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True

    def disabled(self, cmd):
        """
        Prints 'command disabled' message and exits program.

        :param cmd: Name of the disabled command to print.
        :return: None
        """
        fmt = [Fore.RED, cmd, Fore.RESET]
        errmsg = "{}The `{}` command isn't supported with static inventory.{}"
        print(errmsg.format(*fmt))
        sys.exit(1)

    def execute(self):
        raise NotImplementedError
Ejemplo n.º 8
0
class TestCore(testtools.TestCase):
    def setUp(self):
        super(TestCore, self).setUp()
        self._molecule = Molecule(None)

    def test_parse_provisioning_output_failure_00(self):
        failed_output = """
        PLAY RECAP ********************************************************************
        vagrant-01-ubuntu              : ok=36   changed=29   unreachable=0    failed=0
        """

        res, changed_tasks = self._molecule._parse_provisioning_output(
            failed_output)

        self.assertFalse(res)

    def test_parse_provisioning_output_failure_01(self):
        failed_output = """
        PLAY RECAP ********************************************************************
        NI: cisco.common | Non idempotent task for testing
        common-01-rhel-7           : ok=18   changed=14   unreachable=0    failed=0
        """

        res, changed_tasks = self._molecule._parse_provisioning_output(
            failed_output)

        self.assertFalse(res)
        self.assertEqual(1, len(changed_tasks))

    def test_parse_provisioning_output_success_00(self):
        success_output = """
        PLAY RECAP ********************************************************************
        vagrant-01-ubuntu              : ok=36   changed=0    unreachable=0    failed=0
        """

        res, changed_tasks = self._molecule._parse_provisioning_output(
            success_output)

        self.assertTrue(res)
        self.assertEqual([], changed_tasks)
Ejemplo n.º 9
0
    def setUp(self):
        super(TestDockerProvisioner, self).setUp()
        # Setup mock molecule
        self._mock_molecule = Molecule(dict())

        self.temp = '/tmp/test_config_load_defaults_external_file.yml'
        data = {
            'molecule': {
                'molecule_dir': '.test_molecule',
                'inventory_file': 'tests/ansible_inventory'
            },
            'docker': {
                'containers': [{
                    'name': 'test1',
                    'image': 'ubuntu',
                    'image_version': 'latest',
                    'ansible_groups': ['group1']
                }, {
                    'name': 'test2',
                    'image': 'ubuntu',
                    'image_version': 'latest',
                    'ansible_groups': ['group2']
                }]
            },
            'ansible': {
                'config_file': 'test_config',
                'inventory_file': 'test_inventory'
            }
        }

        with open(self.temp, 'w') as f:
            f.write(yaml.dump(data, default_flow_style=True))

        self._mock_molecule._config.load_defaults_file(defaults_file=self.temp)

        self._mock_molecule._state = dict()
Ejemplo n.º 10
0
    def main(self):
        self.molecule = Molecule(self.args)
        self.molecule.main()

        # assume static inventory if no vagrant config block is defined
        if self.molecule._config.config.get('vagrant') is None:
            self.commands = StaticCommands(self.molecule)
            return

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._config.config['vagrant'].get('instances') is None:
            self.commands = StaticCommands(self.molecule)
            return

        self.commands = BaseCommands(self.molecule)
Ejemplo n.º 11
0
class TestCore(testtools.TestCase):
    def setUp(self):
        super(TestCore, self).setUp()
        with patch('molecule.core.Molecule._main') as mocked:
            mocked.return_value = None
            self._molecule = Molecule(None)

    def test_parse_provisioning_output_failure_00(self):
        failed_output = """
        PLAY RECAP ********************************************************************
        vagrant-01-ubuntu              : ok=36   changed=29   unreachable=0    failed=0
        """
        res = self._molecule._parse_provisioning_output(failed_output)

        self.assertFalse(res)

    def test_parse_provisioning_output_success_00(self):
        success_output = """
        PLAY RECAP ********************************************************************
        vagrant-01-ubuntu              : ok=36   changed=0    unreachable=0    failed=0
        """
        res = self._molecule._parse_provisioning_output(success_output)

        self.assertTrue(res)
Ejemplo n.º 12
0
    def __init__(self, args, molecule=False):
        """
        Initialize commands

        :param args: arguments from the CLI
        :param molecule: molecule instance
        """
        self.args = args
        self.static = False

        # only create a molecule instance if one doesn't exist
        if not molecule:
            self.molecule = Molecule(self.args)
            self.molecule.main()
        else:
            self.molecule = molecule

        # assume static inventory if no vagrant config block is defined
        if self.molecule._provisioner is None:
            self.static = True

        # assume static inventory if no instances are defined in vagrant config block
        if self.molecule._provisioner.instances is None:
            self.static = True
Ejemplo n.º 13
0
    def setUp(self):
        super(TestDockerProvisioner, self).setUp()
        # Setup mock molecule
        self._mock_molecule = Molecule(dict())

        self.temp = '/tmp/test_config_load_defaults_external_file.yml'
        data = {
            'molecule': {
                'molecule_dir': '.test_molecule',
                'inventory_file': 'tests/ansible_inventory'
            },

            'docker': {
                'containers': [
                    {'name': 'test1',
                     'image': 'ubuntu',
                     'image_version': 'latest',
                     'ansible_groups': ['group1']},
                    {'name': 'test2',
                     'image': 'ubuntu',
                     'image_version': 'latest',
                     'ansible_groups': ['group2']}
                ]
            },
            'ansible': {
                'config_file': 'test_config',
                'inventory_file': 'test_inventory'
            }
        }

        with open(self.temp, 'w') as f:
            f.write(yaml.dump(data, default_flow_style=True))

        self._mock_molecule._config.load_defaults_file(defaults_file=self.temp)

        self._mock_molecule._state = dict()
Ejemplo n.º 14
0
class TestDockerProvisioner(testtools.TestCase):
    def setUp(self):
        super(TestDockerProvisioner, self).setUp()
        # Setup mock molecule
        self._mock_molecule = Molecule(dict())

        self.temp = '/tmp/test_config_load_defaults_external_file.yml'
        data = {
            'molecule': {
                'molecule_dir': '.test_molecule',
                'inventory_file': 'tests/ansible_inventory'
            },
            'docker': {
                'containers': [{
                    'name': 'test1',
                    'image': 'ubuntu',
                    'image_version': 'latest',
                    'ansible_groups': ['group1']
                }, {
                    'name': 'test2',
                    'image': 'ubuntu',
                    'image_version': 'latest',
                    'ansible_groups': ['group2']
                }]
            },
            'ansible': {
                'config_file': 'test_config',
                'inventory_file': 'test_inventory'
            }
        }

        with open(self.temp, 'w') as f:
            f.write(yaml.dump(data, default_flow_style=True))

        self._mock_molecule._config.load_defaults_file(defaults_file=self.temp)

        self._mock_molecule._state = dict()

    def test_name(self):
        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)
        # false values don't exist in arg dict at all
        self.assertEqual(docker_provisioner.name, 'docker')

    def test_get_provisioner(self):
        self.assertEqual(
            provisioners.get_provisioner(self._mock_molecule).name, 'docker')

    def test_up(self):
        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)
        docker_provisioner.up()
        docker_provisioner.destroy()

    def test_instances(self):
        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)
        self.assertEqual(docker_provisioner.instances[0]['name'], 'test1')
        self.assertEqual(docker_provisioner.instances[1]['name'], 'test2')

    def test_status(self):
        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)

        docker_provisioner.up()

        self.assertEquals('test1', docker_provisioner.status()[1].name)
        self.assertEquals('test2', docker_provisioner.status()[0].name)

        self.assertIn('Up', docker_provisioner.status()[1].state)
        self.assertIn('Up', docker_provisioner.status()[0].state)

        self.assertEqual('docker', docker_provisioner.status()[0].provider)
        self.assertEqual('docker', docker_provisioner.status()[1].provider)

    def test_destroy(self):
        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)

        docker_provisioner.up()

        self.assertEquals('test1', docker_provisioner.status()[1].name)
        self.assertEquals('test2', docker_provisioner.status()[0].name)

        self.assertIn('Up', docker_provisioner.status()[1].state)
        self.assertIn('Up', docker_provisioner.status()[0].state)

        docker_provisioner.destroy()

        self.assertIn('Not Created', docker_provisioner.status()[1].state)
        self.assertIn('Not Created', docker_provisioner.status()[0].state)

    def test_provision(self):

        docker_provisioner = provisioners.DockerProvisioner(
            self._mock_molecule)
        docker_provisioner.destroy()
        docker_provisioner.up()

        self.book = docker_provisioner.ansible_connection_params
        self.book['playbook'] = 'tests/playbook.yml'
        self.book['inventory'] = 'test1,test2,'

        self.ansible = AnsiblePlaybook(self.book)

        self.assertEqual((None, ''), self.ansible.execute())

        docker_provisioner.destroy()

    def test_inventory_generation(self):
        self._mock_molecule._provisioner = provisioners.get_provisioner(
            self._mock_molecule)
        self._mock_molecule._provisioner.destroy()
        self._mock_molecule._provisioner.up()

        self._mock_molecule._create_inventory_file()

        self.book = self._mock_molecule._provisioner.ansible_connection_params
        self.book['playbook'] = 'tests/playbook.yml'
        self.book['inventory'] = 'tests/ansible_inventory'

        self.ansible = AnsiblePlaybook(self.book)

        self.assertEqual((None, ''), self.ansible.execute())
Ejemplo n.º 15
0
 def setUp(self):
     super(TestCore, self).setUp()
     self._molecule = Molecule(None)
Ejemplo n.º 16
0
 def setUp(self):
     super(TestCore, self).setUp()
     with patch('molecule.core.Molecule._main') as mocked:
         mocked.return_value = None
         self._molecule = Molecule(None)
Ejemplo n.º 17
0
 def setUp(self):
     super(TestStringMethods, self).setUp()
     with patch('molecule.core.Molecule._main') as mocked:
         mocked.return_value = None
         self.molecule = Molecule(None)
Ejemplo n.º 18
0
class TestDockerProvisioner(testtools.TestCase):
    def setUp(self):
        super(TestDockerProvisioner, self).setUp()
        # Setup mock molecule
        self._mock_molecule = Molecule(dict())

        self.temp = '/tmp/test_config_load_defaults_external_file.yml'
        data = {
            'molecule': {
                'molecule_dir': '.test_molecule',
                'inventory_file': 'tests/ansible_inventory'
            },

            'docker': {
                'containers': [
                    {'name': 'test1',
                     'image': 'ubuntu',
                     'image_version': 'latest',
                     'ansible_groups': ['group1']},
                    {'name': 'test2',
                     'image': 'ubuntu',
                     'image_version': 'latest',
                     'ansible_groups': ['group2']}
                ]
            },
            'ansible': {
                'config_file': 'test_config',
                'inventory_file': 'test_inventory'
            }
        }

        with open(self.temp, 'w') as f:
            f.write(yaml.dump(data, default_flow_style=True))

        self._mock_molecule._config.load_defaults_file(defaults_file=self.temp)

        self._mock_molecule._state = dict()

    def test_name(self):
        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)
        # false values don't exist in arg dict at all
        self.assertEqual(docker_provisioner.name, 'docker')

    def test_get_provisioner(self):
        self.assertEqual(provisioners.get_provisioner(self._mock_molecule).name, 'docker')

    def test_up(self):
        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)
        docker_provisioner.up()
        docker_provisioner.destroy()

    def test_instances(self):
        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)
        self.assertEqual(docker_provisioner.instances[0]['name'], 'test1')
        self.assertEqual(docker_provisioner.instances[1]['name'], 'test2')

    def test_status(self):
        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)

        docker_provisioner.up()

        self.assertEquals('test1', docker_provisioner.status()[1].name)
        self.assertEquals('test2', docker_provisioner.status()[0].name)

        self.assertIn('Up', docker_provisioner.status()[1].state)
        self.assertIn('Up', docker_provisioner.status()[0].state)

        self.assertEqual('docker', docker_provisioner.status()[0].provider)
        self.assertEqual('docker', docker_provisioner.status()[1].provider)

    def test_destroy(self):
        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)

        docker_provisioner.up()

        self.assertEquals('test1', docker_provisioner.status()[1].name)
        self.assertEquals('test2', docker_provisioner.status()[0].name)

        self.assertIn('Up', docker_provisioner.status()[1].state)
        self.assertIn('Up', docker_provisioner.status()[0].state)

        docker_provisioner.destroy()

        self.assertIn('Not Created', docker_provisioner.status()[1].state)
        self.assertIn('Not Created', docker_provisioner.status()[0].state)

    def test_provision(self):

        docker_provisioner = provisioners.DockerProvisioner(self._mock_molecule)
        docker_provisioner.destroy()
        docker_provisioner.up()

        self.book = docker_provisioner.ansible_connection_params
        self.book['playbook'] = 'tests/playbook.yml'
        self.book['inventory'] = 'test1,test2,'

        self.ansible = AnsiblePlaybook(self.book)

        self.assertEqual((None, ''), self.ansible.execute())

        docker_provisioner.destroy()

    def test_inventory_generation(self):
        self._mock_molecule._provisioner = provisioners.get_provisioner(self._mock_molecule)
        self._mock_molecule._provisioner.destroy()
        self._mock_molecule._provisioner.up()

        self._mock_molecule._create_inventory_file()

        self.book = self._mock_molecule._provisioner.ansible_connection_params
        self.book['playbook'] = 'tests/playbook.yml'
        self.book['inventory'] = 'tests/ansible_inventory'

        self.ansible = AnsiblePlaybook(self.book)

        self.assertEqual((None, ''), self.ansible.execute())