ya2 · news · projects · code · about

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