-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft_test.go
78 lines (68 loc) · 1.39 KB
/
nft_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// +build linux
package nftlib
import (
"fmt"
"github.com/stretchr/testify/suite"
"testing"
)
type NftTest struct {
suite.Suite
table *Table
chain *Chain
rule *Rule
set *Set
}
func (d *NftTest) SetupSuite() {
fmt.Println("测试开始")
}
func (d *NftTest) TearDownSuite() {
fmt.Println("测试结束")
}
func (d *NftTest) Test01_AddTable() {
conn, err := New()
if err != nil {
panic(err)
}
table := conn.ADDTable(&Table{Name: "mytable", Family: TableFamilyInet})
d.table = table
fmt.Println("创建mytable表成功")
}
func (d *NftTest) Test02_AddChain() {
ch := &Chain{
Name: "mychain",
Hook: ChainHookInput,
Type: ChainTypeFilter,
Policy: ChainPolicyAccept,
}
err := d.table.AddBaseChain(ch)
if err != nil {
panic(err)
}
d.chain = ch
fmt.Println("创建mychain链成功")
}
func (d *NftTest) Test03_AddSetIpv4() {
set, err := d.table.AddSet("set1", SetDtypeIpv4, true, "192.168.1.5", "10.0.0.100-10.0.0.200", "172.16.0.0/16")
if err != nil {
panic(err)
}
fmt.Println(IndentJson(set))
d.set = set
fmt.Println("创建ipv4类型集合set1成功")
}
func (d *NftTest) Test04_AddRuleWithSet() {
rule := d.chain.NewRule()
rule.SetL3IpSet(d.set.Name, RuleDireSrc)
rule.SetAccept()
err := d.chain.AddRule(rule)
if err != nil {
panic(err)
}
err = d.chain.Commit()
if err != nil {
panic(err)
}
}
func TestNft(t *testing.T) {
suite.Run(t, new(NftTest))
}