Skip to content

Commit 75fddcd

Browse files
committed
fix(capture): Add unit test.
Signed-off-by: mereta <mereta.degutyte@hotmail.co.uk>
1 parent 0f8e75c commit 75fddcd

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

pkg/capture/provider/network_capture_win.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (ncp *NetworkCaptureProvider) CaptureNetworkPacket(ctx context.Context, fil
8484
}
8585
if stopTrace {
8686
ncp.l.Info("Stopping netsh trace session before starting a new one")
87-
_ = ncp.stopNetworkCapture()
87+
_ = ncp.stopNetworkCapture() //nolint:contextcheck // stopNetworkCapture creates its own context
8888
}
8989

9090
captureFileName := ncp.Filename.String() + ".etl"
@@ -164,7 +164,8 @@ func (ncp *NetworkCaptureProvider) CaptureNetworkPacket(ctx context.Context, fil
164164
}
165165

166166
ncp.l.Info("Stop netsh")
167-
if err := ncp.stopNetworkCapture(); err != nil {
167+
// stopNetworkCapture creates its own context; parent ctx is expired here
168+
if err := ncp.stopNetworkCapture(); err != nil { //nolint:contextcheck
168169
ncp.l.Error("Failed to stop netsh trace by 'netsh trace stop', will kill the process", zap.Error(err))
169170
_ = captureStartCmd.Process.Kill()
170171
return fmt.Errorf("netsh stop failed: Output: %s", err)

pkg/capture/provider/network_capture_win_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
package provider
77

88
import (
9+
"context"
910
"testing"
11+
"time"
12+
13+
"github.com/microsoft/retina/pkg/capture/file"
14+
"github.com/microsoft/retina/pkg/log"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1016
)
1117

1218
func TestValidateNetshFilter(t *testing.T) {
@@ -114,3 +120,37 @@ func TestValidateNetshFilter(t *testing.T) {
114120
})
115121
}
116122
}
123+
124+
// TestStopNetworkCapture_ContextIndependence verifies stopNetworkCapture creates its own context
125+
func TestStopNetworkCapture_ContextIndependence(t *testing.T) {
126+
now := metav1.Now()
127+
ncp := &NetworkCaptureProvider{
128+
NetworkCaptureProviderCommon: NetworkCaptureProviderCommon{
129+
TmpCaptureDir: t.TempDir(),
130+
l: log.Logger().Named("test-capture"),
131+
},
132+
Filename: file.CaptureFilename{
133+
CaptureName: "test-capture",
134+
NodeHostname: "test-node",
135+
StartTimestamp: &now,
136+
},
137+
}
138+
139+
// Create an expired context (simulating capture duration ending)
140+
parentCtx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
141+
time.Sleep(10 * time.Millisecond)
142+
defer cancel()
143+
144+
if parentCtx.Err() == nil {
145+
t.Fatal("Setup error: parent context should be expired")
146+
}
147+
148+
// Call StopNetworkCapture - should NOT return "context deadline exceeded"
149+
err := ncp.stopNetworkCapture()
150+
151+
if err != nil && err.Error() == "context deadline exceeded" {
152+
t.Fatal("StopNetworkCapture returned 'context deadline exceeded' - bug reintroduced")
153+
}
154+
155+
t.Logf("StopNetworkCapture uses independent context (netsh error expected: %v)", err)
156+
}

0 commit comments

Comments
 (0)