Debian Codegraph @

ansible_collections/community/mongodb/roles/mongodb_mongod/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 try:
65 port = include_vars(host)['ansible_facts']['mongod_port']
66 except KeyError:
67 port = 27017
68 s = host.socket("tcp://0.0.0.0:{0}".format(port))
69 assert s.is_listening
70
71
72 def test_mongod_replicaset(host):
73 '''
74 Ensure that the MongoDB replicaset has been created successfully
75 '''
76 try:
77 port = include_vars(host)['ansible_facts']['mongod_port']
78 except KeyError:
79 port = 27017
80 cmd = "mongosh --port {0} --eval 'rs.status()'".format(port)
81 # We only want to run this once
82 if host.ansible.get_variables()['inventory_hostname'] == "ubuntu2204":
83 r = host.run(cmd)
84 assert "rs0" in r.stdout
85 assert "amazon2023:{0}".format(port) in r.stdout
86 assert "debian12:{0}".format(port) in r.stdout
87 assert "ubuntu2204:{0}".format(port) in r.stdout
88 assert "almalinux9:{0}".format(port) in r.stdout
89 assert "rockylinux9:{0}".format(port) in r.stdout
90
91
92 def test_mongod_default_path(host):
93 '''
94 Ensure that the default paths for RedHat and Debian based OSes are respected
95 '''
96 hostname = host.ansible.get_variables()['inventory_hostname']
97 default_path = "/var/lib/mongo"
98 if hostname.startswith('centos'):
99 default_path = "/var/lib/mongo"
100 elif hostname.startswith('ubuntu') or hostname.startswith('debian'):
101 default_path = "/var/lib/mongodb"
102
103 # assert path exists
104 f = host.file(default_path)
105 assert f.exists
106 assert f.is_directory
107 # asset mongodb.cnf contains path
108 conf = host.file('/etc/mongod.conf').content_string
109 assert "dbPath: {0}".format(default_path) in conf