Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ |
| 7 | Unit tests for platform_json.py |
| 8 | |
| 9 | """ |
| 10 | |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 11 | import collections |
| 12 | import io |
| 13 | import json |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 14 | import os |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 15 | import sys |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 16 | import tempfile |
| 17 | import unittest |
| 18 | |
| 19 | import platform_json |
| 20 | |
| 21 | MOCK_CONSOLIDATED_JSON_CONTENTS = \ |
| 22 | '''{ |
| 23 | "DEFAULTS": { |
| 24 | "platform": null, |
| 25 | "parent": null, |
| 26 | "field1": 5, |
| 27 | "field2": 5, |
| 28 | "field3": 5, |
| 29 | "field4": 5 |
| 30 | }, |
| 31 | "my_platform": { |
| 32 | "platform": "my_platform", |
| 33 | "parent": "my_parent", |
| 34 | "field1": 1, |
| 35 | "models": { |
| 36 | "my_model": { |
| 37 | "field1": 4 |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 38 | }, |
| 39 | "my_model2": { |
| 40 | "field2": 4 |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | }, |
| 44 | "my_parent": { |
| 45 | "platform": "my_parent", |
| 46 | "parent": "my_grandparent", |
| 47 | "field1": 2, |
| 48 | "field2": 2 |
| 49 | }, |
| 50 | "my_grandparent": { |
| 51 | "platform": "my_grandparent", |
| 52 | "field1": 3, |
| 53 | "field2": 3, |
| 54 | "field3": 3 |
| 55 | } |
| 56 | }''' |
| 57 | |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 58 | |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 59 | def _run_main(argv): |
| 60 | """Run platform_json.main(argv), capturing and returning stdout.""" |
| 61 | original_stdout = sys.stdout |
| 62 | capture_io = io.StringIO() |
| 63 | sys.stdout = capture_io |
| 64 | try: |
| 65 | platform_json.main(argv) |
| 66 | return capture_io.getvalue() |
| 67 | finally: |
| 68 | capture_io.close() |
| 69 | sys.stdout = original_stdout |
| 70 | |
| 71 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 72 | class AbstractMockConfigTestCase(object): |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 73 | """Parent class to handle setup and teardown of mock configs.""" |
| 74 | |
| 75 | def setUp(self): # pylint:disable=invalid-name |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 76 | """Write mock JSON to a temporary file""" |
| 77 | _, self.mock_filepath = tempfile.mkstemp() |
| 78 | with open(self.mock_filepath, 'w') as mock_file: |
| 79 | mock_file.write(MOCK_CONSOLIDATED_JSON_CONTENTS) |
| 80 | |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 81 | def tearDown(self): # pylint:disable=invalid-name |
| 82 | """Destroy the mock JSON file""" |
| 83 | os.remove(self.mock_filepath) |
| 84 | |
| 85 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 86 | class InheritanceTestCase(AbstractMockConfigTestCase, unittest.TestCase): |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 87 | """Ensure that all levels of inheritance are handled correctly""" |
| 88 | |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 89 | def runTest(self): # pylint:disable=invalid-name |
| 90 | """Load platform config and check that it looks correct""" |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 91 | consolidated_json = platform_json.load_consolidated_json( |
| 92 | self.mock_filepath) |
| 93 | my_platform = platform_json.calculate_config('my_platform', |
| 94 | None, |
| 95 | consolidated_json) |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 96 | self.assertEqual(my_platform['field1'], 1) # No inheritance |
| 97 | self.assertEqual(my_platform['field2'], 2) # Direct inheritance |
| 98 | self.assertEqual(my_platform['field3'], 3) # Recursive inheritance |
| 99 | self.assertEqual(my_platform['field4'], 5) # Inherit from DEFAULTS |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 100 | my_model = platform_json.calculate_config('my_platform', |
| 101 | 'my_model', |
| 102 | consolidated_json) |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 103 | self.assertEqual(my_model['field1'], 4) # Model override |
| 104 | self.assertEqual(my_model['field2'], 2) # Everything else is the same |
| 105 | self.assertEqual(my_model['field3'], 3) |
| 106 | self.assertEqual(my_model['field4'], 5) |
| 107 | |
| 108 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 109 | class EndToEndPlatformTestCase(AbstractMockConfigTestCase, unittest.TestCase): |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 110 | """End-to-end testing for specifying the platform name.""" |
| 111 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 112 | def runTest(self): # pylint: disable=invalid-name |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 113 | """Main test logic""" |
| 114 | # Basic platform specification |
| 115 | argv = ['my_platform', '-c', self.mock_filepath] |
| 116 | expected_json = collections.OrderedDict({ |
| 117 | 'platform': 'my_platform', |
| 118 | 'parent': 'my_parent', |
| 119 | 'field1': 1, |
| 120 | 'field2': 2, |
| 121 | 'field3': 3, |
| 122 | 'field4': 5 |
| 123 | }) |
| 124 | expected_output = json.dumps(expected_json, indent=4) + '\n' |
| 125 | self.assertEqual(_run_main(argv), expected_output) |
| 126 | |
| 127 | # Condensed output |
| 128 | argv.append('--condense-output') |
| 129 | expected_output = json.dumps(expected_json, indent=None) |
| 130 | self.assertEqual(_run_main(argv), expected_output) |
| 131 | |
| 132 | # Non-existent platform should raise an error |
| 133 | argv = ['fake_platform', '-c', self.mock_filepath] |
| 134 | with self.assertRaises(platform_json.PlatformNotFoundError): |
| 135 | _run_main(argv) |
| 136 | |
| 137 | |
Greg Edelston | 64fdc2e | 2020-11-19 15:04:18 -0700 | [diff] [blame^] | 138 | class EndToEndFieldTestCase(AbstractMockConfigTestCase, unittest.TestCase): |
Greg Edelston | f70963a | 2020-11-17 14:36:06 -0700 | [diff] [blame] | 139 | """End-to-end testing for specifying the field name.""" |
| 140 | |
| 141 | def runTest(self): # pylint: disable=invalid-name |
| 142 | """Main test logic""" |
| 143 | # Basic field specification |
| 144 | argv = ['my_platform', '-f', 'field1', '-c', self.mock_filepath] |
| 145 | self.assertEqual(_run_main(argv), '1\n') |
| 146 | |
| 147 | # Condensed output should yield no newline |
| 148 | argv.append('--condense-output') |
| 149 | self.assertEqual(_run_main(argv), '1') |
| 150 | |
| 151 | # Non-existent field should raise an error |
| 152 | argv = ['my_platform', '-f', 'fake_field', '-c', self.mock_filepath] |
| 153 | with self.assertRaises(platform_json.FieldNotFoundError): |
| 154 | _run_main(argv) |
Greg Edelston | 9cd16f5 | 2020-11-12 10:50:28 -0700 | [diff] [blame] | 155 | |
| 156 | |
| 157 | if __name__ == '__main__': |
| 158 | unittest.main() |