ya2 · news · projects · code · about

fixes for building
[pmachines.git] / tests / test_functional.py
CommitLineData
0f9675ba 1from pathlib import Path
8ce16d6c 2from datetime import datetime
0f9675ba
FC
3from itertools import product
4from logging import info
5import sys
6if '' in sys.path: sys.path.remove('')
7sys.path.append(str(Path(__file__).parent.parent.parent))
8from unittest import TestCase
8ce16d6c 9from shutil import rmtree, copy
0f9675ba
FC
10from time import sleep
11from os import system, remove, environ
12from os.path import exists, basename, join
13from glob import glob
a33967c7 14from subprocess import PIPE, run, Popen
0f9675ba 15from panda3d.core import Filename
ea71df6e 16from ya2.build.build import exec_cmd, _branch, _version
0f9675ba
FC
17
18
19class FunctionalTests(TestCase):
20
21 def __clean(self):
22 paths = [
23 'tests/functional',
edeef6f9
FC
24 str(Path.home()) + '/.local/share/pmachines/tests/functional/',
25 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/',
26 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/']
0f9675ba
FC
27 for path in paths:
28 rmtree(path, ignore_errors=True)
29 dirs = [
30 Filename().get_user_appdata_directory(),
edeef6f9 31 '/home/flavio/.var/app/it.ya2.Pmachines/data',
68f6c184 32 '/home/flavio/.wine/drive_c/users/flavio/AppData/Local']
0f9675ba 33 files = [
edeef6f9
FC
34 'pmachines/options.ini',
35 'pmachines/obs_version.txt']
0f9675ba
FC
36 for dir_file in product(dirs, files):
37 _file = join(*dir_file)
38 if exists(_file):
39 remove(_file)
40 info('%s removed' % _file)
41 else:
42 info('%s does not exist' % _file)
2fbba335
FC
43 opt_ini = str(Path.home()) + '/builders/pmachines_builder/pmachines/options.ini'
44 if exists(opt_ini):
45 remove(opt_ini)
edeef6f9 46 system('pkill -f "pmachines.exe"')
0f9675ba
FC
47
48 def __awake(self):
49 system('xdotool key shift')
50
51 def setUp(self):
52 self.__clean()
53
54 def tearDown(self):
8ce16d6c 55 pass # self.__clean()
0f9675ba
FC
56
57 def test_ref(self):
edeef6f9
FC
58 info('test_ref')
59 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
0f9675ba
FC
60 files = glob(join(Filename().get_user_appdata_directory(), path))
61 self.assertGreater(len(files), 1)
62
63 def __similar_images(self, ref_img, tst_img):
64 cmd = 'magick compare -metric NCC %s %s diff.png' % (ref_img, tst_img)
df4f85fc 65 res = exec_cmd(cmd, False)
0f9675ba
FC
66 if exists('diff.png'): remove('diff.png')
67 print('compare %s %s: %s' % (ref_img, tst_img, res))
8ce16d6c 68 return float(res) > .8
0f9675ba 69
a33967c7 70 def __test_template(self, app_cmd, test1_cmd, test2_cmd, path):
0f9675ba
FC
71 if environ.get('FUNCTIONAL') != '1':
72 self.skipTest('skipped functional tests')
73 self.__clean()
74 self.__awake()
75 system('amixer sset Master 0%')
a33967c7
FC
76 #ret = system(cmd)
77 #proc = run(cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True, check=check)
78 #return proc.stdout.strip() + proc.stderr.strip()
79 info('launching ' + app_cmd)
80 print('launching ' + app_cmd)
81 p_app = Popen(app_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
82 sleep(8)
83 info('launching ' + test1_cmd)
84 print('launching ' + test1_cmd)
85 p_test = Popen(test1_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
86 p_app_out, p_app_err = p_app.communicate()
87 t_out, t_err = p_test.communicate()
88 info('output (%s): %s' % (app_cmd, p_app_out))
89 info('error (%s): %s' % (app_cmd, p_app_err))
90 info('output (%s): %s' % (test1_cmd, t_out))
91 info('error (%s): %s' % (test1_cmd, t_err))
92 print('output (%s): %s' % (app_cmd, p_app_out))
93 print('error (%s): %s' % (app_cmd, p_app_err))
94 print('output (%s): %s' % (test1_cmd, t_out))
95 print('error (%s): %s' % (test1_cmd, t_err))
96 self.assertEqual(p_app.returncode, 0, 'error while executing ' + app_cmd)
97 self.assertEqual(p_test.returncode, 0, 'error while executing ' + test1_cmd)
98 sleep(12)
99 info('launching ' + app_cmd)
100 print('launching ' + app_cmd)
101 p_app = Popen(app_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
102 sleep(8)
103 info('launching ' + test2_cmd)
104 print('launching ' + test2_cmd)
105 p_test = Popen(test2_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
106 p_app_out, p_app_err = p_app.communicate()
107 t_out, t_err = p_test.communicate()
108 info('output (%s): %s' % (app_cmd, p_app_out))
109 info('error (%s): %s' % (app_cmd, p_app_err))
110 info('output (%s): %s' % (test2_cmd, t_out))
111 info('error (%s): %s' % (test2_cmd, t_err))
112 print('output (%s): %s' % (app_cmd, p_app_out))
113 print('error (%s): %s' % (app_cmd, p_app_err))
114 print('output (%s): %s' % (test2_cmd, t_out))
115 print('error (%s): %s' % (test2_cmd, t_err))
116 self.assertEqual(p_app.returncode, 0, 'error while executing ' + app_cmd)
117 self.assertEqual(p_test.returncode, 0, 'error while executing ' + test2_cmd)
ad5ef398
FC
118 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
119 self.assertGreater(len(files), 1)
0f9675ba 120 for fname in files:
ad5ef398 121 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
8ce16d6c
FC
122 similar = self.__similar_images(
123 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
124 path + basename(fname)),
125 'error while comparing %s and %s' % (
126 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
127 path + basename(fname))
128 if not similar:
129 timestamp = datetime.now().strftime('%y%m%d%H%M%S%f')
130 copy(path + basename(fname), '~/Desktop/' + basename(fname)[:-4] + timestamp + '.png')
131 self.assertTrue(similar)
0f9675ba 132
ad5ef398
FC
133 def test_code(self):
134 info('test_code')
135 self.__test_template(
a33967c7
FC
136 'timeout 7200s ~/venv/bin/python main.py --functional-test',
137 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
138 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
ad5ef398 139 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
edeef6f9 140
1e689176
FC
141 def test_appimage(self):
142 info('test_appimage')
143 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
144 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
145 self.__test_template(
a33967c7
FC
146 'timeout 7200s ./dist/Pmachines%s-x86_64.AppImage --functional-test' % bld_branch,
147 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
148 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
1e689176 149 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
edeef6f9 150
e9be18cf
FC
151 # def test_flatpak(self):
152 # info('test_flatpak')
153 # if environ.get('FUNCTIONALPOST') != '1':
154 # self.skipTest('skipped functional-post tests')
155 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
156 # cmd = 'flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch
157 # info('executing: %s' % cmd)
158 # #system(cmd)
159 # fout = exec_cmd(cmd)
160 # info('executed: %s' % cmd)
161 # info(fout)
162 # self.__test_template(
163 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & '
164 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
165 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & ' % (bld_branch, bld_branch) +
166 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
167 # str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
addec9c9 168
c8e3e941
FC
169 def __update_itchio(self):
170 system('/home/flavio/.itch/itch')
171 sleep(5)
172 system('xdotool mousemove 860 620')
173 sleep(1)
174 system('xdotool click 1')
175 sleep(300)
176 system('killall itch')
177
178 def test_itchio(self):
179 info('test_itchio')
180 if environ.get('FUNCTIONALPOST') != '1':
181 self.skipTest('skipped functional-post tests')
182 if _branch() != 'master':
183 return
184 self.__update_itchio()
185 self.__test_template(
a33967c7
FC
186 'timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test',
187 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
188 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
c8e3e941 189 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
addec9c9 190
2e5f0efc
FC
191 def test_windows(self):
192 info('test_windows')
193 system('pkill -f "pmachines.exe"')
194 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
195 self.__test_template(
a33967c7
FC
196 'timeout 7200s wine %s --functional-test' % abspath,
197 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
198 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2',
2e5f0efc 199 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
edeef6f9 200
8ce16d6c
FC
201 def test_versions(self):
202 info('test_versions')
203 if environ.get('FUNCTIONAL') != '1':
204 self.skipTest('skipped functional tests')
205 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
206 with open('/home/flavio/builders/pmachines_builder/last_bld.txt') as f:
207 lines = f.readlines()
208 for line in lines:
209 if line.strip().split()[0] == _branch():
210 commit = line.strip().split()[1][:7]
ea71df6e 211 __ver = _version()
8ce16d6c
FC
212 if _branch() == 'stable':
213 with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
a7f2fc93
FC
214 __ver = fver.read().strip() + '-'
215 exp = '%s-%s' % (__ver, commit)
8ce16d6c 216 cmds = [
a33967c7
FC
217 ('timeout 7200s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
218 ('timeout 7200s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
219 ('timeout 7200s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
c8e3e941
FC
220 ]
221 if environ.get('FUNCTIONALPOST') == '1':
222 if _branch() == 'master':
223 self.__update_itchio()
a33967c7
FC
224 cmds += [('timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
225 # cmds += [('timeout 7200s flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
e9be18cf
FC
226 # info('executing flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
227 # #system('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
228 # fout = exec_cmd('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
229 # info('executed flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
230 # info(fout)
8ce16d6c
FC
231 for cmd in cmds:
232 if exists(cmd[1]):
233 remove(cmd[1])
234 info('launching %s' % cmd[0])
235 exec_cmd(cmd[0])
236 with open(cmd[1]) as f:
237 obs = f.read().strip()
238 self.assertEqual(obs, exp, 'during ' + cmd[0])