| 1 |
--- |
| 2 |
#################################################################### |
| 3 |
# WARNING: These are designed specifically for Ansible tests # |
| 4 |
# and should not be used as examples of how to write Ansible roles # |
| 5 |
#################################################################### |
| 6 |
|
| 7 |
# test code for the gem module |
| 8 |
# Copyright (c) 2014, James Tanner <[email protected]> |
| 9 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 10 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 11 |
|
| 12 |
- when: |
| 13 |
- not (ansible_facts.os_family == 'Alpine') # TODO |
| 14 |
block: |
| 15 |
|
| 16 |
- include_vars: '{{ item }}' |
| 17 |
with_first_found: |
| 18 |
- files: |
| 19 |
- '{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml' |
| 20 |
- '{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_version }}.yml' |
| 21 |
- '{{ ansible_facts.distribution }}.yml' |
| 22 |
- '{{ ansible_facts.os_family }}.yml' |
| 23 |
- 'default.yml' |
| 24 |
paths: '../vars' |
| 25 |
|
| 26 |
- name: Install dependencies for test |
| 27 |
package: |
| 28 |
name: "{{ item }}" |
| 29 |
state: present |
| 30 |
loop: "{{ test_packages }}" |
| 31 |
when: ansible_facts.distribution != "MacOSX" |
| 32 |
|
| 33 |
# default user_install: skip on RedHat/Fedora where OS-injected --install-dir conflicts |
| 34 |
- when: ansible_facts.os_family != "RedHat" |
| 35 |
block: |
| 36 |
- name: Install a gem (default user_install) |
| 37 |
gem: |
| 38 |
name: gist |
| 39 |
state: present |
| 40 |
register: install_gem_result |
| 41 |
|
| 42 |
- name: List gems |
| 43 |
command: gem list |
| 44 |
register: current_gems |
| 45 |
|
| 46 |
- name: Ensure gem was installed |
| 47 |
assert: |
| 48 |
that: |
| 49 |
- install_gem_result is changed |
| 50 |
- current_gems.stdout is search('gist\s+\([0-9.]+\)') |
| 51 |
|
| 52 |
- name: Remove a gem |
| 53 |
gem: |
| 54 |
name: gist |
| 55 |
state: absent |
| 56 |
register: remove_gem_results |
| 57 |
|
| 58 |
- name: List gems |
| 59 |
command: gem list |
| 60 |
register: current_gems |
| 61 |
|
| 62 |
- name: Verify gem is not installed |
| 63 |
assert: |
| 64 |
that: |
| 65 |
- remove_gem_results is changed |
| 66 |
- current_gems.stdout is not search('gist\s+\([0-9.]+\)') |
| 67 |
|
| 68 |
# install gem in --no-user-install |
| 69 |
- block: |
| 70 |
- name: Install a gem with --no-user-install |
| 71 |
gem: |
| 72 |
name: gist |
| 73 |
state: present |
| 74 |
user_install: false |
| 75 |
register: install_gem_result |
| 76 |
|
| 77 |
- name: List gems |
| 78 |
command: gem list |
| 79 |
register: current_gems |
| 80 |
|
| 81 |
- name: Ensure gem was installed |
| 82 |
assert: |
| 83 |
that: |
| 84 |
- install_gem_result is changed |
| 85 |
- current_gems.stdout is search('gist\s+\([0-9.]+\)') |
| 86 |
|
| 87 |
- name: Remove a gem |
| 88 |
gem: |
| 89 |
name: gist |
| 90 |
state: absent |
| 91 |
user_install: false |
| 92 |
register: remove_gem_results |
| 93 |
|
| 94 |
- name: List gems |
| 95 |
command: gem list |
| 96 |
register: current_gems |
| 97 |
|
| 98 |
- name: Verify gem is not installed |
| 99 |
assert: |
| 100 |
that: |
| 101 |
- remove_gem_results is changed |
| 102 |
- current_gems.stdout is not search('gist\s+\([0-9.]+\)') |
| 103 |
when: ansible_facts.user_uid == 0 |
| 104 |
|
| 105 |
# Check custom gem directory |
| 106 |
- name: Install gem in a custom directory with incorrect options |
| 107 |
gem: |
| 108 |
name: gist |
| 109 |
state: present |
| 110 |
install_dir: "{{ remote_tmp_dir }}/gems" |
| 111 |
ignore_errors: true |
| 112 |
register: install_gem_fail_result |
| 113 |
|
| 114 |
- debug: |
| 115 |
var: install_gem_fail_result |
| 116 |
tags: debug |
| 117 |
|
| 118 |
- name: Ensure previous task failed |
| 119 |
assert: |
| 120 |
that: |
| 121 |
- install_gem_fail_result is failed |
| 122 |
- install_gem_fail_result.msg == 'install_dir requires user_install=false' |
| 123 |
|
| 124 |
- name: Install a gem in a custom directory |
| 125 |
gem: |
| 126 |
name: gist |
| 127 |
state: present |
| 128 |
user_install: false |
| 129 |
install_dir: "{{ remote_tmp_dir }}/gems" |
| 130 |
register: install_gem_result |
| 131 |
|
| 132 |
- name: Find gems in custom directory |
| 133 |
find: |
| 134 |
paths: "{{ remote_tmp_dir }}/gems/gems" |
| 135 |
file_type: directory |
| 136 |
contains: gist |
| 137 |
register: gem_search |
| 138 |
|
| 139 |
- name: Ensure gem was installed in custom directory |
| 140 |
assert: |
| 141 |
that: |
| 142 |
- install_gem_result is changed |
| 143 |
- gem_search.files[0].path is search('gist-[0-9.]+') |
| 144 |
ignore_errors: true |
| 145 |
|
| 146 |
- name: Remove a gem in a custom directory |
| 147 |
gem: |
| 148 |
name: gist |
| 149 |
state: absent |
| 150 |
user_install: false |
| 151 |
install_dir: "{{ remote_tmp_dir }}/gems" |
| 152 |
register: install_gem_result |
| 153 |
|
| 154 |
- name: Find gems in custom directory |
| 155 |
find: |
| 156 |
paths: "{{ remote_tmp_dir }}/gems/gems" |
| 157 |
file_type: directory |
| 158 |
contains: gist |
| 159 |
register: gem_search |
| 160 |
|
| 161 |
- name: Ensure gem was removed in custom directory |
| 162 |
assert: |
| 163 |
that: |
| 164 |
- install_gem_result is changed |
| 165 |
- gem_search.files | length == 0 |
| 166 |
|
| 167 |
# Custom directory for executables (--bindir) |
| 168 |
- name: Install gem with custom bindir |
| 169 |
gem: |
| 170 |
name: gist |
| 171 |
state: present |
| 172 |
bindir: "{{ remote_tmp_dir }}/custom_bindir" |
| 173 |
norc: true |
| 174 |
user_install: false |
| 175 |
register: install_gem_result |
| 176 |
|
| 177 |
- name: Get stats of gem executable |
| 178 |
stat: |
| 179 |
path: "{{ remote_tmp_dir }}/custom_bindir/gist" |
| 180 |
register: gem_bindir_stat |
| 181 |
|
| 182 |
- name: Ensure gem executable was installed in custom directory |
| 183 |
assert: |
| 184 |
that: |
| 185 |
- install_gem_result is changed |
| 186 |
- gem_bindir_stat.stat.exists and gem_bindir_stat.stat.isreg |
| 187 |
|
| 188 |
- name: Remove gem with custom bindir |
| 189 |
gem: |
| 190 |
name: gist |
| 191 |
state: absent |
| 192 |
bindir: "{{ remote_tmp_dir }}/custom_bindir" |
| 193 |
norc: true |
| 194 |
user_install: false |
| 195 |
register: install_gem_result |
| 196 |
|
| 197 |
- name: Get stats of gem executable |
| 198 |
stat: |
| 199 |
path: "{{ remote_tmp_dir }}/custom_bindir/gist" |
| 200 |
register: gem_bindir_stat |
| 201 |
|
| 202 |
- name: Ensure gem executable was removed from custom directory |
| 203 |
assert: |
| 204 |
that: |
| 205 |
- install_gem_result is changed |
| 206 |
- not gem_bindir_stat.stat.exists |
| 207 |
|
| 208 |
# override_platform_install_dir: install and remove using explicit user gem dir |
| 209 |
- name: Install a gem with override_platform_install_dir |
| 210 |
gem: |
| 211 |
name: gist |
| 212 |
state: present |
| 213 |
override_platform_install_dir: true |
| 214 |
register: install_gem_result |
| 215 |
|
| 216 |
- name: List gems |
| 217 |
command: gem list |
| 218 |
register: current_gems |
| 219 |
|
| 220 |
- name: Ensure gem was installed with override_platform_install_dir |
| 221 |
assert: |
| 222 |
that: |
| 223 |
- install_gem_result is changed |
| 224 |
- current_gems.stdout is search('gist\s+\([0-9.]+\)') |
| 225 |
|
| 226 |
- name: Install a gem with override_platform_install_dir (idempotency) |
| 227 |
gem: |
| 228 |
name: gist |
| 229 |
state: present |
| 230 |
override_platform_install_dir: true |
| 231 |
register: install_gem_result |
| 232 |
|
| 233 |
- name: Ensure install is idempotent |
| 234 |
assert: |
| 235 |
that: |
| 236 |
- install_gem_result is not changed |
| 237 |
|
| 238 |
- name: Remove a gem with override_platform_install_dir |
| 239 |
gem: |
| 240 |
name: gist |
| 241 |
state: absent |
| 242 |
override_platform_install_dir: true |
| 243 |
register: remove_gem_result |
| 244 |
|
| 245 |
- name: List gems |
| 246 |
command: gem list |
| 247 |
register: current_gems |
| 248 |
|
| 249 |
- name: Verify gem was removed with override_platform_install_dir |
| 250 |
assert: |
| 251 |
that: |
| 252 |
- remove_gem_result is changed |
| 253 |
- current_gems.stdout is not search('gist\s+\([0-9.]+\)') |
| 254 |
|
| 255 |
- name: Attempt to uninstall default gem 'json' |
| 256 |
community.general.gem: |
| 257 |
name: json |
| 258 |
state: absent |
| 259 |
when: ansible_facts.distribution == "Ubuntu" |
| 260 |
register: gem_result |
| 261 |
ignore_errors: true |
| 262 |
|
| 263 |
- name: Assert gem uninstall failed as expected |
| 264 |
when: ansible_facts.distribution == "Ubuntu" |
| 265 |
assert: |
| 266 |
that: |
| 267 |
- gem_result is failed |
| 268 |
- gem_result.msg.startswith("Failed to uninstall gem 'json'") |