|
1 | | -#! python2 |
| 1 | +#! python2.7 |
2 | 2 | # Copyright (c) 2015, Intel Corporation |
3 | 3 | # All rights reserved. |
4 | 4 | # |
|
31 | 31 | import os |
32 | 32 | import subprocess |
33 | 33 | import difflib |
| 34 | +import unittest |
| 35 | +import copy |
| 36 | + |
| 37 | +class PfConfig: |
| 38 | + def __init__(self, toplevel, criteria, schemas): |
| 39 | + self.toplevel = toplevel |
| 40 | + self.criteria = criteria |
| 41 | + self.schemas = schemas |
| 42 | + |
| 43 | +class TestVector: |
| 44 | + def __init__(self, initialSettings=None, edds=[], domains=[]): |
| 45 | + self.initialSettings = initialSettings |
| 46 | + self.edds = edds |
| 47 | + self.domains = domains |
| 48 | + |
| 49 | +class Tester(object): |
| 50 | + def __init__(self, pfConfig, testVector): |
| 51 | + self.command = [sys.executable, "domainGenerator.py", |
| 52 | + "--validate", |
| 53 | + "--verbose", |
| 54 | + "--toplevel-config", pfConfig.toplevel, |
| 55 | + "--criteria", pfConfig.criteria, |
| 56 | + "--schemas-dir", pfConfig.schemas] |
| 57 | + |
| 58 | + if testVector.initialSettings: |
| 59 | + self.command += ["--initial-settings", testVector.initialSettings] |
| 60 | + if testVector.edds: |
| 61 | + self.command += ["--add-edds"] + testVector.edds |
| 62 | + if testVector.domains: |
| 63 | + self.command += ["--add-domains"] + testVector.domains |
| 64 | + |
| 65 | + def check(self, reference=None, expectedErrors=0): |
| 66 | + process = subprocess.Popen(self.command, stdout=subprocess.PIPE) |
| 67 | + actual = process.stdout.read().splitlines() |
| 68 | + |
| 69 | + if process.wait() != expectedErrors: |
| 70 | + raise AssertionError("Expected {} errors, found {}".format( |
| 71 | + expectedErrors, |
| 72 | + process.returncode)) |
| 73 | + |
| 74 | + if not reference: |
| 75 | + # The caller only wants to check the number of errors |
| 76 | + return True |
| 77 | + |
| 78 | + # The generation has succeeded as expected - let's compare with the reference. |
| 79 | + unified = difflib.unified_diff(reference, |
| 80 | + actual, |
| 81 | + fromfile="reference.xml", |
| 82 | + tofile="-", |
| 83 | + lineterm="") |
| 84 | + diffs = list(unified) |
| 85 | + if diffs: |
| 86 | + for d in diffs: |
| 87 | + print(d) |
| 88 | + return AssertionError("The result and the reference don't match.") |
| 89 | + return True |
| 90 | + |
34 | 91 |
|
35 | 92 | basedir = os.path.dirname(sys.argv[0]) |
36 | 93 |
|
37 | | -configDir = os.path.join(basedir, "PFConfig") |
38 | | -vectorDir = os.path.join(basedir, "testVector") |
39 | | - |
40 | | -command = [sys.executable, "domainGenerator.py", |
41 | | - "--validate", |
42 | | - "--verbose", |
43 | | - "--toplevel-config", os.path.join(configDir, "configuration.xml"), |
44 | | - "--criteria", os.path.join(configDir, "criteria.txt"), |
45 | | - "--initial-settings", os.path.join(vectorDir, "initialSettings.xml"), |
46 | | - "--add-edds", os.path.join(vectorDir, "first.pfw"), os.path.join(vectorDir, "second.pfw"), |
47 | | - "--add-domains", os.path.join(vectorDir, "third.xml"), os.path.join(vectorDir, "fourth.xml"), |
48 | | - "--schemas-dir", os.path.join(basedir, "../../schemas")] |
49 | | - |
50 | | -reference = open(os.path.join(vectorDir, "reference.xml")).read().splitlines() |
51 | | - |
52 | | -process = subprocess.Popen(command, stdout=subprocess.PIPE) |
53 | | -actual = process.stdout.read().splitlines() |
54 | | - |
55 | | -unified = difflib.unified_diff(reference, |
56 | | - actual, |
57 | | - fromfile="reference.xml", |
58 | | - tofile="-", |
59 | | - lineterm="") |
60 | | -diffs = list(unified) |
61 | | -if diffs: |
62 | | - for d in diffs: |
63 | | - print(d) |
64 | | - sys.exit(1) |
65 | | -if process.wait() != 1: |
66 | | - print("Error: Expected 1 error, found {}".format(process.returncode)) |
67 | | - sys.exit(1) |
| 94 | +config_dir = os.path.join(basedir, "PFConfig") |
| 95 | +vector_dir = os.path.join(basedir, "testVector") |
| 96 | +class TestCase(unittest.TestCase): |
| 97 | + nominal_reference = open(os.path.join(vector_dir, "reference.xml")).read().splitlines() |
| 98 | + nominal_pfconfig = PfConfig(os.path.join(config_dir, "configuration.xml"), |
| 99 | + os.path.join(config_dir, "criteria.txt"), |
| 100 | + os.path.join(basedir, "../../schemas")) |
| 101 | + nominal_vector = TestVector(os.path.join(vector_dir, "initialSettings.xml"), |
| 102 | + [os.path.join(vector_dir, "first.pfw"), |
| 103 | + os.path.join(vector_dir, "second.pfw")], |
| 104 | + [os.path.join(vector_dir, "third.xml"), |
| 105 | + os.path.join(vector_dir, "fourth.xml")]) |
| 106 | + |
| 107 | + def test_nominal(self): |
| 108 | + tester = Tester(self.nominal_pfconfig, self.nominal_vector) |
| 109 | + self.assertTrue(tester.check(self.nominal_reference)) |
| 110 | + |
| 111 | + def test_nonfatalError(self): |
| 112 | + vector = copy.copy(self.nominal_vector) |
| 113 | + vector.edds.append(os.path.join(vector_dir, "duplicate.pfw")) |
| 114 | + |
| 115 | + tester = Tester(self.nominal_pfconfig, vector) |
| 116 | + self.assertTrue(tester.check(self.nominal_reference, expectedErrors=1)) |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + unittest.main() |
0 commit comments