Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Tests for io_utils.""" |
| 5 | |
| 6 | import os |
| 7 | import tempfile |
| 8 | import unittest |
| 9 | |
| 10 | from checker import io_utils |
| 11 | |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 12 | from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle |
| 13 | from chromiumos.config.api.program_pb2 import ProgramList, Program |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 14 | |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 15 | from google.protobuf import json_format |
| 16 | |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 17 | |
| 18 | class IoUtilsTest(unittest.TestCase): |
| 19 | """Tests for io_utils.""" |
| 20 | |
| 21 | def setUp(self): |
| 22 | self.config = ConfigBundle( |
| 23 | programs=ProgramList(value=[Program(name='TestProgram1')])) |
Andrew Lamb | cabb879 | 2020-03-09 13:13:17 -0600 | [diff] [blame] | 24 | repo_path = tempfile.mkdtemp() |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 25 | |
Andrew Lamb | cabb879 | 2020-03-09 13:13:17 -0600 | [diff] [blame] | 26 | os.mkdir(os.path.join(repo_path, 'generated')) |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 27 | |
Andrew Lamb | cabb879 | 2020-03-09 13:13:17 -0600 | [diff] [blame] | 28 | self.config_path = os.path.join(repo_path, 'generated', |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 29 | 'config.jsonproto') |
| 30 | json_output = json_format.MessageToJson(self.config, |
| 31 | sort_keys=True, |
| 32 | use_integers_for_enums=True) |
| 33 | with open(self.config_path, 'w') as f: |
| 34 | print(json_output, file=f) |
Andrew Lamb | eceaec0 | 2020-02-11 14:16:41 -0700 | [diff] [blame] | 35 | |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame] | 36 | def test_read_config(self): |
David Burger | 54f704e | 2020-04-30 13:33:47 -0600 | [diff] [blame] | 37 | """Tests the json proto can be read.""" |
Andrew Lamb | 9328c40 | 2020-03-05 12:57:59 -0700 | [diff] [blame] | 38 | self.assertEqual(io_utils.read_config(self.config_path), self.config) |