Exemplo n.º 1
0
    def test_defaults(self):
        self.out = self.useFixture(fixtures.StringStream('stdout'))
        self.useFixture(
            fixtures.MonkeyPatch('sys.stdout', self.out.stream))

        self.err = self.useFixture(fixtures.StringStream('stderr'))
        self.useFixture(
            fixtures.MonkeyPatch('sys.stderr', self.err.stream))

        self.useFixture(fixtures.EnvironmentVariable('DIB_DEBUG_TRACE', '1'))

        logging_config.setup()
        log = logging.getLogger(__name__)
        log.debug("Debug Message")
        self.assertIn("Debug Message", self.err._details["stderr"].as_text())
        # The follow two are looking for the function name / file name
        # suffix we log only for debug messages
        self.assertIn("test_defaults", self.err._details["stderr"].as_text())
        self.assertIn("test_loggingconfig.py",
                      self.err._details["stderr"].as_text())
        log.info("Info Message")
        self.assertIn("Info Message", self.err._details["stderr"].as_text())
        log.warning("Warning Message")
        self.assertIn("Warning Message", self.err._details["stderr"].as_text())
        log.error("Error Message")
        self.assertIn("Error Message", self.err._details["stderr"].as_text())
Exemplo n.º 2
0
def main():
    logging_config.setup()
    logger = logging.getLogger(__name__)
    phase_doc = generate_phase_doc()

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="Create block device layer",
        epilog="Available phases:\n" + phase_doc)
    parser.add_argument('--phase', required=True, help="phase to execute")
    parser.add_argument('--params',
                        required=True,
                        help="parameters for block device handling")
    parser.add_argument('--symbol',
                        required=False,
                        help="symbol to query for getval")
    args = parser.parse_args()

    logger.info("phase [%s]" % args.phase)
    logger.info("params [%s]" % args.params)
    if args.symbol:
        logger.info("symbol [%s]" % args.symbol)

    bd = BlockDevice(args)

    # Check if the method is available
    method = getattr(bd, "cmd_" + args.phase, None)
    if callable(method):
        # If so: call it.
        return method()
    else:
        logger.error("phase [%s] does not exists" % args.phase)
        return 1

    return 0
Exemplo n.º 3
0
def main():
    logging_config.setup()
    logger = logging.getLogger(__name__)
    phase_doc = generate_phase_doc()

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="Create block device layer",
        epilog="Available phases:\n" + phase_doc)
    parser.add_argument('--phase', required=True, help="phase to execute")
    parser.add_argument('--config',
                        required=False,
                        help="configuration for block device "
                        "layer as JSON object")
    parser.add_argument('--build-dir',
                        required=True,
                        help="path to temporary build dir")
    parser.add_argument('--image-size',
                        required=False,
                        help="default image size")
    parser.add_argument('--image-dir',
                        required=False,
                        help="default image directory")
    args = parser.parse_args()

    logger.info("phase [%s]" % args.phase)
    logger.info("config [%s]" % args.config)
    logger.info("build_dir [%s]" % args.build_dir)

    bd = BlockDevice(val_else_none(args.config), val_else_none(args.build_dir),
                     val_else_none(args.image_size),
                     val_else_none(args.image_dir))

    # Check if the method is available
    method = getattr(bd, "cmd_" + args.phase, None)
    if callable(method):
        # If so: call it.
        return method()
    else:
        logger.error("phase [%s] does not exists" % args.phase)
        return 1

    return 0
Exemplo n.º 4
0
    def test_defaults(self):
        self.out = self.useFixture(fixtures.StringStream('stdout'))
        self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.out.stream))

        self.err = self.useFixture(fixtures.StringStream('stderr'))
        self.useFixture(fixtures.MonkeyPatch('sys.stderr', self.err.stream))

        self.useFixture(fixtures.EnvironmentVariable('DIB_DEBUG_TRACE', '1'))

        logging_config.setup()
        log = logging.getLogger(__name__)
        log.debug("Debug Message")
        self.assertIn("Debug Message", self.err._details["stderr"].as_text())
        # The follow two are looking for the function name / file name
        # suffix we log only for debug messages
        self.assertIn("test_defaults", self.err._details["stderr"].as_text())
        self.assertIn("test_loggingconfig.py",
                      self.err._details["stderr"].as_text())
        log.info("Info Message")
        self.assertIn("Info Message", self.err._details["stderr"].as_text())
        log.warning("Warning Message")
        self.assertIn("Warning Message", self.err._details["stderr"].as_text())
        log.error("Error Message")
        self.assertIn("Error Message", self.err._details["stderr"].as_text())
 def setUp(self):
     super(TestMBR, self).setUp()
     setup()
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import fixtures
import logging
import testtools

from diskimage_builder import block_device
from diskimage_builder.block_device.level0 import localloop
from diskimage_builder.logging_config import setup
from diskimage_builder import utils as dib_utils

# Setup Logging
setup()


class StateSavingBlockDevice(block_device.BlockDevice):
    def cmd_create(self):
        logging.info("StateSavingBlockDevice cmd_create()")
        super(StateSavingBlockDevice, self).cmd_create()
        _, _, self.state = self.load_state()


