-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for mongodb_version type cast issue and tests (#689)
mongodb_mongod and mongodb_config roles.
- Loading branch information
1 parent
683bccc
commit 616e3e9
Showing
7 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
roles/mongodb_mongod/molecule/mongodb_version_float/converge.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
1 change: 1 addition & 0 deletions
1
roles/mongodb_mongod/molecule/mongodb_version_float/molecule.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../mongodb_install/molecule/default/molecule.yml |
36 changes: 36 additions & 0 deletions
36
roles/mongodb_mongod/molecule/mongodb_version_float/prepare.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
109 changes: 109 additions & 0 deletions
109
roles/mongodb_mongod/molecule/mongodb_version_float/tests/test_default.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters