summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wedgwood <cw@f00f.org>2017-10-02 20:02:15 +0000
committerChris Wedgwood <cw@f00f.org>2017-10-30 21:25:33 -0400
commitf31a1d2a94d0adcfa8a88c65f35e65f5ec8d8236 (patch)
treee028e2d6b1b2bf770e764b0e2cd09baa1f77db4f
parenta2befb278b5990c268564a676446568ab4548455 (diff)
Validator cleanups & more test cases for updated schema
-rw-r--r--Makefile4
-rw-r--r--examples/fail-bogus.yaml (renamed from examples/bogus-vm.yaml)2
-rw-r--r--examples/fail-nothing.yaml1
-rw-r--r--examples/fail-parse.yaml3
-rw-r--r--validate.py35
5 files changed, 27 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index 7d4fde5..75ef1d5 100644
--- a/Makefile
+++ b/Makefile
@@ -3,9 +3,7 @@
3 3
4validation: 4validation:
5 @echo =========================================================================== 5 @echo ===========================================================================
6 python validate.py example-vmlist-1.yaml 6 python validate.py examples/*
7 @echo ===========================================================================
8 python validate.py example-vmlist-bad.yaml
9 @echo =========================================================================== 7 @echo ===========================================================================
10 8
11all: test 9all: test
diff --git a/examples/bogus-vm.yaml b/examples/fail-bogus.yaml
index af45c64..4495608 100644
--- a/examples/bogus-vm.yaml
+++ b/examples/fail-bogus.yaml
@@ -9,6 +9,6 @@ vmlist:
9 metadata: "{ " 9 metadata: "{ "
10 userdata: false 10 userdata: false
11# bogus mess 11# bogus mess
12 - noname: "idonthaveaname" 12 somevm:
13 enabled: "can't be bothered" 13 enabled: "can't be bothered"
14 extra: "peanuts" 14 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 @@
1vmlist:
2 - wrong
3
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):
41 return valid_leaves 41 return valid_leaves
42 42
43def validate_file(filename): 43def validate_file(filename):
44 yamlgen = yaml.load_all(open(filename)) 44 try:
45 top = [ x for x in yamlgen ][0] 45 yamlgen = list(yaml.safe_load_all(open(filename)))
46 except yaml.parser.ParserError:
47 print "[E] Invalid yaml"
48 return
49
50 if not yamlgen or not yamlgen[0]:
51 print "[E] File contains no valid yaml"
52 return
53 top = list(yamlgen)[0]
46 54
47 vmlist = top["vmlist"] 55 vmlist = top["vmlist"]
48 index = 0 56 if not vmlist or not isinstance(vmlist, dict):
49 for vm in vmlist: 57 print "[E] No vmlist dict declared"
50 index += 1 58 return
51 name = ""
52 if "name" in vm:
53 name = vm["name"]
54 59
55 print "Checking:", name, ("(index %d)" % index) 60 for name in vmlist:
61 vm = vmlist[name]
62 print "VM:", name
56 63
57 vl = validate_leaves("", vm, [ ("name",str()), ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] ) 64 vl = validate_leaves("", vm, [ ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] )
58 if "vmconfig" in vl: 65 if "vmconfig" in vl:
59 # validate vmconfig 66 # validate vmconfig
60 vmconfig = vm["vmconfig"] 67 vmconfig = vm["vmconfig"]
@@ -76,7 +83,7 @@ def validate_file(filename):
76 if "rootfs" in vl2: 83 if "rootfs" in vl2:
77 # validate vmconfig.rootfs 84 # validate vmconfig.rootfs
78 rootfs = vmconfig["rootfs"] 85 rootfs = vmconfig["rootfs"]
79 vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("type_notimpl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ]) 86 vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ])
80 87
81 if "sourceurl" in vl3: 88 if "sourceurl" in vl3:
82 if not rootfs["sourceurl"].startswith("http"): 89 if not rootfs["sourceurl"].startswith("http"):
@@ -91,7 +98,7 @@ def validate_file(filename):
91 if "cloudconfig" in vl: 98 if "cloudconfig" in vl:
92 # validate cloudconfig 99 # validate cloudconfig
93 cloudconfig = vm["cloudconfig"] 100 cloudconfig = vm["cloudconfig"]
94 vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("instance_id",str()), ("metadata",str()), ("userdata",str()) ]) 101 vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("metadata",str()), ("userdata",str()) ])
95 102
96 # check things look sane 103 # check things look sane
97 for yamlobj in [ "metadata", "userdata" ]: 104 for yamlobj in [ "metadata", "userdata" ]:
@@ -101,9 +108,9 @@ def validate_file(filename):
101 except: 108 except:
102 print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj 109 print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj
103 110
104 print "Done"
105 print
106 111
107if __name__ == "__main__": 112if __name__ == "__main__":
108 for fn in sys.argv[1:]: 113 for fn in sys.argv[1:]:
114 print "Filename:", fn
109 validate_file(fn) 115 validate_file(fn)
116 print