Debian Codegraph @

ansible_collections/community/mongodb/roles/mongodb_config/molecule/default/tests/test_default.py

raw
1 import os
2
3 import testinfra.utils.ansible_runner
4
5 testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
6 os.environ['MOLECULE_INVENTORY_FILE']
7 ).get_hosts('all')
8
9
10 def include_vars(host):
11 current_dir = os.path.dirname(os.path.abspath(__file__))
12 path_components = current_dir.split(os.sep)
13 trim_count = 0
14 # Weird bug where the path of this file is incorrect
15 # It seems the ansible module, at least when used here
16 # Used to run in a different directory, meaning the
17 # relative path was not correct. This method should mean it's
18 # always correct whatever the context.
19 for component in path_components:
20 if component.startswith("mongodb_"):
21 break
22 else:
23 trim_count += 1
24 trim_count = (len(path_components) - 1) - trim_count
25 # Trim off the dirs after the role dir
26 trimmed_components = path_components[:-trim_count]
27 trimmed_path = os.sep.join(trimmed_components)
28
29 if host.system_info.distribution == "debian" \
30 or host.system_info.distribution == "ubuntu":
31 vars_file_path = os.path.join(trimmed_path, 'vars', 'Debian.yml')
32 ansible = host.ansible('include_vars',
33 f'file="{vars_file_path}"',
34 False,
35 False)
36 else:
37 vars_file_path = os.path.join(trimmed_path, 'vars', 'RedHat.yml')
38 ansible = host.ansible('include_vars',
39 f'file="{vars_file_path}"',
40 False,
41 False)
42 return ansible
43
44
45 def test_mongod_cnf_file(host):
46 mongodb_user = include_vars(host)['ansible_facts']['mongodb_user']
47 mongodb_group = include_vars(host)['ansible_facts']['mongodb_group']
48 f = host.file('/etc/mongod.conf')
49
50 assert f.exists
51 assert f.user == mongodb_user
52 assert f.group == mongodb_group
53
54
55 def test_mongod_service(host):
56 mongod_service = include_vars(host)['ansible_facts']['mongod_service']
57 s = host.service(mongod_service)
58
59 assert s.is_running
60 assert s.is_enabled
61
62
63 def test_mongod_port(host):
64 port = include_vars(host)['ansible_facts']['config_port']
65 s = host.socket("tcp://0.0.0.0:{0}".format(port))
66
67 assert s.is_listening
68
69
70 def test_mongod_replicaset(host):
71 '''
72 Ensure that the MongoDB config replicaset has been created successfully
73 '''
74 port = include_vars(host)['ansible_facts']['config_port']
75 cmd = "mongosh --port {0} --eval 'rs.status()'".format(port)
76 # We only want to run this once
77 if host.ansible.get_variables()['inventory_hostname'] == "ubuntu2204":
78 r = host.run(cmd)
79
80 assert "cfg" in r.stdout
81 assert "amazon2023:{0}".format(port) in r.stdout
82 assert "debian12:{0}".format(port) in r.stdout
83 assert "ubuntu2204:{0}".format(port) in r.stdout
84 assert "almalinux9:{0}".format(port) in r.stdout
85 assert "rockylinux9:{0}".format(port) in r.stdout
86
87
88 def test_mongod_config_default_path(host):
89 '''
90 Ensure that the default paths for RedHat and Debian based OSes are respected
91 '''
92 hostname = host.ansible.get_variables()['inventory_hostname']
93 default_path = "/var/lib/mongo"
94 if hostname.startswith('centos'):
95 default_path = "/var/lib/mongo"
96 elif hostname.startswith('ubuntu') or hostname.startswith('debian'):
97 default_path = "/var/lib/mongodb"
98
99 # assert path exists
100 f = host.file(default_path)
101 assert f.exists
102 assert f.is_directory
103 # asset mongodb.cnf contains path
104 conf = host.file('/etc/mongod.conf').content_string
105 assert "dbPath: {0}".format(default_path) in conf