Skip to content

Commit

Permalink
feat(test): Unit testing using kurtestosis (#161)
Browse files Browse the repository at this point in the history
**Description**

[`kurtestosis`](https://github.com/ethereum-optimism/kurtestosis),
currently an MVP, is a unit test runner for `starlark`/`kurtosis`. It
allows for lower-level testing that would otherwise require costly and
complex environment setup and introduce unjustifiable test code
complexity.

This PR adds a PoC test for a simple piece of functionality,
`label_from_image` function in `util.star`. The test is then executed as
a part of the PR pipeline. `mise` is introduced to manage dependencies.
  • Loading branch information
janjakubnanista authored Feb 10, 2025
1 parent 825b5f5 commit 1887774
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/per-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ jobs:
- name: Kurtosis Lint
run: kurtosis lint ${{ github.workspace }}

test:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Install mise
uses: jdx/mise-action@v2

- name: Run kurtestosis tests
run: kurtestosis .

33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,43 @@ kurtosis service stop <enclave-name> <service-name>
kurtosis service start <enclave-name> <service-name>
```

## Development

### Development environment

We use [`mise`](https://mise.jdx.dev/) as a dependency manager for these tools.
Once properly installed, `mise` will provide the correct versions for each tool. `mise` does not
replace any other installations of these binaries and will only serve these binaries when you are
working inside of the `optimism-package` directory.

#### Install `mise`

Install `mise` by following the instructions provided on the
[Getting Started page](https://mise.jdx.dev/getting-started.html#_1-install-mise-cli).

#### Install dependencies

```sh
mise install
```

## Contributing

If you have made changes and would like to submit a PR, test locally and make sure to run `lint` on your changes

```bash
kurtosis lint --format .
```

### Testing

#### Unit tests

We are using [`kurtestosis`](https://github.com/ethereum-optimism/kurtestosis) to run a set of unit tests against the starlark code:

```bash
# To run all unit tests
kurtestosis .
```

The tests can be found in `*_test.star` scripts located in the `test` directory.
4 changes: 4 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tools]

# Core dependencies
"ubi:ethereum-optimism/kurtestosis" = "0.0.5"
60 changes: 60 additions & 0 deletions test/util_test.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
util = import_module("/src/util.star")


def test_label_from_image_short_image_name(plan):
image_name = "my-image"
image_label = util.label_from_image(image_name)

expect.eq(image_name, image_label)


def test_label_from_image_63_character_image_name(plan):
image_name = ("tenletters" * 6) + "333"
expect.eq(len(image_name), 63)

image_label = util.label_from_image(image_name)
expect.eq(len(image_label), 63)
expect.eq(image_name, image_label)


def test_label_from_image_64_character_image_name_no_slashes(plan):
image_name = ("tenletters" * 6) + "4444"
expect.eq(len(image_name), 64)

image_label = util.label_from_image(image_name)
expect.eq(len(image_label), 63)
expect.eq(image_name[-63:], image_label)


def test_label_from_image_long_image_name_no_slashes(plan):
image_name = "tenletters" * 10
expect.eq(len(image_name), 100)

image_label = util.label_from_image(image_name)
expect.eq(len(image_label), 63)
expect.eq(image_name[-63:], image_label)


def test_label_from_image_long_image_name_one_slash(plan):
image_suffix = "444"
image_name = "/".join(["tenletters" * 6, image_suffix])

image_label = util.label_from_image(image_name)
expect.eq(image_suffix, image_label)


def test_label_from_image_long_image_name_more_slashes(plan):
image_suffix = "/".join(["slash", "slash2", "slash3"])
image_name = "/".join(["tenletters" * 8, image_suffix])

image_label = util.label_from_image(image_name)
expect.eq(image_suffix, image_label)


def test_label_from_image_long_image_name_long_suffix(plan):
image_suffix = "/".join(["slash", "slash2", "slash3", "what-a-suffix" * 5])
image_name = "/".join(["tenletters" * 8, image_suffix])

image_label = util.label_from_image(image_name)
expect.eq(len(image_label), 63)
expect.eq(image_suffix[-63:], image_label)

0 comments on commit 1887774

Please sign in to comment.