Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ukclivecox committed Mar 30, 2020
1 parent 5ed37bf commit 650ed50
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions operator/controllers/replicas_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package controllers

import (
. "github.com/onsi/gomega"
machinelearningv1 "github.com/seldonio/seldon-core/operator/apis/machinelearning/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"testing"
)

func createSeldonDeploymentWithReplicas(name string, namespace string, defaultReplicas *int32, predictorReplicas *int32, componentSpecReplicas *int32, svcOrchReplicas *int32) *machinelearningv1.SeldonDeployment {
modelType := machinelearningv1.MODEL
instance := &machinelearningv1.SeldonDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: machinelearningv1.SeldonDeploymentSpec{
DefaultReplicas: defaultReplicas,
Predictors: []machinelearningv1.PredictorSpec{
{
Name: "p1",
Replicas: predictorReplicas,
ComponentSpecs: []*machinelearningv1.SeldonPodSpec{
{
Metadata: metav1.ObjectMeta{},
Replicas: componentSpecReplicas,
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Image: "seldonio/mock_classifier:1.0",
Name: "classifier",
},
},
},
},
},
Graph: &machinelearningv1.PredictiveUnit{
Name: "classifier",
Type: &modelType,
},
},
},
},
}

if svcOrchReplicas != nil {
instance.Spec.Annotations = map[string]string{machinelearningv1.ANNOTATION_SEPARATE_ENGINE: "true"}
instance.Spec.Predictors[0].SvcOrchSpec = machinelearningv1.SvcOrchSpec{
Replicas: svcOrchReplicas,
}
}
return instance
}

func TestDeploymentReplicas(t *testing.T) {
g := NewGomegaWithT(t)
svcOrchReplicas := int32(2)
defaultReplicas := int32(3)
predictorReplicas := int32(4)
componentSpecReplicas := int32(5)
name := "dep"
namespace := "default"

logger := ctrl.Log.WithName("controllers").WithName("SeldonDeployment")
reconciler := &SeldonDeploymentReconciler{
Log: logger,
}

// Just Predictor Replicas
instance := createSeldonDeploymentWithReplicas(name, namespace, nil, &predictorReplicas, nil, nil)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err := reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(1))
g.Expect(*c.deployments[0].Spec.Replicas).To(Equal(predictorReplicas))

// Just Predictor Replicas and default replicas
instance = createSeldonDeploymentWithReplicas(name, namespace, &defaultReplicas, &predictorReplicas, nil, nil)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err = reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(1))
g.Expect(*c.deployments[0].Spec.Replicas).To(Equal(predictorReplicas))

// ComponentSpec replica override
instance = createSeldonDeploymentWithReplicas(name, namespace, &defaultReplicas, &predictorReplicas, &componentSpecReplicas, nil)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err = reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(1))
g.Expect(*c.deployments[0].Spec.Replicas).To(Equal(componentSpecReplicas))

// Just defaultReplicas
instance = createSeldonDeploymentWithReplicas(name, namespace, &defaultReplicas, nil, nil, nil)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err = reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(1))
g.Expect(*c.deployments[0].Spec.Replicas).To(Equal(defaultReplicas))

// All nil
instance = createSeldonDeploymentWithReplicas(name, namespace, nil, nil, nil, nil)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err = reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(1))
g.Expect(c.deployments[0].Spec.Replicas).To(BeNil())

// SvcOrchReplicas
instance = createSeldonDeploymentWithReplicas(name, namespace, nil, nil, nil, &svcOrchReplicas)
instance.Spec.DefaultSeldonDeployment(name, namespace)
c, err = reconciler.createComponents(instance, logger)
g.Expect(err).To(BeNil())
g.Expect(len(c.deployments)).To(Equal(2))
g.Expect(*c.deployments[0].Spec.Replicas).To(Equal(svcOrchReplicas))
g.Expect(c.deployments[1].Spec.Replicas).To(BeNil())
}

0 comments on commit 650ed50

Please sign in to comment.