ansible/test/integration/targets/gitlab_project_variable/tasks/main.yml
Markus Bergholz 18aae0a02b add new module with integration tests to manage gitlab project variables (#56574)
* add new module with integration tests to manage gitlab project variables

* fix invalid yaml in DOCUMENTATION variable and don't import * from ansible module

* remove extends_documentation_fragment and put imports after DOCUMENTATION/EXAMPLES/RETURN/ANSIBLE_METADATA

* fix author in documentation and remove import from display

* add alias file for integration test

* split long lines and try to fix the author key

remove tailing whitespace

* replace email address with github username

* adding the at style to username

* add metaclass and future import

* add state variable to be able to delete selected variables

* add test with state = absent

* update documentation. scheme is necessary

* use singular in exmaple section

* use key purge instead of purge_vars

use purge instead of purge_vars also in the integration test

* create gitlab object in the ansible main function

* remove usedless .format

* follow best practice fail message

* add return documentation, return information about which variables were added, updated or removed and catch gitlab api auth error

* use module_utils.api with api_url and api_token

* use dict instead of list for vars

* use project name instead of name as playbook key

* add ansible checkmode_support, reduce variables in gitlab_project_variables class, remove wrong/duplicated HAS_GITLAB_PACKAGE check

* use extends_documentation_fragment and don't pop elements from basic_auth_argument_spec

* use just project_variable as output variable

* update mutually_exclusive as suggested

* re-add api_token documentation, because it is not included in api basic auth

* remove useless statement

remove unnecessary if

* add one test with a changing value

* put type at first position

* keep item to reduce api calls, build array and keep indexes by replacing with None instead of poping

* more asserts

* Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

* remove unused return key from documentation

msg is only returned when failed

* Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

* remove error key, because it is not returned

* change also documentation from purged_vars to purge

* Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update test/integration/targets/gitlab_project_variable/tasks/main.yml

Co-Authored-By: Felix Fontein <felix@fontein.de>

Update test/integration/targets/gitlab_project_variable/tasks/main.yml

Co-Authored-By: Felix Fontein <felix@fontein.de>

* remove extra spaces

fix wrong spelling

* expand return value documentation with examples

* add check_mode test

reorder tests. first the check_mode test, later all other tests

* Update lib/ansible/modules/source_control/gitlab_project_variable.py

Co-Authored-By: Felix Fontein <felix@fontein.de>

* fix existing keys in 'present' array

rework key handling (reduce code)

fix integration tests

use untouched instead of present to identify unchanged variable keys

fix wrong replacement

minor fixes on request

set aliases to unsupported, because the test succeed

remove posix group1 because it conflicts with unsupported

remove useless item from aliases

* rework gitlab connection
2019-08-02 09:08:37 +02:00

250 lines
8.5 KiB
YAML

- name: Install required libs
pip:
name: python-gitlab
state: present
- name: purge all variables for check_mode test
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
purge: True
- name: add a variable value in check_mode
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: checkmode
check_mode: yes
register: gitlab_project_variable_state
- name: check_mode state must be changed
assert:
that:
- gitlab_project_variable_state is changed
- name: apply add value from check_mode test
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: checkmode
register: gitlab_project_variable_state
- name: state must be changed
assert:
that:
- gitlab_project_variable_state is changed
- name: change a variable value in check_mode again
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: checkmode
check_mode: yes
register: gitlab_project_variable_state
- name: check_mode state must not be changed
assert:
that:
- gitlab_project_variable_state is not changed
- name: apply again the value change from check_mode test
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: checkmode
register: gitlab_project_variable_state
- name: state must not be changed
assert:
that:
- gitlab_project_variable_state is not changed
- name: purge all variables at the beginning
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
purge: True
- name: set two test variables
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: abc123
SECRET_ACCESS_KEY: 321cba
register: gitlab_project_variable_state
- name: set two test variables state must be changed
assert:
that:
- gitlab_project_variable_state is changed
- gitlab_project_variable_state.project_variable.added|length == 2
- gitlab_project_variable_state.project_variable.untouched|length == 0
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0
- name: re-set two test variables
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: abc123
SECRET_ACCESS_KEY: 321cba
register: gitlab_project_variable_state
- name: re-set two test variables state must not be changed
assert:
that:
- gitlab_project_variable_state is not changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 2
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0
- name: edit one variable
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: changed
purge: False
register: gitlab_project_variable_state
- name: edit one variable state must be changed
assert:
that:
- gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 1
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 1
- gitlab_project_variable_state.project_variable.updated[0] == "ACCESS_KEY_ID"
- name: append one variable
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
some: value
purge: False
register: gitlab_project_variable_state
- name: append one variable state must be changed
assert:
that:
- gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 1
- gitlab_project_variable_state.project_variable.untouched|length == 2
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0
- gitlab_project_variable_state.project_variable.added[0] == "some"
- name: re-set all variables
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID: changed
SECRET_ACCESS_KEY: 321cba
some: value
register: gitlab_project_variable_state
- name: re-set all variables state must not be changed
assert:
that:
- not gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 3
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0
- name: set one variables and purge all others
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
some: value
purge: True
register: gitlab_project_variable_state
- name: set one variables and purge all others state must be changed
assert:
that:
- gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 1
- gitlab_project_variable_state.project_variable.removed|length == 2
- gitlab_project_variable_state.project_variable.updated|length == 0
- name: only one variable is left
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
some: value
purge: False
register: gitlab_project_variable_state
- name: only one variable is left state must not be changed
assert:
that:
- not gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 1
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0
- gitlab_project_variable_state.project_variable.untouched[0] == "some"
- name: delete the last left variable
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
state: absent
vars:
some: value
register: gitlab_project_variable_state
- name: no variable is left state must be changed
assert:
that:
- gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 0
- gitlab_project_variable_state.project_variable.removed|length == 1
- gitlab_project_variable_state.project_variable.updated|length == 0
- gitlab_project_variable_state.project_variable.removed[0] == "some"
- name: check that no variables are left
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
purge: True
register: gitlab_project_variable_state
- name: check that no variables are untoucheded state must be changed
assert:
that:
- not gitlab_project_variable_state.changed
- gitlab_project_variable_state.project_variable.added|length == 0
- gitlab_project_variable_state.project_variable.untouched|length == 0
- gitlab_project_variable_state.project_variable.removed|length == 0
- gitlab_project_variable_state.project_variable.updated|length == 0