Add functional tests for 'trigger' command
[integration.git] / test / mocks / mass-pnf-sim / test_lifecycle.py
1 from MassPnfSim import MassPnfSim
2 from glob import glob
3 from os import popen
4 from yaml import load, SafeLoader
5 from ipaddress import ip_address
6 from test_settings import *
7 import pytest
8 from time import sleep
9
10 # These test routines perform functional testing in current file tree context
11 # thus they require that no simulator instances are bootstrapped and running
12 # prior to running tests
13
14 @pytest.mark.parametrize("action", ['start', 'stop', 'trigger', 'status'])
15 def test_not_bootstrapped(action, caplog, args_start, args_stop, args_trigger, args_status): # pylint: disable=W0613
16     try:
17         m = getattr(MassPnfSim(eval(f'args_{action}')), action)
18         m()
19     except SystemExit as e:
20         assert e.code == 1
21     assert 'No bootstrapped instance found' in caplog.text
22     caplog.clear()
23
24 def test_bootstrap(args_bootstrap, parser, caplog):
25     # Initial bootstrap
26     MassPnfSim(args_bootstrap).bootstrap()
27     for instance in range(SIM_INSTANCES):
28         assert f'Creating pnf-sim-lw-{instance}' in caplog.text
29         assert f'Done setting up instance #{instance}' in caplog.text
30     caplog.clear()
31
32     # Verify bootstrap idempotence
33     try:
34         MassPnfSim(args_bootstrap).bootstrap()
35     except SystemExit as e:
36         assert e.code == 1
37     assert 'Bootstrapped instances detected, not overwiriting, clean first' in caplog.text
38     caplog.clear()
39
40     # Verify simulator dirs created
41     sim_dirname_pattern = MassPnfSim(parser.parse_args([])).sim_dirname_pattern
42     assert len(glob(f"{sim_dirname_pattern}*")) == SIM_INSTANCES
43
44     # Verify ROP_file_creator.sh running
45     for instance in range(SIM_INSTANCES):
46         assert f"ROP_file_creator.sh {instance}" in popen('ps afx').read()
47
48     # Verify simulators configs content is valid
49     start_port = 2000
50     for instance in range(SIM_INSTANCES):
51         instance_ip_offset = instance * 16
52         ip_offset = 2
53         with open(f"{sim_dirname_pattern}{instance}/{INSTANCE_CONFIG}") as f:
54             yml = load(f, Loader=SafeLoader)
55         assert URLVES == yml['urlves']
56         assert TYPEFILESERVER == yml['typefileserver']
57         assert f'sftp://onap:pano@{IPFILESERVER}:{start_port + 1}' in yml['urlsftp']
58         assert f'ftps://onap:pano@{IPFILESERVER}:{start_port + 2}' in yml['urlftps']
59         assert str(ip_address(IPSTART) + ip_offset + instance_ip_offset) == yml['ippnfsim']
60         start_port += 2
61         print(yml['ippnfsim'])
62
63 def test_start(args_start, caplog, capfd):
64     MassPnfSim(args_start).start()
65     msg = capfd.readouterr()
66     for instance in range(SIM_INSTANCES):
67         instance_ip_offset = instance * 16
68         ip_offset = 2
69         assert f'Starting pnf-sim-lw-{instance} instance:' in caplog.text
70         assert f'PNF-Sim IP:  {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in msg.out
71         assert 'Starting simulator containers' in msg.out
72     caplog.clear()
73
74 def test_start_idempotence(args_start, capfd):
75     '''Verify start idempotence'''
76     MassPnfSim(args_start).start()
77     msg = capfd.readouterr()
78     assert 'Simulator containers are already up' in msg.out
79     assert 'Starting simulator containers' not in msg.out
80
81 def test_trigger(args_trigger, caplog, capfd):
82     sleep(5) # Wait for the simulator to settle
83     MassPnfSim(args_trigger).trigger()
84     msg = capfd.readouterr()
85     for instance in range(SIM_INSTANCES):
86         instance_ip_offset = instance * 16
87         ip_offset = 2
88         assert f'Triggering pnf-sim-lw-{instance} instance:' in caplog.text
89         assert f'PNF-Sim IP:  {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in msg.out
90         assert 'Simulator started' in msg.out
91
92 def test_trigger_idempotence(args_trigger, capfd):
93     MassPnfSim(args_trigger).trigger()
94     msg = capfd.readouterr()
95     assert "Cannot start simulator since it's already running" in msg.out
96     assert 'Simulator started' not in msg.out