Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1874618: fix unit tests so we can add a CI job #111

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/metal3-io/baremetal-operator v0.0.0
github.com/onsi/gomega v1.9.0
github.com/openshift/machine-api-operator v0.2.1-0.20200721125631-d234cceb5de1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5
k8s.io/api v0.18.2
Expand Down
7 changes: 6 additions & 1 deletion pkg/cloud/baremetal/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
bmv1alpha1 "github.com/openshift/cluster-api-provider-baremetal/pkg/apis/baremetal/v1alpha1"
machinev1beta1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
machineapierrors "github.com/openshift/machine-api-operator/pkg/controller/machine"
gherrors "github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -567,7 +568,11 @@ func (a *Actuator) ensureAnnotation(ctx context.Context, machine *machinev1beta1
log.Printf("setting host annotation for %v to %v=%q", machine.Name, HostAnnotation, hostKey)
annotations[HostAnnotation] = hostKey
machine.ObjectMeta.SetAnnotations(annotations)
return true, a.client.Update(ctx, machine)
err = a.client.Update(ctx, machine)
if err != nil {
return false, gherrors.Wrap(err, "failed to update machine annotation")
}
return true, nil
}

// clearAnnotation makes sure the machine's host annotation is empty.
Expand Down
86 changes: 50 additions & 36 deletions pkg/cloud/baremetal/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,13 +879,16 @@ func TestEnsureAnnotation(t *testing.T) {
machinev1beta1.AddToScheme(scheme)

testCases := []struct {
Machine machinev1beta1.Machine
Host bmh.BareMetalHost
Scenario string
Machine machinev1beta1.Machine
Host bmh.BareMetalHost
}{
{
// annotation exists and is correct
Scenario: "annotation exists and is correct",
Machine: machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "mymachine",
Namespace: "myns",
Annotations: map[string]string{
HostAnnotation: "myns/myhost",
},
Expand All @@ -899,9 +902,11 @@ func TestEnsureAnnotation(t *testing.T) {
},
},
{
// annotation exists but is wrong
Scenario: "annotation exists but is wrong",
Machine: machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "mymachine",
Namespace: "myns",
Annotations: map[string]string{
HostAnnotation: "myns/wrongvalue",
},
Expand All @@ -915,9 +920,11 @@ func TestEnsureAnnotation(t *testing.T) {
},
},
{
// annotations are empty
Scenario: "annotations are empty",
Machine: machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "mymachine",
Namespace: "myns",
Annotations: map[string]string{},
},
},
Expand All @@ -929,8 +936,13 @@ func TestEnsureAnnotation(t *testing.T) {
},
},
{
// annotations are nil
Machine: machinev1beta1.Machine{},
Scenario: "annotations are nil",
Machine: machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "mymachine",
Namespace: "myns",
},
},
Host: bmh.BareMetalHost{
ObjectMeta: metav1.ObjectMeta{
Name: "myhost",
Expand All @@ -941,37 +953,39 @@ func TestEnsureAnnotation(t *testing.T) {
}

for _, tc := range testCases {
c := fakeclient.NewFakeClientWithScheme(scheme, &tc.Machine)
actuator, err := NewActuator(ActuatorParams{
Client: c,
})
if err != nil {
t.Error(err)
}
t.Run(tc.Scenario, func(t *testing.T) {
c := fakeclient.NewFakeClientWithScheme(scheme, &tc.Machine)
actuator, err := NewActuator(ActuatorParams{
Client: c,
})
if err != nil {
t.Error(err)
}

_, err = actuator.ensureAnnotation(context.TODO(), &tc.Machine, &tc.Host)
if err != nil {
t.Errorf("unexpected error %v", err)
}
_, err = actuator.ensureAnnotation(context.TODO(), &tc.Machine, &tc.Host)
if err != nil {
t.Errorf("unexpected error %v", err)
}

// get the machine and make sure it has the correct annotation
machine := machinev1beta1.Machine{}
key := client.ObjectKey{
Name: tc.Machine.Name,
Namespace: tc.Machine.Namespace,
}
err = c.Get(context.TODO(), key, &machine)
annotations := machine.ObjectMeta.GetAnnotations()
if annotations == nil {
t.Error("no annotations found")
}
result, ok := annotations[HostAnnotation]
if !ok {
t.Error("host annotation not found")
}
if result != "myns/myhost" {
t.Errorf("host annotation has value %s, expected \"myns/myhost\"", result)
}
// get the machine and make sure it has the correct annotation
machine := machinev1beta1.Machine{}
key := client.ObjectKey{
Name: tc.Machine.Name,
Namespace: tc.Machine.Namespace,
}
err = c.Get(context.TODO(), key, &machine)
annotations := machine.ObjectMeta.GetAnnotations()
if annotations == nil {
t.Error("no annotations found")
}
result, ok := annotations[HostAnnotation]
if !ok {
t.Error("host annotation not found")
}
if result != "myns/myhost" {
t.Errorf("host annotation has value %s, expected \"myns/myhost\"", result)
}
})
}
}

Expand Down