Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kragniz committed Mar 5, 2018
1 parent b0b47cb commit f3210eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
20 changes: 14 additions & 6 deletions pkg/controllers/cassandra/seedlabeller/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func (c *defaultSeedLabeller) labelSeedNodes(
return nil
}

// default to not a seed
desiredLabel := "false"

// label first n as seeds
if i < np.Seeds {
isSeed := i < np.Seeds

desiredLabel := ""
if isSeed {
desiredLabel = seedprovider.SeedLabelValue
}

Expand All @@ -70,7 +70,13 @@ func (c *defaultSeedLabeller) labelSeedNodes(
if labels == nil {
labels = map[string]string{}
}
labels[seedprovider.SeedLabelKey] = desiredLabel

if isSeed {
labels[seedprovider.SeedLabelKey] = desiredLabel
} else {
delete(labels, seedprovider.SeedLabelKey)
}

podCopy := pod.DeepCopy()
podCopy.SetLabels(labels)
_, err = c.kubeClient.CoreV1().Pods(podCopy.Namespace).Update(podCopy)
Expand All @@ -87,8 +93,10 @@ func (c *defaultSeedLabeller) Sync(cluster *v1alpha1.CassandraCluster) error {

set, err := c.statefulSetLister.StatefulSets(cluster.Namespace).Get(setName)
if err != nil {
return err
glog.Warningf("Couldn't get stateful set: %v", err)
return nil
}

err = c.labelSeedNodes(cluster, &np, set)
if err != nil {
return err
Expand Down
40 changes: 35 additions & 5 deletions pkg/controllers/cassandra/seedlabeller/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
casstesting "github.com/jetstack/navigator/pkg/controllers/cassandra/testing"
)

func CheckSeedLabel(podName, podNamespace string, t *testing.T, state *controllers.State) {
func CheckSeedLabel(podName, seedLabelValue string, podNamespace string, t *testing.T, state *controllers.State) {
p, err := state.Clientset.
CoreV1().
Pods(podNamespace).
Get(podName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
if p.Labels[seedprovider.SeedLabelKey] != seedprovider.SeedLabelValue {
if p.Labels[seedprovider.SeedLabelKey] != seedLabelValue {
t.Errorf("unexpected seed label: %s", p.Labels)
}
}
Expand All @@ -44,6 +44,20 @@ func TestSeedLabellerSync(t *testing.T) {
pod0ValueIncorrect := pod0LabelMissing.DeepCopy()
pod0ValueIncorrect.Labels[seedprovider.SeedLabelKey] = "blah"

pod1 := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cass-cassandra-1-RingNodes-1",
Namespace: cluster.Namespace,
},
}

pod2 := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cass-cassandra-1-RingNodes-2",
Namespace: cluster.Namespace,
},
}

type testT struct {
kubeObjects []runtime.Object
navObjects []runtime.Object
Expand All @@ -63,23 +77,39 @@ func TestSeedLabellerSync(t *testing.T) {
navObjects: []runtime.Object{cluster},
cluster: cluster,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod0.Name, pod0.Namespace, t, state)
CheckSeedLabel(pod0.Name, seedprovider.SeedLabelValue, pod0.Namespace, t, state)
},
},
"add label if key missing": {
kubeObjects: []runtime.Object{ss0, pod0LabelMissing},
navObjects: []runtime.Object{cluster},
cluster: cluster,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod0.Name, pod0.Namespace, t, state)
CheckSeedLabel(pod0.Name, seedprovider.SeedLabelValue, pod0.Namespace, t, state)
},
},
"fix label if value incorrect": {
kubeObjects: []runtime.Object{ss0, pod0ValueIncorrect},
navObjects: []runtime.Object{cluster},
cluster: cluster,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod0.Name, pod0.Namespace, t, state)
CheckSeedLabel(pod0.Name, seedprovider.SeedLabelValue, pod0.Namespace, t, state)
},
},
"add multiple seeds": {
kubeObjects: []runtime.Object{ss0, pod0, pod1, pod2},
navObjects: []runtime.Object{cluster},
cluster: cluster,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod1.Name, seedprovider.SeedLabelValue, pod1.Namespace, t, state)
},
},
"don't add too many seeds": {
kubeObjects: []runtime.Object{ss0, pod0, pod1, pod2},
navObjects: []runtime.Object{cluster},
cluster: cluster,
assertions: func(t *testing.T, state *controllers.State) {
CheckSeedLabel(pod2.Name, "", pod2.Namespace, t, state)
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/cassandra/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func ClusterForTest() *v1alpha1.CassandraCluster {
v1alpha1.CassandraClusterNodePool{
Name: "RingNodes",
Replicas: 3,
Seeds: 2,
},
},
},
Expand Down

0 comments on commit f3210eb

Please sign in to comment.