Add pharos-validator tool
[pharos.git] / tools / pharos-validator / src / validation_tool / src / test / evaluate.py
1 ##############################################################################
2 # Copyright (c) 2015 Todd Gaunt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11
12 from pharosvalidator.util import approxsize
13
14 # Constant macros
15 from pharosvalidator.const import *
16
17 def cpu(cpudata):
18     """Compares system cpu against the pharos specification"""
19     results = []
20
21     # Architecture evaluation, a value of 63 or greater indicates at least a 64-bit OS
22     if cpudata["bitsize"] >= 63:
23         val = True
24     else:
25         val = False
26     result = {"architecture": {
27                 "pass": val,
28                 "description": str(cpudata["architecture"])}}
29     results.append(result)
30
31     # Core evaluation
32     if cpudata["cores"] < MIN_CORECOUNT:
33         val = False
34     else:
35         val = True
36     desc = "Have {0}, Need at least {1}".format(cpudata["cores"], MIN_CORECOUNT)
37     result = {"corecount": {
38                 "pass": val,
39                 "description": desc}}
40     results.append(result)
41
42     # Speed evaluation
43     i = 0
44     for cpufreq in cpudata["frequency"]:
45         # Cpufrequency was not read if this is the result
46         if cpufreq == -1:
47             desc = "(Cpu freuency could not be read)"
48         else:
49             if approxsize(cpufreq, MIN_CPUFREQ, 5) or cpufreq > MIN_CPUFREQ:
50                 val = True
51             else:
52                 val = False
53             desc = "Have {:.2f}Mhz, Need at least ~= {:.2f}Mhz".format( \
54                         cpufreq, MIN_CPUFREQ)
55         result = {"cpu"+str(i): {
56                     "pass": val,
57                     "description": desc}}
58         results.append(result)
59         i += 1
60
61     return results
62
63 def memory(memdata):
64     """Compares system meminfo object against the pharos specification"""
65     logger = logging.getLogger(__name__)
66
67     results = []
68
69     logger.debug("required memory: {}, detected memory: {}".format(\
70             MIN_MEMSIZE, memdata["size"]))
71     # Capacity evaluation
72     if approxsize(memdata["size"], MIN_MEMSIZE, 5) or memdata["size"] > MIN_MEMSIZE:
73         val = True
74     else:
75         val = False
76
77     desc = "Have {:.2f}G, Need at least ~= {:.2f}G".format( \
78             memdata["size"], MIN_MEMSIZE/1000000)
79
80     result = {"memory capacity": {
81                 "pass": val,
82                 "description": desc}}
83     results.append(result)
84
85     return results
86
87 def storage(diskdata):
88     """Compares system storage against the Pharos specification"""
89     def sizecmp(a, b, unit):
90         if approxsize(a, b, 10) or a > b:
91             val = True
92         else:
93             val = False
94         desc = "capacity is {:.2f}{}, Need at least ~= {:.2f}{}".format(a, \
95                                                             unit,  b, unit)
96         return (val,desc)
97
98     results = []
99     # Disk size evaluation (also counts the disks)
100     diskcount = {"ssd":0, "non-ssd":0}
101     for disk in diskdata["names"]:
102         if diskdata["rotational"][disk]:
103             disktype = "non-ssd"
104             diskcount["non-ssd"] += 1
105         else:
106             disktype = "ssd"
107             diskcount["ssd"] += 1
108         val, desc = sizecmp(diskdata["sizes"][disk], MIN_SSDSIZE, 'G')
109         data = diskdata["sizes"][disk]
110         result = {disk: {
111                     "pass": val,
112                     "description": "Disk type: disktype; " + desc}}
113         results.append(result)
114
115     # Disk number evaluation
116     if sum(diskcount.values()) >= 3 and diskcount["ssd"] >= 1:
117         val = True
118     else:
119         val = False
120     desc = "Have {0} drives, Need at least {1} drives and {3} ssds".format( \
121             sum(diskcount.values()), MIN_DISKCOUNT, \
122             diskcount["ssd"], MIN_SSDCOUNT)
123
124     data = diskcount
125     result = {"diskcount": {
126                 "pass": val,
127                 "description": desc}}
128     results.append(result)
129     return results
130
131 """
132 def netinterfaces(netfaces):
133     results = []
134     for netface in netfaces:
135         if netface.status <= 0:
136             val = False
137             state = "down"
138         else:
139             val = True
140             state = "up"
141         try:
142             MACaddr = netface.MAC[0]["addr"]
143         except IndexError:
144             MACaddr = "no MAC"
145         if len(netface.addrs) > 0:
146             addrs = ""
147             for addr in netface.addrs:
148                 if len(addrs) > 0:
149                     addrs += ", "
150                 addrs += addr['addr']
151             addrs = "addresses: " + addrs
152         else:
153             addrs = "no address"
154         desc = "({0} is {1} with {2})".format(netface.name, state, addrs)
155         data = MACaddr
156         results.append(gen_yamltext(netface.name, val, desc, data))
157     return results
158     """
159