Skip to content

Better SCM CI Integration Testing

Saray Cabrera Padrón edited this page Feb 16, 2023 · 3 revisions

Testing In Development Environment

Most of the functionality can be tested by faking the payload sent by an SCM event. You can simply run a curl command against a local endpoint.

  1. Create a personal access token on GitHub with the public_repo scope.

  2. Create a workflow token on OBS with the scm_token containing the token created on GitHub.

  3. Take the payload of a recent delivery from one of the OBS webhooks and save it in a file like payload.json.

  4. Create a workflows.yml file under src/api/ with the workflows and steps you want to test (you can use our OBS workflow if needed). Change the method Workflows::YAMLDownloader#call to instead return File.open('workflows.yml'). This is to use the workflows.yml you created.

  5. In your local OBS instance, create the project/package/repositories/architectures needed for the workflows.yml you created in the previous step.

  6. Change the method TriggerControllerService::TokenExtractor#valid_signature? to return true. This is to circumvent the authentication, unless you want to test that.

  7. Send a POST request with curl. Change the ALL CAPS to the according values. See the example below:

    GitHub:

    curl -X POST 'http://localhost:3000/trigger/workflow?id=YOUR_WORKFLOW_TOKEN_ID' --data @payload.json --header 'X-GitHub-Event: SCM_EVENT_YOU_WANT' --header 'Content-Type: application/json' --header 'X-Hub-Signature-256: sha256=YOUR_WORKFLOW_TOKEN_STRING'
    

    GitLab:

    curl -X POST 'http://localhost:3000/trigger/workflow?id=YOUR_WORKFLOW_TOKEN_ID' --data @payload.json --header 'X-Gitlab-Event: SCM_EVENT_YOU_WANT' --header 'Content-Type: application/json' --header 'X-Gitlab-Token: YOUR_WORKFLOW_TOKEN_STRING'
    

    Gitea:

    curl -X POST 'http://localhost:3000/trigger/workflow?id=YOUR_WORKFLOW_TOKEN_ID' --data @payload.json --header 'X-GitHub-Event: SCM_EVENT_YOU_WANT' --header 'X-Gitea-Event: SCM_EVENT_YOU_WANT' --header 'Content-Type: application/json' --header 'X-Hub-Signature-256: sha256=YOUR_WORKFLOW_TOKEN_STRING'
    

With ngrok

In case it is extremely needed to connect your local application with a real SCM, use ngrok as we describe here:

Warning: always log out of your VPN before starting using ngrok.

  • Create an account in https://ngrok.com and follow the instructions, which are basically:
    • Download ngrok.zip and unzip it
    • Add the authtoken with ./ngrok authtoken <authtoken>
  • Run ngrok http 3000 to set the tunnel.
  • A UI will be displayed in your terminal with the public URL of your tunnel, copy it.
  • Add the host of the public URL to config/environments/development.rb. Example: config.hosts << "2406-79-153-113-247.ngrok.io"
  • Restart the local server.
  • Both localhost:3000 and the public URL should be accessible from your browser.
  • Follow this guide to integrate your locally-running application with GitHub, keeping this in mind:
    • Create the OBS workflow token locally
    • The "Payload URL" field in GitHub webhook should look like: https://2406-79-153-113-247.ngrok.io/trigger/workflow?id=0
  • Open a new PR towards the GitHub repository where you configured the webhook, so you will see that the new subproject has been created in the application running locally.
Clone this wiki locally