1 from MassPnfSim import MassPnfSim
3 from os import popen, stat
4 from yaml import load, SafeLoader
5 from ipaddress import ip_address
6 from test_settings import *
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
14 @pytest.mark.parametrize("action", ['start', 'stop', 'trigger', 'status', 'stop_simulator'])
15 def test_not_bootstrapped(action, caplog, args_start, args_stop, args_trigger, args_status, args_stop_simulator): # pylint: disable=W0613
17 m = getattr(MassPnfSim(), action)
18 m(eval(f'args_{action}'))
19 except SystemExit as e:
21 assert 'No bootstrapped instance found' in caplog.text
24 def test_bootstrap(args_bootstrap, caplog):
26 MassPnfSim().bootstrap(args_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
32 # Verify bootstrap idempotence
34 MassPnfSim().bootstrap(args_bootstrap)
35 except SystemExit as e:
37 assert 'Bootstrapped instances detected, not overwiriting, clean first' in caplog.text
40 # Verify simulator dirs created
41 sim_dirname_pattern = MassPnfSim().sim_dirname_pattern
42 assert len(glob(f"{sim_dirname_pattern}*")) == SIM_INSTANCES
44 # Verify simulators configs content is valid
46 for instance in range(SIM_INSTANCES):
47 instance_ip_offset = instance * 16
49 with open(f"{sim_dirname_pattern}{instance}/{INSTANCE_CONFIG}") as f:
50 yml = load(f, Loader=SafeLoader)
51 assert URLVES == yml['urlves']
52 assert TYPEFILESERVER == yml['typefileserver']
53 assert f'sftp://{USER}:{PASSWORD}@{IPFILESERVER}:{start_port + 1}' in yml['urlsftp']
54 assert f'ftps://{USER}:{PASSWORD}@{IPFILESERVER}:{start_port + 2}' in yml['urlftps']
55 assert str(ip_address(IPSTART) + ip_offset + instance_ip_offset) == yml['ippnfsim']
57 print(yml['ippnfsim'])
59 # Verify vsftpd config file has proper permissions
60 for cfg in glob(f'{sim_dirname_pattern}*/config/vsftpd_ssl.conf'):
61 assert stat(cfg).st_uid == 0
63 def test_bootstrap_status(args_status, caplog):
64 MassPnfSim().status(args_status)
65 for _ in range(SIM_INSTANCES):
66 assert 'Simulator containers are down' in caplog.text
67 assert 'Simulator response' not in caplog.text
70 def test_start(args_start, caplog):
71 MassPnfSim().start(args_start)
72 for instance in range(SIM_INSTANCES):
73 instance_ip_offset = instance * 16
75 assert f'Starting pnf-sim-lw-{instance} instance:' in caplog.text
76 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
77 assert 'Starting simulator containers' in caplog.text
78 assert f"ROP_file_creator.sh {instance} successfully started" in caplog.text
79 assert f"3GPP measurements file generator for instance {instance} is already running" not in caplog.text
80 # Verify ROP_file_creator.sh running
81 assert f"ROP_file_creator.sh {instance}" in popen('ps afx').read()
84 def test_start_status(args_status, docker_containers, caplog):
85 sleep(5) # Wait for the simulator to settle
86 MassPnfSim().status(args_status)
87 for instance in range(SIM_INSTANCES):
88 assert '"simulatorStatus":"NOT RUNNING"' in caplog.text
89 assert '"simulatorStatus":"RUNNING"' not in caplog.text
90 assert f"{PNF_SIM_CONTAINER_NAME}{instance}" in docker_containers
93 def test_start_idempotence(args_start, caplog):
94 '''Verify start idempotence'''
95 MassPnfSim().start(args_start)
96 assert 'containers are already up' in caplog.text
97 assert 'Starting simulator containers' not in caplog.text
98 assert f"is already running" in caplog.text
101 def test_trigger(args_trigger, caplog):
102 MassPnfSim().trigger(args_trigger)
103 for instance in range(SIM_INSTANCES):
104 instance_ip_offset = instance * 16
106 assert f'Triggering pnf-sim-lw-{instance} instance:' in caplog.text
107 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
108 assert 'Simulator started' in caplog.text
111 def test_trigger_status(args_status, capfd, caplog):
112 MassPnfSim().status(args_status)
113 msg = capfd.readouterr()
114 for _ in range(SIM_INSTANCES):
115 assert '"simulatorStatus":"RUNNING"' in caplog.text
116 assert '"simulatorStatus":"NOT RUNNING"' not in caplog.text
117 assert 'Up' in msg.out
118 assert 'Exit' not in msg.out
121 def test_trigger_idempotence(args_trigger, caplog):
122 MassPnfSim().trigger(args_trigger)
123 assert "Cannot start simulator since it's already running" in caplog.text
124 assert 'Simulator started' not in caplog.text
127 def test_stop_simulator(args_stop_simulator, caplog):
128 MassPnfSim().stop_simulator(args_stop_simulator)
129 for instance in range(SIM_INSTANCES):
130 instance_ip_offset = instance * 16
132 assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
133 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
134 assert "Simulator successfully stopped" in caplog.text
135 assert "not running" not in caplog.text
138 def test_stop_simulator_status(args_status, capfd, caplog):
139 MassPnfSim().status(args_status)
140 msg = capfd.readouterr()
141 for _ in range(SIM_INSTANCES):
142 assert '"simulatorStatus":"RUNNING"' not in caplog.text
143 assert '"simulatorStatus":"NOT RUNNING"' in caplog.text
144 assert 'Up' in msg.out
145 assert 'Exit' not in msg.out
148 def test_stop_simulator_idempotence(args_stop_simulator, caplog):
149 MassPnfSim().stop_simulator(args_stop_simulator)
150 for instance in range(SIM_INSTANCES):
151 instance_ip_offset = instance * 16
153 assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
154 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
155 assert "Cannot stop simulator, because it's not running" in caplog.text
156 assert "Simulator successfully stopped" not in caplog.text
159 def test_trigger_custom(args_trigger_custom, caplog):
160 MassPnfSim().trigger_custom(args_trigger_custom)
161 for instance in range(SIM_INSTANCES):
162 instance_ip_offset = instance * 16
164 assert f'Triggering pnf-sim-lw-{instance} instance:' in caplog.text
165 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
166 assert 'Simulator started' in caplog.text
167 assert "Cannot start simulator since it's already running" not in caplog.text
170 def test_stop(args_stop, caplog):
171 MassPnfSim().stop(args_stop)
172 for instance in range(SIM_INSTANCES):
173 instance_ip_offset = instance * 16
175 assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
176 assert f'PNF-Sim IP: {str(ip_address(IPSTART) + ip_offset + instance_ip_offset)}' in caplog.text
177 assert f'ROP_file_creator.sh {instance} successfully killed' in caplog.text
178 assert f"ROP_file_creator.sh {instance}" not in popen('ps afx').read()
181 def test_stop_status(args_status, docker_containers, caplog):
182 MassPnfSim().status(args_status)
183 for instance in range(SIM_INSTANCES):
184 assert f"{PNF_SIM_CONTAINER_NAME}{instance}" not in docker_containers
185 assert 'Simulator containers are down' in caplog.text
188 def test_stop_idempotence(args_stop, caplog, docker_containers):
189 MassPnfSim().stop(args_stop)
190 for instance in range(SIM_INSTANCES):
191 assert f'Stopping pnf-sim-lw-{instance} instance:' in caplog.text
192 assert f'ROP_file_creator.sh {instance} already not running' in caplog.text
193 assert 'Simulator containers are already down' in caplog.text
194 assert f"ROP_file_creator.sh {instance}" not in popen('ps afx').read()
195 assert f"{PNF_SIM_CONTAINER_NAME}{instance}" not in docker_containers
198 def test_clean(args_clean):
201 assert not glob(f"{m.sim_dirname_pattern}*")