-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added more comments - Updated some tests
- Loading branch information
Showing
6 changed files
with
132 additions
and
92 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
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 |
---|---|---|
@@ -1,84 +1,95 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
"encoding/json" | ||
"fmt" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic/dynamicinformer" | ||
fakeDynamic "k8s.io/client-go/dynamic/fake" | ||
"k8s.io/client-go/tools/cache" | ||
"log" | ||
"sigs.k8s.io/yaml" | ||
"testing" | ||
) | ||
|
||
func createFakeResourceInformer(gvr schema.GroupVersionResource) cache.SharedIndexInformer { | ||
fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(runtime.NewScheme()) | ||
func createFakeResourceInformer(gvr schema.GroupVersionResource, fakeDynamicClient *fakeDynamic.FakeDynamicClient) (fakeResourceInformer cache.SharedIndexInformer) { | ||
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(fakeDynamicClient, 0, corev1.NamespaceAll, nil) | ||
fakeResourceInformer := factory.ForResource(gvr).Informer() | ||
fakeResourceInformer = factory.ForResource(gvr).Informer() | ||
if fakeResourceInformer == nil { | ||
log.Printf("[ERROR] Resource Informer was not created") | ||
log.Fatalf("[ERROR] Resource Informer was not created") // program will exit if this happens | ||
} else { | ||
log.Printf("Resource Informer created successfully") | ||
} | ||
return fakeResourceInformer | ||
} | ||
|
||
func TestCreateResourceInformer(t *testing.T) { | ||
fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(runtime.NewScheme()) | ||
resourceGVR := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"} | ||
informer := createFakeResourceInformer(resourceGVR) | ||
informer := createFakeResourceInformer(resourceGVR, fakeDynamicClient) | ||
|
||
if informer == nil { | ||
t.Errorf("Failed to create resource informer") | ||
t.Fatalf("Failed to create resource informer") // test will fail if this happens | ||
} | ||
} | ||
|
||
// Define an interface that includes the function you want to mock | ||
type InformerCreator interface { | ||
createFakeResourceInformer(gvr schema.GroupVersionResource, dynamicClient *fakeDynamic.FakeDynamicClient) cache.SharedIndexInformer | ||
} | ||
func TestEventObject(t *testing.T) { | ||
testDeployment := GetTestDeployment() | ||
// Marshal the struct to JSON | ||
jsonData, err := yaml.Marshal(testDeployment) | ||
if err != nil { | ||
fmt.Printf("error: %s", err) | ||
return | ||
} | ||
|
||
// Unmarshal the JSON to a map | ||
var deploymentMap map[string]interface{} | ||
err = yaml.Unmarshal(jsonData, &deploymentMap) | ||
if err != nil { | ||
fmt.Printf("error: %s", err) | ||
return | ||
} | ||
deploymentMap["eventType"] = "ADDED" | ||
deploymentMap["kind"] = "Deployment" | ||
deploymentMap["newObject"] = &deploymentMap | ||
eventObject := EventObject(deploymentMap, true) | ||
|
||
if eventObject.Kind != "Deployment" { | ||
t.Errorf("Failed to create event object, expected kind Deployment, got %s", eventObject.Kind) | ||
} | ||
|
||
if eventObject.KubernetesMetadata.Name != "test-deployment" { | ||
t.Errorf("Failed to create event object, expected name test-deployment, got %s", eventObject.KubernetesMetadata.Name) | ||
} | ||
|
||
// Have your mock type implement the interface | ||
type MockInformerCreator struct { | ||
mock.Mock | ||
if eventObject.KubernetesMetadata.Namespace != "default" { | ||
t.Errorf("Failed to create event object, expected namespace default, got %s", eventObject.KubernetesMetadata.Namespace) | ||
} | ||
} | ||
|
||
// Replace createResourceInformer with an instance of the interface | ||
func TestStructResourceLog(t *testing.T) { | ||
var deploymentMap map[string]interface{} | ||
testDeployment := GetTestDeployment() | ||
jsonDeployment, err := json.Marshal(testDeployment) | ||
if err != nil { | ||
t.Errorf("Failed to marshal test deployment.\nError:\n %v", err) | ||
} | ||
|
||
func TestAddEventHandlers(t *testing.T) { | ||
// Create a new mock informer creator | ||
fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(runtime.NewScheme()) | ||
mockInformerCreator := new(MockInformerCreator) | ||
mockInformer := createFakeResourceInformer(schema.GroupVersionResource{Group: "", Version: "v1", Resource: "deployments"}) | ||
// Define what should be returned when the mock is called | ||
mockInformerCreator.On("CreateFakeResourceInformer", mock.Anything, mock.Anything).Return(mockInformer) | ||
err = json.Unmarshal(jsonDeployment, &deploymentMap) | ||
if err != nil { | ||
t.Errorf("Failed to unmarshal test deployment.\nError:\n %v", err) | ||
} | ||
deploymentEventMap := map[string]interface{}{ | ||
|
||
// Run the function that you're testing | ||
AddEventHandlers() | ||
"eventType": "ADDED", | ||
"kind": "Deployment", | ||
"newObject": deploymentMap, | ||
} | ||
isStructured, _ := StructResourceLog(deploymentEventMap) | ||
|
||
// Check that the mock was called with the expected parameters | ||
mockInformerCreator.AssertCalled(t, "CreateFakeResourceInformer", schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}, fakeDynamicClient) | ||
mockInformerCreator.AssertCalled(t, "CreateFakeResourceInformer", schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}, fakeDynamicClient) | ||
// ... add more assertions here ... | ||
} | ||
func (m *MockInformerCreator) CreateFakeResourceInformer(gvr schema.GroupVersionResource, dynamicClient *fakeDynamic.FakeDynamicClient) cache.SharedIndexInformer { | ||
args := m.Called(gvr, dynamicClient) | ||
return args.Get(0).(cache.SharedIndexInformer) | ||
if !isStructured { | ||
t.Errorf("Failed to structure resource log") | ||
} | ||
} | ||
|
||
//func TestAddInformerEventHandler(t *testing.T) { | ||
// resourceGVR := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"} | ||
// mockDynamicClient := fakeDynamic.NewSimpleDynamicClient(runtime.NewScheme()) | ||
// | ||
// informer := CreateFakeResourceInformer(resourceGVR, mockDynamicClient) | ||
// | ||
// if informer == nil { | ||
// t.Errorf("Failed to create resource informer") | ||
// } | ||
// | ||
// synced := AddInformerEventHandler(informer) | ||
// | ||
// if !synced { | ||
// t.Errorf("Failed to add event handler for informer") | ||
// } | ||
//} |
Oops, something went wrong.