Validator cleanups & more test cases for updated schema

Change-Id: If4b58dfb6aa1f54235bc9096d80a0513033ff39c
changes/10/569210/1
Chris Wedgwood 5 years ago
parent a2befb278b
commit f31a1d2a94

@ -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

@ -9,6 +9,6 @@ vmlist:
metadata: "{ "
userdata: false
# bogus mess
- noname: "idonthaveaname"
somevm:
enabled: "can't be bothered"
extra: "peanuts"

@ -0,0 +1,3 @@
vmlist:
- wrong

@ -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

Loading…
Cancel
Save