|
| 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 | +// Run docker from the file location as the upload folder is relative to the test |
| 17 | +// docker run -v ./upload:/home/foo/upload -p 2222:22 -d atmoz/sftp foo:pass:1001 |
| 18 | +func TestIntegrationCases(t *testing.T) { |
| 19 | + connectionString := os.Getenv(connectionStringEnvKey) |
| 20 | + if connectionString == "" { |
| 21 | + 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) |
| 22 | + } |
| 23 | + |
| 24 | + t.Run("List operation", testListOperation) |
| 25 | + t.Run("Create operation", testCreateOperation) |
| 26 | +} |
| 27 | + |
| 28 | +func testListOperation(t *testing.T) { |
| 29 | + c := Sftp{} |
| 30 | + m := bindings.Metadata{} |
| 31 | + m.Properties = map[string]string{ |
| 32 | + "rootPath": "/upload", |
| 33 | + "address": os.Getenv(connectionStringEnvKey), |
| 34 | + "username": "foo", |
| 35 | + "password": "pass", |
| 36 | + "insecureIgnoreHostKey": "true", |
| 37 | + } |
| 38 | + err := c.Init(t.Context(), m) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + r, err := c.Invoke(t.Context(), &bindings.InvokeRequest{Operation: bindings.ListOperation}) |
| 42 | + require.NoError(t, err) |
| 43 | + assert.NotNil(t, r.Data) |
| 44 | + |
| 45 | + var d []listResponse |
| 46 | + err = json.Unmarshal(r.Data, &d) |
| 47 | + require.NoError(t, err) |
| 48 | +} |
| 49 | + |
| 50 | +func testCreateOperation(t *testing.T) { |
| 51 | + c := Sftp{} |
| 52 | + m := bindings.Metadata{} |
| 53 | + m.Properties = map[string]string{ |
| 54 | + "rootPath": "/upload", |
| 55 | + "address": os.Getenv(connectionStringEnvKey), |
| 56 | + "username": "foo", |
| 57 | + "password": "pass", |
| 58 | + "insecureIgnoreHostKey": "true", |
| 59 | + } |
| 60 | + |
| 61 | + err := os.Remove("./upload/test.txt") |
| 62 | + if err != nil && !os.IsNotExist(err) { |
| 63 | + require.NoError(t, err) |
| 64 | + } |
| 65 | + |
| 66 | + err = c.Init(t.Context(), m) |
| 67 | + require.NoError(t, err) |
| 68 | + |
| 69 | + r, err := c.Invoke(t.Context(), &bindings.InvokeRequest{ |
| 70 | + Operation: bindings.CreateOperation, |
| 71 | + Data: []byte("test data 1"), |
| 72 | + Metadata: map[string]string{ |
| 73 | + "fileName": "test.txt", |
| 74 | + }, |
| 75 | + }) |
| 76 | + require.NoError(t, err) |
| 77 | + assert.NotNil(t, r.Data) |
| 78 | + |
| 79 | + file, err := os.Stat("./upload/test.txt") |
| 80 | + require.NoError(t, err) |
| 81 | + assert.Equal(t, "test.txt", file.Name()) |
| 82 | +} |
0 commit comments