| 1 |
import os |
| 2 |
import unittest |
| 3 |
from copy import deepcopy |
| 4 |
|
| 5 |
import mozunit |
| 6 |
|
| 7 |
JSON_TYPE = None |
| 8 |
try: |
| 9 |
import simplejson as json |
| 10 |
|
| 11 |
assert json |
| 12 |
except ImportError: |
| 13 |
import json |
| 14 |
|
| 15 |
JSON_TYPE = "json" |
| 16 |
else: |
| 17 |
JSON_TYPE = "simplejson" |
| 18 |
|
| 19 |
import mozharness.base.config as config |
| 20 |
|
| 21 |
MH_DIR = os.path.dirname(os.path.dirname(__file__)) |
| 22 |
|
| 23 |
|
| 24 |
class TestParseConfigFile(unittest.TestCase): |
| 25 |
def _get_json_config( |
| 26 |
self, |
| 27 |
filename=os.path.join(MH_DIR, "configs", "test", "test.json"), |
| 28 |
output="dict", |
| 29 |
): |
| 30 |
fh = open(filename) |
| 31 |
contents = json.load(fh) |
| 32 |
fh.close() |
| 33 |
if "output" == "dict": |
| 34 |
return dict(contents) |
| 35 |
else: |
| 36 |
return contents |
| 37 |
|
| 38 |
def _get_python_config( |
| 39 |
self, filename=os.path.join(MH_DIR, "configs", "test", "test.py"), output="dict" |
| 40 |
): |
| 41 |
global_dict = {} |
| 42 |
local_dict = {} |
| 43 |
# exec(open(filename).read(), global_dict, local_dict) |
| 44 |
exec( |
| 45 |
compile(open(filename, "rb").read(), filename, "exec"), |
| 46 |
global_dict, |
| 47 |
local_dict, |
| 48 |
) |
| 49 |
return local_dict["config"] |
| 50 |
|
| 51 |
def test_json_config(self): |
| 52 |
c = config.BaseConfig(initial_config_file="test/test.json") |
| 53 |
content_dict = self._get_json_config() |
| 54 |
for key in content_dict.keys(): |
| 55 |
self.assertEqual(content_dict[key], c._config[key]) |
| 56 |
|
| 57 |
def test_python_config(self): |
| 58 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 59 |
config_dict = self._get_python_config() |
| 60 |
for key in config_dict.keys(): |
| 61 |
self.assertEqual(config_dict[key], c._config[key]) |
| 62 |
|
| 63 |
def test_illegal_config(self): |
| 64 |
self.assertRaises( |
| 65 |
IOError, |
| 66 |
config.parse_config_file, |
| 67 |
"this_file_does_not_exist.py", |
| 68 |
search_path="yadda", |
| 69 |
) |
| 70 |
|
| 71 |
def test_illegal_suffix(self): |
| 72 |
self.assertRaises( |
| 73 |
RuntimeError, config.parse_config_file, "test/test.illegal_suffix" |
| 74 |
) |
| 75 |
|
| 76 |
def test_malformed_json(self): |
| 77 |
if JSON_TYPE == "simplejson": |
| 78 |
self.assertRaises( |
| 79 |
json.decoder.JSONDecodeError, |
| 80 |
config.parse_config_file, |
| 81 |
"test/test_malformed.json", |
| 82 |
) |
| 83 |
else: |
| 84 |
self.assertRaises( |
| 85 |
ValueError, config.parse_config_file, "test/test_malformed.json" |
| 86 |
) |
| 87 |
|
| 88 |
def test_malformed_python(self): |
| 89 |
self.assertRaises( |
| 90 |
SyntaxError, config.parse_config_file, "test/test_malformed.py" |
| 91 |
) |
| 92 |
|
| 93 |
def test_multiple_config_files_override_string(self): |
| 94 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 95 |
c.parse_args(["--cfg", "test/test_override.py,test/test_override2.py"]) |
| 96 |
self.assertEqual(c._config["override_string"], "yay") |
| 97 |
|
| 98 |
def test_multiple_config_files_override_list(self): |
| 99 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 100 |
c.parse_args(["--cfg", "test/test_override.py,test/test_override2.py"]) |
| 101 |
self.assertEqual(c._config["override_list"], ["yay", "worked"]) |
| 102 |
|
| 103 |
def test_multiple_config_files_override_dict(self): |
| 104 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 105 |
c.parse_args(["--cfg", "test/test_override.py,test/test_override2.py"]) |
| 106 |
self.assertEqual(c._config["override_dict"], {"yay": "worked"}) |
| 107 |
|
| 108 |
def test_multiple_config_files_keep_string(self): |
| 109 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 110 |
c.parse_args(["--cfg", "test/test_override.py,test/test_override2.py"]) |
| 111 |
self.assertEqual(c._config["keep_string"], "don't change me") |
| 112 |
|
| 113 |
def test_optional_config_files_override_value(self): |
| 114 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 115 |
c.parse_args( |
| 116 |
[ |
| 117 |
"--cfg", |
| 118 |
"test/test_override.py,test/test_override2.py", |
| 119 |
"--opt-cfg", |
| 120 |
"test/test_optional.py", |
| 121 |
] |
| 122 |
) |
| 123 |
self.assertEqual(c._config["opt_override"], "new stuff") |
| 124 |
|
| 125 |
def test_optional_config_files_missing_config(self): |
| 126 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 127 |
c.parse_args( |
| 128 |
[ |
| 129 |
"--cfg", |
| 130 |
"test/test_override.py,test/test_override2.py", |
| 131 |
"--opt-cfg", |
| 132 |
"test/test_optional.py,does_not_exist.py", |
| 133 |
] |
| 134 |
) |
| 135 |
self.assertEqual(c._config["opt_override"], "new stuff") |
| 136 |
|
| 137 |
def test_optional_config_files_keep_string(self): |
| 138 |
c = config.BaseConfig(initial_config_file="test/test.py") |
| 139 |
c.parse_args( |
| 140 |
[ |
| 141 |
"--cfg", |
| 142 |
"test/test_override.py,test/test_override2.py", |
| 143 |
"--opt-cfg", |
| 144 |
"test/test_optional.py", |
| 145 |
] |
| 146 |
) |
| 147 |
self.assertEqual(c._config["keep_string"], "don't change me") |
| 148 |
|
| 149 |
|
| 150 |
class TestReadOnlyDict(unittest.TestCase): |
| 151 |
control_dict = { |
| 152 |
"b": "2", |
| 153 |
"c": {"d": "4"}, |
| 154 |
"h": ["f", "g"], |
| 155 |
"e": ["f", "g", {"turtles": ["turtle1"]}], |
| 156 |
"d": {"turtles": ["turtle1"]}, |
| 157 |
} |
| 158 |
|
| 159 |
def get_unlocked_ROD(self): |
| 160 |
r = config.ReadOnlyDict(self.control_dict) |
| 161 |
return r |
| 162 |
|
| 163 |
def get_locked_ROD(self): |
| 164 |
r = config.ReadOnlyDict(self.control_dict) |
| 165 |
r.lock() |
| 166 |
return r |
| 167 |
|
| 168 |
def test_create_ROD(self): |
| 169 |
r = self.get_unlocked_ROD() |
| 170 |
self.assertEqual( |
| 171 |
r, self.control_dict, msg="can't transfer dict to ReadOnlyDict" |
| 172 |
) |
| 173 |
|
| 174 |
def test_pop_item(self): |
| 175 |
r = self.get_unlocked_ROD() |
| 176 |
r.popitem() |
| 177 |
self.assertEqual( |
| 178 |
len(r), |
| 179 |
len(self.control_dict) - 1, |
| 180 |
msg="can't popitem() ReadOnlyDict when unlocked", |
| 181 |
) |
| 182 |
|
| 183 |
def test_pop(self): |
| 184 |
r = self.get_unlocked_ROD() |
| 185 |
r.pop("e") |
| 186 |
self.assertEqual( |
| 187 |
len(r), |
| 188 |
len(self.control_dict) - 1, |
| 189 |
msg="can't pop() ReadOnlyDict when unlocked", |
| 190 |
) |
| 191 |
|
| 192 |
def test_set(self): |
| 193 |
r = self.get_unlocked_ROD() |
| 194 |
r["e"] = "yarrr" |
| 195 |
self.assertEqual( |
| 196 |
r["e"], "yarrr", msg="can't set var in ReadOnlyDict when unlocked" |
| 197 |
) |
| 198 |
|
| 199 |
def test_del(self): |
| 200 |
r = self.get_unlocked_ROD() |
| 201 |
del r["e"] |
| 202 |
self.assertEqual( |
| 203 |
len(r), |
| 204 |
len(self.control_dict) - 1, |
| 205 |
msg="can't del in ReadOnlyDict when unlocked", |
| 206 |
) |
| 207 |
|
| 208 |
def test_clear(self): |
| 209 |
r = self.get_unlocked_ROD() |
| 210 |
r.clear() |
| 211 |
self.assertEqual(r, {}, msg="can't clear() ReadOnlyDict when unlocked") |
| 212 |
|
| 213 |
def test_set_default(self): |
| 214 |
r = self.get_unlocked_ROD() |
| 215 |
for key in self.control_dict.keys(): |
| 216 |
r.setdefault(key, self.control_dict[key]) |
| 217 |
self.assertEqual( |
| 218 |
r, self.control_dict, msg="can't setdefault() ReadOnlyDict when unlocked" |
| 219 |
) |
| 220 |
|
| 221 |
def test_locked_set(self): |
| 222 |
r = self.get_locked_ROD() |
| 223 |
# TODO use |with self.assertRaises(AssertionError):| if/when we're |
| 224 |
# all on 2.7. |
| 225 |
try: |
| 226 |
r["e"] = 2 |
| 227 |
except AssertionError: |
| 228 |
pass |
| 229 |
else: |
| 230 |
self.assertEqual(0, 1, msg="can set r['e'] when locked") |
| 231 |
|
| 232 |
def test_locked_del(self): |
| 233 |
r = self.get_locked_ROD() |
| 234 |
try: |
| 235 |
del r["e"] |
| 236 |
except AssertionError: |
| 237 |
pass |
| 238 |
else: |
| 239 |
self.assertEqual(0, 1, "can del r['e'] when locked") |
| 240 |
|
| 241 |
def test_locked_popitem(self): |
| 242 |
r = self.get_locked_ROD() |
| 243 |
self.assertRaises(AssertionError, r.popitem) |
| 244 |
|
| 245 |
def test_locked_update(self): |
| 246 |
r = self.get_locked_ROD() |
| 247 |
self.assertRaises(AssertionError, r.update, {}) |
| 248 |
|
| 249 |
def test_locked_set_default(self): |
| 250 |
r = self.get_locked_ROD() |
| 251 |
self.assertRaises(AssertionError, r.setdefault, {}) |
| 252 |
|
| 253 |
def test_locked_pop(self): |
| 254 |
r = self.get_locked_ROD() |
| 255 |
self.assertRaises(AssertionError, r.pop) |
| 256 |
|
| 257 |
def test_locked_clear(self): |
| 258 |
r = self.get_locked_ROD() |
| 259 |
self.assertRaises(AssertionError, r.clear) |
| 260 |
|
| 261 |
def test_locked_second_level_dict_pop(self): |
| 262 |
r = self.get_locked_ROD() |
| 263 |
self.assertRaises(AssertionError, r["c"].update, {}) |
| 264 |
|
| 265 |
def test_locked_second_level_list_pop(self): |
| 266 |
r = self.get_locked_ROD() |
| 267 |
with self.assertRaises(AttributeError): |
| 268 |
r["e"].pop() |
| 269 |
|
| 270 |
def test_locked_third_level_mutate(self): |
| 271 |
r = self.get_locked_ROD() |
| 272 |
with self.assertRaises(AttributeError): |
| 273 |
r["d"]["turtles"].append("turtle2") |
| 274 |
|
| 275 |
def test_locked_object_in_tuple_mutate(self): |
| 276 |
r = self.get_locked_ROD() |
| 277 |
with self.assertRaises(AttributeError): |
| 278 |
r["e"][2]["turtles"].append("turtle2") |
| 279 |
|
| 280 |
def test_locked_second_level_dict_pop2(self): |
| 281 |
r = self.get_locked_ROD() |
| 282 |
self.assertRaises(AssertionError, r["c"].update, {}) |
| 283 |
|
| 284 |
def test_locked_second_level_list_pop2(self): |
| 285 |
r = self.get_locked_ROD() |
| 286 |
with self.assertRaises(AttributeError): |
| 287 |
r["e"].pop() |
| 288 |
|
| 289 |
def test_locked_third_level_mutate2(self): |
| 290 |
r = self.get_locked_ROD() |
| 291 |
with self.assertRaises(AttributeError): |
| 292 |
r["d"]["turtles"].append("turtle2") |
| 293 |
|
| 294 |
def test_locked_object_in_tuple_mutate2(self): |
| 295 |
r = self.get_locked_ROD() |
| 296 |
with self.assertRaises(AttributeError): |
| 297 |
r["e"][2]["turtles"].append("turtle2") |
| 298 |
|
| 299 |
def test_locked_deepcopy_set(self): |
| 300 |
r = self.get_locked_ROD() |
| 301 |
c = deepcopy(r) |
| 302 |
c["e"] = "hey" |
| 303 |
self.assertEqual(c["e"], "hey", "can't set var in ROD after deepcopy") |
| 304 |
|
| 305 |
|
| 306 |
class TestActions(unittest.TestCase): |
| 307 |
all_actions = ["a", "b", "c", "d", "e"] |
| 308 |
default_actions = ["b", "c", "d"] |
| 309 |
|
| 310 |
def test_verify_actions(self): |
| 311 |
c = config.BaseConfig(initial_config_file="test/test.json") |
| 312 |
try: |
| 313 |
c.verify_actions(["not_a_real_action"]) |
| 314 |
except SystemExit: |
| 315 |
pass |
| 316 |
else: |
| 317 |
self.assertEqual(0, 1, msg="verify_actions() didn't die on invalid action") |
| 318 |
c = config.BaseConfig(initial_config_file="test/test.json") |
| 319 |
returned_actions = c.verify_actions(c.all_actions) |
| 320 |
self.assertEqual( |
| 321 |
c.all_actions, |
| 322 |
returned_actions, |
| 323 |
msg="returned actions from verify_actions() changed", |
| 324 |
) |
| 325 |
|
| 326 |
def test_default_actions(self): |
| 327 |
c = config.BaseConfig( |
| 328 |
default_actions=self.default_actions, |
| 329 |
all_actions=self.all_actions, |
| 330 |
initial_config_file="test/test.json", |
| 331 |
) |
| 332 |
self.assertEqual( |
| 333 |
self.default_actions, c.get_actions(), msg="default_actions broken" |
| 334 |
) |
| 335 |
|
| 336 |
def test_no_action1(self): |
| 337 |
c = config.BaseConfig( |
| 338 |
default_actions=self.default_actions, |
| 339 |
all_actions=self.all_actions, |
| 340 |
initial_config_file="test/test.json", |
| 341 |
) |
| 342 |
c.parse_args(args=["foo", "--no-action", "a"]) |
| 343 |
self.assertEqual( |
| 344 |
self.default_actions, c.get_actions(), msg="--no-ACTION broken" |
| 345 |
) |
| 346 |
|
| 347 |
def test_no_action2(self): |
| 348 |
c = config.BaseConfig( |
| 349 |
default_actions=self.default_actions, |
| 350 |
all_actions=self.all_actions, |
| 351 |
initial_config_file="test/test.json", |
| 352 |
) |
| 353 |
c.parse_args(args=["foo", "--no-c"]) |
| 354 |
self.assertEqual(["b", "d"], c.get_actions(), msg="--no-ACTION broken") |
| 355 |
|
| 356 |
def test_add_action(self): |
| 357 |
c = config.BaseConfig( |
| 358 |
default_actions=self.default_actions, |
| 359 |
all_actions=self.all_actions, |
| 360 |
initial_config_file="test/test.json", |
| 361 |
) |
| 362 |
c.parse_args(args=["foo", "--add-action", "e"]) |
| 363 |
self.assertEqual( |
| 364 |
["b", "c", "d", "e"], c.get_actions(), msg="--add-action ACTION broken" |
| 365 |
) |
| 366 |
|
| 367 |
def test_only_action(self): |
| 368 |
c = config.BaseConfig( |
| 369 |
default_actions=self.default_actions, |
| 370 |
all_actions=self.all_actions, |
| 371 |
initial_config_file="test/test.json", |
| 372 |
) |
| 373 |
c.parse_args(args=["foo", "--a", "--e"]) |
| 374 |
self.assertEqual(["a", "e"], c.get_actions(), msg="--ACTION broken") |
| 375 |
|
| 376 |
|
| 377 |
if __name__ == "__main__": |
| 378 |
mozunit.main() |