Skip to content

Commit

Permalink
ad controller populator: check for inUse sync in test
Browse files Browse the repository at this point in the history
  • Loading branch information
huww98 authored and pull[bot] committed Dec 19, 2023
1 parent 53fcfc4 commit 1760807
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
kcache "k8s.io/client-go/tools/cache"
"k8s.io/klog/v2/ktesting"
Expand Down Expand Up @@ -126,11 +127,22 @@ func Test_AttachDetachControllerStateOfWorldPopulators_Positive(t *testing.T) {

for _, node := range nodes {
nodeName := types.NodeName(node.Name)
inUseVolumes := sets.New(node.Status.VolumesInUse...)
allAttachedVolumes := map[v1.UniqueVolumeName]cache.AttachedVolume{}
for _, v := range adc.actualStateOfWorld.GetAttachedVolumesForNode(nodeName) {
allAttachedVolumes[v.VolumeName] = v
}

for _, attachedVolume := range node.Status.VolumesAttached {
attachedState := adc.actualStateOfWorld.GetAttachState(attachedVolume.Name, nodeName)
if attachedState != cache.AttachStateAttached {
t.Fatalf("Run failed with error. Node %s, volume %s not found", nodeName, attachedVolume.Name)
}
inUse := inUseVolumes.Has(attachedVolume.Name)
mounted := allAttachedVolumes[attachedVolume.Name].MountedByNode
if mounted != inUse {
t.Fatalf("Node %s, volume %s MountedByNode %v unexpected", nodeName, attachedVolume.Name, mounted)
}
}
}

Expand Down
43 changes: 18 additions & 25 deletions pkg/controller/volume/attachdetach/testing/testvolumespec.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ func CreateTestClient() *fake.Clientset {
// We want also the "mynode" node since all the testing pods live there
nodeName = nodeNamePrefix
}
attachVolumeToNode(nodes, "lostVolumeName", nodeName)
attachVolumeToNode(nodes, "lostVolumeName", nodeName, false)
}
attachVolumeToNode(nodes, "inUseVolume", nodeNamePrefix, true)
fakeClient.AddReactor("update", "nodes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
updateAction := action.(core.UpdateAction)
node := updateAction.GetObject().(*v1.Node)
Expand Down Expand Up @@ -312,21 +313,18 @@ func NewNFSPV(pvName, volumeName string) *v1.PersistentVolume {
}
}

func attachVolumeToNode(nodes *v1.NodeList, volumeName, nodeName string) {
func attachVolumeToNode(nodes *v1.NodeList, volumeName, nodeName string, inUse bool) {
// if nodeName exists, get the object.. if not create node object
var node *v1.Node
found := false
nodes.Size()
for i := range nodes.Items {
curNode := nodes.Items[i]
curNode := &nodes.Items[i]
if curNode.ObjectMeta.Name == nodeName {
node = &curNode
found = true
node = curNode
break
}
}
if !found {
node = &v1.Node{
if node == nil {
nodes.Items = append(nodes.Items, v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
Labels: map[string]string{
Expand All @@ -336,24 +334,19 @@ func attachVolumeToNode(nodes *v1.NodeList, volumeName, nodeName string) {
util.ControllerManagedAttachAnnotation: "true",
},
},
Status: v1.NodeStatus{
VolumesAttached: []v1.AttachedVolume{
{
Name: v1.UniqueVolumeName(TestPluginName + "/" + volumeName),
DevicePath: "fake/path",
},
},
},
}
} else {
volumeAttached := v1.AttachedVolume{
Name: v1.UniqueVolumeName(TestPluginName + "/" + volumeName),
DevicePath: "fake/path",
}
node.Status.VolumesAttached = append(node.Status.VolumesAttached, volumeAttached)
})
node = &nodes.Items[len(nodes.Items)-1]
}
uniqueVolumeName := v1.UniqueVolumeName(TestPluginName + "/" + volumeName)
volumeAttached := v1.AttachedVolume{
Name: uniqueVolumeName,
DevicePath: "fake/path",
}
node.Status.VolumesAttached = append(node.Status.VolumesAttached, volumeAttached)

nodes.Items = append(nodes.Items, *node)
if inUse {
node.Status.VolumesInUse = append(node.Status.VolumesInUse, uniqueVolumeName)
}
}

type TestPlugin struct {
Expand Down

0 comments on commit 1760807

Please sign in to comment.