Skip to content

Commit

Permalink
Move tests into transitioner package
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentportella committed Aug 12, 2024
1 parent 0c3a826 commit 9435eef
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
17 changes: 0 additions & 17 deletions pkg/controller/cyclenoderequest/transitioner/fake/options.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package faketransitioner
package transitioner

import (
"net/http"

v1 "github.com/atlassian-labs/cyclops/pkg/apis/atlassian/v1"
"github.com/atlassian-labs/cyclops/pkg/controller"
"github.com/atlassian-labs/cyclops/pkg/controller/cyclenoderequest/transitioner"
"github.com/atlassian-labs/cyclops/pkg/mock"
)

type Option func(t *Transitioner)

func WithCloudProviderInstances(nodes []*mock.Node) Option {
return func(t *Transitioner) {
t.cloudProviderInstances = append(t.cloudProviderInstances, nodes...)
}
}

func WithKubeNodes(nodes []*mock.Node) Option {
return func(t *Transitioner) {
t.kubeNodes = append(t.kubeNodes, nodes...)
}
}

// ************************************************************************** //

type Transitioner struct {
*transitioner.CycleNodeRequestTransitioner
*CycleNodeRequestTransitioner
*mock.Client

cloudProviderInstances []*mock.Node
Expand Down Expand Up @@ -38,8 +53,8 @@ func NewFakeTransitioner(cnr *v1.CycleNodeRequest, opts ...Option) *Transitioner
CloudProvider: t.CloudProvider,
}

t.CycleNodeRequestTransitioner = transitioner.NewCycleNodeRequestTransitioner(
cnr, rm, transitioner.Options{},
t.CycleNodeRequestTransitioner = NewCycleNodeRequestTransitioner(
cnr, rm, Options{},
)

return t
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package faketransitioner
package transitioner

import (
"testing"
Expand Down Expand Up @@ -59,6 +59,55 @@ func TestPendingSimpleCase(t *testing.T) {
assert.Equal(t, cnr.Status.NumNodesCycled, 0)
}

// Test to ensure the Pending phase will accept a CNR with a correct named node.
func TestPendingWithNamedNode(t *testing.T) {
nodegroup, err := mock.NewNodegroup("ng-1", 2)
if err != nil {
assert.NoError(t, err)
}

cnr := &v1.CycleNodeRequest{
ObjectMeta: metav1.ObjectMeta{
Name: "cnr-1",
Namespace: "kube-system",
},
Spec: v1.CycleNodeRequestSpec{
NodeGroupsList: []string{"ng-1"},
CycleSettings: v1.CycleSettings{
Concurrency: 1,
Method: v1.CycleNodeRequestMethodDrain,
},
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"customer": "kitt",
},
},
NodeNames: []string{
"ng-1-node-0",
},
},
Status: v1.CycleNodeRequestStatus{
Phase: v1.CycleNodeRequestPending,
},
}

fakeTransitioner := NewFakeTransitioner(cnr,
WithKubeNodes(nodegroup),
WithCloudProviderInstances(nodegroup),
)

result, err := fakeTransitioner.Run()
assert.NoError(t, err)
assert.True(t, result.Requeue)

// It should move to the Initialised phase and set up the status of the CNR
// in a predictable manner
assert.Equal(t, v1.CycleNodeRequestInitialised, cnr.Status.Phase)
assert.Len(t, cnr.Status.NodesToTerminate, 1)
assert.Equal(t, cnr.Status.ActiveChildren, int64(0))
assert.Equal(t, cnr.Status.NumNodesCycled, 0)
}

// Test to ensure the Pending phase will reject a CNR with a named node that
// does not match any of the nodes matching the node selector. It should error
// out immediately.
Expand Down Expand Up @@ -228,9 +277,9 @@ func TestPendingDetachedCloudProviderNode(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, v1.CycleNodeRequestPending, cnr.Status.Phase)

// Simulate waiting for 1 day, this is more than the waiting limit
// Simulate waiting for 1s more than the wait limit
cnr.Status.EquilibriumWaitStarted = &metav1.Time{
Time: time.Now().Add(-24 * time.Hour),
Time: time.Now().Add(-nodeEquilibriumWaitLimit - time.Second),
}

// This time should transition to the healing phase
Expand Down
File renamed without changes.

0 comments on commit 9435eef

Please sign in to comment.