diff --git a/changelogs/fragments/200.yaml b/changelogs/fragments/200.yaml new file mode 100644 index 0000000..d2f65f3 --- /dev/null +++ b/changelogs/fragments/200.yaml @@ -0,0 +1,3 @@ +--- +trivial: + - Add integration tests for replace_keys diff --git a/tests/integration/targets/utils_replace_keys/tasks/complex1.yaml b/tests/integration/targets/utils_replace_keys/tasks/complex1.yaml new file mode 100644 index 0000000..9cd3658 --- /dev/null +++ b/tests/integration/targets/utils_replace_keys/tasks/complex1.yaml @@ -0,0 +1,127 @@ +--- +- name: Include expected output data + ansible.builtin.include_vars: + file: complex.yaml + +- name: Block for l1 tests + block: + - name: Setup data as facts for complex replace integration test l1 + ansible.builtin.set_fact: + l1: + - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + + - name: Debug l1 + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.replace_keys(target=[{'before': 'p1', 'after': 'x1'}, {'before': 'p2', 'after': 'x2'}]) }}" + register: result_l1 + + - name: Assert result dicts l1 + ansible.builtin.assert: + that: + - result_l1['msg'] == replace['l1'] + + - name: Debug l1 for starts_with 'p' + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.replace_keys(target=[{'before': 'p', 'after': 'x'}], matching_parameter='starts_with') }}" + register: result_l1_starts_with_p + + - name: Assert result dicts l1 for starts_with 'p' + ansible.builtin.assert: + that: + - result_l1_starts_with_p['msg'] == replace['l1_starts_with_p'] + + - name: Debug l1 for ends_with + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.replace_keys(target=[{'before': 'p1', 'after': 'x1'}, {'before': 'p2', 'after': 'x2'}], matching_parameter='ends_with') }}" + register: result_l1_ends_with + + - name: Assert result dicts l1 for ends_with + ansible.builtin.assert: + that: + - result_l1_ends_with['msg'] == replace['l1_ends_with'] + + - name: Debug l1 for ends_with '2' + ansible.builtin.debug: + msg: "{{ l1 | ansible.utils.replace_keys(target=[{'before': '2', 'after': '9'}], matching_parameter='ends_with') }}" + register: result_l1_ends_with_2 + + - name: Assert result dicts l1 for ends_with '2' + ansible.builtin.assert: + that: + - result_l1_ends_with_2['msg'] == replace['l1_ends_with_2'] + +- name: Block for l2 tests + block: + - name: Setup data as facts for complex replace integration test l2 + ansible.builtin.set_fact: + l2: + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + - - { p1: a, p2: a, p3: a } + - { p1: b, p2: b, p3: b } + - { p1: c, p2: c, p3: c } + + - name: Debug l2 + ansible.builtin.debug: + msg: "{{ l2 | map('ansible.utils.replace_keys', target=[{'before': 'p1', 'after': 'x1'}, {'before': 'p2', 'after': 'x2'}]) | list }}" + register: result_l2 + + - name: Assert result dicts l2 + ansible.builtin.assert: + that: + - result_l2['msg'] == replace['l2'] + +- name: Block for l3 tests + block: + - name: Setup data as facts for replace integration test l3 + ansible.builtin.set_fact: + l3: + - { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a } + - { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b } + - { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c } + + - name: Debug l3 + ansible.builtin.debug: + msg: "{{ l3 | ansible.utils.replace_keys(target=[{'before': 'p1_key_o1', 'after': 'p1_key_oX1'}, {'before': 'p2_key_o2', 'after': 'p2_key_oX2'}]) }}" + register: result_l3 + + - name: Assert result dicts l3 + ansible.builtin.assert: + that: + - result_l3['msg'] == replace['l3'] + + - name: Debug l3 for starts_with + ansible.builtin.debug: + msg: "{{ l3 | ansible.utils.replace_keys(target=[{'before': 'p1', 'after': 'x1'}, {'before': 'p2', 'after': 'x2'}], matching_parameter='starts_with') }}" + register: result_l3_starts_with + + - name: Assert result dicts l3 for starts_with + ansible.builtin.assert: + that: + - result_l3_starts_with['msg'] == replace['l3_starts_with'] + + - name: Debug l3 for ends_with + ansible.builtin.debug: + msg: "{{ l3 | ansible.utils.replace_keys(target=[{'before': 'o1', 'after': 'x1'}, {'before': 'o2', 'after': 'x2'}], matching_parameter='ends_with') }}" + register: result_l3_ends_with + + - name: Assert result dicts l3 for ends_with + ansible.builtin.assert: + that: + - result_l3_ends_with['msg'] == replace['l3_ends_with'] + + - name: Debug l3 for regex + ansible.builtin.debug: + msg: "{{ l3 | ansible.utils.replace_keys(target=[{'before': '^(.*)_key_o2$', 'after': 'XY_key_o2'}], matching_parameter='regex') }}" + register: result_l3_regex + + - name: Assert result dicts l3 for regex + ansible.builtin.assert: + that: + - result_l3_regex['msg'] == replace['l3_regex'] diff --git a/tests/integration/targets/utils_replace_keys/tasks/simple.yaml b/tests/integration/targets/utils_replace_keys/tasks/simple.yaml index a8f4ce2..6145fb2 100644 --- a/tests/integration/targets/utils_replace_keys/tasks/simple.yaml +++ b/tests/integration/targets/utils_replace_keys/tasks/simple.yaml @@ -25,6 +25,10 @@ mtu: 600 enabled: false +- name: Include expected output data + ansible.builtin.include_vars: + file: main.yaml + - name: Debug ansible.builtin.debug: msg: "{{ data | ansible.utils.replace_keys(target=[{'before': 'interface_name', 'after': 'name'}]) }}" diff --git a/tests/integration/targets/utils_replace_keys/vars/complex.yaml b/tests/integration/targets/utils_replace_keys/vars/complex.yaml new file mode 100644 index 0000000..2df84f6 --- /dev/null +++ b/tests/integration/targets/utils_replace_keys/vars/complex.yaml @@ -0,0 +1,44 @@ +--- +replace: + l1: + - { "p3": "a", "x1": "a", "x2": "a" } + - { "p3": "b", "x1": "b", "x2": "b" } + - { "p3": "c", "x1": "c", "x2": "c" } + l2: + - - { "p3": "a", "x1": "a", "x2": "a" } + - { "p3": "b", "x1": "b", "x2": "b" } + - { "p3": "c", "x1": "c", "x2": "c" } + - - { "p3": "a", "x1": "a", "x2": "a" } + - { "p3": "b", "x1": "b", "x2": "b" } + - { "p3": "c", "x1": "c", "x2": "c" } + - - { "p3": "a", "x1": "a", "x2": "a" } + - { "p3": "b", "x1": "b", "x2": "b" } + - { "p3": "c", "x1": "c", "x2": "c" } + l3: + - { "p1_key_oX1": "a", "p2_key_oX2": "a", "p3_key_o3": "a" } + - { "p1_key_o4": "b", "p2_key_o5": "b", "p3_key_o6": "b" } + - { "p1_key_o7": "c", "p2_key_o8": "c", "p3_key_o9": "c" } + l3_starts_with: + - { "p3_key_o3": "a", "x1": "a", "x2": "a" } + - { "p3_key_o6": "b", "x1": "b", "x2": "b" } + - { "p3_key_o9": "c", "x1": "c", "x2": "c" } + l1_starts_with_p: + - { "x": "a" } + - { "x": "b" } + - { "x": "c" } + l1_ends_with: + - { "p3": "a", "x1": "a", "x2": "a" } + - { "p3": "b", "x1": "b", "x2": "b" } + - { "p3": "c", "x1": "c", "x2": "c" } + l3_ends_with: + - { "p3_key_o3": "a", "x1": "a", "x2": "a" } + - { "p1_key_o4": "b", "p2_key_o5": "b", "p3_key_o6": "b" } + - { "p1_key_o7": "c", "p2_key_o8": "c", "p3_key_o9": "c" } + l1_ends_with_2: + - { "9": "a", "p1": "a", "p3": "a" } + - { "9": "b", "p1": "b", "p3": "b" } + - { "9": "c", "p1": "c", "p3": "c" } + l3_regex: + - { "XY_key_o2": "a", "p1_key_o1": "a", "p3_key_o3": "a" } + - { "p1_key_o4": "b", "p2_key_o5": "b", "p3_key_o6": "b" } + - { "p1_key_o7": "c", "p2_key_o8": "c", "p3_key_o9": "c" }