Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rurkss committed Aug 30, 2024
1 parent 2a0c398 commit cf04fb8
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion service/k8s/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (
"github.com/spotahome/redis-operator/log"
"github.com/spotahome/redis-operator/metrics"
"github.com/spotahome/redis-operator/service/k8s"

corev1 "k8s.io/api/core/v1"
)

var (
statefulSetsGroup = schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}
statefulSetsGroup = schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}
persistentVolumeClaimGroup = schema.GroupVersionResource{Group: "", Version: "v1", Resource: "persistentvolumeclaims"}
)

func newStatefulSetUpdateAction(ns string, statefulSet *appsv1.StatefulSet) kubetesting.UpdateActionImpl {
Expand All @@ -38,6 +41,19 @@ func newStatefulSetCreateAction(ns string, statefulSet *appsv1.StatefulSet) kube
return kubetesting.NewCreateAction(statefulSetsGroup, ns, statefulSet)
}

func newStatefulSetDeleteAction(ns string, name string) kubetesting.DeleteActionImpl {
propagation := metav1.DeletePropagationForeground
return kubetesting.NewDeleteActionWithOptions(statefulSetsGroup, ns, name, metav1.DeleteOptions{PropagationPolicy: &propagation})
}

func newPVCUpdateAction(pvc *corev1.PersistentVolumeClaim) kubetesting.UpdateActionImpl {
return kubetesting.NewUpdateAction(persistentVolumeClaimGroup, "", pvc)
}

func newPVCListAction(opts metav1.ListOptions) kubetesting.ListActionImpl {
return kubetesting.NewListAction(persistentVolumeClaimGroup, schema.GroupVersionKind{Group: "", Version: "v1", Kind: "PersistentVolumeClaim"}, "", opts)
}

func TestStatefulSetServiceGetCreateOrUpdate(t *testing.T) {
testStatefulSet := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -195,6 +211,15 @@ func TestStatefulSetServiceGetCreateOrUpdate(t *testing.T) {
},
}
// Mock.
opts := metav1.ListOptions{
LabelSelector: "app.kubernetes.io/component=redis,app.kubernetes.io/name=teststatefulSet1,app.kubernetes.io/part-of=redis-failover",
}
expActions := []kubetesting.Action{
newStatefulSetGetAction(testns, beforeSts.ObjectMeta.Name),
newPVCListAction(opts),
newPVCUpdateAction(&pvcList.Items[0]),
newStatefulSetDeleteAction(testns, afterSts.ObjectMeta.Name),
}
mcli := &kubernetes.Clientset{}
mcli.AddReactor("get", "statefulsets", func(action kubetesting.Action) (bool, runtime.Object, error) {
return true, beforeSts, nil
Expand All @@ -207,17 +232,36 @@ func TestStatefulSetServiceGetCreateOrUpdate(t *testing.T) {
pvcList.Items[0] = *action.(kubetesting.UpdateActionImpl).Object.(*v1.PersistentVolumeClaim)
return true, action.(kubetesting.UpdateActionImpl).Object, nil
})

mcli.AddReactor("delete", "statefulsets", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
return true, nil, nil
})

service := k8s.NewStatefulSetService(mcli, log.Dummy, metrics.Dummy)
err := service.CreateOrUpdateStatefulSet(testns, afterSts)
assert.NoError(err)
assert.Equal(pvcList.Items[0].Spec.Resources, pvcList.Items[1].Spec.Resources)
assert.Equal(expActions, mcli.Actions())
// should not call update

mcli = &kubernetes.Clientset{}
mcli.AddReactor("get", "statefulsets", func(action kubetesting.Action) (bool, runtime.Object, error) {
return true, afterSts, nil
})

expActions = []kubetesting.Action{
newStatefulSetGetAction(testns, beforeSts.ObjectMeta.Name),
newPVCListAction(opts),
newStatefulSetUpdateAction(testns, afterSts),
}

mcli.AddReactor("update", "persistentvolumeclaims", func(action kubetesting.Action) (handled bool, ret runtime.Object, err error) {
panic("shouldn't call update")
})
service = k8s.NewStatefulSetService(mcli, log.Dummy, metrics.Dummy)
err = service.CreateOrUpdateStatefulSet(testns, afterSts)
assert.NoError(err)
assert.Equal(expActions, mcli.Actions())
})
}
}

0 comments on commit cf04fb8

Please sign in to comment.