blob: 22fdea6525391a1b2a280c2da961144093ece19d [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apache_beam.testing.test_utils &mdash; Apache Beam documentation</title>
<link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="../../../genindex.html"/>
<link rel="search" title="Search" href="../../../search.html"/>
<link rel="top" title="Apache Beam documentation" href="../../../index.html"/>
<link rel="up" title="Module code" href="../../index.html"/>
<script src="../../../_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../../../index.html" class="icon icon-home"> Apache Beam
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.coders.html">apache_beam.coders package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.internal.html">apache_beam.internal package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.io.html">apache_beam.io package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.metrics.html">apache_beam.metrics package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.options.html">apache_beam.options package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.portability.html">apache_beam.portability package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.runners.html">apache_beam.runners package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.testing.html">apache_beam.testing package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.tools.html">apache_beam.tools package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.transforms.html">apache_beam.transforms package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.typehints.html">apache_beam.typehints package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.utils.html">apache_beam.utils package</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.error.html">apache_beam.error module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pipeline.html">apache_beam.pipeline module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.pvalue.html">apache_beam.pvalue module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.version.html">apache_beam.version module</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../../index.html">Apache Beam</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../../index.html">Docs</a> &raquo;</li>
<li><a href="../../index.html">Module code</a> &raquo;</li>
<li>apache_beam.testing.test_utils</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apache_beam.testing.test_utils</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c1"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c1"># this work for additional information regarding copyright ownership.</span>
<span class="c1"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c1"># (the &quot;License&quot;); you may not use this file except in compliance with</span>
<span class="c1"># the License. You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="sd">&quot;&quot;&quot;Utility methods for testing</span>
<span class="sd">For internal use only; no backwards-compatibility guarantees.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span>
<span class="kn">import</span> <span class="nn">hashlib</span>
<span class="kn">import</span> <span class="nn">imp</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="k">import</span> <span class="nb">object</span>
<span class="kn">from</span> <span class="nn">mock</span> <span class="k">import</span> <span class="n">Mock</span>
<span class="kn">from</span> <span class="nn">mock</span> <span class="k">import</span> <span class="n">patch</span>
<span class="kn">from</span> <span class="nn">apache_beam.io.filesystems</span> <span class="k">import</span> <span class="n">FileSystems</span>
<span class="kn">from</span> <span class="nn">apache_beam.utils</span> <span class="k">import</span> <span class="n">retry</span>
<span class="n">DEFAULT_HASHING_ALG</span> <span class="o">=</span> <span class="s1">&#39;sha1&#39;</span>
<div class="viewcode-block" id="TempDir"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.TempDir">[docs]</a><span class="k">class</span> <span class="nc">TempDir</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Context Manager to create and clean-up a temporary directory.&quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_tempdir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_tempdir</span><span class="p">):</span>
<span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_tempdir</span><span class="p">)</span>
<div class="viewcode-block" id="TempDir.get_path"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.TempDir.get_path">[docs]</a> <span class="k">def</span> <span class="nf">get_path</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Returns the path to the temporary directory.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tempdir</span></div>
<div class="viewcode-block" id="TempDir.create_temp_file"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.TempDir.create_temp_file">[docs]</a> <span class="k">def</span> <span class="nf">create_temp_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">lines</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Creates a temporary file in the temporary directory.</span>
<span class="sd"> Args:</span>
<span class="sd"> suffix (str): The filename suffix of the temporary file (e.g. &#39;.txt&#39;)</span>
<span class="sd"> lines (List[str]): A list of lines that will be written to the temporary</span>
<span class="sd"> file.</span>
<span class="sd"> Returns:</span>
<span class="sd"> The name of the temporary file created.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span>
<span class="n">delete</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_tempdir</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="n">suffix</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">if</span> <span class="n">lines</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span></div></div>
<div class="viewcode-block" id="compute_hash"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.compute_hash">[docs]</a><span class="k">def</span> <span class="nf">compute_hash</span><span class="p">(</span><span class="n">content</span><span class="p">,</span> <span class="n">hashing_alg</span><span class="o">=</span><span class="n">DEFAULT_HASHING_ALG</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Compute a hash value from a list of string.&quot;&quot;&quot;</span>
<span class="n">content</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">hashing_alg</span><span class="p">)</span>
<span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">content</span><span class="p">:</span>
<span class="n">m</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
<span class="k">return</span> <span class="n">m</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span></div>
<div class="viewcode-block" id="patch_retry"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.patch_retry">[docs]</a><span class="k">def</span> <span class="nf">patch_retry</span><span class="p">(</span><span class="n">testcase</span><span class="p">,</span> <span class="n">module</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;A function to patch retry module to use mock clock and logger.</span>
<span class="sd"> Clock and logger that defined in retry decorator will be replaced in test</span>
<span class="sd"> in order to skip sleep phase when retry happens.</span>
<span class="sd"> Args:</span>
<span class="sd"> testcase: An instance of unittest.TestCase that calls this function to</span>
<span class="sd"> patch retry module.</span>
<span class="sd"> module: The module that uses retry and need to be replaced with mock</span>
<span class="sd"> clock and logger in test.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">real_retry_with_exponential_backoff</span> <span class="o">=</span> <span class="n">retry</span><span class="o">.</span><span class="n">with_exponential_backoff</span>
<span class="k">def</span> <span class="nf">patched_retry_with_exponential_backoff</span><span class="p">(</span><span class="n">num_retries</span><span class="p">,</span> <span class="n">retry_filter</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;A patch for retry decorator to use a mock dummy clock and logger.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">real_retry_with_exponential_backoff</span><span class="p">(</span>
<span class="n">num_retries</span><span class="o">=</span><span class="n">num_retries</span><span class="p">,</span> <span class="n">retry_filter</span><span class="o">=</span><span class="n">retry_filter</span><span class="p">,</span> <span class="n">logger</span><span class="o">=</span><span class="n">Mock</span><span class="p">(),</span>
<span class="n">clock</span><span class="o">=</span><span class="n">Mock</span><span class="p">())</span>
<span class="n">patch</span><span class="o">.</span><span class="n">object</span><span class="p">(</span><span class="n">retry</span><span class="p">,</span> <span class="s1">&#39;with_exponential_backoff&#39;</span><span class="p">,</span>
<span class="n">side_effect</span><span class="o">=</span><span class="n">patched_retry_with_exponential_backoff</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="c1"># Reload module after patching.</span>
<span class="n">imp</span><span class="o">.</span><span class="n">reload</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">remove_patches</span><span class="p">():</span>
<span class="n">patch</span><span class="o">.</span><span class="n">stopall</span><span class="p">()</span>
<span class="c1"># Reload module again after removing patch.</span>
<span class="n">imp</span><span class="o">.</span><span class="n">reload</span><span class="p">(</span><span class="n">module</span><span class="p">)</span>
<span class="n">testcase</span><span class="o">.</span><span class="n">addCleanup</span><span class="p">(</span><span class="n">remove_patches</span><span class="p">)</span></div>
<span class="nd">@retry</span><span class="o">.</span><span class="n">with_exponential_backoff</span><span class="p">(</span>
<span class="n">num_retries</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
<span class="n">retry_filter</span><span class="o">=</span><span class="n">retry</span><span class="o">.</span><span class="n">retry_on_beam_io_error_filter</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">delete_files</span><span class="p">(</span><span class="n">file_paths</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;A function to clean up files or directories using ``FileSystems``.</span>
<span class="sd"> Glob is supported in file path and directories will be deleted recursively.</span>
<span class="sd"> Args:</span>
<span class="sd"> file_paths: A list of strings contains file paths or directories.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">file_paths</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Clean up failed. Invalid file path: </span><span class="si">%s</span><span class="s1">.&#39;</span> <span class="o">%</span>
<span class="n">file_paths</span><span class="p">)</span>
<span class="n">FileSystems</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">file_paths</span><span class="p">)</span>
<div class="viewcode-block" id="wait_for_subscriptions_created"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.wait_for_subscriptions_created">[docs]</a><span class="k">def</span> <span class="nf">wait_for_subscriptions_created</span><span class="p">(</span><span class="n">subs</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">60</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Wait for all PubSub subscriptions are created.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">_wait_until_all_exist</span><span class="p">(</span><span class="n">subs</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span></div>
<div class="viewcode-block" id="wait_for_topics_created"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.wait_for_topics_created">[docs]</a><span class="k">def</span> <span class="nf">wait_for_topics_created</span><span class="p">(</span><span class="n">topics</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">60</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Wait for all PubSub topics are created.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">_wait_until_all_exist</span><span class="p">(</span><span class="n">topics</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span></div>
<span class="k">def</span> <span class="nf">_wait_until_all_exist</span><span class="p">(</span><span class="n">components</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
<span class="n">unchecked_components</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">components</span><span class="p">)</span>
<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="k">while</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span> <span class="o">&lt;=</span> <span class="n">timeout</span><span class="p">:</span>
<span class="n">unchecked_components</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span>
<span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">unchecked_components</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">c</span><span class="o">.</span><span class="n">exists</span><span class="p">()])</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">unchecked_components</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
<span class="s1">&#39;Timeout after </span><span class="si">%d</span><span class="s1"> seconds. </span><span class="si">%d</span><span class="s1"> of </span><span class="si">%d</span><span class="s1"> topics/subscriptions not exist. &#39;</span>
<span class="s1">&#39;They are </span><span class="si">%s</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">timeout</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">unchecked_components</span><span class="p">),</span>
<span class="nb">len</span><span class="p">(</span><span class="n">components</span><span class="p">),</span> <span class="nb">list</span><span class="p">(</span><span class="n">unchecked_components</span><span class="p">)))</span>
<div class="viewcode-block" id="cleanup_subscriptions"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.cleanup_subscriptions">[docs]</a><span class="k">def</span> <span class="nf">cleanup_subscriptions</span><span class="p">(</span><span class="n">subs</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Cleanup PubSub subscriptions if exist.&quot;&quot;&quot;</span>
<span class="n">_cleanup_pubsub</span><span class="p">(</span><span class="n">subs</span><span class="p">)</span></div>
<div class="viewcode-block" id="cleanup_topics"><a class="viewcode-back" href="../../../apache_beam.testing.test_utils.html#apache_beam.testing.test_utils.cleanup_topics">[docs]</a><span class="k">def</span> <span class="nf">cleanup_topics</span><span class="p">(</span><span class="n">topics</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Cleanup PubSub topics if exist.&quot;&quot;&quot;</span>
<span class="n">_cleanup_pubsub</span><span class="p">(</span><span class="n">topics</span><span class="p">)</span></div>
<span class="k">def</span> <span class="nf">_cleanup_pubsub</span><span class="p">(</span><span class="n">components</span><span class="p">):</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">components</span><span class="p">:</span>
<span class="k">if</span> <span class="n">c</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
<span class="n">c</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Cannot delete topic/subscription. </span><span class="si">%s</span><span class="s1"> does not exist.&#39;</span><span class="p">,</span>
<span class="n">c</span><span class="o">.</span><span class="n">full_name</span><span class="p">)</span>
</pre></div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright .
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../../../',
VERSION:'',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../_static/doctools.js"></script>
<script type="text/javascript" src="../../../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>