github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/runners/portability/samza_runner_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  # pytype: skip-file
    18  
    19  # Run as
    20  #
    21  # pytest samza_runner_test.py[::TestClass::test_case] \
    22  #     --test-pipeline-options="--environment_type=LOOPBACK"
    23  import argparse
    24  import logging
    25  import shlex
    26  import unittest
    27  from shutil import rmtree
    28  from tempfile import mkdtemp
    29  
    30  import pytest
    31  
    32  from apache_beam.options.pipeline_options import PortableOptions
    33  from apache_beam.runners.portability import job_server
    34  from apache_beam.runners.portability import portable_runner
    35  from apache_beam.runners.portability import portable_runner_test
    36  
    37  _LOGGER = logging.getLogger(__name__)
    38  
    39  
    40  class SamzaRunnerTest(portable_runner_test.PortableRunnerTest):
    41    _use_grpc = True
    42    _use_subprocesses = True
    43  
    44    expansion_port = None
    45    samza_job_server_jar = None
    46  
    47    @pytest.fixture(autouse=True)
    48    def parse_options(self, request):
    49      if not request.config.option.test_pipeline_options:
    50        raise unittest.SkipTest(
    51            'Skipping because --test-pipeline-options is not specified.')
    52      test_pipeline_options = request.config.option.test_pipeline_options
    53      parser = argparse.ArgumentParser(add_help=True)
    54      parser.add_argument(
    55          '--samza_job_server_jar',
    56          help='Job server jar to submit jobs.',
    57          action='store')
    58      parser.add_argument(
    59          '--environment_type',
    60          default='LOOPBACK',
    61          choices=['DOCKER', 'PROCESS', 'LOOPBACK'],
    62          help='Set the environment type for running user code. DOCKER runs '
    63          'user code in a container. PROCESS runs user code in '
    64          'automatically started processes. LOOPBACK runs user code on '
    65          'the same process that originally submitted the job.')
    66      parser.add_argument(
    67          '--environment_option',
    68          '--environment_options',
    69          dest='environment_options',
    70          action='append',
    71          default=None,
    72          help=(
    73              'Environment configuration for running the user code. '
    74              'Recognized options depend on --environment_type.\n '
    75              'For DOCKER: docker_container_image (optional)\n '
    76              'For PROCESS: process_command (required), process_variables '
    77              '(optional, comma-separated)\n '
    78              'For EXTERNAL: external_service_address (required)'))
    79      known_args, unknown_args = parser.parse_known_args(
    80          shlex.split(test_pipeline_options))
    81      if unknown_args:
    82        _LOGGER.warning('Discarding unrecognized arguments %s' % unknown_args)
    83      self.set_samza_job_server_jar(
    84          known_args.samza_job_server_jar or
    85          job_server.JavaJarJobServer.path_to_beam_jar(
    86              ':runners:samza:job-server:shadowJar'))
    87      self.environment_type = known_args.environment_type
    88      self.environment_options = known_args.environment_options\
    89  
    90    @classmethod
    91    def _subprocess_command(cls, job_port, expansion_port):
    92      # will be cleaned up at the end of this method, and recreated and used by
    93      # the job server
    94      tmp_dir = mkdtemp(prefix='samzatest')
    95  
    96      cls.expansion_port = expansion_port
    97  
    98      try:
    99        return [
   100            'java',
   101            '-jar',
   102            cls.samza_job_server_jar,
   103            '--artifacts-dir',
   104            tmp_dir,
   105            '--job-port',
   106            str(job_port),
   107            '--artifact-port',
   108            '0',
   109            '--expansion-port',
   110            str(expansion_port),
   111        ]
   112      finally:
   113        rmtree(tmp_dir)
   114  
   115    @classmethod
   116    def set_samza_job_server_jar(cls, samza_job_server_jar):
   117      cls.samza_job_server_jar = samza_job_server_jar
   118  
   119    @classmethod
   120    def get_runner(cls):
   121      return portable_runner.PortableRunner()
   122  
   123    @classmethod
   124    def get_expansion_service(cls):
   125      # TODO Move expansion address resides into PipelineOptions
   126      return 'localhost:%s' % cls.expansion_port
   127  
   128    def create_options(self):
   129      options = super().create_options()
   130      options.view_as(PortableOptions).environment_type = self.environment_type
   131      options.view_as(
   132          PortableOptions).environment_options = self.environment_options
   133  
   134      return options
   135  
   136    def test_metrics(self):
   137      # Skip until Samza portable runner supports distribution metrics.
   138      raise unittest.SkipTest("https://github.com/apache/beam/issues/21043")
   139  
   140    def test_flattened_side_input(self):
   141      # Blocked on support for transcoding
   142      # https://github.com/apache/beam/issues/20984
   143      super().test_flattened_side_input(with_transcoding=False)
   144  
   145    def test_pack_combiners(self):
   146      # Stages produced by translations.pack_combiners are fused
   147      # by translations.greedily_fuse, which prevent the stages
   148      # from being detecting using counters by the test.
   149      self._test_pack_combiners(assert_using_counter_names=False)
   150  
   151    def test_pardo_timers(self):
   152      # Skip until Samza portable runner supports clearing timer.
   153      raise unittest.SkipTest("https://github.com/apache/beam/issues/21059")
   154  
   155    def test_register_finalizations(self):
   156      # Skip until Samza runner supports bundle finalization.
   157      raise unittest.SkipTest("https://github.com/apache/beam/issues/21044")
   158  
   159    def test_callbacks_with_exception(self):
   160      # Skip until Samza runner supports bundle finalization.
   161      raise unittest.SkipTest("https://github.com/apache/beam/issues/21044")
   162  
   163    def test_sdf_with_dofn_as_watermark_estimator(self):
   164      # Skip until Samza runner supports SDF and self-checkpoint.
   165      raise unittest.SkipTest("https://github.com/apache/beam/issues/21045")
   166  
   167    def test_sdf_with_sdf_initiated_checkpointing(self):
   168      # Skip until Samza runner supports SDF.
   169      raise unittest.SkipTest("https://github.com/apache/beam/issues/21045")
   170  
   171    def test_sdf_with_watermark_tracking(self):
   172      # Skip until Samza runner supports SDF.
   173      raise unittest.SkipTest("https://github.com/apache/beam/issues/21045")
   174  
   175    def test_custom_merging_window(self):
   176      # Skip until Samza runner supports merging window fns
   177      raise unittest.SkipTest("https://github.com/apache/beam/issues/21049")
   178  
   179  
   180  if __name__ == '__main__':
   181    # Run the tests.
   182    logging.getLogger().setLevel(logging.INFO)
   183    unittest.main()