|
| 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 logging |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import tempfile |
| 22 | +import unittest |
| 23 | +import uuid |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +import apache_beam as beam |
| 28 | +from apache_beam.options.pipeline_options import SetupOptions |
| 29 | +from apache_beam.testing.test_pipeline import TestPipeline |
| 30 | +from apache_beam.testing.util import assert_that |
| 31 | +from apache_beam.testing.util import equal_to |
| 32 | + |
| 33 | +_LOGGER = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class BeamPluginsIT(unittest.TestCase): |
| 37 | + def setUp(self): |
| 38 | + self.temp_dir = tempfile.mkdtemp() |
| 39 | + |
| 40 | + def tearDown(self): |
| 41 | + shutil.rmtree(self.temp_dir) |
| 42 | + |
| 43 | + @pytest.mark.it_postcommit |
| 44 | + def test_beam_plugins_staging(self): |
| 45 | + pipeline = TestPipeline(is_integration_test=True) |
| 46 | + setup_options = pipeline.options.view_as(SetupOptions) |
| 47 | + |
| 48 | + plugin_name = f'beam_integration_plugin_{uuid.uuid4().hex[:8]}' |
| 49 | + plugin_file_path = os.path.join(self.temp_dir, f'{plugin_name}.py') |
| 50 | + |
| 51 | + with open(plugin_file_path, 'w') as f: |
| 52 | + f.write("import sys\nsys.beam_plugin_loaded_for_test = True\n") |
| 53 | + |
| 54 | + staged_files = setup_options.files_to_stage or [] |
| 55 | + staged_files.append(plugin_file_path) |
| 56 | + setup_options.files_to_stage = staged_files |
| 57 | + setup_options.beam_plugins = [plugin_name] |
| 58 | + |
| 59 | + def check_plugin_loaded(_): |
| 60 | + import sys |
| 61 | + return getattr(sys, 'beam_plugin_loaded_for_test', False) |
| 62 | + |
| 63 | + with pipeline as p: |
| 64 | + res = (p | beam.Create([None]) | beam.Map(check_plugin_loaded)) |
| 65 | + assert_that(res, equal_to([True])) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + logging.getLogger().setLevel(logging.INFO) |
| 70 | + unittest.main() |
0 commit comments