test: fix storage status update test

After enabling PersistentVolumeLastPhaseTransitionTime feature, any
test that compares PV objects that transitioned phase needs to handle
timestamp values correctly.

Either the tests should avoid phase transitions if not needed or the
test needs to set the same timestamp on new PV object so it's not
changed and can be checked for equality later, the latter is used in
this commit.
This commit is contained in:
Roman Bednar 2023-09-13 11:36:50 +02:00
parent 53339894a1
commit fb872e8638

View File

@ -22,6 +22,7 @@ import (
"k8s.io/kubernetes/pkg/features"
"testing"
"context"
"github.com/google/go-cmp/cmp"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
@ -35,6 +36,7 @@ import (
"k8s.io/apiserver/pkg/registry/rest"
etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/registry/core/persistentvolume"
"k8s.io/kubernetes/pkg/registry/registrytest"
)
@ -60,6 +62,7 @@ func newHostPathType(pathType string) *api.HostPathType {
}
func validNewPersistentVolume(name string) *api.PersistentVolume {
now := persistentvolume.NowFunc()
pv := &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: name,
@ -75,9 +78,10 @@ func validNewPersistentVolume(name string) *api.PersistentVolume {
PersistentVolumeReclaimPolicy: api.PersistentVolumeReclaimRetain,
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumePending,
Message: "bar",
Reason: "foo",
Phase: api.VolumePending,
Message: "bar",
Reason: "foo",
LastPhaseTransitionTime: &now,
},
}
return pv
@ -181,12 +185,19 @@ func TestUpdateStatus(t *testing.T) {
t.Errorf("unexpected error: %v", err)
}
pvStartTimestamp, err := getPhaseTranstitionTime(ctx, pvStart.Name, storage)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
pvIn := &api.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
// Set the same timestamp as original PV so this won't get updated on phase change breaking DeepEqual() later in test.
LastPhaseTransitionTime: pvStartTimestamp,
},
}
@ -205,6 +216,14 @@ func TestUpdateStatus(t *testing.T) {
}
}
func getPhaseTranstitionTime(ctx context.Context, pvName string, storage *REST) (*metav1.Time, error) {
obj, err := storage.Get(ctx, pvName, &metav1.GetOptions{})
if err != nil {
return nil, err
}
return obj.(*api.PersistentVolume).Status.LastPhaseTransitionTime, nil
}
func TestShortNames(t *testing.T) {
storage, _, server := newStorage(t)
defer server.Terminate(t)