Skip to content

Commit 7921e81

Browse files
test: update tests
1 parent 1e35718 commit 7921e81

File tree

3 files changed

+12
-51
lines changed

3 files changed

+12
-51
lines changed

bpf-prog/azure-block-iptables/cmd/azure-block-iptables/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111

1212
"github.com/Azure/azure-container-networking/bpf-prog/azure-block-iptables/pkg/bpfprogram"
13-
"github.com/cilium/ebpf/rlimit"
1413
)
1514

1615
// ProgramVersion is set during build
@@ -60,11 +59,6 @@ func parseArgs() (*Config, error) {
6059
func attachMode(config *Config) error {
6160
log.Println("Starting attach mode...")
6261

63-
// Remove memory limit for eBPF
64-
if err := rlimit.RemoveMemlock(); err != nil {
65-
return fmt.Errorf("failed to remove memlock rlimit: %w", err)
66-
}
67-
6862
// Initialize BPF program attacher using the factory
6963
bp := config.AttacherFactory()
7064

bpf-prog/azure-block-iptables/cmd/azure-block-iptables/main_test.go

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,31 @@
44
package main
55

66
import (
7-
"os"
87
"testing"
98

109
"github.com/Azure/azure-container-networking/bpf-prog/azure-block-iptables/pkg/bpfprogram"
11-
"github.com/fsnotify/fsnotify"
12-
"github.com/pkg/errors"
1310
)
1411

1512
func TestHandleFileEventWithMock(t *testing.T) {
1613
// Create a mock Attacher
1714
mockAttacher := bpfprogram.NewMockProgram()
1815

19-
// Create a temporary config file for testing
20-
configFile := "/tmp/test-iptables-allow-list"
21-
2216
// Test cases
2317
testCases := []struct {
2418
name string
25-
setupFile func(string) error
19+
mode string
2620
expectedAttach int
2721
expectedDetach int
2822
}{
2923
{
30-
name: "empty file triggers attach",
31-
setupFile: func(path string) error {
32-
// Create empty file
33-
file, err := os.Create(path)
34-
if err != nil {
35-
return errors.Wrap(err, "failed to create file")
36-
}
37-
return file.Close()
38-
},
24+
name: "test attach mode",
25+
mode: "attach",
3926
expectedAttach: 1,
4027
expectedDetach: 0,
4128
},
4229
{
43-
name: "file with content triggers detach",
44-
setupFile: func(path string) error {
45-
// Create file with content
46-
return os.WriteFile(path, []byte("some content"), 0o600)
47-
},
48-
expectedAttach: 0,
49-
expectedDetach: 1,
50-
},
51-
{
52-
name: "missing file triggers detach",
53-
setupFile: func(path string) error {
54-
// Remove file if it exists
55-
os.Remove(path)
56-
return nil
57-
},
30+
name: "test detach mode",
31+
mode: "detach",
5832
expectedAttach: 0,
5933
expectedDetach: 1,
6034
},
@@ -65,20 +39,7 @@ func TestHandleFileEventWithMock(t *testing.T) {
6539
// Reset mock state
6640
mockAttacher.Reset()
6741

68-
// Setup file state
69-
if err := tc.setupFile(configFile); err != nil {
70-
t.Fatalf("Failed to setup file: %v", err)
71-
}
72-
defer os.Remove(configFile)
73-
74-
// Create a fake fsnotify event
75-
event := fsnotify.Event{
76-
Name: configFile,
77-
Op: fsnotify.Write,
78-
}
79-
80-
// Call the function under test
81-
handleFileEvent(event, configFile, mockAttacher)
42+
run(&Config{Mode: tc.mode, AttacherFactory: func() bpfprogram.Attacher { return mockAttacher }})
8243

8344
// Verify expectations
8445
if mockAttacher.AttachCallCount() != tc.expectedAttach {

bpf-prog/azure-block-iptables/pkg/bpfprogram/program.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
blockservice "github.com/Azure/azure-container-networking/bpf-prog/azure-block-iptables/pkg/blockservice"
1313
"github.com/cilium/ebpf"
1414
"github.com/cilium/ebpf/link"
15+
"github.com/cilium/ebpf/rlimit"
1516
"github.com/pkg/errors"
1617
)
1718

@@ -124,6 +125,11 @@ func (p *Program) Attach() error {
124125
return nil
125126
}
126127

128+
// Remove memory limit for eBPF
129+
if err := rlimit.RemoveMemlock(); err != nil {
130+
return errors.Wrapf(err, "failed to remove memlock rlimit")
131+
}
132+
127133
log.Println("Attaching BPF program...")
128134

129135
// Get the host network namespace inode

0 commit comments

Comments
 (0)