forked from hyperboria/peers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·64 lines (59 loc) · 2.32 KB
/
tests.py
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
#!/usr/bin/env python3
"""Checks that the peering details in this repo conform to a few basic rules."""
import json
import os
import sys
REQUIRED_FIELDS = ['publicKey', 'password', 'contact', 'peerName']
RECOMMENDED_FIELDS = ['gpg', 'login']
RED = '\x1b[01;31m'
GREEN = '\x1b[01;32m'
YELLOW = '\x1b[01;33m'
END = '\x1b[0m'
def validate(path):
"""Test a single set of peering creds."""
print("Validating %s" % path)
try:
creds = open(path).read()
peers = json.loads(creds)
# Check formatting
pretty = json.dumps(peers, sort_keys=True, indent=4, separators=(',', ':'))
pretty = "%s\n" % pretty
formatting = True
if pretty != creds:
if "--clean" in sys.argv:
with open(path, 'w') as outfile:
outfile.write(pretty)
print(" %sJSON in %s has been fixed.%s" % (YELLOW, path, END))
else:
print(" %sJSON in %s is NOT properly formatted! This is really easy to fix!"
" just run tests.py --clean.%s" % (YELLOW, path, END))
formatting = False
hosts = peers.keys()
for host in hosts:
for field in REQUIRED_FIELDS:
if field not in peers[host]:
print(" %sHost %s is missing the required field %s%s" % (RED, host,
field, END))
return False
for field in RECOMMENDED_FIELDS:
if field not in peers[host]:
print(" %sHost %s is missing the recommended field %s%s" % (YELLOW, host,
field, END))
if formatting:
return True
else:
return False
except ValueError:
print(" %sInvalid JSON!%s" % (RED, END))
return False
if __name__ == "__main__":
success = True
for directory, subdirs, files in os.walk('.'):
if len(files) > 0:
if directory != '.' and not directory.startswith('./.git'):
for f in files:
result = validate("%s/%s" % (directory, f))
if not result:
success = False
if not success:
sys.exit(1)