From 0914cd212b561b17a914d0b49afb93f6ac3049c6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 14 Jul 2024 15:24:36 +0800 Subject: [PATCH] test: enable comprehensive debugging in plugin execution (#278) * 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 * test: refactor codebase to improve performance and readability - Remove debug command block from `TestSudoCommand` test Signed-off-by: Bo-Yi Wu * 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 * 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 --------- Signed-off-by: Bo-Yi Wu --- plugin.go | 10 ++++++---- plugin_test.go | 31 +------------------------------ 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/plugin.go b/plugin.go index d99639d..3841103 100644 --- a/plugin.go +++ b/plugin.go @@ -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 { @@ -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======") diff --git a/plugin_test.go b/plugin_test.go index 8674bca..e5d63b8 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -392,6 +392,7 @@ func TestCommandOutput(t *testing.T) { }, CommandTimeout: 60 * time.Second, Sync: true, + Debug: true, }, Writer: &buffer, } @@ -443,9 +444,6 @@ func TestFingerprint(t *testing.T) { var ( buffer bytes.Buffer expected = ` - ======CMD====== - whoami - ======END====== out: drone-scp ` ) @@ -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 ` ) @@ -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 ` ) @@ -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 ` @@ -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 ` @@ -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] @@ -938,9 +915,6 @@ func TestSudoCommand(t *testing.T) { var ( buffer bytes.Buffer expected = ` - ======CMD====== - sudo su - -c "whoami" - ======END====== out: root ` ) @@ -968,9 +942,6 @@ func TestCommandWithIPv6(t *testing.T) { var ( buffer bytes.Buffer expected = ` - ======CMD====== - whoami - ======END====== out: drone-scp ` )