Debian Codegraph @

ansible_collections/community/mongodb/roles/mongodb_mongos/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 if host.ansible.get_variables()['inventory_hostname'] != 'config1':
47 mongodb_user = include_vars(host)['ansible_facts']['mongodb_user']
48 mongodb_group = include_vars(host)['ansible_facts']['mongodb_group']
49 f = host.file('/etc/mongos.conf')
50
51 assert f.exists
52 assert f.user == mongodb_user
53 assert f.group == mongodb_group
54
55
56 def test_mongod_service(host):
57
58 if host.ansible.get_variables()['inventory_hostname'] != 'config1':
59 mongos_service = include_vars(host)['ansible_facts']['mongos_service']
60 s = host.service(mongos_service)
61
62 assert s.is_running
63 assert s.is_enabled
64
65
66 def test_mongod_port(host):
67 if host.ansible.get_variables()['inventory_hostname'] != 'config1':
68 port = include_vars(host)['ansible_facts']['mongos_port']
69 s = host.socket("tcp://0.0.0.0:{0}".format(port))
70
71 assert s.is_listening
72
73
74 def test_mongos_shell_connectivity(host):
75 '''
76 Tests that we can connect to mongos via the shell annd run a cmd
77 '''
78 if host.ansible.get_variables()['inventory_hostname'] != 'config1':
79 port = include_vars(host)['ansible_facts']['mongos_port']
80 cmd = host.run("mongosh admin --username admin --password admin --port {0} --eval 'db.runCommand({{listDatabases: 1}})'".format(port))
81
82 assert cmd.rc == 0
83 assert "config" in cmd.stdout
84 assert "admin" in cmd.stdout