Skip to content

Commit

Permalink
Merge pull request kubeedge#5803 from 1Shubham7/dynamic-ctrl
Browse files Browse the repository at this point in the history
Test coverage for `cloud/pkg/dynamiccontroller` module
  • Loading branch information
kubeedge-bot authored Aug 28, 2024
2 parents 7dfb5aa + bc66592 commit c26b5a2
Show file tree
Hide file tree
Showing 4 changed files with 637 additions and 20 deletions.
107 changes: 87 additions & 20 deletions cloud/pkg/dynamiccontroller/application/listener_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package application

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -32,7 +32,22 @@ var testGVR = schema.GroupVersionResource{
var selector1 = NewSelector("key1=value1", "")
var selector2 = NewSelector("key2=value2", "")

func TestAddGetListener(t *testing.T) {
func TestNewListenerManager(t *testing.T) {
assert := assert.New(t)

lm := newListenerManager()

assert.NotNil(lm)
assert.NotNil(lm.listenerByNodeID)
assert.Empty(lm.listenerByNodeID)

assert.NotNil(lm.listenerByGVR)
assert.Empty(lm.listenerByGVR)
}

func TestAddListener(t *testing.T) {
assert := assert.New(t)

listener1 := NewSelectorListener("testID1", "node1", testGVR, selector1)
listener2 := NewSelectorListener("testID2", "node2", testGVR, selector2)

Expand All @@ -42,32 +57,26 @@ func TestAddGetListener(t *testing.T) {
lm.AddListener(listener2)

listenerByNodeID := lm.GetListenersForNode("node1")
if len(listenerByNodeID) != 1 {
t.Errorf("listenerByNodeID expected length 1. but got %v", len(listenerByNodeID))
}
assert.Len(listenerByNodeID, 1)

for _, v := range listenerByNodeID {
if !reflect.DeepEqual(v, listener1) {
t.Errorf("expected %v. but got %v", listener1, v)
}
assert.Equal(listener1, v)
}

listenerByGVR := lm.GetListenersForGVR(testGVR)
if len(listenerByGVR) != 2 {
t.Errorf("listenerByGVR expected length 2. but got %v", len(listenerByNodeID))
}
assert.Len(listenerByGVR, 2)

expected := map[string]*SelectorListener{
listener1.id: listener1,
listener2.id: listener2,
}

if !reflect.DeepEqual(expected, listenerByGVR) {
t.Errorf("expected %v. but got %v", expected, listenerByGVR)
}
assert.Equal(expected, listenerByGVR)
}

func TestDeleteListener(t *testing.T) {
assert := assert.New(t)

listener1 := NewSelectorListener("testID1", "node1", testGVR, selector1)
listener2 := NewSelectorListener("testID2", "node2", testGVR, selector2)

Expand All @@ -78,13 +87,71 @@ func TestDeleteListener(t *testing.T) {

lm.DeleteListener(listener1)
listenerByNodeID := lm.GetListenersForNode("node1")
if len(listenerByNodeID) != 0 {
t.Errorf("listenerByNodeID expected length 0. but got %v", len(listenerByNodeID))
}
assert.Len(listenerByNodeID, 0)

lm.DeleteListener(listener2)
listenerByGVR := lm.GetListenersForGVR(testGVR)
if len(listenerByGVR) != 0 {
t.Errorf("listenerByGVR expected length 0. but got %v", len(listenerByNodeID))
}
assert.Len(listenerByGVR, 0)
}

func TestGetListenersForNode(t *testing.T) {
assert := assert.New(t)

lm := newListenerManager()

listener1 := NewSelectorListener("testID1", "node1", testGVR, selector1)
listener2 := NewSelectorListener("testID2", "node1", testGVR, selector2)
listener3 := NewSelectorListener("testID3", "node2", testGVR, selector1)

lm.AddListener(listener1)
lm.AddListener(listener2)
lm.AddListener(listener3)

node1Listeners := lm.GetListenersForNode("node1")
assert.Len(node1Listeners, 2)
assert.Contains(node1Listeners, listener1.id)
assert.Contains(node1Listeners, listener2.id)
assert.Equal(listener1, node1Listeners[listener1.id])
assert.Equal(listener2, node1Listeners[listener2.id])

node2Listeners := lm.GetListenersForNode("node2")
assert.Len(node2Listeners, 1)
assert.Contains(node2Listeners, listener3.id)
assert.Equal(listener3, node2Listeners[listener3.id])

// Get listeners for non existent node
nonExistentNodeListeners := lm.GetListenersForNode("node3")
assert.Nil(nonExistentNodeListeners)
}

func TestGetListenersForGVR(t *testing.T) {
assert := assert.New(t)

lm := newListenerManager()

listener1 := NewSelectorListener("testID1", "node1", testGVR, selector1)
listener2 := NewSelectorListener("testID2", "node2", testGVR, selector2)
differentGVR := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"}
listener3 := NewSelectorListener("testID3", "node1", differentGVR, selector1)

lm.AddListener(listener1)
lm.AddListener(listener2)
lm.AddListener(listener3)

gvrListeners := lm.GetListenersForGVR(testGVR)
assert.Len(gvrListeners, 2)
assert.Contains(gvrListeners, listener1.id)
assert.Contains(gvrListeners, listener2.id)
assert.Equal(listener1, gvrListeners[listener1.id])
assert.Equal(listener2, gvrListeners[listener2.id])

differentGVRListeners := lm.GetListenersForGVR(differentGVR)
assert.Len(differentGVRListeners, 1)
assert.Contains(differentGVRListeners, listener3.id)
assert.Equal(listener3, differentGVRListeners[listener3.id])

// Get listeners for non existent GVR
nonExistentGVR := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "nonexistent"}
nonExistentGVRListeners := lm.GetListenersForGVR(nonExistentGVR)
assert.Nil(nonExistentGVRListeners)
}
Loading

0 comments on commit c26b5a2

Please sign in to comment.