majordome_test.py
6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
import os
import sys
import django
import subprocess
import fileinput
import time
from django.shortcuts import get_object_or_404
from django.conf import settings as djangosettings
import datetime
DEBUG=True
agent="majordome"
fixture = 'tests/majordome_test'
#venv_bin = '../../private/venv_py3_pyros/bin/python'
venv_bin = '../private/venv_py3_pyros/bin/python'
venv_bin2 = '../../private/venv_py3_pyros/bin/python'
def tearDown(self):
os.chdir('..')
self.replacePatternInFile("MAJORDOME_TEST = True", "MAJORDOME_TEST = False", "pyros/settings.py")
os.chdir('majordome')
def replacePatternInFile(pattern, replace, file_path):
with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(pattern, replace), end='')
# Now, check that replacement has been done or ERROR !!!
FOUND=False
#with fileinput.FileInput(file_path) as file:
with open(file_path) as file:
for line in file:
#if replace in line:
if line.startswith(replace):
FOUND = True
break
#if FOUND: print("pattern "+replace+" found in file "+file_path)
if not FOUND: raise(Exception("pattern "+replace+" not found in file "+file_path))
def execProcessAsync(command):
#self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]")
p = subprocess.Popen(command, shell=True)
'''
self.subproc.append((p, command))
self.printFullTerm(Colors.GREEN, "Process launched successfully")
self.addExecuted(self.current_command, command)
'''
return p
def execProcessFromVenv(command: str):
command = venv_bin + ' ' + command
args = command.split()
process = subprocess.Popen(args)
process.wait()
if process.returncode == 0:
print("Process executed successfully")
else:
print("Process execution failed")
return process.returncode
def execProcessFromVenvAsync(command: str):
command = venv_bin2 + ' ' + command
args = command.split()
#self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]")
p = subprocess.Popen(args)
#self.subproc.append((p, ' '.join(args[1:])))
#self.printFullTerm(Colors.GREEN, "Process launched successfully")
#self.addExecuted(self.current_command, str(' '.join(args[1:])))
return p
def init_database():
# Go into src/
#os.chdir('..')
print("Current directory : " + str(os.getcwd()))
execProcessFromVenv("manage.py flush --noinput")
execProcessFromVenv("manage.py makemigrations")
execProcessFromVenv("manage.py migrate")
execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + fixture)
def getConfigFromDB():
return get_object_or_404(Config, id=1)
def changeDirectory(path):
#if DEBUG: print("Moving to : " + path)
os.chdir(path)
if DEBUG: print("NEW Current directory : " + str(os.getcwd()))
def assert_majordome_is(state:str) -> bool:
config = getConfigFromDB()
print()
print("Majordome state should now be", config.pyros_state)
assert config.pyros_state == state
print("=> CHECKED")
print()
print("Current directory : " + str(os.getcwd()))
# Go into src/
os.chdir('..')
replacePatternInFile("MAJORDOME_TEST = False", "MAJORDOME_TEST = True", "pyros/settings.py")
#os.chdir('majordome')
#os.chdir(agent)
init_database()
sys.path.append('..')
#print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings")
django.setup()
# Needs sys.path.append('..')
from common.models import Config, PlcDeviceStatus
config = get_object_or_404(Config, id=1)
print("config id is", config.id)
print("config latitude is", config.latitude)
print("config global mode is", config.global_mode)
print("config row_data_save_frequency is", config.row_data_save_frequency)
#with print_lock:
print("global mode is", getConfigFromDB().global_mode)
#sys.stdout.write("global mode")
#sys.stdout.flush()
print("global mode is", getConfigFromDB().global_mode)
print("DB1 used is:", djangosettings.DATABASES['default']['NAME'])
# Launch the Majordome agent
os.chdir(agent)
#p = execProcessFromVenvAsync(venv_bin + ' start_agent_'+agent+'_from_test.py')
#p = execProcessFromVenvAsync(venv_bin + ' start_agent_'+agent+'.py')
majordome = execProcessFromVenvAsync("start_agent_"+agent+'.py')
#p = self.execProcessFromVenvAsync('./start_agent_'+agent+'.py')
#p.wait()
print("hello1")
print("hello2")
time.sleep(10)
#
# START TESTING Majordome
#
plcstatus = get_object_or_404(PlcDeviceStatus, id=1)
# Now, Majordome should be in state "PASSIVE no PLC" (waiting for PLC)
assert_majordome_is("PASSIVE_NO_PLC")
# Let's pretend that PLC is now OK (via DB)
# ==> check that it goes to state "PASSIVE" (waiting for PLC "AUTO")
#plcstatus.created = "2018-07-24 11:49:49"
# ex: '2018-07-27T10:12:38.875112'
plcstatus.created = datetime.datetime.now().isoformat()
plcstatus.save()
time.sleep(5)
assert_majordome_is("PASSIVE")
# Let's pretend that PLC is now AUTO (via DB)
# ==> check that it goes to state "STANDBY"
plcstatus.plc_mode = "AUTO"
plcstatus.save()
time.sleep(15)
assert_majordome_is("Standby")
# Let's pretend that we are going REMOTE mode
# ==> check that it goes to state "REMOTE"
config.global_mode = False
config.lock = False
config.save()
time.sleep(10)
assert_majordome_is("Remote")
# Let's pretend that PLC is now UNSAFE
# ==> check that shutter is closing
#TODO:
#plc_status.is_safe
# Let's pretend that PLC is now SAFE again
# ==> check that shutter is opening
#TODO:
# Let's pretend that we are going SCHEDULER mode
# ==> check that it goes to state "STANDBY"
config.global_mode = True
config.lock = False
config.save()
time.sleep(10)
assert_majordome_is("Standby")
# Let's set conditions for going to SCHEDULER mode
# ==> check that it goes to state "SCHEDULER"
#TODO:
# self.is_night() and self.plc_is_safe() and self.config.ack and not self.config.lock
# Let's set conditions for leaving the SCHEDULER mode
# ==> check that it goes to state "STANDBY"
#TODO:
#
# KILL agent Majordome (using DB config table)
#
# Let's set conditions for stopping the Majordome
# ==> check that it goes to state config.majordome_state = "RUNNING"
config.majordome_state = "STOP"
config.save()
time.sleep(5)
config = getConfigFromDB()
print("config is", config.majordome_state)
assert config.majordome_state == "RUNNING"
##majordome.kill()
#self.execProcessAsync("ps aux | grep \"start_agent_majordome.py\" | awk '{ print $2 }' | xargs kill")
#self.execProcessAsync("ps aux | grep start_agent_majordome.py | awk '{ print $2 }' | xargs kill")
#'''
print("hello3")
time.sleep(5)
print("hello4")
#thread_majordome.join()
#thread_majordome.killExecutingSequence()
os.chdir('..')
replacePatternInFile("MAJORDOME_TEST = True", "MAJORDOME_TEST = False", "pyros/settings.py")
#os.chdir('majordome')