Browse Source

Validator cleanups & more test cases for updated schema

Change-Id: If4b58dfb6aa1f54235bc9096d80a0513033ff39c
Chris Wedgwood 1 year ago
parent
commit
f31a1d2a94
5 changed files with 27 additions and 18 deletions
  1. 1
    3
      Makefile
  2. 1
    1
      examples/fail-bogus.yaml
  3. 1
    0
      examples/fail-nothing.yaml
  4. 3
    0
      examples/fail-parse.yaml
  5. 21
    14
      validate.py

+ 1
- 3
Makefile View File

@@ -3,9 +3,7 @@
3 3
 
4 4
 validation:
5 5
 	@echo ===========================================================================
6
-	python validate.py example-vmlist-1.yaml
7
-	@echo ===========================================================================
8
-	python validate.py example-vmlist-bad.yaml
6
+	python validate.py examples/*
9 7
 	@echo ===========================================================================
10 8
 
11 9
 all: test

examples/bogus-vm.yaml → examples/fail-bogus.yaml View File

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

+ 1
- 0
examples/fail-nothing.yaml View File

@@ -0,0 +1 @@
1
+# nada

+ 3
- 0
examples/fail-parse.yaml View File

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

+ 21
- 14
validate.py View File

@@ -41,20 +41,27 @@ def validate_leaves(prefix, vm, l):
41 41
     return valid_leaves
42 42
 
43 43
 def validate_file(filename):
44
-    yamlgen = yaml.load_all(open(filename))
45
-    top = [ x  for x in yamlgen ][0]
44
+    try:
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 55
     vmlist = top["vmlist"]
48
-    index = 0
49
-    for vm in vmlist:
50
-        index += 1
51
-        name = ""
52
-        if "name" in vm:
53
-            name = vm["name"]
56
+    if not vmlist  or  not isinstance(vmlist, dict):
57
+        print "[E] No vmlist dict declared"
58
+        return
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 65
         if "vmconfig" in vl:
59 66
             # validate vmconfig
60 67
             vmconfig = vm["vmconfig"]
@@ -76,7 +83,7 @@ def validate_file(filename):
76 83
             if "rootfs" in vl2:
77 84
                 # validate vmconfig.rootfs
78 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 88
                 if "sourceurl" in vl3:
82 89
                     if not rootfs["sourceurl"].startswith("http"):
@@ -91,7 +98,7 @@ def validate_file(filename):
91 98
         if "cloudconfig" in vl:
92 99
             # validate cloudconfig
93 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 103
             # check things look sane
97 104
             for yamlobj in [ "metadata", "userdata" ]:
@@ -101,9 +108,9 @@ def validate_file(filename):
101 108
                     except:
102 109
                         print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj
103 110
 
104
-        print "Done"
105
-        print
106 111
 
107 112
 if __name__ == "__main__":
108 113
     for fn in sys.argv[1:]:
114
+        print "Filename:", fn
109 115
         validate_file(fn)
116
+        print

Loading…
Cancel
Save