blob: 09ba58b95ba9439b11bb65c52f3e5b0c879b4c1d [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
17from chromiumos.config.api.program_pb2 import ProgramList, Program
Andrew Lambeceaec02020-02-11 14:16:41 -070018
19
20class IoUtilsTest(unittest.TestCase):
21 """Tests for io_utils."""
22
23 def setUp(self):
24 self.config = ConfigBundle(
25 programs=ProgramList(value=[Program(name='TestProgram1')]))
Andrew Lambcabb8792020-03-09 13:13:17 -060026 repo_path = tempfile.mkdtemp()
Andrew Lambeceaec02020-02-11 14:16:41 -070027
Andrew Lambcabb8792020-03-09 13:13:17 -060028 os.mkdir(os.path.join(repo_path, 'generated'))
Andrew Lambeceaec02020-02-11 14:16:41 -070029
Andrew Lamb2413c982020-05-29 12:15:36 -060030 self.config_path = os.path.join(repo_path, 'generated', 'config.jsonproto')
31 json_output = json_format.MessageToJson(
32 self.config, sort_keys=True, use_integers_for_enums=True)
David Burger54f704e2020-04-30 13:33:47 -060033 with open(self.config_path, 'w') as f:
34 print(json_output, file=f)
Andrew Lambeceaec02020-02-11 14:16:41 -070035
Andrew Lamb5d132052020-05-29 14:49:06 -060036 self.factory_path = os.path.join(repo_path, 'factory')
37 os.makedirs(os.path.join(self.factory_path, 'generated'))
38 self.model_sku = {"model": {"a": 1}}
39 with open(
40 os.path.join(self.factory_path, 'generated', 'model_sku.json'),
41 'w') as f:
42 json.dump(self.model_sku, f)
43
Andrew Lamb9328c402020-03-05 12:57:59 -070044 def test_read_config(self):
David Burger54f704e2020-04-30 13:33:47 -060045 """Tests the json proto can be read."""
Andrew Lamb9328c402020-03-05 12:57:59 -070046 self.assertEqual(io_utils.read_config(self.config_path), self.config)
Andrew Lamb5d132052020-05-29 14:49:06 -060047
48 def test_read_model_sku_json(self):
49 """Tests model_sku.json can be read."""
50 self.assertDictEqual(
51 io_utils.read_model_sku_json(pathlib.Path(self.factory_path)),
52 self.model_sku)