@@ -26,6 +26,7 @@ import (
2626 "k8s.io/kubernetes/pkg/api"
2727 "k8s.io/kubernetes/pkg/api/v1"
2828 "k8s.io/kubernetes/pkg/apimachinery/registered"
29+ apps "k8s.io/kubernetes/pkg/apis/apps/v1beta1"
2930 extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
3031 metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
3132 policy "k8s.io/kubernetes/pkg/apis/policy/v1beta1"
@@ -92,6 +93,7 @@ func newFakeDisruptionController() (*DisruptionController, *pdbStates) {
9293 rcLister : cache.StoreToReplicationControllerLister {Indexer : cache .NewIndexer (controller .KeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })},
9394 rsLister : cache.StoreToReplicaSetLister {Indexer : cache .NewIndexer (controller .KeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })},
9495 dLister : cache.StoreToDeploymentLister {Indexer : cache .NewIndexer (controller .KeyFunc , cache.Indexers {})},
96+ ssLister : cache.StoreToStatefulSetLister {Store : cache .NewStore (controller .KeyFunc )},
9597 getUpdater : func () updater { return ps .Set },
9698 broadcaster : record .NewBroadcaster (),
9799 }
@@ -236,6 +238,30 @@ func newReplicaSet(t *testing.T, size int32) (*extensions.ReplicaSet, string) {
236238 return rs , rsName
237239}
238240
241+ func newStatefulSet (t * testing.T , size int32 ) (* apps.StatefulSet , string ) {
242+ ss := & apps.StatefulSet {
243+ TypeMeta : metav1.TypeMeta {APIVersion : registered .GroupOrDie (v1 .GroupName ).GroupVersion .String ()},
244+ ObjectMeta : v1.ObjectMeta {
245+ UID : uuid .NewUUID (),
246+ Name : "foobar" ,
247+ Namespace : v1 .NamespaceDefault ,
248+ ResourceVersion : "18" ,
249+ Labels : fooBar (),
250+ },
251+ Spec : apps.StatefulSetSpec {
252+ Replicas : & size ,
253+ Selector : newSelFooBar (),
254+ },
255+ }
256+
257+ ssName , err := controller .KeyFunc (ss )
258+ if err != nil {
259+ t .Fatalf ("Unexpected error naming StatefulSet %q: %v" , ss .Name , err )
260+ }
261+
262+ return ss , ssName
263+ }
264+
239265func update (t * testing.T , store cache.Store , obj interface {}) {
240266 if err := store .Update (obj ); err != nil {
241267 t .Fatalf ("Could not add %+v to %+v: %v" , obj , store , err )
@@ -409,6 +435,41 @@ func TestReplicationController(t *testing.T) {
409435 ps .VerifyDisruptionAllowed (t , pdbName , 0 )
410436}
411437
438+ func TestStatefulSetController (t * testing.T ) {
439+ labels := map [string ]string {
440+ "foo" : "bar" ,
441+ "baz" : "quux" ,
442+ }
443+
444+ dc , ps := newFakeDisruptionController ()
445+
446+ // 34% should round up to 2
447+ pdb , pdbName := newPodDisruptionBudget (t , intstr .FromString ("34%" ))
448+ add (t , dc .pdbLister .Store , pdb )
449+ ss , _ := newStatefulSet (t , 3 )
450+ add (t , dc .ssLister .Store , ss )
451+ dc .sync (pdbName )
452+
453+ // It starts out at 0 expected because, with no pods, the PDB doesn't know
454+ // about the SS. This is a known bug. TODO(mml): file issue
455+ ps .VerifyPdbStatus (t , pdbName , 0 , 0 , 0 , 0 , map [string ]metav1.Time {})
456+
457+ pods := []* v1.Pod {}
458+
459+ for i := int32 (0 ); i < 3 ; i ++ {
460+ pod , _ := newPod (t , fmt .Sprintf ("foobar %d" , i ))
461+ pods = append (pods , pod )
462+ pod .Labels = labels
463+ add (t , dc .podLister .Indexer , pod )
464+ dc .sync (pdbName )
465+ if i < 2 {
466+ ps .VerifyPdbStatus (t , pdbName , 0 , i + 1 , 2 , 3 , map [string ]metav1.Time {})
467+ } else {
468+ ps .VerifyPdbStatus (t , pdbName , 1 , 3 , 2 , 3 , map [string ]metav1.Time {})
469+ }
470+ }
471+ }
472+
412473func TestTwoControllers (t * testing.T ) {
413474 // Most of this test is in verifying intermediate cases as we define the
414475 // three controllers and create the pods.
0 commit comments