mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-24 06:12:28 +00:00
Fix race in TestWebhook
Previous commit misused a bytes.Buffer. Protect the buffer with a mutex.
This commit is contained in:
parent
0feeb945fc
commit
337621998e
1 changed files with 35 additions and 4 deletions
|
@ -13,6 +13,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
@ -66,18 +67,18 @@ func TestWebhook(t *testing.T) {
|
||||||
hookecho, cleanupHookecho := buildHookecho(t)
|
hookecho, cleanupHookecho := buildHookecho(t)
|
||||||
defer cleanupHookecho()
|
defer cleanupHookecho()
|
||||||
|
|
||||||
|
webhook, cleanupWebhookFn := buildWebhook(t)
|
||||||
|
defer cleanupWebhookFn()
|
||||||
|
|
||||||
for _, hookTmpl := range []string{"test/hooks.json.tmpl", "test/hooks.yaml.tmpl"} {
|
for _, hookTmpl := range []string{"test/hooks.json.tmpl", "test/hooks.yaml.tmpl"} {
|
||||||
configPath, cleanupConfigFn := genConfig(t, hookecho, hookTmpl)
|
configPath, cleanupConfigFn := genConfig(t, hookecho, hookTmpl)
|
||||||
defer cleanupConfigFn()
|
defer cleanupConfigFn()
|
||||||
|
|
||||||
webhook, cleanupWebhookFn := buildWebhook(t)
|
|
||||||
defer cleanupWebhookFn()
|
|
||||||
|
|
||||||
ip, port := serverAddress(t)
|
ip, port := serverAddress(t)
|
||||||
args := []string{fmt.Sprintf("-hooks=%s", configPath), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
|
args := []string{fmt.Sprintf("-hooks=%s", configPath), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
|
||||||
|
|
||||||
// Setup a buffer for capturing webhook logs for later evaluation
|
// Setup a buffer for capturing webhook logs for later evaluation
|
||||||
b := &bytes.Buffer{}
|
b := &buffer{}
|
||||||
|
|
||||||
cmd := exec.Command(webhook, args...)
|
cmd := exec.Command(webhook, args...)
|
||||||
cmd.Stderr = b
|
cmd.Stderr = b
|
||||||
|
@ -610,3 +611,33 @@ env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00
|
||||||
{"static params should pass", "static-params-ok", nil, `{}`, false, http.StatusOK, "arg: passed\n", `(?s)command output: arg: passed`},
|
{"static params should pass", "static-params-ok", nil, `{}`, false, http.StatusOK, "arg: passed\n", `(?s)command output: arg: passed`},
|
||||||
{"command with space logs warning", "warn-on-space", nil, `{}`, false, http.StatusInternalServerError, "Error occurred while executing the hook's command. Please check your logs for more details.", `(?s)unable to locate command.*use 'pass[-]arguments[-]to[-]command' to specify args`},
|
{"command with space logs warning", "warn-on-space", nil, `{}`, false, http.StatusInternalServerError, "Error occurred while executing the hook's command. Please check your logs for more details.", `(?s)unable to locate command.*use 'pass[-]arguments[-]to[-]command' to specify args`},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// buffer provides a concurrency-safe bytes.Buffer to tests above.
|
||||||
|
type buffer struct {
|
||||||
|
b bytes.Buffer
|
||||||
|
m sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Read(p []byte) (n int, err error) {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
return b.b.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Write(p []byte) (n int, err error) {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
return b.b.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) String() string {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
return b.b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Reset() {
|
||||||
|
b.m.Lock()
|
||||||
|
defer b.m.Unlock()
|
||||||
|
b.b.Reset()
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue