Skip to content

Commit

Permalink
add NodeNamesDiff test and fix it
Browse files Browse the repository at this point in the history
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
  • Loading branch information
Valeriy Khorunzhin committed Jan 14, 2025
1 parent fdb767e commit ea2db1c
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (h *DiscoveryHandler) Handle(ctx context.Context, s state.VirtualMachineCla
sort.Strings(featuresEnabled)
sort.Strings(featuresNotEnabled)

addedNodes, removedNodes := nodeNamesDiff(current.Status.AvailableNodes, availableNodeNames)
addedNodes, removedNodes := NodeNamesDiff(current.Status.AvailableNodes, availableNodeNames)
if len(addedNodes) > 0 || len(removedNodes) > 0 {
if len(availableNodes) > 0 {
h.recorder.Eventf(
Expand Down Expand Up @@ -203,7 +203,9 @@ func (h *DiscoveryHandler) maxAllocatableResources(nodes []corev1.Node) corev1.R
return resourceList
}

func nodeNamesDiff(prev, current []string) (added, removed []string) {
func NodeNamesDiff(prev, current []string) (added, removed []string) {
added = make([]string, 0)
removed = make([]string, 0)
prevMap := make(map[string]struct{})
currentMap := make(map[string]struct{})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2024 Flant JSC
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 internal

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

type nodeNamesDiffTestParams struct {
prev []string
current []string
added []string
removed []string
}

var _ = Describe("DiscoveryHandler Run", func() {
DescribeTable(
"NodeNamesDiffTest",
func(params nodeNamesDiffTestParams) {
calculatedAdded, calculatedRemoved := NodeNamesDiff(params.prev, params.current)
Expect(calculatedAdded).Should(Equal(params.added))
Expect(calculatedRemoved).Should(Equal(params.removed))
},
Entry(
"Should be no diff",
nodeNamesDiffTestParams{
prev: []string{
"node1",
"node2",
},
current: []string{
"node1",
"node2",
},
added: []string{},
removed: []string{},
},
),
Entry(
"Should be added node",
nodeNamesDiffTestParams{
prev: []string{
"node1",
"node2",
},
current: []string{
"node1",
"node2",
"node3",
},
added: []string{
"node3",
},
removed: []string{},
},
),
Entry(
"Should be removed node",
nodeNamesDiffTestParams{
prev: []string{
"node1",
"node2",
"node3",
},
current: []string{
"node1",
"node2",
},
added: []string{},
removed: []string{
"node3",
},
},
),
Entry(
"Should be added and removed node",
nodeNamesDiffTestParams{
prev: []string{
"node1",
"node2",
},
current: []string{
"node2",
"node3",
},
added: []string{
"node3",
},
removed: []string{
"node1",
},
},
),
Entry(
"Should be multiple added and removed node",
nodeNamesDiffTestParams{
prev: []string{
"node3",
"node4",
"node5",
},
current: []string{
"node1",
"node2",
"node3",
},
added: []string{
"node1",
"node2",
},
removed: []string{
"node4",
"node5",
},
},
),
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 Flant JSC
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 internal

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestInternal(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Internal")
}

0 comments on commit ea2db1c

Please sign in to comment.