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 10b6cc6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
6 changes: 3 additions & 3 deletions hack/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function apply_cassandracluster() {
--namespace "${namespace}" \
--filename \
<(envsubst \
'$NAVIGATOR_IMAGE_REPOSITORY:$NAVIGATOR_IMAGE_TAG:$NAVIGATOR_IMAGE_PULLPOLICY:$CASS_NAME:$CASS_REPLICAS:$CASS_CQL_PORT:$CASS_SEEDS' \
'$NAVIGATOR_IMAGE_REPOSITORY:$NAVIGATOR_IMAGE_TAG:$NAVIGATOR_IMAGE_PULLPOLICY:$CASS_NAME:$CASS_REPLICAS:$CASS_CQL_PORT:$CASS_SEEDS:$CASS_VERSION' \
< "${SCRIPT_DIR}/testdata/cass-cluster-test.template.yaml")
}

Expand Down Expand Up @@ -331,7 +331,7 @@ function test_cassandracluster() {
# TODO: A better test would be to query the endpoints and check that only
# the `-0` pods are included. E.g.
# kubectl -n test-cassandra-1519754828-19864 get ep cass-cassandra-1519754828-19864-cassandra-seedprovider -o "jsonpath={.subsets[*].addresses[*].hostname}"
if ! stdout_equals "cass-${CASS_NAME}-ringnodes-0" \
if ! retry stdout_equals "cass-${CASS_NAME}-ringnodes-0" \
kubectl get pods --namespace "${namespace}" \
--selector=navigator.jetstack.io/cassandra-seed=true \
--output 'jsonpath={.items[*].metadata.name}'
Expand All @@ -358,7 +358,7 @@ function test_cassandracluster() {
fail_test "Failed to apply cassandracluster"
fi

if ! stdout_equals "cass-${CASS_NAME}-ringnodes-0 cass-${CASS_NAME}-ringnodes-1" \
if ! retry stdout_equals "cass-${CASS_NAME}-ringnodes-0 cass-${CASS_NAME}-ringnodes-1" \
kubectl get pods --namespace "${namespace}" \
--selector=navigator.jetstack.io/cassandra-seed=true \
--output 'jsonpath={.items[*].metadata.name}'
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/navigator/validation/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ValidateCassandraClusterNodePool(np *navigator.CassandraClusterNodePool, fl

if np.Seeds < 0 {
allErrs = append(allErrs,
field.Invalid(fldPath.Child("seeds"), np.Seeds, "number of seeds must be 1 or greater"),
field.Invalid(fldPath.Child("seeds"), np.Seeds, "number of seeds must be greater than or equal to 1"),
)
}

Expand Down
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 10b6cc6

Please sign in to comment.