예제 #1
0
def testCmdValidator():
    # test successful cmd validation
    msgs, v = cmdval([os.path.join(DATA_PATH,  "testValidCmd1.yaml"), cmd.getDefaultSchema()])
    dispmsgs(msgs)
    assert v
    assert len(msgs) == 0

    try:
        msgs, v = cmdval([os.path.join(DATA_PATH,  "testCmdValidator1.yaml"), cmd.getDefaultSchema()])
        assert False
    except util.YAMLError, e:
        assert "Duplicate Command name" in e.message
예제 #2
0
def testValidatorCmd():
    msgs = []

    # test successful validation
    msgs, v = validate([os.path.join(DATA_PATH,  "testValidCmd1.yaml"), cmd.getDefaultSchema()])
    assert v
    assert len(msgs) == 0

    # test failed validation
    msgs, v = validate([os.path.join(DATA_PATH,  "testInvalidCmd1.yaml"), cmd.getDefaultSchema()])

    assert not v
    assert len(msgs) == 1
    assert "Value 'BAD_OPCODE' should be of type 'integer'" in msgs[0]
예제 #3
0
def testCmdDictValidation():
    # Validation test of current command dictionary
    msgs, v = cmdval([ait.config.cmddict.filename, cmd.getDefaultSchema()])
    dispmsgs(msgs)
    assert v
    assert len(msgs) == 0
예제 #4
0
def testCmdValidator():
    # test successful cmd validation
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testValidCmd1.yaml"),
        cmd.getDefaultSchema()
    ])
    dispmsgs(msgs)
    assert v
    assert len(msgs) == 0

    # test failed cmd validation - duplicate name
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator1.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 1
    assert "Duplicate command name" in msgs[0]

    # test failed cmd validation - duplicate opcode
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator2.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 2
    assert "Duplicate opcode" in msgs[1]

    # test failed cmd validation - bad argtype
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator3.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 1
    assert "Invalid argument type" in msgs[0]

    # test failed cmd validation - bad nbytes
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator4.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 2
    assert "Invalid argument size" in msgs[0]

    # test failed cmd validation - bad byte order
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator5.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 1
    assert "Invalid byte order" in msgs[0]

    # test failed cmd validation - bad start byte
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator6.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 1
    assert "Invalid byte order" in msgs[0]

    # test success cmd validation - ensure quoted YAML booleans in enums
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator7.yaml"),
        cmd.getDefaultSchema()
    ])
    dispmsgs(msgs)
    assert v
    assert len(msgs) == 0

    # test failed cmd validation - YAML booleans not quoted
    msgs, v = cmdval([
        os.path.join(DATA_PATH, "testCmdValidator8.yaml"),
        cmd.getDefaultSchema()
    ])
    assert not v
    assert len(msgs) == 2
    assert "Invalid enum value" in msgs[0]
