• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The ChromiumOS Authors
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
6import json
7import os
8import pathlib
9import tempfile
10import unittest
11
12from google.protobuf import json_format
13
14from checker import io_utils
15from common import config_bundle_utils
16
17from chromiumos.config.payload.config_bundle_pb2 import ConfigBundle
18from chromiumos.config.api.program_pb2 import Program
19
20
21class IoUtilsTest(unittest.TestCase):
22  """Tests for io_utils."""
23
24  def setUp(self):
25    self.config = ConfigBundle(program_list=[Program(name='TestProgram1')])
26    self.flat_config = config_bundle_utils.flatten_config(self.config)
27    repo_path = tempfile.mkdtemp()
28
29    os.mkdir(os.path.join(repo_path, 'generated'))
30
31    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)
34    with open(self.config_path, 'w', encoding='utf-8') as f:
35      print(json_output, file=f)
36
37    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', encoding='utf-8') as f:
42      print(json_output, file=f)
43
44    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',
50        encoding='utf-8') as f:
51      json.dump(self.model_sku, f)
52
53  def test_read_config(self):
54    """Tests the json proto can be read."""
55    self.assertEqual(io_utils.read_config(self.config_path), self.config)
56
57  def test_read_flat_config(self):
58    """Tests the json proto can be read."""
59    self.assertEqual(
60        io_utils.read_flat_config(self.flat_config_path), self.flat_config)
61
62  def test_read_model_sku_json(self):
63    """Tests model_sku.json can be read."""
64    self.assertDictEqual(
65        io_utils.read_model_sku_json(pathlib.Path(self.factory_path)),
66        self.model_sku)
67