Browse Source

VM values validator

Change-Id: I512e064257eb22582d9c684d325578670c9600ee
Chris Wedgwood 1 year ago
parent
commit
a2befb278b
3 changed files with 120 additions and 0 deletions
  1. 7
    0
      Makefile
  2. 4
    0
      examples/bogus-vm.yaml
  3. 109
    0
      validate.py

+ 7
- 0
Makefile View File

@@ -1,6 +1,13 @@
1 1
 # This Makefile is used during development and can usually be ignored
2 2
 # by most people.
3 3
 
4
+validation:
5
+	@echo ===========================================================================
6
+	python validate.py example-vmlist-1.yaml
7
+	@echo ===========================================================================
8
+	python validate.py example-vmlist-bad.yaml
9
+	@echo ===========================================================================
10
+
4 11
 all: test
5 12
 
6 13
 default: test

+ 4
- 0
examples/bogus-vm.yaml View File

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

+ 109
- 0
validate.py View File

@@ -0,0 +1,109 @@
1
+#!/usr/bin/python
2
+#
3
+# Ideally we would use jsonschema.validate but getting useful error
4
+# output has been challenging.  This can be revisited as needed.
5
+
6
+import yaml
7
+import sys
8
+
9
+def validate_leaves(prefix, vm, l):
10
+    valid_leaves = [ ]
11
+
12
+    # first check to make sure we have what we think we should
13
+    for nd in l:
14
+        n = nd[0]
15
+        t = nd[1]
16
+        o = False
17
+        if len(nd)>2:
18
+            o = nd[2]
19
+        try:
20
+            if n not in vm.keys():
21
+                if o:
22
+                    print "[W] Missing leaf:", prefix+n
23
+                else:
24
+                    print "[E] Missing leaf:", prefix+n
25
+                continue
26
+            if type(vm[n]) != type(t):
27
+                print "[E] Wrong type for %s (got '%s' expected '%s')" % (prefix+n, type(vm[n]).__name__, type(t).__name__)
28
+                continue
29
+        except:
30
+            print "[W] Unable to validate leaf:", prefix+n
31
+            continue
32
+
33
+        valid_leaves.append(n)
34
+
35
+    if type(vm) == type(dict()):
36
+        # now look for things we don't know how to deal with
37
+        for n in vm.keys():
38
+            if n not in [ x[0]  for x in l]:
39
+                print "[W] Unexpected leaf:", prefix+n
40
+
41
+    return valid_leaves
42
+
43
+def validate_file(filename):
44
+    yamlgen = yaml.load_all(open(filename))
45
+    top = [ x  for x in yamlgen ][0]
46
+
47
+    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"]
54
+
55
+        print "Checking:", name, ("(index %d)" % index)
56
+
57
+        vl = validate_leaves("", vm, [ ("name",str()), ("enabled",bool()), ("vmconfig",dict()), ("netconfig",dict()), ("cloudconfig",dict()) ] )
58
+        if "vmconfig" in vl:
59
+            # validate vmconfig
60
+            vmconfig = vm["vmconfig"]
61
+            vl2 = validate_leaves("vmconfig.", vmconfig, [ ("cpu",dict()), ("rootfs",dict()) ])
62
+
63
+            if "cpu" in vl2:
64
+                # validate vmconfig.cpu
65
+                cpu = vmconfig["cpu"]
66
+                vl3 = validate_leaves("vmconfig.cpu.", cpu, [ ("vcpu",int()), ("ram_mb",int()) ])
67
+                if "vcpus" in vl3:
68
+                    vcpu = int(cpu["vcpus"])
69
+                    if vcpu < 1  or  vcpu > 8:
70
+                        print "[W] vmconfig.cpu.vcpu has odd looking value:", vcpu
71
+                if "ram_mb" in vl3:
72
+                    ram_mb = int(cpu["ram_mb"])
73
+                    if ram_mb < 512   or   ram_mb > 32768:
74
+                        print "[W] vmconfig.cpu.ram_mb has odd looking value:", ram_mb
75
+
76
+            if "rootfs" in vl2:
77
+                # validate vmconfig.rootfs
78
+                rootfs = vmconfig["rootfs"]
79
+                vl3 = validate_leaves("vmconfig.rootfs.", rootfs, [ ("sourceurl",str()), ("type_notimpl",str()), ("localtarget",str()), ("pvc_size",str()), ("pvc_class",str(), True) ])
80
+
81
+                if "sourceurl" in vl3:
82
+                    if not rootfs["sourceurl"].startswith("http"):
83
+                        print "[W] vmconfig.rootfs.sourceurl has odd looking value:", rootfs["sourceurl"]
84
+
85
+        if "netconfig" in vl:
86
+            # validate netconfig
87
+            netconfig = vm["netconfig"]
88
+            vm2 = validate_leaves("netconfig.", netconfig, [ ("ports",dict()), ("readinessTcpProbe",int(), True) ])
89
+            # do more?
90
+
91
+        if "cloudconfig" in vl:
92
+            # validate cloudconfig
93
+            cloudconfig = vm["cloudconfig"]
94
+            vl2 = validate_leaves("cloudconfig.", cloudconfig, [ ("instance_id",str()), ("metadata",str()), ("userdata",str()) ])
95
+
96
+            # check things look sane
97
+            for yamlobj in [ "metadata", "userdata" ]:
98
+                if yamlobj in vl2:
99
+                    try:
100
+                        yaml.load(cloudconfig[yamlobj])
101
+                    except:
102
+                        print "[E] Bad yaml for vmconfig.cloudconfig.%s" % yamlobj
103
+
104
+        print "Done"
105
+        print
106
+
107
+if __name__ == "__main__":
108
+    for fn in sys.argv[1:]:
109
+        validate_file(fn)

Loading…
Cancel
Save