From 5fc989e1749ca02814fafc1f701dc75cc00ae33f Mon Sep 17 00:00:00 2001 From: nanjingfm Date: Tue, 2 Apr 2024 16:11:59 +0800 Subject: [PATCH] feat: deepcopy custom labels of conformance test node (#567) --- testing/framework/conformance/node.go | 8 ++- testing/framework/conformance/node_test.go | 21 ++++++++ testing/framework/conformance/testcase.go | 14 +++++ .../framework/conformance/testcase_test.go | 53 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 testing/framework/conformance/testcase_test.go diff --git a/testing/framework/conformance/node.go b/testing/framework/conformance/node.go index 854a73c0..a05337ce 100644 --- a/testing/framework/conformance/node.go +++ b/testing/framework/conformance/node.go @@ -51,6 +51,11 @@ func (n *Node) AddAdditionalLabels(labels Labels) { n.additionalLabels = append(n.additionalLabels, labels...) } +// AdditionalLabels return the additional labels of the node +func (n *Node) AdditionalLabels() Labels { + return n.additionalLabels +} + // RegisterTestCase iterate over the node tree, register all the test case to ginkgo func (n *Node) RegisterTestCase() { n.registerTestCase() @@ -87,7 +92,8 @@ func (n *Node) clone(original *Node) *Node { } } - clone.additionalLabels = Labels{} + clone.additionalLabels = make(Labels, len(original.additionalLabels)) + copy(clone.additionalLabels, original.additionalLabels) return &clone } diff --git a/testing/framework/conformance/node_test.go b/testing/framework/conformance/node_test.go index caace9ae..1151f221 100644 --- a/testing/framework/conformance/node_test.go +++ b/testing/framework/conformance/node_test.go @@ -108,6 +108,27 @@ var _ = Describe("test for node clone", func() { compareNode(clonedNode.SubNodes[0], subNode) }) }) + + When("with custom labels", func() { + var subNode *Node + BeforeEach(func() { + subNode = NewNode(FunctionLevel, "sub") + subNode.AddAdditionalLabels(Labels{"custom:label"}) + subNode.LinkParentNode(node) + }) + It("should be cloned with custom labels", func() { + checkTopNode(clonedNode) + compareNode(clonedNode, node) + + clonedSubNode := clonedNode.SubNodes[0] + Expect(clonedSubNode.additionalLabels).To(HaveLen(1)) + Expect(clonedSubNode.additionalLabels[0]).To(Equal("custom:label")) + + By("update the original node should not affect the cloned node") + subNode.additionalLabels[0] = "updated" + Expect(clonedSubNode.additionalLabels[0]).To(Equal("custom:label")) + }) + }) }) func TestNode_Equal(t *testing.T) { diff --git a/testing/framework/conformance/testcase.go b/testing/framework/conformance/testcase.go index 4b9f5799..312f796f 100644 --- a/testing/framework/conformance/testcase.go +++ b/testing/framework/conformance/testcase.go @@ -18,8 +18,11 @@ package conformance import ( "context" + + . "github.com/onsi/ginkgo/v2" ) +// NewTestCase a construct new test case func NewTestCase(name string) *testCase { return &testCase{node: NewNode(TestCaseLevel, name)} } @@ -30,6 +33,17 @@ type testCase struct { testPoints []*testPoint } +// AddLabels add custom labels for the test case +func (t *testCase) AddLabels(labels ...Labels) *testCase { + var list Labels + for _, item := range labels { + list = append(list, item...) + } + t.node.AddAdditionalLabels(list) + return t +} + +// Build construct a `CaseSetFactory` with the `caseRegister` func (t *testCase) Build(caseRegister func(ctx context.Context)) CaseSetFactory { return NewCaseProxy(t).Build(caseRegister) } diff --git a/testing/framework/conformance/testcase_test.go b/testing/framework/conformance/testcase_test.go new file mode 100644 index 00000000..249baede --- /dev/null +++ b/testing/framework/conformance/testcase_test.go @@ -0,0 +1,53 @@ +/* +Copyright 2024 The Katanomi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package conformance + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func Test_testCase_AddLabels(t *testing.T) { + g := NewGomegaWithT(t) + tests := []struct { + name string + labels []Labels + want Labels + }{ + { + name: "empty labels", + labels: []Labels{}, + want: nil, + }, + { + name: "multiple labels", + labels: []Labels{{"test"}, {"abc", "123"}}, + want: Labels{"test", "abc", "123"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t1 *testing.T) { + testcase := &testCase{ + node: NewNode(ModuleLevel, "test"), + } + testcase.AddLabels(tt.labels...) + g.Expect(testcase.node.additionalLabels).To(Equal(tt.want)) + }) + } +}