Skip to content
This repository was archived by the owner on Jan 1, 2024. It is now read-only.

Commit 4dbe3dc

Browse files
authored
Restructure tests (#276)
1 parent 2c8e55a commit 4dbe3dc

File tree

13 files changed

+95
-86
lines changed

13 files changed

+95
-86
lines changed

filter_plugins/filters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22

3+
34
def path_join(parts):
45
return os.path.join(*parts)
56

7+
68
class FilterModule(object):
79
def filters(self):
810
return {

molecule/common/tests/test_cluster.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
)
1414
testinfra_hosts = ansible_runner.get_hosts('all')
1515

16+
scenario_name = os.environ['MOLECULE_SCENARIO_NAME']
17+
1618
APP_NAME = 'myapp'
17-
HOSTS_PATH = os.path.join('molecule', 'default', 'hosts.yml')
19+
HOSTS_PATH = os.path.join('molecule', scenario_name, 'hosts.yml')
1820

1921
inventory = InventoryManager(loader=DataLoader(), sources=HOSTS_PATH)
2022
variable_manager = VariableManager(loader=DataLoader(), inventory=inventory)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import os
2+
3+
import testinfra.utils.ansible_runner
4+
import requests
5+
6+
from ansible.inventory.manager import InventoryManager
7+
from ansible.vars.manager import VariableManager
8+
from ansible.parsing.dataloader import DataLoader
9+
10+
ansible_runner = testinfra.utils.ansible_runner.AnsibleRunner(
11+
os.environ['MOLECULE_INVENTORY_FILE']
12+
)
13+
testinfra_hosts = ansible_runner.get_hosts('all')
14+
15+
scenario_name = os.environ['MOLECULE_SCENARIO_NAME']
16+
17+
APP_NAME = 'myapp'
18+
HOSTS_PATH = os.path.join('molecule', scenario_name, 'hosts.yml')
19+
20+
inventory = InventoryManager(loader=DataLoader(), sources=HOSTS_PATH)
21+
variable_manager = VariableManager(loader=DataLoader(), inventory=inventory)
22+
23+
cluster_cookie = inventory.groups['cluster'].get_vars()['cartridge_cluster_cookie']
24+
25+
__authorized_session = None
26+
__configured_instances = None
27+
28+
29+
def get_authorized_session(cluster_cookie):
30+
global __authorized_session
31+
if __authorized_session is None:
32+
__authorized_session = requests.Session()
33+
__authorized_session.auth = ('admin', cluster_cookie)
34+
35+
return __authorized_session
36+
37+
38+
def get_configured_instances():
39+
global __configured_instances
40+
if __configured_instances is None:
41+
__configured_instances = {
42+
inventory.hosts[i].get_vars()['inventory_hostname']: inventory.hosts[i].get_vars()
43+
for i in inventory.hosts
44+
}
45+
return __configured_instances
46+
47+
48+
def get_any_instance_http_port(instances):
49+
for _, instance_vars in instances.items():
50+
return instance_vars['config']['http_port']
51+
assert False
52+
53+
54+
def get_admin_api_url(instances):
55+
admin_url = 'http://localhost:{}'.format(get_any_instance_http_port(instances))
56+
admin_api_url = '{}/admin/api'.format(
57+
admin_url
58+
)
59+
60+
return admin_api_url
61+
62+
63+
def test_cluster_is_healthy():
64+
configured_instances = get_configured_instances()
65+
66+
# Select one instance to be control
67+
admin_api_url = get_admin_api_url(configured_instances)
68+
69+
# Get all started instances
70+
query = '''
71+
query {
72+
replicasets {
73+
status
74+
}
75+
}
76+
'''
77+
session = get_authorized_session(cluster_cookie)
78+
response = session.post(admin_api_url, json={'query': query})
79+
assert response.status_code == 200
80+
81+
replicasets = response.json()['data']['replicasets']
82+
assert all([r['status'] == 'healthy' for r in replicasets])

molecule/default/molecule.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ platforms:
5959
lint: |
6060
set -xe
6161
yamllint .
62-
flake8 library molecule/default/tests
62+
flake8
6363
6464
provisioner:
6565
name: ansible

molecule/default/tests

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../common/tests/test_cluster.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../common/tests/test_cluster_is_healthy.py

molecule/tasks_from/molecule.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ platforms:
2222
lint: |
2323
set -xe
2424
yamllint .
25-
flake8 library molecule/default/tests
25+
flake8
2626
2727
provisioner:
2828
name: ansible

molecule/tasks_from/tests

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../common/tests/test_cluster.py

0 commit comments

Comments
 (0)