From 2d5bf1d41f092e5f8b67c66e0139082af6e7e0db Mon Sep 17 00:00:00 2001 From: Icarus Wu Date: Sat, 21 Dec 2024 00:39:05 +0800 Subject: [PATCH] intersect unit test Signed-off-by: Icarus Wu --- pkg/plugins/policies/core/rules/rules_test.go | 142 ++++++++++++------ 1 file changed, 94 insertions(+), 48 deletions(-) diff --git a/pkg/plugins/policies/core/rules/rules_test.go b/pkg/plugins/policies/core/rules/rules_test.go index 6a51a859895a..bc74ba7e0260 100644 --- a/pkg/plugins/policies/core/rules/rules_test.go +++ b/pkg/plugins/policies/core/rules/rules_test.go @@ -241,135 +241,181 @@ var _ = Describe("Rules", func() { func(given testCase) { Expect(given.s1.Intersect(given.s2)).To(Equal(given.intersect)) }, - // TODO(Icarus9913): rewrite the description and filter them into "positive" and "negation" types - Entry("entry 1", testCase{ + Entry("positive, same key and value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "backend"}, + {Key: "service", Value: "frontend"}, }, s2: []core_rules.Tag{ - {Key: "service", Not: true, Value: "frontend"}, - {Key: "version", Value: "v2"}, + {Key: "service", Value: "frontend"}, }, intersect: true, }), - Entry("entry 2", testCase{ + Entry("positive, same key, different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "backend"}, + {Key: "service", Value: "frontend"}, }, s2: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, - {Key: "version", Value: "v2"}, + {Key: "service", Value: "backend"}, }, intersect: false, }), - Entry("entry 3", testCase{ + Entry("positive, multiple key-values with overlap key-value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, + {Key: "service", Value: "frontend"}, + {Key: "version", Value: "v1"}, }, s2: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "version", Value: "v2"}, + {Key: "service", Value: "frontend"}, }, intersect: true, }), - Entry("entry 4", testCase{ + Entry("positive, multiple key-values with overlap key but different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "version", Not: true, Value: "v1"}, + {Key: "service", Value: "frontend"}, }, s2: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "version", Not: true, Value: "v1"}, - {Key: "zone", Value: "east"}, + {Key: "service", Value: "backend"}, + {Key: "version", Value: "v1"}, + }, + intersect: false, + }), + Entry("positive, different key, different value", testCase{ + s1: []core_rules.Tag{ + {Key: "service", Value: "frontend"}, + }, + s2: []core_rules.Tag{ + {Key: "version", Value: "v1"}, }, intersect: true, }), - Entry("entry 5", testCase{ + Entry("positive, superset", testCase{ s1: []core_rules.Tag{}, s2: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "version", Not: true, Value: "v1"}, + {Key: "service", Value: "backend"}, + {Key: "version", Value: "v1"}, {Key: "zone", Value: "east"}, }, intersect: true, }), - Entry("entry 6", testCase{ + Entry("a part of negation, same key and value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "version", Not: true, Value: "v1"}, + {Key: "service", Not: true, Value: "frontend"}, + }, + s2: []core_rules.Tag{ + {Key: "service", Value: "frontend"}, }, - s2: []core_rules.Tag{}, intersect: true, }), - Entry("entry 7", testCase{ + Entry("a part of negation, same key, different value", testCase{ s1: []core_rules.Tag{ {Key: "service", Not: true, Value: "frontend"}, }, s2: []core_rules.Tag{ - {Key: "service", Not: true, Value: "backend"}, - {Key: "zone", Value: "east"}, + {Key: "service", Value: "backend"}, }, intersect: true, }), - Entry("entry 8", testCase{ + Entry("a part of negation, multiple key-values with overlap key-value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, + {Key: "service", Not: true, Value: "frontend"}, + {Key: "version", Value: "v1"}, }, s2: []core_rules.Tag{ {Key: "service", Value: "frontend"}, }, intersect: true, }), - Entry("entry 9", testCase{ + Entry("a part of negation, multiple key-values with overlap key but different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, + {Key: "service", Not: true, Value: "frontend"}, + {Key: "version", Value: "v1"}, }, s2: []core_rules.Tag{ {Key: "service", Value: "backend"}, }, - intersect: false, + intersect: true, }), - Entry("entry 10", testCase{ + Entry("a part of negation, different key, different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, + {Key: "service", Not: true, Value: "frontend"}, }, s2: []core_rules.Tag{ {Key: "version", Value: "v1"}, }, intersect: true, }), - Entry("entry 11", testCase{ + Entry("negation, same key and value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "backend"}, - {Key: "version", Value: "v1"}, + {Key: "service", Not: true, Value: "backend"}, }, s2: []core_rules.Tag{ - {Key: "version", Value: "v2"}, + {Key: "service", Not: true, Value: "backend"}, + }, + intersect: true, + }), + Entry("negation, same key, different value", testCase{ + s1: []core_rules.Tag{ + {Key: "service", Not: true, Value: "frontend"}, + }, + s2: []core_rules.Tag{ + {Key: "service", Not: true, Value: "backend"}, + }, + intersect: true, + }), + Entry("negation, multiple key-values with overlap key-value", testCase{ + s1: []core_rules.Tag{ + {Key: "service", Not: true, Value: "backend"}, + {Key: "version", Not: true, Value: "v1"}, + }, + s2: []core_rules.Tag{ + {Key: "service", Not: true, Value: "backend"}, + {Key: "version", Not: true, Value: "v1"}, {Key: "zone", Value: "east"}, }, - intersect: false, + intersect: true, }), - Entry("entry 12", testCase{ + Entry("negation, multiple key-values with overlap key but different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, + {Key: "service", Not: true, Value: "frontend"}, + {Key: "version", Not: true, Value: "v1"}, }, s2: []core_rules.Tag{ - {Key: "version", Value: "v1"}, + {Key: "service", Not: true, Value: "backend"}, + {Key: "version", Not: true, Value: "v1"}, {Key: "zone", Value: "east"}, }, intersect: true, }), - Entry("entry 13", testCase{ + Entry("negation, multiple key-values with overlap key but different value", testCase{ s1: []core_rules.Tag{ - {Key: "service", Value: "frontend"}, - {Key: "version", Value: "v1"}, + {Key: "service", Not: true, Value: "frontend"}, + {Key: "version", Not: true, Value: "v1"}, }, s2: []core_rules.Tag{ - {Key: "version", Value: "v1"}, + {Key: "service", Not: true, Value: "backend"}, + {Key: "version", Not: true, Value: "v1"}, {Key: "zone", Value: "east"}, }, intersect: true, }), + Entry("negation, different key, different value", testCase{ + s1: []core_rules.Tag{ + {Key: "service", Not: true, Value: "frontend"}, + }, + s2: []core_rules.Tag{ + {Key: "version", Not: true, Value: "v1"}, + {Key: "zone", Value: "east"}, + }, + intersect: true, + }), + Entry("negation, superset", testCase{ + s1: []core_rules.Tag{ + {Key: "service", Not: true, Value: "backend"}, + {Key: "version", Not: true, Value: "v1"}, + }, + s2: []core_rules.Tag{}, + intersect: true, + }), ) })