diff --git a/.github/workflows/build_interu.yml b/.github/workflows/build_interu.yml new file mode 100644 index 0000000..9377421 --- /dev/null +++ b/.github/workflows/build_interu.yml @@ -0,0 +1,44 @@ +name: Build interu + +on: + workflow_call: + inputs: + os: + required: true + type: string + target: + required: true + type: string + upload: + default: false + type: boolean + +env: + RUST_VERSION: 1.83.0 + +jobs: + build: + name: Build interu-${{ inputs.target }} + runs-on: ${{ inputs.os }} + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - uses: dtolnay/rust-toolchain@0e66bd3e6b38ec0ad5312288c83e47c143e6b09e # v1 + with: + toolchain: ${{ env.RUST_VERSION }} + targets: ${{ inputs.target }} + + - name: Build Binary + run: cargo build --target ${{ inputs.target }} --release --package interu + + - name: Rename Binary + run: mv target/${{ inputs.target }}/release/interu interu-${{ inputs.target }} + + - name: Upload Artifact + if: inputs.upload + uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 + with: + path: interu-${{ inputs.target }} diff --git a/.github/workflows/pr_interu.yml b/.github/workflows/pr_interu.yml new file mode 100644 index 0000000..bd6d1ec --- /dev/null +++ b/.github/workflows/pr_interu.yml @@ -0,0 +1,25 @@ +name: Build interu + +on: + pull_request: + paths: + - .github/workflows/release_interu.yml + - .github/workflows/build_interu.yml + - .github/workflows/pr_interu.yml + - rust-toolchain.toml + - tools/interu/** + - Cargo.toml + +jobs: + build: + uses: ./.github/workflows/build_interu.yml + with: + target: ${{ matrix.target }} + os: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest diff --git a/.github/workflows/release_interu.yml b/.github/workflows/release_interu.yml new file mode 100644 index 0000000..d43e6e6 --- /dev/null +++ b/.github/workflows/release_interu.yml @@ -0,0 +1,33 @@ +name: Release interu + +on: + push: + tags: + - "interu-[0-9]+.[0-9]+.[0-9]+**" + +jobs: + build: + uses: ./.github/workflows/build_interu.yml + with: + upload: true + target: ${{ matrix.target }} + os: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + include: + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + release: + runs-on: ubuntu-latest + steps: + - name: Download Artifacts + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 + with: + path: artifacts + + - name: Upload Release Binary + uses: softprops/action-gh-release@7b4da11513bf3f43f9999e90eabced41ab8bb048 # v2.2.0 + with: + files: artifacts/* diff --git a/.gitignore b/.gitignore index bee8a64..691f494 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fa6f07f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1010 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "argh" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7af5ba06967ff7214ce4c7419c7d185be7ecd6cc4965a8f6e1d8ce0398aad219" +dependencies = [ + "argh_derive", + "argh_shared", +] + +[[package]] +name = "argh_derive" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56df0aeedf6b7a2fc67d06db35b09684c3e8da0c95f8f27685cb17e08413d87a" +dependencies = [ + "argh_shared", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "argh_shared" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5693f39141bda5760ecc4111ab08da40565d1771038c4a0250f03457ec707531" +dependencies = [ + "serde", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cc" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +dependencies = [ + "equivalent", + "hashbrown 0.15.2", + "serde", +] + +[[package]] +name = "interu" +version = "0.1.0" +dependencies = [ + "argh", + "rand", + "rstest", + "serde", + "serde_with", + "serde_yaml", + "snafu", + "strum", + "tracing", +] + +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "js-sys" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.168" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "pin-project-lite" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro-crate" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + +[[package]] +name = "rstest" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2c585be59b6b5dd66a9d2084aa1d8bd52fbdb806eafdeffb52791147862035" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "825ea780781b15345a146be27eaefb05085e337e869bff01b4306a4fd4a9ad5a" +dependencies = [ + "cfg-if", + "glob", + "proc-macro-crate", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn", + "unicode-ident", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "rustversion" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "semver" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" + +[[package]] +name = "serde" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_with" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" +dependencies = [ + "base64", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.7.0", + "serde", + "serde_derive", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.7.0", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "snafu" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "223891c85e2a29c3fe8fb900c1fae5e69c2e42415e3177752e8718475efa5019" +dependencies = [ + "snafu-derive", +] + +[[package]] +name = "snafu-derive" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03c3c6b7927ffe7ecaa769ee0e3994da3b8cafc8f444578982c83ecb161af917" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "time" +version = "0.3.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.22.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" +dependencies = [ + "indexmap 2.7.0", + "toml_datetime", + "winnow", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" +dependencies = [ + "memchr", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f4259cf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,24 @@ +[workspace] +members = ["tools/*"] +resolver = "2" + +[workspace.package] +edition = "2021" + +[workspace.dependencies] +argh = "0.1.12" +rand = "0.8.5" +rstest = "0.23.0" +serde = { version = "1.0.216", features = ["derive"] } +serde_with = "3.11.0" +serde_yaml = "0.9.34+deprecated" +snafu = "0.8.5" +strum = { version = "0.26.3", features = ["derive"] } +tracing = "0.1.41" + +[profile.release] +opt-level = "z" # Optimize for size. +lto = true # Enable Link Time Optimization +codegen-units = 1 # Reduce number of codegen units to increase optimizations. +panic = "abort" # Abort on panic +strip = true # Automatically strip symbols from the binary. diff --git a/run-integration-test/README.md b/run-integration-test/README.md index 744f991..61b87c4 100644 --- a/run-integration-test/README.md +++ b/run-integration-test/README.md @@ -23,6 +23,75 @@ There is no mapping for `oke` yet. Supported Kubernetes version can be inspected on the official Replicated documentation [page][supported-clusters]. Supported architectures are `amd64` and `arm64`. +## Integration Test Configuration File + +Each downstream repository needs a configuration file. This allows customization of various +parameters based on the needs of the operator, or the particular tests. The config file needs to be +placed here: `tests/interu.yaml` to be picked up automatically. + +There are two major components in the config file: + +- Definition of `runners`. +- Test `profiles`. + +### Runners + +The runner configuration selects from the available Kubernetes versions and distributions along with +node groups of desired instance size, architecture, disk size. + +```yaml +runners: + default-amd64: + platform: rke2-1.31.2 + ttl: 4h + node-groups: + - name: default + arch: amd64 + size: large + disk: 50 + nodes: 3 +``` + +### Profiles + +Profiles allow for a variety of pre-configured runners and strategies. A profile can be chosen when +calling interu. For example, the `schedule` profile could be used in CI on the `schedule` event. + +The following strategies are currently available: + +- `weighted`. +- `use-runner`. + +- The `weighted` strategy allows defining two or more `weights`. Each `weight` defines how often the + runner specified is used when this profile is used. It should be noted that the weights *don't* + need to add up to 100, but it is recommended to more easily gauge the probability. +- The `use-runner` strategy just uses the specified `runner`. + +Each profile can additionally specify test `options`, like `parallelism`, `test-run` and `test-parameter`. + +```yaml +profiles: + schedule: + strategy: weighted + weights: + - weight: 80 + runner: default-amd64 + - weight: 10 + runner: default-arm64 + - weight: 10 + runner: default-mixed + options: + parallelism: 1 + + workflow_dispatch: + strategy: use-runner + runner: default-amd64 + options: + test-run: test-suite + test-parameter: smoke + parallelism: 2 +``` + ## Inputs and Outputs > [!TIP] diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..2ceee0d --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1 @@ +toolchain.channel = "1.83.0" diff --git a/tools/interu/Cargo.toml b/tools/interu/Cargo.toml new file mode 100644 index 0000000..4525038 --- /dev/null +++ b/tools/interu/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "interu" +version = "0.1.0" +edition.workspace = true + +[dependencies] +argh.workspace = true +rand.workspace = true +rstest.workspace = true +serde.workspace = true +serde_with.workspace = true +serde_yaml.workspace = true +snafu.workspace = true +strum.workspace = true +tracing.workspace = true diff --git a/tools/interu/README.md b/tools/interu/README.md new file mode 100644 index 0000000..78e6a0c --- /dev/null +++ b/tools/interu/README.md @@ -0,0 +1,6 @@ +# interu + +interu is a small utility to read, validate and expand integration test config files. For more +information on what it can do, consult the `run-integration-test` action [README][action-readme]. + +[action-readme]: ../../run-integration-test/README.md diff --git a/tools/interu/fixtures/instances.yaml b/tools/interu/fixtures/instances.yaml new file mode 100644 index 0000000..0a38232 --- /dev/null +++ b/tools/interu/fixtures/instances.yaml @@ -0,0 +1,47 @@ +eks: + arm64: + small: m7g.large + medium: m7g.2xlarge + large: m7g.4xlarge + amd64: + small: m6i.large + medium: m6i.2xlarge + large: m6i.4xlarge + +gke: + arm64: + small: t2a-standard-2 + medium: t2a-standard-8 + large: t2a-standard-16 + amd64: + small: e2-standard-2 + medium: e2-standard-8 + large: e2-standard-16 + +aks: + arm64: + small: Standard_D2ps_v5 + medium: Standard_D8ps_v5 + large: Standard_D16ps_v5 + amd64: + small: Standard_DS1_v2 + medium: Standard_DS3_v2 + large: Standard_DS5_v2 + +kind: + amd64: + small: r1.small + medium: r1.medium + large: r1.large + +k3s: + amd64: + small: r1.small + medium: r1.medium + large: r1.large + +rke2: + amd64: + small: r1.small + medium: r1.medium + large: r1.large diff --git a/tools/interu/fixtures/interu.yaml b/tools/interu/fixtures/interu.yaml new file mode 100644 index 0000000..279ee08 --- /dev/null +++ b/tools/interu/fixtures/interu.yaml @@ -0,0 +1,56 @@ +runners: + default-amd64: + platform: rke2-1.31.2 + ttl: 4h + node-groups: + - name: default + arch: amd64 + size: large + disk: 50 + nodes: 3 + + default-arm64: + platform: aks-1.31 + ttl: 4h + node-groups: + - name: default + arch: arm64 + size: large + disk: 50 + nodes: 3 + + default-mixed: + platform: aks-1.31 + ttl: 4h + node-groups: + - name: amd64-nodes + arch: amd64 + size: large + disk: 50 + nodes: 3 + - name: arm64-nodes + arch: arm64 + size: large + disk: 50 + nodes: 3 + +profiles: + schedule: + strategy: weighted + weights: + - weight: 80 + runner: default-amd64 + - weight: 10 + runner: default-arm64 + - weight: 10 + runner: default-mixed + options: + parallelism: 1 + + workflow_dispatch: + strategy: use-runner + runner: default-amd64 + options: + test-run: test-suite + test-parameter: smoke + parallelism: 2 diff --git a/tools/interu/src/cli.rs b/tools/interu/src/cli.rs new file mode 100644 index 0000000..7ce6577 --- /dev/null +++ b/tools/interu/src/cli.rs @@ -0,0 +1,39 @@ +use std::path::PathBuf; + +use argh::FromArgs; + +#[derive(Debug, FromArgs)] +/// Expand configuration into key=value pairs used by the run-integration-test action +pub struct Cli { + /// path to integration test config file + #[argh( + option, + short = 'c', + long = "config", + default = r#"PathBuf::from("tests/interu.yaml")"# + )] + pub config: PathBuf, + + /// path to instances file + #[argh(option, short = 'i', long = "instances")] + pub instances: PathBuf, + + /// write configuration key=value pairs separated by newlines to file + #[argh( + option, + short = 'o', + long = "output", + description = "write configuration key=value pairs separated by newlines to file + Useful for CI tools which give a file to write env vars and outputs to which are used in subsequent steps" + )] + pub output: Option, + + /// run without producing output on stdout + // #[arg(short, long, visible_short_alias('s'), visible_alias("silent"))] + #[argh(switch, short = 'q', long = "quiet")] + pub quiet: bool, + + /// which test profile to use + #[argh(positional)] + pub profile: String, +} diff --git a/tools/interu/src/config/mod.rs b/tools/interu/src/config/mod.rs new file mode 100644 index 0000000..fbcb5fd --- /dev/null +++ b/tools/interu/src/config/mod.rs @@ -0,0 +1,234 @@ +use std::{ + collections::BTreeMap, + fmt::Display, + path::{Path, PathBuf}, +}; + +use rand::{distributions::WeightedIndex, prelude::Distribution as _, thread_rng}; +use serde::Deserialize; +use snafu::{OptionExt, ResultExt, Snafu}; +use tracing::instrument; + +use crate::{ + config::{ + profile::{Profile, StrategyValidationError, TestRun}, + runner::{ + ConvertNodeGroupError, Distribution, ReplicatedNodeGroup, Runner, RunnerValidationError, + }, + }, + instances::Instances, +}; + +pub mod profile; +pub mod runner; + +/// Errors which can be encountered when reading and validating the config file. +#[derive(Debug, Snafu)] +pub enum Error { + #[snafu(display("failed to read config file at {path}", path = path.display()))] + ReadFile { + source: std::io::Error, + path: PathBuf, + }, + + #[snafu(display("failed to deserialize config file at {path} as yaml", path = path.display()))] + Deserialize { + source: serde_yaml::Error, + path: PathBuf, + }, + + #[snafu(display("failed to validate config file at {path}", path = path.display()))] + Validate { + source: ValidationError, + path: PathBuf, + }, + + #[snafu(display("failed to find profile named {profile_name:?}"))] + UnknownProfileName { profile_name: String }, + + #[snafu(display("failed to convert node-group to Replicated format"))] + ConvertNodeGroup { source: ConvertNodeGroupError }, +} + +/// Errors which can be encountered during config file validation. +#[derive(Debug, Snafu)] +pub enum ValidationError { + #[snafu(display("encountered invalid runner config"))] + InvalidRunnerConfig { source: RunnerValidationError }, + + #[snafu(display("encountered invalid profile config"))] + InvalidProfileConfig { source: StrategyValidationError }, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct Config { + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub runners: BTreeMap, + + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub profiles: BTreeMap, +} + +impl Config { + /// Read, deserialize and validate a config from a file located at `path`. + #[instrument(name = "load_config_from_file", skip(path), fields(path = %path.as_ref().display()))] + pub fn from_file

