blob: d2da36f5897b2e3d674cb3d29b58df41ae82d484 [file] [log] [blame]
Andrew Lambeceaec02020-02-11 14:16:41 -07001# 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
Andrew Lamb5d132052020-05-29 14:49:06 -06006import json
Andrew Lambeceaec02020-02-11 14:16:41 -07007import os
Andrew Lamb5d132052020-05-29 14:49:06 -06008import pathlib
Andrew Lambeceaec02020-02-11 14:16:41 -07009import tempfile
10import unittest
11
Andrew Lamb0ed1b902020-06-08 17:03:47 -060012from google.protobuf import json_format
13
Andrew Lambeceaec02020-02-11 14:16:41 -070014from checker import io_utils
Sean McAllisterd2d91882020-10-12 10:49:19 -060015from common import config_bundle_utils
Andrew Lambeceaec02020-02-11 14:16:41 -070016
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070017from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle
Sean McAllisterf38d1e92020-08-03 13:57:53 -060018from chromiumos.config.api.program_pb2 import Program
Andrew Lambeceaec02020-02-11 14:16:41 -070019
20
21class IoUtilsTest(unittest.TestCase):
22 """Tests for io_utils."""
23
24 def setUp(self):
Sean McAllister6cbb0ec2020-08-03 14:03:37 -060025 self.config = ConfigBundle(program_list=[Program(name='TestProgram1')])
Sean McAllisterd2d91882020-10-12 10:49:19 -060026 self.flat_config = config_bundle_utils.flatten_config(self.config)
Andrew Lambcabb8792020-03-09 13:13:17 -060027 repo_path = tempfile.mkdtemp()
Andrew Lambeceaec02020-02-11 14:16:41 -070028
Andrew Lambcabb8792020-03-09 13:13:17 -060029 os.mkdir(os.path.join(repo_path, 'generated'))
Andrew Lambeceaec02020-02-11 14:16:41 -070030
Andrew Lamb2413c982020-05-29 12:15:36 -060031 self.config_path = os.path.join(repo_path, 'generated', 'config.jsonproto')
32 json_output = json_format.MessageToJson(
33 self.config, sort_keys=True, use_integers_for_enums=True)
David Burger54f704e2020-04-30 13:33:47 -060034 with open(self.config_path, 'w') as f:
35 print(json_output, file=f)
Andrew Lambeceaec02020-02-11 14:16:41 -070036
Sean McAllisterd2d91882020-10-12 10:49:19 -060037 self.flat_config_path = os.path.join(repo_path, 'generated',
38 'flattened.jsonproto')
39 json_output = json_format.MessageToJson(
40 self.flat_config, sort_keys=True, use_integers_for_enums=True)
41 with open(self.flat_config_path, 'w') as f:
42 print(json_output, file=f)
43
Andrew Lamb5d132052020-05-29 14:49:06 -060044 self.factory_path = os.path.join(repo_path, 'factory')
45 os.makedirs(os.path.join(self.factory_path, 'generated'))
46 self.model_sku = {"model": {"a": 1}}
47 with open(
48 os.path.join(self.factory_path, 'generated', 'model_sku.json'),
49 'w') as f:
50 json.dump(self.model_sku, f)
51
Andrew Lamb9328c402020-03-05 12:57:59 -070052 def test_read_config(self):
David Burger54f704e2020-04-30 13:33:47 -060053 """Tests the json proto can be read."""
Andrew Lamb9328c402020-03-05 12:57:59 -070054 self.assertEqual(io_utils.read_config(self.config_path), self.config)
Andrew Lamb5d132052020-05-29 14:49:06 -060055
Sean McAllisterd2d91882020-10-12 10:49:19 -060056 def test_read_flat_config(self):
57 """Tests the json proto can be read."""
58 self.assertEqual(
59 io_utils.read_flat_config(self.flat_config_path), self.flat_config)
60
Andrew Lamb5d132052020-05-29 14:49:06 -060061 def test_read_model_sku_json(self):
62 """Tests model_sku.json can be read."""
63 self.assertDictEqual(
64 io_utils.read_model_sku_json(pathlib.Path(self.factory_path)),
65 self.model_sku)