ya2 · news · projects · code · about

c5cb38047dd43c54bf5f91a97461269a8ea7cf63
[pmachines.git] / tests / test_functional.py
1 from pathlib import Path
2 from datetime import datetime
3 from itertools import product
4 from logging import info
5 import sys
6 if '' in sys.path: sys.path.remove('')
7 sys.path.append(str(Path(__file__).parent.parent.parent))
8 from unittest import TestCase
9 from shutil import rmtree, copy
10 from time import sleep
11 from os import system, remove, environ
12 from os.path import exists, basename, join
13 from glob import glob
14 from subprocess import PIPE, run, Popen
15 from panda3d.core import Filename
16 from ya2.build.build import exec_cmd, _branch, _version
17
18
19 class FunctionalTests(TestCase):
20
21 def __clean(self):
22 paths = [
23 'tests/functional',
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/']
27 for path in paths:
28 rmtree(path, ignore_errors=True)
29 dirs = [
30 Filename().get_user_appdata_directory(),
31 '/home/flavio/.var/app/it.ya2.Pmachines/data',
32 '/home/flavio/.wine/drive_c/users/flavio/AppData/Local']
33 files = [
34 'pmachines/options.ini',
35 'pmachines/obs_version.txt']
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)
43 opt_ini = str(Path.home()) + '/builders/pmachines_builder/pmachines/options.ini'
44 if exists(opt_ini):
45 remove(opt_ini)
46 system('pkill -f "pmachines.exe"')
47
48 def __awake(self):
49 system('xdotool key shift')
50
51 def setUp(self):
52 self.__clean()
53
54 def tearDown(self):
55 pass # self.__clean()
56
57 def test_ref(self):
58 info('test_ref')
59 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
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)
65 res = exec_cmd(cmd, False)
66 if exists('diff.png'): remove('diff.png')
67 print('compare %s %s: %s' % (ref_img, tst_img, res))
68 return float(res) > .8
69
70 def __test_template(self, app_cmd, test1_cmd, test2_cmd, path):
71 if environ.get('FUNCTIONAL') != '1':
72 self.skipTest('skipped functional tests')
73 self.__clean()
74 self.__awake()
75 system('amixer sset Master 0%')
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)
118 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
119 self.assertGreater(len(files), 1)
120 for fname in files:
121 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
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)
132
133 def test_code(self):
134 info('test_code')
135 self.__test_template(
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',
139 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
140
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(
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',
149 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
150
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/')
168
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(
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',
189 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
190
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(
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',
199 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
200
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]
211 __ver = _version()
212 if _branch() == 'stable':
213 with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
214 __ver = fver.read().strip() + '-'
215 exp = '%s-%s' % (__ver, commit)
216 cmds = [
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')
220 ]
221 if environ.get('FUNCTIONALPOST') == '1':
222 if _branch() == 'master':
223 self.__update_itchio()
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')]
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)
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])