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