Skip to content

Commit

Permalink
Merge pull request #196 from texierp/v1
Browse files Browse the repository at this point in the history
activeinactive: implement 'SetValidate' function
  • Loading branch information
otavio committed Mar 26, 2018
2 parents 3046230 + 88fc3b1 commit f75e97e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
16 changes: 16 additions & 0 deletions activeinactive/activeinactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type Interface interface {
Active() (int, error)
SetActive(active int) error
SetValidate() error
}

// DefaultImpl is the default implementation for Interface
Expand Down Expand Up @@ -64,3 +65,18 @@ func (i *DefaultImpl) SetActive(active int) error {

return nil
}

// SetValidate validate the current update
// by calling 'updatehub-active-validated'
func (i *DefaultImpl) SetValidate() error {
log.Debug("Running 'updatehub-active-validated'")

_, err := i.Execute(fmt.Sprintf("updatehub-active-validated"))
if err != nil {
finalErr := fmt.Errorf("failed to execute 'updatehub-active-validated': %s", err)
log.Error(finalErr)
return finalErr
}

return nil
}
5 changes: 5 additions & 0 deletions testsmocks/activeinactivemock/activeinactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ func (aim *ActiveInactiveMock) SetActive(active int) error {
args := aim.Called(active)
return args.Error(0)
}

func (aim *ActiveInactiveMock) SetValidate() error {
args := aim.Called()
return args.Error(0)
}
13 changes: 13 additions & 0 deletions testsmocks/activeinactivemock/activeinactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ func TestDownloadUpdate(t *testing.T) {

aim.AssertExpectations(t)
}

func TestValidateUpdate(t *testing.T) {
expectedError := fmt.Errorf("some error")

aim := &ActiveInactiveMock{}
aim.On("SetValidate").Return(expectedError)

err := aim.SetValidate()

assert.Equal(t, expectedError, err)

aim.AssertExpectations(t)
}
13 changes: 12 additions & 1 deletion updatehub/updatehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,10 @@ func (uh *UpdateHub) rollbackProcedure() error {
}

func (uh *UpdateHub) validateProcedure() error {
aii := uh.ActiveInactiveBackend

err := uh.validateCallback()
if err != nil {
aii := uh.ActiveInactiveBackend

active, activeErr := aii.Active()
if activeErr != nil {
Expand All @@ -643,16 +644,26 @@ func (uh *UpdateHub) validateProcedure() error {

newActive := (active - 1) * -1

// Switch the active partion
setActiveErr := aii.SetActive(newActive)
if setActiveErr != nil {
return setActiveErr
}

// Force reboot
uh.Rebooter.Reboot()

return err
}

// We can Validate the update by calling
// 'updatehub-active-validated', and then go
// back to the state machine.
err = aii.SetValidate()
if err != nil {
return err
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions updatehub/updatehub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,8 @@ func TestStartWithSuccessfulInstallationValidation(t *testing.T) {
aim := &activeinactivemock.ActiveInactiveMock{}
aim.On("Active").Return(0, nil).Once()

aim.On("SetValidate").Return(nil)

uh, _ := newTestUpdateHub(nil, aim)

uh.Settings.ProbeASAP = true
Expand Down Expand Up @@ -2213,6 +2215,8 @@ func newTestUpdateHub(state State, aii activeinactive.Interface) (*UpdateHub, er
func TestValidateProcedureWithNonExistantCallback(t *testing.T) {
aim := &activeinactivemock.ActiveInactiveMock{}

aim.On("SetValidate").Return(nil)

uh, err := newTestUpdateHub(nil, aim)
assert.NoError(t, err)

Expand All @@ -2234,6 +2238,8 @@ func TestValidateProcedureWithNonExistantCallback(t *testing.T) {
func TestValidateProcedureWithCallbackSuccess(t *testing.T) {
aim := &activeinactivemock.ActiveInactiveMock{}

aim.On("SetValidate").Return(nil)

uh, err := newTestUpdateHub(nil, aim)
assert.NoError(t, err)

Expand Down

0 comments on commit f75e97e

Please sign in to comment.