From 658cda621fef91cff547cc06664c49b1a185865b Mon Sep 17 00:00:00 2001 From: ning xie Date: Thu, 17 Nov 2016 22:25:04 +0800 Subject: [PATCH] fix filechecker in health with precondition check Signed-off-by: andy xie --- health/checks/checks.go | 17 ++++++++++++++--- health/doc.go | 5 +++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/health/checks/checks.go b/health/checks/checks.go index e3c3b08d..7760f610 100644 --- a/health/checks/checks.go +++ b/health/checks/checks.go @@ -2,9 +2,11 @@ package checks import ( "errors" + "fmt" "net" "net/http" "os" + "path/filepath" "strconv" "time" @@ -15,10 +17,19 @@ import ( // if the file exists. func FileChecker(f string) health.Checker { return health.CheckFunc(func() error { - if _, err := os.Stat(f); err == nil { - return errors.New("file exists") + absoluteFilePath, err := filepath.Abs(f) + if err != nil { + return fmt.Errorf("failed to get absolute path for %q: %v", f, err) } - return nil + + _, err = os.Stat(absoluteFilePath) + if err == nil { + return errors.New("file exists") + } else if os.IsNotExist(err) { + return nil + } + + return err }) } diff --git a/health/doc.go b/health/doc.go index 8c106b42..2ea5f16d 100644 --- a/health/doc.go +++ b/health/doc.go @@ -122,6 +122,11 @@ // # curl localhost:5001/debug/health // {"fileChecker":"file exists"} // +// FileChecker only accepts absolute or relative file path. It does not work properly with tilde(~). +// You should make sure that the application has proper permission(read and execute permission +// for directory along with the specified file path). Otherwise, the FileChecker will report error +// and file health check is not ok. +// // You could also test the connectivity to a downstream service by using a // "HTTPChecker", but ensure that you only mark the test unhealthy if there // are a minimum of two failures in a row: