Skip to content

Commit

Permalink
stop relaying on handlers to install global packages, and improve the…
Browse files Browse the repository at this point in the history
… whole process in general. Better testing + include Focal Fossa as a supported target
  • Loading branch information
grzegorznowak committed Jun 3, 2020
1 parent 994073d commit 7d2a146
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 34 deletions.
8 changes: 0 additions & 8 deletions handlers/main.yml

This file was deleted.

1 change: 1 addition & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ galaxy_info:
- trusty
- xenial
- bionic
- focal
- name: Debian
versions:
- stretch
Expand Down
12 changes: 7 additions & 5 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ driver:
lint:
name: yamllint
platforms:
- name: ansible-role-node-test-instance-18-08
- name: ansible-nvm-node-test-20-04
image: ubuntu/focal/amd64
- name: ansible-nvm-node-test-18-08
image: ubuntu/bionic/amd64
- name: ansible-role-node-test-instance-16-04
- name: ansible-nvm-node-test-16-04
image: ubuntu/xenial/amd64
- name: ansible-role-node-test-instance-14-04
- name: ansible-nvm-node-test-14-04
image: ubuntu/trusty/amd64
- name: ansible-role-node-test-instance-buster
- name: ansible-nvm-node-test-buster
image: debian/buster/amd64
- name: ansible-role-node-test-instance-stretch
- name: ansible-nvm-node-test-stretch
image: debian/stretch/amd64
provisioner:
name: ansible
Expand Down
16 changes: 14 additions & 2 deletions molecule/default/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@
yum: name=sudo state=present
when: ansible_os_family == "RedHat"

- name: Crontab for which-ing gulp
cron:
name: "which gulp"
minute: "*"
job: "which gulp > /tmp/which_gulp"

- name: Crontab for which-ing lighthouse
cron:
name: "which lighthouse"
minute: "*"
job: "which lighthouse > /tmp/which_lighthouse"

roles:
- role: ansible-nvm-node
nvm_node_version: "8.11.3"
nvm_install_globally: ['gulp']
nvm_node_version: "10.16.3"
nvm_install_globally: ['gulp', 'lighthouse']
50 changes: 37 additions & 13 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
import os
import time


import testinfra.utils.ansible_runner

# let the which-checking crontab run
# anyone know better way of checking what cron thinks ? Please, do let me know!
time.sleep(61)

# man handle the damn 20.04's sudo bug...
FOCAL_BUG_STR = 'sudo: setrlimit(RLIMIT_CORE): Operation not permitted\n'

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


def test_node_version_ok(host):
cmd = host.run("node -v")
assert cmd.stderr == ''
assert cmd.stdout.rstrip() == 'v8.11.3'
assert cmd.stdout.rstrip() == 'v10.16.3'


def test_global_gulp_version_ok(host):
def test_global_libraries(host):
cmd = host.run("gulp -v")
assert cmd.stderr == ''
assert 'CLI version' in cmd.stdout

cmd = host.run("sudo -u www-data which gulp")
cmd = host.run("lighthouse --version")
assert cmd.stderr == ''
assert cmd.stdout.rstrip() == '/usr/bin/gulp'
assert cmd.stdout != ''

with host.sudo("nvm_tester"):
cmd = host.run("which lighthouse")
assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR
assert cmd.stdout.rstrip() == '/usr/bin/lighthouse'

cmd = host.run("which gulp")
assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR
assert cmd.stdout.rstrip() == '/usr/bin/gulp'


def test_cron_environment_for_global_libs(host):

# make sure cron sees global libraries in correct locations
assert host.run("cat /tmp/which_gulp").stdout.rstrip() == '/usr/bin/gulp'
assert host.run("cat /tmp/which_lighthouse").stdout.rstrip() == '/usr/bin/lighthouse'


def test_user_node_version_ok(host):
with host.sudo("nvm_tester"):
cmd = host.run("node -v")
assert cmd.stderr == '' or cmd.stderr == FOCAL_BUG_STR
assert cmd.stdout.rstrip() == 'v10.16.3'


def test_npm_ok(host):
cmd = host.run("npm -v")
assert cmd.stdout.rstrip() == '5.6.0'
assert cmd.stdout.rstrip() == '6.9.0'


def test_user_npm_ok(host):
with host.sudo("nvm_tester"):
host.check_output("whoami")
cmd = host.run("npm -v")
assert cmd.stdout.rstrip() == '5.6.0'


def test_user_node_version_ok(host):
with host.sudo("nvm_tester"):
cmd = host.run("node -v")
assert cmd.stderr == ''
assert cmd.stdout.rstrip() == 'v8.11.3'
assert cmd.stdout.rstrip() == '6.9.0'
19 changes: 13 additions & 6 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
- name: Create the target VNM dir
file: path="{{ nvm_dir }}" state=directory owner="{{ nvm_user_name }}" mode="o+rwx"

# variablize the name of the installation script so it fire handlers properly when
# variablize the name of the installation script so it fire installation script properly when
# updating from version to version
- name: Copy over nvm installation script
template: src="install_nvm.script.j2" dest="{{ nvm_dir }}/install_nvm_{{ nvm_node_version }}.script"
mode="u+x,g+x" owner="{{ nvm_user_name }}"
register: "nvm_installation_script_changed"
register: "nvm_installation_script_template"

- include: handlers/main.yml
when: nvm_installation_script_changed is defined and nvm_installation_script_changed.changed
- name: Execute the nvm installation script
command: "{{ nvm_dir }}/install_nvm_{{ nvm_node_version }}.script"
when: nvm_installation_script_template is defined and nvm_installation_script_template.changed
# the tag is needed so that linter wouldn't complain about this not being handler,
# and handlers are so unstable I eventually decided not to use them in this role
tags: skip_ansible_lint

- name: Add the NVM's variables to all the profiles in the system (Debian)
blockinfile: |
Expand All @@ -44,8 +48,11 @@
file: src="{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm"
dest="/usr/bin/npm" state=link mode="u+rwx,g+rx,o+rx"

- name: We love handlers
meta: flush_handlers
- name: Install global packages
shell:
cmd: ". /root/.bashrc && {{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/npm install --global {{ item }}"
creates: "{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/{{ item }}"
loop: "{{ nvm_install_globally }}"

- name: Symlink global packages into PATH for specific environments (like cron's) to be able to access them.
file: src="{{ nvm_dir }}/versions/node/v{{ nvm_node_version }}/bin/{{ item }}"
Expand Down

0 comments on commit 7d2a146

Please sign in to comment.