(path: P) -> Result + where + P: AsRef, + { + let contents = std::fs::read_to_string(&path).context(ReadFileSnafu { + path: path.as_ref(), + })?; + + tracing::debug!("deserialize file contents"); + let config: Self = serde_yaml::from_str(&contents).context(DeserializeSnafu { + path: path.as_ref(), + })?; + + config.validate().context(ValidateSnafu { + path: path.as_ref(), + })?; + Ok(config) + } + + #[instrument(name = "validate_config", skip(self))] + fn validate(&self) -> Result<(), ValidationError> { + for (runner_name, runner) in &self.runners { + tracing::debug!(runner_name, "validate runner"); + + runner + .validate(runner_name) + .context(InvalidRunnerConfigSnafu)?; + } + + for (profile_name, profile) in &self.profiles { + tracing::debug!(profile_name, "validate profile"); + + profile + .validate(profile_name, &self.runners) + .context(InvalidProfileConfigSnafu)?; + } + + Ok(()) + } + + /// Determines the final expanded parameters based on the provided profile. + pub fn determine_parameters<'a>( + &'a self, + profile_name: &String, + instances: &'a Instances, + ) -> Result, Error> { + // First, lookup the profile by name. Error if the profile does't exist. + let profile = self + .profiles + .get(profile_name) + .context(UnknownProfileNameSnafu { profile_name })?; + + // Next, lookup the runner ref based on the profile strategy + let runner_ref = match &profile.strategy { + profile::Strategy::Weighted(options) => { + let weights: Vec<_> = options.weights.iter().map(|w| w.weight).collect(); + let random_distribution = WeightedIndex::new(weights).unwrap(); + let mut rng = thread_rng(); + + let index = random_distribution.sample(&mut rng); + let weight = options.weights.get(index).expect("always valid index"); + + &weight.runner + } + profile::Strategy::UseRunner(options) => &options.runner, + }; + + // Get the runner based on the runner ref + let runner = self.runners.get(runner_ref).unwrap(); + + // Get test options + let (test_parallelism, test_run, test_parameter) = profile.strategy.get_test_options(); + + // Convert our node groups to replicated node groups + let node_groups = runner + .node_groups + .clone() + .into_iter() + .map(|ng| ReplicatedNodeGroup::try_from(ng, instances, &runner.platform.distribution)) + .collect::, ConvertNodeGroupError>>() + .context(ConvertNodeGroupSnafu)?; + + Ok(Parameters { + kubernetes_distribution: &runner.platform.distribution, + kubernetes_version: &runner.platform.version, + cluster_ttl: &runner.ttl, + test_parallelism, + test_parameter, + node_groups, + test_run, + }) + } +} + +/// Parameters which will be expanded into environment variables via the [`Display`] implementation. +#[derive(Debug)] +pub struct Parameters<'a> { + /// Selected Kubernetes distribution available on Replicated. + kubernetes_distribution: &'a Distribution, + + /// Kubernetes version used for the cluster. + kubernetes_version: &'a str, + + /// Maximum TTL of the cluster. + cluster_ttl: &'a str, + + /// Node groups which will be created in the cluster. + node_groups: Vec>, + + /// Number of tests which get run in parallel. + test_parallelism: usize, + + /// Optional test parameter passed to `test_run`. + test_parameter: &'a str, + + /// Set of tests to run. + test_run: &'a TestRun, +} + +impl<'a> Display for Parameters<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // Destructure all fields so that any additional parameters are handled here. + // DO NOT USE `..`. + let Self { + kubernetes_distribution, + kubernetes_version, + cluster_ttl, + node_groups, + test_parallelism, + test_parameter, + test_run, + } = self; + + #[rustfmt::skip] // Skip formatting because otherwise the next line would be split into three lines. + write!(f, "INTERU_KUBERNETES_DISTRIBUTION={kubernetes_distribution}\n")?; + write!(f, "INTERU_KUBERNETES_VERSION={kubernetes_version}\n")?; + write!(f, "INTERU_TEST_PARALLELISM={test_parallelism}\n")?; + write!(f, "INTERU_TEST_PARAMETER={test_parameter}\n")?; + write!(f, "INTERU_CLUSTER_TTL={cluster_ttl}\n")?; + write!(f, "INTERU_TEST_RUN={test_run}\n")?; + + // See: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#multiline-strings + let node_groups = serde_yaml::to_string(&node_groups).expect("must be serializable"); + write!(f, "INTERU_NODE_GROUPS<, + ) -> Result<(), StrategyValidationError> { + self.strategy.validate(profile_name, runners) + } +} + +#[derive(Debug, Snafu)] +pub enum StrategyValidationError { + #[snafu(display("runner {runner_ref:?} referenced in {at} is not defined"))] + InvalidRunnerReference { at: String, runner_ref: String }, + + #[snafu(display( + r#"strategy {at} must define two or more weights, or use the "use-runner" strategy instead"# + ))] + WeightsCount { at: String }, + + #[snafu(display("strategy {at} references a runner already referenced by another weight"))] + NonUniqueWeightRunner { at: String }, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(tag = "strategy", rename_all = "kebab-case")] +pub enum Strategy { + Weighted(WeightedOptions), + UseRunner(UseRunnerOptions), +} + +impl Strategy { + pub fn validate( + &self, + profile_name: &str, + runners: &BTreeMap, + ) -> Result<(), StrategyValidationError> { + match &self { + Strategy::Weighted(weighted_options) => { + weighted_options.validate(profile_name, runners) + } + Strategy::UseRunner(use_runner_options) => { + use_runner_options.validate(profile_name, runners) + } + } + } + + pub fn get_test_options(&self) -> (usize, &TestRun, &str) { + match self { + Strategy::Weighted(options) => ( + options.options.parallelism, + &options.options.test_run, + options.options.test_parameter.as_str(), + ), + Strategy::UseRunner(options) => ( + options.options.parallelism, + &options.options.test_run, + options.options.test_parameter.as_str(), + ), + } + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct WeightedOptions { + pub weights: Vec, + pub options: TestOptions, +} + +impl WeightedOptions { + pub fn validate( + &self, + profile_name: &str, + runners: &BTreeMap, + ) -> Result<(), StrategyValidationError> { + ensure!( + self.weights.len() > 1, + WeightsCountSnafu { + at: format!("profiles.{profile_name}.weights") + } + ); + + for (i, weight) in self.weights.iter().enumerate() { + ensure!( + runners.contains_key(&weight.runner), + InvalidRunnerReferenceSnafu { + at: format!( + "profiles.{profile_name}.weights.{weight}", + weight = weight.weight + ), + runner_ref: weight.runner.clone(), + } + ); + + // Ensure that all weights use unique runners + let before = &mut self.weights[..i].iter().map(|w| w.runner.as_str()); + if before.len() > 0 && before.any(|n| n == weight.runner) { + return NonUniqueWeightRunnerSnafu { + at: format!( + "profiles.{profile_name}.weights[{index}].runner", + index = i - 1 + ), + } + .fail(); + } + } + + Ok(()) + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct Weight { + pub weight: usize, + pub runner: String, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct UseRunnerOptions { + pub runner: String, + pub options: TestOptions, +} + +impl UseRunnerOptions { + pub fn validate( + &self, + profile_name: &str, + runners: &BTreeMap, + ) -> Result<(), StrategyValidationError> { + if !runners.contains_key(&self.runner) { + return InvalidRunnerReferenceSnafu { + at: format!("profile.{profile_name}.runner"), + runner_ref: self.runner.clone(), + } + .fail(); + } + + Ok(()) + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct TestOptions { + pub parallelism: usize, + + #[serde(default)] + pub test_run: TestRun, + + #[serde(default)] + pub test_parameter: String, +} + +#[derive(Clone, Debug, Default, Deserialize, Serialize, strum::Display)] +#[strum(serialize_all = "kebab-case")] +#[serde(rename_all = "kebab-case")] +pub enum TestRun { + TestSuite, + Test, + + #[default] + All, +} diff --git a/tools/interu/src/config/runner.rs b/tools/interu/src/config/runner.rs new file mode 100644 index 0000000..016e932 --- /dev/null +++ b/tools/interu/src/config/runner.rs @@ -0,0 +1,178 @@ +use std::{fmt::Display, str::FromStr}; + +use serde::{Deserialize, Serialize}; +use serde_with::{serde_as, DisplayFromStr}; +use snafu::{ensure, OptionExt, ResultExt as _, Snafu}; + +use crate::instances::Instances; + +#[derive(Debug, Snafu)] +pub enum RunnerValidationError { + #[snafu(display("{at} must contain at least one node group"))] + ZeroNodeGroups { at: String }, +} + +#[serde_as] +#[derive(Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct Runner { + #[serde_as(as = "DisplayFromStr")] + pub platform: PlatformPair, + + // TODO (@Techassi): Allow some kind of inheritance here (size, disk, ttl, etc...) + /// The time-to-live of the cluster. + pub ttl: String, + + /// Define one or more node groups. + pub node_groups: Vec, +} + +impl Runner { + pub fn validate(&self, runner_name: &str) -> Result<(), RunnerValidationError> { + ensure!( + !self.node_groups.is_empty(), + ZeroNodeGroupsSnafu { + at: format!("runners.{runner_name}") + } + ); + + Ok(()) + } +} + +#[derive(Debug, Snafu)] +pub enum ParsePlatformTripleError { + #[snafu(display("invalid format, expected pair separated by dashes"))] + InvalidFormat, + + #[snafu(display("failed to parse distribution"))] + ParseDistribution { source: strum::ParseError }, +} + +#[derive(Debug)] +pub struct PlatformPair { + pub distribution: Distribution, + // Ideally we want to use SemVer here, but cloud vendors make stupid + // decisions and just use major.minor. + pub version: String, +} + +impl FromStr for PlatformPair { + type Err = ParsePlatformTripleError; + + fn from_str(s: &str) -> Result { + let (distribution, version) = s.split_once('-').context(InvalidFormatSnafu)?; + let distribution = Distribution::from_str(distribution).context(ParseDistributionSnafu)?; + + Ok(PlatformPair { + version: version.to_owned(), + distribution, + }) + } +} + +impl Display for PlatformPair { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{distribution}-{version}", + distribution = self.distribution, + version = self.version, + ) + } +} + +#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, strum::Display, strum::EnumString)] +#[strum(serialize_all = "lowercase")] +#[serde(rename_all = "lowercase")] +pub enum Distribution { + Eks, + Gke, + Aks, + Kind, + K3s, + Rke2, +} + +#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, strum::Display, strum::EnumString)] +#[strum(serialize_all = "kebab-case")] +#[serde(rename_all = "kebab-case")] +pub enum Architecture { + Amd64, + Arm64, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Size { + Small, + Medium, + Large, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct NodeGroup { + name: String, + + #[serde(rename = "arch")] + architecture: Architecture, + + nodes: usize, + size: Size, + + #[serde(rename = "disk-gb")] + disk_size: usize, +} + +#[derive(Debug, Snafu)] +pub enum ConvertNodeGroupError { + #[snafu(display("unknown distribution {distribution}"))] + UnknownDistribution { distribution: Distribution }, + + #[snafu(display("unknown architecture {architecture}"))] + UnknownArchitecture { architecture: Architecture }, +} + +#[derive(Debug, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct ReplicatedNodeGroup<'a> { + instance_type: &'a str, + name: String, + nodes: usize, + disk: usize, +} + +impl<'a> ReplicatedNodeGroup<'a> { + pub fn try_from( + node_group: NodeGroup, + instances: &'a Instances, + distribution: &Distribution, + ) -> Result { + let architectures = + instances + .get(distribution) + .with_context(|| UnknownDistributionSnafu { + distribution: distribution.clone(), + })?; + + let sizes = architectures + .get(&node_group.architecture) + .with_context(|| UnknownArchitectureSnafu { + architecture: node_group.architecture.clone(), + })?; + + let instance_type = match node_group.size { + Size::Small => sizes.small.as_str(), + Size::Medium => sizes.medium.as_str(), + Size::Large => sizes.large.as_str(), + }; + + Ok(Self { + instance_type, + name: node_group.name, + nodes: node_group.nodes, + disk: node_group.disk_size, + }) + } +} diff --git a/tools/interu/src/instances.rs b/tools/interu/src/instances.rs new file mode 100644 index 0000000..f129864 --- /dev/null +++ b/tools/interu/src/instances.rs @@ -0,0 +1,82 @@ +use std::{collections::HashMap, ops::Deref, path::Path}; + +use serde::Deserialize; +use snafu::{ResultExt as _, Snafu}; +use tracing::instrument; + +use crate::config::runner::{Architecture, Distribution}; + +#[derive(Debug, Snafu)] +pub enum Error { + #[snafu(display("failed to read file"))] + ReadFile { source: std::io::Error }, + + #[snafu(display("failed to deserialize file"))] + Deserialize { source: serde_yaml::Error }, +} + +/// Contains distribution to architecture mappings. +#[derive(Debug, Deserialize)] +pub struct Instances(HashMap); + +impl Deref for Instances { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +/// Contains architecture to size mappings. +#[derive(Debug, Deserialize)] +pub struct Architectures(HashMap); + +impl Deref for Architectures { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Instances { + /// Reads and deserializes the mappings from a file located at `path`. + #[instrument(name = "load_instance_mappings_from_file", skip(path), fields(path = %path.as_ref().display()))] + pub fn from_file

