From 5f57feab54f8b5129135272cc95a26c61de073db Mon Sep 17 00:00:00 2001 From: Kishan Mohanbhai Sagathiya Date: Sat, 31 Aug 2019 11:15:16 +0530 Subject: [PATCH] Addressed reviews on tests --- api/rest/restapi_test.go | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/api/rest/restapi_test.go b/api/rest/restapi_test.go index b7360307..e72f7d79 100644 --- a/api/rest/restapi_test.go +++ b/api/rest/restapi_test.go @@ -13,6 +13,7 @@ import ( "net/http" "net/http/httputil" "os" + "path/filepath" "strings" "testing" "time" @@ -1045,31 +1046,57 @@ func TestAPILogging(t *testing.T) { cfg := &Config{} cfg.Default() - cfg.HTTPLogFile = "/tmp/http.log" - - rest := testAPIwithConfig(t, cfg, "log_enabled") - defer rest.Shutdown(ctx) - defer os.Remove(cfg.HTTPLogFile) - - data, err := ioutil.ReadFile(cfg.HTTPLogFile) + baseDir, err := filepath.Abs("") if err != nil { t.Fatal(err) } + cfg.BaseDir = baseDir + cfg.HTTPLogFile = "http.log" - if len(data) > 0 { - t.Errorf("expected empty log file, %+v", data) + rest := testAPIwithConfig(t, cfg, "log_enabled") + defer os.Remove(cfg.HTTPLogFile) + + info, err := os.Stat(cfg.HTTPLogFile) + if err != nil { + t.Fatal(err) + } + if info.Size() > 0 { + t.Errorf("expected empty log file") } id := api.ID{} makeGet(t, rest, httpURL(rest)+"/id", &id) - data, err = ioutil.ReadFile(cfg.HTTPLogFile) + info, err = os.Stat(cfg.HTTPLogFile) if err != nil { t.Fatal(err) } - if len(data) == 0 { + size1 := info.Size() + if size1 == 0 { t.Error("did not expect an empty log file") } + + // Restart API and make sure that logs are being appended + rest.Shutdown(ctx) + + rest = testAPIwithConfig(t, cfg, "log_enabled") + defer rest.Shutdown(ctx) + + makeGet(t, rest, httpURL(rest)+"/id", &id) + + info, err = os.Stat(cfg.HTTPLogFile) + if err != nil { + t.Fatal(err) + } + size2 := info.Size() + if size2 == 0 { + t.Error("did not expect an empty log file") + } + + if !(size2 > size1) { + t.Error("logs were not appended") + } + } func TestNotFoundHandler(t *testing.T) {