Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] K6 generator - Use openapi examples values as query parameters & body values #8378

Open
thim81 opened this issue Jan 8, 2021 · 6 comments

Comments

@thim81
Copy link

thim81 commented Jan 8, 2021

Is your feature request related to a problem? Please describe.

The K6 generator, generates an almost ready to use K6 script.

Example

group("/portal/v1/domainclusters/{domainClusterId}", () => {
        let domainClusterId = "TODO_EDIT_THE_DOMAINCLUSTERID";
        let url = BASE_URL + `/portal/v1/domainclusters/${domainClusterId}`;
        // Request No. 1
        let request = http.get(url);
        check(request, {
            "Success": (r) => r.status === 200
        });
        sleep(SLEEP_DURATION);

        // Request No. 2
        // TODO: edit the parameters of the request body.
        body = {"name": "string", "servers": "list"};
        params = {headers: {"Content-Type": "application/json"}};
        request = http.put(url, body, params);
        check(request, {
            "Success": (r) => r.status === 204
        });
        sleep(SLEEP_DURATION);

In the above there are 2 parts of the generated script, that require some manual modifications.

Query parameter:
let domainClusterId = "TODO_EDIT_THE_DOMAINCLUSTERID";

Request Body
body = {"name": "string", "servers": "list"};

The goal is to use the generated script in an Continuous integration flow.

The flow would be:

  • Manage OpenApi spec
  • Push to OA to GIT
  • Trigger build pipeline
    -- generated documentation
    -- generate postman collection
    -- generate a K6 script via the openapi-generator
    -- trigger a load test via K6

Describe the solution you'd like

In the OpenApi you can define "example" values

 '/portal/v1/domainclusters/{domainClusterId}':
    get:
      operationId: get-domain-clusters-id
      tags:
        - DomainClusters
      summary: Get a specific domain cluster configuration
      parameters:
           - domainClusterId:
                  name: domainClusterId
                  description: ID of the domain cluster setting.
                  in: path
                  required: true
                  schema:
                        type: string
                        nullable: true
                        example: f0c01c3a-6880-46c3-abb3-7a45828d1c45
      responses:
        '200':
          description: Success
    put:
      operationId: put-domain-clusters-id
      tags:
        - DomainClusters
      summary: Update a domain cluster configuration
      parameters:
           - domainClusterId:
                  name: domainClusterId
                  description: ID of the domain cluster setting.
                  in: path
                  required: true
                  schema:
                        type: string
                        nullable: true
                        example: f0c01c3a-6880-46c3-abb3-7a45828d1c45
      requestBody:
        description: The updated values
        content:
          application/json:
            schema:
              required:
                    - name
            type: object
            properties:
                name:
                       description: The name of the domain cluster.
                       type: string
                       example: CLUSTER-1
                servers:
                       description: The list of servers.
                       type: array
                       items:
                      type: string
                       example:
                               - s1
                               - s2
      responses:
        '204':
          description: Success

A handy solution would be to use the examples, like os

Query parameter:
let domainClusterId = "TODO_EDIT_THE_DOMAINCLUSTERID";

Take the example values from OpenApi
params > servers > schema > example: f0c01c3a-6880-46c3-abb3-7a45828d1c45

which would result in:
let domainClusterId = "f0c01c3a-6880-46c3-abb3-7a45828d1c45";

Request Body
body = {"name": "string", "servers": "list"};

Take the example values from OpenApi
requestBody > content > properties > name > example: CLUSTER-1
requestBody > content > properties > servers > example: - S1 - S2

Describe alternatives you've considered

creating a PR, but I'm not sure how to get started with the excellent K6 generator from @mostafa , due to no experience in JAVA development and if the mentioned example data is available in the OpenApi class.

@mostafa
Copy link
Contributor

mostafa commented Jan 11, 2021

Hey @thim81,

thanks for filing the issue. I'd be happy to see any contribution from you or any member of the community. Currently I am busy doing other things and this is not one of the priorities, but I might look at it in the future. Also, I can help with the code review if you create a PR.

@thim81
Copy link
Author

thim81 commented Jan 11, 2021

Hi @mostafa,

Fully understood. Do you find it a valuable addition?

Since I'm not at home of the code base, can you perhaps me towards the piece in the code where you would want to see this extension being implemented?

@mostafa
Copy link
Contributor

mostafa commented Jan 11, 2021

@thim81 Of course!

This is the file that generates the k6 script. And this is the template I used to generate the script. In this case, all those TODO_EDIT_THE_{{{value}}} should be replaced by correct examples. In the code, look for instances of the Parameter static class. Let me know if you need more information!

@mostafa
Copy link
Contributor

mostafa commented Feb 10, 2021

Hey @thim81,

have you been able to add the feature/enhancement? Do you need any help?

@ideas-into-software
Copy link
Contributor

Hi,

As part of recent automation work done on a commercial project, where I utilized the K6 OpenAPI generator, I hit this issue where examples defined for path/query parameters were not taken into account. Therefore, the script produced was not really usable, only a skeleton. I needed to generate script which was fully ready to run, out-of-the box, so smoke tests (https://k6.io/docs/test-types/smoke-testing/) could be run as part of CI/CD pipeline.

I went ahead and added what was needed, and now, Data In Motion (https://www.datainmotion.de/), client for whom this work was done, generously agreed to share this with the community.

So that’s the background on the PR which I just opened: #9750

This addresses extracting examples defined for path/query parameters; as you know, there are also examples defined at requestBody level and other (https://swagger.io/docs/specification/adding-examples/); hopefully the community can pick it up from there and enhance further – path/query params was all that was needed for this particular use case.

@ideas-into-software
Copy link
Contributor

ideas-into-software commented Oct 17, 2021

Hi,

More goodies: #10614

With this latest MR, this feature request (i.e. #8378) is now feature complete.

Please see #10614 for more info.

New code is documented. Entire solution will be showcased as part of the upcoming EclipseCon 2021 session: "Automated testing of OpenAPI-described RESTful microservices utilizing open source tools" https://www.eclipsecon.org/2021/sessions/automated-testing-openapi-described-restful-microservices-utilizing-open-source-tools

Hopefully formatting is now OK, in case of any problems please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants