Skip to content

Commit

Permalink
feat: deepcopy custom labels of conformance test node (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm authored Apr 2, 2024
1 parent 4d69642 commit 5fc989e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
8 changes: 7 additions & 1 deletion testing/framework/conformance/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions testing/framework/conformance/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions testing/framework/conformance/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
Expand All @@ -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)
}
Expand Down
53 changes: 53 additions & 0 deletions testing/framework/conformance/testcase_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}

0 comments on commit 5fc989e

Please sign in to comment.