-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
alternatives: handle absent link, add integration tests #27967
Changes from all commits
fad3966
4d97567
c7157a2
427190c
efbcd5a
ed8827e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
posix/ci/group3 | ||
destructive | ||
needs/root | ||
skip/freebsd | ||
skip/osx |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright (c) 2017 Pierre-Louis Bonicoli <pierre-louis.bonicoli@libregerbil.fr> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
- name: 'setup: create a dummy alternative' | ||
block: | ||
- import_tasks: setup.yml | ||
|
||
############## | ||
# Test parameters: | ||
# link parameter present / absent ('with_link' variable) | ||
# with / without alternatives defined in alternatives file ('with_alternatives' variable) | ||
# auto / manual ('mode' variable) | ||
|
||
- include_tasks: tests.yml | ||
with_nested: | ||
- [ True, False ] # with_link | ||
- [ True, False ] # with_alternatives | ||
- [ 'auto', 'manual' ] # mode | ||
loop_control: | ||
loop_var: test_conf | ||
|
||
########## | ||
# Priority | ||
- block: | ||
- include_tasks: remove_links.yml | ||
- include_tasks: setup_test.yml | ||
# at least two iterations again | ||
- include_tasks: tests_set_priority.yml | ||
with_sequence: start=3 end=4 | ||
vars: | ||
with_alternatives: True | ||
mode: auto | ||
|
||
- block: | ||
- include_tasks: remove_links.yml | ||
- include_tasks: setup_test.yml | ||
# at least two iterations again | ||
- include_tasks: tests_set_priority.yml | ||
with_sequence: start=3 end=4 | ||
vars: | ||
with_alternatives: False | ||
mode: auto | ||
always: | ||
- include_tasks: remove_links.yml | ||
|
||
- file: | ||
path: '{{ item }}' | ||
state: absent | ||
with_items: | ||
- '{{ alternatives_dir }}/dummy' | ||
|
||
- file: | ||
path: '/usr/bin/dummy{{ item }}' | ||
state: absent | ||
with_sequence: start=1 end=4 | ||
# *Disable tests on Fedora 24* | ||
# Shippable Fedora 24 image provides chkconfig-1.7-2.fc24.x86_64 but not the | ||
# latest available version (chkconfig-1.8-1.fc24.x86_64). update-alternatives | ||
# in chkconfig-1.7-2 fails when /etc/alternatives/dummy link is missing, | ||
# error is: 'failed to read link /usr/bin/dummy: No such file or directory'. | ||
# Moreover Fedora 24 is no longer maintained. | ||
when: ansible_distribution != 'Fedora' or ansible_distribution_major_version|int > 24 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- name: remove links | ||
file: | ||
path: '{{ item }}' | ||
state: absent | ||
with_items: | ||
- /etc/alternatives/dummy | ||
- /usr/bin/dummy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
- include_vars: '{{ item }}' | ||
with_first_found: | ||
- files: | ||
- '{{ ansible_os_family }}.yml' | ||
- 'default.yml' | ||
paths: '../vars' | ||
|
||
- template: | ||
src: dummy_command | ||
dest: '/usr/bin/dummy{{ item }}' | ||
owner: root | ||
group: root | ||
mode: 0755 | ||
with_sequence: start=1 end=4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- template: | ||
src: dummy_alternative | ||
dest: '{{ alternatives_dir }}/dummy' | ||
owner: root | ||
group: root | ||
mode: 0644 | ||
when: with_alternatives or ansible_os_family != 'RedHat' | ||
|
||
# update-alternatives included in Fedora 26 (1.10) & Red Hat 7.4 (1.8) segfaults | ||
# when <admindir>/dummy file contains only mode and link. Hence the file is | ||
# deleted instead of containing only mode and link. The file below works fine with | ||
# newer version of update-alternatives: | ||
# """ | ||
# auto | ||
# /usr/bin/dummy | ||
# | ||
# | ||
# """ | ||
- file: | ||
path: '{{ alternatives_dir }}/dummy' | ||
state: absent | ||
when: not with_alternatives and ansible_os_family == 'RedHat' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
- debug: | ||
msg: ' with_alternatives: {{ with_alternatives }}, mode: {{ mode }}' | ||
|
||
- block: | ||
- name: set alternative (using link parameter) | ||
alternatives: | ||
name: dummy | ||
path: '/usr/bin/dummy{{ item }}' | ||
link: '/usr/bin/dummy' | ||
register: alternative | ||
|
||
- name: check expected command was executed | ||
assert: | ||
that: | ||
- 'alternative|success' | ||
- 'alternative|changed' | ||
when: with_link | ||
|
||
- block: | ||
- name: set alternative (without link parameter) | ||
alternatives: | ||
name: dummy | ||
path: '/usr/bin/dummy{{ item }}' | ||
register: alternative | ||
|
||
- name: check expected command was executed | ||
assert: | ||
that: | ||
- 'alternative|success' | ||
- 'alternative|changed' | ||
when: not with_link | ||
|
||
- name: execute dummy command | ||
shell: dummy | ||
register: cmd | ||
|
||
- name: check expected command was executed | ||
assert: | ||
that: | ||
- 'cmd.stdout == "dummy" ~ item' | ||
|
||
- name: 'check mode (manual: alternatives file existed, it has been updated)' | ||
shell: 'head -n1 {{ alternatives_dir }}/dummy | grep "^manual$"' | ||
when: ansible_os_family != 'RedHat' or with_alternatives or item != 1 | ||
|
||
- name: 'check mode (auto: alternatives file didn''t exist, it has been created)' | ||
shell: 'head -n1 {{ alternatives_dir }}/dummy | grep "^auto$"' | ||
when: ansible_os_family == 'RedHat' and not with_alternatives and item == 1 | ||
|
||
- name: check that alternative has been updated | ||
command: "grep -Pzq '/bin/dummy{{ item }}\\n' '{{ alternatives_dir }}/dummy'" | ||
# priority doesn't seem updated | ||
#command: "grep -Pzq '/bin/dummy{{ item }}\\n50' '{{ alternatives_dir }}/dummy'" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
- block: | ||
- include_tasks: remove_links.yml | ||
- include_tasks: setup_test.yml | ||
# at least two iterations: | ||
# - first will use 'link currently absent', | ||
# - second will receive 'link currently points to' | ||
- include_tasks: test.yml | ||
with_sequence: start=1 end=2 | ||
vars: | ||
with_link: '{{ test_conf[0] }}' | ||
with_alternatives: '{{ test_conf[1] }}' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to assign 'mode' here if I am not wrong. However, I am puzzled then on why the test still work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI was successful because tests weren't run: |
||
mode: '{{ test_conf[2] }}' | ||
# update-alternatives included in Fedora 26 (1.10) & Red Hat 7.4 (1.8) doesn't provide | ||
# '--query' switch, 'link' is mandatory for these distributions. | ||
when: ansible_os_family != 'RedHat' or test_conf[0] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
- name: update dummy alternative | ||
alternatives: | ||
name: dummy | ||
path: '/usr/bin/dummy{{ item }}' | ||
link: /usr/bin/dummy | ||
priority: '{{ 60 + item|int }}' | ||
register: alternative | ||
|
||
- name: execute dummy command | ||
shell: dummy | ||
register: cmd | ||
|
||
- name: check if link group is in manual mode | ||
shell: 'head -n1 {{ alternatives_dir }}/dummy | grep "^manual$"' | ||
|
||
- name: check expected command was executed | ||
assert: | ||
that: | ||
- 'alternative|changed' | ||
- 'cmd.stdout == "dummy{{ item }}"' | ||
|
||
- name: check that alternative has been updated | ||
command: "grep -Pzq '/bin/dummy{{ item }}\\n{{ 60 + item|int }}' '{{ alternatives_dir }}/dummy'" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{ mode }} | ||
/usr/bin/dummy | ||
|
||
{% if with_alternatives %} | ||
/usr/bin/dummy1 | ||
40 | ||
/usr/bin/dummy2 | ||
30 | ||
|
||
{% else %} | ||
|
||
{% endif %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
echo dummy{{ item }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
alternatives_dir: /var/lib/dpkg/alternatives/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
alternatives_dir: /var/lib/rpm/alternatives/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
alternatives_dir: /var/lib/alternatives/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be clearer to use a variable rather than '2'