| 1 |
|
| 2 |
|
| 3 |
import unittest |
| 4 |
from cpuinfo import * |
| 5 |
import helpers |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class TestCLI(unittest.TestCase): |
| 10 |
def setUp(self): |
| 11 |
helpers.backup_data_source(cpuinfo) |
| 12 |
|
| 13 |
def tearDown(self): |
| 14 |
helpers.restore_data_source(cpuinfo) |
| 15 |
|
| 16 |
def test_json(self): |
| 17 |
from subprocess import Popen, PIPE |
| 18 |
import json |
| 19 |
|
| 20 |
command = [sys.executable, 'cpuinfo/cpuinfo.py', '--json'] |
| 21 |
p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
| 22 |
output = p1.communicate()[0] |
| 23 |
|
| 24 |
self.assertEqual(0, p1.returncode) |
| 25 |
|
| 26 |
output = output.decode(encoding='UTF-8') |
| 27 |
|
| 28 |
info = json.loads(output, object_hook = cpuinfo._utf_to_str) |
| 29 |
|
| 30 |
self.assertEqual(list(cpuinfo.CPUINFO_VERSION), info['cpuinfo_version']) |
| 31 |
self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, info['cpuinfo_version_string']) |
| 32 |
|
| 33 |
def test_version(self): |
| 34 |
from subprocess import Popen, PIPE |
| 35 |
|
| 36 |
command = [sys.executable, 'cpuinfo/cpuinfo.py', '--version'] |
| 37 |
p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
| 38 |
output = p1.communicate()[0] |
| 39 |
|
| 40 |
self.assertEqual(0, p1.returncode) |
| 41 |
|
| 42 |
output = output.decode(encoding='UTF-8') |
| 43 |
output = output.strip() |
| 44 |
|
| 45 |
self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, output) |
| 46 |
|
| 47 |
def test_trace(self): |
| 48 |
import os |
| 49 |
import re |
| 50 |
from subprocess import Popen, PIPE |
| 51 |
|
| 52 |
# Get all log files before test |
| 53 |
before_log_files = [f for f in os.listdir('.') if os.path.isfile(f) and re.match(r'^cpuinfo_trace_\d+-\d+-\d+_\d+-\d+-\d+-\d+.trace$', f)] |
| 54 |
#print('\n', before_log_files) |
| 55 |
|
| 56 |
# Run with trace to generate new log file |
| 57 |
command = [sys.executable, 'cpuinfo/cpuinfo.py', '--trace'] |
| 58 |
p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
| 59 |
output = p1.communicate()[0] |
| 60 |
self.assertEqual(0, p1.returncode) |
| 61 |
|
| 62 |
# Get all log files after test |
| 63 |
after_log_files = [f for f in os.listdir('.') if os.path.isfile(f) and re.match(r'^cpuinfo_trace_\d+-\d+-\d+_\d+-\d+-\d+-\d+.trace$', f)] |
| 64 |
#print('\n', after_log_files) |
| 65 |
|
| 66 |
# Read the new log file into a string |
| 67 |
new_log_file = list(set(after_log_files) - set(before_log_files))[0] |
| 68 |
with open(new_log_file, 'r') as f: |
| 69 |
output = f.read().strip() |
| 70 |
|
| 71 |
# Remove the new log file |
| 72 |
os.remove(new_log_file) |
| 73 |
|
| 74 |
self.assertTrue(len(output) > 200) |
| 75 |
self.assertTrue(output.startswith('!' * 80)) |
| 76 |
self.assertTrue(output.endswith('!' * 80)) |
| 77 |
|
| 78 |
def test_default(self): |
| 79 |
from subprocess import Popen, PIPE |
| 80 |
|
| 81 |
command = [sys.executable, 'cpuinfo/cpuinfo.py'] |
| 82 |
p1 = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
| 83 |
output = p1.communicate()[0] |
| 84 |
|
| 85 |
self.assertEqual(0, p1.returncode) |
| 86 |
|
| 87 |
output = output.decode(encoding='UTF-8') |
| 88 |
|
| 89 |
version = output.split('Cpuinfo Version: ')[1].split('\n')[0].strip() |
| 90 |
|
| 91 |
self.assertEqual(cpuinfo.CPUINFO_VERSION_STRING, version) |