|
| 1 | +package sftp |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/dapr/components-contrib/bindings" |
| 12 | +) |
| 13 | + |
| 14 | +var connectionStringEnvKey = "DAPR_TEST_SFTP_CONNSTRING" |
| 15 | + |
| 16 | +// docker run \ |
| 17 | +// -v ./upload:/home/foo/upload \ |
| 18 | +// -p 2222:22 -d atmoz/sftp \ |
| 19 | +// foo:pass:1001 |
| 20 | +func TestIntegrationCases(t *testing.T) { |
| 21 | + connectionString := os.Getenv(connectionStringEnvKey) |
| 22 | + if connectionString == "" { |
| 23 | + t.Skipf(`sftp binding integration tests skipped. To enable this test, define the connection string using environment variable '%[1]s' (example 'export %[1]s="localhost:2222")'`, connectionStringEnvKey) |
| 24 | + } |
| 25 | + |
| 26 | + t.Run("List operation", testListOperation) |
| 27 | + t.Run("Create operation", testCreateOperation) |
| 28 | +} |
| 29 | + |
| 30 | +func testListOperation(t *testing.T) { |
| 31 | + c := Sftp{} |
| 32 | + m := bindings.Metadata{} |
| 33 | + m.Properties = map[string]string{ |
| 34 | + "rootPath": "/upload", |
| 35 | + "address": os.Getenv(connectionStringEnvKey), |
| 36 | + "username": "foo", |
| 37 | + "password": "pass", |
| 38 | + "insecureIgnoreHostKey": "true", |
| 39 | + } |
| 40 | + err := c.Init(t.Context(), m) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + r, err := c.Invoke(t.Context(), &bindings.InvokeRequest{Operation: bindings.ListOperation}) |
| 44 | + require.NoError(t, err) |
| 45 | + assert.NotNil(t, r.Data) |
| 46 | + |
| 47 | + var d []listResponse |
| 48 | + err = json.Unmarshal(r.Data, &d) |
| 49 | + require.NoError(t, err) |
| 50 | +} |
| 51 | + |
| 52 | +func testCreateOperation(t *testing.T) { |
| 53 | + c := Sftp{} |
| 54 | + m := bindings.Metadata{} |
| 55 | + m.Properties = map[string]string{ |
| 56 | + "rootPath": "/upload", |
| 57 | + "address": os.Getenv(connectionStringEnvKey), |
| 58 | + "username": "sftpuser", |
| 59 | + "password": "sftpsecret", |
| 60 | + "insecureIgnoreHostKey": "true", |
| 61 | + } |
| 62 | + err := c.Init(t.Context(), m) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + r, err := c.Invoke(t.Context(), &bindings.InvokeRequest{ |
| 66 | + Operation: bindings.CreateOperation, |
| 67 | + Data: []byte("test data 1"), |
| 68 | + Metadata: map[string]string{ |
| 69 | + "fileName": "test.txt", |
| 70 | + }, |
| 71 | + }) |
| 72 | + require.NoError(t, err) |
| 73 | + assert.NotNil(t, r.Data) |
| 74 | +} |
0 commit comments