blob: e2bed4c96a6e313e1b14f9b61ecdecae0cd5edd1 [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
15
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070016from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle
Sean McAllisterf38d1e92020-08-03 13:57:53 -060017from chromiumos.config.api.program_pb2 import Program
Andrew Lambeceaec02020-02-11 14:16:41 -070018
19
20class IoUtilsTest(unittest.TestCase):
21 """Tests for io_utils."""
22
23 def setUp(self):
Sean McAllister6cbb0ec2020-08-03 14:03:37 -060024 self.config = ConfigBundle(program_list=[Program(name='TestProgram1')])
Andrew Lambcabb8792020-03-09 13:13:17 -060025 repo_path = tempfile.mkdtemp()
Andrew Lambeceaec02020-02-11 14:16:41 -070026
Andrew Lambcabb8792020-03-09 13:13:17 -060027 os.mkdir(os.path.join(repo_path, 'generated'))
Andrew Lambeceaec02020-02-11 14:16:41 -070028
Andrew Lamb2413c982020-05-29 12:15:36 -060029 self.config_path = os.path.join(repo_path, 'generated', 'config.jsonproto')
30 json_output = json_format.MessageToJson(
31 self.config, sort_keys=True, use_integers_for_enums=True)
David Burger54f704e2020-04-30 13:33:47 -060032 with open(self.config_path, 'w') as f:
33 print(json_output, file=f)
Andrew Lambeceaec02020-02-11 14:16:41 -070034
Andrew Lamb5d132052020-05-29 14:49:06 -060035 self.factory_path = os.path.join(repo_path, 'factory')
36 os.makedirs(os.path.join(self.factory_path, 'generated'))
37 self.model_sku = {"model": {"a": 1}}
38 with open(
39 os.path.join(self.factory_path, 'generated', 'model_sku.json'),
40 'w') as f:
41 json.dump(self.model_sku, f)
42
Andrew Lamb9328c402020-03-05 12:57:59 -070043 def test_read_config(self):
David Burger54f704e2020-04-30 13:33:47 -060044 """Tests the json proto can be read."""
Andrew Lamb9328c402020-03-05 12:57:59 -070045 self.assertEqual(io_utils.read_config(self.config_path), self.config)
Andrew Lamb5d132052020-05-29 14:49:06 -060046
47 def test_read_model_sku_json(self):
48 """Tests model_sku.json can be read."""
49 self.assertDictEqual(
50 io_utils.read_model_sku_json(pathlib.Path(self.factory_path)),
51 self.model_sku)