2016-10-19 16:23:28 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-01 11:40:23 +08:00
|
|
|
"io"
|
2020-05-21 23:08:17 +08:00
|
|
|
"log"
|
2016-12-30 10:44:49 +08:00
|
|
|
"os"
|
2016-12-30 21:50:39 +08:00
|
|
|
"os/exec"
|
2016-12-30 10:44:49 +08:00
|
|
|
"os/user"
|
2017-03-25 20:19:07 +08:00
|
|
|
"path/filepath"
|
2019-09-28 16:59:01 +08:00
|
|
|
"reflect"
|
2016-10-19 16:23:28 +08:00
|
|
|
"testing"
|
2019-03-07 13:56:40 +08:00
|
|
|
"time"
|
2017-02-13 11:09:14 +08:00
|
|
|
|
2017-03-25 22:46:03 +08:00
|
|
|
"github.com/appleboy/easyssh-proxy"
|
2017-02-13 11:09:14 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-21 23:08:17 +08:00
|
|
|
"golang.org/x/crypto/ssh"
|
2016-10-19 16:23:28 +08:00
|
|
|
)
|
|
|
|
|
2016-12-30 10:44:49 +08:00
|
|
|
func TestMissingAllConfig(t *testing.T) {
|
2016-10-19 16:23:28 +08:00
|
|
|
var plugin Plugin
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingSSHConfig(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2016-12-19 15:23:16 +08:00
|
|
|
Host: []string{"example.com"},
|
2016-10-19 16:23:28 +08:00
|
|
|
Username: "ubuntu",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-10-20 13:24:23 +08:00
|
|
|
func TestMissingSourceConfig(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2016-12-19 15:23:16 +08:00
|
|
|
Host: []string{"example.com"},
|
2016-10-20 13:24:23 +08:00
|
|
|
Username: "ubuntu",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 443,
|
2016-10-20 13:24:23 +08:00
|
|
|
Password: "1234",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-10-19 16:23:28 +08:00
|
|
|
func TestTrimElement(t *testing.T) {
|
|
|
|
var input, result []string
|
|
|
|
|
|
|
|
input = []string{"1", " ", "3"}
|
|
|
|
result = []string{"1", "3"}
|
|
|
|
|
2023-04-09 09:51:08 +08:00
|
|
|
assert.Equal(t, result, trimValues(input))
|
2016-10-19 16:23:28 +08:00
|
|
|
|
|
|
|
input = []string{"1", "2"}
|
|
|
|
result = []string{"1", "2"}
|
|
|
|
|
2023-04-09 09:51:08 +08:00
|
|
|
assert.Equal(t, result, trimValues(input))
|
2016-10-19 16:23:28 +08:00
|
|
|
}
|
2016-12-30 10:44:49 +08:00
|
|
|
|
2016-12-30 21:50:39 +08:00
|
|
|
func TestSCPFileFromPublicKey(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
2019-01-18 15:12:36 +08:00
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
2016-12-30 21:50:39 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 10:44:49 +08:00
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2017-03-06 12:35:57 +08:00
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-03-06 12:35:57 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
2017-03-25 20:19:07 +08:00
|
|
|
Target: []string{filepath.Join(u.HomeDir, "/test")},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2016-12-30 10:44:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/a.txt")); os.IsNotExist(err) {
|
2016-12-30 10:44:49 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/b.txt")); os.IsNotExist(err) {
|
2016-12-30 10:44:49 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test -rm flag
|
|
|
|
plugin.Config.Source = []string{"tests/a.txt"}
|
|
|
|
plugin.Config.Remove = true
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test/tests/b.txt")); os.IsExist(err) {
|
2016-12-30 10:44:49 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:34:16 +08:00
|
|
|
func TestSCPFileFromPublicKeyWithPassphrase(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2020-01-20 22:34:16 +08:00
|
|
|
KeyPath: "tests/.ssh/test",
|
|
|
|
Passphrase: "1234",
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "/test2")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-05-21 23:08:17 +08:00
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test2/tests/a.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test2/tests/b.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrongFingerprint(t *testing.T) {
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2020-05-21 23:08:17 +08:00
|
|
|
KeyPath: "./tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "/test2")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
Fingerprint: "wrong",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
log.Println(err)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostPublicKeyFile(keypath string) (ssh.PublicKey, error) {
|
|
|
|
var pubkey ssh.PublicKey
|
|
|
|
var err error
|
2022-12-29 21:10:16 +08:00
|
|
|
buf, err := os.ReadFile(keypath)
|
2020-05-21 23:08:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey, _, _, _, err = ssh.ParseAuthorizedKey(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pubkey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSCPFileFromPublicKeyWithFingerprint(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostKey, err := getHostPublicKeyFile("/etc/ssh/ssh_host_rsa_key.pub")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2020-05-21 23:08:17 +08:00
|
|
|
KeyPath: "./tests/.ssh/id_rsa",
|
|
|
|
Fingerprint: ssh.FingerprintSHA256(hostKey),
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "/test2")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-01-20 22:34:16 +08:00
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test2/tests/a.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "/test2/tests/b.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 11:09:14 +08:00
|
|
|
func TestSCPWildcardFileList(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
2019-01-18 15:12:36 +08:00
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
2017-02-13 11:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2017-03-06 12:35:57 +08:00
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-03-06 12:35:57 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
2017-03-25 20:19:07 +08:00
|
|
|
Target: []string{filepath.Join(u.HomeDir, "abc")},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2017-02-13 11:09:14 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "abc/tests/global/c.txt")); os.IsNotExist(err) {
|
2017-02-13 11:09:14 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "abc/tests/global/d.txt")); os.IsNotExist(err) {
|
2017-02-13 11:09:14 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 14:43:52 +08:00
|
|
|
func TestSCPFromProxySetting(t *testing.T) {
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-03-06 14:43:52 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
2017-03-25 20:19:07 +08:00
|
|
|
Target: []string{filepath.Join(u.HomeDir, "def")},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2017-03-25 22:46:03 +08:00
|
|
|
Proxy: easyssh.DefaultConfig{
|
2017-03-06 14:43:52 +08:00
|
|
|
Server: "localhost",
|
|
|
|
User: "drone-scp",
|
|
|
|
Port: "22",
|
|
|
|
KeyPath: "./tests/.ssh/id_rsa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "def/tests/global/c.txt")); os.IsNotExist(err) {
|
2017-03-06 14:43:52 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:07 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "def/tests/global/d.txt")); os.IsNotExist(err) {
|
2017-03-06 14:43:52 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:18:22 +08:00
|
|
|
func TestStripComponentsFlag(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
2019-01-18 15:12:36 +08:00
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
2017-04-30 23:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-04-30 23:18:22 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
|
|
|
StripComponents: 2,
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "123")},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2017-04-30 23:18:22 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/c.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/d.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 14:17:43 +08:00
|
|
|
func TestUseInsecureCipherFlag(t *testing.T) {
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2020-05-24 14:17:43 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
|
|
|
StripComponents: 2,
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "123")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
UseInsecureCipher: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/c.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123/d.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:12:36 +08:00
|
|
|
func TestIgnoreList(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2019-01-18 15:12:36 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
2019-01-18 16:40:50 +08:00
|
|
|
Source: []string{"tests/global/*", "!tests/global/c.txt", "!tests/global/e.txt"},
|
2019-01-18 15:12:36 +08:00
|
|
|
StripComponents: 2,
|
2019-01-18 16:06:22 +08:00
|
|
|
Target: []string{filepath.Join(u.HomeDir, "ignore")},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2019-01-18 15:12:36 +08:00
|
|
|
TarExec: "tar",
|
2019-09-28 14:52:31 +08:00
|
|
|
Debug: true,
|
2019-01-18 15:12:36 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
2023-04-09 09:51:08 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/c.txt")); !os.IsNotExist(err) {
|
2019-01-18 16:06:22 +08:00
|
|
|
t.Fatal("c.txt file exist")
|
2019-01-18 15:12:36 +08:00
|
|
|
}
|
|
|
|
|
2019-01-18 16:40:50 +08:00
|
|
|
// check file exist
|
2023-04-09 09:51:08 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/e.txt")); !os.IsNotExist(err) {
|
2019-01-18 16:40:50 +08:00
|
|
|
t.Fatal("c.txt file exist")
|
|
|
|
}
|
|
|
|
|
2019-01-18 16:06:22 +08:00
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "ignore/d.txt")); os.IsNotExist(err) {
|
2019-01-18 15:12:36 +08:00
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 21:50:39 +08:00
|
|
|
// func TestSCPFileFromSSHAgent(t *testing.T) {
|
|
|
|
// if os.Getenv("SSH_AUTH_SOCK") == "" {
|
|
|
|
// exec.Command("eval", "`ssh-agent -s`").Run()
|
|
|
|
// exec.Command("ssh-add", "tests/.ssh/id_rsa").Run()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// u, err := user.Lookup("drone-scp")
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatalf("Lookup: %v", err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// plugin := Plugin{
|
|
|
|
// Config: Config{
|
|
|
|
// Host: []string{"localhost"},
|
|
|
|
// Username: "drone-scp",
|
|
|
|
// Port: "22",
|
|
|
|
// Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
// Target: []string{u.HomeDir + "/test"},
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err = plugin.Exec()
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func TestSCPFileFromPassword(t *testing.T) {
|
|
|
|
// u, err := user.Lookup("drone-scp")
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatalf("Lookup: %v", err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// plugin := Plugin{
|
|
|
|
// Config: Config{
|
|
|
|
// Host: []string{"localhost"},
|
|
|
|
// Username: "drone-scp",
|
|
|
|
// Port: "22",
|
|
|
|
// Password: "1234",
|
|
|
|
// Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
// Target: []string{u.HomeDir + "/test"},
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err = plugin.Exec()
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// }
|
|
|
|
|
2016-12-30 10:44:49 +08:00
|
|
|
func TestIncorrectPassword(t *testing.T) {
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2017-03-06 12:35:57 +08:00
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-03-06 12:35:57 +08:00
|
|
|
Password: "123456",
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
Target: []string{"/home"},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2016-12-30 10:44:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plugin.Exec()
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoPermissionCreateFolder(t *testing.T) {
|
2017-03-25 20:19:07 +08:00
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:44:49 +08:00
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2017-03-06 12:35:57 +08:00
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2017-03-06 12:35:57 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/a.txt", "tests/b.txt"},
|
|
|
|
Target: []string{"/etc/test"},
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2018-11-05 15:22:19 +08:00
|
|
|
TarExec: "tar",
|
2016-12-30 10:44:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-03-25 20:19:07 +08:00
|
|
|
err = plugin.Exec()
|
2016-12-30 10:44:49 +08:00
|
|
|
assert.NotNil(t, err)
|
2017-03-25 20:19:07 +08:00
|
|
|
|
|
|
|
// check tmp file exist
|
|
|
|
if _, err = os.Stat(filepath.Join(u.HomeDir, plugin.DestFile)); os.IsExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
2016-12-30 10:44:49 +08:00
|
|
|
}
|
2017-02-13 11:09:14 +08:00
|
|
|
|
|
|
|
func TestGlobList(t *testing.T) {
|
|
|
|
// wrong patern
|
|
|
|
paterns := []string{"[]a]", "tests/?.txt"}
|
|
|
|
expects := []string{"tests/a.txt", "tests/b.txt"}
|
2019-01-18 15:12:36 +08:00
|
|
|
assert.Equal(t, expects, globList(paterns).Source)
|
2017-02-13 11:09:14 +08:00
|
|
|
|
|
|
|
paterns = []string{"tests/*.txt", "tests/.ssh/*", "abc*"}
|
2020-01-20 22:34:16 +08:00
|
|
|
expects = []string{"tests/a.txt", "tests/b.txt", "tests/.ssh/id_rsa", "tests/.ssh/id_rsa.pub", "tests/.ssh/test", "tests/.ssh/test.pub"}
|
2019-01-18 15:12:36 +08:00
|
|
|
assert.Equal(t, expects, globList(paterns).Source)
|
2017-02-13 11:09:14 +08:00
|
|
|
|
|
|
|
paterns = []string{"tests/?.txt"}
|
|
|
|
expects = []string{"tests/a.txt", "tests/b.txt"}
|
2019-01-18 15:12:36 +08:00
|
|
|
assert.Equal(t, expects, globList(paterns).Source)
|
2017-03-06 12:35:57 +08:00
|
|
|
|
2017-03-06 14:43:52 +08:00
|
|
|
// remove item which file not found.
|
2017-03-06 12:35:57 +08:00
|
|
|
paterns = []string{"tests/aa.txt", "tests/b.txt"}
|
|
|
|
expects = []string{"tests/b.txt"}
|
2019-01-18 15:12:36 +08:00
|
|
|
assert.Equal(t, expects, globList(paterns).Source)
|
2017-04-30 23:18:22 +08:00
|
|
|
|
|
|
|
paterns = []string{"./tests/b.txt"}
|
|
|
|
expects = []string{"./tests/b.txt"}
|
2019-01-18 15:12:36 +08:00
|
|
|
assert.Equal(t, expects, globList(paterns).Source)
|
|
|
|
|
|
|
|
paterns = []string{"./tests/*.txt", "!./tests/b.txt"}
|
|
|
|
expectSources := []string{"tests/a.txt", "tests/b.txt"}
|
|
|
|
expectIgnores := []string{"./tests/b.txt"}
|
|
|
|
result := globList(paterns)
|
|
|
|
assert.Equal(t, expectSources, result.Source)
|
|
|
|
assert.Equal(t, expectIgnores, result.Ignore)
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:05:13 +08:00
|
|
|
func TestRemoveDestFile(t *testing.T) {
|
|
|
|
ssh := &easyssh.MakeConfig{
|
|
|
|
Server: "localhost",
|
|
|
|
User: "drone-scp",
|
|
|
|
Port: "22",
|
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
// io timeout
|
|
|
|
Timeout: 1,
|
|
|
|
}
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
2019-03-07 13:56:40 +08:00
|
|
|
CommandTimeout: 60 * time.Second,
|
2017-03-25 23:05:13 +08:00
|
|
|
},
|
|
|
|
DestFile: "/etc/resolv.conf",
|
|
|
|
}
|
|
|
|
|
2023-04-09 08:14:05 +08:00
|
|
|
_, _, _, err := ssh.Run("ver", plugin.Config.CommandTimeout)
|
|
|
|
systemType := "unix"
|
|
|
|
if err == nil {
|
|
|
|
systemType = "windows"
|
|
|
|
}
|
|
|
|
|
2017-03-25 23:05:13 +08:00
|
|
|
// ssh io timeout
|
2023-04-09 08:14:05 +08:00
|
|
|
err = plugin.removeDestFile(systemType, ssh)
|
2017-03-25 23:05:13 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
ssh.Timeout = 0
|
|
|
|
|
|
|
|
// permission denied
|
2023-04-09 08:14:05 +08:00
|
|
|
err = plugin.removeDestFile(systemType, ssh)
|
2017-03-25 23:05:13 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2019-09-28 16:59:01 +08:00
|
|
|
|
2023-04-09 15:49:32 +08:00
|
|
|
func TestPlugin_buildUnTarArgs(t *testing.T) {
|
2019-09-28 16:59:01 +08:00
|
|
|
type fields struct {
|
|
|
|
Config Config
|
|
|
|
DestFile string
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
target string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default command",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
2022-12-29 21:15:21 +08:00
|
|
|
Overwrite: false,
|
|
|
|
UnlinkFirst: false,
|
|
|
|
TarExec: "tar",
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
2023-04-09 15:49:32 +08:00
|
|
|
DestFile: "foo.tar.gz",
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
target: "foo",
|
|
|
|
},
|
2023-04-16 12:05:23 +08:00
|
|
|
want: []string{"tar", "-zxf", "foo.tar.gz", "-C", "foo"},
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "strip components",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
Overwrite: false,
|
2022-12-29 21:15:21 +08:00
|
|
|
UnlinkFirst: false,
|
2019-09-28 16:59:01 +08:00
|
|
|
TarExec: "tar",
|
|
|
|
StripComponents: 2,
|
|
|
|
},
|
2023-04-09 15:49:32 +08:00
|
|
|
DestFile: "foo.tar.gz",
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
target: "foo",
|
|
|
|
},
|
2023-04-16 12:05:23 +08:00
|
|
|
want: []string{"tar", "-zxf", "foo.tar.gz", "--strip-components", "2", "-C", "foo"},
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "overwrite",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
StripComponents: 2,
|
|
|
|
Overwrite: true,
|
2022-12-29 21:15:21 +08:00
|
|
|
UnlinkFirst: false,
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
2023-04-09 15:49:32 +08:00
|
|
|
DestFile: "foo.tar.gz",
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
target: "foo",
|
|
|
|
},
|
2023-04-16 12:05:23 +08:00
|
|
|
want: []string{"tar", "-zxf", "foo.tar.gz", "--strip-components", "2", "--overwrite", "-C", "foo"},
|
2019-09-28 16:59:01 +08:00
|
|
|
},
|
2022-12-29 21:15:21 +08:00
|
|
|
{
|
|
|
|
name: "unlink first",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
StripComponents: 2,
|
|
|
|
Overwrite: true,
|
|
|
|
UnlinkFirst: true,
|
|
|
|
},
|
2023-04-09 15:49:32 +08:00
|
|
|
DestFile: "foo.tar.gz",
|
2022-12-29 21:15:21 +08:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
target: "foo",
|
|
|
|
},
|
2023-04-16 12:05:23 +08:00
|
|
|
want: []string{"tar", "-zxf", "foo.tar.gz", "--strip-components", "2", "--overwrite", "--unlink-first", "-C", "foo"},
|
2023-04-11 12:54:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "output folder path with space",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
StripComponents: 2,
|
|
|
|
Overwrite: true,
|
|
|
|
UnlinkFirst: true,
|
|
|
|
},
|
|
|
|
DestFile: "foo.tar.gz",
|
|
|
|
},
|
|
|
|
args: args{
|
2023-04-16 12:05:23 +08:00
|
|
|
target: "foo\\ bar",
|
2023-04-11 12:54:53 +08:00
|
|
|
},
|
2023-04-16 12:05:23 +08:00
|
|
|
want: []string{"tar", "-zxf", "foo.tar.gz", "--strip-components", "2", "--overwrite", "--unlink-first", "-C", "foo\\ bar"},
|
2022-12-29 21:15:21 +08:00
|
|
|
},
|
2019-09-28 16:59:01 +08:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &Plugin{
|
|
|
|
Config: tt.fields.Config,
|
|
|
|
DestFile: tt.fields.DestFile,
|
|
|
|
}
|
2023-04-09 15:49:32 +08:00
|
|
|
if got := p.buildUnTarArgs(tt.args.target); !reflect.DeepEqual(got, tt.want) {
|
2019-09-28 16:59:01 +08:00
|
|
|
t.Errorf("Plugin.buildArgs() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 15:49:32 +08:00
|
|
|
|
|
|
|
func TestPlugin_buildTarArgs(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Config Config
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
src string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default command",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
src: "foo.tar.gz",
|
|
|
|
},
|
|
|
|
want: []string{"-zcf", "foo.tar.gz"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ignore list",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
Source: []string{
|
|
|
|
"tests/*.txt",
|
|
|
|
"!tests/a.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
src: "foo.tar.gz",
|
|
|
|
},
|
|
|
|
want: []string{"--exclude", "tests/a.txt", "-zcf", "foo.tar.gz", "tests/a.txt", "tests/b.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "dereference flag",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
TarExec: "tar",
|
|
|
|
TarDereference: true,
|
|
|
|
Source: []string{
|
|
|
|
"tests/*.txt",
|
|
|
|
"!tests/a.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
src: "foo.tar.gz",
|
|
|
|
},
|
|
|
|
want: []string{"--exclude", "tests/a.txt", "--dereference", "-zcf", "foo.tar.gz", "tests/a.txt", "tests/b.txt"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &Plugin{
|
|
|
|
Config: tt.fields.Config,
|
|
|
|
}
|
|
|
|
if got := p.buildTarArgs(tt.args.src); !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Plugin.buildTarArgs() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 12:54:53 +08:00
|
|
|
|
|
|
|
func TestTargetFolderWithSpaces(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 22,
|
2023-04-11 12:54:53 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
|
|
|
StripComponents: 2,
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "123 456 789")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123 456 789", "c.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "123 456 789", "d.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-16 11:29:15 +08:00
|
|
|
|
|
|
|
func TestHostPortString(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost:22", "localhost:22"},
|
|
|
|
Username: "drone-scp",
|
2024-06-01 11:40:23 +08:00
|
|
|
Protocol: easyssh.PROTOCOL_TCP4,
|
|
|
|
Port: 8080,
|
2023-04-16 11:29:15 +08:00
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/global/*"},
|
|
|
|
StripComponents: 2,
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "1234")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "1234", "c.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "1234", "d.txt")); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unit test for hostPort
|
|
|
|
func TestHostPort(t *testing.T) {
|
|
|
|
p := Plugin{
|
|
|
|
Config: Config{
|
2024-06-01 11:40:23 +08:00
|
|
|
Port: 8080,
|
|
|
|
Protocol: easyssh.PROTOCOL_TCP4,
|
2023-04-16 11:29:15 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case 1: host string with port
|
|
|
|
host1 := "example.com:1234"
|
|
|
|
expectedHost1 := "example.com"
|
|
|
|
expectedPort1 := "1234"
|
|
|
|
actualHost1, actualPort1 := p.hostPort(host1)
|
|
|
|
if actualHost1 != expectedHost1 || actualPort1 != expectedPort1 {
|
|
|
|
t.Errorf("hostPort(%s) = (%s, %s); expected (%s, %s)", host1, actualHost1, actualPort1, expectedHost1, expectedPort1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case 2: host string without port
|
|
|
|
host2 := "example.com"
|
|
|
|
expectedHost2 := "example.com"
|
|
|
|
expectedPort2 := "8080" // default port
|
|
|
|
actualHost2, actualPort2 := p.hostPort(host2)
|
|
|
|
if actualHost2 != expectedHost2 || actualPort2 != expectedPort2 {
|
|
|
|
t.Errorf("hostPort(%s) = (%s, %s); expected (%s, %s)", host2, actualHost2, actualPort2, expectedHost2, expectedPort2)
|
|
|
|
}
|
|
|
|
}
|
2024-06-01 11:40:23 +08:00
|
|
|
|
|
|
|
func TestPlugin_hostPort(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Config Config
|
|
|
|
Writer io.Writer
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
h string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantHost string
|
|
|
|
wantPort string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default host and port",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
Port: 22,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
h: "localhost",
|
|
|
|
},
|
|
|
|
wantHost: "localhost",
|
|
|
|
wantPort: "22",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "different port",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
Port: 22,
|
|
|
|
Protocol: easyssh.PROTOCOL_TCP4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
h: "localhost:443",
|
|
|
|
},
|
|
|
|
wantHost: "localhost",
|
|
|
|
wantPort: "443",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ipv6",
|
|
|
|
fields: fields{
|
|
|
|
Config: Config{
|
|
|
|
Port: 22,
|
|
|
|
Protocol: easyssh.PROTOCOL_TCP6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
h: "::1",
|
|
|
|
},
|
|
|
|
wantHost: "::1",
|
|
|
|
wantPort: "22",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := Plugin{
|
|
|
|
Config: tt.fields.Config,
|
|
|
|
}
|
|
|
|
gotHost, gotPort := p.hostPort(tt.args.h)
|
|
|
|
if gotHost != tt.wantHost {
|
|
|
|
t.Errorf("Plugin.hostPort() gotHost = %v, want %v", gotHost, tt.wantHost)
|
|
|
|
}
|
|
|
|
if gotPort != tt.wantPort {
|
|
|
|
t.Errorf("Plugin.hostPort() gotPort = %v, want %v", gotPort, tt.wantPort)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-01 12:33:11 +08:00
|
|
|
|
|
|
|
func TestIgnoreFolder(t *testing.T) {
|
|
|
|
if os.Getenv("SSH_AUTH_SOCK") != "" {
|
|
|
|
if err := exec.Command("eval", "`ssh-agent -k`").Run(); err != nil {
|
|
|
|
t.Fatalf("exec: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := user.Lookup("drone-scp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := Plugin{
|
|
|
|
Config: Config{
|
|
|
|
Host: []string{"localhost"},
|
|
|
|
Username: "drone-scp",
|
|
|
|
Protocol: easyssh.PROTOCOL_TCP4,
|
|
|
|
Port: 22,
|
|
|
|
KeyPath: "tests/.ssh/id_rsa",
|
|
|
|
Source: []string{"tests/*", "!tests/global"},
|
|
|
|
Target: []string{filepath.Join(u.HomeDir, "test_ignore")},
|
|
|
|
CommandTimeout: 60 * time.Second,
|
|
|
|
TarExec: "tar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = plugin.Exec()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// check file exist
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "test_ignore", "global", "c.txt")); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(u.HomeDir, "test_ignore", "global", "d.txt")); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("SCP-error: %v", err)
|
|
|
|
}
|
|
|
|
}
|