Skip to content

Commit

Permalink
Add testcase
Browse files Browse the repository at this point in the history
s

s

s

s

s

s

s

s

s
  • Loading branch information
friedrichwilken committed Mar 6, 2024
1 parent 88f0755 commit 31ba29a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,16 +580,16 @@ func Test_WatcherEventingCRK8sObjects(t *testing.T) {
}

func Test_EventMesh_MalformattedSecret(t *testing.T) {

testCases := []struct {
name string
givenObjName string
givenEventMeshNamespace string
givenMessages []eventingcontroller.Message
wantMatcher gomegatypes.GomegaMatcher
}{
{
name: "should have ready state when EventMesh secret is malformatted",
givenObjName: "something",
givenObjName: "everything-ok",
givenEventMeshNamespace: "event-mesh-namespace",
givenMessages: []eventingcontroller.Message{
{
Expand All @@ -604,31 +604,36 @@ func Test_EventMesh_MalformattedSecret(t *testing.T) {
},
URI: "uri",
}},
wantMatcher: gomega.And(
matchers.HaveStatusReady(),
),
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
// given
//g := gomega.NewWithT(t)

// Given:
// Make the Deployment always beeing ready.
eventingcontroller.IsDeploymentReady = func(deployment *kappsv1.Deployment) bool {
return true
}

// Create an unique namespace for this test run.
givenNamespace := fmt.Sprintf("namespace-%s", tc.givenObjName)
testEnvironment.EnsureNamespaceCreation(t, givenNamespace)

// Create an Eventing CR. We do not care about the details.
givenEveningCR := utils.NewEventingCR(
utils.WithEventMeshBackend(fmt.Sprintf("test-%s", tc.givenObjName)),
// Create an Eventing CR. We do not care about the details in this test.
givenEventingCR := utils.NewEventingCR(
utils.WithEventMeshBackend("test-secret-name"),
utils.WithEventingPublisherData(1, 1, "199m", "99Mi", "399m", "199Mi"),
utils.WithEventingEventTypePrefix("test-prefix"),
)

// Create a EventMesh Secret. This is the crucial part of the test.
// If the Secret is malformatted, the Eventing CR should have a warning state.
secretData, err := json.Marshal(tc.givenMessages)
// Create an unique Namespace for this test run.
givenNamespace := givenEventingCR.Namespace
testEnvironment.EnsureNamespaceCreation(t, givenNamespace)

// Create an EventMesh Secret. This is the crucial part of the test;
// if the Secret is malformatted, Eventing should be in warning state.
messageBytes, err := json.Marshal(tc.givenMessages)
if err != nil {
t.Fail()
}
Expand All @@ -639,13 +644,39 @@ func Test_EventMesh_MalformattedSecret(t *testing.T) {
},
Data: map[string][]byte{
"namespace": []byte(tc.givenEventMeshNamespace),
"message": secretData,
"message": messageBytes,
},
}
testEnvironment.EnsuretEventMeshSecretCreated(t, givenEveningCR, &secret)
testEnvironment.EnsureEventMeshSecretCreated(t, givenEventingCR, &secret)

// when
// Set up the K8s client to be able to mock the K8s API.
// First, to be able to reversible replace the original K8s client with a mocked one,
// we need to store the original K8s client.
originalKubeClient := testEnvironment.KubeClient
// Now we can replace the original K8s client with a mocked one in the test environment.
mockedKubeClient := &MockKubeClient{
Client: originalKubeClient,
}
testEnvironment.KubeClient = mockedKubeClient
testEnvironment.Reconciler.SetKubeClient(mockedKubeClient)

// This func will be executed to clean up the resources after the test.
defer func() {
// Set the original K8s client back.
testEnvironment.KubeClient = originalKubeClient
testEnvironment.Reconciler.SetKubeClient(originalKubeClient)

// Clean up the resources.
testEnvironment.EnsureNamespaceDeleted(t, givenNamespace)
}()

// When:
// Create the Eventing CR with the given EventMesh Secret.
//testEnvironment.EnsureK8sResourceCreated(t, givenEventingCR)

// Then:
// Check the Eventing CR status against the expected status.
//estEnvironment.GetEventingAssert(g, givenEventingCR).Should(tc.wantMatcher)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/utils/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,10 @@ func (env TestEnvironment) EnsureDefaultEventMeshSecretCreated(t *testing.T, eve
t.Helper()
subarr := strings.Split(eventing.Spec.Backend.Config.EventMeshSecret, "/")
secret := testutils.NewEventMeshSecret(subarr[1], subarr[0])
env.EnsuretEventMeshSecretCreated(t, eventing, secret)
env.EnsureEventMeshSecretCreated(t, eventing, secret)
}

func (env TestEnvironment) EnsuretEventMeshSecretCreated(t *testing.T, eventing *v1alpha1.Eventing, secret *kcorev1.Secret) {
func (env TestEnvironment) EnsureEventMeshSecretCreated(t *testing.T, eventing *v1alpha1.Eventing, secret *kcorev1.Secret) {
t.Helper()
env.EnsureK8sResourceCreated(t, secret)
}
Expand Down
2 changes: 2 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func NewAPIRuleCRD() *kapiextensionsv1.CustomResourceDefinition {
return result
}

// NewEventingCR creates a new Eventing CR with the given options.
// If no options are provided, the default (e. g. random name and namespace) will be used.
func NewEventingCR(opts ...EventingOption) *v1alpha1.Eventing {
name := fmt.Sprintf(NameFormat, GetRandString(randomNameLen))
namespace := fmt.Sprintf(NamespaceFormat, GetRandString(randomNameLen))
Expand Down

0 comments on commit 31ba29a

Please sign in to comment.