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

Add tests for actuator events #107

Merged
merged 6 commits into from Aug 5, 2020
Merged

Add tests for actuator events #107

merged 6 commits into from Aug 5, 2020

Conversation

alexander-demicev
Copy link
Contributor

This PR adds tests for actuator events, but this required couple of other changes.

  1. Pass context to machine scope
  2. Improve error formatting and make it consistent with other providers
  3. Add GCP client builder, similar to what we have in AWS. This allows mocking the client in a cleaner way.
  4. Validate machine labels in update()

https://github.com/openshift/machine-api-operator/blob/master/pkg/controller/vsphere/actuator_test.go
https://github.com/openshift/cluster-api-provider-aws/blob/master/pkg/actuators/machine/actuator_test.go

doneMgr := make(chan struct{})

go func() {
g.Expect(mgr.Start(doneMgr)).To(Succeed())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel should be closed when the test exits, I don't think there's any issues with the manager not starting right here, apart from the fact we can't detect whether it has started/stopped. If you want to be thorough I would suggest the use of a waitgroup to ensure the goroutine finishes before the tests end

klog.Errorf("Failed to patch machine %q: %v", s.machine.GetName(), err)
return err
}

s.machine.Status = statusCopy

// patch status
if err := s.coreClient.Status().Patch(context.Background(), s.machine, s.machineToBePatched); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simply pass s as a first argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd leave s.Context for better readability.

@@ -45,6 +49,10 @@ type machineScope struct {
// newMachineScope creates a new MachineScope from the supplied parameters.
// This is meant to be called for each machine actuator operation.
func newMachineScope(params machineScopeParams) (*machineScope, error) {
if params.Context == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should default to context.Background here, as I can't yet see real uses for other context values

eventsChannel := make(chan string, 1)
recorder := &record.FakeRecorder{
Events: eventsChannel,
func TestMachineEvents(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should be called TestActuatorScopeEvents or just TestActuatorEvents

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -61,12 +62,14 @@ func (a *Actuator) Create(ctx context.Context, machine *machinev1.Machine) error
machine: machine,
})
if err != nil {
return a.handleMachineError(machine, err, createEventAction)
fmtErr := fmt.Errorf(scopeFailFmt, machine.GetName(), err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this shadow the error type breaking consumers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Go 1.13 it shouldn't, https://blog.golang.org/go1.13-errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consumers will need to make sure that they use errors.Is or errors.As from the errors standard library if they want to check the type, I believe we are already doing this in MAO but worth checking if there's anywhere else expecting a particular error type

@@ -168,6 +168,10 @@ func (r *Reconciler) create() error {
}

func (r *Reconciler) update() error {
if err := validateMachine(*r.machine, *r.providerSpec); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale behind introducing this in the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this validation in other providers, and tests for actuator events cover this case. I'd like to keep these tests consistent among all providers.

@@ -61,12 +62,14 @@ func (a *Actuator) Create(ctx context.Context, machine *machinev1.Machine) error
machine: machine,
})
if err != nil {
return a.handleMachineError(machine, err, createEventAction)
fmtErr := fmt.Errorf(scopeFailFmt, machine.GetName(), err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consumers will need to make sure that they use errors.Is or errors.As from the errors standard library if they want to check the type, I believe we are already doing this in MAO but worth checking if there's anywhere else expecting a particular error type

doneMgr := make(chan struct{})

go func() {
g.Expect(mgr.Start(doneMgr)).To(Succeed())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel should be closed when the test exits, I don't think there's any issues with the manager not starting right here, apart from the fact we can't detect whether it has started/stopped. If you want to be thorough I would suggest the use of a waitgroup to ensure the goroutine finishes before the tests end

@@ -18,12 +17,17 @@ import (

// machineScopeParams defines the input parameters used to create a new MachineScope.
type machineScopeParams struct {
coreClient controllerclient.Client
machine *machinev1.Machine
context.Context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, why embed the context rather than have it as an explicit field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// machineScope defines a scope defined around a machine and its cluster.
type machineScope struct {
context.Context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, is there a benefit to embedding?

@JoelSpeed
Copy link
Contributor

/approve
/retest

@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JoelSpeed

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 31, 2020
@enxebre
Copy link
Member

enxebre commented Aug 3, 2020

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2020
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

10 similar comments
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

20 similar comments
@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit d30db1b into openshift:master Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants