1616import org .apache .sshd .server .session .ServerSession ;
1717import org .apache .sshd .server .subsystem .sftp .SftpSubsystemFactory ;
1818import org .embulk .EmbulkTestRuntime ;
19+ import org .embulk .config .ConfigException ;
1920import org .embulk .config .ConfigLoader ;
2021import org .embulk .config .ConfigSource ;
2122import org .embulk .config .TaskReport ;
3637import org .junit .Test ;
3738import org .junit .rules .ExpectedException ;
3839import org .junit .rules .TemporaryFolder ;
40+ import org .littleshoot .proxy .HttpProxyServer ;
41+ import org .littleshoot .proxy .impl .DefaultHttpProxyServer ;
3942import org .slf4j .Logger ;
4043
4144import java .io .File ;
@@ -74,6 +77,8 @@ public class TestSftpFileOutputPlugin
7477 private SshServer sshServer ;
7578 private static final String HOST = "127.0.0.1" ;
7679 private static final int PORT = 20022 ;
80+ private static final String PROXY_HOST = "127.0.0.1" ;
81+ private static final int PROXY_PORT = 8080 ;
7782 private static final String USERNAME = "username" ;
7883 private static final String PASSWORD = "password" ;
7984 private static final String SECRET_KEY_FILE = Resources .getResource ("id_rsa" ).getPath ();
@@ -94,13 +99,18 @@ public void createResources()
9499 SftpFileOutputPlugin sftpFileOutputPlugin = new SftpFileOutputPlugin ();
95100 runner = new FileOutputRunner (sftpFileOutputPlugin );
96101
102+ sshServer = createSshServer (HOST , PORT , USERNAME , PASSWORD );
103+ }
104+
105+ private SshServer createSshServer (String host , int port , final String sshUsername , final String sshPassword )
106+ {
97107 // setup a mock sftp server
98- sshServer = SshServer .setUpDefaultServer ();
108+ SshServer sshServer = SshServer .setUpDefaultServer ();
99109 VirtualFileSystemFactory fsFactory = new VirtualFileSystemFactory ();
100- fsFactory .setUserHomeDir (USERNAME , testFolder .getRoot ().toPath ());
110+ fsFactory .setUserHomeDir (sshUsername , testFolder .getRoot ().toPath ());
101111 sshServer .setFileSystemFactory (fsFactory );
102- sshServer .setHost (HOST );
103- sshServer .setPort (PORT );
112+ sshServer .setHost (host );
113+ sshServer .setPort (port );
104114 sshServer .setSubsystemFactories (Collections .<NamedFactory <Command >>singletonList (new SftpSubsystemFactory ()));
105115 sshServer .setCommandFactory (new ScpCommandFactory ());
106116 sshServer .setKeyPairProvider (new SimpleGeneratorHostKeyProvider ());
@@ -109,7 +119,7 @@ public void createResources()
109119 @ Override
110120 public boolean authenticate (final String username , final String password , final ServerSession session )
111121 {
112- return USERNAME .contentEquals (username ) && PASSWORD .contentEquals (password );
122+ return sshUsername .contentEquals (username ) && sshPassword .contentEquals (password );
113123 }
114124 });
115125 sshServer .setPublickeyAuthenticator (new PublickeyAuthenticator ()
@@ -127,6 +137,14 @@ public boolean authenticate(String username, PublicKey key, ServerSession sessio
127137 catch (IOException e ) {
128138 logger .debug (e .getMessage (), e );
129139 }
140+ return sshServer ;
141+ }
142+
143+ private HttpProxyServer createProxyServer (int port )
144+ {
145+ return DefaultHttpProxyServer .bootstrap ()
146+ .withPort (port )
147+ .start ();
130148 }
131149
132150 @ After
@@ -269,6 +287,49 @@ public void testConfigValuesIncludingDefault()
269287 assertEquals (pathPrefix , task .getPathPrefix ());
270288 assertEquals ("txt" , task .getFileNameExtension ());
271289 assertEquals ("%03d.%02d." , task .getSequenceFormat ());
290+ assertEquals (Optional .absent (), task .getProxy ());
291+ }
292+
293+ @ Test
294+ public void testConfigValuesIncludingProxy ()
295+ {
296+ // setting embulk config
297+ final String pathPrefix = "/test/testUserPassword" ;
298+ String configYaml = "" +
299+ "type: sftp\n " +
300+ "host: " + HOST + "\n " +
301+ "user: " + USERNAME + "\n " +
302+ "path_prefix: " + pathPrefix + "\n " +
303+ "file_ext: txt\n " +
304+ "proxy: \n " +
305+ " type: http\n " +
306+ " host: proxy_host\n " +
307+ " port: 80 \n " +
308+ " user: proxy_user\n " +
309+ " password: proxy_pass\n " +
310+ " command: proxy_command\n " +
311+ "formatter:\n " +
312+ " type: csv\n " +
313+ " newline: CRLF\n " +
314+ " newline_in_field: LF\n " +
315+ " header_line: true\n " +
316+ " charset: UTF-8\n " +
317+ " quote_policy: NONE\n " +
318+ " quote: \" \\ \" \" \n " +
319+ " escape: \" \\ \\ \" \n " +
320+ " null_string: \" \" \n " +
321+ " default_timezone: 'UTC'" ;
322+
323+ ConfigSource config = getConfigFromYaml (configYaml );
324+ PluginTask task = config .loadConfig (PluginTask .class );
325+
326+ ProxyTask proxy = task .getProxy ().get ();
327+ assertEquals ("proxy_command" , proxy .getCommand ().get ());
328+ assertEquals ("proxy_host" , proxy .getHost ().get ());
329+ assertEquals ("proxy_user" , proxy .getUser ().get ());
330+ assertEquals ("proxy_pass" , proxy .getPassword ().get ());
331+ assertEquals (80 , proxy .getPort ());
332+ assertEquals (ProxyTask .ProxyType .HTTP , proxy .getType ());
272333 }
273334
274335 // Cases
@@ -353,6 +414,60 @@ public void testUserSecretKeyFileAndPutToRootDirectory()
353414 pathPrefix ));
354415 }
355416
417+ @ Test
418+ public void testUserSecretKeyFileWithProxy ()
419+ {
420+ HttpProxyServer proxyServer = null ;
421+ try {
422+ proxyServer = createProxyServer (PROXY_PORT );
423+
424+ // setting embulk config
425+ final String pathPrefix = "/test/testUserPassword" ;
426+ String configYaml = "" +
427+ "type: sftp\n " +
428+ "host: " + HOST + "\n " +
429+ "port: " + PORT + "\n " +
430+ "user: " + USERNAME + "\n " +
431+ "secret_key_file: " + SECRET_KEY_FILE + "\n " +
432+ "secret_key_passphrase: " + SECRET_KEY_PASSPHRASE + "\n " +
433+ "path_prefix: " + testFolder .getRoot ().getAbsolutePath () + pathPrefix + "\n " +
434+ "file_ext: txt\n " +
435+ "proxy: \n " +
436+ " type: http\n " +
437+ " host: " + PROXY_HOST + "\n " +
438+ " port: " + PROXY_PORT + " \n " +
439+ " user: " + USERNAME + "\n " +
440+ " password: " + PASSWORD + "\n " +
441+ " command: \n " +
442+ "formatter:\n " +
443+ " type: csv\n " +
444+ " newline: CRLF\n " +
445+ " newline_in_field: LF\n " +
446+ " header_line: true\n " +
447+ " charset: UTF-8\n " +
448+ " quote_policy: NONE\n " +
449+ " quote: \" \\ \" \" \n " +
450+ " escape: \" \\ \\ \" \n " +
451+ " null_string: \" \" \n " +
452+ " default_timezone: 'UTC'" ;
453+
454+ // runner.transaction -> ...
455+ run (configYaml , Optional .<Integer >absent ());
456+
457+ List <String > fileList = lsR (Lists .<String >newArrayList (), Paths .get (testFolder .getRoot ().getAbsolutePath ()));
458+ assertThat (fileList , hasItem (containsString (pathPrefix + "001.00.txt" )));
459+
460+ assertRecordsInFile (String .format ("%s/%s001.00.txt" ,
461+ testFolder .getRoot ().getAbsolutePath (),
462+ pathPrefix ));
463+ }
464+ finally {
465+ if (proxyServer != null ) {
466+ proxyServer .stop ();
467+ }
468+ }
469+ }
470+
356471 @ Test
357472 public void testTimeout ()
358473 {
@@ -388,4 +503,30 @@ public void testTimeout()
388503 // runner.transaction -> ...
389504 run (configYaml , Optional .of (60 )); // sleep 1 minute while processing
390505 }
506+
507+ @ Test
508+ public void testProxyType ()
509+ {
510+ // test valueOf()
511+ assertEquals ("http" , ProxyTask .ProxyType .valueOf ("HTTP" ).toString ());
512+ assertEquals ("socks" , ProxyTask .ProxyType .valueOf ("SOCKS" ).toString ());
513+ assertEquals ("stream" , ProxyTask .ProxyType .valueOf ("STREAM" ).toString ());
514+ try {
515+ ProxyTask .ProxyType .valueOf ("non-existing-type" );
516+ }
517+ catch (Exception e ) {
518+ assertEquals (IllegalArgumentException .class , e .getClass ());
519+ }
520+
521+ // test fromString
522+ assertEquals (ProxyTask .ProxyType .HTTP , ProxyTask .ProxyType .fromString ("http" ));
523+ assertEquals (ProxyTask .ProxyType .SOCKS , ProxyTask .ProxyType .fromString ("socks" ));
524+ assertEquals (ProxyTask .ProxyType .STREAM , ProxyTask .ProxyType .fromString ("stream" ));
525+ try {
526+ ProxyTask .ProxyType .fromString ("non-existing-type" );
527+ }
528+ catch (Exception e ) {
529+ assertEquals (ConfigException .class , e .getClass ());
530+ }
531+ }
391532}
0 commit comments