summaryrefslogtreecommitdiffstats
path: root/roles/lib_utils/src/test/integration/yedit.yml
blob: e3dfd490b0ebba826963fd6f65f413c05ec089b7 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/ansible-playbook --module-path=../../../library/
#
# Yedit test so that we can quickly determine if features are working
# Ensure that the kube-manager.yaml file exists
#
# ./yedit_test.yml
#
---
- hosts: localhost
  gather_facts: no
  vars:
    test_file: kube-manager-test.yaml
    test: test
  strategy: debug

  post_tasks:
  - name: copy the kube-manager.yaml file so that we have a pristine copy each time
    copy:
      src: kube-manager.yaml
      dest: "./{{ test_file }}"
    changed_when: False

  ####### add key to top level #####
  - name: add a key at the top level
    yedit:
      src: "{{ test_file }}"
      key: yedittest
      value: yedittest

  - name: retrieve the inserted key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: yedittest
    register: results

  - name: Assert that key is at top level
    assert:
      that: results.result == 'yedittest'
      msg: 'Test: add a key to top level failed.  yedittest != [{{ results.result }}]'
  ###### end add key to top level #####

  ###### modify multilevel key, value #####
  - name: modify multilevel key, value
    yedit:
      src: "{{ test_file }}"
      key: metadata-namespace
      value: openshift-is-awesome
      separator: '-'

  - name: retrieve the inserted key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: metadata-namespace
      separator: '-'
    register: results

  - name: Assert that key is as expected
    assert:
      that: results.result == 'openshift-is-awesome'
      msg: 'Test: multilevel key, value modification:  openshift-is-awesome != [{{ results.result }}]'
  ###### end modify multilevel key, value #####

  ###### test a string boolean #####
  - name: test a string boolean
    yedit:
      src: "{{ test_file }}"
      key: spec.containers[0].volumeMounts[1].readOnly
      value: 'true'
      value_type: str

  - name: retrieve the inserted key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: spec.containers[0].volumeMounts[1].readOnly
    register: results

  - name: Assert that key is a string
    assert:
      that: results.result == "true"
      msg: "Test: boolean str:  'true' != [{{ results.result }}]"

  - name: Assert that key is not bool
    assert:
      that: results.result != true
      msg: "Test: boolean str:  true != [{{ results.result }}]"
  ###### end test boolean string #####

  ###### test array append #####
  - name: test array append
    yedit:
      src: "{{ test_file }}"
      key: spec.containers[0].command
      value: --my-new-parameter=openshift
      append: True

  - name: retrieve the array
    yedit:
      src: "{{ test_file }}"
      state: list
      key: spec.containers[0].command
    register: results

  - name: Assert that the last element in array is our value
    assert:
      that: results.result[-1] == "--my-new-parameter=openshift"
      msg: "Test: '--my-new-parameter=openshift' != [{{ results.result[-1] }}]"
  ###### end test array append #####

  ###### test non-existing array append #####
  - name: test array append to non-existing key
    yedit:
      src: "{{ test_file }}"
      key: nonexistingkey
      value: --my-new-parameter=openshift
      append: True

  - name: retrieve the array
    yedit:
      src: "{{ test_file }}"
      state: list
      key: nonexistingkey
    register: results

  - name: Assert that the last element in array is our value
    assert:
      that: results.result[-1] == "--my-new-parameter=openshift"
      msg: "Test: '--my-new-parameter=openshift' != [{{ results.result[-1] }}]"
  ###### end test non-existing array append #####

  ###### test array update modify #####
  - name: test array update modify
    yedit:
      src: "{{ test_file }}"
      key: spec.containers[0].command
      value: --root-ca-file=/etc/k8s/ssl/my.pem
      curr_value: --root-ca-file=/etc/kubernetes/ssl/ca.pem
      curr_value_format: str
      update: True

  - name: retrieve the array
    yedit:
      src: "{{ test_file }}"
      state: list
      key: spec.containers[0].command
    register: results

  - name: Assert that the element in array is our value
    assert:
      that: results.result[5] == "--root-ca-file=/etc/k8s/ssl/my.pem"
      msg: "Test: '--root-ca-file=/etc/k8s/ssl/my.pem' != [{{ results.result[5] }}]"
  ###### end test array update modify#####

  ###### test dict create #####
  - name: test dict create
    yedit:
      src: "{{ test_file }}"
      key: a.b.c
      value: d

  - name: retrieve the key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: a.b.c
    register: results

  - name: Assert that the key was created
    assert:
      that: results.result == "d"
      msg: "Test: 'd' != [{{ results.result }}]"
  ###### end test dict create #####

  ###### test create dict value #####
  - name: test create dict value
    yedit:
      src: "{{ test_file }}"
      key: e.f.g
      value:
        h:
          i:
            j: k

  - name: retrieve the key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: e.f.g.h.i.j
    register: results

  - name: Assert that the key was created
    assert:
      that: results.result == "k"
      msg: "Test: 'k' != [{{ results.result }}]"
  ###### end test dict create #####

  ###### test create list value #####
  - name: test create list value
    yedit:
      src: "{{ test_file }}"
      key: z.x.y
      value:
      - 1
      - 2
      - 3

  - name: retrieve the key
    yedit:
      src: "{{ test_file }}"
      state: list
      key: z#x#y
      separator: '#'
    register: results
  - debug: var=results

  - name: Assert that the key was created
    assert:
      that: results.result == [1, 2, 3]
      msg: "Test: '[1, 2, 3]' != [{{ results.result }}]"
###### end test create list value #####