mirror of
https://github.com/minio/minio.git
synced 2024-12-24 06:05:55 -05:00
Webhook endpoints can fail, we must start the server. (#4255)
This PR fixes a regression introduced in #4060
This commit is contained in:
parent
a02575ebf9
commit
df027a8f51
@ -69,10 +69,15 @@ func isNetErrorIgnored(err error) bool {
|
||||
}
|
||||
switch err.(type) {
|
||||
case net.Error:
|
||||
switch err.(type) {
|
||||
switch e := err.(type) {
|
||||
case *net.DNSError, *net.OpError, net.UnknownNetworkError:
|
||||
return true
|
||||
case *url.Error:
|
||||
// Fixes https://github.com/minio/minio/issues/4050
|
||||
switch e.Err.(type) {
|
||||
case *net.DNSError, *net.OpError, net.UnknownNetworkError:
|
||||
return true
|
||||
}
|
||||
// For a URL error, where it replies back "connection closed"
|
||||
// retry again.
|
||||
if strings.Contains(err.Error(), "Connection closed by foreign host") {
|
||||
@ -127,6 +132,7 @@ func lookupEndpoint(urlStr string) error {
|
||||
resp, derr := client.Do(req)
|
||||
if derr != nil {
|
||||
if isNetErrorIgnored(derr) {
|
||||
errorIf(derr, "Unable to lookup webhook endpoint %s", urlStr)
|
||||
return nil
|
||||
}
|
||||
return derr
|
||||
|
@ -17,6 +17,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -38,6 +39,12 @@ func (p postHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, r.Body)
|
||||
}
|
||||
|
||||
type errorHandler struct{}
|
||||
|
||||
func (e errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, fmt.Sprintf("Unexpected method %s", r.Method), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
// Tests web hook initialization.
|
||||
func TestNewWebHookNotify(t *testing.T) {
|
||||
root, err := newTestConfig(globalMinioDefaultRegion)
|
||||
@ -53,8 +60,8 @@ func TestNewWebHookNotify(t *testing.T) {
|
||||
|
||||
serverConfig.Notify.SetWebhookByID("10", webhookNotify{Enable: true, Endpoint: "http://127.0.0.1:xxx"})
|
||||
_, err = newWebhookNotify("10")
|
||||
if err == nil {
|
||||
t.Fatal("Unexpected should fail with lookupHost")
|
||||
if err != nil {
|
||||
t.Fatal("Unexpected should not fail with lookupEndpoint", err)
|
||||
}
|
||||
|
||||
serverConfig.Notify.SetWebhookByID("15", webhookNotify{Enable: true, Endpoint: "http://%"})
|
||||
@ -77,3 +84,35 @@ func TestNewWebHookNotify(t *testing.T) {
|
||||
"EventType": "s3:ObjectCreated:Put",
|
||||
}).Info()
|
||||
}
|
||||
|
||||
// Add tests for lookup endpoint.
|
||||
func TestLookupEndpoint(t *testing.T) {
|
||||
server := httptest.NewServer(errorHandler{})
|
||||
defer server.Close()
|
||||
|
||||
testCases := []struct {
|
||||
endpoint string
|
||||
err error
|
||||
}{
|
||||
// Ignore endpoints which don't exist.
|
||||
{
|
||||
endpoint: "http://unknown",
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
endpoint: "%%%",
|
||||
err: errors.New("parse %%%: invalid URL escape \"%%%\""),
|
||||
},
|
||||
{
|
||||
endpoint: server.URL,
|
||||
err: fmt.Errorf("Unexpected response from webhook server %s: (400 Bad Request)", server.URL),
|
||||
},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
if err := lookupEndpoint(test.endpoint); err != nil {
|
||||
if err.Error() != test.err.Error() {
|
||||
t.Errorf("Expected %s, got %s", test.err, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user