-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathenvironments_test.py
More file actions
146 lines (129 loc) · 5.93 KB
/
environments_test.py
File metadata and controls
146 lines (129 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -- coding: utf-8 --
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Unit tests for the transform.environments classes."""
# pytype: skip-file
import logging
import tempfile
import unittest
from unittest.mock import MagicMock
from apache_beam.options.pipeline_options import PortableOptions
from apache_beam.portability import common_urns
from apache_beam.runners import pipeline_context
from apache_beam.transforms import environments
from apache_beam.transforms.environments import DockerEnvironment
from apache_beam.transforms.environments import EmbeddedPythonEnvironment
from apache_beam.transforms.environments import EmbeddedPythonGrpcEnvironment
from apache_beam.transforms.environments import Environment
from apache_beam.transforms.environments import ExternalEnvironment
from apache_beam.transforms.environments import ProcessEnvironment
from apache_beam.transforms.environments import SubprocessSDKEnvironment
class RunnerApiTest(unittest.TestCase):
def test_environment_encoding(self):
for environment in (DockerEnvironment(),
DockerEnvironment(container_image='img'),
DockerEnvironment(capabilities=['x, y, z']),
ProcessEnvironment('run.sh'),
ProcessEnvironment('run.sh',
os='linux',
arch='amd64',
env={'k1': 'v1'}),
ExternalEnvironment('localhost:8080'),
ExternalEnvironment('localhost:8080',
params={'k1': 'v1'}),
EmbeddedPythonEnvironment(),
EmbeddedPythonGrpcEnvironment(),
EmbeddedPythonGrpcEnvironment(
state_cache_size=0, data_buffer_time_limit_ms=0),
SubprocessSDKEnvironment(command_string='foö')):
context = pipeline_context.PipelineContext()
proto = environment.to_runner_api(context)
reconstructed = Environment.from_runner_api(proto, context)
self.assertEqual(environment, reconstructed)
self.assertEqual(proto, reconstructed.to_runner_api(context))
def test_sdk_capabilities(self):
sdk_capabilities = environments.python_sdk_capabilities()
self.assertIn(common_urns.coders.LENGTH_PREFIX.urn, sdk_capabilities)
self.assertIn(
common_urns.protocols.HARNESS_MONITORING_INFOS.urn, sdk_capabilities)
self.assertIn(common_urns.protocols.WORKER_STATUS.urn, sdk_capabilities)
self.assertIn(
common_urns.sdf_components.TRUNCATE_SIZED_RESTRICTION.urn,
sdk_capabilities)
self.assertIn(common_urns.primitives.TO_STRING.urn, sdk_capabilities)
def test_default_capabilities(self):
environment = DockerEnvironment.from_options(
PortableOptions(sdk_location='container'))
context = pipeline_context.PipelineContext()
proto = environment.to_runner_api(context)
self.assertEqual(
set(proto.capabilities),
set(environments.python_sdk_docker_capabilities()))
class EnvironmentOptionsTest(unittest.TestCase):
def setUp(self) -> None:
self.tmp_dir = tempfile.TemporaryDirectory()
self.actual_mkdtemp = tempfile.mkdtemp
tempfile.mkdtemp = MagicMock(return_value=self.tmp_dir.name)
def tearDown(self) -> None:
tempfile.mkdtemp = self.actual_mkdtemp
self.tmp_dir.cleanup()
def test_process_variables_empty(self):
options = PortableOptions([
'--environment_type=PROCESS',
'--environment_option=process_command=foo',
'--sdk_location=container'
])
environment = ProcessEnvironment.from_options(options)
self.assertEqual(environment.command, 'foo')
self.assertEqual(environment.env, {})
def test_process_variables_set(self):
options = PortableOptions([
'--environment_type=PROCESS',
'--environment_option=process_command=foo',
'--environment_option=process_variables='
'BASH_VARIABLE_ONE=spam,BASH_VARIABLE_TWO=ham',
'--sdk_location=container'
])
environment = ProcessEnvironment.from_options(options)
self.assertEqual(environment.command, 'foo')
self.assertEqual(
environment.env, {
'BASH_VARIABLE_ONE': 'spam', 'BASH_VARIABLE_TWO': 'ham'
})
def test_process_variables_missing_rvalue(self):
with self.assertRaises(ValueError):
options = PortableOptions([
'--environment_type=PROCESS',
'--environment_option=process_command=foo',
'--environment_option=process_variables='
'BASH_VARIABLE_ONE=spam,MISSING_RVALUE',
'--sdk_location=container'
])
ProcessEnvironment.from_options(options)
def test_environments_with_same_hints_are_equal(self):
options = PortableOptions([
'--environment_type=PROCESS',
'--environment_option=process_command=foo',
'--sdk_location=container',
'--resource_hint=accelerator=gpu',
])
environment1 = ProcessEnvironment.from_options(options)
environment2 = ProcessEnvironment.from_options(options)
self.assertEqual(environment1, environment2)
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
unittest.main()