diff --git a/Makefile b/Makefile index 7d4fde5..75ef1d5 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,7 @@ validation: @echo =========================================================================== - python validate.py example-vmlist-1.yaml - @echo =========================================================================== - python validate.py example-vmlist-bad.yaml + python validate.py examples/* @echo =========================================================================== all: test diff --git a/examples/bogus-vm.yaml b/examples/fail-bogus.yaml similarity index 91% rename from examples/bogus-vm.yaml rename to examples/fail-bogus.yaml index af45c64..4495608 100644 --- a/examples/bogus-vm.yaml +++ b/examples/fail-bogus.yaml @@ -9,6 +9,6 @@ vmlist: metadata: "{ " userdata: false # bogus mess - - noname: "idonthaveaname" + somevm: enabled: "can't be bothered" extra: "peanuts" diff --git a/examples/fail-nothing.yaml b/examples/fail-nothing.yaml new file mode 100644 index 0000000..7300fbc --- /dev/null +++ b/examples/fail-nothing.yaml @@ -0,0 +1 @@ +# nada diff --git a/examples/fail-parse.yaml b/examples/fail-parse.yaml new file mode 100644 index 0000000..df10564 --- /dev/null +++ b/examples/fail-parse.yaml @@ -0,0 +1,3 @@ +vmlist: + - wrong + diff --git a/validate.py b/validate.py index 61b6c50..12f5000 100644 --- a/validate.py +++ b/validate.py @@ -41,20 +41,27 @@ def validate_leaves(prefix, vm, l): return valid_leaves def validate_file(filename): - yamlgen = yaml.load_all(open(filename)) - top = [ x for x in yamlgen ][0] + try: + yamlgen = list(yaml.safe_load_all(open(filename))) + except yaml.parser.ParserError: + print "[E] Invalid yaml" + return + + if not yamlgen or not yamlgen[0]: + print "[E] File contains no valid yaml" + return + top = list(yamlgen)[0] vmlist = top["vmlist"] - index = 0 - for vm in vmlist: - index += 1 - name = "" - if "name" in vm: - name = vm["name"] + if not vmlist or not isinstance(vmlist, dict): + print "[E] No vmlist dict declared" + return - print "Checking:", name, ("(index %d)" % index) + for name in vmlist: + vm = vmlist[name] + print "VM:", name - vl = validate_leaves("", vm, [ ("name",str()), ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] ) + vl = validate_leaves("", vm, [ ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] ) if "vmconfig" in vl: # validate vmconfig vmconfig = vm["vmconfig"] @@ -76,7 +83,7 @@ def validate_file(filename): if "rootfs" in vl2: # validate vmconfig.rootfs rootfs = vmconfig["rootfs"] - vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("type_notimpl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ]) + vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ]) if "sourceurl" in vl3: if not rootfs["sourceurl"].startswith("http"): @@ -91,7 +98,7 @@ def validate_file(filename): if "cloudconfig" in vl: # validate cloudconfig cloudconfig = vm["cloudconfig"] - vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("instance_id",str()), ("metadata",str()), ("userdata",str()) ]) + vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("metadata",str()), ("userdata",str()) ]) # check things look sane for yamlobj in [ "metadata", "userdata" ]: @@ -101,9 +108,9 @@ def validate_file(filename): except: print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj - print "Done" - print if __name__ == "__main__": for fn in sys.argv[1:]: + print "Filename:", fn validate_file(fn) + print