Modify tests

This commit is contained in:
Ayush Pateria 2018-02-22 19:19:06 +05:30
parent 1beed0f4c6
commit a269491f18
3 changed files with 28 additions and 22 deletions

View File

@ -56,17 +56,20 @@ func ControllerRevisionName(prefix string, hash uint32) string {
} }
// NewControllerRevision returns a ControllerRevision with a ControllerRef pointing to parent and indicating that // NewControllerRevision returns a ControllerRevision with a ControllerRef pointing to parent and indicating that
// parent is of parentKind. The ControllerRevision has labels matching pod, contains Data equal to data, and // parent is of parentKind. The ControllerRevision has labels matching template labels, contains Data equal to data, and
// has a Revision equal to revision. The collisionCount is used when creating the name of the ControllerRevision // has a Revision equal to revision. The collisionCount is used when creating the name of the ControllerRevision
// so the name is likely unique. If the returned error is nil, the returned ControllerRevision is valid. If the // so the name is likely unique. If the returned error is nil, the returned ControllerRevision is valid. If the
// returned error is not nil, the returned ControllerRevision is invalid for use. // returned error is not nil, the returned ControllerRevision is invalid for use.
func NewControllerRevision(parent metav1.Object, func NewControllerRevision(parent metav1.Object,
parentKind schema.GroupVersionKind, parentKind schema.GroupVersionKind,
podLabels map[string]string, templateLabels map[string]string,
data runtime.RawExtension, data runtime.RawExtension,
revision int64, revision int64,
collisionCount *int32) (*apps.ControllerRevision, error) { collisionCount *int32) (*apps.ControllerRevision, error) {
labelMap := podLabels labelMap := make(map[string]string)
for k, v := range templateLabels {
labelMap[k] = v
}
blockOwnerDeletion := true blockOwnerDeletion := true
isController := true isController := true
cr := &apps.ControllerRevision{ cr := &apps.ControllerRevision{

View File

@ -77,8 +77,10 @@ func TestRealHistory_ListControllerRevisions(t *testing.T) {
} }
ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"})
ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"}) ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"})
sel1 := labels.Set(ss1.Spec.Template.Labels).AsSelector() sel1, err := metav1.LabelSelectorAsSelector(ss1.Spec.Selector)
if err != nil {
t.Fatal(err)
}
ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil) ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -172,7 +174,10 @@ func TestFakeHistory_ListControllerRevisions(t *testing.T) {
} }
ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"})
ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"}) ss2 := newStatefulSet(3, "ss2", types.UID("ss2"), map[string]string{"goo": "car"})
sel1 := labels.Set(ss1.Spec.Template.Labels).AsSelector() sel1, err := metav1.LabelSelectorAsSelector(ss1.Spec.Selector)
if err != nil {
t.Fatal(err)
}
ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil) ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -1539,6 +1544,7 @@ func TestSortControllerRevisions(t *testing.T) {
} }
ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"}) ss1 := newStatefulSet(3, "ss1", types.UID("ss1"), map[string]string{"foo": "bar"})
ss1.Status.CollisionCount = new(int32) ss1.Status.CollisionCount = new(int32)
ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, ss1.Status.CollisionCount) ss1Rev1, err := NewControllerRevision(ss1, parentKind, ss1.Spec.Template.Labels, rawTemplate(&ss1.Spec.Template), 1, ss1.Status.CollisionCount)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -1583,13 +1589,15 @@ func TestSortControllerRevisions(t *testing.T) {
} }
func newStatefulSet(replicas int, name string, uid types.UID, labels map[string]string) *apps.StatefulSet { func newStatefulSet(replicas int, name string, uid types.UID, labels map[string]string) *apps.StatefulSet {
var testLabelKey string // Converting all the map-only selectors to set-based selectors.
var testLabelValue string var testMatchExpressions []metav1.LabelSelectorRequirement
// Get the first label's key, value to be used for set based selector.
for key, value := range labels { for key, value := range labels {
testLabelKey = key sel := metav1.LabelSelectorRequirement{
testLabelValue = value Key: key,
break Operator: metav1.LabelSelectorOpIn,
Values: []string{value},
}
testMatchExpressions = append(testMatchExpressions, sel)
} }
return &apps.StatefulSet{ return &apps.StatefulSet{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
@ -1603,14 +1611,10 @@ func newStatefulSet(replicas int, name string, uid types.UID, labels map[string]
}, },
Spec: apps.StatefulSetSpec{ Spec: apps.StatefulSetSpec{
Selector: &metav1.LabelSelector{ Selector: &metav1.LabelSelector{
MatchLabels: labels, // Purposely leaving MatchLabels nil, so to ensure it will break if any link
MatchExpressions: []metav1.LabelSelectorRequirement{ // in the chain ignores the set-based MatchExpressions.
{ MatchLabels: nil,
Key: testLabelKey, MatchExpressions: testMatchExpressions,
Operator: metav1.LabelSelectorOpIn,
Values: []string{testLabelValue},
},
},
}, },
Replicas: func() *int32 { i := int32(replicas); return &i }(), Replicas: func() *int32 { i := int32(replicas); return &i }(),
Template: v1.PodTemplateSpec{ Template: v1.PodTemplateSpec{

View File

@ -312,10 +312,9 @@ func newRevision(set *apps.StatefulSet, revision int64, collisionCount *int32) (
if err != nil { if err != nil {
return nil, err return nil, err
} }
podLabels := set.Spec.Template.Labels
cr, err := history.NewControllerRevision(set, cr, err := history.NewControllerRevision(set,
controllerKind, controllerKind,
podLabels, set.Spec.Template.Labels,
runtime.RawExtension{Raw: patch}, runtime.RawExtension{Raw: patch},
revision, revision,
collisionCount) collisionCount)