From 18877745fe64ce97403b2dba27d9bad9f45ca7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Jakub=20Nani=C5=A1ta?= Date: Mon, 10 Feb 2025 13:12:55 -0800 Subject: [PATCH] feat(test): Unit testing using kurtestosis (#161) **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. --- .github/workflows/per-pr.yml | 12 ++++++++ README.md | 33 ++++++++++++++++++++ mise.toml | 4 +++ test/util_test.star | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 mise.toml create mode 100644 test/util_test.star diff --git a/.github/workflows/per-pr.yml b/.github/workflows/per-pr.yml index 8ac35b08..b70ff204 100644 --- a/.github/workflows/per-pr.yml +++ b/.github/workflows/per-pr.yml @@ -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 . + diff --git a/README.md b/README.md index 8332e422..0ef1182b 100644 --- a/README.md +++ b/README.md @@ -663,6 +663,26 @@ kurtosis service stop kurtosis service start ``` +## 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 @@ -670,3 +690,16 @@ If you have made changes and would like to submit a PR, test locally and make su ```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. diff --git a/mise.toml b/mise.toml new file mode 100644 index 00000000..30f93eca --- /dev/null +++ b/mise.toml @@ -0,0 +1,4 @@ +[tools] + +# Core dependencies +"ubi:ethereum-optimism/kurtestosis" = "0.0.5" \ No newline at end of file diff --git a/test/util_test.star b/test/util_test.star new file mode 100644 index 00000000..9f9a7caa --- /dev/null +++ b/test/util_test.star @@ -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)