-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add modification_time field to filestat input plugin #3305
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2bb092
added modification_time field
puckpuck 31a9392
use UnixNano() for mod time
puckpuck fff8aff
updated readme to show nanoseconds
puckpuck 8b25cad
updated all tests
puckpuck ba6c7ab
updated test to use acc.HasPoint
puckpuck 2bf4c12
go fmt
puckpuck f6211ad
Merge branch 'master' into filestat-modtime
puckpuck 9dce627
go fmt
puckpuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf/internal/globpath" | ||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
@@ -25,17 +26,19 @@ func TestGatherNoMd5(t *testing.T) { | |
"file": dir + "log1.log", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags1["file"])), | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) | ||
|
||
tags2 := map[string]string{ | ||
"file": dir + "log2.log", | ||
} | ||
fields2 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags2["file"])), | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) | ||
|
||
|
@@ -65,19 +68,21 @@ func TestGatherExplicitFiles(t *testing.T) { | |
"file": dir + "log1.log", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags1["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) | ||
|
||
tags2 := map[string]string{ | ||
"file": dir + "log2.log", | ||
} | ||
fields2 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags2["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) | ||
|
||
|
@@ -105,19 +110,21 @@ func TestGatherGlob(t *testing.T) { | |
"file": dir + "log1.log", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags1["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) | ||
|
||
tags2 := map[string]string{ | ||
"file": dir + "log2.log", | ||
} | ||
fields2 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags2["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) | ||
} | ||
|
@@ -137,29 +144,32 @@ func TestGatherSuperAsterisk(t *testing.T) { | |
"file": dir + "log1.log", | ||
} | ||
fields1 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags1["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields1, tags1) | ||
|
||
tags2 := map[string]string{ | ||
"file": dir + "log2.log", | ||
} | ||
fields2 := map[string]interface{}{ | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
"size_bytes": int64(0), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags2["file"])), | ||
"md5_sum": "d41d8cd98f00b204e9800998ecf8427e", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields2, tags2) | ||
|
||
tags3 := map[string]string{ | ||
"file": dir + "test.conf", | ||
} | ||
fields3 := map[string]interface{}{ | ||
"size_bytes": int64(104), | ||
"exists": int64(1), | ||
"md5_sum": "5a7e9b77fa25e7bb411dbd17cf403c1f", | ||
"size_bytes": int64(104), | ||
"exists": int64(1), | ||
"modification_time": int64(getModificationTime(fs, tags3["file"])), | ||
"md5_sum": "5a7e9b77fa25e7bb411dbd17cf403c1f", | ||
} | ||
acc.AssertContainsTaggedFields(t, "filestat", fields3, tags3) | ||
} | ||
|
@@ -174,6 +184,36 @@ func TestGetMd5(t *testing.T) { | |
assert.Error(t, err) | ||
} | ||
|
||
func getModificationTime(f *FileStat, filepath string) int64 { | ||
// This function is a near copy from the main code, | ||
// however since the modification time of test files will change based on code pull this seems the only way to solve unit tests | ||
// All unit tests should call this function for expected value of modification_time | ||
|
||
var err error | ||
g, ok := f.globs[filepath] | ||
if !ok { | ||
if g, err = globpath.Compile(filepath); err != nil { | ||
return 0 | ||
} | ||
f.globs[filepath] = g | ||
} | ||
|
||
files := g.Match() | ||
if len(files) == 0 { | ||
return 0 | ||
} | ||
|
||
for _, fileInfo := range files { | ||
if fileInfo == nil { | ||
return 0 | ||
} else { | ||
return fileInfo.ModTime().Unix() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about providing precision greater than 1s? |
||
} | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func getTestdataDir() string { | ||
_, filename, _, _ := runtime.Caller(1) | ||
return strings.Replace(filename, "filestat_test.go", "testdata/", 1) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use
acc.HasInt64Field(
to assert that the field is set, then you can remove the getModificationTime function. This will be good enough for now in my opinion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping you wouldn't ask for something like that 😉
means I need to modify each test case to assert all fields individually, because
acc.AssertContainsTaggedFields(...)
will fail now. Hence why I did the getModificationTime function... required fewer changes to implement.