forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestUniversalRunner.java
125 lines (110 loc) · 4.7 KB
/
TestUniversalRunner.java
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
/*
* 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.
*/
package org.apache.beam.runners.portability.testing;
import static org.hamcrest.MatcherAssert.assertThat;
import com.google.auto.service.AutoService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.beam.runners.portability.PortableRunner;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.PipelineRunner;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.options.PipelineOptionsRegistrar;
import org.apache.beam.sdk.options.PortablePipelineOptions;
import org.apache.beam.sdk.runners.PipelineRunnerRegistrar;
import org.apache.beam.sdk.testing.TestPipelineOptions;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets;
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
import org.hamcrest.Matchers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** A {@link PipelineRunner} a {@link Pipeline} against a {@code JobService}. */
public class TestUniversalRunner extends PipelineRunner<PipelineResult> {
private static final Logger LOG = LoggerFactory.getLogger(TestUniversalRunner.class);
private final PipelineOptions options;
private TestUniversalRunner(PipelineOptions options) {
this.options = options;
}
/**
* Constructs a runner from the provided options.
*
* @param options Properties which configure the runner.
* @return The newly created runner.
*/
public static TestUniversalRunner fromOptions(PipelineOptions options) {
return new TestUniversalRunner(options);
}
@Override
public PipelineResult run(Pipeline pipeline) {
Options testOptions = options.as(Options.class);
if (testOptions.getLocalJobServicePortFile() != null) {
String localServicePortFilePath = testOptions.getLocalJobServicePortFile();
try {
testOptions.setJobEndpoint(
"localhost:"
+ new String(
Files.readAllBytes(Paths.get(localServicePortFilePath)), Charsets.UTF_8)
.trim());
} catch (IOException e) {
throw new RuntimeException(
String.format("Error reading local job service port file %s", localServicePortFilePath),
e);
}
}
PortablePipelineOptions portableOptions = options.as(PortablePipelineOptions.class);
portableOptions.setRunner(PortableRunner.class);
PortableRunner runner = PortableRunner.fromOptions(portableOptions);
PipelineResult result = runner.run(pipeline);
assertThat(
"Pipeline did not succeed.",
result.waitUntilFinish(),
Matchers.is(PipelineResult.State.DONE));
return result;
}
public interface Options extends TestPipelineOptions, PortablePipelineOptions {
/**
* A file containing the job service port, since Gradle needs to know this filename statically
* to provide it in Beam testing options.
*
* <p>This option conflicts with {@link #getJobEndpoint()}. This option will override the job
* endpoint to be localhost at the port specified in the file.
*/
@Description("File containing local job service port.")
String getLocalJobServicePortFile();
void setLocalJobServicePortFile(String endpoint);
}
/** Register {@link Options}. */
@AutoService(PipelineOptionsRegistrar.class)
public static class OptionsRegistrar implements PipelineOptionsRegistrar {
@Override
public Iterable<Class<? extends PipelineOptions>> getPipelineOptions() {
return ImmutableList.of(Options.class);
}
}
/** Registrar for the portable runner. */
@AutoService(PipelineRunnerRegistrar.class)
public static class RunnerRegistrar implements PipelineRunnerRegistrar {
@Override
public Iterable<Class<? extends PipelineRunner<?>>> getPipelineRunners() {
return ImmutableList.of(TestUniversalRunner.class);
}
}
}