summaryrefslogtreecommitdiffstats
path: root/examples/test.py
blob: b3a7be7b62d1da9bdd79ebeba5344a92d99afcb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
#.awk '$0 ~ /case [0-9]+: .. 3/ { sys.stdout.write($2 }' src/dmidecode.c|tr ':\n' ', '

from pprint import pprint
import os, sys, random, tempfile, time
import commands

DUMPS_D = "private"

def ascii(s, i): return "\033[%d;1m%s\033[0m"%(30+i, str(s))
def black(s): return "\033[30;1m%s\033[0m"%(str(s))
def red(s): return "\033[31;1m%s\033[0m"%(str(s))
def green(s): return "\033[32;1m%s\033[0m"%(str(s))
def yellow(s): return "\033[33;1m%s\033[0m"%(str(s))
def blue(s): return "\033[34;1m%s\033[0m"%(str(s))
def magenta(s): return "\033[35;1m%s\033[0m"%(str(s))
def cyan(s): return "\033[36;1m%s\033[0m"%(str(s))
def white(s): return "\033[37;1m%s\033[0m"%(str(s))

DISPATCH = {
  1 : red,
  2 : green,
  3 : yellow,
  4 : blue,
  5 : magenta,
  6 : cyan,
  7 : white,
}

LINE = "%s\n"%(magenta("="*80))

score = {
  "total"   : 0,
  "skipped" : 0,
  "passed"  : 0,
  "failed"  : 0,
}

def passed(msg=None, indent=1):
  global score
  score["total"] += 1
  score["passed"] += 1
  sys.stdout.write("%s\n"%green("PASS"))
  if msg: sys.stdout.write("%s %s %s\n"%("  "*indent, green("P"), msg))
def skipped(msg=None, indent=1):
  global score
  score["total"] += 1
  score["skipped"] += 1
  sys.stdout.write("%s\n"%yellow("SKIP"))
  if msg: sys.stdout.write("%s %s %s\n"%("  "*indent, yellow("S"), msg))
def failed(msg=None, indent=1):
  global score
  score["total"] += 1
  score["failed"] += 1
  sys.stdout.write("%s\n"%red("FAIL"))
  if msg: sys.stdout.write("%s %s %s\n"%("  "*indent, red("F"), msg))
def test(r, msg=None, indent=1):
  if r:
    passed(msg, indent)
    return True
  else:
    failed(msg, indent)
    return False

sys.stdout.write(LINE)
sys.stdout.write(" * Testing for dmidecode (upstream)...")
d = True in [os.path.exists(os.path.join(_, "dmidecode")) for _ in os.getenv("PATH").split(':')]
test(d)

sys.stdout.write(" * Creation of temporary files...")
try:
  FH, DUMP = tempfile.mkstemp()
  os.unlink(DUMP)
  os.close(FH)
  passed()
except:
  failed()

sys.stdout.write(LINE)
sys.stdout.write(" * Importing module...")
try:
  import dmidecode
  passed()
  sys.stdout.write("   * Version: %s\n"%blue(dmidecode.version))
  sys.stdout.write("   * DMI Version String: %s\n"%blue(dmidecode.dmi))

  sys.stdout.write(" * Testing that default device is /dev/mem...")
  test(dmidecode.get_dev() == "/dev/mem")

  sys.stdout.write(" * Testing that write-lock will not break on dump()...")
  test(not dmidecode.dump())

  sys.stdout.write(" * Testing ability to change device to %s..."%DUMP)
  test(dmidecode.set_dev(DUMP))

  sys.stdout.write(" * Testing that device has changed to %s..."%DUMP)
  test(dmidecode.get_dev() == DUMP)

  sys.stdout.write(" * Testing that write on new file is ok...")
  test(dmidecode.dump())

  sys.stdout.write(" * Testing that file was actually written...")
  time.sleep(0.1)
  if test(os.path.exists(DUMP)):
    os.unlink(DUMP)

  types = range(0, 42)+range(126, 128)
  bad_types = [-1, -1000, 256]
  sections = ["bios", "system", "baseboard", "chassis", "processor", "memory", "cache", "connector", "slot"]
  devices = []
  if os.path.exists(DUMPS_D):
    devices.extend([os.path.join(DUMPS_D, _) for _ in os.listdir(DUMPS_D)])
  else:
    sys.stdout.write(" * If you have memory dumps to test, create a directory called `%s' and drop them in there.\n"%(DUMPS_D))
  devices.append("/dev/mem")
  random.shuffle(types)
  random.shuffle(devices)
  random.shuffle(sections)

  for dev in devices:
    sys.stdout.write(LINE)
    sys.stdout.write(" * Testing %s..."%yellow(dev)); sys.stdout.flush()
    try:
      fH = open(dev, 'r')
      fH.close()
      passed()
      sys.stdout.write("   * Testing set_dev/get_dev on %s..."%(yellow(dev))); sys.stdout.flush()
      if test(dmidecode.set_dev(dev) and dmidecode.get_dev() == dev):
        i = 0
        for section in sections:
          i += 1
          sys.stdout.write("   * Testing %s (%d/%d)..."%(cyan(section), i, len(sections))); sys.stdout.flush()
          try:
            output = getattr(dmidecode, section)()
            test(output is not False)
            if output:
              sys.stdout.write("     * %s\n"%black(output.keys()))
          except LookupError, e:
            failed(e, 2)

        for i in bad_types:
          sys.stdout.write("   * Testing bad type %s..."%red(i)); sys.stdout.flush()
          try:
            output = dmidecode.type(i)
            test(output is False)
          except SystemError:
            failed()

        for i in types:
          sys.stdout.write("   * Testing type %s..."%red(i)); sys.stdout.flush()
          try:
            output = dmidecode.type(i)
            if dmidecode:
              _output = commands.getoutput("dmidecode -t %d"%i).strip().split('\n')
              test(len(_output) == 1 and len(output) == 0 or True)
            else:
              test(output is not False)
            if output:
              sys.stdout.write("     * %s\n"%output.keys())
          except IOError, e:
            failed(e, 2)
    except IOError:
      skipped()

except ImportError:
  failed()

sys.stdout.write(LINE)
sys.stdout.write("Devices : %s\n"%cyan(len(devices)))
sys.stdout.write("Total   : %s\n"%blue(score["total"]))
sys.stdout.write("Skipped : %s\n"%yellow(score["skipped"]))
sys.stdout.write("Passed  : %s\n"%green(score["passed"]))
sys.stdout.write("Failed  : %s\n"%red(score["failed"]))