Introduce bundle and helper mock objects

These mock objects can be tuned according to a test
 case and then fed to a testable objects. For example,
 all the phase executors are supposed to consume document
 bundles and react respectively depending on the
 behavior of the bundle.

 Tests will be modified in later patches.

Change-Id: I7cd71752709d6d1f255d4bfbc641803479eed66e
Relates-To: #464
Relates-To: #465
This commit is contained in:
Vladimir Kozhukalov 2021-03-30 22:17:27 +03:00
parent 7998615a7b
commit d104e488f0
2 changed files with 319 additions and 0 deletions

161
testutil/document/bundle.go Normal file
View File

@ -0,0 +1,161 @@
/*
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
https://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 document
import (
"errors"
"io"
"github.com/stretchr/testify/mock"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/fs"
)
var _ document.Bundle = &MockBundle{}
// MockBundle mocks document.Bundle interface
type MockBundle struct {
mock.Mock
}
// Write mock
func (mb *MockBundle) Write(writer io.Writer) error {
args := mb.Called(writer)
return args.Error(0)
}
// SetFileSystem mock
func (mb *MockBundle) SetFileSystem(filesystem fs.FileSystem) error {
args := mb.Called(filesystem)
return args.Error(0)
}
// GetFileSystem mock
func (mb *MockBundle) GetFileSystem() fs.FileSystem {
args := mb.Called()
val, ok := args.Get(0).(fs.FileSystem)
if !ok {
return nil
}
return val
}
// Select mock
func (mb *MockBundle) Select(selector document.Selector) ([]document.Document, error) {
args := mb.Called(selector)
val, ok := args.Get(0).([]document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// SelectOne mock
func (mb *MockBundle) SelectOne(selector document.Selector) (document.Document, error) {
args := mb.Called(selector)
val, ok := args.Get(0).(document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// SelectBundle mock
func (mb *MockBundle) SelectBundle(selector document.Selector) (document.Bundle, error) {
args := mb.Called(selector)
val, ok := args.Get(0).(document.Bundle)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// SelectByFieldValue mock
func (mb *MockBundle) SelectByFieldValue(path string, condition func(interface{}) bool) (document.Bundle, error) {
args := mb.Called(path, condition)
val, ok := args.Get(0).(document.Bundle)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// GetByGvk mock
func (mb *MockBundle) GetByGvk(group, version, kind string) ([]document.Document, error) {
args := mb.Called(group, version, kind)
val, ok := args.Get(0).([]document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// GetByName mock
func (mb *MockBundle) GetByName(name string) (document.Document, error) {
args := mb.Called(name)
val, ok := args.Get(0).(document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// GetByAnnotation mock
func (mb *MockBundle) GetByAnnotation(annotationSelector string) ([]document.Document, error) {
args := mb.Called(annotationSelector)
val, ok := args.Get(0).([]document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// GetByLabel mock
func (mb *MockBundle) GetByLabel(labelSelector string) ([]document.Document, error) {
args := mb.Called(labelSelector)
val, ok := args.Get(0).([]document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// GetAllDocuments mock
func (mb *MockBundle) GetAllDocuments() ([]document.Document, error) {
args := mb.Called()
val, ok := args.Get(0).([]document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// Append mock
func (mb *MockBundle) Append(doc document.Document) error {
args := mb.Called(doc)
return args.Error(0)
}
var (
// EmptyBundleFactory returns empty MockBundle
EmptyBundleFactory document.BundleFactoryFunc = func() (document.Bundle, error) {
return &MockBundle{}, nil
}
// ErrorBundleFactory returns error instead of bundle
ErrorBundleFactory document.BundleFactoryFunc = func() (document.Bundle, error) {
return nil, errors.New("error")
}
)

158
testutil/phase/helper.go Normal file
View File

@ -0,0 +1,158 @@
/*
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
https://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 phase
import (
"github.com/stretchr/testify/mock"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/cluster/clustermap"
"opendev.org/airship/airshipctl/pkg/document"
inventoryifc "opendev.org/airship/airshipctl/pkg/inventory/ifc"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
)
var _ ifc.Helper = &MockHelper{}
// MockHelper mock ifc.Helper interface
type MockHelper struct {
mock.Mock
}
// TargetPath mock
func (mh *MockHelper) TargetPath() string {
args := mh.Called()
return args.Get(0).(string)
}
// PhaseRepoDir mock
func (mh *MockHelper) PhaseRepoDir() string {
args := mh.Called()
return args.Get(0).(string)
}
// DocEntryPointPrefix mock
func (mh *MockHelper) DocEntryPointPrefix() string {
args := mh.Called()
return args.Get(0).(string)
}
// WorkDir mock
func (mh *MockHelper) WorkDir() string {
args := mh.Called()
return args.Get(0).(string)
}
// Phase mock
func (mh *MockHelper) Phase(id ifc.ID) (*v1alpha1.Phase, error) {
args := mh.Called(id)
val, ok := args.Get(0).(*v1alpha1.Phase)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// Plan mock
func (mh *MockHelper) Plan(id ifc.ID) (*v1alpha1.PhasePlan, error) {
args := mh.Called(id)
val, ok := args.Get(0).(*v1alpha1.PhasePlan)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// ListPhases mock
func (mh *MockHelper) ListPhases(o ifc.ListPhaseOptions) ([]*v1alpha1.Phase, error) {
args := mh.Called(o)
val, ok := args.Get(0).([]*v1alpha1.Phase)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// ListPlans mock
func (mh *MockHelper) ListPlans() ([]*v1alpha1.PhasePlan, error) {
args := mh.Called()
val, ok := args.Get(0).([]*v1alpha1.PhasePlan)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// ClusterMapAPIobj mock
func (mh *MockHelper) ClusterMapAPIobj() (*v1alpha1.ClusterMap, error) {
args := mh.Called()
val, ok := args.Get(0).(*v1alpha1.ClusterMap)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// ClusterMap mock
func (mh *MockHelper) ClusterMap() (clustermap.ClusterMap, error) {
args := mh.Called()
val, ok := args.Get(0).(clustermap.ClusterMap)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// ExecutorDoc mock
func (mh *MockHelper) ExecutorDoc(phaseID ifc.ID) (document.Document, error) {
args := mh.Called()
val, ok := args.Get(0).(document.Document)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// PhaseBundleRoot mock
func (mh *MockHelper) PhaseBundleRoot() string {
args := mh.Called()
return args.Get(0).(string)
}
// Inventory mock
func (mh *MockHelper) Inventory() (inventoryifc.Inventory, error) {
args := mh.Called()
val, ok := args.Get(0).(inventoryifc.Inventory)
if !ok {
return nil, args.Error(1)
}
return val, args.Error(1)
}
// PhaseEntryPointBasePath mock
func (mh *MockHelper) PhaseEntryPointBasePath() string {
args := mh.Called()
return args.Get(0).(string)
}
// PhaseConfigBundle mock
func (mh *MockHelper) PhaseConfigBundle() document.Bundle {
args := mh.Called()
val, ok := args.Get(0).(document.Bundle)
if !ok {
return nil
}
return val
}