ya2 · news · projects · code · about

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