@@ -4,8 +4,10 @@ import (
44 "os"
55 "testing"
66
7+ "bytes"
78 "github.com/appleboy/easyssh-proxy"
89 "github.com/stretchr/testify/assert"
10+ "strings"
911)
1012
1113func TestMissingHostOrUser (t * testing.T ) {
@@ -23,6 +25,7 @@ func TestMissingKeyOrPassword(t *testing.T) {
2325 Host : []string {"localhost" },
2426 UserName : "ubuntu" ,
2527 },
28+ os .Stdout ,
2629 }
2730
2831 err := plugin .Exec ()
@@ -39,6 +42,7 @@ func TestSetPasswordAndKey(t *testing.T) {
3942 Password : "1234" ,
4043 Key : "1234" ,
4144 },
45+ os .Stdout ,
4246 }
4347
4448 err := plugin .Exec ()
@@ -327,9 +331,132 @@ func Test_escapeArg(t *testing.T) {
327331 }
328332 for _ , tt := range tests {
329333 t .Run (tt .name , func (t * testing.T ) {
330- if got := escapeArg (tt .args .arg ); got != tt .want {
331- t .Errorf ("escapeArg() = %v, want %v" , got , tt .want )
332- }
334+ got := escapeArg (tt .args .arg )
335+ assert .Equal (t , tt .want , got )
333336 })
334337 }
335338}
339+
340+ func TestCommandOutput (t * testing.T ) {
341+ var (
342+ buffer bytes.Buffer
343+ expected = `
344+ localhost: ======CMD======
345+ localhost: pwd
346+ whoami
347+ uname
348+ localhost: ======END======
349+ localhost: out: /home/drone-scp
350+ localhost: out: drone-scp
351+ localhost: out: Linux
352+ 127.0.0.1: ======CMD======
353+ 127.0.0.1: pwd
354+ whoami
355+ uname
356+ 127.0.0.1: ======END======
357+ 127.0.0.1: out: /home/drone-scp
358+ 127.0.0.1: out: drone-scp
359+ 127.0.0.1: out: Linux
360+ `
361+ )
362+
363+ plugin := Plugin {
364+ Config : Config {
365+ Host : []string {"localhost" , "127.0.0.1" },
366+ UserName : "drone-scp" ,
367+ Port : 22 ,
368+ KeyPath : "./tests/.ssh/id_rsa" ,
369+ Script : []string {
370+ "pwd" ,
371+ "whoami" ,
372+ "uname" ,
373+ },
374+ CommandTimeout : 60 ,
375+ Sync : true ,
376+ },
377+ Writer : & buffer ,
378+ }
379+
380+ err := plugin .Exec ()
381+ assert .Nil (t , err )
382+
383+ assert .Equal (t , unindent (expected ), unindent (buffer .String ()))
384+ }
385+
386+ func TestEnvOutput (t * testing.T ) {
387+ var (
388+ buffer bytes.Buffer
389+ expected = `
390+ ======CMD======
391+ echo "[${ENV_1}]"
392+ echo "[${ENV_2}]"
393+ echo "[${ENV_3}]"
394+ echo "[${ENV_4}]"
395+ echo "[${ENV_5}]"
396+ echo "[${ENV_6}]"
397+ echo "[${ENV_7}]"
398+ ======END======
399+ ======ENV======
400+ ENV_1='test'
401+ ENV_2='test test'
402+ ENV_3='test '
403+ ENV_4=' test test '
404+ ENV_5='test'\'''
405+ ENV_6='test"'
406+ ENV_7='test,!#;?.@$~'\''"'
407+ ======END======
408+ out: [test]
409+ out: [test test]
410+ out: [test ]
411+ out: [ test test ]
412+ out: [test']
413+ out: [test"]
414+ out: [test,!#;?.@$~'"]
415+ `
416+ )
417+
418+ os .Setenv ("ENV_1" , `test` )
419+ os .Setenv ("ENV_2" , `test test` )
420+ os .Setenv ("ENV_3" , `test ` )
421+ os .Setenv ("ENV_4" , ` test test ` )
422+ os .Setenv ("ENV_5" , `test'` )
423+ os .Setenv ("ENV_6" , `test"` )
424+ os .Setenv ("ENV_7" , `test,!#;?.@$~'"` )
425+
426+ plugin := Plugin {
427+ Config : Config {
428+ Host : []string {"localhost" },
429+ UserName : "drone-scp" ,
430+ Port : 22 ,
431+ KeyPath : "./tests/.ssh/id_rsa" ,
432+ Envs : []string {"env_1" , "env_2" , "env_3" , "env_4" , "env_5" , "env_6" , "env_7" },
433+ Debug : true ,
434+ Script : []string {
435+ `echo "[${ENV_1}]"` ,
436+ `echo "[${ENV_2}]"` ,
437+ `echo "[${ENV_3}]"` ,
438+ `echo "[${ENV_4}]"` ,
439+ `echo "[${ENV_5}]"` ,
440+ `echo "[${ENV_6}]"` ,
441+ `echo "[${ENV_7}]"` ,
442+ },
443+ CommandTimeout : 10 ,
444+ Proxy : easyssh.DefaultConfig {
445+ Server : "localhost" ,
446+ User : "drone-scp" ,
447+ Port : "22" ,
448+ KeyPath : "./tests/.ssh/id_rsa" ,
449+ },
450+ },
451+ Writer : & buffer ,
452+ }
453+
454+ err := plugin .Exec ()
455+ assert .Nil (t , err )
456+
457+ assert .Equal (t , unindent (expected ), unindent (buffer .String ()))
458+ }
459+
460+ func unindent (text string ) string {
461+ return strings .TrimSpace (strings .Replace (text , "\t " , "" , - 1 ))
462+ }
0 commit comments