Skip to content

Commit

Permalink
test: enable comprehensive debugging in plugin execution (#278)
Browse files Browse the repository at this point in the history
* test: enable comprehensive debugging in plugin execution

- Add debug logging to `exec` function in `plugin.go`
- Enable debug mode in `TestCommandOutput` and `TestSudoCommand` tests
- Remove redundant command blocks from multiple tests in `plugin_test.go`

Signed-off-by: Bo-Yi Wu <[email protected]>

* test: refactor codebase to improve performance and readability

- Remove debug command block from `TestSudoCommand` test

Signed-off-by: Bo-Yi Wu <[email protected]>

* refactor: refactor test environment setup and command markers

- Add environment markers to `TestCommandOutput` for localhost and 127.0.0.1
- Remove command markers from `TestFingerprint`

Signed-off-by: Bo-Yi Wu <[email protected]>

* fix: improve environment variable handling in debug mode

- Add a check for non-empty environment variables when debug mode is enabled
- Remove redundant environment variable output in tests

Signed-off-by: Bo-Yi Wu <[email protected]>

---------

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy authored Jul 14, 2024
1 parent e28acf4 commit 0914cd2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
10 changes: 6 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
},
}

p.log(host, "======CMD======")
p.log(host, strings.Join(p.Config.Script, "\n"))
p.log(host, "======END======")
if p.Config.Debug {
p.log(host, "======CMD======")
p.log(host, strings.Join(p.Config.Script, "\n"))
p.log(host, "======END======")
}

env := []string{}
if p.Config.AllEnvs {
Expand All @@ -123,7 +125,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {

p.Config.Script = append(env, p.scriptCommands()...)

if p.Config.Debug {
if p.Config.Debug && len(env) > 0 {
p.log(host, "======ENV======")
p.log(host, strings.Join(env, "\n"))
p.log(host, "======END======")
Expand Down
31 changes: 1 addition & 30 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func TestCommandOutput(t *testing.T) {
},
CommandTimeout: 60 * time.Second,
Sync: true,
Debug: true,
},
Writer: &buffer,
}
Expand Down Expand Up @@ -443,9 +444,6 @@ func TestFingerprint(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
whoami
======END======
out: drone-scp
`
)
Expand Down Expand Up @@ -477,10 +475,6 @@ func TestScriptStopWithMultipleHostAndSyncMode(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
mkdir a/b/c
mkdir d/e/f
======END======
err: mkdir: can't create directory 'a/b/c': No such file or directory
`
)
Expand Down Expand Up @@ -512,10 +506,6 @@ func TestScriptStop(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
mkdir a/b/c
mkdir d/e/f
======END======
err: mkdir: can't create directory 'a/b/c': No such file or directory
`
)
Expand Down Expand Up @@ -546,10 +536,6 @@ func TestNoneScriptStop(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
mkdir a/b/c
mkdir d/e/f
======END======
err: mkdir: can't create directory 'a/b/c': No such file or directory
err: mkdir: can't create directory 'd/e/f': No such file or directory
`
Expand Down Expand Up @@ -733,10 +719,6 @@ func TestUseInsecureCipher(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
mkdir a/b/c
mkdir d/e/f
======END======
err: mkdir: can't create directory 'a/b/c': No such file or directory
err: mkdir: can't create directory 'd/e/f': No such file or directory
`
Expand Down Expand Up @@ -889,11 +871,6 @@ func TestAllEnvs(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
echo "[${INPUT_1}]"
echo "[${GITHUB_2}]"
echo "[${PLUGIN_3}]"
======END======
out: [foobar]
out: [foobar]
out: [foobar]
Expand Down Expand Up @@ -938,9 +915,6 @@ func TestSudoCommand(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
sudo su - -c "whoami"
======END======
out: root
`
)
Expand Down Expand Up @@ -968,9 +942,6 @@ func TestCommandWithIPv6(t *testing.T) {
var (
buffer bytes.Buffer
expected = `
======CMD======
whoami
======END======
out: drone-scp
`
)
Expand Down

0 comments on commit 0914cd2

Please sign in to comment.