github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/transforms/resources_test.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 import unittest 19 20 from parameterized import param 21 from parameterized import parameterized 22 23 from apache_beam import PTransform 24 from apache_beam.transforms.resources import ResourceHint 25 26 27 class ResourcesTest(unittest.TestCase): 28 @parameterized.expand([ 29 param( 30 name='min_ram', 31 val='100 MiB', 32 urn='beam:resources:min_ram_bytes:v1', 33 bytestr=b'104857600'), 34 param( 35 name='minRam', 36 val='100MB', 37 urn='beam:resources:min_ram_bytes:v1', 38 bytestr=b'100000000'), 39 param( 40 name='min_ram', 41 val='6.5 GiB', 42 urn='beam:resources:min_ram_bytes:v1', 43 bytestr=b'6979321856'), 44 param( 45 name='accelerator', 46 val='gpu', 47 urn='beam:resources:accelerator:v1', 48 bytestr=b'gpu'), 49 ]) 50 def test_known_resource_hints(self, name, val, urn, bytestr): 51 t = PTransform() 52 t = t.with_resource_hints(**{name: val}) 53 self.assertTrue(ResourceHint.is_registered(name)) 54 self.assertEqual(t.get_resource_hints(), {urn: bytestr}) 55 56 @parameterized.expand([ 57 param(name='min_ram', val='3,500G'), 58 param(name='accelerator', val=1), 59 param(name='unknown_hint', val=1) 60 ]) 61 def test_resource_hint_parsing_fails_early(self, name, val): 62 t = PTransform() 63 with self.assertRaises(ValueError): 64 _ = t.with_resource_hints(**{name: val}) 65 66 67 if __name__ == '__main__': 68 unittest.main()