Skip to content

Commit

Permalink
324 - Migrate greeting devMode test to BDD
Browse files Browse the repository at this point in the history
  • Loading branch information
domhanak committed Mar 26, 2024
1 parent 6777c01 commit 52cb02c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bddframework/pkg/framework/kogitojobsservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func GetKogitoJobsService(namespace string) (*v1beta1.KogitoSupportingService, e

// WaitForKogitoJobsServiceLogContainsTextWithinMinutes waits until any pods contains a text
func WaitForKogitoJobsServiceLogContainsTextWithinMinutes(namespace, logText string, timeoutInMin int) error {
return WaitForAnyPodsByDeploymentToContainTextInLog(namespace, getJobsServiceName(), logText, timeoutInMin)
return WaitForAnyPodsByDeploymentToContainTextInLog(namespace, getJobsServiceName(), getJobsServiceName(), logText, timeoutInMin)
}

func getJobsServiceName() string {
Expand Down
15 changes: 8 additions & 7 deletions bddframework/pkg/framework/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,26 @@ func WaitForAllPodsByDeploymentConfigToContainTextInLog(namespace, dcName, logTe
}

// WaitForAllPodsByDeploymentToContainTextInLog waits for pods of specified deployment to contain specified text in log
func WaitForAllPodsByDeploymentToContainTextInLog(namespace, dName, logText string, timeoutInMin int) error {
return waitForPodsByDeploymentToContainTextInLog(namespace, dName, logText, timeoutInMin, checkAllPodsContainingTextInLog)
func WaitForAllPodsByDeploymentToContainTextInLog(namespace, dName, dContainerName, logText string, timeoutInMin int) error {
return waitForPodsByDeploymentToContainTextInLog(namespace, dName, dContainerName, logText, timeoutInMin, checkAllPodsContainingTextInLog)
}

// WaitForAnyPodsByDeploymentToContainTextInLog waits for pods of specified deployment to contain specified text in log
func WaitForAnyPodsByDeploymentToContainTextInLog(namespace, dName, logText string, timeoutInMin int) error {
return waitForPodsByDeploymentToContainTextInLog(namespace, dName, logText, timeoutInMin, checkAnyPodsContainingTextInLog)
func WaitForAnyPodsByDeploymentToContainTextInLog(namespace, dName, dContainerName, logText string, timeoutInMin int) error {
return waitForPodsByDeploymentToContainTextInLog(namespace, dName, dContainerName, logText, timeoutInMin, checkAnyPodsContainingTextInLog)
}

func waitForPodsByDeploymentToContainTextInLog(namespace, dName, logText string, timeoutInMin int, predicate func(string, []corev1.Pod, string, string) (bool, error)) error {
func waitForPodsByDeploymentToContainTextInLog(namespace, dName, dContainerName, logText string, timeoutInMin int, predicate func(string, []corev1.Pod, string, string) (bool, error)) error {
return WaitForOnOpenshift(namespace, fmt.Sprintf("Pods for deployment '%s' contain text '%s'", dName, logText), timeoutInMin,
func() (bool, error) {
pods, err := GetPodsByDeployment(namespace, dName)
if err != nil {
return false, err
}

// Container name is equal to deployment config name
return predicate(namespace, pods, dName, logText)
// Container name is equal to deployment config name, for sonataflow it is workflow so I added variable that can be set
// TODO: Extract this to sonataflow.go as this is so far only useful for us
return predicate(namespace, pods, dContainerName, logText)
}, CheckPodsByDeploymentInError(namespace, dName))
}

Expand Down
2 changes: 1 addition & 1 deletion bddframework/pkg/steps/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ func (data *Data) deploymentHasResourcesWithinMinutes(dName string, podNb, timeo
}

func (data *Data) deploymentPodsLogContainsTextWithinMinutes(dName, logText string, timeoutInMin int) error {
return framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, dName, logText, timeoutInMin)
return framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, dName, dName, logText, timeoutInMin)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ metadata:
annotations:
sonataflow.org/description: Greeting example on k8s!
sonataflow.org/version: 0.0.1
sonataflow.org/expressionLang: jsonpath
sonataflow.org/profile: dev
spec:
flow:
start: ChooseOnLanguage
events:
- kind: consumed
name: GreetingEvent
type: greetingEvent
source: greetingEvent
functions:
- name: greetFunction
type: custom
Expand All @@ -39,9 +35,9 @@ spec:
- name: ChooseOnLanguage
type: switch
dataConditions:
- condition: "${ .language == \"English\" }"
- condition: "{{ $.[?(@.language == \"English\")] }}"
transition: GreetInEnglish
- condition: "${ .language == \"Spanish\" }"
- condition: "{{ $.[?(@.language == \"Spanish\")] }}"
transition: GreetInSpanish
defaultCondition: GreetInEnglish
- name: GreetInEnglish
Expand Down
21 changes: 17 additions & 4 deletions testbdd/features/e2e.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Feature: Deploy SonataFlow Operator
Then Deployment "event-listener" pods log contains text '"description": "iPhone 12",' within 1 minutes
Then Deployment "event-listener" pods log contains text '"fraudEvaluation": true' within 1 minutes
Then Deployment "event-listener" pods log contains text '"shipping": "international"' within 1 minutes
Then SonataFlow "order-processing" pods log does not contain text 'ERROR' within 0 minutes

@devMode
Scenario: greeting-example DevMode E2E test
Expand All @@ -34,9 +35,21 @@ Feature: Deploy SonataFlow Operator
When SonataFlow greeting example is deployed
Then SonataFlow "greeting" has the condition "Running" set to "True" within 5 minutes
Then SonataFlow "greeting" is addressable within 1 minute
Then HTTP POST request as Cloud Event on SonataFlow "greeting" is successful within 1 minute with path "", headers "content-type= application/json,ce-specversion= 1.0,ce-source= /from/localhost,ce-type= greetingEvent,ce-id= f0643c68-609c-48aa-a820-5df423fa4fe0" and body:
Then HTTP POST request on SonataFlow "greeting" is successful within 1 minute with path "greeting", expectedResponseContains '"workflowdata":{"name":"Petr Pavel","language":"Spanish","greeting":"Saludos desde JSON Workflow, "}' and body:
"""json
{"language":"Spanish",
"name":"Petr Pavel"
{"name":"Petr Pavel",
"language":"Spanish"
}
"""
"""

Then HTTP GET request on SonataFlow "greeting" is successful within 1 minute with path "greeting", expectedResponseContains ''
Then SonataFlow "greeting" pods log contains text '"name" : "Petr Pavel",' within 1 minutes
Then SonataFlow "greeting" pods log contains text '"language" : "Spanish"' within 1 minutes
Then SonataFlow "greeting" pods log contains text '(executor-thread-1) Starting workflow' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'Start' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'ChooseOnLanguage' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'GreetInSpanish' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'Join-GreetPerson' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'GreetPerson' within 1 minutes
Then SonataFlow "greeting" pods log contains text 'End' within 1 minutes
Then SonataFlow "greeting" pods log does not contain text 'ERROR' within 0 minutes
2 changes: 1 addition & 1 deletion testbdd/steps/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func registerKubernetesSteps(ctx *godog.ScenarioContext, data *Data) {
}

func (data *Data) deploymentPodsLogContainsTextWithinMinutes(dName, logText string, timeoutInMin int) error {
return framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, dName, logText, timeoutInMin)
return framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, dName, dName, logText, timeoutInMin)
}
79 changes: 79 additions & 0 deletions testbdd/steps/sonataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ func registerSonataFlowSteps(ctx *godog.ScenarioContext, data *Data) {
ctx.Step(`^SonataFlow "([^"]*)" has the condition "(Running|Succeed|Built)" set to "(True|False|Unknown)" within (\d+) minutes?$`, data.sonataFlowHasTheConditionSetToWithinMinutes)
ctx.Step(`^SonataFlow "([^"]*)" is addressable within (\d+) minutes?$`, data.sonataFlowIsAddressableWithinMinutes)
ctx.Step(`^HTTP POST request as Cloud Event on SonataFlow "([^"]*)" is successful within (\d+) minutes? with path "([^"]*)", headers "([^"]*)" and body:$`, data.httpPostRequestAsCloudEventOnSonataFlowIsSuccessfulWithinMinutesWithPathHeadersAndBody)
ctx.Step(`^HTTP GET request on SonataFlow "([^"]*)" is successful within (\d+) minutes? with path "([^"]*)", expectedResponseContains '([^']*)'$`, data.httpGetRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseContains)
ctx.Step(`^HTTP POST request on SonataFlow "([^"]*)" is successful within (\d+) minutes? with path "([^"]*)", expectedResponseContains '([^']*)' and body:$`, data.httpPostRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseAndBody)
ctx.Step(`^SonataFlow "([^"]*)" pods log contains text '([^']*)' within (\d+) minutes$`, data.sonataFlowLogContainsTextWithinMinutes)
ctx.Step(`^SonataFlow "([^"]*)" pods log does not contain text '([^']*)' within (\d+) minutes$`, data.sonataFlowLogDoesNotContainTextWithinMinutes)
}

// TODO: Parametrize example
func (data *Data) sonataFlowOrderProcessingExampleIsDeployed() error {
projectDir, _ := utils.GetProjectDir()
projectDir = strings.Replace(projectDir, "/testbdd", "", -1)
Expand Down Expand Up @@ -118,6 +123,19 @@ func getSonataFlow(namespace, name string) (*v1alpha08.SonataFlow, error) {
return sonataFlow, nil
}

func (data *Data) sonataFlowLogContainsTextWithinMinutes(name, logText string, timeoutInMin int) error {
return framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, name, "workflow", logText, timeoutInMin)
}

func (data *Data) sonataFlowLogDoesNotContainTextWithinMinutes(name, logText string, timeoutInMin int) error {
err := framework.WaitForAllPodsByDeploymentToContainTextInLog(data.Namespace, name, "workflow", logText, timeoutInMin)
if err != nil {
return nil
} else {
return fmt.Errorf("found %s in %s pod logs", logText, name)
}
}

func (data *Data) httpPostRequestAsCloudEventOnSonataFlowIsSuccessfulWithinMinutesWithPathHeadersAndBody(name string, timeoutInMin int, path, headersContent string, body *godog.DocString) error {
path = data.ResolveWithScenarioContext(path)
bodyContent := data.ResolveWithScenarioContext(body.Content)
Expand All @@ -139,6 +157,67 @@ func (data *Data) httpPostRequestAsCloudEventOnSonataFlowIsSuccessfulWithinMinut
return framework.WaitForSuccessfulHTTPRequest(data.Namespace, requestInfo, timeoutInMin)
}

func (data *Data) httpGetRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseContains(name string, timeoutInMin int, path, expectedResponseContains string) error {
path = data.ResolveWithScenarioContext(path)
framework.GetLogger(data.Namespace).Debug("httpGetRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseContains", "sonataflow", name, "path", path, "expectedResponseContains", expectedResponseContains, "timeout", timeoutInMin)
sonataFlow, err := getSonataFlow(data.Namespace, name)
if err != nil {
return err
} else if sonataFlow == nil {
return fmt.Errorf("no SonataFlow found with name %s in namespace %s", name, data.Namespace)
}
sonataFlowUri := sonataFlow.Status.Endpoint
uri := strings.TrimSuffix(sonataFlowUri.String(), sonataFlowUri.Path)
if err != nil {
return err
}

requestInfo := framework.NewGETHTTPRequestInfo(uri, path)
actualResponse, err := framework.ExecuteHTTPRequestWithStringResponse(data.Namespace, requestInfo)
if err != nil {
return err
} else {
fmt.Printf("Got response %s.\n", actualResponse)
result := strings.Contains(actualResponse, expectedResponseContains)
if result {
return nil
} else {
return fmt.Errorf("response does not contain - expected response to contain [%s] actual response was [%s]", expectedResponseContains, actualResponse)
}
}
}

func (data *Data) httpPostRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseAndBody(name string, timeoutInMin int, path, expectedResponseContains string, body *godog.DocString) error {
path = data.ResolveWithScenarioContext(path)
bodyContent := data.ResolveWithScenarioContext(body.Content)
framework.GetLogger(data.Namespace).Debug("httpPostRequestOnSonataFlowIsSuccessfulWithinMinutesWithResponseAndBody", "sonataflow", name, "path", path, "bodyMediaType", body.MediaType, "bodyContent", bodyContent, "expectedResponseContains", expectedResponseContains, "timeout", timeoutInMin)
sonataFlow, err := getSonataFlow(data.Namespace, name)
if err != nil {
return err
} else if sonataFlow == nil {
return fmt.Errorf("no SonataFlow found with name %s in namespace %s", name, data.Namespace)
}
sonataFlowUri := sonataFlow.Status.Endpoint
uri := strings.TrimSuffix(sonataFlowUri.String(), sonataFlowUri.Path)
if err != nil {
return err
}

requestInfo := framework.NewPOSTHTTPRequestInfo(uri, path, body.MediaType, bodyContent)
actualResponse, err := framework.ExecuteHTTPRequestWithStringResponse(data.Namespace, requestInfo)
if err != nil {
return fmt.Errorf("error during execution of HTTP request. Error %s", err)
} else {
fmt.Printf("Got response %s.\n", actualResponse)
result := strings.Contains(actualResponse, expectedResponseContains)
if result {
return nil
} else {
return fmt.Errorf("response does not contain - expected response to contain [%s] actual response was [%s]", expectedResponseContains, actualResponse)
}
}
}

func parseHeaders(headersContent string) (map[string]string, error) {
headers := make(map[string]string)

Expand Down

0 comments on commit 52cb02c

Please sign in to comment.