Skip to content

Commit 93ad551

Browse files
committed
*.: Add more fake test
1 parent 1364a02 commit 93ad551

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

pkg/controller/pv_control.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
apierrs "k8s.io/apimachinery/pkg/api/errors"
1010
"k8s.io/apimachinery/pkg/types"
1111
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
12+
coreinformers "k8s.io/client-go/informers/core/v1"
1213
"k8s.io/client-go/kubernetes"
1314
corelisters "k8s.io/client-go/listers/core/v1"
15+
"k8s.io/client-go/tools/cache"
1416
"k8s.io/client-go/tools/record"
1517
"k8s.io/client-go/util/retry"
1618

@@ -148,3 +150,68 @@ func (rpc *realPVControl) recordPVEvent(verb string, tc *v1alpha1.Redis, pvName
148150
}
149151

150152
var _ PVControlInterface = &realPVControl{}
153+
154+
// FakePVControl is a fake PVControlInterface
155+
type FakePVControl struct {
156+
PVCLister corelisters.PersistentVolumeClaimLister
157+
PVIndexer cache.Indexer
158+
updatePVTracker requestTracker
159+
}
160+
161+
// NewFakePVControl returns a FakePVControl
162+
func NewFakePVControl(pvInformer coreinformers.PersistentVolumeInformer, pvcInformer coreinformers.PersistentVolumeClaimInformer) *FakePVControl {
163+
return &FakePVControl{
164+
pvcInformer.Lister(),
165+
pvInformer.Informer().GetIndexer(),
166+
requestTracker{0, nil, 0},
167+
}
168+
}
169+
170+
// SetUpdatePVError sets the error attributes of updatePVTracker
171+
func (fpc *FakePVControl) SetUpdatePVError(err error, after int) {
172+
fpc.updatePVTracker.err = err
173+
fpc.updatePVTracker.after = after
174+
}
175+
176+
// PatchPVReclaimPolicy patchs the reclaim policy of PV
177+
func (fpc *FakePVControl) PatchPVReclaimPolicy(_ *v1alpha1.Redis, pv *corev1.PersistentVolume, reclaimPolicy corev1.PersistentVolumeReclaimPolicy) error {
178+
defer fpc.updatePVTracker.inc()
179+
if fpc.updatePVTracker.errorReady() {
180+
defer fpc.updatePVTracker.reset()
181+
return fpc.updatePVTracker.err
182+
}
183+
pv.Spec.PersistentVolumeReclaimPolicy = reclaimPolicy
184+
185+
return fpc.PVIndexer.Update(pv)
186+
}
187+
188+
// UpdatePV update a pv in a Redis.
189+
func (fpc *FakePVControl) UpdatePV(rc *v1alpha1.Redis, pv *corev1.PersistentVolume) (*corev1.PersistentVolume, error) {
190+
ns := rc.GetNamespace()
191+
if pv.Labels == nil {
192+
pv.Labels = make(map[string]string)
193+
}
194+
if pv.Annotations == nil {
195+
pv.Annotations = make(map[string]string)
196+
}
197+
198+
pvcName := pv.Spec.ClaimRef.Name
199+
pvc, err := fpc.PVCLister.PersistentVolumeClaims(ns).Get(pvcName)
200+
if err != nil {
201+
return nil, err
202+
}
203+
204+
component := pvc.Labels[label.ComponentLabelKey]
205+
podName := pvc.Annotations[label.AnnPodNameKey]
206+
pv.Labels[label.NamespaceLabelKey] = ns
207+
pv.Labels[label.ComponentLabelKey] = component
208+
pv.Labels[label.NameLabelKey] = pvc.Labels[label.NameLabelKey]
209+
pv.Labels[label.ManagedByLabelKey] = pvc.Labels[label.ManagedByLabelKey]
210+
pv.Labels[label.InstanceLabelKey] = pvc.Labels[label.InstanceLabelKey]
211+
212+
setIfNotEmpty(pv.Annotations, label.AnnPodNameKey, podName)
213+
214+
return pv, fpc.PVIndexer.Update(pv)
215+
}
216+
217+
var _ PVControlInterface = &FakePVControl{}

pkg/controller/pvc_control.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"github.com/golang/glog"
88
corev1 "k8s.io/api/core/v1"
99
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
10+
coreinformers "k8s.io/client-go/informers/core/v1"
1011
"k8s.io/client-go/kubernetes"
1112
corelisters "k8s.io/client-go/listers/core/v1"
13+
"k8s.io/client-go/tools/cache"
1214
"k8s.io/client-go/tools/record"
1315
"k8s.io/client-go/util/retry"
1416

@@ -117,3 +119,65 @@ func (rpc *realPVCControl) recordPVCEvent(verb string, rc *v1alpha1.Redis, pvcNa
117119
}
118120

119121
var _ PVCControlInterface = &realPVCControl{}
122+
123+
// FakePVCControl is a fake PVCControlInterface
124+
type FakePVCControl struct {
125+
PVCIndexer cache.Indexer
126+
updatePVCTracker requestTracker
127+
deletePVCTracker requestTracker
128+
}
129+
130+
// NewFakePVCControl returns a FakePVCControl
131+
func NewFakePVCControl(pvcInformer coreinformers.PersistentVolumeClaimInformer) *FakePVCControl {
132+
return &FakePVCControl{
133+
pvcInformer.Informer().GetIndexer(),
134+
requestTracker{0, nil, 0},
135+
requestTracker{0, nil, 0},
136+
}
137+
}
138+
139+
// SetUpdatePVCError sets the error attributes of updatePVCTracker
140+
func (fpc *FakePVCControl) SetUpdatePVCError(err error, after int) {
141+
fpc.updatePVCTracker.err = err
142+
fpc.updatePVCTracker.after = after
143+
}
144+
145+
// SetDeletePVCError sets the error attributes of deletePVCTracker
146+
func (fpc *FakePVCControl) SetDeletePVCError(err error, after int) {
147+
fpc.deletePVCTracker.err = err
148+
fpc.deletePVCTracker.after = after
149+
}
150+
151+
// DeletePVC deletes the pvc
152+
func (fpc *FakePVCControl) DeletePVC(_ *v1alpha1.Redis, pvc *corev1.PersistentVolumeClaim) error {
153+
defer fpc.deletePVCTracker.inc()
154+
if fpc.deletePVCTracker.errorReady() {
155+
defer fpc.deletePVCTracker.reset()
156+
return fpc.deletePVCTracker.err
157+
}
158+
159+
return fpc.PVCIndexer.Delete(pvc)
160+
}
161+
162+
// UpdatePVC updates the annotation, labels and spec of pvc
163+
func (fpc *FakePVCControl) UpdatePVC(rc *v1alpha1.Redis, pvc *corev1.PersistentVolumeClaim, pod *corev1.Pod) (*corev1.PersistentVolumeClaim, error) {
164+
defer fpc.updatePVCTracker.inc()
165+
if fpc.updatePVCTracker.errorReady() {
166+
defer fpc.updatePVCTracker.reset()
167+
return nil, fpc.updatePVCTracker.err
168+
}
169+
170+
if pvc.Annotations == nil {
171+
pvc.Annotations = make(map[string]string)
172+
}
173+
if pvc.Labels == nil {
174+
pvc.Labels = make(map[string]string)
175+
}
176+
177+
podName := pod.GetName()
178+
setIfNotEmpty(pvc.Annotations, label.AnnPodNameKey, podName)
179+
180+
return pvc, fpc.PVCIndexer.Update(pvc)
181+
}
182+
183+
var _ PVCControlInterface = &FakePVCControl{}

0 commit comments

Comments
 (0)