embulk-input-remote 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,255 +0,0 @@
1
- package org.embulk.input;
2
-
3
- import ch.qos.logback.classic.Level;
4
- import ch.qos.logback.classic.Logger;
5
- import com.github.dockerjava.api.DockerClient;
6
- import com.github.dockerjava.api.model.Container;
7
- import com.github.dockerjava.core.DockerClientBuilder;
8
- import org.embulk.EmbulkEmbed;
9
- import org.embulk.config.ConfigSource;
10
- import org.embulk.spi.InputPlugin;
11
- import org.embulk.test.MemoryOutputPlugin;
12
- import org.embulk.test.MyEmbulkTests;
13
- import org.embulk.test.MyTestingEmbulk;
14
- import org.embulk.test.TestingEmbulk;
15
- import org.junit.Before;
16
- import org.junit.Ignore;
17
- import org.junit.Rule;
18
- import org.junit.Test;
19
- import org.slf4j.LoggerFactory;
20
-
21
- import java.util.Arrays;
22
- import java.util.Collections;
23
- import java.util.HashSet;
24
- import java.util.List;
25
- import java.util.Set;
26
-
27
- import static org.hamcrest.Matchers.is;
28
- import static org.junit.Assert.assertThat;
29
-
30
- public class TestRemoteFileInputPlugin {
31
- private static final String CONTAINER_ID_HOST1 = "embulkinputremote_host1_1";
32
- private static final String CONTAINER_ID_HOST2 = "embulkinputremote_host2_1";
33
- private static final DockerClient dockerClient = DockerClientBuilder.getInstance().build();
34
-
35
- @Rule
36
- public MyTestingEmbulk embulk = (MyTestingEmbulk) MyTestingEmbulk
37
- .builder()
38
- .registerPlugin(InputPlugin.class, "remote", RemoteFileInputPlugin.class)
39
- .build();
40
-
41
- @Before
42
- public void prepare() {
43
- startContainer(CONTAINER_ID_HOST1);
44
- startContainer(CONTAINER_ID_HOST2);
45
-
46
- String logLevel = System.getenv("LOG_LEVEL");
47
- if (logLevel != null) {
48
- // Set log level
49
- Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
50
- rootLogger.setLevel(Level.toLevel(logLevel));
51
- }
52
- }
53
-
54
- @Test
55
- public void loadFromRemote() throws Exception
56
- {
57
- embulk.runInput(baseConfig());
58
- assertValues(values(1L, "user1"));
59
- }
60
-
61
- @Ignore("Cannot pass on TravisCI, although pass on Local Mac OS...")
62
- @Test
63
- public void loadFromRemoteViaPublicKey() throws Exception
64
- {
65
- String keyPath = System.getenv("KEY_PATH");
66
- if (keyPath == null) {
67
- keyPath = "./id_rsa_test";
68
- }
69
-
70
- final ConfigSource publicKeyAuth = newConfig().set("auth", newConfig()
71
- .set("type", "public_key")
72
- .set("key_path", keyPath)
73
- );
74
- embulk.runInput(baseConfig().merge(publicKeyAuth));
75
-
76
- assertValues(values(1L, "user1"));
77
- }
78
-
79
- @Test
80
- public void testMultiHosts() throws Exception
81
- {
82
- final ConfigSource multiHosts = newConfig()
83
- .set("hosts", Arrays.asList("localhost:10022", "localhost:10023"));
84
- final ConfigSource config = baseConfig().merge(multiHosts);
85
-
86
- // Run
87
- embulk.runInput(config);
88
- assertValues(
89
- values(1L, "user1"),
90
- values(2L, "user2")
91
- );
92
- }
93
-
94
- @Test
95
- public void loadAllFilesInDirectory() throws Exception
96
- {
97
- final ConfigSource multiHosts = newConfig()
98
- .set("path", "/mount");
99
- final ConfigSource config = baseConfig().merge(multiHosts);
100
-
101
- embulk.runInput(config);
102
- assertValues(
103
- values(1L, "user1"),
104
- values(1L, "command_user1")
105
- );
106
- }
107
-
108
- @Test
109
- public void testDefaultPort() throws Exception
110
- {
111
- final ConfigSource defaultPort = newConfig()
112
- .set("hosts", Collections.singletonList("localhost"))
113
- .set("default_port", 10022);
114
-
115
- embulk.runInput(baseConfig().merge(defaultPort));
116
-
117
- assertValues(values(1L, "user1"));
118
- }
119
-
120
- @Test
121
- public void testConfDiff() throws Exception
122
- {
123
- final ConfigSource host2Config = newConfig()
124
- .set("hosts", Collections.singletonList("localhost:10023"));
125
- ConfigSource config = baseConfig().merge(host2Config);
126
-
127
- // Run
128
- TestingEmbulk.RunResult runResult = embulk.runInput(config);
129
- assertValues(values(2L, "user2"));
130
-
131
- // Re-run with additional host1
132
- final ConfigSource multiHost = newConfig()
133
- .set("hosts", Arrays.asList("localhost:10022", "localhost:10023"));
134
- config = baseConfig().merge(multiHost);
135
-
136
- embulk.runInput(config, runResult.getConfigDiff());
137
-
138
- assertValues(values(1L, "user1"));
139
- }
140
-
141
- @Test
142
- public void testResume() throws Exception
143
- {
144
- final ConfigSource multiHost = newConfig()
145
- .set("hosts", Arrays.asList("localhost:10022", "localhost:10023"));
146
- final ConfigSource config = baseConfig().merge(multiHost);
147
-
148
- // Stop host2 temporarily
149
- stopContainer(CONTAINER_ID_HOST2);
150
-
151
- // Run (but will fail)
152
- EmbulkEmbed.ResumableResult resumableResult = embulk.resume(config);
153
-
154
- assertThat(resumableResult.isSuccessful(), is(false));
155
- assertValues(values(1L, "user1"));
156
-
157
- // Start host2 again
158
- startContainer(CONTAINER_ID_HOST2);
159
-
160
- // Resume
161
- resumableResult = embulk.resume(config, resumableResult.getResumeState());
162
-
163
- assertThat(resumableResult.isSuccessful(), is(true));
164
- assertValues(values(2L, "user2"));
165
- }
166
-
167
- @Test
168
- public void testIgnoreNotFoundHosts() throws Exception
169
- {
170
- final ConfigSource ignoreNotFoundHosts = newConfig()
171
- .set("hosts", Arrays.asList("localhost:10022", "localhost:10023"))
172
- .set("ignore_not_found_hosts", true);
173
- final ConfigSource config = baseConfig().merge(ignoreNotFoundHosts);
174
-
175
- // Stop host2
176
- stopContainer(CONTAINER_ID_HOST2);
177
-
178
- // Run (host2 will be ignored)
179
- EmbulkEmbed.ResumableResult resumableResult = embulk.resume(config);
180
-
181
- assertThat(resumableResult.isSuccessful(), is(true));
182
- assertValues(values(1L, "user1"));
183
- }
184
-
185
- @Test
186
- public void testCommandOptions() throws Exception
187
- {
188
- final ConfigSource ignoreNotFoundHosts = newConfig()
189
- .set("hosts_command", "echo localhost:10022,localhost:10023")
190
- .set("hosts_separator", ",")
191
- .set("path_command", "echo /mount/test_command.csv");
192
- final ConfigSource config = baseConfig().merge(ignoreNotFoundHosts);
193
-
194
- embulk.runInput(config);
195
-
196
- assertValues(
197
- values(1L, "command_user1"),
198
- values(2L, "command_user2")
199
- );
200
- }
201
-
202
- private ConfigSource baseConfig() {
203
- return MyEmbulkTests.configFromResource("yaml/base.yml");
204
- }
205
-
206
- private ConfigSource newConfig() {
207
- return embulk.newConfig();
208
- }
209
-
210
- private void assertValues(List... valuesList) {
211
- Set<List> actual = new HashSet<>();
212
- for (MemoryOutputPlugin.Record record : MemoryOutputPlugin.getRecords()) {
213
- actual.add(record.getValues());
214
- }
215
-
216
- Set<List> expected = new HashSet<>();
217
- Collections.addAll(expected, valuesList);
218
-
219
- assertThat(actual, is(expected));
220
- }
221
-
222
- private List values(Object... values) {
223
- return Arrays.asList(values);
224
- }
225
-
226
- //////////////////////////////
227
- // Methods for Docker
228
- //////////////////////////////
229
-
230
- private static void stopContainer(String containerId) {
231
- if (isRunning(containerId)) {
232
- dockerClient.stopContainerCmd(containerId).exec();
233
- }
234
- }
235
-
236
- private static void startContainer(String containerId) {
237
- if (!isRunning(containerId)) {
238
- dockerClient.startContainerCmd(containerId).exec();
239
- }
240
- }
241
-
242
- private static boolean isRunning(String containerId) {
243
- List<Container> containers = dockerClient.listContainersCmd().exec();
244
- for (Container container : containers) {
245
- for (String name : container.getNames()) {
246
- if (name.contains(containerId)) {
247
- System.out.println("Found " + containerId);
248
- return true;
249
- }
250
- }
251
- }
252
- System.out.println("Not Found " + containerId);
253
- return false;
254
- }
255
- }
@@ -1,143 +0,0 @@
1
- package org.embulk.test;
2
-
3
- import com.google.common.collect.ImmutableList;
4
- import org.embulk.config.ConfigDiff;
5
- import org.embulk.config.ConfigSource;
6
- import org.embulk.config.Task;
7
- import org.embulk.config.TaskReport;
8
- import org.embulk.config.TaskSource;
9
- import org.embulk.spi.Column;
10
- import org.embulk.spi.Exec;
11
- import org.embulk.spi.OutputPlugin;
12
- import org.embulk.spi.Page;
13
- import org.embulk.spi.PageReader;
14
- import org.embulk.spi.Schema;
15
- import org.embulk.spi.TransactionalPageOutput;
16
- import org.embulk.spi.util.Pages;
17
-
18
- import java.util.ArrayList;
19
- import java.util.List;
20
-
21
- public class MemoryOutputPlugin implements OutputPlugin
22
- {
23
- public interface PluginTask extends Task { }
24
-
25
- @Override
26
- public ConfigDiff transaction(ConfigSource config,
27
- Schema schema, int taskCount,
28
- OutputPlugin.Control control)
29
- {
30
- final PluginTask task = config.loadConfig(PluginTask.class);
31
- return resume(task.dump(), schema, taskCount, control);
32
- }
33
-
34
- @Override
35
- public ConfigDiff resume(TaskSource taskSource,
36
- Schema schema, int taskCount,
37
- OutputPlugin.Control control)
38
- {
39
- control.run(taskSource);
40
- return Exec.newConfigDiff();
41
- }
42
-
43
- @Override
44
- public void cleanup(TaskSource taskSource,
45
- Schema schema, int taskCount,
46
- List<TaskReport> successTaskReports)
47
- { }
48
-
49
- @Override
50
- public TransactionalPageOutput open(final TaskSource taskSource, final Schema schema, final int taskIndex)
51
- {
52
- return new TransactionalPageOutput()
53
- {
54
- private final PageReader reader = new PageReader(schema);
55
-
56
- public void add(Page page)
57
- {
58
- reader.setPage(page);
59
- while (reader.nextRecord())
60
- {
61
- Recorder.addRecord(reader);
62
- }
63
- }
64
-
65
- public void finish() { }
66
-
67
- public void close()
68
- {
69
- reader.close();
70
- }
71
-
72
- public void abort() { }
73
-
74
- public TaskReport commit()
75
- {
76
- return Exec.newTaskReport();
77
- }
78
- };
79
- }
80
-
81
- public static void clearRecords()
82
- {
83
- Recorder.clearRecords();
84
- }
85
-
86
- public static List<Record> getRecords()
87
- {
88
- return Recorder.getRecords();
89
- }
90
-
91
- private static class Recorder
92
- {
93
- private static List<Record> records = new ArrayList<>();
94
-
95
- private Recorder() { }
96
-
97
- private synchronized static void addRecord(PageReader reader)
98
- {
99
- final ImmutableList.Builder<Object> values = ImmutableList.builder();
100
- final ImmutableList.Builder<Column> columns = ImmutableList.builder();
101
- reader.getSchema().visitColumns(new Pages.ObjectColumnVisitor(reader) {
102
- @Override
103
- public void visit(org.embulk.spi.Column column, Object value) {
104
- values.add(value);
105
- columns.add(column);
106
- }
107
- });
108
- records.add(new Record(values.build(), columns.build()));
109
- }
110
-
111
- static void clearRecords()
112
- {
113
- records = new ArrayList<>();
114
- }
115
-
116
- static List<Record> getRecords()
117
- {
118
- return new ArrayList<>(records);
119
- }
120
- }
121
-
122
- public static class Record
123
- {
124
- private final List<Object> values;
125
- private final List<Column> columns;
126
-
127
- Record(List<Object> values, List<Column> columns)
128
- {
129
- this.values = values;
130
- this.columns = columns;
131
- }
132
-
133
- public List<Object> getValues()
134
- {
135
- return values;
136
- }
137
-
138
- public List<Column> getColumns()
139
- {
140
- return columns;
141
- }
142
- }
143
- }
@@ -1,23 +0,0 @@
1
- package org.embulk.test;
2
-
3
- import org.embulk.EmbulkEmbed;
4
- import org.embulk.config.ConfigSource;
5
-
6
- import static com.google.common.base.Strings.isNullOrEmpty;
7
- import static org.embulk.test.EmbulkTests.readResource;
8
- import static org.hamcrest.Matchers.is;
9
- import static org.junit.Assume.assumeThat;
10
-
11
- public class MyEmbulkTests {
12
- private MyEmbulkTests() {
13
- }
14
-
15
- public static ConfigSource configFromString(String yaml) {
16
- assumeThat(isNullOrEmpty(yaml), is(false));
17
- return EmbulkEmbed.newSystemConfigLoader().fromYamlString(yaml);
18
- }
19
-
20
- public static ConfigSource configFromResource(String name) {
21
- return configFromString(readResource(name));
22
- }
23
- }
@@ -1,92 +0,0 @@
1
- package org.embulk.test;
2
-
3
- import org.embulk.EmbulkEmbed;
4
- import org.embulk.config.ConfigDiff;
5
- import org.embulk.config.ConfigSource;
6
- import org.embulk.exec.ResumeState;
7
- import org.embulk.spi.OutputPlugin;
8
-
9
- import java.io.IOException;
10
- import java.lang.reflect.Field;
11
-
12
- public class MyTestingEmbulk extends TestingEmbulk {
13
-
14
- public static class Builder extends TestingEmbulk.Builder {
15
- public TestingEmbulk build() {
16
- this.registerPlugin(OutputPlugin.class, "memory", MemoryOutputPlugin.class);
17
- return new MyTestingEmbulk(this);
18
- }
19
- }
20
-
21
- public static TestingEmbulk.Builder builder()
22
- {
23
- return new MyTestingEmbulk.Builder();
24
- }
25
-
26
- private final EmbulkEmbed superEmbed;
27
-
28
- MyTestingEmbulk(Builder builder) {
29
- super(builder);
30
- this.superEmbed = extractSuperField("embed");
31
- }
32
-
33
- public RunResult runInput(ConfigSource inConfig) throws IOException {
34
- return runInput(inConfig, (ConfigDiff) null);
35
- }
36
-
37
- public RunResult runInput(ConfigSource inConfig, ConfigDiff confDiff) throws IOException {
38
- ConfigSource execConfig = newConfig()
39
- .set("min_output_tasks", 1);
40
-
41
- ConfigSource outConfig = newConfig()
42
- .set("type", "memory");
43
-
44
- ConfigSource config = newConfig()
45
- .set("exec", execConfig)
46
- .set("in", inConfig)
47
- .set("out", outConfig);
48
-
49
- // embed.run returns TestingBulkLoader.TestingExecutionResult because
50
- MemoryOutputPlugin.clearRecords();
51
- if (confDiff == null) {
52
- return (RunResult) superEmbed.run(config);
53
- } else {
54
- return (RunResult) superEmbed.run(config.merge(confDiff));
55
- }
56
- }
57
-
58
- public EmbulkEmbed.ResumableResult resume(ConfigSource inConfig) throws IOException {
59
- return resume(inConfig, null);
60
- }
61
-
62
- public EmbulkEmbed.ResumableResult resume(ConfigSource inConfig, ResumeState resumeState) throws IOException {
63
- ConfigSource execConfig = newConfig()
64
- .set("min_output_tasks", 1);
65
-
66
- ConfigSource outConfig = newConfig()
67
- .set("type", "memory");
68
-
69
- ConfigSource config = newConfig()
70
- .set("exec", execConfig)
71
- .set("in", inConfig)
72
- .set("out", outConfig);
73
-
74
- MemoryOutputPlugin.clearRecords();
75
- if (resumeState == null) {
76
- return superEmbed.runResumable(config);
77
- } else {
78
- return superEmbed.new ResumeStateAction(config, resumeState).resume();
79
- }
80
- }
81
-
82
- @SuppressWarnings("unchecked")
83
- private <T> T extractSuperField(String fieldName) {
84
- try {
85
- Field field = TestingEmbulk.class.getDeclaredField(fieldName);
86
- field.setAccessible(true);
87
- return (T) field.get(this);
88
- } catch (NoSuchFieldException | IllegalAccessException e) {
89
- throw new RuntimeException(e);
90
- }
91
- }
92
- }