|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import time |
| 6 | + |
| 7 | +sys.path.append(os.path.realpath(__file__ + '/../../../lib')) |
| 8 | + |
| 9 | +import udf |
| 10 | +import docker_db_environment |
| 11 | + |
| 12 | +class PysftpConnectionTest(udf.TestCase): |
| 13 | + |
| 14 | + @udf.skipIfNot(docker_db_environment.is_available, reason="This test requires a docker-db environment") |
| 15 | + def test_pysftp_connect_python3(self): |
| 16 | + self.pysftp_connect("PYTHON3") |
| 17 | + |
| 18 | + |
| 19 | + @udf.skipIfNot(docker_db_environment.is_available, reason="This test requires a docker-db environment") |
| 20 | + def test_pysftp_connect_python2(self): |
| 21 | + self.pysftp_connect("PYTHON") |
| 22 | + |
| 23 | + |
| 24 | + def pysftp_connect(self, python_version): |
| 25 | + schema="test_pysftp_connect"+python_version |
| 26 | + env=docker_db_environment.DockerDBEnvironment(schema) |
| 27 | + try: |
| 28 | + self.query(udf.fixindent("DROP SCHEMA %s CASCADE"%schema),ignore_errors=True) |
| 29 | + self.query(udf.fixindent("CREATE SCHEMA %s"%schema)) |
| 30 | + self.query(udf.fixindent("OPEN SCHEMA %s"%schema)) |
| 31 | + self.query(udf.fixindent(''' |
| 32 | + CREATE OR REPLACE {python_version} SCALAR SCRIPT connect_container(host varchar(1000), port int,username varchar(1000),password varchar(1000), input_string varchar(1000)) returns VARCHAR(100000) AS |
| 33 | + import socket |
| 34 | + import io |
| 35 | + import traceback |
| 36 | + import sys |
| 37 | + def run(ctx): |
| 38 | + import pysftp |
| 39 | + cnopts = pysftp.CnOpts() |
| 40 | + cnopts.hostkeys = None |
| 41 | + try: |
| 42 | + with pysftp.Connection(ctx.host, username=ctx.username, password=ctx.password,cnopts=cnopts) as sftp: |
| 43 | + with sftp.cd("tmp"): |
| 44 | + input_buffer = io.StringIO(ctx.input_string) |
| 45 | + sftp.putfo(input_buffer,"test_file") |
| 46 | + output_buffer = io.BytesIO() |
| 47 | + written=sftp.getfo('test_file',output_buffer) |
| 48 | + value=output_buffer.getvalue() |
| 49 | + value_decoded=value.decode("utf-8") |
| 50 | + return value_decoded |
| 51 | + except: |
| 52 | + return traceback.format_exc() |
| 53 | + |
| 54 | + / |
| 55 | + '''.format(python_version=python_version))) |
| 56 | + env.get_client().images.pull("panubo/sshd",tag="1.1.0") |
| 57 | + container=env.run(name="sshd_sftp",image="panubo/sshd:1.1.0",environment=["SSH_USERS=test_user:1000:1000","SSH_ENABLE_PASSWORD_AUTH=true","SFTP_MODE=true"], |
| 58 | + tmpfs={'/data': 'size=1M,uid=0'}) |
| 59 | + print(container.logs()) |
| 60 | + time.sleep(10) |
| 61 | + print(container.logs()) |
| 62 | + result=container.exec_run(cmd=''' sh -c "echo 'test_user:test_user' | chpasswd" ''') |
| 63 | + result=container.exec_run(cmd='''mkdir /data/tmp''') |
| 64 | + result=container.exec_run(cmd='''chmod 777 /data/tmp''') |
| 65 | + time.sleep(5) |
| 66 | + print(result) |
| 67 | + host=env.get_ip_address_of_container(container) |
| 68 | + rows=self.query("select connect_container('%s',%s,'test_user','test_user','success')"%(host,22)) |
| 69 | + self.assertRowsEqual([("success",)], rows) |
| 70 | + print(container.logs()) |
| 71 | + finally: |
| 72 | + try: |
| 73 | + self.query(udf.fixindent("DROP SCHEMA %s CASCADE"%schema)) |
| 74 | + except: |
| 75 | + pass |
| 76 | + try: |
| 77 | + env.close() |
| 78 | + except: |
| 79 | + pass |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + udf.main() |
| 84 | + |
0 commit comments