def test_aoh_merge_key_ini(self, quiet_logger, tmp_path_factory): config_file = create_temp_yaml_file( tmp_path_factory, """ [keys] /array_of_hashes = id """) lhs_yaml_file = create_temp_yaml_file( tmp_path_factory, """--- hash: lhs_exclusive: lhs value 1 merge_targets: subkey: lhs value 2 subarray: - one - two array_of_hashes: - name: LHS Record 1 id: 1 prop: LHS value AoH 1 - name: LHS Record 2 id: 2 prop: LHS value AoH 2 """) lhs_yaml = get_yaml_editor() (lhs_data, lhs_loaded) = get_yaml_data(lhs_yaml, quiet_logger, lhs_yaml_file) mc = MergerConfig(quiet_logger, SimpleNamespace(config=config_file)) mc.prepare(lhs_data) node = lhs_data["array_of_hashes"] parent = lhs_data parentref = "array_of_hashes" record = node[0] assert mc.aoh_merge_key(NodeCoords(node, parent, parentref), record) == "id"
def test_cannot_save_multiple_matches(self, script_runner, tmp_path_factory): content = """--- key1: value1 key2: value2 """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--change=/[.^key]", "--random=1", "--saveto=/backup", yaml_file ) assert not result.success, result.stderr assert "It is impossible to meaningly save more than one" in result.stderr
def test_change_key_name_good(self, script_runner, tmp_path_factory): yamlin = """--- key: value """ yamlout = """--- renamed_key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin) result = script_runner.run(self.command, "--change=/key[name()]", "--value=renamed_key", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == yamlout
def test_ignore_aliases(self, script_runner, tmp_path_factory): content = """--- aliases: - &anchoredValue Anchored value parent: child: *anchoredValue """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--search", "$alue", yaml_file) assert result.success, result.stderr assert "\n".join([ "/aliases[&anchoredValue]", ]) + "\n" == result.stdout
def test_bad_yaml_path(self, script_runner, tmp_path_factory): content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) # Explicit --mustexist result = script_runner.run(self.command, "--change=key2", "--random=1", "--mustexist", yaml_file) assert not result.success, result.stderr assert "Required YAML Path does not match any nodes" in result.stderr # Implicit --mustexist via --saveto result = script_runner.run(self.command, "--change=key3", "--random=1", "--saveto=save_here", yaml_file) assert not result.success, result.stderr assert "Required YAML Path does not match any nodes" in result.stderr
def test_expand_sequence_parents(self, script_runner, tmp_path_factory): content = """--- - &list - - - value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--expand", "--refnames", "--search", "=list", yaml_file) assert result.success, result.stderr assert "\n".join([ "/&list[0][0][0]", ]) + "\n" == result.stdout
def test_assign_docroot_tag(self, script_runner, tmp_path_factory): yamlin = """--- key: value """ yamlout = """--- !something key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin) result = script_runner.run(self.command, "--change=/", "--tag=!something", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == yamlout
def test_bad_eyaml_command(self, script_runner, tmp_path_factory): content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--change=key", "--random=1", "--eyamlcrypt", "--eyaml=/does/not/exist/on-most/systems", yaml_file ) assert not result.success, result.stderr assert "The eyaml binary is not executable" in result.stderr
def test_get_yaml_multidoc_data_keyboardinterrupt_error( self, capsys, quiet_logger, tmp_path_factory, force_ruamel_load_keyboardinterrupt): yp = get_yaml_editor() content = """--- no: '' """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) docs_yielded = 0 for doc in get_yaml_multidoc_data(yp, quiet_logger, yaml_file): if not doc and not isinstance(doc, bool): docs_yielded += 1 assert docs_yielded == 0 captured = capsys.readouterr() assert -1 < captured.err.find("keyboard interrupt")
def test_nonrepeating_anchor_name_in_array(self, script_runner, tmp_path_factory): content = """--- aliases: - &anchor1 element 1 array: - *anchor1 """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--refnames", "--search", "^anchor", yaml_file) assert result.success, result.stderr assert "\n".join(["/aliases[&anchor1]"]) + "\n" == result.stdout
def test_multi_file_multi_expression_search(self, script_runner, tmp_path_factory): content1 = """--- - node: 1 value: A """ content2 = """--- - node: 2 value: B nest: - alpha - bravo """ yaml_file1 = create_temp_yaml_file(tmp_path_factory, content1) yaml_file2 = create_temp_yaml_file(tmp_path_factory, content2) result = script_runner.run(self.command, "--pathsep=/", "--keynames", "--search", "^value", "--search", "=bravo", yaml_file1, yaml_file2) assert result.success, result.stderr assert "\n".join([ "{}[^value]: /[0]/value".format(yaml_file1), "{}[^value]: /[0]/value".format(yaml_file2), "{}[=bravo]: /[0]/nest[1]".format(yaml_file2) ]) + "\n" == result.stdout
def test_set_value_in_empty_file(self, script_runner, tmp_path_factory): yaml_file = create_temp_yaml_file(tmp_path_factory, "") result_content = """--- some: key: to: nowhere """ result = script_runner.run(self.command, "--change=some.key.to", "--value=nowhere", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == result_content
def test_corrupted_eyaml_value(self, script_runner, tmp_path_factory, old_eyaml_keys, new_eyaml_keys): content = """--- key: > ENC[PKCS7,MII ... corrupted-value ... DBAEqBBAwcy7jvcOGcMfLEtugGVWWUnWq1DJ4Q==] """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--newprivatekey={}".format(new_eyaml_keys[0]), "--newpublickey={}".format(new_eyaml_keys[1]), "--oldprivatekey={}".format(old_eyaml_keys[0]), "--oldpublickey={}".format(old_eyaml_keys[1]), yaml_file) assert not result.success, result.stderr assert "Unable to decrypt value!" in result.stderr
def test_backup_file(self, script_runner, tmp_path_factory): import os content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) backup_file = yaml_file + ".bak" result = script_runner.run(self.command, "--change=key", "--random=1", "--backup", yaml_file) assert result.success, result.stderr assert os.path.isfile(backup_file) with open(backup_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == content
def test_save_old_crypt_value(self, script_runner, tmp_path_factory): import re content = """--- encrypted: > ENC[PKCS7,MIIB...] """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--change=encrypted", "--value=now_plaintext", "--saveto=backup", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert re.findall(r"^backup:\s+>$", filedat, re.M), filedat
def test_hash_merge_anchor(self, script_runner, tmp_path_factory): content = """--- anchored_hash: &anchoredHash key: value more_hash: <<: *anchoredHash more: values """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--keynames", "--refnames", "--search", "^anchored", yaml_file) assert result.success, result.stderr assert "\n".join([ "/anchored_hash", ]) + "\n" == result.stdout
def test_get_yaml_multidoc_data_duplicateanchor_error( self, capsys, quiet_logger, tmp_path_factory): yp = get_yaml_editor() content = """--- aliases: - &anchor value1 - &anchor value2 """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) docs_yielded = 0 for doc in get_yaml_multidoc_data(yp, quiet_logger, yaml_file): if not doc and not isinstance(doc, bool): docs_yielded += 1 assert docs_yielded == 0 captured = capsys.readouterr() assert -1 < captured.err.find("Duplicate YAML Anchor detected")
def test_get_yaml_multidoc_data_construction_error(self, capsys, quiet_logger, tmp_path_factory): yp = get_yaml_editor() content = """--- missing: <<: """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) docs_yielded = 0 for doc in get_yaml_multidoc_data(yp, quiet_logger, yaml_file): if not doc and not isinstance(doc, bool): docs_yielded += 1 assert docs_yielded == 0 captured = capsys.readouterr() assert -1 < captured.err.find("YAML construction error")
def test_bad_decryption(self, script_runner, tmp_path_factory, old_eyaml_keys): content = """--- encrypted: ENC[PKCS7,MIIx...broken-on-purpose...==] """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--change=encrypted", "--random=1", "--check=n/a", "--privatekey={}".format(old_eyaml_keys[0]), "--publickey={}".format(old_eyaml_keys[1]), yaml_file ) assert not result.success, result.stderr assert "Unable to decrypt value!" in result.stderr
def test_bad_crypt_path(self, script_runner, tmp_path_factory, old_eyaml_keys): content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--change=[0]", "--random=1", "--eyamlcrypt", "--privatekey={}".format(old_eyaml_keys[0]), "--publickey={}".format(old_eyaml_keys[1]), yaml_file ) assert not result.success, result.stderr assert "Cannot add" in result.stderr
def test_search_encrypted_values(self, script_runner, tmp_path_factory, old_eyaml_keys): content = """--- aliases: - &doesNotMatch > ENC[PKCS7,MIIBiQYJKoZIhvcNAQcDoIIBejCCAXYCAQAxggEhMIIBHQIBADAFMAACAQEw DQYJKoZIhvcNAQEBBQAEggEAMjjCGsU40xyRcwMIEUOHxAcwftmvKCiMsZxk cLgp7tc52f+dNdymNDrfh6Q9LasFZZe6e7gzSjukVj9URZQkDDl7csAcLgIU MhladFC30XgNHdejogyXLm3ZEISXRGWuYWCldMz8SgXKdjh53lrjup4vq+30 oj0l63Ayvx713lwMqO1wfpqX2gOJUcsobSNVXZ5a/j7pbqlgazaJjw859ZzJ l/PeunPHtfM987TX+sLb3gNoCmEer6DTgP9OH/mcvOog032QqADaBXSa5NCD b+wf5eYYEn3/FlgVZlTse865JAlDB+xcE0UoJtBt2qqOQcqVxWMKTahVdcMA fbUDqTBMBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBA+cYFas5DLzK18/KwN lR5igCCdj/rltHjF5wh/zf0wC9OxImzBIWRsJFiTU7cdN6cndg==] - &doesMatch > ENC[PKCS7,MIIBiQYJKoZIhvcNAQcDoIIBejCCAXYCAQAxggEhMIIBHQIBADAFMAACAQEw DQYJKoZIhvcNAQEBBQAEggEAoxeZnZUr27Uf0joVgiIzeTSGGWYIv/YMCVyH dqe0jvIrSjjg2ShQjPBe+syXKTeKZryU+eyuL5BmSBKXxhT9DNh89n2KpUwL euY+zDmIhzC2kaYpk3So2BKcf1U083xzVQi0tHQ6hnaddx/fSMocaO8eX9jO itSfVIuEylGNdH/HtZ8BtMz7t7AsnAiSTpkqx3BfBXbE7UDy5zEofnz9JwTr M1Hr7/E4ggi+oWXi+KKFmboVgRdTvN16F0/3v8IzytfkXMcKmaY2EbvVPc6X aUlBv7lav7iHXdtETMpVP5i04BGVNMaejd6Ij8O0j6pnAIZWPzFtmp/GIM3k uZ8FKTBMBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBDErMS1u80DI+6I3tXH 0383gCBxoEtDQ9n93/oYDStliDn7/hOGOgwCExaIHRXxeMHLrg==] also_matches: > ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw DQYJKoZIhvcNAQEBBQAEggEAgaYgoKqf77s8S8yfb6bvhrbubtym67/DkltB em0rN4Z2MVKgVp02wJKuaEX8L4DEftROgAz3TDqJihwcGXqMryr15OfRF7wM VLdqkD0+iRwqOWCoVIwFrJjquO/FyyaCeCNVH0XUjvJQyyUzokaHo9Jw6oqC 9wx5GytBmGtoiniUJgHaetiDQ6OBYXufqNYGZMKJN1u1qJqnYnw+kJMnfoND SENEi0bRc9hufCNTCT1cuJolxQYCfhpbWPSQFfIWjrIguF3Yud7CRfCc8AFy NSk+MYcrYNJxpF4OPCdFS3KdGAKQrbUVMTzp8IJ6Sd9WLOKIM/+84nFoZaQ3 KyhLjDA8BgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBDaeiqnwlecngAcBJlO 8OvCgBDvP5ZkrDJjHj6N5T8wSl/0] """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--search", "%what", "--decrypt", "--privatekey={}".format(old_eyaml_keys[0]), "--publickey={}".format(old_eyaml_keys[1]), yaml_file) assert result.success, result.stderr assert "\n".join([ "/aliases[&doesMatch]", "/also_matches", ]) + "\n" == result.stdout
def test_value_dump(self, script_runner, tmp_path_factory): content = """--- sample_scalar: value sample_hash: sub: - list - elements """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--keynames", "--values", "--search", "^sample", yaml_file) assert result.success, result.stderr assert "\n".join([ "/sample_scalar: value", '/sample_hash: {"sub": ["list", "elements"]}', ]) + "\n" == result.stdout
def test_input_by_stdin(self, tmp_path_factory): import re import subprocess content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = subprocess.run( [self.command, "--change=/key", "--stdin", yaml_file], stdout=subprocess.PIPE, input="abc".encode(), ) assert 0 == result.returncode, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert re.findall(r"^key:\s+abc$", filedat, re.M), filedat
def test_rename_anchor_implicit_no_longer_possible(self, script_runner, tmp_path_factory): yamlin = """--- aliases: - &old_anchor Some string key: *old_anchor """ yamlout = """--- aliases: - &new_anchor Some string key: *new_anchor """ yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin) result = script_runner.run(self.command, "--change=aliases[&old_anchor]", "--anchor=new_anchor", yaml_file) assert not result.success, result.stderr assert "The --anchor|-H option may be used only when --aliasof|-A or --mergekey|-K are also set" in result.stderr
def test_input_by_random(self, script_runner, tmp_path_factory): import re content = """--- key: value """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run( self.command, "--change=/key", "--random=50", yaml_file ) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert re.findall(r"^key:\s+[A-Za-z0-9]{50}$", filedat, re.M), filedat
def test_expanded_key_refmatches(self, script_runner, tmp_path_factory): content = """--- anchors: &keyAnchor key: &valueAnchor value *keyAnchor : ignoreChild: *valueAnchor includeChild: static """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--expand", "--keynames", "--refnames", "--allowkeyaliases", "--search", "=keyAnchor", yaml_file) assert result.success, result.stderr assert "\n".join([ "/anchors/key", "/key/includeChild", ]) + "\n" == result.stdout
def test_change_null(self, script_runner, tmp_path_factory): yamlin = """--- ingress_key: Preceding value concrete_key: egress_key: Following value """ yamlout = """--- ingress_key: Preceding value concrete_key: Now not null egress_key: Following value """ yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin) result = script_runner.run(self.command, "--change=concrete_key", "--value=Now not null", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == yamlout
def test_missing_key(self, script_runner, tmp_path_factory, old_eyaml_keys): content = """--- encrypted: ENC[PKCS7,MIIx...blahblahblah...==] """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--change=encrypted", "--random=1", "--check=n/a", "--privatekey={}".format(old_eyaml_keys[0]), yaml_file) assert not result.success, result.stderr assert "Neither or both private and public EYAML keys must be set" in result.stderr result = script_runner.run(self.command, "--change=encrypted", "--random=1", "--check=n/a", "--publickey={}".format(old_eyaml_keys[1]), yaml_file) assert not result.success, result.stderr assert "Neither or both private and public EYAML keys must be set" in result.stderr
def test_reuse_anchor(self, script_runner, tmp_path_factory): yamlin = """--- some_key: &has_anchor its value a_hash: a_key: A value """ yamlout = """--- some_key: &has_anchor its value a_hash: a_key: *has_anchor """ yaml_file = create_temp_yaml_file(tmp_path_factory, yamlin) result = script_runner.run(self.command, "--change=a_hash.a_key", "--aliasof=some_key", yaml_file) assert result.success, result.stderr with open(yaml_file, 'r') as fhnd: filedat = fhnd.read() assert filedat == yamlout
def test_multi_search_expressions(self, script_runner, tmp_path_factory): content = """--- - node: 1 value: A - node: 2 value: B nest: - alpha - bravo """ yaml_file = create_temp_yaml_file(tmp_path_factory, content) result = script_runner.run(self.command, "--nostdin", "--nofile", "--pathsep=/", "--keynames", "--search", "^value", "--search", "=bravo", yaml_file) assert result.success, result.stderr assert "\n".join([ "[^value]: /[0]/value", "[^value]: /[1]/value", "[=bravo]: /[1]/nest[1]" ]) + "\n" == result.stdout