(path: P) -> Result + where + P: AsRef, + { + let contents = std::fs::read_to_string(path).context(ReadFileSnafu)?; + + tracing::debug!("deserialize file contents"); + let config: Instances = serde_yaml::from_str(&contents).context(DeserializeSnafu)?; + + Ok(config) + } +} + +// NOTE (@Techassi): Can we somehow re-use the size enum here? +/// Contains size to instance name mappings. +/// +/// This mapping translates our sizes into instance names. Every cloud vendor uses a different +/// scheme to name their instances. It removes the need to specify the exact name and instead +/// enables the use of a simple size: small, medium, and large. +#[derive(Debug, Deserialize)] +pub struct Sizes { + pub small: String, + pub medium: String, + pub large: String, +} + +#[cfg(test)] +mod test { + use std::path::PathBuf; + + use super::*; + use rstest::rstest; + + #[rstest] + fn deserialize(#[files("fixtures/instances.yaml")] path: PathBuf) { + let _ = Instances::from_file(path).unwrap(); + } +} diff --git a/tools/interu/src/main.rs b/tools/interu/src/main.rs new file mode 100644 index 0000000..0ccc6be --- /dev/null +++ b/tools/interu/src/main.rs @@ -0,0 +1,61 @@ +use std::{fs::OpenOptions, io::Write as _}; + +use snafu::{report, ResultExt, Snafu}; + +use crate::{cli::Cli, config::Config, instances::Instances}; + +mod cli; +mod config; +mod instances; + +#[derive(Debug, Snafu)] +enum Error { + #[snafu(display("failed to load config"))] + LoadConfig { source: config::Error }, + + #[snafu(display("failed to determine expanded parameters"))] + DetermineParameters { source: config::Error }, + + #[snafu(display("failed to load instances file"))] + LoadInstances { source: instances::Error }, + + #[snafu(display("failed to write to output file"))] + WriteOutputFile { source: std::io::Error }, +} + +#[report] +fn main() -> Result<(), Error> { + tracing::debug!("setup cli from env"); + let cli: Cli = argh::from_env(); + + tracing::info!("load config and instance mappings file"); + let config = Config::from_file(&cli.config).context(LoadConfigSnafu)?; + let instances = Instances::from_file(&cli.instances).context(LoadInstancesSnafu)?; + + tracing::info!("determine parameters"); + let parameters = config + .determine_parameters(&cli.profile, &instances) + .context(DetermineParametersSnafu)?; + + let parameters = parameters.to_string(); + + // Optionally write the expanded parameters into an output file + if let Some(output_path) = cli.output { + tracing::info!(output_path = %output_path.display(), "write parameters to output file"); + + let mut file = OpenOptions::new() + .append(true) + .open(output_path) + .context(WriteOutputFileSnafu)?; + + file.write(parameters.as_bytes()) + .context(WriteOutputFileSnafu)?; + } + + // Output the expanded parameters to stdout if no --quiet flag was passed + if !cli.quiet { + print!("{parameters}"); + } + + Ok(()) +}