Skip to content

Commit

Permalink
fix for mongodb_version type cast issue and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysmeister committed Jan 15, 2025
1 parent 683bccc commit 89a4dbe
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ jobs:
molecule test --scenario-name custom_db_path
if: ${{ matrix.mongodb_role == 'mongodb_config' }}
working-directory: ansible_collections/community/mongodb/roles/mongodb_config

- name: Run molecule tests for an extra mongodb_mongod scenario
run: |
molecule test --scenario-name mongodb_version_float
if: ${{ matrix.mongodb_role == 'mongodb_mongod' }}
working-directory: ansible_collections/community/mongodb/roles/mongodb_mongod
2 changes: 1 addition & 1 deletion roles/mongodb_config/templates/configsrv.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ systemLog:
# Where and how to store data.
storage:
dbPath: {{ db_path }}
{% if mongodb_version < '6.1' %}
{% if mongodb_version | float < 6.1 %}
journal:
enabled: true
{% endif %}
Expand Down
43 changes: 43 additions & 0 deletions roles/mongodb_mongod/molecule/mongodb_version_float/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
- name: Converge
hosts: all
roles:
- role: mongodb_repository
tags: molecule-idempotence-notest
- role: mongodb_mongod
mongodb_version: 7.1 # test role with a float version

tasks:

- name: Install pymongo
pip:
name: "{{ 'pymongo==3.12.*' if ansible_python_version[0:3] | float < 3.6 else 'pymongo' }}"
when: ansible_hostname == "ubuntu2204"

- name: Determine openssl version
command: openssl version
changed_when: false
register: openssl

- name: Set mongosh package version
set_fact:
mongosh_package: "{{ 'mongodb-mongosh-shared-openssl3' if openssl.stdout.startswith('OpenSSL 3') else 'mongodb-mongosh-shared-openssl11' }}"
when: mongosh_package is not defined

- name: Install MongoDB Shell
package:
name:
- "{{ mongosh_package }}"

- name: Initialise replicaset
community.mongodb.mongodb_replicaset:
login_host: localhost
replica_set: rs0
validate: no
members:
- amazon2023:27017
- debian12:27017
- ubuntu2204:27017
- almalinux9:27017
- rockylinux9:27017
when: ansible_hostname == "ubuntu2204"
36 changes: 36 additions & 0 deletions roles/mongodb_mongod/molecule/mongodb_version_float/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- name: Prepare
hosts: all
become: yes
vars:
redhat_packages:
- net-tools
debian_packages:
- net-tools
tasks:

- name: Ensure epel is available
ansible.builtin.package:
name: epel-release
state: present
when:
- ansible_os_family == "RedHat"
- ansible_distribution != "Amazon"
- ansible_distribution != "Fedora"

- name: Install redhat packages
ansible.builtin.package:
name: "{{ redhat_packages }}"
state: present
when: ansible_os_family == "RedHat"

- name: Update the APT cache on Debian-based systems
apt:
update_cache: yes
when: ansible_os_family == "Debian"

- name: Install Debian packages
ansible.builtin.package:
name: "{{ debian_packages }}"
state: present
when: ansible_os_family == "Debian"
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os

import testinfra.utils.ansible_runner

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


def include_vars(host):
current_dir = os.path.dirname(os.path.abspath(__file__))
path_components = current_dir.split(os.sep)
trim_count = 0
# Weird bug where the path of this file is incorrect
# It seems the ansible module, at least when used here
# Used to run in a different directory, meaning the
# relative path was not correct. This method should mean it's
# always correct whatever the context.
for component in path_components:
if component.startswith("mongodb_"):
break
else:
trim_count += 1
trim_count = (len(path_components) - 1) - trim_count
# Trim off the dirs after the role dir
trimmed_components = path_components[:-trim_count]
trimmed_path = os.sep.join(trimmed_components)

if host.system_info.distribution == "debian" \
or host.system_info.distribution == "ubuntu":
vars_file_path = os.path.join(trimmed_path, 'vars', 'Debian.yml')
ansible = host.ansible('include_vars',
f'file="{vars_file_path}"',
False,
False)
else:
vars_file_path = os.path.join(trimmed_path, 'vars', 'RedHat.yml')
ansible = host.ansible('include_vars',
f'file="{vars_file_path}"',
False,
False)
return ansible


def test_mongod_cnf_file(host):
mongodb_user = include_vars(host)['ansible_facts']['mongodb_user']
mongodb_group = include_vars(host)['ansible_facts']['mongodb_group']
f = host.file('/etc/mongod.conf')

assert f.exists
assert f.user == mongodb_user
assert f.group == mongodb_group


def test_mongod_service(host):
mongod_service = include_vars(host)['ansible_facts']['mongod_service']
s = host.service(mongod_service)

assert s.is_running
assert s.is_enabled


def test_mongod_port(host):
try:
port = include_vars(host)['ansible_facts']['mongod_port']
except KeyError:
port = 27017
s = host.socket("tcp://0.0.0.0:{0}".format(port))
assert s.is_listening


def test_mongod_replicaset(host):
'''
Ensure that the MongoDB replicaset has been created successfully
'''
try:
port = include_vars(host)['ansible_facts']['mongod_port']
except KeyError:
port = 27017
cmd = "mongosh --port {0} --eval 'rs.status()'".format(port)
# We only want to run this once
if host.ansible.get_variables()['inventory_hostname'] == "ubuntu2204":
r = host.run(cmd)
assert "rs0" in r.stdout
assert "amazon2023:{0}".format(port) in r.stdout
assert "debian12:{0}".format(port) in r.stdout
assert "ubuntu2204:{0}".format(port) in r.stdout
assert "almalinux9:{0}".format(port) in r.stdout
assert "rockylinux9:{0}".format(port) in r.stdout


def test_mongod_default_path(host):
'''
Ensure that the default paths for RedHat and Debian based OSes are respected
'''
hostname = host.ansible.get_variables()['inventory_hostname']
default_path = "/var/lib/mongo"
if hostname.startswith('centos'):
default_path = "/var/lib/mongo"
elif hostname.startswith('ubuntu') or hostname.startswith('debian'):
default_path = "/var/lib/mongodb"

# assert path exists
f = host.file(default_path)
assert f.exists
assert f.is_directory
# asset mongodb.cnf contains path
conf = host.file('/etc/mongod.conf').content_string
assert "dbPath: {0}".format(default_path) in conf
2 changes: 1 addition & 1 deletion roles/mongodb_mongod/templates/mongod.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ systemLog:
# Where and how to store data.
storage:
dbPath: {{ db_path }}
{% if mongodb_version < '6.1' %}
{% if mongodb_version | float < 6.1 %}
journal:
enabled: true
{% endif %}
Expand Down

0 comments on commit 89a4dbe

Please sign in to comment.