class BlockDeviceFixture(fixtures.Fixture):
    def __init__(self, *args, **kwargs):
        logging.info("BlockDeviceFixture constructor")
        self.args = args
        self.kwargs = kwargs
        self.bd = None
Exemplo n.º 7
0
    def main(self):
        logging_config.setup()

        parser = argparse.ArgumentParser(description="DIB Block Device helper")
        parser.add_argument('--params', required=False,
                            help="YAML file containing parameters for "
                            "block-device handling.  Default is "
                            "DIB_BLOCK_DEVICE_PARAMS_YAML")

        subparsers = parser.add_subparsers(title='commands',
                                           description='valid commands',
                                           dest='command',
                                           help='additional help')

        cmd_init = subparsers.add_parser('init',
                                         help='Initialize configuration')
        cmd_init.set_defaults(func=self.cmd_init)

        cmd_getval = subparsers.add_parser('getval',
                                           help='Retrieve information about '
                                           'internal state')
        cmd_getval.set_defaults(func=self.cmd_getval)
        cmd_getval.add_argument('symbol', help='symbol to print')

        cmd_create = subparsers.add_parser('create',
                                           help='Create the block device')
        cmd_create.set_defaults(func=self.cmd_create)

        cmd_umount = subparsers.add_parser('umount',
                                           help='Unmount blockdevice and '
                                           'cleanup resources')
        cmd_umount.set_defaults(func=self.cmd_umount)

        cmd_cleanup = subparsers.add_parser('cleanup', help='Final cleanup')
        cmd_cleanup.set_defaults(func=self.cmd_cleanup)

        cmd_delete = subparsers.add_parser('delete', help='Error cleanup')
        cmd_delete.set_defaults(func=self.cmd_delete)

        cmd_writefstab = subparsers.add_parser('writefstab',
                                               help='Create fstab for system')
        cmd_writefstab.set_defaults(func=self.cmd_writefstab)

        self.args = parser.parse_args()

        # Find, open and parse the parameters file
        if not self.args.params:
            if 'DIB_BLOCK_DEVICE_PARAMS_YAML' in os.environ:
                param_file = os.environ['DIB_BLOCK_DEVICE_PARAMS_YAML']
            else:
                parser.error(
                    "DIB_BLOCK_DEVICE_PARAMS_YAML or --params not set")
        else:
            param_file = self.args.params
            logger.info("params [%s]", param_file)
        try:
            with open(param_file) as f:
                self.params = yaml.safe_load(f)
        except Exception:
            logger.exception("Failed to open parameter YAML")
            sys.exit(1)

        # Setup main BlockDevice object from args
        self.bd = BlockDevice(self.params)

        self.args.func()
Exemplo n.º 8
0
    def main(self):
        logging_config.setup()

        parser = argparse.ArgumentParser(description="DIB Block Device helper")
        parser.add_argument('--params',
                            required=False,
                            help="YAML file containing parameters for "
                            "block-device handling.  Default is "
                            "DIB_BLOCK_DEVICE_PARAMS_YAML")

        subparsers = parser.add_subparsers(title='commands',
                                           description='valid commands',
                                           dest='command',
                                           help='additional help')

        cmd_init = subparsers.add_parser('init',
                                         help='Initialize configuration')
        cmd_init.set_defaults(func=self.cmd_init)

        cmd_getval = subparsers.add_parser('getval',
                                           help='Retrieve information about '
                                           'internal state')
        cmd_getval.set_defaults(func=self.cmd_getval)
        cmd_getval.add_argument('symbol', help='symbol to print')

        cmd_create = subparsers.add_parser('create',
                                           help='Create the block device')
        cmd_create.set_defaults(func=self.cmd_create)

        cmd_umount = subparsers.add_parser('umount',
                                           help='Unmount blockdevice and '
                                           'cleanup resources')
        cmd_umount.set_defaults(func=self.cmd_umount)

        cmd_cleanup = subparsers.add_parser('cleanup', help='Final cleanup')
        cmd_cleanup.set_defaults(func=self.cmd_cleanup)

        cmd_delete = subparsers.add_parser('delete', help='Error cleanup')
        cmd_delete.set_defaults(func=self.cmd_delete)

        cmd_writefstab = subparsers.add_parser('writefstab',
                                               help='Create fstab for system')
        cmd_writefstab.set_defaults(func=self.cmd_writefstab)

        self.args = parser.parse_args()

        # Find, open and parse the parameters file
        if not self.args.params:
            if 'DIB_BLOCK_DEVICE_PARAMS_YAML' in os.environ:
                param_file = os.environ['DIB_BLOCK_DEVICE_PARAMS_YAML']
            else:
                parser.error(
                    "DIB_BLOCK_DEVICE_PARAMS_YAML or --params not set")
        else:
            param_file = self.args.params
            logger.info("params [%s]", param_file)
        try:
            with open(param_file) as f:
                self.params = yaml.safe_load(f)
        except Exception:
            logger.exception("Failed to open parameter YAML")
            sys.exit(1)

        # Setup main BlockDevice object from args
        self.bd = BlockDevice(self.params)

        self.args.func()