-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtest_config.py
More file actions
176 lines (143 loc) · 7.01 KB
/
test_config.py
File metadata and controls
176 lines (143 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import json
import os
import unittest
from pathlib import Path
from unittest.mock import patch
from hta.configs.config import HtaConfig
from hta.configs.default_values import DEFAULT_CONFIG_FILENAME
from hta.configs.env_options import (
CP_LAUNCH_EDGE_ENV,
get_options,
HTA_DISABLE_NS_ROUNDING_ENV,
HTAEnvOptions,
)
class HtaConfigTestCase(unittest.TestCase):
def setUp(self) -> None:
self.test_config_path = "/tmp/test_config.json"
self.test_config = {
"a": 1,
"b": ["s", "t"],
"c": {"c1": 2, "c2": {"c21": 10.0}},
}
with open(self.test_config_path, "w+") as fp:
json.dump(self.test_config, fp)
def test_get_default_paths(self):
paths = HtaConfig.get_default_paths()
self.assertEqual(
len(paths), 3, f"expect the default file paths to be 3 but got {len(paths)}"
)
self.assertTrue(
all(str(path).endswith(DEFAULT_CONFIG_FILENAME) for path in paths)
)
def test_constructor_no_config_file(self):
config = HtaConfig(load_default_paths=False)
self.assertDictEqual(config.get_config(), {})
def test_constructor_one_config_file(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
self.assertEqual(config.get_config(), self.test_config)
def test_get_config_file_paths(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
paths = config.get_config_file_paths()
self.assertListEqual(paths, [self.test_config_path])
def test_get_config_all(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
config_values = config.get_config()
self.assertDictEqual(config_values, self.test_config)
def test_get_config_one_level(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
self.assertEqual(config.get_config("a"), self.test_config["a"])
self.assertListEqual(config.get_config("b"), self.test_config["b"])
self.assertDictEqual(config.get_config("c"), self.test_config["c"])
def test_get_config_multiple_levels(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
self.assertDictEqual(config.get_config("c"), self.test_config["c"])
self.assertEqual(config.get_config("c.c1"), self.test_config["c"]["c1"])
self.assertEqual(
config.get_config("c.c2.c21"), self.test_config["c"]["c2"]["c21"]
)
self.assertIsNone(config.get_config("d"))
self.assertIsNone(config.get_config("c.c2.c22"))
self.assertIsNone(config.get_config("c.c1.c3"))
def test_get_config_default_values(self):
config = HtaConfig(self.test_config_path, load_default_paths=False)
self.assertEqual(config.get_config("c", 10), self.test_config["c"])
self.assertEqual(config.get_config("d", 10), 10)
def test_get_env_options(self):
self.assertNotEqual(get_options(), "")
def test_get_test_data_path(self):
data_path = HtaConfig.get_test_data_path("h100")
self.assertTrue(Path(data_path).exists())
class HTAEnvOptionsTestCase(unittest.TestCase):
def setUp(self) -> None:
# Reset the singleton instance before each test
HTAEnvOptions._instance = None
# Save original environment variables
self.original_env = os.environ.copy()
def tearDown(self) -> None:
# Reset the singleton instance after each test
HTAEnvOptions._instance = None
# Restore original environment variables
os.environ.clear()
os.environ.update(self.original_env)
def test_singleton_behavior(self):
"""Test that instance() always returns the same instance."""
instance1 = HTAEnvOptions.instance()
instance2 = HTAEnvOptions.instance()
self.assertIs(instance1, instance2, "instance() should return the same object")
def test_get_set_options(self):
"""Test getting and setting options."""
options = HTAEnvOptions.instance()
# Test default values
self.assertFalse(options.disable_ns_rounding())
self.assertFalse(options.disable_call_graph_depth())
self.assertFalse(options.critical_path_add_zero_weight_launch_edges())
self.assertFalse(options.critical_path_show_zero_weight_launch_edges())
self.assertFalse(options.critical_path_strict_negative_weight_check())
# Test setting values
options.set_disable_ns_rounding(True)
self.assertTrue(options.disable_ns_rounding())
options.set_critical_path_add_zero_weight_launch_edges(True)
self.assertTrue(options.critical_path_add_zero_weight_launch_edges())
# Test that other values remain unchanged
self.assertFalse(options.disable_call_graph_depth())
self.assertFalse(options.critical_path_show_zero_weight_launch_edges())
self.assertFalse(options.critical_path_strict_negative_weight_check())
def test_environment_variable_reading(self):
"""Test that environment variables are correctly read."""
# Set environment variables
os.environ[HTA_DISABLE_NS_ROUNDING_ENV] = "1"
os.environ[CP_LAUNCH_EDGE_ENV] = "1"
# Create a new instance that should read these environment variables
HTAEnvOptions._instance = None
options = HTAEnvOptions.instance()
# Check that the environment variables were correctly read
self.assertTrue(options.disable_ns_rounding())
self.assertTrue(options.critical_path_add_zero_weight_launch_edges())
self.assertFalse(options.disable_call_graph_depth()) # Default value
def test_get_options_str(self):
"""Test the get_options_str method."""
options = HTAEnvOptions.instance()
options_str = options.get_options_str()
# Check that the string contains all option names
self.assertIn("disable_ns_rounding", options_str)
self.assertIn("disable_call_graph_depth", options_str)
self.assertIn("critical_path_add_zero_weight_launch_edges", options_str)
self.assertIn("critical_path_show_zero_weight_launch_edges", options_str)
self.assertIn("critical_path_strict_negative_weight_check", options_str)
@patch.dict(os.environ, {HTA_DISABLE_NS_ROUNDING_ENV: "1"})
def test_legacy_functions(self):
"""Test that legacy functions use the singleton instance."""
from hta.configs.env_options import (
disable_call_graph_depth,
disable_ns_rounding,
)
# Reset the singleton to ensure it reads the patched environment
HTAEnvOptions._instance = None
# Check that legacy functions return the correct values
self.assertTrue(disable_ns_rounding())
self.assertFalse(disable_call_graph_depth())
if __name__ == "__main__": # pragma: no cover
unittest.main()