ya2 · news · projects · code · about

renamed lib to ya2
[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 panda3d.core import Filename
15 from ya2.build.build import exec_cmd, _branch, ver
16
17
18 class FunctionalTests(TestCase):
19
20 def __clean(self):
21 paths = [
22 'tests/functional',
23 str(Path.home()) + '/.local/share/pmachines/tests/functional/',
24 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/',
25 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/']
26 for path in paths:
27 rmtree(path, ignore_errors=True)
28 dirs = [
29 Filename().get_user_appdata_directory(),
30 '/home/flavio/.var/app/it.ya2.Pmachines/data',
31 '/home/flavio/.wine/drive_c/users/flavio/AppData/Local']
32 files = [
33 'pmachines/options.ini',
34 'pmachines/obs_version.txt']
35 for dir_file in product(dirs, files):
36 _file = join(*dir_file)
37 if exists(_file):
38 remove(_file)
39 info('%s removed' % _file)
40 else:
41 info('%s does not exist' % _file)
42 opt_ini = str(Path.home()) + '/builders/pmachines_builder/pmachines/options.ini'
43 if exists(opt_ini):
44 remove(opt_ini)
45 system('pkill -f "pmachines.exe"')
46
47 def __awake(self):
48 system('xdotool key shift')
49
50 def setUp(self):
51 self.__clean()
52
53 def tearDown(self):
54 pass # self.__clean()
55
56 def test_ref(self):
57 info('test_ref')
58 path = 'pmachines/tests/functional_ref_%s/*.png' % _branch()
59 files = glob(join(Filename().get_user_appdata_directory(), path))
60 self.assertGreater(len(files), 1)
61
62 def __similar_images(self, ref_img, tst_img):
63 cmd = 'magick compare -metric NCC %s %s diff.png' % (ref_img, tst_img)
64 res = exec_cmd(cmd)
65 if exists('diff.png'): remove('diff.png')
66 print('compare %s %s: %s' % (ref_img, tst_img, res))
67 return float(res) > .8
68
69 def __test_template(self, cmd, path):
70 if environ.get('FUNCTIONAL') != '1':
71 self.skipTest('skipped functional tests')
72 self.__clean()
73 self.__awake()
74 system('amixer sset Master 0%')
75 ret = system(cmd)
76 self.assertEqual(ret, 0, 'error while executing ' + cmd)
77 files = glob(str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/*.png' % _branch())
78 self.assertGreater(len(files), 1)
79 for fname in files:
80 self.assertTrue(exists(path + basename(fname)), '%s does not exist' % (path + basename(fname)))
81 similar = self.__similar_images(
82 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
83 path + basename(fname)),
84 'error while comparing %s and %s' % (
85 str(Path.home()) + '/.local/share/pmachines/tests/functional_ref_%s/' % _branch() + basename(fname),
86 path + basename(fname))
87 if not similar:
88 timestamp = datetime.now().strftime('%y%m%d%H%M%S%f')
89 copy(path + basename(fname), '~/Desktop/' + basename(fname)[:-4] + timestamp + '.png')
90 self.assertTrue(similar)
91
92 def test_code(self):
93 info('test_code')
94 self.__test_template(
95 'timeout 720s ~/venv/bin/python main.py --functional-test & '
96 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
97 'timeout 720s ~/venv/bin/python main.py --functional-test & '
98 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
99 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
100
101 def test_appimage(self):
102 info('test_appimage')
103 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
104 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
105 self.__test_template(
106 'timeout 720s ./dist/Pmachines%s-x86_64.AppImage --functional-test & '
107 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
108 'timeout 720s ./dist/Pmachines%s-x86_64.AppImage --functional-test & ' % (bld_branch, bld_branch) +
109 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
110 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
111
112 def test_flatpak(self):
113 info('test_flatpak')
114 if environ.get('FUNCTIONALPOST') != '1':
115 self.skipTest('skipped functional-post tests')
116 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
117 cmd = 'flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch
118 info('executing: %s' % cmd)
119 #system(cmd)
120 fout = exec_cmd(cmd)
121 info('executed: %s' % cmd)
122 info(fout)
123 self.__test_template(
124 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & '
125 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
126 'timeout 720s flatpak run it.ya2.Pmachines//%s --functional-test & ' % (bld_branch, bld_branch) +
127 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
128 str(Path.home()) + '/.var/app/it.ya2.Pmachines/data/pmachines/tests/functional/')
129
130 def __update_itchio(self):
131 system('/home/flavio/.itch/itch')
132 sleep(5)
133 system('xdotool mousemove 860 620')
134 sleep(1)
135 system('xdotool click 1')
136 sleep(300)
137 system('killall itch')
138
139 def test_itchio(self):
140 info('test_itchio')
141 if environ.get('FUNCTIONALPOST') != '1':
142 self.skipTest('skipped functional-post tests')
143 if _branch() != 'master':
144 return
145 self.__update_itchio()
146 self.__test_template(
147 'timeout 720s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test & '
148 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
149 'timeout 720s /home/flavio/.config/itch/apps/pmachines/pmachines --functional-test & '
150 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
151 str(Path.home()) + '/.local/share/pmachines/tests/functional/')
152
153 def test_windows(self):
154 info('test_windows')
155 system('pkill -f "pmachines.exe"')
156 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/pmachines.exe'
157 self.__test_template(
158 'timeout 720s wine %s --functional-test & ' % abspath +
159 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 1; sleep 5; '
160 'timeout 720s wine %s --functional-test & ' % abspath +
161 'timeout 720s ~/venv/bin/python -m tests.functional_test.py 2',
162 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/pmachines/tests/functional/')
163
164 def test_versions(self):
165 info('test_versions')
166 if environ.get('FUNCTIONAL') != '1':
167 self.skipTest('skipped functional tests')
168 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
169 with open('/home/flavio/builders/pmachines_builder/last_bld.txt') as f:
170 lines = f.readlines()
171 for line in lines:
172 if line.strip().split()[0] == _branch():
173 commit = line.strip().split()[1][:7]
174 _ver = ver
175 if _branch() == 'stable':
176 with open('/home/flavio/builders/pmachines_builder/pmachines/assets/version.txt') as fver:
177 _ver = fver.read().strip() + '-'
178 exp = '%s-%s' % (_ver, commit)
179 cmds = [
180 ('timeout 720s ./build/manylinux2010_x86_64/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
181 ('timeout 720s ./dist/Pmachines-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt'),
182 ('timeout 720s wine ./build/win_amd64/pmachines.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/pmachines/obs_version.txt')
183 ]
184 if environ.get('FUNCTIONALPOST') == '1':
185 if _branch() == 'master':
186 self.__update_itchio()
187 cmds += [('timeout 720s /home/flavio/.config/itch/apps/pmachines/pmachines --version', str(Filename.get_user_appdata_directory()) + '/pmachines/obs_version.txt')]
188 cmds += [('timeout 720s flatpak run it.ya2.Pmachines//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.Pmachines/data/pmachines/obs_version.txt')]
189 info('executing flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
190 #system('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
191 fout = exec_cmd('flatpak update -y --user it.ya2.Pmachines//%s' % bld_branch)
192 info('executed flatpak update --user -y it.ya2.Pmachines//%s' % bld_branch)
193 info(fout)
194 for cmd in cmds:
195 if exists(cmd[1]):
196 remove(cmd[1])
197 info('launching %s' % cmd[0])
198 exec_cmd(cmd[0])
199 with open(cmd[1]) as f:
200 obs = f.read().strip()
201 self.assertEqual(obs, exp, 'during ' + cmd[0])