Skip to content

Commit 12b6fcd

Browse files
committed
chore:unit test
1 parent d8b3653 commit 12b6fcd

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

sqle/api/controller/v1/audit_plan_test.go renamed to sqle/server/auditplan/task_wrap_test.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
package v1_test
1+
package auditplan
22

33
import (
44
"testing"
55

6-
v1 "github.com/actiontech/sqle/sqle/api/controller/v1"
76
"github.com/actiontech/sqle/sqle/model"
87
)
98

109
func TestIsSqlInBlackList(t *testing.T) {
11-
filter := v1.ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
10+
filter := ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
1211
{
1312
FilterContent: "SELECT",
14-
FilterType: "SQL",
13+
FilterType: "sql",
1514
}, {
1615
FilterContent: "table_1",
17-
FilterType: "SQL",
18-
},{
16+
FilterType: "sql",
17+
}, {
1918
FilterContent: "ignored_service",
20-
FilterType: "SQL",
19+
FilterType: "sql",
2120
},
2221
})
2322

@@ -30,7 +29,7 @@ func TestIsSqlInBlackList(t *testing.T) {
3029
`/* this is a comment, Service: ignored_service */ update * from table_ignored where id < 123;`,
3130
}
3231
for _, matchSql := range matchSqls {
33-
if !filter.IsSqlInBlackList(matchSql) {
32+
if _, isSqlInBlackList := filter.IsSqlInBlackList(matchSql); !isSqlInBlackList {
3433
t.Error("Expected SQL to match blacklist")
3534
}
3635
}
@@ -42,20 +41,20 @@ func TestIsSqlInBlackList(t *testing.T) {
4241
service */ update * from table_ignored where id < 123;`,
4342
}
4443
for _, notMatchSql := range notMatchSqls {
45-
if filter.IsSqlInBlackList(notMatchSql) {
44+
if _, isSqlInBlackList := filter.IsSqlInBlackList(notMatchSql); isSqlInBlackList {
4645
t.Error("Did not expect SQL to match blacklist")
4746
}
4847
}
4948
}
5049

5150
func TestIsIpInBlackList(t *testing.T) {
52-
filter := v1.ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
51+
filter := ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
5352
{
5453
FilterContent: "192.168.1.23",
55-
FilterType: "IP",
54+
FilterType: "ip",
5655
}, {
5756
FilterContent: "10.0.5.67",
58-
FilterType: "IP",
57+
FilterType: "ip",
5958
},
6059
})
6160

@@ -64,7 +63,7 @@ func TestIsIpInBlackList(t *testing.T) {
6463
"192.168.1.23",
6564
}
6665
for _, matchIp := range matchIps {
67-
if !filter.HasEndpointInBlackList([]string{matchIp}) {
66+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{matchIp}); !hasEndpointInBlackList {
6867
t.Error("Expected Ip to match blacklist")
6968
}
7069
}
@@ -75,20 +74,20 @@ func TestIsIpInBlackList(t *testing.T) {
7574
"50.67.89.12",
7675
}
7776
for _, notMatchIp := range notMatchIps {
78-
if filter.HasEndpointInBlackList([]string{notMatchIp}) {
77+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{notMatchIp}); hasEndpointInBlackList {
7978
t.Error("Did not expect Ip to match blacklist")
8079
}
8180
}
8281
}
8382

8483
func TestIsCidrInBlackList(t *testing.T) {
85-
filter := v1.ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
84+
filter := ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
8685
{
8786
FilterContent: "192.168.0.0/24",
88-
FilterType: "CIDR",
87+
FilterType: "cidr",
8988
}, {
9089
FilterContent: "10.100.0.0/16",
91-
FilterType: "CIDR",
90+
FilterType: "cidr",
9291
},
9392
})
9493

@@ -99,7 +98,7 @@ func TestIsCidrInBlackList(t *testing.T) {
9998
"192.168.0.45",
10099
}
101100
for _, matchIp := range matchIps {
102-
if !filter.HasEndpointInBlackList([]string{matchIp}) {
101+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{matchIp}); !hasEndpointInBlackList {
103102
t.Error("Expected CIDR to match blacklist")
104103
}
105104
}
@@ -112,20 +111,20 @@ func TestIsCidrInBlackList(t *testing.T) {
112111
"172.30.30.45",
113112
}
114113
for _, notMatchIp := range notMatchIps {
115-
if filter.HasEndpointInBlackList([]string{notMatchIp}) {
114+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{notMatchIp}); hasEndpointInBlackList {
116115
t.Error("Did not expect CIDR to match blacklist")
117116
}
118117
}
119118
}
120119

121120
func TestIsHostInBlackList(t *testing.T) {
122-
filter := v1.ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
121+
filter := ConvertToBlackFilter([]*model.BlackListAuditPlanSQL{
123122
{
124123
FilterContent: "host",
125-
FilterType: "HOST",
124+
FilterType: "host",
126125
}, {
127126
FilterContent: "some_site",
128-
FilterType: "HOST",
127+
FilterType: "host",
129128
},
130129
})
131130

@@ -138,7 +137,7 @@ func TestIsHostInBlackList(t *testing.T) {
138137
}
139138

140139
for _, matchHost := range matchHosts {
141-
if !filter.HasEndpointInBlackList([]string{matchHost}) {
140+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{matchHost}); !hasEndpointInBlackList {
142141
t.Error("Expected HOST to match blacklist")
143142
}
144143
}
@@ -148,7 +147,7 @@ func TestIsHostInBlackList(t *testing.T) {
148147
"any_other_site/local",
149148
}
150149
for _, noMatchHost := range notMatchHosts {
151-
if filter.HasEndpointInBlackList([]string{noMatchHost}) {
150+
if _, hasEndpointInBlackList := filter.HasEndpointInBlackList([]string{noMatchHost}); hasEndpointInBlackList {
152151
t.Error("Did not expect HOST to match blacklist")
153152
}
154153
}

sqle/server/sqled_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func Test_action_audit_UpdateTask(t *testing.T) {
173173
WithArgs("").
174174
WillReturnRows(sqlmock.NewRows([]string{"value", "match_type"}).AddRow(whitelist.Value, whitelist.MatchType))
175175

176+
mock.ExpectBegin()
177+
mock.ExpectExec(regexp.QuoteMeta("UPDATE `sql_whitelist` SET `last_matched_time`=?,`matched_count`=matched_count + ? WHERE sql_whitelist.id = ? AND `sql_whitelist`.`deleted_at` IS NULL")).
178+
WillReturnResult(sqlmock.NewResult(1, 1))
179+
mock.ExpectCommit()
180+
176181
mock.ExpectBegin()
177182
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO `execute_sql_detail`")).
178183
// WithArgs(model.MockTime, model.MockTime, nil, 0, 0, act.task.ExecuteSQLs[0].Content, "", "", 0, "", 0, 0, "", "", "", 0, "", model.SQLAuditStatusFinished, `[{"level":"normal","message":"白名单","rule_name":""}]`, "2882fdbb7d5bcda7b49ea0803493467e", "normal").

0 commit comments

Comments
 (0)