ya2 · news · projects · code · about

unit tests
[pmachines.git] / tests / test_functional.py
1 from pathlib import Path
2 from itertools import product
3 from logging import info
4 import sys
5 if '' in sys.path: sys.path.remove('')
6 sys.path.append(str(Path(__file__).parent.parent.parent))
7 from unittest import TestCase
8 from shutil import rmtree
9 from time import sleep
10 from os import system, remove, environ
11 from os.path import exists, basename, join
12 from glob import glob
13 from panda3d.core import Filename
14 from lib.build.build import exec_cmd, _branch, ver
15
16
17 class FunctionalTests(TestCase):
18
19 def __clean(self):
20 paths = [
21 'tests/functional',
22 str(Path.home()) + '/.local/share/yocto_racer/tests/functional/',
23 str(Path.home()) + '/.var/app/it.ya2.YoctoRacer/data/yocto_racer/tests/functional/',
24 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/yocto_racer/tests/functional/']
25 for path in paths:
26 rmtree(path, ignore_errors=True)
27 dirs = [
28 Filename().get_user_appdata_directory(),
29 '/home/flavio/.var/app/it.ya2.YoctoRacer/data',
30 '/home/flavio/.wine/drive_c/users/flavio/AppData']
31 files = [
32 'yocto_racer/options.ini',
33 'yocto_racer/obs_version.txt']
34 for dir_file in product(dirs, files):
35 _file = join(*dir_file)
36 if exists(_file):
37 remove(_file)
38 info('%s removed' % _file)
39 else:
40 info('%s does not exist' % _file)
41 system('pkill -f "yocto.exe"')
42
43 def __awake(self):
44 system('xdotool key shift')
45
46 def setUp(self):
47 self.__clean()
48
49 def tearDown(self):
50 self.__clean()
51
52 def test_ref(self):
53 path = 'yocto_racer/tests/functional_ref_%s/*.png' % _branch()
54 files = glob(join(Filename().get_user_appdata_directory(), path))
55 self.assertGreater(len(files), 1)
56
57 def __similar_images(self, ref_img, tst_img):
58 cmd = 'magick compare -metric NCC %s %s diff.png' % (ref_img, tst_img)
59 res = exec_cmd(cmd)
60 if exists('diff.png'): remove('diff.png')
61 print('compare %s %s: %s' % (ref_img, tst_img, res))
62 return float(res) > .64
63
64 def __test_template(self, cmd, path):
65 if environ.get('FUNCTIONAL') != '1':
66 self.skipTest('skipped functional tests')
67 self.__clean()
68 self.__awake()
69 system('amixer sset Master 0%')
70 ret = system(cmd)
71 self.assertEqual(ret, 0)
72 files = glob('tests/functional_ref/*.png')
73 for fname in files:
74 self.assertTrue(exists(path + basename(fname)))
75 self.assertTrue(
76 self.__similar_images(
77 fname, path + basename(fname)))
78
79 def test_code(self):
80 info('test_code')
81 self.__test_template(
82 '~/venv/bin/python main.py --functional-test 1 ; '
83 '~/venv/bin/python main.py --functional-test 2',
84 str(Path.home()) + '/.local/share/yocto_racer/tests/functional/')
85
86 def test_appimage(self):
87 info('test_appimage')
88 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
89 bld_branch = '' if bld_branch == 'stable' else ('-' + bld_branch)
90 self.__test_template(
91 './dist/Yocto%s-x86_64.AppImage --functional-test 1 ;'
92 './dist/Yocto%s-x86_64.AppImage --functional-test 2' % (bld_branch, bld_branch),
93 str(Path.home()) + '/.local/share/yocto_racer/tests/functional/')
94
95 def test_flatpak(self):
96 info('test_flatpak')
97 if environ.get('FUNCTIONALPOST') != '1':
98 self.skipTest('skipped functional-post tests')
99 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
100 cmd = 'flatpak update -y it.ya2.YoctoRacer//%s' % bld_branch
101 info('executing: %s' % cmd)
102 system(cmd)
103 info('executed: %s' % cmd)
104 self.__test_template(
105 'flatpak run it.ya2.YoctoRacer//%s --functional-test 1 ;'
106 'flatpak run it.ya2.YoctoRacer//%s --functional-test 2' % (bld_branch, bld_branch),
107 str(Path.home()) + '/.var/app/it.ya2.YoctoRacer/data/yocto_racer/tests/functional/')
108
109 def __update_itchio(self):
110 system('/home/flavio/.itch/itch')
111 sleep(5)
112 system('xdotool mousemove 1280 620')
113 sleep(1)
114 system('xdotool click 1')
115 sleep(300)
116 system('killall itch')
117
118 def test_itchio(self):
119 info('test_itchio')
120 if environ.get('FUNCTIONALPOST') != '1':
121 self.skipTest('skipped functional-post tests')
122 if _branch() != 'master':
123 return
124 self.__update_itchio()
125 self.__test_template(
126 '/home/flavio/.config/itch/apps/yocto-racer/yocto --functional-test 1 ;'
127 '/home/flavio/.config/itch/apps/yocto-racer/yocto --functional-test 2',
128 str(Path.home()) + '/.local/share/yocto_racer/tests/functional/')
129
130 def test_windows(self):
131 info('test_windows')
132 system('pkill -f "yocto.exe"')
133 abspath = str(Path(__file__).parent.parent) + '/build/win_amd64/yocto.exe'
134 self.__test_template(
135 'timeout 720s wine %s --functional-test 1 ; '
136 'timeout 720s wine %s --functional-test 2' % (abspath, abspath),
137 str(Path.home()) + '/.wine/drive_c/users/flavio/AppData/Local/yocto_racer/tests/functional/')
138
139 def test_versions(self):
140 info('test_versions')
141 if environ.get('FUNCTIONAL') != '1':
142 self.skipTest('skipped functional tests')
143 bld_branch = {'master': 'alpha', 'rc': 'rc', 'stable': 'stable'}[_branch()]
144 with open('/home/flavio/yocto_builder/last_bld.txt') as f:
145 lines = f.readlines()
146 for line in lines:
147 if line.strip().split()[0] == _branch():
148 commit = line.strip().split()[1][:7]
149 _ver = ver
150 if _branch() == 'stable':
151 with open('/home/flavio/yocto_builder/yocto/assets/version.txt') as fver:
152 _ver = fver.read().strip() + '-'
153 exp = '%s-%s' % (_ver, commit)
154 cmds = [
155 ('./build/manylinux1_x86_64/yocto --version', str(Filename.get_user_appdata_directory()) + '/yocto_racer/obs_version.txt'),
156 ('./dist/Yocto-%s-x86_64.AppImage --version' % bld_branch, str(Filename.get_user_appdata_directory()) + '/yocto_racer/obs_version.txt'),
157 ('timeout 720s wine ./build/win_amd64/yocto.exe --version', '/home/flavio/.wine/drive_c/users/flavio/AppData/Local/yocto_racer/obs_version.txt')
158 ]
159 if environ.get('FUNCTIONALPOST') == '1':
160 if _branch() == 'master':
161 self.__update_itchio()
162 cmds += [('/home/flavio/.config/itch/apps/yocto-racer/yocto --version', str(Filename.get_user_appdata_directory()) + '/yocto_racer/obs_version.txt')]
163 cmds += [('flatpak run it.ya2.YoctoRacer//%s --version' % bld_branch, '/home/flavio/.var/app/it.ya2.YoctoRacer/data/yocto_racer/obs_version.txt')]
164 system('flatpak update -y it.ya2.YoctoRacer//%s' % bld_branch)
165 for cmd in cmds:
166 if exists(cmd[1]):
167 remove(cmd[1])
168 info('launching %s' % cmd[0])
169 exec_cmd(cmd[0])
170 with open(cmd[1]) as f:
171 obs = f.read().strip()
172 self.assertEqual(obs, exp)