-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin/clientmgmt refactoring for BackupItemAction v1
Refactors the clientmgmt package to implement the plugin versioning changes needed for BIA v1 and overall package refactoring to support plugin versions in different packages. This should be all that's needed to move on to v2 for BackupItemAction. The remaining plugin types still need similar refactoring to what's being done here for BIA before attempting a v2 implementation. Signed-off-by: Scott Seago <sseago@redhat.com>
- Loading branch information
Showing
28 changed files
with
466 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
plugin/clientmgmt refactoring for BackupItemAction v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
pkg/plugin/clientmgmt/backupitemaction/v1/shared_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Copyright 2018 the Velero contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package v1 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process" | ||
"github.com/vmware-tanzu/velero/pkg/plugin/framework" | ||
) | ||
|
||
type mockRestartableProcess struct { | ||
mock.Mock | ||
} | ||
|
||
func (rp *mockRestartableProcess) AddReinitializer(key process.KindAndName, r process.Reinitializer) { | ||
rp.Called(key, r) | ||
} | ||
|
||
func (rp *mockRestartableProcess) Reset() error { | ||
args := rp.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (rp *mockRestartableProcess) ResetIfNeeded() error { | ||
args := rp.Called() | ||
return args.Error(0) | ||
} | ||
|
||
func (rp *mockRestartableProcess) GetByKindAndName(key process.KindAndName) (interface{}, error) { | ||
args := rp.Called(key) | ||
return args.Get(0), args.Error(1) | ||
} | ||
|
||
func (rp *mockRestartableProcess) Stop() { | ||
rp.Called() | ||
} | ||
|
||
type restartableDelegateTest struct { | ||
function string | ||
inputs []interface{} | ||
expectedErrorOutputs []interface{} | ||
expectedDelegateOutputs []interface{} | ||
} | ||
|
||
type mockable interface { | ||
Test(t mock.TestingT) | ||
On(method string, args ...interface{}) *mock.Call | ||
AssertExpectations(t mock.TestingT) bool | ||
} | ||
|
||
func runRestartableDelegateTests( | ||
t *testing.T, | ||
kind framework.PluginKind, | ||
newRestartable func(key process.KindAndName, p process.RestartableProcess) interface{}, | ||
newMock func() mockable, | ||
tests ...restartableDelegateTest, | ||
) { | ||
for _, tc := range tests { | ||
t.Run(tc.function, func(t *testing.T) { | ||
p := new(mockRestartableProcess) | ||
p.Test(t) | ||
defer p.AssertExpectations(t) | ||
|
||
// getDelegate error | ||
p.On("ResetIfNeeded").Return(errors.Errorf("reset error")).Once() | ||
name := "delegateName" | ||
key := process.KindAndName{Kind: kind, Name: name} | ||
r := newRestartable(key, p) | ||
|
||
// Get the method we're going to call using reflection | ||
method := reflect.ValueOf(r).MethodByName(tc.function) | ||
require.NotEmpty(t, method) | ||
|
||
// Convert the test case inputs ([]interface{}) to []reflect.Value | ||
var inputValues []reflect.Value | ||
for i := range tc.inputs { | ||
inputValues = append(inputValues, reflect.ValueOf(tc.inputs[i])) | ||
} | ||
|
||
// Invoke the method being tested | ||
actual := method.Call(inputValues) | ||
|
||
// This function asserts that the actual outputs match the expected outputs | ||
checkOutputs := func(expected []interface{}, actual []reflect.Value) { | ||
require.Equal(t, len(expected), len(actual)) | ||
|
||
for i := range actual { | ||
// Get the underlying value from the reflect.Value | ||
a := actual[i].Interface() | ||
|
||
// Check if it's an error | ||
actualErr, actualErrOk := a.(error) | ||
// Check if the expected output element is an error | ||
expectedErr, expectedErrOk := expected[i].(error) | ||
// If both are errors, use EqualError | ||
if actualErrOk && expectedErrOk { | ||
assert.EqualError(t, actualErr, expectedErr.Error()) | ||
continue | ||
} | ||
|
||
// If function returns nil as struct return type, we cannot just | ||
// compare the interface to nil as its type will not be nil, | ||
// only the value will be | ||
if expected[i] == nil && reflect.ValueOf(a).Kind() == reflect.Ptr { | ||
assert.True(t, reflect.ValueOf(a).IsNil()) | ||
continue | ||
} | ||
|
||
// Otherwise, use plain Equal | ||
assert.Equal(t, expected[i], a) | ||
} | ||
} | ||
|
||
// Make sure we get what we expected when getDelegate returned an error | ||
checkOutputs(tc.expectedErrorOutputs, actual) | ||
|
||
// Invoke delegate, make sure all returned values are passed through | ||
p.On("ResetIfNeeded").Return(nil) | ||
|
||
delegate := newMock() | ||
delegate.Test(t) | ||
defer delegate.AssertExpectations(t) | ||
|
||
p.On("GetByKindAndName", key).Return(delegate, nil) | ||
|
||
// Set up the mocked method in the delegate | ||
delegate.On(tc.function, tc.inputs...).Return(tc.expectedDelegateOutputs...) | ||
|
||
// Invoke the method being tested | ||
actual = method.Call(inputValues) | ||
|
||
// Make sure we get what we expected when invoking the delegate | ||
checkOutputs(tc.expectedDelegateOutputs, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.