github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/results-processor/wptscreenshot_test.py (about) 1 # Copyright 2019 The WPT Dashboard Project. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import contextlib 6 import gzip 7 import tempfile 8 import unittest 9 import warnings 10 11 import test_util 12 from wptscreenshot import WPTScreenshot 13 14 15 class WPTScreenshotTest(unittest.TestCase): 16 def setUp(self): 17 self.server, base_url = test_util.start_server(True) 18 self.api = base_url + '/api/screenshots/upload' 19 20 # We would like to make ResourceWarning (unclosed files) fatal, but 21 # -Werror::ResourceWarning does not work since the error is often 22 # "unraisable", so we have to use a context manager to record warnings. 23 self.context = contextlib.ExitStack() 24 # This is equivalent to a test-scope 25 # `with warnings.catch_warnings(record=True) as self.warnings`. 26 self.warnings = self.context.enter_context( 27 warnings.catch_warnings(record=True)) 28 29 def tearDown(self): 30 if self.server.poll() is None: 31 self.server.kill() 32 33 self.context.close() 34 messages = [w.message for w in self.warnings] 35 self.assertListEqual(messages, []) 36 37 def _batch_sizes(self, err_text): 38 s = [] 39 for i in err_text.decode('ascii').splitlines(): 40 s.append(int(i)) 41 return s 42 43 def test_basic(self): 44 with tempfile.NamedTemporaryFile() as f: 45 f.write(b'data:image/png;base64,0001\n') 46 f.write(b'data:image/png;base64,0002\n') 47 f.flush() 48 with WPTScreenshot(f.name, api=self.api, processes=1) as s: 49 s.process() 50 self.server.terminate() 51 _, err = self.server.communicate() 52 sizes = self._batch_sizes(err) 53 self.assertListEqual(sizes, [2]) 54 55 def test_gzip(self): 56 with tempfile.NamedTemporaryFile(suffix='.gz') as f: 57 with gzip.GzipFile(filename=f.name, mode='wb') as g: 58 g.write(b'data:image/png;base64,0001\n') 59 g.write(b'data:image/png;base64,0002\n') 60 f.flush() 61 with WPTScreenshot(f.name, api=self.api, processes=1) as s: 62 s.process() 63 self.server.terminate() 64 _, err = self.server.communicate() 65 sizes = self._batch_sizes(err) 66 self.assertListEqual(sizes, [2]) 67 68 def test_invalid_encoding(self): 69 with tempfile.NamedTemporaryFile() as f: 70 f.write(b'\xc8\n') 71 f.flush() 72 with self.assertRaises(UnicodeDecodeError): 73 with WPTScreenshot(f.name, api=self.api, processes=1) as s: 74 s.process() 75 self.server.terminate() 76 _, err = self.server.communicate() 77 sizes = self._batch_sizes(err) 78 self.assertListEqual(sizes, []) 79 80 def test_invalid_gzip(self): 81 with tempfile.NamedTemporaryFile(suffix=".gz") as f: 82 f.write(b'Hello\n') 83 f.flush() 84 with self.assertRaises(OSError): 85 with WPTScreenshot(f.name, api=self.api, processes=1) as s: 86 s.process() 87 self.server.terminate() 88 _, err = self.server.communicate() 89 sizes = self._batch_sizes(err) 90 self.assertListEqual(sizes, []) 91 92 def test_multiple_batches(self): 93 with tempfile.NamedTemporaryFile() as f: 94 f.write(b'data:image/png;base64,0001\n') 95 f.write(b'data:image/png;base64,0002\n') 96 f.write(b'data:image/png;base64,0003\n') 97 f.flush() 98 with WPTScreenshot(f.name, api=self.api, processes=2) as s: 99 s.MAXIMUM_BATCH_SIZE = 2 100 s.process() 101 self.server.terminate() 102 _, err = self.server.communicate() 103 sizes = self._batch_sizes(err) 104 self.assertSetEqual(set(sizes), {1, 2}) 105 106 def test_errors(self): 107 with tempfile.NamedTemporaryFile() as f: 108 f.write(b'invalid,0001\n') 109 f.write(b'data:image/png;base64,0002\n') 110 f.write(b'data:image/png;base64,0\n') 111 f.flush() 112 with self.assertLogs() as lm: 113 with WPTScreenshot(f.name, api=self.api, processes=1) as s: 114 s.process() 115 self.server.terminate() 116 _, err = self.server.communicate() 117 sizes = self._batch_sizes(err) 118 self.assertListEqual(sizes, [1]) 119 self.assertListEqual( 120 lm.output, 121 ['ERROR:wptscreenshot:Invalid data URI: invalid,0001', 122 'ERROR:wptscreenshot:Invalid base64: data:image/png;base64,0'])