// TestStatusLocalLogs checks to ensure that local/logfiles, // local/logfiles/{filename}, local/log and local/log/{level} function // correctly. func TestStatusLocalLogs(t *testing.T) { defer leaktest.AfterTest(t) dir, err := ioutil.TempDir("", "local_log_test") if err != nil { t.Fatal(err) } log.EnableLogFileOutput(dir) defer func() { log.DisableLogFileOutput() if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } }() ts := startServer(t) defer ts.Stop() // Log an error which we expect to show up on every log file. timestamp := time.Now().UnixNano() log.Errorf("TestStatusLocalLogFile test message-Error") timestampE := time.Now().UnixNano() log.Warningf("TestStatusLocalLogFile test message-Warning") timestampEW := time.Now().UnixNano() log.Infof("TestStatusLocalLogFile test message-Info") timestampEWI := time.Now().UnixNano() type logsWrapper struct { Data []log.FileInfo `json:"d"` } var logs logsWrapper if err := json.Unmarshal(getRequest(t, ts, "/_status/logfiles/local"), &logs); err != nil { t.Fatal(err) } if a, e := len(logs.Data), 3; a != e { t.Fatalf("expected %d log files; got %d", e, a) } for i, name := range []string{"log.ERROR", "log.INFO", "log.WARNING"} { if !strings.Contains(logs.Data[i].Name, name) { t.Errorf("expected log file name %s to contain %q", logs.Data[i].Name, name) } } // Fetch the full list of log entries. type logWrapper struct { Data []log.LogEntry `json:"d"` } // Check each individual log can be fetched and is non-empty. var foundInfo, foundWarning, foundError bool for _, file := range logs.Data { var log logWrapper if err := json.Unmarshal(getRequest(t, ts, fmt.Sprintf("/_status/logfiles/local/%s", file.Name)), &log); err != nil { t.Fatal(err) } for _, entry := range log.Data { switch entry.Format { case "TestStatusLocalLogFile test message-Error": foundError = true case "TestStatusLocalLogFile test message-Warning": foundWarning = true case "TestStatusLocalLogFile test message-Info": foundInfo = true } } } if !(foundInfo && foundWarning && foundError) { t.Errorf("expected to find test messages in %v", logs.Data) } testCases := []struct { Level log.Severity MaxEntities int StartTimestamp int64 EndTimestamp int64 Pattern string ExpectedError bool ExpectedWarning bool ExpectedInfo bool }{ // Test filtering by log severity. {log.InfoLog, 0, 0, 0, "", true, true, true}, {log.WarningLog, 0, 0, 0, "", true, true, false}, {log.ErrorLog, 0, 0, 0, "", true, false, false}, // Test entry limit. Ignore Info/Warning/Error filters. {log.InfoLog, 1, timestamp, timestampEWI, "", false, false, false}, {log.InfoLog, 2, timestamp, timestampEWI, "", false, false, false}, {log.InfoLog, 3, timestamp, timestampEWI, "", false, false, false}, // Test filtering in different timestamp windows. {log.InfoLog, 0, timestamp, timestamp, "", false, false, false}, {log.InfoLog, 0, timestamp, timestampE, "", true, false, false}, {log.InfoLog, 0, timestampE, timestampEW, "", false, true, false}, {log.InfoLog, 0, timestampEW, timestampEWI, "", false, false, true}, {log.InfoLog, 0, timestamp, timestampEW, "", true, true, false}, {log.InfoLog, 0, timestampE, timestampEWI, "", false, true, true}, {log.InfoLog, 0, timestamp, timestampEWI, "", true, true, true}, // Test filtering by regexp pattern. {log.InfoLog, 0, 0, 0, "Info", false, false, true}, {log.InfoLog, 0, 0, 0, "Warning", false, true, false}, {log.InfoLog, 0, 0, 0, "Error", true, false, false}, {log.InfoLog, 0, 0, 0, "Info|Error|Warning", true, true, true}, {log.InfoLog, 0, 0, 0, "Nothing", false, false, false}, } for i, testCase := range testCases { var url bytes.Buffer fmt.Fprintf(&url, "/_status/logs/local?level=%s", testCase.Level.Name()) if testCase.MaxEntities > 0 { fmt.Fprintf(&url, "&max=%d", testCase.MaxEntities) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "&starttime=%d", testCase.StartTimestamp) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "&endtime=%d", testCase.EndTimestamp) } if len(testCase.Pattern) > 0 { fmt.Fprintf(&url, "&pattern=%s", testCase.Pattern) } var log logWrapper path := url.String() if err := json.Unmarshal(getRequest(t, ts, path), &log); err != nil { t.Fatal(err) } if testCase.MaxEntities > 0 { if a, e := len(log.Data), testCase.MaxEntities; a != e { t.Errorf("%d expected %d entries, got %d: \n%+v", i, e, a, log.Data) } } else { var actualInfo, actualWarning, actualError bool var formats bytes.Buffer for _, entry := range log.Data { fmt.Fprintf(&formats, "%s\n", entry.Format) switch entry.Format { case "TestStatusLocalLogFile test message-Error": actualError = true case "TestStatusLocalLogFile test message-Warning": actualWarning = true case "TestStatusLocalLogFile test message-Info": actualInfo = true } } if !(testCase.ExpectedInfo == actualInfo && testCase.ExpectedWarning == actualWarning && testCase.ExpectedError == actualError) { t.Errorf( "%d: expected info, warning, error: (%t, %t, %t) from %s, got:\n%s", i, testCase.ExpectedInfo, testCase.ExpectedWarning, testCase.ExpectedError, path, formats.String(), ) } } } }
// TestStatusLocalLogs checks to ensure that local/logfiles, // local/logfiles/{filename}, local/log and local/log/{level} function // correctly. func TestStatusLocalLogs(t *testing.T) { defer leaktest.AfterTest(t) dir, err := ioutil.TempDir("", "local_log_test") if err != nil { t.Fatal(err) } log.EnableLogFileOutput(dir) defer func() { log.DisableLogFileOutput() if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } }() ts := startServer(t, statusLocalLogFileKeyPrefix) defer ts.Stop() // Log an error which we expect to show up on every log file. timestamp := time.Now().UnixNano() log.Errorf("TestStatusLocalLogFile test message-Error") timestampE := time.Now().UnixNano() log.Warningf("TestStatusLocalLogFile test message-Warning") timestampEW := time.Now().UnixNano() log.Infof("TestStatusLocalLogFile test message-Info") timestampEWI := time.Now().UnixNano() body := getRequest(t, ts, statusLocalLogFileKeyPrefix) type logsWrapper struct { Data []log.FileInfo `json:"d"` } logs := logsWrapper{} if err := json.Unmarshal(body, &logs); err != nil { t.Fatal(err) } if a, e := len(logs.Data), 3; a != e { t.Fatalf("expected %d log files; got %d", e, a) } for i, name := range []string{"log.ERROR", "log.INFO", "log.WARNING"} { if !strings.Contains(logs.Data[i].Name, name) { t.Errorf("expected log file name %s to contain %q", logs.Data[i].Name, name) } } // Fetch a each listed log directly. type logWrapper struct { Data []log.LogEntry `json:"d"` } // Check each individual log can be fetched and is non-empty. for _, log := range logs.Data { body = getRequest(t, ts, fmt.Sprintf("%s%s", statusLocalLogFileKeyPrefix, log.Name)) logW := logWrapper{} if err := json.Unmarshal(body, &logW); err != nil { t.Fatal(err) } var found bool for _, data := range logW.Data { if data.Format == "TestStatusLocalLogFile test message-Error" { found = true break } } if !found { t.Errorf("expected to find test message in %v", logW.Data) } } // Fetch the full list of log entries. type entryWrapper struct { Data []log.LogEntry `json:"d"` } testCases := []struct { Level log.Severity MaxEntities int StartTimestamp int64 EndTimestamp int64 Pattern string ExpectedError bool ExpectedWarning bool ExpectedInfo bool }{ // Test filtering by log severity. {log.InfoLog, 0, 0, 0, "", true, true, true}, {log.WarningLog, 0, 0, 0, "", true, true, false}, {log.ErrorLog, 0, 0, 0, "", true, false, false}, // Test filtering in different timestamp windows. {log.InfoLog, 1, timestamp, timestampEWI, "", true, false, false}, {log.InfoLog, 2, timestamp, timestampEWI, "", true, true, false}, {log.InfoLog, 3, timestamp, timestampEWI, "", true, true, true}, {log.InfoLog, 0, timestamp, timestamp, "", false, false, false}, {log.InfoLog, 0, timestamp, timestampE, "", true, false, false}, {log.InfoLog, 0, timestampE, timestampEW, "", false, true, false}, {log.InfoLog, 0, timestampEW, timestampEWI, "", false, false, true}, {log.InfoLog, 0, timestamp, timestampEW, "", true, true, false}, {log.InfoLog, 0, timestampE, timestampEWI, "", false, true, true}, {log.InfoLog, 0, timestamp, timestampEWI, "", true, true, true}, // Test filtering by regexp pattern. {log.InfoLog, 0, 0, 0, "Info", false, false, true}, {log.InfoLog, 0, 0, 0, "Warning", false, true, false}, {log.InfoLog, 0, 0, 0, "Error", true, false, false}, {log.InfoLog, 0, 0, 0, "Info|Error|Warning", true, true, true}, {log.InfoLog, 0, 0, 0, "Nothing", false, false, false}, } for i, testCase := range testCases { var url bytes.Buffer fmt.Fprintf(&url, "%s?level=%s&", statusLogKeyPrefix, testCase.Level.Name()) if testCase.MaxEntities > 0 { fmt.Fprintf(&url, "max=%d&", testCase.MaxEntities) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "starttime=%d&", testCase.StartTimestamp) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "endtime=%d&", testCase.EndTimestamp) } if len(testCase.Pattern) > 0 { fmt.Fprintf(&url, "pattern=%s&", testCase.Pattern) } body = getRequest(t, ts, url.String()) entities := entryWrapper{} if err := json.Unmarshal(body, &entities); err != nil { t.Fatal(err) } var actualInfo, actualWarning, actualError bool for _, entity := range entities.Data { switch entity.Format { case "TestStatusLocalLogFile test message-Error": actualError = true case "TestStatusLocalLogFile test message-Warning": actualWarning = true case "TestStatusLocalLogFile test message-Info": actualInfo = true } } if testCase.ExpectedError != actualError { t.Errorf("%d: expected error:%t, actual error:%t", i, testCase.ExpectedError, actualError) } if testCase.ExpectedWarning != actualWarning { t.Errorf("%d: expected warning:%t, actual warning:%t", i, testCase.ExpectedWarning, actualWarning) } if testCase.ExpectedInfo != actualInfo { t.Errorf("%d: expected info:%t, actual info:%t", i, testCase.ExpectedInfo, actualInfo) } } }
// TestStatusLocalLogs checks to ensure that local/logfiles, // local/logfiles/{filename}, local/log and local/log/{level} function // correctly. func TestStatusLocalLogs(t *testing.T) { defer leaktest.AfterTest(t) dir, err := ioutil.TempDir("", "local_log_test") if err != nil { t.Fatal(err) } log.EnableLogFileOutput(dir) defer func() { log.DisableLogFileOutput() if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } }() ts, body := startServerAndGetStatus(t, statusLocalLogFileKeyPrefix) defer ts.Stop() type logsWrapper struct { Data []log.FileInfo `json:"d"` } logs := logsWrapper{} if err := json.Unmarshal(body, &logs); err != nil { t.Fatal(err) } if l := len(logs.Data); l != 3 { t.Fatalf("expected 3 log files; got %d", l) } for i, pat := range []string{`.*log.ERROR.*`, `.*log.INFO.*`, `.*log.WARNING.*`} { if ok, err := regexp.MatchString(pat, logs.Data[i].Name); !ok || err != nil { t.Errorf("expected log file %s to match %q: %s", logs.Data[i].Name, pat, err) } } // Log an error which we expect to show up on every log file. timestamp := time.Now().UnixNano() log.Errorf("TestStatusLocalLogFile test message-Error") timestampE := time.Now().UnixNano() log.Warningf("TestStatusLocalLogFile test message-Warning") timestampEW := time.Now().UnixNano() log.Infof("TestStatusLocalLogFile test message-Info") timestampEWI := time.Now().UnixNano() // Fetch a each listed log directly. type logWrapper struct { Data []log.LogEntry `json:"d"` } // Check each individual log can be fetched and is non-empty. for _, log := range logs.Data { body = getRequest(t, ts, fmt.Sprintf("%s%s", statusLocalLogFileKeyPrefix, log.Name)) logW := logWrapper{} if err := json.Unmarshal(body, &logW); err != nil { t.Fatal(err) } var found bool for i := len(logW.Data) - 1; i >= 0; i-- { if logW.Data[i].Format == "TestStatusLocalLogFile test message-Error" { found = true break } } if !found { t.Errorf("expected to find test message in %v", logW.Data) } } // Fetch the full list of log entries. type entryWrapper struct { Data []log.LogEntry `json:"d"` } testCases := []struct { Level log.Severity MaxEntities int StartTimestamp int64 EndTimestamp int64 ExpectedError bool ExpectedWarning bool ExpectedInfo bool }{ {log.InfoLog, 0, 0, 0, true, true, true}, {log.WarningLog, 0, 0, 0, true, true, false}, {log.ErrorLog, 0, 0, 0, true, false, false}, {log.InfoLog, 1, timestamp, timestampEWI, true, false, false}, {log.InfoLog, 2, timestamp, timestampEWI, true, true, false}, {log.InfoLog, 3, timestamp, timestampEWI, true, true, true}, {log.InfoLog, 0, timestamp, timestamp, false, false, false}, {log.InfoLog, 0, timestamp, timestampE, true, false, false}, {log.InfoLog, 0, timestampE, timestampEW, false, true, false}, {log.InfoLog, 0, timestampEW, timestampEWI, false, false, true}, {log.InfoLog, 0, timestamp, timestampEW, true, true, false}, {log.InfoLog, 0, timestampE, timestampEWI, false, true, true}, {log.InfoLog, 0, timestamp, timestampEWI, true, true, true}, } for i, testCase := range testCases { var url bytes.Buffer fmt.Fprintf(&url, "%s%s?", statusLocalLogKeyPrefix, testCase.Level.Name()) if testCase.MaxEntities > 0 { fmt.Fprintf(&url, "max=%d&", testCase.MaxEntities) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "starttime=%d&", testCase.StartTimestamp) } if testCase.StartTimestamp > 0 { fmt.Fprintf(&url, "endtime=%d&", testCase.EndTimestamp) } body = getRequest(t, ts, url.String()) entities := entryWrapper{} if err := json.Unmarshal(body, &entities); err != nil { t.Fatal(err) } var actualInfo, actualWarning, actualError bool for _, entity := range entities.Data { switch entity.Format { case "TestStatusLocalLogFile test message-Error": actualError = true case "TestStatusLocalLogFile test message-Warning": actualWarning = true case "TestStatusLocalLogFile test message-Info": actualInfo = true } } if testCase.ExpectedError != actualError { t.Errorf("%d: expected error:%t, actual error:%t", i, testCase.ExpectedError, actualError) } if testCase.ExpectedWarning != actualWarning { t.Errorf("%d: expected warning:%t, actual warning:%t", i, testCase.ExpectedWarning, actualWarning) } if testCase.ExpectedInfo != actualInfo { t.Errorf("%d: expected info:%t, actual info:%t", i, testCase.ExpectedInfo, actualInfo) } } }