예제 #5
0
def main():
    argparser = argparse.ArgumentParser(
        description="""
Validate YAML files with applicable schema and/or advanced
content validation for CMD and TLM dictionaries.

YAML validation is done through a combination of JSON Schema
(http://json-schema.org/) and Python-coded content validation.  The
JSON Schema is used to validate general format of the YAML, i.e
dictionaries contain the expected keys, values are the expected type,
etc.

Why JSON Schema? All of the available YAML validators did not meet the
robustness expected for this tool. Since JSON and YAML are stored
similarly in memory, the JSON Schema became an option.  The only
difference between YAML and JSON is the use of multiple documents in
the same YAML file. The val.py module handles this implication. See
TBD wiki page for more details on developing JSON schema for an
applicable YAML file.
""",
        epilog="""
Examples:

    $ ait-yaml-validate.py --cmd
    $ ait-yaml-validate.py --tlm
    $ ait-yaml-validate.py --evr
    $ ait-yaml-validate.py --cmd --yaml /path/to/cmd.yaml
    $ ait-yaml-validate.py --tlm --yaml /path/to/tlm.yaml
    $ ait-yaml-validate.py --yaml /path/to/yaml --schema /path/to/schema
""",
        formatter_class=argparse.RawDescriptionHelpFormatter)

    argparser.add_argument('-y',
                           '--yaml',
                           metavar='</path/to/yaml>',
                           type=str,
                           help='Path to YAML file.')

    argparser.add_argument('-s',
                           '--schema',
                           metavar='</path/to/schema>',
                           type=str,
                           help='Path to JSON schema file.')

    argparser.add_argument(
        '-c',
        '--cmd',
        action='store_true',
        default=False,
        help="""Command dictionary flag. If a YAML file is not
        specified, the default command dictionary and schema will be used.
        """)

    argparser.add_argument(
        '-t',
        '--tlm',
        action='store_true',
        default=False,
        help="""Telemetry dictionary flag. If a YAML file is not
        specified, the default telemetry dictionary and schema will be used.
        """)

    argparser.add_argument(
        '-e',
        '--evr',
        action='store_true',
        default=False,
        help="""EVR dictionary flag. If a YAML file is not specified,
        the default EVR dictionary and schema will be used.
        """)

    argparser.add_argument(
        '-l',
        '--limits',
        action='store_true',
        default=False,
        help="""Limits dictionary flag. If a YAML file is not specified,
        the default limits dictionary and schema will be used.
        """)

    if len(sys.argv) < 2:
        argparser.print_usage()
        print 'Run with --help for detailed help.'
        sys.exit(2)

    options = argparser.parse_args()

    log.begin()

    # Validate specified yaml file with specified schema
    if options.yaml is not None and options.schema is not None:
        # Check YAML exists
        if not os.path.exists(options.yaml):
            raise os.error(options.yaml + " does not exist.")

        # Check schema exists
        if not os.path.exists(options.schema):
            raise os.error(options.schema + " does not exist.")

        validator = val.Validator
        retcode = validate(validator, options.yaml, options.schema)

    else:
        if options.cmd:
            yml = ait.config.cmddict.filename
            schema = cmd.getDefaultSchema()
            validator = val.CmdValidator
        elif options.evr:
            yml = ait.config.evrdict.filename
            schema = evr.getDefaultSchema()
            validator = val.Validator
        elif options.tlm:
            yml = ait.config.tlmdict.filename
            schema = tlm.getDefaultSchema()
            validator = val.TlmValidator
        elif options.limits:
            yml = ait.config.limits.filename
            schema = limits.getDefaultSchema()
            validator = val.Validator

        if options.yaml is not None:
            yml = options.yaml

        retcode = validate(validator, yml, schema)

    log.end()
    return retcode
예제 #6
0
def testCmdValidator():
    # test successful cmd validation
    msgs, v = cmdval([os.path.join(DATA_PATH,  "testValidCmd1.yaml"), cmd.getDefaultSchema()])
    dispmsgs(msgs)
    assert v
    assert len(msgs) == 0

    try:
        msgs, v = cmdval([os.path.join(DATA_PATH,  "testCmdValidator1.yaml"), cmd.getDefaultSchema()])
        assert False
    except util.YAMLError, e:
        assert "Duplicate Command name" in e.message

    try:
        msgs, v = cmdval([os.path.join(DATA_PATH,  "testCmdValidator2.yaml"), cmd.getDefaultSchema()])
        assert False
    except util.YAMLError, e:
        assert "Duplicate Command opcode" in e.message

    # test failed cmd validation - bad argtype
    msgs, v = cmdval([os.path.join(DATA_PATH,  "testCmdValidator3.yaml"), cmd.getDefaultSchema()])
    assert not v
    assert len(msgs) == 1
    assert "Invalid argument type" in msgs[0]

    # test failed cmd validation - bad nbytes
    msgs, v = cmdval([os.path.join(DATA_PATH,  "testCmdValidator4.yaml"), cmd.getDefaultSchema()])
    assert not v
    assert len(msgs) == 2
    assert "Invalid argument size" in msgs[0]