Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanth26 committed Jan 29, 2020
1 parent 85ac25c commit 6a658ee
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
69 changes: 63 additions & 6 deletions pkg/controller/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"math"
"net/http"
"time"

machineapi "github.com/gardener/machine-controller-manager/pkg/apis/machine"
Expand All @@ -33,8 +34,10 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
k8stesting "k8s.io/client-go/testing"
)
Expand Down Expand Up @@ -514,14 +517,12 @@ var _ = Describe("machine", func() {
func() (string, string, error) {
return action.fakeProviderID, action.fakeNodeName, action.fakeError
},
nil, nil))
func(string) error {
return action.fakeError
}, nil))

if data.expect.err {
Expect(err).To(HaveOccurred())
actual, err := controller.controlMachineClient.Machines(machine.Namespace).Get(machine.Name, metav1.GetOptions{})
Expect(err).To(BeNil())
Expect(actual.Status.LastOperation.Description).To(Equal(data.expect.machine.Status.LastOperation.Description))
Expect(actual.Status.CurrentStatus.Phase).To(Equal(data.expect.machine.Status.CurrentStatus.Phase))
return
}

Expand Down Expand Up @@ -657,7 +658,15 @@ var _ = Describe("machine", func() {
}, nil, nil, nil, nil),
fakeResourceActions: &customfake.ResourceActions{
Machine: customfake.Actions{
Get: "Failed to GET machine",
Get: apierrors.NewGenericServerResponse(
http.StatusBadRequest,
"dummy method",
schema.GroupResource{},
"dummy name",
"Failed to GET machine",
30,
true,
),
},
},
},
Expand All @@ -684,6 +693,54 @@ var _ = Describe("machine", func() {
err: false,
},
}),
Entry("Orphan VM deletion on faling to find referred machine object", &data{
setup: setup{
secrets: []*corev1.Secret{
{
ObjectMeta: *newObjectMeta(objMeta, 0),
},
},
aws: []*machinev1.AWSMachineClass{
{
ObjectMeta: *newObjectMeta(objMeta, 0),
Spec: machinev1.AWSMachineClassSpec{
SecretRef: newSecretReference(objMeta, 0),
},
},
},
machines: newMachines(1, &machinev1.MachineTemplateSpec{
ObjectMeta: *newObjectMeta(objMeta, 0),
Spec: machinev1.MachineSpec{
Class: machinev1.ClassSpec{
Kind: "AWSMachineClass",
Name: "machine-0",
},
},
}, nil, nil, nil, nil),
fakeResourceActions: &customfake.ResourceActions{
Machine: customfake.Actions{
Get: apierrors.NewGenericServerResponse(
http.StatusNotFound,
"dummy method",
schema.GroupResource{},
"dummy name",
"Machine not found",
30,
true,
),
},
},
},
action: action{
machine: "machine-0",
fakeProviderID: "fakeID-0",
fakeNodeName: "fakeNode-0",
fakeError: nil,
},
expect: expect{
err: true,
},
}),
)
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/fakeclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (t *FakeObjectTracker) Get(gvr schema.GroupVersionResource, ns, name string
}

if gvr.Resource == "machines" {
if t.fakingOptions.failAt.Machine.Get != "" {
return nil, errors.New(t.fakingOptions.failAt.Machine.Get)
if t.fakingOptions.failAt.Machine.Get != nil {
return nil, t.fakingOptions.failAt.Machine.Get
}
}

Expand Down Expand Up @@ -357,7 +357,7 @@ type ResourceActions struct {
// Actions contains the actions whose response can be faked
type Actions struct {
Create string
Get string
Get error
Delete string
Update string
}
Expand Down

0 comments on commit 6a658ee

Please sign in to comment.