Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekgogia committed Nov 23, 2020
1 parent 2b23031 commit b277a7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
22 changes: 10 additions & 12 deletions pkg/cloudprovider/fake/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/awslabs/karpenter/pkg/apis/autoscaling/v1alpha1"
"github.com/awslabs/karpenter/pkg/cloudprovider"
"knative.dev/pkg/ptr"
)

var (
Expand All @@ -29,14 +28,15 @@ var (
type Factory struct {
WantErr error
// NodeReplicas is use by tests to control observed replicas.
NodeReplicas map[string]int32

nodegroup *NodeGroup
NodeReplicas map[string]*int32
NodeGroupStable bool
NodeGroupMessage string
}

func NewFactory(options cloudprovider.Options) *Factory {
return &Factory{
NodeReplicas: make(map[string]int32),
NodeReplicas: make(map[string]*int32),
NodeGroupStable: true,
}
}

Expand All @@ -45,14 +45,12 @@ func NewNotImplementedFactory() *Factory {
}

func (f *Factory) NodeGroupFor(sng *v1alpha1.ScalableNodeGroupSpec) cloudprovider.NodeGroup {
if f.nodegroup == nil {
f.nodegroup = &NodeGroup{
WantErr: f.WantErr,
Stable: true,
Replicas: ptr.Int32(f.NodeReplicas[sng.ID]),
}
return &NodeGroup{
WantErr: f.WantErr,
Stable: f.NodeGroupStable,
Message: f.NodeGroupMessage,
Replicas: f.NodeReplicas[sng.ID],
}
return f.nodegroup
}

func (f *Factory) QueueFor(spec *v1alpha1.QueueSpec) cloudprovider.Queue {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/horizontalautoscaler/v1alpha1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var _ = Describe("Examples", func() {
It("should scale to average utilization target, metric=85, target=60, replicas=5, want=8", func() {
Expect(ns.ParseResources("docs/examples/reserved-capacity-utilization.yaml", ha, sng)).To(Succeed())
sng.Spec.Replicas = ptr.Int32(5)
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = *sng.Spec.Replicas
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = ptr.Int32(*sng.Spec.Replicas)
MockMetricValue(fakeServer, .85)

ExpectCreated(ns.Client, sng, ha)
Expand All @@ -105,7 +105,7 @@ var _ = Describe("Examples", func() {
It("should scale to average value target, metric=41, target=4, want=11", func() {
Expect(ns.ParseResources("docs/examples/queue-length-average-value.yaml", ha, sng)).To(Succeed())
sng.Spec.Replicas = ptr.Int32(1)
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = *sng.Spec.Replicas
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = ptr.Int32(*sng.Spec.Replicas)
MockMetricValue(fakeServer, 41)

ExpectCreated(ns.Client, sng, ha)
Expand Down
52 changes: 20 additions & 32 deletions pkg/controllers/scalablenodegroup/v1alpha1/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,82 +56,70 @@ var _ = Describe("Examples", func() {
var ns *environment.Namespace
var desiredReplicas = ptr.Int32(5)
var dummyMessage = "test message"
var nodeGroupObject *fake.NodeGroup
var sng *v1alpha1.ScalableNodeGroup
var ng cloudprovider.NodeGroup
var defaultNodeGroupObject fake.NodeGroup

BeforeEach(func() {
var err error
sng = &v1alpha1.ScalableNodeGroup{}
ng = fakeController.CloudProvider.NodeGroupFor(&sng.Spec)
defaultNodeGroupObject = *ng.(*fake.NodeGroup)
ns, err = env.NewNamespace()
Expect(err).NotTo(HaveOccurred())
sng = &v1alpha1.ScalableNodeGroup{}
fakeCloudProvider.NodeGroupStable = true
fakeCloudProvider.NodeGroupMessage = ""
fakeCloudProvider.WantErr = nil
sng.Spec.Replicas = desiredReplicas
nodeGroupObject = ng.(*fake.NodeGroup)
})

AfterEach(func() {
nodeGroupObject.Stable = defaultNodeGroupObject.Stable
nodeGroupObject.Message = defaultNodeGroupObject.Message
nodeGroupObject.WantErr = defaultNodeGroupObject.WantErr
sng.Spec.Replicas = desiredReplicas
ng = fakeController.CloudProvider.NodeGroupFor(&sng.Spec)
sng.Spec.ID = "dummyID"
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = ptr.Int32(0)
})

Context("ScalableNodeGroup", func() {
It("should be created", func() {
Expect(ns.ParseResources("docs/examples/reserved-capacity-utilization.yaml", sng)).To(Succeed())
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = ptr.Int32(*sng.Spec.Replicas)
ExpectCreated(ns.Client, sng)
ExpectEventuallyHappy(ns.Client, sng)
ExpectDeleted(ns.Client, sng)
})
})

Context("Basic ScalableNodeGroup Reconcile tests", func() {
Context("ScalableNodeGroup Reconcile tests", func() {
It("Test reconciler to scale up nodes", func() {
Expect(fakeController.Reconcile(sng)).To(Succeed())
Expect(ng.GetReplicas()).To(Equal(*desiredReplicas))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(desiredReplicas))
})

It("Test reconciler to scale down nodes", func() {
Expect(ng.SetReplicas(10)).To(Succeed()) // set existing replicas higher than desired
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = ptr.Int32(10) // set existing replicas higher than desired
Expect(fakeController.Reconcile(sng)).To(Succeed())
Expect(ng.GetReplicas()).To(Equal(*desiredReplicas))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(desiredReplicas))
})

It("Test reconciler to make no change to node count", func() {
Expect(ng.SetReplicas(*desiredReplicas)).To(Succeed()) // set existing replicas equal to desired
fakeCloudProvider.NodeReplicas[sng.Spec.ID] = desiredReplicas // set existing replicas equal to desired
Expect(fakeController.Reconcile(sng)).To(Succeed())
Expect(ng.GetReplicas()).To(Equal(*desiredReplicas))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(desiredReplicas))
})
})

Context("Advanced ScalableNodeGroup Reconcile tests", func() {
It("Scale up nodes when not node group is stabilized and check status condition", func() {
Expect(fakeController.Reconcile(sng)).To(Succeed())
Expect(ng.GetReplicas()).To(Equal(*desiredReplicas))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(desiredReplicas))
Expect(sng.StatusConditions().GetCondition(v1alpha1.Stabilized).IsTrue()).To(Equal(true))
Expect(sng.StatusConditions().GetCondition(v1alpha1.Stabilized).Message).To(Equal(""))
})

It("Scale up nodes when not node group is NOT stabilized and check status condition", func() {
nodeGroupObject.Stable = false
nodeGroupObject.Message = dummyMessage
fakeCloudProvider.NodeGroupStable = false
fakeCloudProvider.NodeGroupMessage = dummyMessage
Expect(fakeController.Reconcile(sng)).To(Succeed())
Expect(ng.GetReplicas()).To(Equal(*desiredReplicas))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(desiredReplicas))
Expect(sng.StatusConditions().GetCondition(v1alpha1.Stabilized).IsFalse()).To(Equal(true))
Expect(sng.StatusConditions().GetCondition(v1alpha1.Stabilized).Message).To(Equal(dummyMessage))
})

It("Retryable error while reconciling", func() {
nodeGroupObject.WantErr = fake.RetryableError(fmt.Errorf(dummyMessage)) // retryable error
existingReplicas, _ := ng.GetReplicas()
fakeCloudProvider.WantErr = fake.RetryableError(fmt.Errorf(dummyMessage)) // retryable error
existingReplicas := fakeCloudProvider.NodeReplicas[sng.Spec.ID]
Expect(fakeController.Reconcile(sng)).To(Succeed())
replicas, err := ng.GetReplicas()
Expect(replicas).To(Equal(existingReplicas))
Expect(err).To(Equal(fake.RetryableError(fmt.Errorf(dummyMessage))))
Expect(fakeCloudProvider.NodeReplicas[sng.Spec.ID]).To(Equal(existingReplicas))
Expect(sng.StatusConditions().GetCondition(v1alpha1.AbleToScale).IsFalse()).To(Equal(true))
Expect(sng.StatusConditions().GetCondition(v1alpha1.AbleToScale).Message).To(Equal(dummyMessage))
})
Expand Down

0 comments on commit b277a7e

Please sign in to comment.