Testing a Sample Application
In this getting started tutorial you will learn how to run tests on a sample application with a three-tier architecture.
You can find the application on GitHub in this repository. The technologies and testing tools are a detail as the goal is to go over workflow features that you will likely run into while getting started yourself. It consists of a small React web app with a NodeJS backend and PostgreSQL database.
Objectives
- Learn how to run unit, black-box and end-to-end tests using TestWorkflows.
- Learn how to use private repositories.
- Learn about TestWorkflow configuration, sharding and services.
Running Blackbox Tests
To run our blackbox tests, we are going to git clone, install dependencies and run our testing tool. You can use TestWorkflow configuration to set the git ref dynamically and run the workflow for any given commit.
apiVersion: testworkflows.testkube.io/v1
kind: TestWorkflow
metadata:
  name: test-api-blackbox
  namespace: testkube
spec:
  # Defines parameters that can be used within your workflows.
  # You can interpolate the variable with an expression: {{ config.your_variable }}.
  config:
    ref:
      type: string
      default: "main"
  content:
    git:
      uri: https://github.com/kubeshop/testkube-samples
      # The revision is the branch, tag or commit sha of your git repository.
      # In this case, it's a configuration variable that defaults to the 'main' branch.
      revision: "{{ config.ref }}"
  container:
    # The default image for your steps. You can choose to override the image within each step.
    image: node:alpine
    # The default working directory for your steps. Git content will be checked out in `/data/repo`.
    workingDir: /data/repo
  steps:
    # The `shell` keyword is a shorthand over `run` which makes it convenient to execute commands.
    # In this case, we first install all dependencies and afterwards run the tests.
    # Each of these steps runs in its own container while sharing the file system.
    - name: Install dependencies
      shell: npm ci
    - name: Run test
      shell: npm run test
Then you would call this in your continuous integration with the --config flag as seen below:
testkube run testworkflow test-api-blackbox --config ref=${CI_COMMIT_SHA} --watch
Running Tests on a Private Git Repository
When running your own test workflows, you likely use a private Git repository. You can use a Kubernetes Secret to store your Git credentials. Afterwards, you can use them to configure your username and password.
- With HTTP
- With HTTP Header
- With SSH
To authenticate with basic authentication:
apiVersion: testworkflows.testkube.io/v1
kind: TestWorkflow
metadata: ...
spec:
  content:
    git:
      uri: gitlab.example.com/kubeshop/awesome_project.git
      username: <username>
      tokenFrom:
        secretKeyRef:
          name: gitlab-credentials
          key: token
To authenticate with Authorization: Bearer <token> header:
apiVersion: testworkflows.testkube.io/v1
kind: TestWorkflow
metadata: ...
spec:
  content:
    git:
      uri: gitlab.example.com/kubeshop/awesome_project.git
      authType: header
      tokenFrom:
        secretKeyRef:
          name: gitlab-credentials
          key: token
To authenticate with SSH you will need to add the relevant files:
apiVersion: testworkflows.testkube.io/v1
kind: TestWorkflow
metadata: ...
spec:
  content:
    git:
      uri: gitlab.example.com/kubeshop/awesome_project.git
      sshKeyFrom:
        secretKeyRef:
          name: git-credentials
          key: ssh-key
      # sshKey: |
      #   -----BEGIN OPENSSH PRIVATE KEY-----
      #   b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
      #   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      #   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      #   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      #   Py55nRNObUBBaG/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=
      #   -----END OPENSSH PRIVATE KEY-----
Run E2E Tests
There are various ways to run your end-to-end tests. In this example we will rely on an externally created preview environment.
- With external previews
kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
  name: test-e2e
  namespace: testkube
spec:
  config:
    # Define the base URL on which the application under test will be available.
    baseUrl:
      type: string
  content:
    git:
      uri: https://github.com/kubeshop/testkube-samples
      revision: main
      paths: ["app"]
  container:
    workingDir: /data/repo/app
    # You have full control over the resource requests and limits. In this case,
    # let's increase Cypress requests as it's a large tool to run.
    resources:
      requests:
        cpu: 2
        memory: 2Gi
  steps:
    - name: Run e2e tests
      container:
        image: cypress/included:13.10.0
        env:
          - name: CYPRESS_baseUrl
            value: "{{ config.baseUrl }}"
      shell: cypress run