Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2] chore: added unit tests for SharedState #1478

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions pkg/controller/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -285,109 +283,3 @@ func TestReplicaReconcile(t *testing.T) {
})
}
}

func TestGetNodesForReplica(t *testing.T) {
tests := []struct {
description string
volumes []string
pods []v1.Pod
setupFunc func(*testing.T, *gomock.Controller) *ReconcileReplica
verifyFunc func(*testing.T, []string, error)
}{
{
description: "[Success] Should not select nodes with no remaining capacity.",
volumes: []string{testPersistentVolume0Name},
pods: []v1.Pod{testPod0},
setupFunc: func(t *testing.T, mockCtl *gomock.Controller) *ReconcileReplica {
replicaAttachment := testReplicaAzVolumeAttachment
now := metav1.Time{Time: metav1.Now().Add(-1000)}
replicaAttachment.DeletionTimestamp = &now

newVolume := testAzVolume0.DeepCopy()
newVolume.Status.Detail = &azdiskv1beta2.AzVolumeStatusDetail{
VolumeID: testManagedDiskURI0,
}

newNode := testNode0.DeepCopy()
newNode.Status.Allocatable[consts.AttachableVolumesField] = resource.MustParse("0")

controller := NewTestReplicaController(
mockCtl,
testNamespace,
newVolume,
&testPersistentVolume0,
newNode,
&testNode2,
&testPod0,
)

mockClients(controller.cachedClient.(*mockclient.MockClient), controller.azClient, controller.kubeClient)
return controller
},
verifyFunc: func(t *testing.T, nodes []string, err error) {
require.NoError(t, err)
require.Len(t, nodes, 1)
require.NotEqual(t, nodes[0], testNode0Name)
},
},
{
description: "[Success] Should not create replica attachment on a node that does not match volume's node affinity rule",
volumes: []string{testPersistentVolume0Name},
pods: []v1.Pod{testPod0},
setupFunc: func(t *testing.T, mockCtl *gomock.Controller) *ReconcileReplica {
replicaAttachment := testReplicaAzVolumeAttachment
now := metav1.Time{Time: metav1.Now().Add(-1000)}
replicaAttachment.DeletionTimestamp = &now

newPV := testPersistentVolume0.DeepCopy()
newPV.Spec.NodeAffinity = &v1.VolumeNodeAffinity{
Required: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{{
MatchExpressions: []v1.NodeSelectorRequirement{{
Key: consts.TopologyRegionKey,
Operator: v1.NodeSelectorOpIn,
Values: []string{"westus2"},
}},
}},
},
}

newVolume := testAzVolume0.DeepCopy()
newVolume.Status.Detail = &azdiskv1beta2.AzVolumeStatusDetail{
VolumeID: testManagedDiskURI0,
}

newNode := testNode0.DeepCopy()
newNode.Labels = map[string]string{consts.TopologyRegionKey: "westus2"}

controller := NewTestReplicaController(
mockCtl,
testNamespace,
newVolume,
newPV,
newNode,
&testNode1,
&testPod0,
)

mockClients(controller.cachedClient.(*mockclient.MockClient), controller.azClient, controller.kubeClient)
return controller
},
verifyFunc: func(t *testing.T, nodes []string, err error) {
require.NoError(t, err)
require.Len(t, nodes, 1)
require.NotEqual(t, nodes[0], testNode1Name)
},
},
}
for _, test := range tests {
tt := test
t.Run(tt.description, func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
controller := tt.setupFunc(t, mockCtl)
nodes, err := controller.getRankedNodesForReplicaAttachments(context.TODO(), tt.volumes, tt.pods)
tt.verifyFunc(t, nodes, err)
})
}
}
5 changes: 4 additions & 1 deletion pkg/controller/shared_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ func (c *SharedState) addPod(ctx context.Context, pod *v1.Pod, updateOption upda
}
namespacedClaimName := getQualifiedName(pod.Namespace, volume.PersistentVolumeClaim.ClaimName)
if _, ok := c.claimToVolumeMap.Load(namespacedClaimName); !ok {
w.Logger().V(5).Infof("Skipping Pod %s. Volume %s not csi. Driver: %+v", pod.Name, volume.Name, volume.CSI)
// Log message if the Pod status is Running
if pod.Status.Phase == v1.PodRunning {
w.Logger().V(5).Infof("Skipping Pod %s. Volume %s not csi. Driver: %+v", pod.Name, volume.Name, volume.CSI)
}
continue
}
w.Logger().V(5).Infof("Pod %s. Volume %v is csi.", pod.Name, volume)
Expand Down
Loading