-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Added unit tests for Instance.go (#74)
Added tests for Get() and FromAgentPoolToInstance()
- Loading branch information
1 parent
e504aac
commit 78cd7d1
Showing
23 changed files
with
5,455 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package fake | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"github.com/stretchr/testify/mock" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
k8sClient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Client is a mock for the controller-runtime dynamic client interface. | ||
type MockClient struct { | ||
mock.Mock | ||
|
||
ObjectMap map[reflect.Type]map[k8sClient.ObjectKey]k8sClient.Object | ||
StatusMock *MockStatusClient | ||
UpdateCb func(key types.NamespacedName) | ||
} | ||
|
||
var _ k8sClient.Client = &MockClient{} | ||
|
||
func NewClient() *MockClient { | ||
return &MockClient{ | ||
StatusMock: &MockStatusClient{}, | ||
ObjectMap: map[reflect.Type]map[k8sClient.ObjectKey]k8sClient.Object{}, | ||
} | ||
} | ||
|
||
// Retrieves or creates a map associated with the type of obj | ||
func (m *MockClient) ensureMapForType(t reflect.Type) map[k8sClient.ObjectKey]k8sClient.Object { | ||
if _, ok := m.ObjectMap[t]; !ok { | ||
//create a new map with the object key if it doesn't exist | ||
m.ObjectMap[t] = map[k8sClient.ObjectKey]k8sClient.Object{} | ||
} | ||
return m.ObjectMap[t] | ||
} | ||
|
||
func (m *MockClient) CreateMapWithType(t interface{}) map[k8sClient.ObjectKey]k8sClient.Object { | ||
objType := reflect.TypeOf(t) | ||
|
||
return m.ensureMapForType(objType) | ||
} | ||
|
||
func (m *MockClient) CreateOrUpdateObjectInMap(obj k8sClient.Object) { | ||
t := reflect.TypeOf(obj) | ||
relevantMap := m.ensureMapForType(t) | ||
objKey := k8sClient.ObjectKeyFromObject(obj) | ||
|
||
relevantMap[objKey] = obj | ||
} | ||
|
||
func (m *MockClient) GetObjectFromMap(obj k8sClient.Object, key types.NamespacedName) { | ||
t := reflect.TypeOf(obj) | ||
relevantMap := m.ensureMapForType(t) | ||
|
||
if val, ok := relevantMap[key]; ok { | ||
v := reflect.ValueOf(obj).Elem() | ||
v.Set(reflect.ValueOf(val).Elem()) | ||
} | ||
} | ||
|
||
// k8s Client interface | ||
func (m *MockClient) Get(ctx context.Context, key types.NamespacedName, obj k8sClient.Object, opts ...k8sClient.GetOption) error { | ||
//make any necessary changes to the object | ||
if m.UpdateCb != nil { | ||
m.UpdateCb(key) | ||
} | ||
|
||
m.GetObjectFromMap(obj, key) | ||
|
||
args := m.Called(ctx, key, obj, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) List(ctx context.Context, list k8sClient.ObjectList, opts ...k8sClient.ListOption) error { | ||
|
||
v := reflect.ValueOf(list).Elem() | ||
newList := m.getObjectListFromMap(list) | ||
v.Set(reflect.ValueOf(newList).Elem()) | ||
|
||
args := m.Called(ctx, list, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) getObjectListFromMap(list k8sClient.ObjectList) k8sClient.ObjectList { | ||
objType := reflect.TypeOf(list) | ||
relevantMap := m.ensureMapForType(objType) | ||
|
||
switch list.(type) { | ||
case *corev1.NodeList: | ||
nodeList := &corev1.NodeList{} | ||
for _, obj := range relevantMap { | ||
if node, ok := obj.(*corev1.Node); ok { | ||
nodeList.Items = append(nodeList.Items, *node) | ||
} | ||
} | ||
return nodeList | ||
} | ||
//add additional object lists as needed | ||
return nil | ||
} | ||
|
||
func (m *MockClient) Create(ctx context.Context, obj k8sClient.Object, opts ...k8sClient.CreateOption) error { | ||
m.CreateOrUpdateObjectInMap(obj) | ||
|
||
args := m.Called(ctx, obj, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) Delete(ctx context.Context, obj k8sClient.Object, opts ...k8sClient.DeleteOption) error { | ||
args := m.Called(ctx, obj, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) Update(ctx context.Context, obj k8sClient.Object, opts ...k8sClient.UpdateOption) error { | ||
args := m.Called(ctx, obj, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) Patch(ctx context.Context, obj k8sClient.Object, patch k8sClient.Patch, opts ...k8sClient.PatchOption) error { | ||
args := m.Called(ctx, obj, patch, opts) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *MockClient) DeleteAllOf(ctx context.Context, obj k8sClient.Object, opts ...k8sClient.DeleteAllOfOption) error { | ||
args := m.Called(ctx, obj, opts) | ||
return args.Error(0) | ||
} | ||
|
||
// GroupVersionKindFor implements client.Client | ||
func (m *MockClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
// IsObjectNamespaced implements client.Client | ||
func (m *MockClient) IsObjectNamespaced(obj runtime.Object) (bool, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
func (m *MockClient) Scheme() *runtime.Scheme { | ||
args := m.Called() | ||
return args.Get(0).(*runtime.Scheme) | ||
} | ||
|
||
func (m *MockClient) RESTMapper() meta.RESTMapper { | ||
args := m.Called() | ||
return args.Get(0).(meta.RESTMapper) | ||
} | ||
|
||
type MockStatusClient struct { | ||
mock.Mock | ||
} | ||
|
||
// Patch implements client.StatusWriter | ||
func (*MockStatusClient) Patch(ctx context.Context, obj k8sClient.Object, patch k8sClient.Patch, opts ...k8sClient.PatchOption) error { | ||
panic("unimplemented") | ||
} | ||
|
||
// Update implements client.StatusWriter | ||
func (*MockStatusClient) Update(ctx context.Context, obj k8sClient.Object, opts ...k8sClient.UpdateOption) error { | ||
panic("unimplemented") | ||
} | ||
|
||
// StatusClient interface | ||
|
||
func (m *MockClient) Status() k8sClient.StatusWriter { | ||
return m.StatusMock | ||
} |
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
Oops, something went wrong.