def test_check_output(self):
        self.patch_object(generic_utils, 'logging', name='mock_logging')

        async def mock_communicate():
            return (b"output log", b"error log")

        mock_proc = mock.Mock(communicate=mock_communicate, returncode=0)

        async def mock_create_subprocess_exec(*args, **kwargs):
            return mock_proc

        self.patch_object(generic_utils.asyncio,
                          'create_subprocess_exec',
                          side_effect=mock_create_subprocess_exec)
        check_call = zaza.sync_wrapper(generic_utils.check_output)
        expected = {
            'Code': str(mock_proc.returncode),
            'Stderr': "error log",
            'Stdout': "output log",
        }
        self.assertEqual(check_call(['a', 'command']), expected)
        # check for raising an error
        mock_proc.returncode = 5
        expected = {
            'Code': str(mock_proc.returncode),
            'Stderr': "error log",
            'Stdout': "output log",
        }
        self.subprocess.CalledProcessError = subprocess.CalledProcessError
        try:
            check_call(['a', 'command'])
        except subprocess.CalledProcessError:
            pass
예제 #2
0
    def test_get_machine_interfaces(self):
        async def af():
            async for machine, interface in maas.async_get_machine_interfaces(
                    self.maas_client):
                if machine == self.machine1:
                    self.assertIn(interface,
                                  (self.m1_interface1, self.m1_interface2))
                elif machine == self.machine2:
                    self.assertIn(interface,
                                  (self.m2_interface1, self.m2_interface2))
                else:
                    self.assertIn(machine, (self.machine1, self.machine2))

        f = zaza.sync_wrapper(af)
        f()
    def test_check_call(self):
        async def async_check_output(*args, **kwargs):
            return "hello"

        self.patch_object(generic_utils,
                          'check_output',
                          side_effect=async_check_output)
        check_call = zaza.sync_wrapper(generic_utils.check_call)
        check_call("a command")
        self.check_output.assert_called_once_with('a command',
                                                  log_stdout=True,
                                                  log_stderr=True)
        self.check_output.reset_mock()
        check_call("b command", log_stdout=False)
        self.check_output.assert_called_once_with('b command',
                                                  log_stdout=False,
                                                  log_stderr=True)
        self.check_output.reset_mock()
        check_call("c command", log_stderr=False)
        self.check_output.assert_called_once_with('c command',
                                                  log_stdout=True,
                                                  log_stderr=False)
예제 #4
0
    """Add a model to the current controller.

    :param model_name: Name to give the new model.
    :type model_name: str
    :param config: Model configuration.
    :type config: dict
    """
    controller = Controller()
    await controller.connect()
    logging.debug("Adding model {}".format(model_name))
    model = await controller.add_model(model_name, config=config)
    await model.disconnect()
    await controller.disconnect()


add_model = sync_wrapper(async_add_model)


async def async_destroy_model(model_name):
    """Remove a model from the current controller.

    :param model_name: Name of model to remove
    :type model_name: str
    """
    controller = Controller()
    await controller.connect()
    logging.debug("Destroying model {}".format(model_name))
    await controller.destroy_model(model_name)
    await controller.disconnect()

예제 #5
0
파일: maas.py 프로젝트: Gleland/zaza

async def async_get_maas_client(maas_url, apikey):
    """Get a connected MAAS client.

    :param maas_url: URL to MAAS API.
    :type maas_url: str
    :param apikey: MAAS API Key for authentication.
    :type apikey: str
    :returns: Connected MAAS client.
    :rtpye: maas.client.facade.Client object.
    """
    return await maas.client.connect(maas_url, apikey=apikey)


get_maas_client = zaza.sync_wrapper(async_get_maas_client)


@async_generator
async def async_get_machine_interfaces(maas_client,
                                       machine_id=None,
                                       link_mode=None,
                                       cidr=None):
    """Get machine and interface objects, optionally apply filters.

    :param maas_client: MAAS Client object.
    :type maas_client: maas.client.facade.Client
    :param machine_id: ID of a specific machine to get information on.
    :type machine_id: Optional[str]
    :param link_mode: Only return interfaces with this link_mode.
    :type link_mode: Optional[LinkMode]
예제 #6
0
파일: model.py 프로젝트: javacruft/zaza
async def deployed():
    """List deployed applications."""
    # Create a Model instance. We need to connect our Model to a Juju api
    # server before we can use it.
    model = Model()
    # Connect to the currently active Juju model
    await model.connect_current()
    try:
        # list currently deploeyd services
        return list(model.applications.keys())
    finally:
        # Disconnect from the api server and cleanup.
        await model.disconnect()

sync_deployed = sync_wrapper(deployed)


def get_unit_from_name(unit_name, model=None, model_name=None):
    """Return the units that corresponds to the name in the given model.

    :param unit_name: Name of unit to match
    :type unit_name: str
    :param model: Model to perform lookup in
    :type model: model.Model()
    :param model_name: Name of the model to perform lookup in
    :type model_name: string
    :returns: Unit matching given name
    :rtype: juju.unit.Unit or None
    :raises: UnitNotFound
    """
예제 #7
0
    :param units: List of unit names.
    :type units: []
    :param model_name: Name of model to query.
    :type model_name: str
    :rtype: juju.action.Action
    :raises: zaza.model.ActionFailed
    """
    logging.info("Pausing {}".format(', '.join(units)))
    await zaza.model.async_run_action_on_units(units,
                                               'pause',
                                               model_name=model_name,
                                               raise_on_failure=True)


pause_units = sync_wrapper(async_pause_units)


async def async_resume_units(units, model_name=None):
    """Resume all units in unit list.

    Resume all units in unit list. Wait for resume action
    to complete.

    :param units: List of unit names.
    :type units: []
    :param model_name: Name of model to query.
    :type model_name: str
    :rtype: juju.action.Action
    :raises: zaza.model.ActionFailed
    """
예제 #8
0
    :param units: List of unit names.
    :type units: []
    :param model_name: Name of model to query.
    :type model_name: str
    :rtype: juju.action.Action
    :raises: zaza.model.ActionFailed
    """
    logging.info("Pausing {}".format(', '.join(units)))
    await zaza.model.async_run_action_on_units(units,
                                               'pause',
                                               model_name=model_name,
                                               raise_on_failure=True)


pause_units = sync_wrapper(async_pause_units)


async def async_resume_units(units, model_name=None):
    """Resume all units in unit list.

    Resume all units in unit list. Wait for resume action
    to complete.

    :param units: List of unit names.
    :type units: []
    :param model_name: Name of model to query.
    :type model_name: str
    :rtype: juju.action.Action
    :raises: zaza.model.ActionFailed
    """