Update dry-run-testing.md based on comments

Signed-off-by: Nolan, Tavi <Tavi.Nolan@fmr.com>
This commit is contained in:
Nolan, Tavi
2024-07-26 12:00:06 +01:00
committed by GitHub
parent e5e7d0501b
commit 0bb72c38f0
@@ -1,5 +1,5 @@
---
id: dry run testing
id: dry-run-testing
title: Dry Run Testing
description: How to enable and implement dry run testing in actions
---
@@ -77,57 +77,3 @@ You will also need to add tests for the dry run handling, for example:
expect(...);
});
```
## Dry run via API call
Dry run can be performed using the dry-run API, which allows the dry run to be completed in code.
This [command line script](https://github.com/backstage/backstage/blob/master/contrib/scaffolder/template-testing-dry-run.md) offers a way for you to do work with dry-run API. You run it against a running instance, either locally or remote, and use it like:
```bash
scaffolder-dry http://localhost:7007/ template-directory values.yml output-directory
```
If you're using backend permissions, pass a front-end auth token from a current browser session via --token $FRONTEND_TOKEN.
You can also query the dry run endpoint directly in code, for example:
```javascript title="dry-run.js"
const template = yaml.load(
await fs.readFile('path/to/templates/template.yaml', 'utf-8'),
);
const values = JSON.parse(
await fs.readFile('path/to/templates/template_values.json', 'utf-8'),
);
// Prepare the request body
const body = {
template,
values,
directoryContents,
};
// Send the request to the dry-run endpoint
const response = await fetch(
'http://localhost:7000/api/scaffolder/v2/dry-run',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer <FRONT_END_TOKEN>', // replace 'FRONT_END_TOKEN' with your actual front-end auth token
},
body: JSON.stringify(body),
},
);
```
`template_values.json`
```json
{
"example": "test",
"name": "helloworld"
}
```
In this example `template.yaml` is your template yaml file, `template_values.json` would be a json file containing key value pairs for your template inputs, and `directoryContents` is an array that is populated with objects representing the contents of a specified directory (e.g. the same directory where you have your template yaml). Each object in the array corresponds to a file within the directory and contains two properties; the name of the file and the content of the file, encoded in Base64.
This is needed if any other files were required by the actions your template is using.