ad controller populator: check for inUse sync in test

This commit is contained in:
huweiwen 2023-10-28 00:08:55 +08:00
parent 63b3085f2a
commit 854347ac81
2 changed files with 30 additions and 25 deletions

View File

@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers" "k8s.io/client-go/informers"
kcache "k8s.io/client-go/tools/cache" kcache "k8s.io/client-go/tools/cache"
"k8s.io/klog/v2/ktesting" "k8s.io/klog/v2/ktesting"
@ -126,11 +127,22 @@ func Test_AttachDetachControllerStateOfWorldPopulators_Positive(t *testing.T) {
for _, node := range nodes { for _, node := range nodes {
nodeName := types.NodeName(node.Name) 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 { for _, attachedVolume := range node.Status.VolumesAttached {
attachedState := adc.actualStateOfWorld.GetAttachState(attachedVolume.Name, nodeName) attachedState := adc.actualStateOfWorld.GetAttachState(attachedVolume.Name, nodeName)
if attachedState != cache.AttachStateAttached { if attachedState != cache.AttachStateAttached {
t.Fatalf("Run failed with error. Node %s, volume %s not found", nodeName, attachedVolume.Name) 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)
}
} }
} }

View File

@ -155,8 +155,9 @@ func CreateTestClient() *fake.Clientset {
// We want also the "mynode" node since all the testing pods live there // We want also the "mynode" node since all the testing pods live there
nodeName = nodeNamePrefix 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) { fakeClient.AddReactor("update", "nodes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
updateAction := action.(core.UpdateAction) updateAction := action.(core.UpdateAction)
node := updateAction.GetObject().(*v1.Node) node := updateAction.GetObject().(*v1.Node)
@ -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 // if nodeName exists, get the object.. if not create node object
var node *v1.Node var node *v1.Node
found := false
nodes.Size()
for i := range nodes.Items { for i := range nodes.Items {
curNode := nodes.Items[i] curNode := &nodes.Items[i]
if curNode.ObjectMeta.Name == nodeName { if curNode.ObjectMeta.Name == nodeName {
node = &curNode node = curNode
found = true
break break
} }
} }
if !found { if node == nil {
node = &v1.Node{ nodes.Items = append(nodes.Items, v1.Node{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: nodeName, Name: nodeName,
Labels: map[string]string{ Labels: map[string]string{
@ -336,24 +334,19 @@ func attachVolumeToNode(nodes *v1.NodeList, volumeName, nodeName string) {
util.ControllerManagedAttachAnnotation: "true", util.ControllerManagedAttachAnnotation: "true",
}, },
}, },
Status: v1.NodeStatus{ })
VolumesAttached: []v1.AttachedVolume{ node = &nodes.Items[len(nodes.Items)-1]
{
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)
} }
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 { type TestPlugin struct {