Skip to content

Commit 19495a6

Browse files
committed
test: add test cases for monitor mode
1 parent ec597d9 commit 19495a6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

pkg/agent/datapath/multiBridgeDatapath_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828

2929
log "github.com/Sirupsen/logrus"
3030
. "github.com/onsi/gomega"
31+
32+
"github.com/everoute/everoute/pkg/apis/security/v1alpha1"
3133
)
3234

3335
const (
@@ -110,6 +112,7 @@ func TestDpManager(t *testing.T) {
110112

111113
testLocalEndpoint(t)
112114
testERPolicyRule(t)
115+
testMonitorRule(t)
113116
testFlowReplay(t)
114117
testRoundNumFlip(t)
115118
}
@@ -171,6 +174,34 @@ func testERPolicyRule(t *testing.T) {
171174
})
172175
}
173176

177+
func testMonitorRule(t *testing.T) {
178+
t.Run("test ER policy rule with monitor mode", func(t *testing.T) {
179+
if err := datapathManager.AddEveroutePolicyRule(rule1, "rule1", POLICY_DIRECTION_IN, POLICY_TIER2, v1alpha1.MonitorMode.String()); err != nil {
180+
t.Errorf("Failed to add ER policy rule: %v, error: %v", rule1, err)
181+
}
182+
if _, ok := datapathManager.Rules[rule1.RuleID]; !ok {
183+
t.Errorf("Failed to add ER policy rule, not found %v in cache", rule1)
184+
}
185+
186+
if err := datapathManager.RemoveEveroutePolicyRule(rule1.RuleID, "rule1"); err != nil {
187+
t.Errorf("Failed to remove ER policy rule: %v, error: %v", rule1, err)
188+
}
189+
if _, ok := datapathManager.Rules[rule1.RuleID]; ok {
190+
t.Errorf("Failed to remove ER policy rule, rule %v in cache", rule1)
191+
}
192+
193+
if err := datapathManager.AddEveroutePolicyRule(rule2, "rule2", POLICY_DIRECTION_OUT, POLICY_TIER1, v1alpha1.MonitorMode.String()); err != nil {
194+
t.Errorf("Failed to add ER policy rule: %v, error: %v", rule2, err)
195+
}
196+
if _, ok := datapathManager.Rules[rule2.RuleID]; !ok {
197+
t.Errorf("Failed to add ER policy rule, not found %v in cache", rule2)
198+
}
199+
if err := datapathManager.AddEveroutePolicyRule(rule2, "rule2", POLICY_DIRECTION_OUT, POLICY_TIER1, v1alpha1.MonitorMode.String()); err != nil {
200+
t.Errorf("Failed to add ER policy rule: %v, error: %v", rule2, err)
201+
}
202+
})
203+
}
204+
174205
func testFlowReplay(t *testing.T) {
175206
RegisterTestingT(t)
176207

tests/e2e/cases/security_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,35 @@ var _ = Describe("SecurityPolicy", func() {
224224
})
225225
})
226226

227+
When("create monitor mode security policies", func() {
228+
var nginxPolicy, serverPolicy, dbPolicy *securityv1alpha1.SecurityPolicy
229+
230+
BeforeEach(func() {
231+
nginxPolicy = newPolicy("nginx-policy", constants.Tier2, securityv1alpha1.DefaultRuleDrop, nginxSelector)
232+
nginxPolicy.Spec.SecurityPolicyEnforcementMode = securityv1alpha1.MonitorMode
233+
addIngressRule(nginxPolicy, "TCP", nginxPort) // allow all connection with nginx port
234+
addEngressRule(nginxPolicy, "TCP", serverPort, serverSelector)
235+
236+
serverPolicy = newPolicy("server-policy", constants.Tier2, securityv1alpha1.DefaultRuleDrop, serverSelector)
237+
serverPolicy.Spec.SecurityPolicyEnforcementMode = securityv1alpha1.MonitorMode
238+
addIngressRule(serverPolicy, "TCP", serverPort, nginxSelector)
239+
addEngressRule(serverPolicy, "TCP", dbPort, dbSelector)
240+
241+
dbPolicy = newPolicy("db-policy", constants.Tier2, securityv1alpha1.DefaultRuleDrop, dbSelector)
242+
dbPolicy.Spec.SecurityPolicyEnforcementMode = securityv1alpha1.MonitorMode
243+
addIngressRule(dbPolicy, "TCP", dbPort, dbSelector, serverSelector)
244+
addEngressRule(dbPolicy, "TCP", dbPort, dbSelector)
245+
246+
Expect(e2eEnv.SetupObjects(ctx, nginxPolicy, serverPolicy, dbPolicy)).Should(Succeed())
247+
})
248+
249+
It("should allow all packets", func() {
250+
assertReachable([]*model.Endpoint{nginx, client, server01, server02, db01, db02},
251+
[]*model.Endpoint{nginx, client, server01, server02, db01, db02}, "TCP", true)
252+
})
253+
254+
})
255+
227256
When("limits icmp packets between components", func() {
228257
var icmpAllowPolicy, icmpDropPolicy *securityv1alpha1.SecurityPolicy
229258

0 commit comments

Comments
 (0)