-
Notifications
You must be signed in to change notification settings - Fork 287
fix: pcap file name formatting #1749
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
Merged
kamilprz
merged 4 commits into
microsoft:main
from
kamilprz:kamilp/capture-filename-format
Jul 28, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package file | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| func TestCaptureFilenameFormat(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| captureName string | ||
| nodeHostname string | ||
| timestamp *v1.Time | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "valid filename", | ||
| captureName: "capture-name", | ||
| nodeHostname: "node1", | ||
| timestamp: &v1.Time{Time: time.Date(2025, 1, 1, 12, 30, 0, 0, time.UTC)}, | ||
| expected: "capture-name-node1-20250101123000UTC", | ||
| }, | ||
| { | ||
| name: "different timezone", | ||
| captureName: "capture-name", | ||
| nodeHostname: "node1", | ||
| timestamp: &v1.Time{Time: time.Date(2025, 1, 1, 8, 30, 0, 0, time.FixedZone("EDT", -4*60*60))}, // 8:30 EDT is 12:30 UTC | ||
| expected: "capture-name-node1-20250101123000UTC", | ||
| }, | ||
| { | ||
| name: "empty capture name", | ||
| captureName: "", | ||
| nodeHostname: "node1", | ||
| timestamp: &v1.Time{Time: time.Date(2025, 1, 1, 12, 30, 0, 0, time.UTC)}, | ||
| expected: "-node1-20250101123000UTC", | ||
| }, | ||
| { | ||
| name: "empty node name", | ||
| captureName: "capture-name", | ||
| nodeHostname: "", | ||
| timestamp: &v1.Time{Time: time.Date(2025, 1, 1, 12, 30, 0, 0, time.UTC)}, | ||
| expected: "capture-name--20250101123000UTC", | ||
| }, | ||
| { | ||
| name: "zero time", | ||
| captureName: "capture-name", | ||
| nodeHostname: "node1", | ||
| timestamp: &v1.Time{Time: time.Time{}}, | ||
| expected: "capture-name-node1-00010101000000UTC", | ||
| }, | ||
| { | ||
| name: "nil timestamp", | ||
| captureName: "capture-name", | ||
| nodeHostname: "node1", | ||
| timestamp: nil, | ||
| expected: "capture-name-node1-00010101000000UTC", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| filename := CaptureFilename{ | ||
| CaptureName: tt.captureName, | ||
| NodeHostname: tt.nodeHostname, | ||
| StartTimestamp: tt.timestamp, | ||
| } | ||
|
|
||
| // .String() here relies on TimeToString(), which should handle nil timestamps gracefully | ||
| // and return a zero time string - this should not panic | ||
| var result string | ||
| assert.NotPanics(t, func() { | ||
| result = filename.String() | ||
| }, "CaptureFilename.String() should handle nil timestamp gracefully") | ||
|
|
||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package file | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| func TestNow(t *testing.T) { | ||
| before := time.Now().UTC().Truncate(time.Second) | ||
| result := Now() | ||
| after := time.Now().UTC().Truncate(time.Second) | ||
|
|
||
| require.NotNil(t, result) | ||
| assert.GreaterOrEqual(t, result.Time, before) | ||
| assert.LessOrEqual(t, result.Time, after) | ||
| assert.Equal(t, 0, result.Time.Nanosecond()) // ensure timestamp is truncated | ||
| } | ||
|
|
||
| func TestStringToTime(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected *metav1.Time | ||
| wantError bool | ||
| }{ | ||
| { | ||
| name: "valid timestamp", | ||
| input: "20250101120000UTC", | ||
| expected: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC)}, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "another valid timestamp", | ||
| input: "20251231235959UTC", | ||
| expected: &metav1.Time{Time: time.Date(2025, 12, 31, 23, 59, 59, 0, time.UTC)}, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "midnight timestamp", | ||
| input: "20250101000000UTC", | ||
| expected: &metav1.Time{Time: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}, | ||
| wantError: false, | ||
| }, | ||
| { | ||
| name: "invalid format", | ||
| input: "2025-01-01 12:00:00", | ||
| expected: nil, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| input: "", | ||
| expected: nil, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "invalid month", | ||
| input: "20251301120000UTC", | ||
| expected: nil, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "invalid day", | ||
| input: "20250132120000UTC", | ||
| expected: nil, | ||
| wantError: true, | ||
| }, | ||
| { | ||
| name: "invalid hour", | ||
| input: "20250101250000UTC", | ||
| expected: nil, | ||
| wantError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result, err := StringToTime(tt.input) | ||
| if tt.wantError { | ||
| require.Error(t, err) | ||
| assert.Nil(t, result) | ||
| assert.Equal(t, tt.expected, result) | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, result) | ||
| assert.Equal(t, tt.expected, result) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeToString(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input *metav1.Time | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "valid time", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC)}, | ||
| expected: "20250101120000UTC", | ||
| }, | ||
| { | ||
| name: "invalid month", | ||
| input: &metav1.Time{Time: time.Date(2025, 13, 1, 12, 0, 0, 0, time.UTC)}, | ||
| expected: "20260101120000UTC", | ||
| }, | ||
| { | ||
| name: "invalid day", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 32, 12, 0, 0, 0, time.UTC)}, | ||
| expected: "20250201120000UTC", | ||
| }, | ||
| { | ||
| name: "invalid hour", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 1, 25, 0, 0, 0, time.UTC)}, | ||
| expected: "20250102010000UTC", | ||
| }, | ||
| { | ||
| name: "invalid minutes", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 61, 0, 0, time.UTC)}, | ||
| expected: "20250101130100UTC", | ||
| }, | ||
| { | ||
| name: "invalid seconds", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 1, 12, 0, 61, 0, time.UTC)}, | ||
| expected: "20250101120101UTC", | ||
| }, | ||
| { | ||
| name: "midnight", | ||
| input: &metav1.Time{Time: time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)}, | ||
| expected: "20251201000000UTC", | ||
| }, | ||
| { | ||
| name: "end of day", | ||
| input: &metav1.Time{Time: time.Date(2025, 1, 1, 23, 59, 59, 0, time.UTC)}, | ||
| expected: "20250101235959UTC", | ||
| }, | ||
| { | ||
| name: "nil time pointer", | ||
| input: nil, // Should return a zero time string | ||
| expected: "00010101000000UTC", | ||
| }, | ||
| { | ||
| name: "zero time", | ||
| input: &metav1.Time{Time: time.Time{}}, // Same as nil case | ||
| expected: "00010101000000UTC", | ||
| }, | ||
| { | ||
| name: "time with different timezone", | ||
| input: &metav1.Time{Time: time.Date(2025, 6, 15, 14, 30, 45, 0, time.FixedZone("EST", -5*60*60))}, | ||
| expected: "20250615193045UTC", // Should be converted to UTC | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := TimeToString(tt.input) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeToStringAndBack(t *testing.T) { | ||
| originalTime := &metav1.Time{Time: time.Date(2025, 1, 1, 12, 30, 0, 0, time.UTC)} | ||
|
|
||
| timeString := TimeToString(originalTime) | ||
| parsedTime, err := StringToTime(timeString) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.True(t, originalTime.Time.Equal(parsedTime.Time)) | ||
| } | ||
|
|
||
| func TestStringToTimeAndBack(t *testing.T) { | ||
| originalString := "20250101123000UTC" | ||
|
|
||
| parsedTime, err := StringToTime(originalString) | ||
| require.NoError(t, err) | ||
|
|
||
| timeString := TimeToString(parsedTime) | ||
| assert.Equal(t, originalString, timeString) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.