예제 #1
0
파일: cli.py 프로젝트: bac/juju-bundlelib
def get_changeset(args):
    """Dump the changeset objects as JSON, reading the provided bundle YAML.

    The YAML can be provided either from stdin or by passing a file path as
    first argument.
    """
    # Parse the arguments.
    parser = argparse.ArgumentParser(description=get_changeset.__doc__)
    parser.add_argument(
        'infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
        help='path to the bundle YAML file')
    parser.add_argument(
        '--version', action='version', version='%(prog)s {}'.format(version))
    options = parser.parse_args(args)

    # Parse the provided YAML file.
    try:
        bundle = yaml.safe_load(options.infile)
    except Exception:
        return 'error: the provided bundle is not a valid YAML'

    # Validate the bundle object.
    errors = validation.validate(bundle)
    if errors:
        return '\n'.join(errors)

    # Dump the changeset to stdout.
    print('[')
    for num, change in enumerate(changeset.parse(bundle)):
        if num:
            print(',')
        print(json.dumps(change))
    print(']')
 def test_bundle(self):
     # All the charm store bundles pass validation.
     # It is possible to get the change set corresponding to each bundle.
     # This test ensures there are no false positives when validating a
     # bundle: charm store bundles are assumed to be valid.
     # Note that this test requires network connection.
     for ref in self.references:
         try:
             bundle = get_bundle(ref)
         except HTTPError as err:
             print('skipping {}: {}'.format(ref, err))
             continue
         # Check bundle validation.
         errors = validation.validate(bundle)
         self.assertEqual(
             [], errors,
             'ref: {}\n{}\nerrors: {}'.format(
                 ref, pprint.pformat(bundle), errors))
         # Check change set generation.
         try:
             changes = list(changeset.parse(bundle))
         except:
             msg = 'changeset parsing error\nref: {}\n{}\n{}'.format(
                 ref, pprint.pformat(bundle), traceback.format_exc())
             self.fail(msg)
         self.assertTrue(changes)
예제 #3
0
def _validate_and_parse_bundle(content):
    """Validate and parse the given bundle YAML encoded content.

    If the content is valid, return the resulting change set and an empty list
    of errors. Otherwise, return an empty list of changes and a list of errors.
    """
    try:
        bundle = yaml.safe_load(content)
    except Exception:
        error = 'the provided bundle is not a valid YAML'
        return [], [error]
    errors = validation.validate(bundle)
    if errors:
        return [], errors
    return list(changeset.parse(bundle)), []
예제 #4
0
def _validate_and_parse_bundle(content):
    """Validate and parse the given bundle YAML encoded content.

    If the content is valid, return the resulting change set and an empty list
    of errors. Otherwise, return an empty list of changes and a list of errors.
    """
    try:
        bundle = yaml.safe_load(content)
    except Exception:
        error = 'the provided bundle is not a valid YAML'
        return [], [error]
    errors = validation.validate(bundle)
    if errors:
        return [], errors
    return list(changeset.parse(bundle)), []
예제 #5
0
 def test_parse(self):
     bundle = {
         'services': {},
         'machines': {},
         'relations': {},
         'series': 'trusty',
     }
     changes = list(changeset.parse(bundle, handler=self.handler1))
     self.assertEqual(
         [
             (1, 0),
             (1, 1),
             (1, 2),
             (2, 0),
             (2, 1),
             (2, 2),
         ],
         changes,
     )
예제 #6
0
 def test_parse(self):
     bundle = {
         'services': {},
         'machines': {},
         'relations': {},
         'series': 'trusty',
     }
     changes = list(changeset.parse(bundle, handler=self.handler1))
     self.assertEqual(
         [
             (1, 0),
             (1, 1),
             (1, 2),
             (2, 0),
             (2, 1),
             (2, 2),
         ],
         changes,
     )
예제 #7
0
 def test_bundle(self):
     # All the charm store bundles pass validation.
     # It is possible to get the change set corresponding to each bundle.
     # This test ensures there are no false positives when validating a
     # bundle: charm store bundles are assumed to be valid.
     # Note that this test requires network connection.
     for ref in self.references:
         try:
             bundle = get_bundle(ref)
         except HTTPError as err:
             print('skipping {}: {}'.format(ref, err))
             continue
         # Check bundle validation.
         errors = validation.validate(bundle)
         self.assertEqual([], errors, 'ref: {}\n{}\nerrors: {}'.format(
             ref, pprint.pformat(bundle), errors))
         # Check change set generation.
         try:
             changes = list(changeset.parse(bundle))
         except:
             msg = 'changeset parsing error\nref: {}\n{}\n{}'.format(
                 ref, pprint.pformat(bundle), traceback.format_exc())
             self.fail(msg)
         self.assertTrue(changes)
예제 #8
0
def get_changeset(args):
    """Dump the changeset objects as JSON, reading the provided bundle YAML.

    The YAML can be provided either from stdin or by passing a file path as
    first argument.
    """
    # Parse the arguments.
    parser = argparse.ArgumentParser(description=get_changeset.__doc__)
    parser.add_argument('infile',
                        nargs='?',
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help='path to the bundle YAML file')
    parser.add_argument('--version',
                        action='version',
                        version='%(prog)s {}'.format(version))
    options = parser.parse_args(args)

    # Parse the provided YAML file.
    try:
        bundle = yaml.safe_load(options.infile)
    except Exception:
        return 'error: the provided bundle is not a valid YAML'

    # Validate the bundle object.
    errors = validation.validate(bundle)
    if errors:
        return '\n'.join(errors)

    # Dump the changeset to stdout.
    print('[')
    for num, change in enumerate(changeset.parse(bundle)):
        if num:
            print(',')
        print(json.dumps(change))
    print(']')
예제 #9
0
 def test_parse_nothing(self):
     bundle = {'services': {}}
     self.assertEqual([], list(changeset.parse(bundle)))
예제 #10
0
 def test_parse_nothing(self):
     bundle = {'services': {}}
     self.assertEqual([], list(changeset.parse(bundle)))