ya2 · news · projects · code · about

81c8f927167768cbae6faf1ca1c8059a6f7e7e00
[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, test_cmds, 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 for test_cmd in test_cmds:
80 sleep(8)
81 info('launching ' + app_cmd)
82 print('launching ' + app_cmd)
83 p_app = Popen(app_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
84 sleep(30)
85 info('launching ' + test_cmd)
86 print('launching ' + test_cmd)
87 p_test = Popen(test_cmd, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
88 p_app_out, p_app_err = p_app.communicate()
89 t_out, t_err = p_test.communicate()
90 info('output (%s): %s' % (app_cmd, p_app_out))
91 info('error (%s): %s' % (app_cmd, p_app_err))
92 info('output (%s): %s' % (test_cmd, t_out))
93 info('error (%s): %s' % (test_cmd, t_err))
94 print('output (%s): %s' % (app_cmd, p_app_out))
95 print('error (%s): %s' % (app_cmd, p_app_err))
96 print('output (%s): %s' % (test_cmd, t_out))
97 print('error (%s): %s' % (test_cmd, t_err))
98 self.assertEqual(p_app.returncode, 0, 'error while executing ' + app_cmd)
99 self.assertEqual(p_test.returncode, 0, 'error while executing ' + test_cmd)
100 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
101 self.assertGreater(len(files), 1)
102 for fname in files:
103 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
104 similar = self.__similar_images(
105 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
106 path + basename(fname)),
107 'error while comparing %s and %s' % (
108 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
109 path + basename(fname))
110 if not similar:
111 timestamp = datetime.now().strftime('%y%m%d%H%M%S%f')
112 copy(path + basename(fname), '~/Desktop/' + basename(fname)[:-4] + timestamp + '.png')
113 self.assertTrue(similar)
114
115 def test_code(self):
116 info('test_code')
117 self.__test_template(
118 '/home/flavio/venv/bin/python main.py --functional-test',
119 ['/home/flavio/venv/bin/python -m tests.functional_test.py 1',
120 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
121 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
122
123 def test_appimage(self):
124 info('test_appimage')
125 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
126 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
127 self.__test_template(
128 'timeout 7200s ./dist/Pmachines%s-x86_64.AppImage --functional-test' % bld_branch,
129 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
130 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
131 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
132
133 # def test_flatpak(self):
134 # info('test_flatpak')
135 # if environ.get('FUNCTIONALPOST') != '1':
136 # self.skipTest('skipped functional-post tests')
137 # bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
138 # cmd = 'flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch
139 # info('executing: %s' % cmd)
140 # #system(cmd)
141 # fout = exec_cmd(cmd)
142 # info('executed: %s' % cmd)
143 # info(fout)
144 # self.__test_template(
145 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & '
146 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
147 # 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & ' % (bld_branch, bld_branch) +
148 # 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
149 # str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
150
151 def __update_itchio(self):
152 system('/home/flavio/.itch/itch')
153 sleep(5)
154 system('xdotool mousemove 860 620')
155 sleep(1)
156 system('xdotool click 1')
157 sleep(300)
158 system('killall itch')
159
160 def test_itchio(self):
161 info('test_itchio')
162 if environ.get('FUNCTIONALPOST') != '1':
163 self.skipTest('skipped functional-post tests')
164 if _branch() != 'master':
165 return
166 self.__update_itchio()
167 self.__test_template(
168 'timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test',
169 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
170 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
171 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
172
173 def test_windows(self):
174 info('test_windows')
175 system('pkill -f "pmachines.exe"')
176 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
177 self.__test_template(
178 'timeout 7200s wine %s --functional-test' % abspath,
179 ['timeout 7200s ~/venv/bin/python -m tests.functional_test.py 1',
180 'timeout 7200s ~/venv/bin/python -m tests.functional_test.py 2'],
181 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
182
183 def test_versions(self):
184 info('test_versions')
185 if environ.get('FUNCTIONAL') != '1':
186 self.skipTest('skipped functional tests')
187 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
188 with open('/home/flavio/builders/pmachines_builder/last_bld.txt') as f:
189 lines = f.readlines()
190 for line in lines:
191 if line.strip().split()[0] == _branch():
192 commit = line.strip().split()[1][:7]
193 __ver = _version()
194 if _branch() == 'stable':
195 with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
196 __ver = fver.read().strip() + '-'
197 exp = '%s-%s' % (__ver, commit)
198 cmds = [
199 ('timeout 7200s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
200 ('timeout 7200s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
201 ('timeout 7200s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
202 ]
203 if environ.get('FUNCTIONALPOST') == '1':
204 if _branch() == 'master':
205 self.__update_itchio()
206 cmds += [('timeout 7200s /home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
207 # cmds += [('timeout 7200s flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
208 # info('executing flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
209 # #system('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
210 # fout = exec_cmd('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
211 # info('executed flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
212 # info(fout)
213 for cmd in cmds:
214 if exists(cmd[1]):
215 remove(cmd[1])
216 info('launching %s' % cmd[0])
217 exec_cmd(cmd[0])
218 with open(cmd[1]) as f:
219 obs = f.read().strip()
220 self.assertEqual(obs, exp, 'during ' + cmd[0])