-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from moov-io/use-connection-helper
fix: use the connection() helper to check for network errors
- Loading branch information
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package go_sftp_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/moov-io/base/log" | ||
sftp "github.com/moov-io/go-sftp" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNetwork(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("-short flag was provided") | ||
} | ||
|
||
client, err := sftp.NewClient(log.NewTestLogger(), &sftp.ClientConfig{ | ||
Hostname: "localhost:2222", | ||
Username: "demo", | ||
Password: "password", | ||
Timeout: 5 * time.Second, | ||
MaxConnections: 1, | ||
PacketSize: 32000, | ||
}) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
require.NoError(t, client.Close()) | ||
}) | ||
|
||
t.Run("Ping", func(t *testing.T) { | ||
require.NoError(t, client.Ping()) | ||
}) | ||
|
||
t.Run("Read after Closing", func(t *testing.T) { | ||
// Close the connection but have the caller try without knowing it's closed | ||
require.NoError(t, client.Close()) | ||
|
||
files, err := client.ListFiles("/outbox") | ||
require.NoError(t, err) | ||
require.Greater(t, len(files), 0) | ||
|
||
// close it again for fun | ||
require.NoError(t, client.Close()) | ||
|
||
// try again | ||
files, err = client.ListFiles("/outbox") | ||
require.NoError(t, err) | ||
require.Greater(t, len(files), 0) | ||
}) | ||
} |