diff --git a/.github/ISSUE_TEMPLATE/release.md b/.github/ISSUE_TEMPLATE/release.md index 50836768f..111e3c4b9 100644 --- a/.github/ISSUE_TEMPLATE/release.md +++ b/.github/ISSUE_TEMPLATE/release.md @@ -7,7 +7,7 @@ labels: ["release"] **Release Date**: -22nd June 2026 **<- WRITE HERE THE UPDATED RELEASE DATE** +21st September 2026 **<- WRITE HERE THE UPDATED RELEASE DATE** ## Release Activities @@ -15,8 +15,15 @@ labels: ["release"] - [ ] Create a new `release-v{major}.{minor}.0` branch. - [ ] Add a new release notes file using the available [template](https://github.com/grafana/k6/blob/master/release%20notes/template.md) to the [repository's `release notes` folder](https://github.com/grafana/k6/blob/master/release%20notes). - - [ ] Go through the potential [dependencies updates](https://github.com/grafana/k6/blob/master/Dependencies.md) and create a dedicated PR if any of them is relevant to this release. - [ ] Create a new `release-v{major}.{minor}.0` branch on the [grafana/k6-DefinitelyTyped](https://github.com/grafana/k6-DefinitelyTyped) fork repository. + - Branch directly off `upstream/master`, so the branch starts from the latest upstream state. Do **not** use GitHub's "Sync fork" button: repository rules on the fork block it and they cannot be amended. + + ``` + git remote add upstream https://github.com/DefinitelyTyped/DefinitelyTyped.git # only once + git fetch upstream + git checkout -b release-v2.3.0 upstream/master + git push -u origin release-v2.3.0 + ``` - [ ] Bump the version in [types/k6/package.json](https://github.com/grafana/k6-DefinitelyTyped/blob/master/types/k6/package.json#L4) to the next one. - [ ] [Lock the conversation](https://docs.github.com/en/communities/moderating-comments-and-conversations/locking-conversations) of the current issue to prevent any abuse or misuse of the issue. Note, it still allow the maintainers to edit the issue's description and to add comments. @@ -25,7 +32,6 @@ labels: ["release"] #### ~ 2 weeks before the release date - [ ] Ensure that all PRs from a release milestone are merged. -- [ ] Ensure experimental modules (if needed) have been updated to their latest version. #### ~ 1 week before the release date @@ -34,11 +40,11 @@ labels: ["release"] - [ ] Ensure all PRs in the `k6` repository, part of the current [milestone](https://github.com/grafana/k6/milestones), have been merged. - [ ] Open a PR with the release notes for the new version - [ ] Ask teams that might have contributed to the release to contribute their notes and review the existing ones. - - [ ] Remember to mention and thank [external contributors](https://github.com/search?q=user%3Agrafana+repo%3Ak6+milestone%3A%22v2.1.0%22+-author%3Amstoykov+-author%3Aoleiade+-author%3Acodebien+-author%3Aandrewslotin+-author%3Ajoanlopez+-author%3Aankur22+-author%3Ainancgumus+-author%3Aszkiba+-author%3AAgnesToulet+-author%3Adependabot%5Bbot%5D+-author%3Arenovate-sh-app%5Bbot%5D&type=pullrequests). (**<- Update the query with the correct milestone version**). + - [ ] Remember to mention and thank [external contributors](https://github.com/search?q=user%3Agrafana+repo%3Ak6+milestone%3A%22v2.3.0%22+-author%3Amstoykov+-author%3Aoleiade+-author%3Acodebien+-author%3Aandrewslotin+-author%3Ajoanlopez+-author%3Aankur22+-author%3Ainancgumus+-author%3Aszkiba+-author%3AAgnesToulet+-author%3Adependabot%5Bbot%5D+-author%3Arenovate-sh-app%5Bbot%5D+-author%3AjanHildebrandt98&type=pullrequests). (**<- Update the query with the correct milestone version**). - [ ] Share the release notes PR with the k6 open-source teams. Request contributions from all affected teams and any other stakeholders involved in the new release. - [ ] Open a separate PR for bumping [the k6 Go project's version](https://github.com/grafana/k6/blob/master/internal/build/version.go#L6). - [ ] Open a PR in the `DefinitelyTyped/DefinitelyTyped` repository using the release branch created in the grafana/k6-DefinitelyTyped fork to update the k6 type definitions for the new release. -- [ ] Open a PR to add the new k6 version to the extensions registry (similar to [this one](https://github.com/grafana/k6-extension-registry/pull/104) for v2.1.0). +- [ ] Open a PR to add the new k6 version to the extensions registry (similar to [this one](https://github.com/grafana/k6-extension-registry/pull/261) for v2.2.0). #### ~ 1 day before the release date @@ -50,12 +56,14 @@ labels: ["release"] - [ ] Open and merge a PR from `main` in the `k6-docs` repository: - [ ] Creating a new `v{major}.{minor}.0` file in the [next release notes folder](https://github.com/grafana/k6-docs/tree/main/docs/sources/k6/next/release-notes) and copy/paste the main sections of the release notes (new features, deprecations and roadmap). - - [ ] Copying the current k6's `next` (including the newly created release note file) to a folder named with the k6 version (e.g., `v2.1.x`). + - Include the front matter (`title`, `menuTitle`, `description`, `weight`), or the page won't appear in the release notes index. + - [ ] Copying the current k6's `next` (including the newly created release note file) to a folder named with the k6 version (e.g., `v2.2.x`). + - [ ] Ensure the file is in `next` and the highest version folder, not only its own. Only the highest tree is published as `latest`, so notes left in an older folder are lost (see [k6-docs#2299](https://github.com/grafana/k6-docs/pull/2299)). Applies to patch releases from maintenance branches too. - [ ] Ensure the `k6` repository release notes PR contains the correct links to the docs. #### In k6 repository -- [ ] Merge the PR bumping [the k6 Go project's version](https://github.com/grafana/k6/blob/master/lib/consts/consts.go#L11-L12). +- [ ] Merge the PR bumping [the k6 Go project's version](https://github.com/grafana/k6/blob/master/internal/build/version.go#L6). - [ ] Merge the release notes PR. - [ ] Pull locally the previously merged changes. - [ ] Create a new long-lived `v{major}.{minor}.x` release branch from the `master` branch. @@ -64,7 +72,7 @@ labels: ["release"] !!! Update the below when making the issue ``` -git checkout master && git pull && git checkout -b v2.1.x && git tag v2.1.0 -m "v2.1.0" && git push origin v2.1.0 +git checkout master && git pull && git checkout -b v2.3.x && git push -u origin v2.3.x && git tag v2.3.0 -m "v2.3.0" && git push origin v2.3.0 ``` #### In k6-extension-registry @@ -80,5 +88,4 @@ git checkout master && git pull && git checkout -b v2.1.x && git tag v2.1.0 -m " ## Wrapping Release - [ ] Ensure the `DefinitelyTyped/DefinitelyTyped` PR(s) are merged. -- [ ] Ensure to sync `grafana/k6-DefinitelyTyped` fork with the latest changes from the `DefinitelyTyped/DefinitelyTyped` repository. - [ ] Update the k6 repository's `.github/ISSUE_TEMPLATE/release.md` in the event steps from this checklist were incorrect or missing. diff --git a/.github/actions/lint/action.yml b/.github/actions/lint/action.yml deleted file mode 100644 index 9440090a1..000000000 --- a/.github/actions/lint/action.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: golangci-lint-k6 -description: Run golangci-lint with the k6 ruleset - -runs: - using: composite - steps: - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.26.x - check-latest: true - - name: Get golangci-lint version and download rules - shell: bash - env: - GITHUB_ACTION_REF: ${{ github.action_ref }} - GITHUB_HEAD_REF: ${{ github.head_ref }} - REPO_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }} - run: | - branch="${GITHUB_HEAD_REF:-master}" - if [ "${REPO_FULL_NAME}" != "grafana/k6" ]; then - branch="${GITHUB_ACTION_REF:-master}" - fi - rules_url="https://raw.githubusercontent.com/grafana/k6/${branch}/.golangci.yml" - echo "Downloading '${rules_url}' ..." - curl --silent --show-error --fail --no-location "${rules_url}" --output "${{ github.action_path }}/.golangci.yml" - - echo "GolangCIVersion=$(head -n 1 "${{ github.action_path }}/.golangci.yml" | tr -d '# ')" >> "${GITHUB_OUTPUT}" - id: getenv - - name: golangci-lint - uses: golangci/golangci-lint-action@1e7e51e771db61008b38414a730f564565cf7c20 # v9.2.0 - with: - version: ${{ steps.getenv.outputs.GolangCIVersion }} - args: "--config=${{ github.action_path }}/.golangci.yml" diff --git a/.github/actions/test-common/action.yml b/.github/actions/test-common/action.yml index 78c79342c..4e0856aa9 100644 --- a/.github/actions/test-common/action.yml +++ b/.github/actions/test-common/action.yml @@ -18,7 +18,7 @@ runs: steps: - name: Install Go if: ${{ inputs.go_version != 'gotip' }} - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 + uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6 with: go-version: ${{ inputs.go_version }} check-latest: true diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml index 7a9f22a10..ec95d6122 100644 --- a/.github/actions/test/action.yml +++ b/.github/actions/test/action.yml @@ -39,15 +39,4 @@ runs: unset args[2] fi fi - if [[ ${USE_GOTIP} == 'true' ]]; then - # Under Go 1.27+, golang.org/x/net/http2 switches to a "wrapping - # implementation" that delegates to net/http. With it, ConfigureTransport - # no longer forces HTTP/2 over custom dialers, and HTTP/2 errors surface as - # unexported net/http/internal/http2 types that lib/netext/httpext can no - # longer classify (only StreamError has an upstream errors.As bridge; - # ConnectionError and GoAwayError do not). Force the legacy x/net - # implementation until k6 adapts. See Dependencies.md / the http2 error - # handling in lib/netext/httpext/error_codes.go. - args+=("-tags" "http2legacy") - fi go test "${args[@]}" -timeout 1600s `go list ./... | grep -v browser/tests` diff --git a/.github/scripts/check-feature-briefs.sh b/.github/scripts/check-feature-briefs.sh new file mode 100644 index 000000000..b36ef04fa --- /dev/null +++ b/.github/scripts/check-feature-briefs.sh @@ -0,0 +1,156 @@ +#!/usr/bin/env bash +# check-feature-briefs.sh — validate feature briefs in docs/features/ +# +# Usage: +# ./check-feature-briefs.sh # check all briefs (local default) +# ./check-feature-briefs.sh path/to/a.md # check specific file(s) (used by CI) +# +# Rules enforced: +# 1. Total prose ≤ 1,200 words (hard limit, error) +# Total prose ≤ 800 words (soft target, warning) +# 2. "## Problem" section ≤ 3 sentences (heuristic: counts . ! ? terminators) +# 3. "## Cost of inaction" section must be present and non-empty +# +# Prose is defined as all text after stripping: +# - Markdown heading lines (lines starting with #) +# - HTML comments (, including multi-line) +# +# README.md and 000-TEMPLATE.md are always excluded. + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" +BRIEFS_DIR="$REPO_ROOT/docs/features" + +SKIP_PATTERN="README\.md$|000-TEMPLATE\.md$" + +# ────────────────────────────────────────────── +# Helpers +# ────────────────────────────────────────────── + +# strip_comments FILE — print file contents with HTML comments removed +strip_comments() { + awk ' + { + line = $0 + while (1) { + if (in_comment) { + idx = index(line, "-->") + if (idx == 0) { line = ""; break } + line = substr(line, idx + 3) + in_comment = 0 + } else { + idx = index(line, "") + if (end_idx > 0) { + line = before substr(rest, end_idx + 3) + } else { + printf "%s", before + in_comment = 1 + line = "" + break + } + } + } + print line + } + ' in_comment=0 "$1" +} + +# prose_text FILE — file with comments and heading lines stripped +prose_text() { + strip_comments "$1" | grep -v '^[[:space:]]*#' +} + +# section_text FILE HEADING — text of the section starting at HEADING, up to next ## heading +section_text() { + local file="$1" + local heading="$2" + strip_comments "$file" | awk -v heading="$heading" ' + BEGIN { found=0 } + /^##[[:space:]]/ { + if (found) exit + if (tolower($0) ~ tolower(heading)) { found=1; next } + } + found { print } + ' +} + +# ────────────────────────────────────────────── +# Build the file list +# ────────────────────────────────────────────── + +if [[ $# -gt 0 ]]; then + files=("$@") +else + # No args: scan all .md files in docs/features/ + mapfile -t files < <(find "$BRIEFS_DIR" -maxdepth 1 -name '*.md' | sort) +fi + +# Filter out excluded files +filtered=() +for f in "${files[@]}"; do + if echo "$f" | grep -qE "$SKIP_PATTERN"; then + continue + fi + if [[ ! -f "$f" ]]; then + echo "::warning::File not found, skipping: $f" + continue + fi + filtered+=("$f") +done + +if [[ ${#filtered[@]} -eq 0 ]]; then + echo "No feature briefs to check." + exit 0 +fi + +# ────────────────────────────────────────────── +# Validate each brief +# ────────────────────────────────────────────── + +failed=0 + +for f in "${filtered[@]}"; do + echo "Checking: $f" + + # 1. Word count ----------------------------------------------------------- + word_count=$(prose_text "$f" | wc -w | tr -d '[:space:]') + + if [[ "$word_count" -gt 1200 ]]; then + echo "::error file=$f::Word count is $word_count (limit: 1200). Trim the prose." + failed=1 + elif [[ "$word_count" -gt 800 ]]; then + echo "::warning file=$f::Word count is $word_count (soft target: ≤800). Consider trimming." + fi + + # 2. Problem sentence count ----------------------------------------------- + # Heuristic: count sentence-terminating punctuation (. ! ?) that is followed + # by whitespace or end of string. HTML comments already stripped by section_text. + problem_text=$(section_text "$f" "## Problem") + sentence_count=$(echo "$problem_text" | grep -oE '[.!?]([[:space:]]|$)' | wc -l | tr -d '[:space:]') + + if [[ "$sentence_count" -gt 3 ]]; then + echo "::error file=$f::Problem statement has ~$sentence_count sentences (limit: 3)." + failed=1 + fi + + # 3. Cost of inaction — present and non-empty ----------------------------- + coi_text=$(section_text "$f" "## Cost of inaction" | tr -d '[:space:]') + + if [[ -z "$coi_text" ]]; then + echo "::error file=$f::\"Cost of inaction\" section is missing or empty. It is required." + failed=1 + fi + +done + +if [[ "$failed" -ne 0 ]]; then + exit 1 +fi + +echo "All briefs OK." diff --git a/.github/workflows/browser_e2e.yml b/.github/workflows/browser_e2e.yml deleted file mode 100644 index 3804deaa0..000000000 --- a/.github/workflows/browser_e2e.yml +++ /dev/null @@ -1,80 +0,0 @@ -name: E2E -on: - # Enable manually triggering this workflow via the API or web UI - workflow_dispatch: - push: - branches: - - master - pull_request: - schedule: - # At 06:00 AM UTC from Monday through Friday - - cron: '0 6 * * 1-5' - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - test: - strategy: - fail-fast: false - matrix: - go: [stable, tip] - platform: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.x - check-latest: true - cache: false # against cache-poisoning - - name: Install Go tip - if: matrix.go == 'tip' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh release download ${{ matrix.platform }} --repo grafana/gotip --pattern 'go.zip' - unzip go.zip -d $HOME/sdk - echo "GOROOT=$HOME/sdk/gotip" >> "$GITHUB_ENV" - echo "GOPATH=$HOME/go" >> "$GITHUB_ENV" - echo "$HOME/go/bin" >> "$GITHUB_PATH" - echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH" - - name: Install chromium - # This is only needed on arm images, chromium comes preinstalled on amd64 runner images. - if: contains(matrix.platform, 'arm') - run: | - sudo apt update && sudo apt install chromium-browser - chromium --version - - name: Build k6 - run: | - which go - go version - - go build . - ./k6 version - - name: Run E2E tests - run: | - set -x - if [ "$RUNNER_OS" == "Linux" ] && [ "$RUNNER_ARCH" == "X64" ]; then - export K6_BROWSER_EXECUTABLE_PATH=/usr/bin/google-chrome - fi - export K6_BROWSER_HEADLESS=true - export K6_NO_USAGE_REPORT=true - for f in examples/browser/*.js; do - if [ "$f" == "examples/browser/hosts.js" ] && [ "$RUNNER_OS" == "Windows" ]; then - echo "skipping $f on Windows" - continue - fi - ./k6 run -q "$f" - done - - name: Check screenshot - # TODO: Do something more sophisticated? - run: test -s screenshot.png diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index ac0b2ad53..000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,692 +0,0 @@ -name: Build -on: - workflow_dispatch: - inputs: - k6_version: - description: 'The version of the release, it must use the semantic versioning format with the v prefix. It is a development release so it is suggested to append a build metadata (e.g. v0.38.0-dev).' - required: true - go_version: - description: 'Go version for building binaries' - default: '1.x' - required: true - push: - branches: - - master - tags: - - v* - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -env: - APP_NAME: "k6" - DOCKER_IMAGE_ID: "us-docker.pkg.dev/grafanalabs-global/dockerhub-k6-prod-mirror/k6" - GHCR_IMAGE_ID: ${{ github.repository }} - DEFAULT_GO_VERSION: "1.26.x" - -jobs: - configure: - runs-on: ubuntu-latest - outputs: - k6_version: ${{ steps.get_k6_version.outputs.k6_version }} - go_version: ${{ steps.get_go_version.outputs.go_version }} - sign_windows_artifacts: ${{ steps.determine_windows_signing.outputs.sign_windows_artifacts }} - major: ${{ steps.classify_release.outputs.major }} - is_prerelease: ${{ steps.classify_release.outputs.is_prerelease }} - is_latest_stable: ${{ steps.classify_release.outputs.is_latest_stable }} - is_latest_in_major: ${{ steps.classify_release.outputs.is_latest_in_major }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - fetch-depth: 0 - persist-credentials: false - - name: Get the k6 version - id: get_k6_version - env: - INPUT_K6_VERSION: ${{ github.event.inputs.k6_version }} - run: | - set -x # Show exactly what commands are executed - if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]] && [[ "${INPUT_K6_VERSION}" != "" ]]; then - VERSION="${INPUT_K6_VERSION}" - echo "Building custom dev build with version '${VERSION}' from manual workflow_dispatch..." - elif [[ "${GITHUB_REF}" =~ ^refs/tags/v.+$ ]]; then - VERSION="${GITHUB_REF##*/}" - echo "Building real version tag '${GITHUB_REF}', parsed '${VERSION}' as the actual version..." - else - VERSION="$(git describe --tags --always --long --dirty)" - echo "Building a non-version ref '${GITHUB_REF}', use '${VERSION}' as the version instead..." - fi - echo "VERSION=${VERSION}" - echo "k6_version=${VERSION}" >> $GITHUB_OUTPUT - - name: Get the used Go version - id: get_go_version - env: - INPUT_GO_VERSION: ${{ github.event.inputs.go_version }} - run: | - set -x # Show exactly what commands are executed - if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]] && [[ "${INPUT_GO_VERSION}" != "" ]]; then - GO_VERSION="${INPUT_GO_VERSION}" - echo "Using custom Go version '${GO_VERSION}' from manual workflow_dispatch..." - else - GO_VERSION="${DEFAULT_GO_VERSION}" - echo "Using the default Go version '${GO_VERSION}'..." - fi - echo "GO_VERSION=${GO_VERSION}" - echo "go_version=${GO_VERSION}" >> $GITHUB_OUTPUT - - # Secrets are unavailable when building from project forks, so this - # will fail for external PRs, even if we wanted to do it. And we don't. - # We are only going to sign packages that are built from the default branch - # or a version tag, or manually triggered dev builds, so we have enough - # assurance that package signing works, but don't sign every PR build. - - name: Determine whether to sign the Windows artifacts - id: determine_windows_signing - env: - SIGN_FILES: ${{ github.ref_name == github.event.repository.default_branch || startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch' }} - run: | - set -x # Show exactly what commands are executed - if [[ "${SIGN_FILES}" == "true" ]]; then - echo "Windows artifacts will be signed" - sign_windows_artifacts="true" - else - echo "Windows artifacts will not be signed" - sign_windows_artifacts="false" - fi - echo "sign_windows_artifacts=${sign_windows_artifacts}" >> ${GITHUB_OUTPUT} - - # Classify the release so we can decide whether this build should: - # - move the Docker :latest tag (only the highest stable tag overall) - # - move the Docker :vN floating major tag (any stable tag, per major) - # - be marked as the GitHub "Latest release" or as a pre-release - # This prevents pre-releases (e.g. -rc) and maintenance releases on - # older majors (e.g. v1.x after v2.0.0 ships) from claiming "latest". - - name: Classify release - id: classify_release - env: - VERSION: ${{ steps.get_k6_version.outputs.k6_version }} - run: | - set -x - MAJOR="" - IS_PRERELEASE=false - IS_LATEST_STABLE=false - IS_LATEST_IN_MAJOR=false - if [[ "${GITHUB_REF}" =~ ^refs/tags/v.+$ ]]; then - MAJOR=$(echo "${VERSION}" | sed -E 's/^v([0-9]+).*/\1/') - if [[ "${VERSION}" == *-* ]]; then - IS_PRERELEASE=true - fi - # Highest stable tag in the repo (excludes anything with a - # pre-release identifier like -rc, -beta, -dev). - MAX_STABLE=$(git tag --sort=-v:refname | grep -vE '-' | head -n1 || true) - if [[ "${IS_PRERELEASE}" == "false" && "${VERSION}" == "${MAX_STABLE}" ]]; then - IS_LATEST_STABLE=true - fi - # Highest stable tag within this tag's major. Needed because - # we can ship multiple bugfix releases on an older major - # (e.g. v1.4.2 after v1.5.0 is already out on the v1 line): - # only the newest one on that major should move :vN. - MAX_STABLE_IN_MAJOR=$(git tag --sort=-v:refname \ - | grep -vE '-' \ - | grep -E "^v${MAJOR}\." \ - | head -n1 || true) - if [[ "${IS_PRERELEASE}" == "false" && "${VERSION}" == "${MAX_STABLE_IN_MAJOR}" ]]; then - IS_LATEST_IN_MAJOR=true - fi - fi - echo "major=${MAJOR}" >> ${GITHUB_OUTPUT} - echo "is_prerelease=${IS_PRERELEASE}" >> ${GITHUB_OUTPUT} - echo "is_latest_stable=${IS_LATEST_STABLE}" >> ${GITHUB_OUTPUT} - echo "is_latest_in_major=${IS_LATEST_IN_MAJOR}" >> ${GITHUB_OUTPUT} - - build: - runs-on: ubuntu-latest - needs: [configure] - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - fetch-depth: 0 - persist-credentials: false - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: ${{ needs.configure.outputs.go_version }} - check-latest: true - cache: false # against cache-poisoning - - name: Install nfpm (dep and rpm package builder) - run: | - go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.16.0 - - name: Install goversioninfo (.syso file creator) - run: | - go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@v1.4.0 - - name: Generate Windows binary metadata (.syso files) - run: | - IFS=. read -a version_parts <<< "${VERSION#v}" - IFS=- read -a version_patch <<< "${version_parts[2]}" - - # Need a blank versioninfo.json for the CLI overrides to work. - echo '{}' > versioninfo.json - set -x - goversioninfo -64 \ - -platform-specific=true \ - -charset="1200" \ - -company="Raintank Inc. d.b.a. Grafana Labs" \ - -copyright="© Raintank Inc. d.b.a. Grafana Labs. Licensed under AGPL." \ - -description="A modern load testing tool, using Go and JavaScript" \ - -icon=packaging/k6.ico \ - -internal-name="k6" \ - -original-name="k6.exe" \ - -product-name="k6" \ - -translation="0x0409" \ - -ver-major="${version_parts[0]}" \ - -ver-minor="${version_parts[1]}" \ - -ver-patch="${version_patch[0]}" \ - -special-build=$(IFS='-'; echo "${version_patch[*]:1}";) \ - -product-version="${VERSION#v}" - - set +x - ls -lah | grep -i syso - - - name: Build - run: | - go version - ./build-release.sh "dist" "${VERSION}" - - name: Upload artifacts - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - name: binaries - path: dist/ - retention-days: 7 - - build-docker: - permissions: - contents: read - packages: write - id-token: write - runs-on: ubuntu-latest - needs: [configure] - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - fetch-depth: 0 - persist-credentials: false - - name: Build - run: | - docker buildx create \ - --name multibuilder \ - --platform linux/amd64,linux/arm64 \ - --bootstrap --use - docker buildx build \ - --target release \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID . - - name: Check - run: | - docker buildx build --load -t $DOCKER_IMAGE_ID . - # Assert that simple cases works for the new built image - docker run $DOCKER_IMAGE_ID version - docker run $DOCKER_IMAGE_ID --help - docker run $DOCKER_IMAGE_ID help - docker run $DOCKER_IMAGE_ID run --help - docker run $DOCKER_IMAGE_ID inspect --help - - name: Login to GAR (Docker Hub mirror) - if: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }} - uses: grafana/shared-workflows/actions/login-to-gar@c7ad5f57693ac858c698d95783685a9cbfd91223 # login-to-gar/v1.0.1 - with: - registry: us-docker.pkg.dev - - name: Login to GitHub Container Registry - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 - env: - GITHUB_ACTOR: ${{ github.actor }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - registry: ghcr.io - username: ${{ env.GITHUB_ACTOR }} - password: ${{ env.GITHUB_TOKEN }} - if: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }} - - name: Publish k6:master images - if: ${{ github.ref == 'refs/heads/master' }} - run: | - echo "Publish $GHCR_IMAGE_ID:master* images" - docker buildx build --push \ - --target release \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:master \ - -t ghcr.io/$GHCR_IMAGE_ID:master . - docker buildx build --push \ - --target with-browser \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:master-with-browser \ - -t ghcr.io/$GHCR_IMAGE_ID:master-with-browser . - - name: Publish tagged version images - if: ${{ startsWith(github.ref, 'refs/tags/v') }} - run: | - VERSION="${VERSION#v}" - echo "Publish $GHCR_IMAGE_ID:$VERSION images" - docker buildx build --push \ - --target release \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:$VERSION \ - -t ghcr.io/$GHCR_IMAGE_ID:$VERSION . - docker buildx build --push \ - --target with-browser \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:$VERSION-with-browser \ - -t ghcr.io/$GHCR_IMAGE_ID:$VERSION-with-browser . - # Move the floating major tag (e.g. :v1, :v2) only when this tag is - # the highest stable tag on its major. Pre-releases never move it, - # and a bugfix released on an older line within the same major - # (e.g. v1.4.2 after v1.5.0) does not regress :vN. - - name: Publish floating major tag - if: ${{ needs.configure.outputs.is_latest_in_major == 'true' }} - env: - MAJOR: ${{ needs.configure.outputs.major }} - run: | - echo "Publish $GHCR_IMAGE_ID:v$MAJOR images" - docker buildx build --push \ - --target release \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:v$MAJOR \ - -t ghcr.io/$GHCR_IMAGE_ID:v$MAJOR . - docker buildx build --push \ - --target with-browser \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:v$MAJOR-with-browser \ - -t ghcr.io/$GHCR_IMAGE_ID:v$MAJOR-with-browser . - # Only the highest stable tag in the repo gets :latest. Maintenance - # releases on older majors (e.g. v1.x after v2.0.0 ships) and any - # pre-release must not move :latest. - - name: Publish :latest - if: ${{ needs.configure.outputs.is_latest_stable == 'true' }} - run: | - echo "Publish $GHCR_IMAGE_ID:latest" - docker buildx build --push \ - --target release \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:latest \ - -t ghcr.io/$GHCR_IMAGE_ID:latest . - docker buildx build --push \ - --target with-browser \ - --platform linux/amd64,linux/arm64 \ - -t $DOCKER_IMAGE_ID:latest-with-browser \ - -t ghcr.io/$GHCR_IMAGE_ID:latest-with-browser . - - # Forks, PRs etc. won't actually sign the binary, but the workflow will run most of the same steps as - # GitHub Actions workflows don't support conditional `needs` so we have to run the signing step unconditionally. - sign-binaries: - permissions: - contents: read - actions: read - id-token: write # Required for Vault - - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - - environment: - name: azure-trusted-signing - - runs-on: windows-latest - defaults: - run: - shell: pwsh - needs: [configure, build] - outputs: - binary_artifact_name: ${{ steps.assign-artifact-names.outputs.binary-artifact-name }} - windows_binary_artifact_name: ${{ steps.assign-artifact-names.outputs.windows-binary-artifact-name }} - steps: - - name: Download binaries - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: binaries - path: dist - - - name: Unzip Windows binary - run: | - Expand-Archive -Path ".\dist\k6-${env:VERSION}-windows-amd64.zip" -DestinationPath .\packaging\ - - - name: Upload artifact for Windows installer build - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - name: windows-binary - path: 'packaging/k6-${{ env.VERSION }}-windows-amd64/k6.exe' - retention-days: 7 - if-no-files-found: error - - - name: Get secrets for Azure Trusted Signing - uses: grafana/shared-workflows/actions/get-vault-secrets@a53fc80bc30b0a16a262520465db899fa3af08b7 # get-vault-secrets/v1.3.2 - id: get-signing-secrets - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - export_env: false - repo_secrets: | - client-id=azure-trusted-signing:client-id - subscription-id=azure-trusted-signing:subscription-id - tenant-id=azure-trusted-signing:tenant-id - - - name: Sign Windows binary - uses: grafana/shared-workflows/actions/azure-trusted-signing@fd75379254f1ff2d0a1f1c12b329a9008863ad82 # azure-trusted-signing/v1.0.2 - id: sign-artifacts - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - application-description: 'Grafana k6' - artifact-to-sign: 'windows-binary' - azure-client-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).client-id }} - azure-subscription-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).subscription-id }} - azure-tenant-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).tenant-id }} - signed-artifact-name: 'windows-binary-signed' - - - name: Download signed Windows binary - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - name: ${{ steps.sign-artifacts.outputs.artifact-name }} - path: 'packaging/k6-${{ env.VERSION }}-windows-amd64' - - # Re-zip the signed Windows binary to replace the original unsigned version - - name: Zip signed Windows binary - if: needs.configure.outputs.sign_windows_artifacts == 'true' - run: | - Compress-Archive -Path ".\packaging\*" -DestinationPath ".\dist\k6-${env:VERSION}-windows-amd64.zip" -Force - - - name: Upload signed artifacts - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - name: binaries-signed - path: dist/ - retention-days: 7 - if-no-files-found: error - - - name: Assign artifact name for Windows binary for installer build - id: assign-artifact-names - env: - BINARY_ARTIFACT_NAME: ${{ needs.configure.outputs.sign_windows_artifacts == 'true' && 'binaries-signed' || 'binaries' }} - WINDOWS_BINARY_ARTIFACT_NAME: ${{ needs.configure.outputs.sign_windows_artifacts == 'true' && steps.sign-artifacts.outputs.artifact-name || 'windows-binary' }} - run: | - echo "binary-artifact-name=${env:BINARY_ARTIFACT_NAME}" >> ${env:GITHUB_OUTPUT} - echo "windows-binary-artifact-name=${env:WINDOWS_BINARY_ARTIFACT_NAME}" >> ${env:GITHUB_OUTPUT} - - package: - permissions: - contents: read - actions: read - - runs-on: windows-latest - defaults: - run: - shell: pwsh - needs: [configure, build, sign-binaries] - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Install pandoc - uses: crazy-max/ghaction-chocolatey@dff3862348493b11fba2fbc49147b6d2dfe09b66 # v4.0.0 - with: - args: install -y pandoc - - name: Install wix tools - run: | - curl -Lso wix311-binaries.zip https://github.com/wixtoolset/wix3/releases/download/wix3112rtm/wix311-binaries.zip - Expand-Archive -Path .\wix311-binaries.zip -DestinationPath .\wix311\ - echo "$pwd\wix311" | Out-File -FilePath $env:GITHUB_PATH -Append - - name: Download Windows binary - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: ${{ needs.sign-binaries.outputs.windows_binary_artifact_name }} - path: packaging - - - name: Create the MSI package - run: | - $env:VERSION = $env:VERSION -replace 'v(\d+\.\d+\.\d+).*','$1' - pandoc -s -f markdown -t rtf -o packaging\LICENSE.rtf LICENSE.md - cd .\packaging - candle.exe -arch x64 "-dVERSION=${env:VERSION}" k6.wxs - light.exe -ext WixUIExtension k6.wixobj - - - name: Rename MSI package - # To keep it consistent with the other artifacts - run: move "packaging\k6.msi" "packaging\k6-${env:VERSION}-windows-amd64.msi" - - - name: Upload Windows installer - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 - with: - name: binaries-windows - path: | - packaging/k6-*.msi - retention-days: 7 - if-no-files-found: error - - # Forks, PRs etc. won't actually sign the installer, but the workflow will run most of the same steps as - # GitHub Actions workflows don't support conditional `needs` so we have to run the signing step unconditionally. - sign-packages: - permissions: - actions: read - contents: read - id-token: write # Required for Vault - - environment: - name: azure-trusted-signing - - outputs: - artifact_name: ${{ steps.assign-artifact-name.outputs.artifact-name }} - - runs-on: windows-latest - defaults: - run: - shell: pwsh - needs: [configure, package] - steps: - - name: Download Windows artifacts - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - name: binaries-windows - path: packaging - - - name: Get secrets for Azure Trusted Signing - uses: grafana/shared-workflows/actions/get-vault-secrets@a53fc80bc30b0a16a262520465db899fa3af08b7 # get-vault-secrets/v1.3.2 - id: get-signing-secrets - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - export_env: false - repo_secrets: | - client-id=azure-trusted-signing:client-id - subscription-id=azure-trusted-signing:subscription-id - tenant-id=azure-trusted-signing:tenant-id - - - name: Sign Windows installer - uses: grafana/shared-workflows/actions/azure-trusted-signing@fd75379254f1ff2d0a1f1c12b329a9008863ad82 # azure-trusted-signing/v1.0.2 - id: sign-artifacts - if: needs.configure.outputs.sign_windows_artifacts == 'true' - with: - application-description: 'Grafana k6' - artifact-to-sign: 'binaries-windows' - azure-client-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).client-id }} - azure-subscription-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).subscription-id }} - azure-tenant-id: ${{ fromJSON(steps.get-signing-secrets.outputs.secrets).tenant-id }} - signed-artifact-name: 'binaries-windows-signed' - - - name: Assign artifact name for Windows installer - id: assign-artifact-name - env: - ARTIFACT_NAME: ${{ needs.configure.outputs.sign_windows_artifacts == 'true' && steps.sign-artifacts.outputs.artifact-name || 'binaries-windows' }} - run: | - echo "artifact-name=${env:ARTIFACT_NAME}" >> ${env:GITHUB_OUTPUT} - - publish-github: - runs-on: ubuntu-latest - needs: [configure, sign-binaries, sign-packages] - if: ${{ startsWith(github.ref, 'refs/tags/v') && github.event_name != 'workflow_dispatch' }} - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - permissions: - actions: read - contents: write - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Download binaries - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: ${{ needs.sign-binaries.outputs.binary_artifact_name }} - path: dist - - name: Download Windows binaries - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: ${{ needs.sign-packages.outputs.artifact_name }} - path: dist - - name: Generate checksum file - run: cd dist && sha256sum * > "k6-${VERSION}-checksums.txt" - - name: Anchore SBOM Action - continue-on-error: true - uses: anchore/sbom-action@e22c389904149dbc22b58101806040fa8d37a610 # v0.24.0 - with: - artifact-name: k6-${{ env.VERSION }}-spdx.json - upload-release-assets: false - output-file: dist/k6-${{ env.VERSION }}-spdx.json - - name: Create release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - IS_LATEST_STABLE: ${{ needs.configure.outputs.is_latest_stable }} - IS_PRERELEASE: ${{ needs.configure.outputs.is_prerelease }} - run: | - set -x - assets=() - for asset in ./dist/*; do - assets+=("$asset") - done - # --latest=false prevents pre-releases and maintenance releases on - # older majors from being shown as the "Latest release" on GitHub. - extra_args=( "--latest=${IS_LATEST_STABLE}" ) - if [[ "${IS_PRERELEASE}" == "true" ]]; then - extra_args+=( "--prerelease" ) - fi - gh release create "$VERSION" "${assets[@]}" \ - --target "$GITHUB_SHA" \ - -F "./release notes/${VERSION}.md" \ - "${extra_args[@]}" - - submit-winget-manifest: - needs: [configure, publish-github] - runs-on: windows-2025 - permissions: - contents: read - id-token: write - steps: - - - name: Install WingetCreate - shell: pwsh - run: | - Import-Module Appx -UseWindowsPowerShell - $appxBundleFile = Join-Path ${env:RUNNER_TEMP} "wingetcreate.msixbundle" - Invoke-WebRequest https://aka.ms/wingetcreate/latest/msixbundle -OutFile $appxBundleFile - Add-AppxPackage $appxBundleFile - - - name: Get WinGet token - uses: grafana/shared-workflows/actions/get-vault-secrets@a53fc80bc30b0a16a262520465db899fa3af08b7 # get-vault-secrets/v1.3.2 - id: get-token - with: - export_env: false - common_secrets: | - token=winget-packages:token - - - name: Submit WinGet Manifest - env: - PACKAGE_ID: GrafanaLabs.k6 - PACKAGE_VERSION: ${{ needs.configure.outputs.k6_version }} - WINGET_CREATE_GITHUB_TOKEN: ${{ fromJSON(steps.get-token.outputs.secrets).token }} - shell: pwsh - run: | - wingetcreate token --store - wingetcreate update ${env:PACKAGE_ID} ` - --urls "${env:GITHUB_SERVER_URL}/${env:GITHUB_REPOSITORY}/releases/download/${env:PACKAGE_VERSION}/k6-${env:PACKAGE_VERSION}-windows-amd64.msi" ` - --version ${env:PACKAGE_VERSION}.TrimStart("v") ` - --submit - - publish-packages: - runs-on: ubuntu-latest - needs: [configure, sign-binaries, sign-packages] - if: ${{ startsWith(github.ref, 'refs/tags/v') && github.event_name != 'workflow_dispatch' }} - env: - VERSION: ${{ needs.configure.outputs.k6_version }} - permissions: - actions: read - contents: read - packages: write - id-token: write # Required for Vault - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Download binaries - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: ${{ needs.sign-binaries.outputs.binary_artifact_name }} - path: dist - - name: Download Windows binaries - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 - with: - name: ${{ needs.sign-packages.outputs.artifact_name }} - path: dist - - name: Rename binaries - # To be consistent with the filenames used in dl.k6.io - run: | - mv "dist/k6-$VERSION-windows-amd64.msi" "dist/k6-$VERSION-amd64.msi" - mv "dist/k6-$VERSION-linux-amd64.rpm" "dist/k6-$VERSION-amd64.rpm" - mv "dist/k6-$VERSION-linux-amd64.deb" "dist/k6-$VERSION-amd64.deb" - - uses: grafana/shared-workflows/actions/get-vault-secrets@a53fc80bc30b0a16a262520465db899fa3af08b7 # get-vault-secrets/v1.3.2 - with: - repo_secrets: | - IAM_ROLE_ARN=deploy:packager-iam-role - AWS_CF_DISTRIBUTION=cloudfront:AWS_CF_DISTRIBUTION - PGP_SIGN_KEY_PASSPHRASE=pgp:PGP_SIGN_KEY_PASSPHRASE - PGP_SIGN_KEY=pgp:PGP_SIGN_KEY - S3_BUCKET=s3:AWS_S3_BUCKET - - uses: grafana/shared-workflows/actions/aws-auth@85022085ed5314601c05d10e846de56bdd71e369 # aws-auth/v1.0.3 - with: - aws-region: "us-east-2" - role-arn: ${{ env.IAM_ROLE_ARN }} - set-creds-in-environment: true - - name: Setup docker compose environment - run: | - cat > packaging/.env < packaging/sign-key.gpg - - name: Login to GitHub Container Registry - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 - env: - GITHUB_ACTOR: ${{ github.actor }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - registry: ghcr.io - username: ${{ env.GITHUB_ACTOR }} - password: ${{ env.GITHUB_TOKEN }} - - name: Publish packages - run: | - cd packaging - docker compose pull packager - docker compose run --rm packager diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 0fa9a59b9..000000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: "CodeQL" - -on: - push: - branches: [ master ] - -permissions: - contents: read - security-events: write - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - steps: - - name: Checkout repo - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - - name: Initialize CodeQL - uses: github/codeql-action/init@68bde559dea0fdcac2102bfdf6230c5f70eb485e # v4.35.4 - with: - languages: go - - - name: CodeQL Analysis - uses: github/codeql-action/analyze@68bde559dea0fdcac2102bfdf6230c5f70eb485e # v4.35.4 diff --git a/.github/workflows/issue-auto-assign.yml b/.github/workflows/issue-auto-assign.yml deleted file mode 100644 index 1ca95166f..000000000 --- a/.github/workflows/issue-auto-assign.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: "Auto assign maintainer to issue" -on: - # makes this workflow re-usable - workflow_call: - issues: - types: [opened] - issue_comment: - types: [created] - -permissions: - issues: write - -jobs: - assign-user: - runs-on: ubuntu-latest - # issue_comment triggers for both, issues and prs, - # as we need to run only on issues, it filter out prs. - if: ${{ !github.event.issue.pull_request }} - steps: - - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 - with: - script: | - const assignees = ['ankur22', 'inancgumus', 'joanlopez', 'mstoykov', 'szkiba']; - const teamMembers = assignees.concat(['andrewslotin']); - const assigneeCount = 1; - - // Do not automatically assign users if someone was already assigned or it was opened by a team member - if (context.payload.issue.assignees.length > 0 || teamMembers.includes(context.actor)) { - return; - } - const crypto = require("node:crypto"); - - const getNRandom = (n, array) => { - let result = new Array(); - for (;n > 0 && array.length > 0; n--) { - const chosen = array[crypto.randomInt(array.length)]; - result.push(chosen); - array = array.filter(el => el != chosen); - } - return result; - } - - github.rest.issues.addAssignees({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - assignees: getNRandom(assigneeCount, assignees), - }); - github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - labels: ["triage"] - }); diff --git a/.github/workflows/lean.yml b/.github/workflows/lean.yml new file mode 100644 index 000000000..ec2c863be --- /dev/null +++ b/.github/workflows/lean.yml @@ -0,0 +1,51 @@ +name: Lean CI + +on: + pull_request: + branches: [master] + push: + branches: [master] + workflow_dispatch: + +permissions: + contents: read + +jobs: + build: + name: Build lean release binaries + runs-on: ubuntu-24.04 + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6 + with: + go-version-file: go.mod + cache: false + - run: ./build-linux-binaries.sh + + test: + name: Test lean fork + runs-on: ubuntu-24.04 + timeout-minutes: 40 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6 + with: + go-version-file: go.mod + cache: false + - name: Run lean CLI, runtime, transport and output tests + run: | + go test -race -tags lean -p 2 -parallel 2 -timeout 15m \ + ./internal/cmd ./internal/cmd/tests ./internal/api ./internal/js ./internal/features \ + ./internal/js/modules/k6 ./internal/output/breakingit ./lib/netext/httpext ./metrics ./output + - name: Run Chromium group regression tests + env: + K6_BROWSER_EXECUTABLE_PATH: /usr/bin/google-chrome + K6_BROWSER_ARGS: no-sandbox + run: | + go test -race -tags lean -parallel 1 -timeout 15m \ + -run '^(TestAsyncGroup|TestBrowserGroupTransactionOutcomes)' ./internal/js/modules/k6/browser/tests diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index 4890a1dee..000000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: Lint -on: - # Enable manually triggering this workflow via the API or web UI - workflow_dispatch: - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - deps: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - fetch-depth: 0 - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.26.x - check-latest: true - - name: Check dependencies - run: | - go version - test -z "$(go mod tidy && go mod vendor && git status --porcelain)" - go mod verify - - name: Check code generation - run: | - make generate - test -z "$(git status --porcelain | tee >(cat 1>&2))" - - lint: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - fetch-depth: 0 - - name: Run linters - uses: ./.github/actions/lint/ diff --git a/.github/workflows/packager.yml b/.github/workflows/packager.yml deleted file mode 100644 index 6edec1d30..000000000 --- a/.github/workflows/packager.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: k6packager -on: - # Enable manually triggering this workflow via the API or web UI - workflow_dispatch: - schedule: - - cron: '0 0 * * 0' # weekly (Sundays at 00:00) - -defaults: - run: - shell: bash - -jobs: - publish-packager: - runs-on: ubuntu-latest - env: - VERSION: 0.0.4 - DOCKER_IMAGE_ID: ghcr.io/grafana/k6packager - GITHUB_ACTOR: ${{ github.actor }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - permissions: - contents: read - id-token: write - packages: write - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Build - run: | - cd packaging - docker compose build packager - - name: Login to GitHub Container Registry - uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0 - with: - registry: ghcr.io - username: ${{ env.GITHUB_ACTOR }} - password: ${{ env.GITHUB_TOKEN }} - - name: Publish - run: | - docker push "${DOCKER_IMAGE_ID}:latest" - docker tag "${DOCKER_IMAGE_ID}" "${DOCKER_IMAGE_ID}:${VERSION}" - docker push "${DOCKER_IMAGE_ID}:${VERSION}" diff --git a/.github/workflows/publish-techdocs.yaml b/.github/workflows/publish-techdocs.yaml deleted file mode 100644 index ba17fd343..000000000 --- a/.github/workflows/publish-techdocs.yaml +++ /dev/null @@ -1,25 +0,0 @@ -name: Publish TechDocs -on: - push: - branches: - - master - paths: - - 'docs/docs/**' - - 'docs/mkdocs.yml' - - 'catalog-info.yaml' - - '.github/workflows/publish-techdocs.yaml' -concurrency: - group: '${{ github.workflow }}-${{ github.ref }}' - cancel-in-progress: true -permissions: {} -jobs: - publish-docs: - uses: grafana/shared-workflows/.github/workflows/publish-techdocs.yaml@e5b7989c92b55fe3ac6dd006d6de49082ba8c507 # main - permissions: - contents: read - id-token: write - with: - namespace: default - kind: component - name: k6 - default-working-directory: . diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml deleted file mode 100644 index 2347823b6..000000000 --- a/.github/workflows/stale.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: Close stale PRs - -on: - schedule: - - cron: "0 8 * * 1" # once per week, at 08:00 on Mondays - -permissions: - pull-requests: write - -jobs: - stale: - runs-on: ubuntu-latest - steps: - - name: Run stale action - uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - operations-per-run: 50 - ascending: true # process oldest PRs first - - days-before-pr-stale: 61 - days-before-pr-close: 7 - - stale-pr-label: stale - exempt-pr-labels: "wip" - - stale-pr-message: | - This pull request has been automatically marked as stale because it has not had - activity in the last two months. It will be closed in one week if no further activity occurs. Please - feel free to give a status update or ping for review. Thank you for your contributions! - close-pr-message: | - Closing this pull request due to two months of inactivity after being marked stale. Thank you for your contributions! diff --git a/.github/workflows/summary-code-generation.yml b/.github/workflows/summary-code-generation.yml deleted file mode 100644 index 530375099..000000000 --- a/.github/workflows/summary-code-generation.yml +++ /dev/null @@ -1,68 +0,0 @@ -name: Summary code generation - -on: - push: - branches: - - master - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - check-generated-code: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - - name: Install cog - run: | - COG_VERSION="v0.0.46" - curl -sSL "https://github.com/grafana/cog/releases/download/${COG_VERSION}/cog_Linux_x86_64.tar.gz" \ - | sudo tar -xz -C /usr/local/bin - cog --version - - - name: Checkout schemas - run: | - ./internal/lib/summary/machinereadable/checkout.sh - - - name: Generate code - run: | - ./internal/lib/summary/machinereadable/generate.sh - - - name: Check for uncommitted changes - run: | - # Check if there are any changes in the Git working directory - if ! git diff --exit-code; then - echo "❌ Generated code is not up-to-date!" - echo "" - echo "The following files have uncommitted changes:" - git diff --name-only - echo "" - echo "Please run the following commands to update the generated code:" - echo " ./internal/lib/summary/machinereadable/checkout.sh" - echo " ./internal/lib/summary/machinereadable/generate.sh" - echo "" - echo "Then commit the changes." - exit 1 - fi - - # Also check for untracked files - if [ -n "$(git ls-files --others --exclude-standard internal/lib/summary/machinereadable/ | grep -v k6-summary/)" ]; then - echo "❌ There are untracked generated files!" - echo "" - echo "Untracked files:" - git ls-files --others --exclude-standard internal/lib/summary/machinereadable/ | grep -v k6-summary/ - echo "" - echo "Please run the generation scripts and commit all generated files." - exit 1 - fi - - echo "✅ Generated code is up-to-date!" diff --git a/.github/workflows/tc39.yml b/.github/workflows/tc39.yml deleted file mode 100644 index 95b70d9ea..000000000 --- a/.github/workflows/tc39.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: TC39 -on: - workflow_dispatch: - push: - branches: - - master - pull_request: - paths: - - 'js/**' - - 'internal/js/**' - - 'vendor/github.com/grafana/sobek/**' - - 'vendor/github.com/evanw/esbuild**' - -defaults: - run: - shell: bash - -permissions: - contents: read - - -jobs: - tc39: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.26.x - check-latest: true - - name: Run tests - run: | - set -x - cd internal/js/tc39 - sh checkout.sh - go test -timeout 1h diff --git a/.github/workflows/test-browser.yml b/.github/workflows/test-browser.yml deleted file mode 100644 index 34fa63adb..000000000 --- a/.github/workflows/test-browser.yml +++ /dev/null @@ -1,93 +0,0 @@ -name: Browser tests -on: - push: - branches: - - master - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - test-prev: - if: ${{ github.ref == 'refs/heads/master' }} - strategy: - fail-fast: false - matrix: - go-version: [1.25.x] - platform: - [ubuntu-24.04, ubuntu-arm64-large, github-hosted-windows-x64-large] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - fetch-depth: 0 - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: ${{ matrix.go-version }} - platform: ${{ matrix.platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Get the k6 version - id: get_k6_version - run: | - echo "Running tests on '${GITHUB_REF}' with '$(git describe --tags --always --long --dirty)' checked out..." - - name: Run tests - uses: ./.github/actions/test-browser - - test-tip: - strategy: - fail-fast: false - matrix: - include: - - platform: ubuntu-24.04 - gotip_platform: ubuntu-24.04 - - platform: ubuntu-arm64-large - gotip_platform: ubuntu-24.04-arm - - platform: github-hosted-windows-x64-large - gotip_platform: windows-latest - runs-on: ${{ matrix.platform }} - continue-on-error: true - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: "gotip" - platform: ${{ matrix.gotip_platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Run tests - uses: ./.github/actions/test-browser - with: - use_gotip: "true" - - test-latest: - strategy: - fail-fast: false - matrix: - go-version: [1.26.x] - platform: - [ubuntu-24.04, ubuntu-arm64-large, github-hosted-windows-x64-large] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: ${{ matrix.go-version }} - platform: ${{ matrix.platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Run tests - uses: ./.github/actions/test-browser diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index d864ae280..000000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,90 +0,0 @@ -name: Test -on: - push: - branches: - - master - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - test-prev: - if: ${{ github.ref == 'refs/heads/master' }} - strategy: - fail-fast: false - matrix: - go-version: [1.25.x] - platform: [ubuntu-24.04, ubuntu-24.04-arm, windows-latest] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - fetch-depth: 0 - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: ${{ matrix.go-version }} - platform: ${{ matrix.platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Get the k6 version - id: get_k6_version - run: | - echo "Running tests on '${GITHUB_REF}' with '$(git describe --tags --always --long --dirty)' checked out..." - - name: Run tests - uses: ./.github/actions/test - with: - platform: ${{ matrix.platform }} - - test-tip: - strategy: - fail-fast: false - matrix: - platform: [ubuntu-24.04, ubuntu-24.04-arm, windows-latest] - runs-on: ${{ matrix.platform }} - continue-on-error: true - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: "gotip" - platform: ${{ matrix.platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Run tests - uses: ./.github/actions/test - with: - use_gotip: "true" - platform: ${{ matrix.platform }} - - test-latest: - strategy: - fail-fast: false - matrix: - go-version: [1.26.x] - platform: [ubuntu-24.04, ubuntu-24.04-arm, windows-latest] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Run common test steps - uses: ./.github/actions/test-common - with: - go_version: ${{ matrix.go-version }} - platform: ${{ matrix.platform }} - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Run tests - uses: ./.github/actions/test - with: - platform: ${{ matrix.platform }} diff --git a/.github/workflows/wpt.yml b/.github/workflows/wpt.yml deleted file mode 100644 index 21d752394..000000000 --- a/.github/workflows/wpt.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Web Platform Tests -on: - workflow_dispatch: - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - test: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Install Go - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.26.x - check-latest: true - # TODO: combine WebPlatform tests checkout & patch into the single step - - name: Run Streams Tests - run: | - set -x - cd internal/js/modules/k6/experimental/streams/tests - sh checkout.sh - go test -race ../... - - name: Run Webcrypto Tests - run: | - set -x - cd internal/js/modules/k6/webcrypto/tests - sh checkout.sh - go test -race ./... diff --git a/.github/workflows/xk6-tests/xk6-js-test/go.mod b/.github/workflows/xk6-tests/xk6-js-test/go.mod deleted file mode 100644 index af874f432..000000000 --- a/.github/workflows/xk6-tests/xk6-js-test/go.mod +++ /dev/null @@ -1,48 +0,0 @@ -module github.com/grafana/xk6-js-test - -go 1.25.10 - -require go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8 - -require ( - github.com/cenkalti/backoff/v5 v5.0.3 // indirect - github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/dlclark/regexp2 v1.11.5 // indirect - github.com/evanw/esbuild v0.28.0 // indirect - github.com/fatih/color v1.19.0 // indirect - github.com/fsnotify/fsnotify v1.4.9 // indirect - github.com/go-logr/logr v1.4.3 // indirect - github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect - github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 // indirect - github.com/google/uuid v1.6.0 // indirect - github.com/grafana/sobek v0.0.0-20260331145705-2272ac4993ef // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/mailru/easyjson v0.9.2 // indirect - github.com/mattn/go-colorable v0.1.14 // indirect - github.com/mattn/go-isatty v0.0.21 // indirect - github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect - github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect - github.com/sirupsen/logrus v1.9.3 // indirect - github.com/spf13/afero v1.1.2 // indirect - go.opentelemetry.io/auto/sdk v1.2.1 // indirect - go.opentelemetry.io/otel v1.43.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 // indirect - go.opentelemetry.io/otel/metric v1.43.0 // indirect - go.opentelemetry.io/otel/sdk v1.43.0 // indirect - go.opentelemetry.io/otel/trace v1.43.0 // indirect - go.opentelemetry.io/proto/otlp v1.10.0 // indirect - golang.org/x/net v0.53.0 // indirect - golang.org/x/sys v0.43.0 // indirect - golang.org/x/text v0.36.0 // indirect - golang.org/x/time v0.15.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 // indirect - google.golang.org/grpc v1.80.0 // indirect - google.golang.org/protobuf v1.36.11 // indirect - gopkg.in/guregu/null.v3 v3.3.0 // indirect - gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect -) diff --git a/.github/workflows/xk6-tests/xk6-js-test/go.sum b/.github/workflows/xk6-tests/xk6-js-test/go.sum deleted file mode 100644 index ce8b91c4d..000000000 --- a/.github/workflows/xk6-tests/xk6-js-test/go.sum +++ /dev/null @@ -1,128 +0,0 @@ -github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro= -github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= -github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM= -github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw= -github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= -github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ= -github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= -github.com/evanw/esbuild v0.28.0 h1:V96ghtc5p5JnNUQIUsc5H3kr+AcFcMqOJll2ZmJW6Lo= -github.com/evanw/esbuild v0.28.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= -github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w= -github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= -github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= -github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q= -github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= -github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= -github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 h1:z2ogiKUYzX5Is6zr/vP9vJGqPwcdqsWjOt+V8J7+bTc= -github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI= -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= -github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/sobek v0.0.0-20260331145705-2272ac4993ef h1:onLtCR9w3Iqn57s39rYf1PtRl89uSznaM36TKcepxn4= -github.com/grafana/sobek v0.0.0-20260331145705-2272ac4993ef/go.mod h1:YtuqiJX1W3XvRSilL/kUZzduJG3phPJWyzM9DiIEfBo= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 h1:HWRh5R2+9EifMyIHV7ZV+MIZqgz+PMpZ14Jynv3O2Zs= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0/go.mod h1:JfhWUomR1baixubs02l85lZYYOm7LV6om4ceouMv45c= -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE= -github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= -github.com/mailru/easyjson v0.9.2 h1:dX8U45hQsZpxd80nLvDGihsQ/OxlvTkVUXH2r/8cb2M= -github.com/mailru/easyjson v0.9.2/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= -github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= -github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= -github.com/mattn/go-isatty v0.0.21 h1:xYae+lCNBP7QuW4PUnNG61ffM4hVIfm+zUzDuSzYLGs= -github.com/mattn/go-isatty v0.0.21/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= -github.com/mccutchen/go-httpbin/v2 v2.20.0 h1:iMUzhdbAcjo9hepfG5W3hz1yWAyxiYlJMzKQtwyGDms= -github.com/mccutchen/go-httpbin/v2 v2.20.0/go.mod h1:GBy5I7XwZ4ZLhT3hcq39I4ikwN9x4QUt6EAxNiR8Jus= -github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd h1:AC3N94irbx2kWGA8f/2Ks7EQl2LxKIRQYuT9IJDwgiI= -github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd/go.mod h1:9vRHVuLCjoFfE3GT06X0spdOAO+Zzo4AMjdIwUHBvAk= -github.com/mstoykov/envconfig v1.5.0 h1:E2FgWf73BQt0ddgn7aoITkQHmgwAcHup1s//MsS5/f8= -github.com/mstoykov/envconfig v1.5.0/go.mod h1:vk/d9jpexY2Z9Bb0uB4Ndesss1Sr0Z9ZiGUrg5o9VGk= -github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY= -github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc= -github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= -github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= -github.com/onsi/gomega v1.39.1 h1:1IJLAad4zjPn2PsnhH70V4DKRFlrCzGBNrNaru+Vf28= -github.com/onsi/gomega v1.39.1/go.mod h1:hL6yVALoTOxeWudERyfppUcZXjMwIMLnuSfruD2lcfg= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+9UgnHXd/ekCK99C8EbDSAt5qsjXE= -github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8 h1:I3utcXRVcS15XNk7pcOR5Ufofup3A3Tt3yJD1pA6RBc= -go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8/go.mod h1:eLG4XT8rbibmfnjn7N+a4c4+0tUi3+8zbox58DMp9WU= -go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= -go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I= -go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 h1:88Y4s2C8oTui1LGM6bTWkw0ICGcOLCAI5l6zsD1j20k= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0/go.mod h1:Vl1/iaggsuRlrHf/hfPJPvVag77kKyvrLeD10kpMl+A= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0 h1:zWWrB1U6nqhS/k6zYB74CjRpuiitRtLLi68VcgmOEto= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0/go.mod h1:2qXPNBX1OVRC0IwOnfo1ljoid+RD0QK3443EaqVlsOU= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 h1:3iZJKlCZufyRzPzlQhUIWVmfltrXuGyfjREgGP3UUjc= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0/go.mod h1:/G+nUPfhq2e+qiXMGxMwumDrP5jtzU+mWN7/sjT2rak= -go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM= -go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY= -go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg= -go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg= -go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw= -go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A= -go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A= -go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0= -go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpuCSL2g= -go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= -go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= -golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI= -golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q= -golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA= -golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs= -golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI= -golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg= -golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164= -golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= -golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= -gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= -gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= -google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 h1:VPWxll4HlMw1Vs/qXtN7BvhZqsS9cdAittCNvVENElA= -google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:7QBABkRtR8z+TEnmXTqIqwJLlzrZKVfAUm7tY3yGv0M= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 h1:m8qni9SQFH0tJc1X0vmnpw/0t+AImlSvp30sEupozUg= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= -google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM= -google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4= -google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= -google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/guregu/null.v3 v3.3.0 h1:8j3ggqq+NgKt/O7mbFVUFKUMWN+l1AmT5jQmJ6nPh2c= -gopkg.in/guregu/null.v3 v3.3.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/.github/workflows/xk6-tests/xk6-js-test/jstest.go b/.github/workflows/xk6-tests/xk6-js-test/jstest.go deleted file mode 100644 index 9c075d98b..000000000 --- a/.github/workflows/xk6-tests/xk6-js-test/jstest.go +++ /dev/null @@ -1,69 +0,0 @@ -package jstest - -import ( - "fmt" - "time" - - "go.k6.io/k6/v2/js/modules" - "go.k6.io/k6/v2/metrics" -) - -func init() { - modules.Register("k6/x/jsexttest", New()) -} - -type ( - RootModule struct{} - - // JSTest is meant to test xk6 and the JS extension sub-system of k6. - JSTest struct { - vu modules.VU - - foos *metrics.Metric - } -) - -// Ensure the interfaces are implemented correctly. -var ( - _ modules.Module = &RootModule{} - _ modules.Instance = &JSTest{} -) - -// New returns a pointer to a new RootModule instance. -func New() *RootModule { - return &RootModule{} -} - -// NewModuleInstance implements the modules.Module interface and returns -// a new instance for each VU. -func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance { - return &JSTest{ - vu: vu, - foos: vu.InitEnv().Registry.MustNewMetric("foos", metrics.Counter), - } -} - -// Exports implements the modules.Instance interface and returns the exports -// of the JS module. -func (j *JSTest) Exports() modules.Exports { - return modules.Exports{Default: j} -} - -// Foo emits a foo metric -func (j *JSTest) Foo(arg float64) (bool, error) { - state := j.vu.State() - if state == nil { - return false, fmt.Errorf("the VU State is not available in the init context") - } - - ctx := j.vu.Context() - - tags := state.Tags.GetCurrentValues().Tags.With("foo", "bar") - metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{ - Time: time.Now(), - TimeSeries: metrics.TimeSeries{Metric: j.foos, Tags: tags}, - Value: arg, - }) - - return true, nil -} diff --git a/.github/workflows/xk6-tests/xk6-output-test/go.mod b/.github/workflows/xk6-tests/xk6-output-test/go.mod deleted file mode 100644 index 1e3fd4747..000000000 --- a/.github/workflows/xk6-tests/xk6-output-test/go.mod +++ /dev/null @@ -1,40 +0,0 @@ -module github.com/grafana/xk6-output-test - -go 1.25.10 - -require go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8 - -require ( - github.com/cenkalti/backoff/v5 v5.0.3 // indirect - github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/fatih/color v1.19.0 // indirect - github.com/go-logr/logr v1.4.3 // indirect - github.com/go-logr/stdr v1.2.2 // indirect - github.com/google/uuid v1.6.0 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect - github.com/josharian/intern v1.0.0 // indirect - github.com/mailru/easyjson v0.9.2 // indirect - github.com/mattn/go-colorable v0.1.14 // indirect - github.com/mattn/go-isatty v0.0.21 // indirect - github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd // indirect - github.com/sirupsen/logrus v1.9.3 // indirect - github.com/spf13/afero v1.1.2 // indirect - go.opentelemetry.io/auto/sdk v1.2.1 // indirect - go.opentelemetry.io/otel v1.43.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 // indirect - go.opentelemetry.io/otel/metric v1.43.0 // indirect - go.opentelemetry.io/otel/sdk v1.43.0 // indirect - go.opentelemetry.io/otel/trace v1.43.0 // indirect - go.opentelemetry.io/proto/otlp v1.10.0 // indirect - golang.org/x/net v0.53.0 // indirect - golang.org/x/sys v0.43.0 // indirect - golang.org/x/text v0.36.0 // indirect - golang.org/x/time v0.15.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 // indirect - google.golang.org/grpc v1.80.0 // indirect - google.golang.org/protobuf v1.36.11 // indirect - gopkg.in/guregu/null.v3 v3.3.0 // indirect -) diff --git a/.github/workflows/xk6-tests/xk6-output-test/go.sum b/.github/workflows/xk6-tests/xk6-output-test/go.sum deleted file mode 100644 index 2dfb53833..000000000 --- a/.github/workflows/xk6-tests/xk6-output-test/go.sum +++ /dev/null @@ -1,103 +0,0 @@ -github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro= -github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= -github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM= -github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw= -github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= -github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w= -github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE= -github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= -github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= -github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= -github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= -github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= -github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 h1:HWRh5R2+9EifMyIHV7ZV+MIZqgz+PMpZ14Jynv3O2Zs= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0/go.mod h1:JfhWUomR1baixubs02l85lZYYOm7LV6om4ceouMv45c= -github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= -github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE= -github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= -github.com/mailru/easyjson v0.9.2 h1:dX8U45hQsZpxd80nLvDGihsQ/OxlvTkVUXH2r/8cb2M= -github.com/mailru/easyjson v0.9.2/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= -github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= -github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= -github.com/mattn/go-isatty v0.0.21 h1:xYae+lCNBP7QuW4PUnNG61ffM4hVIfm+zUzDuSzYLGs= -github.com/mattn/go-isatty v0.0.21/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= -github.com/mccutchen/go-httpbin/v2 v2.20.0 h1:iMUzhdbAcjo9hepfG5W3hz1yWAyxiYlJMzKQtwyGDms= -github.com/mccutchen/go-httpbin/v2 v2.20.0/go.mod h1:GBy5I7XwZ4ZLhT3hcq39I4ikwN9x4QUt6EAxNiR8Jus= -github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd h1:AC3N94irbx2kWGA8f/2Ks7EQl2LxKIRQYuT9IJDwgiI= -github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd/go.mod h1:9vRHVuLCjoFfE3GT06X0spdOAO+Zzo4AMjdIwUHBvAk= -github.com/mstoykov/envconfig v1.5.0 h1:E2FgWf73BQt0ddgn7aoITkQHmgwAcHup1s//MsS5/f8= -github.com/mstoykov/envconfig v1.5.0/go.mod h1:vk/d9jpexY2Z9Bb0uB4Ndesss1Sr0Z9ZiGUrg5o9VGk= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8 h1:I3utcXRVcS15XNk7pcOR5Ufofup3A3Tt3yJD1pA6RBc= -go.k6.io/k6/v2 v2.0.0-20260424133653-c3427b0487c8/go.mod h1:eLG4XT8rbibmfnjn7N+a4c4+0tUi3+8zbox58DMp9WU= -go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= -go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I= -go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 h1:88Y4s2C8oTui1LGM6bTWkw0ICGcOLCAI5l6zsD1j20k= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0/go.mod h1:Vl1/iaggsuRlrHf/hfPJPvVag77kKyvrLeD10kpMl+A= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0 h1:zWWrB1U6nqhS/k6zYB74CjRpuiitRtLLi68VcgmOEto= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.42.0/go.mod h1:2qXPNBX1OVRC0IwOnfo1ljoid+RD0QK3443EaqVlsOU= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 h1:3iZJKlCZufyRzPzlQhUIWVmfltrXuGyfjREgGP3UUjc= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0/go.mod h1:/G+nUPfhq2e+qiXMGxMwumDrP5jtzU+mWN7/sjT2rak= -go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM= -go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY= -go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg= -go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg= -go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw= -go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A= -go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A= -go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0= -go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpuCSL2g= -go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= -golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI= -golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q= -golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA= -golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI= -golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg= -golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164= -golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= -golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= -gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= -gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= -google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 h1:VPWxll4HlMw1Vs/qXtN7BvhZqsS9cdAittCNvVENElA= -google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:7QBABkRtR8z+TEnmXTqIqwJLlzrZKVfAUm7tY3yGv0M= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 h1:m8qni9SQFH0tJc1X0vmnpw/0t+AImlSvp30sEupozUg= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= -google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM= -google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4= -google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= -google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/guregu/null.v3 v3.3.0 h1:8j3ggqq+NgKt/O7mbFVUFKUMWN+l1AmT5jQmJ6nPh2c= -gopkg.in/guregu/null.v3 v3.3.0/go.mod h1:E4tX2Qe3h7QdL+uZ3a0vqvYwKQsRSQKM5V4YltdgH9Y= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/.github/workflows/xk6-tests/xk6-output-test/outputtest.go b/.github/workflows/xk6-tests/xk6-output-test/outputtest.go deleted file mode 100644 index bf06b938f..000000000 --- a/.github/workflows/xk6-tests/xk6-output-test/outputtest.go +++ /dev/null @@ -1,58 +0,0 @@ -package outputtest - -import ( - "io" - "strconv" - - "go.k6.io/k6/v2/metrics" - "go.k6.io/k6/v2/output" -) - -func init() { - output.RegisterExtension("outputtest", func(params output.Params) (output.Output, error) { - return &Output{params: params}, nil - }) -} - -// Output is meant to test xk6 and the output extension sub-system of k6. -type Output struct { - params output.Params - calcResult float64 - outputFile io.WriteCloser -} - -// Description returns a human-readable description of the output. -func (o *Output) Description() string { - return "test output extension" -} - -// Start opens the specified output file. -func (o *Output) Start() error { - out, err := o.params.FS.Create(o.params.ConfigArgument) - if err != nil { - return err - } - o.outputFile = out - - return nil -} - -// AddMetricSamples just plucks out the metric we're interested in. -func (o *Output) AddMetricSamples(sampleContainers []metrics.SampleContainer) { - for _, sc := range sampleContainers { - for _, sample := range sc.GetSamples() { - if sample.Metric.Name == "foos" { - o.calcResult += sample.Value - } - } - } -} - -// Stop saves the dummy results and closes the file. -func (o *Output) Stop() error { - _, err := o.outputFile.Write([]byte(strconv.FormatFloat(o.calcResult, 'f', 0, 64))) - if err != nil { - return err - } - return o.outputFile.Close() -} diff --git a/.github/workflows/xk6-tests/xk6-test.js b/.github/workflows/xk6-tests/xk6-test.js deleted file mode 100644 index 093bad76c..000000000 --- a/.github/workflows/xk6-tests/xk6-test.js +++ /dev/null @@ -1,23 +0,0 @@ - -import { check } from 'k6'; -import jsexttest from 'k6/x/jsexttest'; - -export let options = { - iterations: 5, - thresholds: { - checks: ['rate===1'], - } -}; - -export function handleSummary(data) { - return { - 'summary-results.txt': data.metrics.foos.values.count.toString(), - }; -} - - -export default function () { - check(null, { - "foo is true": () => jsexttest.foo(__ITER), - }); -} \ No newline at end of file diff --git a/.github/workflows/xk6.yml b/.github/workflows/xk6.yml deleted file mode 100644 index ade6e3766..000000000 --- a/.github/workflows/xk6.yml +++ /dev/null @@ -1,96 +0,0 @@ -name: xk6 -on: - # Enable manually triggering this workflow via the API or web UI - workflow_dispatch: - push: - branches: - - master - pull_request: - -defaults: - run: - shell: bash - -permissions: - contents: read - -jobs: - test-xk6: - strategy: - fail-fast: false - matrix: - go: [stable, tip] - platform: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-latest] - runs-on: ${{ matrix.platform }} - steps: - - name: Checkout code - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - name: Install Go - if: matrix.go != 'tip' - uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 - with: - go-version: 1.26.x - check-latest: true - - name: Download Go tip - if: matrix.go == 'tip' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh release download ${{ matrix.platform }} --repo grafana/gotip --pattern 'go.zip' - - name: Install Go tip - if: matrix.go == 'tip' - run: | - unzip go.zip -d $HOME/sdk - echo "GOROOT=$HOME/sdk/gotip" >> "$GITHUB_ENV" - echo "GOPATH=$HOME/go" >> "$GITHUB_ENV" - echo "$HOME/go/bin" >> "$GITHUB_PATH" - echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH" - - name: Run tests - env: - PR_REPO: ${{ github.event.pull_request.head.repo.full_name }} - run: | - set -x - which go - go version - - export CGO_ENABLED=0 - - COMMIT_ID="${{ github.event.pull_request.head.sha }}" - if [[ "$COMMIT_ID" == "" ]]; then - COMMIT_ID="$(git rev-parse HEAD)" # branch, not PR merge commit - fi - echo "COMMIT_ID=$COMMIT_ID" - cd .github/workflows/xk6-tests - go install go.k6.io/xk6/cmd/xk6@master - if [ "${{ github.event_name }}" == "pull_request" -a \ - "${PR_REPO}" != "${{ github.repository }}" ]; then - export XK6_K6_REPO="github.com/${PR_REPO}" - fi - # The following is a workaround for Windows, cause when using 'shell: bash', the PATH is expressed - # with ':' as separator, but Go code, running on a Windows OS, expects ';' as separator. - XPATH="$PATH" - if [[ "${{ matrix.platform }}" == "windows-latest" || "${{ matrix.platform }}" == "windows-latest" ]]; then - XPATH="$HOME/sdk/gotip/bin;$XPATH" - fi - PATH="$XPATH" \ - GOPRIVATE="go.k6.io/k6" xk6 build "$COMMIT_ID" \ - --output ./k6ext \ - --with github.com/grafana/xk6-js-test="$(pwd)/xk6-js-test" \ - --with github.com/grafana/xk6-output-test="$(pwd)/xk6-output-test" - export K6_NO_USAGE_REPORT=true - ./k6ext version - ./k6ext run --out outputtest=output-results.txt xk6-test.js - - OUTPUT_RESULT="$(cat output-results.txt)" - if [[ "$OUTPUT_RESULT" != "10" ]]; then - echo "output result was not as expected: $OUTPUT_RESULT" - exit 11 - fi - - SUMMARY_RESULT="$(cat summary-results.txt)" - if [[ "$SUMMARY_RESULT" != "10" ]]; then - echo "summary result was not as expected: $SUMMARY_RESULT" - exit 12 - fi diff --git a/.gitignore b/.gitignore index cd8d6b80d..57600202c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /.protoc /output.csv +/build/ .vscode *.sublime-workspace diff --git a/.golangci.patch b/.golangci.patch new file mode 100644 index 000000000..2a47d961b --- /dev/null +++ b/.golangci.patch @@ -0,0 +1,32 @@ +--- a/.golangci.yml ++++ b/.golangci.yml +@@ -99,8 +99,29 @@ + - gocognit + - lll + - gosec ++ - goconst ++ - prealloc + path: _(test|gen)\.go + - linters: ++ - contextcheck ++ - revive ++ path: js\/modules\/k6\/browser\/.*\.go ++ - linters: ++ - goconst ++ path: js\/modules\/k6\/browser\/.*mapping\.go ++ - linters: ++ - revive ++ path: js\/modules\/k6\/html\/.*\.go ++ text: 'exported: exported ' ++ - linters: ++ - paralleltest ++ - tparallel ++ path: js\/modules\/k6\/html\/.*\.go ++ - linters: ++ - paralleltest ++ - tparallel ++ path: js\/modules\/k6\/http\/.*_test\.go ++ - linters: + - forbidigo + text: use of `os\.(SyscallError|Signal|Interrupt)` forbidden + - linters: diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index 159d1a046..000000000 --- a/.golangci.yml +++ /dev/null @@ -1,148 +0,0 @@ -# v2.10.1 -# Please don't remove the first line. It uses in CI to determine the golangci version -version: "2" -linters: - default: none - enable: - - asasalint - - asciicheck - - bidichk - - bodyclose - - canonicalheader - - contextcheck - - copyloopvar - - cyclop - - dogsled - - dupl - - durationcheck - - errcheck - - errchkjson - - errname - - errorlint - - exhaustive - - fatcontext - - forbidigo - - forcetypeassert - - funlen - - gocheckcompilerdirectives - - gochecknoglobals - - gocognit - - goconst - - gocritic - - gomoddirectives - - goprintffuncname - - gosec - - govet - - importas - - ineffassign - - interfacebloat - - lll - - makezero - - misspell - - modernize - - nakedret - - nestif - - nilerr - - nilnil - - noctx - - nolintlint - - nosprintfhostport - - paralleltest - - prealloc - - predeclared - - promlinter - - reassign - - revive - - rowserrcheck - - sqlclosecheck - - staticcheck - - tparallel - - unconvert - - unparam - - unused - - usestdlibvars - - usetesting - - wastedassign - - whitespace - settings: - cyclop: - max-complexity: 25 - dupl: - threshold: 150 - exhaustive: - default-signifies-exhaustive: true - forbidigo: - forbid: - - pattern: ^(fmt\\.Print(|f|ln)|print|println)$ - - pattern: ^os\.(.*)$(# Using anything except Signal and SyscallError from the os package is forbidden )? - - pattern: ^syscall\.[^A-Z_]+$(# Using anything except constants from the syscall package is forbidden )? - - pattern: ^logrus\.Logger$ - - pattern: ^afero\.(.*)$(# Using afero outside of the fsext package is forbidden )? - funlen: - lines: 80 - statements: 60 - goconst: - min-len: 10 - min-occurrences: 4 - usetesting: - os-setenv: true - exclusions: - generated: lax - rules: - - linters: - - canonicalheader - - cyclop - - dupl - - forcetypeassert - - funlen - - gocognit - - lll - - gosec - path: _(test|gen)\.go - - linters: - - contextcheck - - revive - path: js\/modules\/k6\/browser\/.*\.go - - linters: - - revive - path: js\/modules\/k6\/html\/.*\.go - text: 'exported: exported ' - - linters: - - paralleltest - - tparallel - path: js\/modules\/k6\/html\/.*\.go - - linters: - - paralleltest - - tparallel - path: js\/modules\/k6\/http\/.*_test\.go - - linters: - - forbidigo - text: use of `os\.(SyscallError|Signal|Interrupt)` forbidden - - linters: - - forbidigo - text: use of `afero\.(.*)` forbidden - path: lib/fsext/.*\.go - - linters: - - revive - text: avoid meaningless package names - - linters: - - revive - text: avoid package names that conflict with Go standard library - paths: - - third_party$ - - builtin$ - - examples$ -issues: - max-issues-per-linter: 0 - max-same-issues: 0 -formatters: - enable: - - gofmt - - gofumpt - - goimports - exclusions: - generated: lax - paths: - - third_party$ - - builtin$ - - examples$ diff --git a/AGENTS.md b/AGENTS.md index 03be6deed..c02b86182 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,11 +1,11 @@ -## Fork Policy (jgaalen/k6) +## Fork Policy (Breaking-IT/k6) -This repository is a personal fork of grafana/k6 with private modifications +This repository is the public Breaking-IT fork of grafana/k6 with BreakTest modifications (lean cloud-free builds via the `lean` build tag, the breakingit output, etc.). - **Never push to upstream (grafana/k6).** Upstream is fetch-only: pull releases from it and merge them into this fork's branches, then push only to `origin` - (github.com/jgaalen/k6). + (github.com/Breaking-IT/k6). - After cloning this fork, disable pushes to upstream once: ```bash git remote add upstream https://github.com/grafana/k6.git @@ -13,10 +13,12 @@ This repository is a personal fork of grafana/k6 with private modifications ``` If a push to upstream fails with `DISABLED`, that is intentional — do not "fix" the URL. -- To pull in a new upstream release: fetch upstream tags, fast-forward `master` - to the release tag, merge `master` into `breakingit`, and gate any new cloud - functionality behind `//go:build !lean` (see `internal/cmd/cloud_lean.go` for - the stub pattern). Release binaries are built with `-tags lean` +- `master` is the canonical fork development and build branch. The historical + `breakingit` work is consolidated there; do not use upstream as a replacement for it. +- To pull in a new stable upstream release: fetch upstream tags, create an integration + branch from `origin/master`, merge the release tag, and open a tested pull request + against Breaking-IT/k6 `master`. Gate any new cloud functionality behind `//go:build !lean` + (see `internal/cmd/cloud_lean.go` for the stub pattern). Release binaries are built with `-tags lean` (`./build-linux-binaries.sh`). ## Project Overview @@ -85,7 +87,7 @@ make grpc-server-run **JavaScript Runtime**: `internal/js/` and `js/` - `internal/js/`: Core JavaScript execution engine using Sobek (Goja fork) -- `js/modules/`: Built-in k6 modules (http, browser, crypto, etc.) +- `js/modules/`: Built-in k6 modules (http, html) - `js/common/`: Shared JavaScript runtime utilities **Load Testing Core**: `lib/` @@ -205,8 +207,8 @@ git log ## Common File Locations **CLI Commands**: `internal/cmd/` (run.go, cloud.go, etc.) -**HTTP Module**: `js/modules/k6/http/` and `internal/js/modules/k6/http/` +**HTTP Module**: `js/modules/k6/http/` **Browser Module**: `internal/js/modules/k6/browser/` **Metrics**: `metrics/` package for metric definitions **Examples**: `examples/` directory with various test script examples -**Test Data**: `internal/cmd/testdata/` and module-specific test fixtures \ No newline at end of file +**Test Data**: `internal/cmd/testdata/` and module-specific test fixtures diff --git a/CODEOWNERS b/CODEOWNERS index c3044d480..dae46afc5 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -6,3 +6,6 @@ # It turned out to be less noisy if the author picks manually every time # the best reviewers based on the context of the working area. /release\ notes/ + +# Feature briefs are owned by the maintainers (k6-core) and product. +/docs/features/ @grafana/k6-core @andrewslotin diff --git a/Dependencies.md b/Dependencies.md index d7e689b02..bd04f49df 100644 --- a/Dependencies.md +++ b/Dependencies.md @@ -94,4 +94,4 @@ It's preferable to make multiple PRs - in most cases you can split them in three Further splitting is recommended if PRs become too big. -When updating Sobek it's recommended to run the tc39 tests in `js/tc39`. And if needed, update the breaking ones as explained in an [Introduction to a k6's TC39 testing](./js/tc39/README.md). +When updating Sobek it's recommended to run the tc39 tests in `internal/js/tc39`. And if needed, update the breaking ones as explained in an [Introduction to a k6's TC39 testing](./internal/js/tc39/README.md). diff --git a/Dockerfile b/Dockerfile index d8dd245a4..c4c729bf6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM --platform=$BUILDPLATFORM golang:1.26.4-alpine@sha256:f23e8b227fb4493eabe03bede4d5a32d04092da71962f1fb79b5f7d1e6c2a17f as builder +FROM --platform=$BUILDPLATFORM golang:1.27.1-alpine@sha256:4cb7ac979db5fcc41cae44b2227ba5ab8a51e8807f40d9ba4dee20a0ad960b5b as builder WORKDIR $GOPATH/src/go.k6.io/k6 COPY . . ARG TARGETOS TARGETARCH @@ -6,7 +6,7 @@ RUN apk --no-cache add git=~2 RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -o /usr/bin/k6 # Runtime stage -FROM alpine:3.24.0@sha256:a2d49ea686c2adfe3c992e47dc3b5e7fa6e6b5055609400dc2acaeb241c829f4 as release +FROM alpine:3.24.1@sha256:28bd5fe8b56d1bd048e5babf5b10710ebe0bae67db86916198a6eec434943f8b as release RUN adduser -D -u 12345 -g 12345 k6 COPY --from=builder /usr/bin/k6 /usr/bin/k6 diff --git a/FORK.md b/FORK.md index 6dfba5925..6802e5f22 100644 --- a/FORK.md +++ b/FORK.md @@ -11,7 +11,8 @@ license notices are retained. ## Source releases -The `breakingit` branch contains the current fork source. Immutable source-release tags are +The `master` branch contains the current fork source. The historical `breakingit` branch is +retained for reference. Immutable source-release tags are published for distributed builds. The initial public release is [`breakingit-v2.1.0-26`](https://github.com/Breaking-IT/k6/tree/breakingit-v2.1.0-26). @@ -26,6 +27,59 @@ code to the k6 executable. - Extends browser network and error metrics, including optional error screenshots. - Adds the `lean` build tag used for smaller cloud-free release binaries. - Adds Breaking-IT build helpers and operational configuration. +- Excludes extension-usage, installation-ID, and build-origin reporting in all builds. + Basic anonymous run statistics retain the existing `K6_NO_USAGE_REPORT` opt-out. +- Preserves custom browser timings and error payloads with upstream async metric context. +- Enables async metric context by default, with explicit feature lists retaining opt-out control. +- Exports native group durations and failure outcomes as BreakTest transactions alongside legacy + `pages` metrics. See [transaction configuration](internal/output/breakingit/README.md). +- Restores metric context correctly when native promise settlement re-enters the runtime, preventing + browser group paths from accumulating across iterations. +- Excludes upstream's Grafana-specific Renovate approval automation. +- Runs lean release builds and fork regression tests on standard Linux GitHub Actions runners. -For the complete change history, compare the `breakingit` branch with the upstream +## Development and builds in Breaking-IT/k6 + +`master` is the default development and build branch, based on upstream k6 v2.3.0. It includes +the consolidated `breakingit` customizations, native group transactions and browser group-context +fix. Browser admission control remains reverted. Create feature branches from `origin/master` +and merge tested pull requests into `master` in Breaking-IT/k6. + +Track stable upstream releases: fetch tags from `upstream`, create an integration branch from +`origin/master`, and merge the new release tag into that branch. Preserve the fork changes, +validate normal and lean builds, and open a pull request against Breaking-IT/k6 `master`. Do not +replace the fork branch with upstream history. The `upstream` remote is fetch-only; keep its +push URL set to `DISABLED` and push only to `origin` (Breaking-IT/k6). + +```sh +git switch master +git pull --ff-only origin master +./build-linux-binaries.sh +``` + +The build script reads the source in the current checkout and writes Linux and macOS binaries for +amd64 and arm64 into `dist/`. It does not install or deploy them. Its displayed `VERSION` value is +informational; use the binary's `version` command and SHA-256 checksum to identify an artifact. + +For the complete change history, compare the `master` branch with the upstream [grafana/k6](https://github.com/grafana/k6) repository. + +## Validation before merging + +Run lint and the full race-enabled suite with bounded concurrency. Browser tests launch real +Chromium processes; on a workstation, the upstream 210-second package timeout and default +parallelism can overload the browser suite. + +```sh +make lint +go test -race -p 2 -parallel 2 -timeout 15m ./... +go test -race -p 2 -parallel 2 -tags lean -timeout 210s \ + ./internal/cmd ./internal/cmd/tests ./internal/api ./internal/js \ + ./internal/features ./internal/output/breakingit +go build -o dist/k6 . +go build -tags lean -o dist/k6-lean . +``` + +Cloud, REST API, and excluded-output integration tests require the full build. Lean-specific +checks verify their stubs and output availability; shared runtime and fork regression tests +run in both configurations. diff --git a/Makefile b/Makefile index 51332c92f..2dd8c8f8a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ MAKEFLAGS += --silent -GOLANGCI_LINT_VERSION = $(shell head -n 1 .golangci.yml | tr -d '\# ') PROTOC_VERSION := 21.12 +LINT_WORKFLOW ?= .github/workflows/lint.yml +K6_CI_REF := $(shell grep -oE 'grafana/k6-ci/[^@[:space:]]+@[A-Za-z0-9._/-]+' $(LINT_WORKFLOW) | head -n1 | cut -d@ -f2) +LINT_BASE_URL := https://raw.githubusercontent.com/grafana/k6-ci/$(K6_CI_REF)/.golangci.yml +LINT_DIR ?= build/lint +LINT_BASE := $(LINT_DIR)/.golangci-base.yml +LINT_FINAL := $(LINT_DIR)/.golangci.yml +LINT_PATCH ?= .golangci.patch + ifeq ($(OS),Windows_NT) DETECTED_OS := Windows PROTOC_ARCHIVE := protoc-$(PROTOC_VERSION)-win64.zip @@ -51,14 +58,36 @@ format: grpc-server-run: go run -mod=mod examples/grpc_server/*.go -## check-linter-version: Checks if the linter version is the same as the one specified in the linter config. -check-linter-version: - (golangci-lint version | grep -E "version v?$(shell head -n 1 .golangci.yml | tr -d '\# v')") || echo "Your installation of golangci-lint is different from the one that is specified in k6's linter config (there it's $(shell head -n 1 .golangci.yml | tr -d '\# ')). Results could be different in the CI." +$(LINT_DIR): + mkdir -p $@ + +$(LINT_BASE): $(LINT_WORKFLOW) | $(LINT_DIR) + curl -fsSL $(LINT_BASE_URL) -o $@ + +$(LINT_FINAL): $(LINT_BASE) $(wildcard $(LINT_PATCH)) + cp $(LINT_BASE) $@ + @if [ -f $(LINT_PATCH) ]; then \ + echo "Applying $(LINT_PATCH)"; \ + git apply --directory=$(LINT_DIR) $(LINT_PATCH); \ + fi -## lint: Runs the linters. -lint: check-linter-version +## lint: Run golangci-lint with the k6-ci config + $(LINT_PATCH). +lint: $(LINT_FINAL) echo "Running linters..." - golangci-lint run ./... + go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$$(head -n 1 $(LINT_BASE) | tr -d '# ') \ + run --config=$(LINT_FINAL) ./... + +## update-lint-patch: Regenerate $(LINT_PATCH) from the locally edited $(LINT_FINAL). +update-lint-patch: $(LINT_BASE) + @if [ ! -f $(LINT_FINAL) ]; then \ + echo "Run 'make lint' first to materialize $(LINT_FINAL), edit it, then re-run."; \ + exit 1; \ + fi + -diff -u --label a/.golangci.yml --label b/.golangci.yml $(LINT_BASE) $(LINT_FINAL) > $(LINT_PATCH) + +## clean-lint: Remove $(LINT_DIR). +clean-lint: + rm -rf $(LINT_DIR) ## tests: Executes any unit tests. tests: @@ -82,4 +111,4 @@ clean: @echo "cleaning" rm -f ./k6 -.PHONY: build format lint tests check check-linter-version help +.PHONY: build format lint tests check help update-lint-patch clean-lint diff --git a/README.md b/README.md index 5ea9e5a36..0fee66c56 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@

Github release - Build status + Breaking-IT lean CI Go Report Card Codecov branch
diff --git a/api/v1/client/client.go b/api/v1/client/client.go index 8acec9f4c..378d72f5e 100644 --- a/api/v1/client/client.go +++ b/api/v1/client/client.go @@ -94,7 +94,7 @@ func (c *Client) CallAPI(ctx context.Context, method string, rel *url.URL, body, } req = req.WithContext(ctx) - res, err := c.httpClient.Do(req) //nolint:gosec + res, err := c.httpClient.Do(req) if err != nil { return err } diff --git a/api/v1/group_routes_test.go b/api/v1/group_routes_test.go index 5badc2690..069ae67b2 100644 --- a/api/v1/group_routes_test.go +++ b/api/v1/group_routes_test.go @@ -102,7 +102,7 @@ func TestGetGroups(t *testing.T) { t.Parallel() rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/groups", nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/groups", nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) @@ -158,7 +158,7 @@ func TestGetGroups(t *testing.T) { t.Run(gp.Name, func(t *testing.T) { t.Parallel() rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/groups/"+gp.ID, nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/groups/"+gp.ID, nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) diff --git a/api/v1/metric_routes_test.go b/api/v1/metric_routes_test.go index 0f31a5f20..92322ac69 100644 --- a/api/v1/metric_routes_test.go +++ b/api/v1/metric_routes_test.go @@ -29,7 +29,7 @@ func TestGetMetrics(t *testing.T) { cs.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true) rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/metrics", nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/metrics", nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) @@ -91,7 +91,7 @@ func TestGetMetric(t *testing.T) { t.Parallel() rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/metrics/notreal", nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/metrics/notreal", nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) @@ -103,7 +103,7 @@ func TestGetMetric(t *testing.T) { t.Parallel() rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/metrics/my_metric", nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/metrics/my_metric", nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) diff --git a/api/v1/setup_teardown_routes_test.go b/api/v1/setup_teardown_routes_test.go index c89d9332e..f0af9d205 100644 --- a/api/v1/setup_teardown_routes_test.go +++ b/api/v1/setup_teardown_routes_test.go @@ -166,7 +166,7 @@ func TestSetupData(t *testing.T) { checkSetup := func(method, body, expResult string) { rw := httptest.NewRecorder() - handler.ServeHTTP(rw, httptest.NewRequest(method, "/v1/setup", bytes.NewBufferString(body))) + handler.ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), method, "/v1/setup", bytes.NewBufferString(body))) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) diff --git a/api/v1/status.go b/api/v1/status.go index c82a000f0..d1753ee7c 100644 --- a/api/v1/status.go +++ b/api/v1/status.go @@ -10,16 +10,26 @@ import ( type Status struct { Status lib.ExecutionStatus `json:"status" yaml:"status"` - Paused null.Bool `json:"paused" yaml:"paused"` - VUs null.Int `json:"vus" yaml:"vus"` - VUsMax null.Int `json:"vus-max" yaml:"vus-max"` - Stopped bool `json:"stopped" yaml:"stopped"` - Running bool `json:"running" yaml:"running"` - Tainted bool `json:"tainted" yaml:"tainted"` + Paused null.Bool `json:"paused" yaml:"paused"` + VUs null.Int `json:"vus" yaml:"vus"` + VUsMax null.Int `json:"vus-max" yaml:"vus-max"` + Stopped bool `json:"stopped" yaml:"stopped"` + Running bool `json:"running" yaml:"running"` + Tainted bool `json:"tainted" yaml:"tainted"` + ExecutionResult *ExecutionResult `json:"execution_result" yaml:"execution_result"` +} + +// ExecutionResult represents the result of a completed test execution. +type ExecutionResult struct { + ExitCode int `json:"exit_code" yaml:"exit_code"` } func newStatus(cs *ControlSurface) Status { executionState := cs.Scheduler.GetState() + var executionResult *ExecutionResult + if exitCode, ok := executionState.GetExecutionResult(); ok { + executionResult = &ExecutionResult{ExitCode: exitCode} + } isStopped := false select { case <-cs.RunCtx.Done(): @@ -27,12 +37,13 @@ func newStatus(cs *ControlSurface) Status { default: } return Status{ - Status: executionState.GetCurrentExecutionStatus(), - Running: executionState.HasStarted() && !executionState.HasEnded(), - Paused: null.BoolFrom(executionState.IsPaused()), - Stopped: isStopped, - VUs: null.IntFrom(executionState.GetCurrentlyActiveVUsCount()), - VUsMax: null.IntFrom(executionState.GetInitializedVUsCount()), - Tainted: cs.MetricsEngine.GetMetricsWithBreachedThresholdsCount() > 0, + Status: executionState.GetCurrentExecutionStatus(), + Running: executionState.HasStarted() && !executionState.HasEnded(), + Paused: null.BoolFrom(executionState.IsPaused()), + Stopped: isStopped, + VUs: null.IntFrom(executionState.GetCurrentlyActiveVUsCount()), + VUsMax: null.IntFrom(executionState.GetInitializedVUsCount()), + Tainted: cs.MetricsEngine.GetMetricsWithBreachedThresholdsCount() > 0, + ExecutionResult: executionResult, } } diff --git a/api/v1/status_routes_test.go b/api/v1/status_routes_test.go index f43ac296b..b767634ec 100644 --- a/api/v1/status_routes_test.go +++ b/api/v1/status_routes_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.k6.io/k6/v2/internal/lib/testutils/minirunner" "go.k6.io/k6/v2/lib" @@ -19,7 +20,7 @@ func TestGetStatus(t *testing.T) { cs := getControlSurface(t, testState) rw := httptest.NewRecorder() - NewHandler(cs).ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "/v1/status", nil)) + NewHandler(cs).ServeHTTP(rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/status", nil)) res := rw.Result() t.Cleanup(func() { assert.NoError(t, res.Body.Close()) @@ -49,5 +50,24 @@ func TestGetStatus(t *testing.T) { assert.True(t, status.VUsMax.Valid) assert.False(t, status.Stopped) assert.False(t, status.Tainted) + assert.Nil(t, status.ExecutionResult) + assert.Contains(t, rw.Body.String(), `"execution_result":null`) }) } + +func TestGetStatusExecutionResult(t *testing.T) { + t.Parallel() + + testState := getTestRunState(t, lib.Options{}, &minirunner.MiniRunner{}) + cs := getControlSurface(t, testState) + cs.Scheduler.GetState().SetExecutionResult(108) + + rw := httptest.NewRecorder() + NewHandler(cs).ServeHTTP( + rw, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/v1/status", nil), + ) + + var statusEnvelop StatusJSONAPI + require.NoError(t, json.Unmarshal(rw.Body.Bytes(), &statusEnvelop)) + assert.Equal(t, &ExecutionResult{ExitCode: 108}, statusEnvelop.Status().ExecutionResult) +} diff --git a/cloudapi/client.go b/cloudapi/client.go index 4379ba9a8..7ca3b70b0 100644 --- a/cloudapi/client.go +++ b/cloudapi/client.go @@ -13,6 +13,7 @@ import ( "time" "github.com/sirupsen/logrus" + "go.k6.io/k6/v2/internal/cloudapi/httperr" ) const ( @@ -151,7 +152,7 @@ func (c *Client) prepareHeaders(req *http.Request) { } func (c *Client) do(req *http.Request, v any, attempt int) (retry bool, err error) { - resp, err := c.client.Do(req) //nolint:gosec + resp, err := c.client.Do(req) defer func() { if resp != nil { @@ -204,11 +205,8 @@ func CheckResponse(r *http.Response) error { Error ResponseError `json:"error"` } if err := json.Unmarshal(data, &payload); err != nil { - if r.StatusCode == http.StatusUnauthorized { - return errNotAuthenticated - } - if r.StatusCode == http.StatusForbidden { - return errNotAuthorized + if classified := httperr.ClassifyStatus(r.StatusCode); classified != nil { + return classified } return fmt.Errorf( "unexpected HTTP error from %s: %d %s", diff --git a/cloudapi/config.go b/cloudapi/config.go index d2846c93f..d7fde24db 100644 --- a/cloudapi/config.go +++ b/cloudapi/config.go @@ -34,12 +34,45 @@ type Config struct { StopOnError null.Bool `json:"stopOnError" envconfig:"K6_CLOUD_STOP_ON_ERROR"` APIVersion null.Int `json:"apiVersion" envconfig:"K6_CLOUD_API_VERSION"` + // MetricsPushURL is the URL to push metrics to. It is set only + // programmatically: from the provisioning API's start_local_execution + // response in the self-provision flow, or from K6_CLOUD_METRICS_PUSH_URL + // read explicitly by the cmd layer (internal/cmd/outputs_cloud.go) in the + // externally-provisioned flow. It is deliberately NOT env-bound (no + // envconfig tag): binding it would let a stray K6_CLOUD_METRICS_PUSH_URL + // in the environment override the value obtained from provisioning and + // corrupt the self-provisioned push. When set, the cloud Output pushes + // here instead of deriving a URL from Host. + MetricsPushURL null.String `json:"metricsPushURL"` + + // TestRunToken is the scoped test-run token used as the Bearer token for + // metrics push (and notify, in the self-provision flow). Like + // MetricsPushURL it is set only programmatically — from the provisioning + // response, or from K6_CLOUD_TEST_RUN_TOKEN read explicitly by the cmd + // layer for an externally-provisioned run — and is deliberately NOT + // env-bound, so a stray env var cannot hijack the self-provisioned push. + TestRunToken null.String `json:"testRunToken"` + // PushRefID is the identifier used by k6 Cloud to correlate all the things that // belong to the same test run/execution. Currently, it is equivalent to the test run id. // But, in the future, or in future solutions (e.g. Synthetic Monitoring), there might be // no test run id, and we may still need an identifier to correlate all the things. PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"` + // Log-push configuration for `k6 cloud run --local-execution`. Like the + // scoped push creds above, these are programmatic-only (no envconfig tag): + // the self-provisioned flow sets them from the provisioning API's + // runtime_config.logs, while the externally-provisioned flow reads the + // K6_CLOUD_LOGS_* env vars explicitly in cmd (applyExternalLogsConfig), so + // a stray env value can't override the run-scoped values. Consumed by cmd + // to configure the cloud log pusher; never serialised. + LogsPushURL null.String `json:"-"` + LogsLevel null.String `json:"-"` + LogsLimit null.Int `json:"-"` + LogsPushPeriod types.NullDuration `json:"-"` + LogsMessageMaxSize null.Int `json:"-"` + LogsAllowedLabels []string `json:"-"` + // Defines the max allowed number of time series in a single batch. MaxTimeSeriesInBatch null.Int `json:"maxTimeSeriesInBatch" envconfig:"K6_CLOUD_MAX_TIME_SERIES_IN_BATCH"` @@ -104,7 +137,7 @@ func NewConfig() Config { // Apply saves config non-zero config values from the passed config in the receiver. // -//nolint:cyclop,gocognit +//nolint:cyclop,funlen,gocognit func (c Config) Apply(cfg Config) Config { if cfg.StackID.Valid { c.StackID = cfg.StackID @@ -133,9 +166,33 @@ func (c Config) Apply(cfg Config) Config { if cfg.LogsTailURL.Valid && cfg.LogsTailURL.String != "" { c.LogsTailURL = cfg.LogsTailURL } + if cfg.MetricsPushURL.Valid { + c.MetricsPushURL = cfg.MetricsPushURL + } + if cfg.TestRunToken.Valid { + c.TestRunToken = cfg.TestRunToken + } if cfg.PushRefID.Valid { c.PushRefID = cfg.PushRefID } + if cfg.LogsPushURL.Valid { + c.LogsPushURL = cfg.LogsPushURL + } + if cfg.LogsLevel.Valid { + c.LogsLevel = cfg.LogsLevel + } + if cfg.LogsLimit.Valid { + c.LogsLimit = cfg.LogsLimit + } + if cfg.LogsPushPeriod.Valid { + c.LogsPushPeriod = cfg.LogsPushPeriod + } + if cfg.LogsMessageMaxSize.Valid { + c.LogsMessageMaxSize = cfg.LogsMessageMaxSize + } + if len(cfg.LogsAllowedLabels) > 0 { + c.LogsAllowedLabels = cfg.LogsAllowedLabels + } if cfg.WebAppURL.Valid { c.WebAppURL = cfg.WebAppURL } diff --git a/cloudapi/config_test.go b/cloudapi/config_test.go index 08c85e153..c054a4bdf 100644 --- a/cloudapi/config_test.go +++ b/cloudapi/config_test.go @@ -32,6 +32,8 @@ func TestConfigApply(t *testing.T) { Hostv6: null.NewString("Hostv6", true), Timeout: types.NewNullDuration(5*time.Second, true), LogsTailURL: null.NewString("LogsTailURL", true), + MetricsPushURL: null.NewString("MetricsPushURL", true), + TestRunToken: null.NewString("TestRunToken", true), PushRefID: null.NewString("PushRefID", true), WebAppURL: null.NewString("foo", true), NoCompress: null.NewBool(true, true), @@ -55,6 +57,169 @@ func TestConfigApply(t *testing.T) { assert.Equal(t, full, defaults.Apply(full)) } +// TestConfig_NewFieldsRoundTripThroughJSON also serves as the security +// regression for "scoped tokens stay in-process": the fields live on +// cloudapi.Config (which round-trips only via derivedConfig.Collectors[cloud]) +// and NOT on lib.Options (which is serialised into the archive's +// metadata.json and pushed to the cloud). A new field accidentally added +// to lib.Options would be serialised into the archive — but the new +// fields here are on cloudapi.Config, so they cannot leak that way. +func TestConfig_NewFieldsRoundTripThroughJSON(t *testing.T) { + t.Parallel() + + original := Config{ + MetricsPushURL: null.StringFrom("https://ingest.example/metrics/abc"), + TestRunToken: null.StringFrom("scoped-token-xyz"), + } + + data, err := json.Marshal(original) + require.NoError(t, err) + + assert.Contains(t, string(data), `"metricsPushURL":"https://ingest.example/metrics/abc"`) + assert.Contains(t, string(data), `"testRunToken":"scoped-token-xyz"`) + + var roundTripped Config + require.NoError(t, json.Unmarshal(data, &roundTripped)) + + assert.Equal(t, original.MetricsPushURL, roundTripped.MetricsPushURL) + assert.Equal(t, original.TestRunToken, roundTripped.TestRunToken) +} + +func TestConfig_NewFieldsNotPickedUpByEnvconfig(t *testing.T) { + t.Parallel() + + // Set env vars with every plausible name a user might guess. + envVars := map[string]string{ + "K6_CLOUD_METRICS_PUSH_URL": "foo", + "K6_CLOUD_TEST_RUN_TOKEN": "bar", + "K6_CLOUD_METRICSPUSHURL": "foo", + "K6_CLOUD_TESTRUNTOKEN": "bar", + "K6_CLOUD_LOGS_PUSH_URL": "https://stray.example/logs", + "K6_CLOUD_LOGS_LEVEL": "debug", + "K6_CLOUD_LOGS_LIMIT": "5", + "K6_CLOUD_LOGS_PUSH_PERIOD": "9s", + "K6_CLOUD_LOGS_MESSAGE_MAX_SIZE": "42", + "K6_CLOUD_LOGS_ALLOWED_LABELS": "stray", + } + + config, _, err := GetConsolidatedConfig(nil, envVars, "", nil) + require.NoError(t, err) + + // The scoped push creds and the log-push config are all programmatic-only, + // so none of these env vars are picked up by envconfig. + assert.Equal(t, null.String{}, config.MetricsPushURL) + assert.Equal(t, null.String{}, config.TestRunToken) + assert.Equal(t, null.String{}, config.LogsPushURL) + assert.Equal(t, null.String{}, config.LogsLevel) + assert.Equal(t, null.Int{}, config.LogsLimit) + assert.Equal(t, types.NullDuration{}, config.LogsPushPeriod) + assert.Equal(t, null.Int{}, config.LogsMessageMaxSize) + assert.Empty(t, config.LogsAllowedLabels) +} + +func TestConfig_Apply_MergesNewFields(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + base Config + applied Config + wantPushURL null.String + wantRunToken null.String + }{ + { + name: "applied populated, base unset → uses applied", + base: Config{}, + applied: Config{MetricsPushURL: null.StringFrom("https://push.example"), TestRunToken: null.StringFrom("tok-1")}, + wantPushURL: null.StringFrom("https://push.example"), + wantRunToken: null.StringFrom("tok-1"), + }, + { + name: "applied unset, base populated → keeps base", + base: Config{MetricsPushURL: null.StringFrom("https://base.example"), TestRunToken: null.StringFrom("tok-base")}, + applied: Config{}, + wantPushURL: null.StringFrom("https://base.example"), + wantRunToken: null.StringFrom("tok-base"), + }, + { + name: "both populated → applied wins", + base: Config{MetricsPushURL: null.StringFrom("https://base.example"), TestRunToken: null.StringFrom("tok-base")}, + applied: Config{MetricsPushURL: null.StringFrom("https://applied.example"), TestRunToken: null.StringFrom("tok-applied")}, + wantPushURL: null.StringFrom("https://applied.example"), + wantRunToken: null.StringFrom("tok-applied"), + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + got := tc.base.Apply(tc.applied) + assert.Equal(t, tc.wantPushURL, got.MetricsPushURL) + assert.Equal(t, tc.wantRunToken, got.TestRunToken) + }) + } +} + +func TestConfig_Apply_MergesLogsFields(t *testing.T) { + t.Parallel() + + baseCfg := Config{ + LogsPushURL: null.StringFrom("https://base.example/logs"), + LogsLevel: null.StringFrom("warn"), + LogsLimit: null.IntFrom(100), + LogsPushPeriod: types.NewNullDuration(1*time.Second, true), + LogsMessageMaxSize: null.IntFrom(1024), + LogsAllowedLabels: []string{"level"}, + } + appliedCfg := Config{ + LogsPushURL: null.StringFrom("https://applied.example/logs"), + LogsLevel: null.StringFrom("info"), + LogsLimit: null.IntFrom(900), + LogsPushPeriod: types.NewNullDuration(3*time.Second, true), + LogsMessageMaxSize: null.IntFrom(10000), + LogsAllowedLabels: []string{"lz", "test_run_id"}, + } + + cases := []struct { + name string + base Config + applied Config + want Config + }{ + { + name: "applied populated, base unset → uses applied", + base: Config{}, + applied: appliedCfg, + want: appliedCfg, + }, + { + name: "applied unset, base populated → keeps base", + base: baseCfg, + applied: Config{}, + want: baseCfg, + }, + { + name: "both populated → applied wins", + base: baseCfg, + applied: appliedCfg, + want: appliedCfg, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + got := tc.base.Apply(tc.applied) + assert.Equal(t, tc.want.LogsPushURL, got.LogsPushURL) + assert.Equal(t, tc.want.LogsLevel, got.LogsLevel) + assert.Equal(t, tc.want.LogsLimit, got.LogsLimit) + assert.Equal(t, tc.want.LogsPushPeriod, got.LogsPushPeriod) + assert.Equal(t, tc.want.LogsMessageMaxSize, got.LogsMessageMaxSize) + assert.Equal(t, tc.want.LogsAllowedLabels, got.LogsAllowedLabels) + }) + } +} + func TestGetConsolidatedConfig(t *testing.T) { t.Parallel() config, warn, err := GetConsolidatedConfig(json.RawMessage(`{"token":"jsonraw"}`), nil, "", nil) diff --git a/cloudapi/errors.go b/cloudapi/errors.go index 669f3df76..0f18f689a 100644 --- a/cloudapi/errors.go +++ b/cloudapi/errors.go @@ -8,11 +8,7 @@ import ( "strings" ) -var ( - errNotAuthorized = errors.New("not allowed to upload result to k6 Cloud") - errNotAuthenticated = errors.New("failed to authenticate with k6 Cloud") - errUnknown = errors.New("an error occurred communicating with k6 Cloud") -) +var errUnknown = errors.New("an error occurred communicating with k6 Cloud") // ResponseError represents an error cause by talking to the API type ResponseError struct { diff --git a/cloudapi/errors_test.go b/cloudapi/errors_test.go index 631ab3e35..850853507 100644 --- a/cloudapi/errors_test.go +++ b/cloudapi/errors_test.go @@ -1,9 +1,13 @@ package cloudapi import ( + "io" + "net/http" + "strings" "testing" "github.com/stretchr/testify/assert" + "go.k6.io/k6/v2/internal/cloudapi/httperr" ) func TestContains(t *testing.T) { @@ -33,3 +37,53 @@ func TestErrorResponse_Error(t *testing.T) { expected := "(E123) " + msg1 + "\n " + msg2 + "\n field1: error1, error2" assert.Equal(t, expected, errResp.Error()) } + +func TestCheckResponse(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + response *http.Response + expectedError error + }{ + { + name: "nil response", + response: nil, + expectedError: errUnknown, + }, + { + name: "successful response 200", + response: &http.Response{StatusCode: http.StatusOK}, + }, + { + name: "unauthorized 401 with invalid JSON", + response: &http.Response{ + StatusCode: http.StatusUnauthorized, + Body: io.NopCloser(strings.NewReader("invalid json")), + }, + expectedError: httperr.ErrNotAuthenticated, + }, + { + name: "forbidden 403 with invalid JSON", + response: &http.Response{ + StatusCode: http.StatusForbidden, + Body: io.NopCloser(strings.NewReader("invalid json")), + }, + expectedError: httperr.ErrNotAuthorized, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + err := CheckResponse(tt.response) + + if tt.expectedError == nil { + assert.NoError(t, err) + return + } + assert.ErrorIs(t, err, tt.expectedError) + }) + } +} diff --git a/cloudapi/logs.go b/cloudapi/logs.go index 0c30d2347..eece3ea8b 100644 --- a/cloudapi/logs.go +++ b/cloudapi/logs.go @@ -94,7 +94,6 @@ func (c *Config) logtailConn(ctx context.Context, referenceID string, since time u.RawQuery = fmt.Sprintf(`query={test_run_id="%s"}&start=%d`, referenceID, since.UnixNano()) headers := make(http.Header) - headers.Add("Sec-WebSocket-Protocol", "token="+c.Token.String) headers.Add("Authorization", "token "+c.Token.String) headers.Add("X-K6testrun-Id", referenceID) @@ -140,7 +139,7 @@ func (c *Config) StreamLogsToLogger( msgBuffer := make(chan []byte, 10) defer close(msgBuffer) - var mostRecent int64 + var mostRecent atomic.Int64 go func() { for message := range msgBuffer { var m msg @@ -151,7 +150,7 @@ func (c *Config) StreamLogsToLogger( continue } ts := m.Log(logger) - atomic.StoreInt64(&mostRecent, ts) + mostRecent.Store(ts) } }() @@ -167,7 +166,7 @@ func (c *Config) StreamLogsToLogger( logger.WithError(err).Warn("error reading a log message from the cloud, trying to establish a fresh connection with the logs service...") //nolint:lll var since time.Time - if ts := atomic.LoadInt64(&mostRecent); ts > 0 { + if ts := mostRecent.Load(); ts > 0 { // add 1ns for avoid possible repetition since = time.Unix(0, ts).Add(time.Nanosecond) } else { diff --git a/cloudapi/logs_test.go b/cloudapi/logs_test.go index d373429a7..3f837f4aa 100644 --- a/cloudapi/logs_test.go +++ b/cloudapi/logs_test.go @@ -214,6 +214,7 @@ func TestStreamLogsToLogger(t *testing.T) { wsurl := strings.TrimPrefix(tb.ServerHTTP.URL, "http://") return Config{ LogsTailURL: null.NewString(fmt.Sprintf("ws://%s/api/v1/tail", wsurl), false), + Token: null.NewString("fake-token", true), } } @@ -236,7 +237,10 @@ func TestStreamLogsToLogger(t *testing.T) { defer cancel() tb := httpmultibin.NewHTTPMultiBin(t) - logtailHandleFunc(tb, func(conn *websocket.Conn, _ *http.Request) { + logtailHandleFunc(tb, func(conn *websocket.Conn, req *http.Request) { + assert.Equal(t, "token fake-token", req.Header.Get("Authorization")) + assert.Equal(t, "ref_id", req.Header.Get("X-K6testrun-Id")) + rawmsg := json.RawMessage(generateLogline("stream1", 1598282752000000000, "logline1")) err := conn.WriteJSON(rawmsg) require.NoError(t, err) @@ -278,11 +282,11 @@ func TestStreamLogsToLogger(t *testing.T) { return start, err } - var requestsCount uint64 + var requestsCount atomic.Uint64 tb := httpmultibin.NewHTTPMultiBin(t) logtailHandleFunc(tb, func(conn *websocket.Conn, req *http.Request) { - requests := atomic.AddUint64(&requestsCount, 1) + requests := requestsCount.Add(1) start, err := startFilter(*req.URL) require.NoError(t, err) @@ -357,12 +361,12 @@ func TestStreamLogsToLogger(t *testing.T) { return start, err } - var requestsCount uint64 + var requestsCount atomic.Uint64 t0 := time.Now() tb := httpmultibin.NewHTTPMultiBin(t) logtailHandleFunc(tb, func(conn *websocket.Conn, req *http.Request) { - requests := atomic.AddUint64(&requestsCount, 1) + requests := requestsCount.Add(1) start, err := startFilter(*req.URL) require.NoError(t, err) diff --git a/cmd/state/cloud_log_full.go b/cmd/state/cloud_log_full.go new file mode 100644 index 000000000..68cd30771 --- /dev/null +++ b/cmd/state/cloud_log_full.go @@ -0,0 +1,7 @@ +//go:build !lean + +package state + +import cloudlog "go.k6.io/k6/v2/internal/log/cloud" + +type cloudLogPusher = *cloudlog.Pusher diff --git a/cmd/state/cloud_log_lean.go b/cmd/state/cloud_log_lean.go new file mode 100644 index 000000000..2137c0b6f --- /dev/null +++ b/cmd/state/cloud_log_lean.go @@ -0,0 +1,5 @@ +//go:build lean + +package state + +type cloudLogPusher = any diff --git a/cmd/state/state.go b/cmd/state/state.go index ae5e7163a..9a3ec162b 100644 --- a/cmd/state/state.go +++ b/cmd/state/state.go @@ -44,6 +44,11 @@ const ( // to keep the command tree construction off the real network. ProvisionCatalogURL = "K6_PROVISION_CATALOG_URL" + // UsageReportURL overrides the endpoint the anonymous usage report is sent + // to. It defaults to the production endpoint and lets tests point it at an + // httptest server to observe the report. + UsageReportURL = "K6_USAGE_REPORT_URL" + // defaultBuildServiceURL defines the URL to the default (grafana hosted) build service defaultBuildServiceURL = "https://ingest.k6.io/builder/api/v1" @@ -90,6 +95,7 @@ type GlobalState struct { SecretsManager *secretsource.Manager CloudSecretSource *cloudsecrets.SecretSource + CloudLogPusher cloudLogPusher Usage *usage.Usage TestStatus *lib.TestStatus } @@ -107,13 +113,13 @@ func NewGlobalState(ctx context.Context) *GlobalState { stderrTTY := !isDumbTerm && (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) outMutex := &sync.Mutex{} stdout := &console.Writer{ - RawOutFd: int(os.Stdout.Fd()), //nolint:gosec + RawOutFd: int(os.Stdout.Fd()), Mutex: outMutex, Writer: colorable.NewColorable(os.Stdout), IsTTY: stdoutTTY, } stderr := &console.Writer{ - RawOutFd: int(os.Stderr.Fd()), //nolint:gosec + RawOutFd: int(os.Stderr.Fd()), Mutex: outMutex, Writer: colorable.NewColorable(os.Stderr), IsTTY: stderrTTY, @@ -185,15 +191,16 @@ func NewGlobalState(ctx context.Context) *GlobalState { // GlobalFlags contains global config values that apply for all k6 sub-commands. type GlobalFlags struct { - ConfigFilePath string - Quiet bool - NoColor bool - Address string - ProfilingEnabled bool - LogOutput string - SecretSource []string - LogFormat string - Verbose bool + ConfigFilePath string + Quiet bool + NoColor bool + Address string + ProfilingEnabled bool + LogOutput string + SecretSource []string + LogFormat string + LogNanosecondTimestamps bool + Verbose bool AutoExtensionResolution bool BuildServiceURL string @@ -240,6 +247,9 @@ func getFlags(defaultFlags GlobalFlags, env map[string]string, args []string) Gl if _, ok := env["NO_COLOR"]; ok { result.NoColor = true } + if _, ok := env["K6_LOG_NS_TIMESTAMPS"]; ok { + result.LogNanosecondTimestamps = true + } if _, ok := env["K6_PROFILING_ENABLED"]; ok { result.ProfilingEnabled = true } diff --git a/docs/design/020-distributed-execution-and-test-suites.md b/docs/design/020-distributed-execution-and-test-suites.md index b56ad8f29..0694ee150 100644 --- a/docs/design/020-distributed-execution-and-test-suites.md +++ b/docs/design/020-distributed-execution-and-test-suites.md @@ -298,9 +298,9 @@ Let's get back to my actual proposal of a `execution.Controller` API + new `k6 c To show that the proposed solution is a viable one, I'll go through the 9 items from the list in the "Missing Parts in the Current State" section and show how the proposal can be used to easily implement them: 1. `k6 coordinator` assembles the script and its dependencies into a [.tar archive bundle](https://k6.io/docs/misc/archive-command/) and distributes it to all `k6 agent` nodes automatically once they connect to it (either pull-based or push-based). -2. I showed how `setup()` can be cooperatively execuded only on a single instance and how its result can be distributed to the rest of the instances with the `GetOrCreateData()` pseudo-API described in the "Instance Synchronization API" section. +2. I showed how `setup()` can be cooperatively executed only on a single instance and how its result can be distributed to the rest of the instances with the `GetOrCreateData()` pseudo-API described in the "Instance Synchronization API" section. 3. Similarly, in the "Instance Synchronization API" section I showed how the `SignalAndWait()` pseudo-API can be used to ensure that all instances have finished initializing their VUs and executed `setup()` before they start the actual test execution simultaneously -4. Most failures will be handled by the coordinator and propaged to waiting instances. For the best UX, instances might subscribe to a special error channel so they can be interupted even during the test execution. +4. Most failures will be handled by the coordinator and propagated to waiting instances. For the best UX, instances might subscribe to a special error channel so they can be interrupted even during the test execution. 5. Metric collection was described in the "Metrics Handling" section, and the coordinator will be able to stop the test if a threshold is crossed. 6. We can probably handle `test.abort()` calls from an instance with a `GetOrCreateData()` call that uses a special `dataID` watched by the coordinator, but it might be simpler to just add a custom API for that... :thinking: 7. Detecting when the test has finished is trivial with `SignalAndWait()`. diff --git a/docs/features/000-TEMPLATE.md b/docs/features/000-TEMPLATE.md new file mode 100644 index 000000000..d52a12d6d --- /dev/null +++ b/docs/features/000-TEMPLATE.md @@ -0,0 +1,23 @@ +# Title + + + +## Problem + + + +## Significance + + + +## Cost of inaction + + + +## Desired state + + + +## Out of scope + + diff --git a/docs/features/README.md b/docs/features/README.md new file mode 100644 index 000000000..5d24364d2 --- /dev/null +++ b/docs/features/README.md @@ -0,0 +1,31 @@ +# Feature briefs + +A feature brief is a short Markdown document that describes a user problem and explains why it matters. You submit it as a pull request and the team reviews it before any implementation work begins. Keeping briefs in the repo means the whole proposal history lives alongside the code. + +## When to write one + +Write a brief for new features or significant changes to existing behaviour. You do **not** need one for bug fixes, small improvements, or refactors. + +## How to submit + +1. Copy [`000-TEMPLATE.md`](000-TEMPLATE.md) to a new file named `NNN-short-title.md`, where `NNN` is the next available number (e.g. `007-websocket-multiplexing.md`). +2. Fill in every section, deleting the HTML comment guidance as you go. +3. Open a PR that adds only that file under `docs/features/`. + +## Review process + +Briefs are reviewed as pull requests, with discussion happening in the PR comments. Once a brief is merged we treat it as final, so any follow-up changes go into a new brief. + +## Constraints + +| Constraint | Limit | +|---|---| +| Total prose | 1,200 words (soft target: 800) | +| Problem statement | ≤ 3 sentences | +| "Cost of inaction" section | Required | + +The word count is enforced in CI. Template headings and HTML comments do not count towards it. + +--- + +For general contribution guidelines see [`CONTRIBUTING.md`](../../CONTRIBUTING.md). diff --git a/errext/abort_reason.go b/errext/abort_reason.go index 429969866..a4dbf920f 100644 --- a/errext/abort_reason.go +++ b/errext/abort_reason.go @@ -31,8 +31,7 @@ func WithAbortReasonIfNone(err error, abortReason AbortReason) error { if err == nil { return nil // No error, do nothing } - var arerr HasAbortReason - if errors.As(err, &arerr) { + if _, ok := errors.AsType[HasAbortReason](err); ok { // The given error already has an abort reason, do nothing return err } diff --git a/errext/exit_code.go b/errext/exit_code.go index d141a3a72..5e7de1294 100644 --- a/errext/exit_code.go +++ b/errext/exit_code.go @@ -25,8 +25,7 @@ func WithExitCodeIfNone(err error, exitCode exitcodes.ExitCode) error { // No error, do nothing return nil } - var ecerr HasExitCode - if errors.As(err, &ecerr) { + if _, ok := errors.AsType[HasExitCode](err); ok { // The given error already has an exit code, do nothing return err } diff --git a/errext/fields.go b/errext/fields.go index 667fe3e44..5292ed1b2 100644 --- a/errext/fields.go +++ b/errext/fields.go @@ -28,8 +28,7 @@ func WithFields(err error, fields map[string]any) error { // FieldsFromErr returns structured log fields attached to err. func FieldsFromErr(err error) map[string]any { fields := map[string]any{} - var ferr fieldsError - if errors.As(err, &ferr) { + if ferr, ok := errors.AsType[fieldsError](err); ok { maps.Copy(fields, ferr.fields) } return fields diff --git a/errext/format.go b/errext/format.go index afd291106..7e2618b9a 100644 --- a/errext/format.go +++ b/errext/format.go @@ -14,13 +14,11 @@ func Format(err error) (string, map[string]any) { errText := err.Error() fields := FieldsFromErr(err) - var xerr Exception - if errors.As(err, &xerr) { + if xerr, ok := errors.AsType[Exception](err); ok { errText = xerr.StackTrace() fields["source"] = "stacktrace" } - var herr HasHint - if errors.As(err, &herr) { + if herr, ok := errors.AsType[HasHint](err); ok { fields["hint"] = herr.Hint() } diff --git a/errext/hint.go b/errext/hint.go index 1b053b120..93b14b16f 100644 --- a/errext/hint.go +++ b/errext/hint.go @@ -32,8 +32,7 @@ func (wh withHintError) Unwrap() error { func (wh withHintError) Hint() string { hint := wh.hint - var oldhint HasHint - if errors.As(wh.error, &oldhint) { + if oldhint, ok := errors.AsType[HasHint](wh.error); ok { // The given error already had a hint, wrap it hint = hint + " (" + oldhint.Hint() + ")" } diff --git a/examples/async_metric_context.js b/examples/async_metric_context.js new file mode 100644 index 000000000..39b5c4746 --- /dev/null +++ b/examples/async_metric_context.js @@ -0,0 +1,63 @@ +// Run with: +// k6 run --features async-metric-context examples/async_metric_context.js + +import { check } from 'k6'; +import exec from 'k6/execution'; +import { Counter } from 'k6/metrics'; + +const callbackPhases = new Counter('callback_phases'); + +export const options = { + iterations: 1, + thresholds: { + callback_phases: ['count == 6'], + checks: ['rate == 1'], + }, +}; + +function setContext(owner, phase) { + exec.vu.metrics.tags.owner = owner; + exec.vu.metrics.tags.phase = phase; + exec.vu.metrics.metadata.trace = owner; + exec.vu.metrics.metadata.step = phase; +} + +function contextMatches(owner, phase) { + return exec.vu.metrics.tags.owner === owner + && exec.vu.metrics.tags.phase === phase + && exec.vu.metrics.metadata.trace === owner + && exec.vu.metrics.metadata.step === phase; +} + +export default async function () { + let releaseAlpha; + let releaseBeta; + const alphaGate = new Promise(resolve => { releaseAlpha = resolve; }); + const betaGate = new Promise(resolve => { releaseBeta = resolve; }); + + const register = (owner, gate) => { + setContext(owner, 'registered'); + return gate.then(async () => { + for (const phase of ['first', 'second', 'third']) { + setContext(owner, phase); + await Promise.resolve(); + callbackPhases.add(1); + check(null, { + [`${owner}:${phase} kept its context`]: () => contextMatches(owner, phase), + }); + } + }); + }; + + const alpha = register('alpha', alphaGate); + const beta = register('beta', betaGate); + + setContext('root', 'waiting'); + releaseBeta(); + releaseAlpha(); + await Promise.all([alpha, beta]); + + check(null, { + 'the awaiting context was restored': () => contextMatches('root', 'waiting'), + }); +} diff --git a/examples/browser/async_metric_context.js b/examples/browser/async_metric_context.js new file mode 100644 index 000000000..ce3cac48d --- /dev/null +++ b/examples/browser/async_metric_context.js @@ -0,0 +1,67 @@ +// Run with: +// k6 run --features async-metric-context examples/browser/async_metric_context.js + +import { browser } from 'k6/browser'; +import { check, group } from 'k6'; +import exec from 'k6/execution'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + iterations: 1, + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + 'browser_http_req_duration{group:::navigation,owner:browser-navigation}': ['max >= 0'], + checks: ['rate == 1'], + }, +}; + +function contextMatches(owner, groupName) { + return exec.vu.metrics.tags.owner === owner + && exec.vu.metrics.tags.group === groupName + && exec.vu.metrics.metadata.trace === owner; +} + +export default async function () { + const page = await browser.newPage(); + exec.vu.metrics.tags.owner = 'root'; + exec.vu.metrics.metadata.trace = 'root'; + + let observedRequest = false; + try { + await group('navigation', async () => { + exec.vu.metrics.tags.owner = 'browser-navigation'; + exec.vu.metrics.metadata.trace = 'browser-navigation'; + + page.on('request', request => { + if (!request.url().startsWith('https://quickpizza.grafana.com/')) { + return; + } + observedRequest = true; + check(null, { + 'request callback kept its registration context': () => ( + contextMatches('browser-navigation', '::navigation') + ), + }); + }); + + await page.goto('https://quickpizza.grafana.com/', { waitUntil: 'domcontentloaded' }); + await page.title(); + page.url(); + }); + + check(null, { + 'navigation emitted a request event': () => observedRequest, + 'the context outside the group was restored': () => contextMatches('root', ''), + }); + } finally { + await page.close(); + } +} diff --git a/examples/browser/colorscheme.js b/examples/browser/colorscheme.js index 4141f0909..219310d62 100644 --- a/examples/browser/colorscheme.js +++ b/examples/browser/colorscheme.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -33,6 +34,8 @@ export default async function() { 'isDarkColorScheme': p => p.evaluate(() => window.matchMedia('(prefers-color-scheme: dark)').matches) }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/cookies.js b/examples/browser/cookies.js index 48f588b60..3d4d01a83 100644 --- a/examples/browser/cookies.js +++ b/examples/browser/cookies.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -120,6 +121,8 @@ export default async function () { await check(await context.cookies(), { 'number of cookies should be zero': c => c.length === 0, }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/device_emulation.js b/examples/browser/device_emulation.js index 7e62ab26a..ed4db21c0 100644 --- a/examples/browser/device_emulation.js +++ b/examples/browser/device_emulation.js @@ -1,4 +1,5 @@ import { browser, devices } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -45,6 +46,8 @@ export default async function() { if (!__ENV.K6_BROWSER_HEADLESS) { await page.waitForTimeout(10000); } + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/dispatch.js b/examples/browser/dispatch.js index 5c12bc327..f77e59273 100644 --- a/examples/browser/dispatch.js +++ b/examples/browser/dispatch.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -33,6 +34,8 @@ export default async function() { return text == 'Contact us'; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/evaluate.js b/examples/browser/evaluate.js index 6801adf83..7924204a3 100644 --- a/examples/browser/evaluate.js +++ b/examples/browser/evaluate.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -39,6 +40,8 @@ export default async function() { return n == 25; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/fillform.js b/examples/browser/fillform.js index c3cf6cfb1..73f45b9eb 100644 --- a/examples/browser/fillform.js +++ b/examples/browser/fillform.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -54,6 +55,8 @@ export default async function() { return cookies.find(c => c.name == 'AWSALB') !== undefined; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/framelocator.js b/examples/browser/framelocator.js index d12c4685a..98ded9821 100644 --- a/examples/browser/framelocator.js +++ b/examples/browser/framelocator.js @@ -1,5 +1,6 @@ import { browser } from 'k6/browser'; import { check } from 'k6'; +import exec from 'k6/execution'; export const options = { scenarios: { @@ -18,9 +19,11 @@ export const options = { } export default async function () { - const page = await browser.newPage(); + let page; try { + page = await browser.newPage(); + // create a page with an iframe await page.setContent(` @@ -65,7 +68,16 @@ export default async function () { 'found heading inside iframe': (text) => text === 'Inside iframe', }); + } catch (error) { + // This script is run as a smoke test in the build workflow to verify the + // browser Docker image. exec.test.fail() is used here (instead of fail()) + // to produce a non-zero exit code on exception, failing that CI check. + // error.message is empty for rejected promises, so fall back to error.toString() + const message = error.message || error.toString(); + exec.test.fail(`Browser iteration failed: ${message}`); } finally { - await page.close(); + if (page) { + await page.close(); + } } } diff --git a/examples/browser/getattribute.js b/examples/browser/getattribute.js index 57dcbc1e3..80db83272 100644 --- a/examples/browser/getattribute.js +++ b/examples/browser/getattribute.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -31,6 +32,8 @@ export default async function() { return await e.getAttribute('mode') === 'light'; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/grant_permission.js b/examples/browser/grant_permission.js index d306e226e..c2fdfbabd 100644 --- a/examples/browser/grant_permission.js +++ b/examples/browser/grant_permission.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -29,6 +30,8 @@ export default async function() { await page.goto('https://quickpizza.grafana.com/test.k6.io/'); await page.screenshot({ path: `example-chromium.png` }); await context.clearPermissions(); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/hosts.js b/examples/browser/hosts.js index 4baf41477..48d22c39f 100644 --- a/examples/browser/hosts.js +++ b/examples/browser/hosts.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -29,6 +30,8 @@ export default async function() { await check(res, { 'null response': r => r === null, }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/locator.js b/examples/browser/locator.js index b5d9c0860..424d7eee0 100644 --- a/examples/browser/locator.js +++ b/examples/browser/locator.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -68,6 +69,8 @@ export default async function() { tails.click(), ]); console.log(await currentBet.innerText()); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/locator_pom.js b/examples/browser/locator_pom.js index 6be6e20c9..ccab2c69c 100644 --- a/examples/browser/locator_pom.js +++ b/examples/browser/locator_pom.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -71,6 +72,8 @@ export default async function() { console.log("Current bet:", await bet.current()); await bet.heads(); console.log("Current bet:", await bet.current()); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/multiple-scenario.js b/examples/browser/multiple-scenario.js index d050e3203..bba8b9cfe 100644 --- a/examples/browser/multiple-scenario.js +++ b/examples/browser/multiple-scenario.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -37,6 +38,8 @@ export async function messages() { try { await page.goto('https://quickpizza.grafana.com/my_messages.php', { waitUntil: 'networkidle' }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } @@ -47,6 +50,8 @@ export async function news() { try { await page.goto('https://quickpizza.grafana.com/news.php', { waitUntil: 'networkidle' }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/page_waitForEvent.js b/examples/browser/page_waitForEvent.js index 2424328ee..73bc9ef6d 100644 --- a/examples/browser/page_waitForEvent.js +++ b/examples/browser/page_waitForEvent.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -40,6 +41,8 @@ export default async function() { const response = await responsePromise; console.log(`Response received: ${response.url()}`); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/pageon-metric.js b/examples/browser/pageon-metric.js index e50d6326c..22eff4ab1 100644 --- a/examples/browser/pageon-metric.js +++ b/examples/browser/pageon-metric.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -28,6 +29,8 @@ export default async function() { try { await page.goto('https://quickpizza.grafana.com/test.k6.io/?q=abc123'); await page.goto('https://quickpizza.grafana.com/test.k6.io/?q=def456'); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/pageon.js b/examples/browser/pageon.js index ae22fe317..19cccfff4 100644 --- a/examples/browser/pageon.js +++ b/examples/browser/pageon.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -39,6 +40,8 @@ export default async function() { })); await page.evaluate(() => console.log('this is a console.log message', 42)); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/querying.js b/examples/browser/querying.js index 1d0ef479d..7c24b4c35 100644 --- a/examples/browser/querying.js +++ b/examples/browser/querying.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -34,6 +35,8 @@ export default async function() { return await e.textContent() === 'QuickPizza Legacy'; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/remote.js b/examples/browser/remote.js new file mode 100644 index 000000000..5cedb44fd --- /dev/null +++ b/examples/browser/remote.js @@ -0,0 +1,41 @@ +import { chromium } from 'k6/browser'; + +// Connect to an external CDP browser with a URL computed at runtime. +// +// For instance, you could fetch that URL from your browser provider's API, +// in setup(), and pass it to the iterations: +// +// import http from 'k6/http'; +// +// export function setup() { +// const res = http.post('https://provider.example/v1/sessions', /* ... */); +// return { wsURL: res.json().connectUrl }; +// } +// +// export default async function (data) { +// const browser = await chromium.connectOverCDP(data.wsURL); +// // ... +// } +// +// For a quick local run, start Chrome with --remote-debugging-port=9222 and pass +// its ws URL via an env var — e.g., CDP_WS_URL, simulating a URL computed at runtime: +// CDP_WS_URL=ws://localhost:9222/devtools/browser/ k6 run examples/browser/remote.js +// +// Note this is different from K6_BROWSER_WS_URL, which covers a different path where +// k6 users can provide the ws URL via an env var, not computed at runtime. + +export default async function () { + const browser = await chromium.connectOverCDP(__ENV.CDP_WS_URL); + const page = await browser.newPage(); + + try { + await page.goto('https://quickpizza.grafana.com/', { waitUntil: 'networkidle' }); + console.log(`title: ${await page.title()}`); + } finally { + await page.close(); + // For user-managed browsers (those created via chromium.connectOverCDP), + // close() is exposed so you can release the connection early. + // It's optional, k6 auto-closes it at iteration end, just like k6-managed browsers. + await browser.close(); + } +} diff --git a/examples/browser/route.js b/examples/browser/route.js index ec50f69ab..e38bfd39d 100644 --- a/examples/browser/route.js +++ b/examples/browser/route.js @@ -1,5 +1,5 @@ import { browser } from 'k6/browser'; -import { check } from 'k6'; +import { check, fail } from 'k6'; export const options = { scenarios: { @@ -64,6 +64,8 @@ export default async function () { 'pizza API status is 200': (r) => r.status() === 200, 'pizza API URL is correct': (r) => r.url() === 'https://quickpizza.grafana.com/api/pizza', }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/route_continue.js b/examples/browser/route_continue.js index d76efa3ec..604bcdc71 100644 --- a/examples/browser/route_continue.js +++ b/examples/browser/route_continue.js @@ -1,4 +1,4 @@ -import { sleep } from 'k6'; +import { sleep, fail } from 'k6'; import { browser } from 'k6/browser'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; @@ -37,6 +37,8 @@ export default async function () { check(null, { pizzaName: await e.innerText() === 'Name: My Pizza', }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/screenshot.js b/examples/browser/screenshot.js index 315e3a4f7..0b28d37a6 100644 --- a/examples/browser/screenshot.js +++ b/examples/browser/screenshot.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -25,6 +26,8 @@ export default async function() { await page.screenshot({ path: 'screenshot.png' }); // TODO: Assert this somehow. Upload as CI artifact or just an external `ls`? // Maybe even do a fuzzy image comparison against a preset known good screenshot? + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/throttle.js b/examples/browser/throttle.js index 28a47590a..72ebd4da8 100644 --- a/examples/browser/throttle.js +++ b/examples/browser/throttle.js @@ -1,4 +1,5 @@ import { browser, networkProfiles } from 'k6/browser'; +import { fail } from 'k6'; export const options = { scenarios: { @@ -46,6 +47,8 @@ export async function normal() { try { await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } @@ -59,6 +62,8 @@ export async function networkThrottled() { await page.throttleNetwork(networkProfiles["Slow 3G"]); await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } @@ -72,6 +77,8 @@ export async function cpuThrottled() { await page.throttleCPU({ rate: 4 }); await page.goto('https://quickpizza.grafana.com/test.k6.io/', { waitUntil: 'networkidle' }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/browser/waitforfunction.js b/examples/browser/waitforfunction.js index ea939b6d9..adea8f9a4 100644 --- a/examples/browser/waitforfunction.js +++ b/examples/browser/waitforfunction.js @@ -1,4 +1,5 @@ import { browser } from 'k6/browser'; +import { fail } from 'k6'; import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; export const options = { @@ -41,6 +42,8 @@ export default async function() { return await e.innerHTML() === 'Hello'; } }); + } catch (error) { + fail(`Browser iteration failed: ${error.message}`); } finally { await page.close(); } diff --git a/examples/experimental/writable-streams.js b/examples/experimental/writable-streams.js new file mode 100644 index 000000000..3c2c4f8e3 --- /dev/null +++ b/examples/experimental/writable-streams.js @@ -0,0 +1,47 @@ +import { WritableStream } from 'k6/experimental/streams' +import { setTimeout } from 'k6/timers' + +function delay(milliseconds) { + return new Promise((resolve) => setTimeout(resolve, milliseconds)) +} + +// loggingStream returns a WritableStream whose underlying sink writes chunks slowly enough +// for the producer to observe and respect backpressure through writer.ready. +function loggingStream() { + const chunks = [] + + return new WritableStream({ + start() { + console.log('stream opened') + }, + async write(chunk) { + await delay(200) + + chunks.push(chunk) + console.log(`wrote ${chunk}`) + }, + close() { + console.log(`stream closed after receiving: ${chunks.join(', ')}`) + }, + abort(reason) { + console.log(`stream aborted: ${reason}`) + }, + }, { highWaterMark: 1 }) +} + +export default async function () { + const stream = loggingStream() + const writer = stream.getWriter() + const writes = [] + + for (let i = 1; i <= 5; i++) { + await writer.ready + writes.push(writer.write(`chunk ${i}`)) + } + + await Promise.all(writes) + await writer.close() + await writer.closed + + console.log('we are done') +} diff --git a/examples/grpc_server/go.mod b/examples/grpc_server/go.mod index f86a4e007..3799f7135 100644 --- a/examples/grpc_server/go.mod +++ b/examples/grpc_server/go.mod @@ -6,13 +6,13 @@ replace go.k6.io/k6 => ../../ require ( go.k6.io/k6/v2 v2.0.0-rc1 - google.golang.org/grpc v1.80.0 + google.golang.org/grpc v1.83.2 ) require ( - golang.org/x/net v0.55.0 // indirect - golang.org/x/sys v0.45.0 // indirect - golang.org/x/text v0.37.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 // indirect + golang.org/x/net v0.58.0 // indirect + golang.org/x/sys v0.47.0 // indirect + golang.org/x/text v0.41.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect google.golang.org/protobuf v1.36.11 // indirect ) diff --git a/examples/grpc_server/go.sum b/examples/grpc_server/go.sum index 410a7e13f..489eb6fef 100644 --- a/examples/grpc_server/go.sum +++ b/examples/grpc_server/go.sum @@ -14,27 +14,27 @@ go.k6.io/k6/v2 v2.0.0-rc1 h1:el4d7gjBcGbxIxtKYgfpVS9728g9HXDZ6/t1KQn1w8M= go.k6.io/k6/v2 v2.0.0-rc1/go.mod h1:yzYXN2/xmcjLHgQl1MLd7DObVncNCPH3chZ3H9HPmck= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I= -go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0= -go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM= -go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY= -go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg= -go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg= -go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw= -go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A= -go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A= -go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0= -golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8= -golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww= -golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY= -golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc= -golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38= +go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU= +go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc= +go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc= +go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo= +go.opentelemetry.io/otel/sdk v1.44.0 h1:nHYwb9lK+fJPU/dnT6s7W7Z8itMWyqrnVfbheVYrZ58= +go.opentelemetry.io/otel/sdk v1.44.0/go.mod h1:Osuydd3Se74nqjAKxid74N5eC+jfEqfTegHRnq58oK0= +go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRks6si09iEfI= +go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA= +go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk= +go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE= +golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 h1:m8qni9SQFH0tJc1X0vmnpw/0t+AImlSvp30sEupozUg= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= -google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM= -google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:mZHHdPZl0dbGHCflZgAq/Q468DWVFcU2whhB2KAo8fk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= +google.golang.org/grpc v1.83.2 h1:EManeRomTObA0BU7I8vXgg/78uE5MJ9M8B39EX2WscU= +google.golang.org/grpc v1.83.2/go.mod h1:YPI1hK3kDked6iHvgX3tR0y+nX/qpMFKhPgFsokw1S8= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= diff --git a/ext/ext.go b/ext/ext.go index fc8e9f726..f020e230e 100644 --- a/ext/ext.go +++ b/ext/ext.go @@ -134,7 +134,7 @@ func extractModuleInfo(mod any) (path, version string) { t := reflect.TypeOf(mod) switch t.Kind() { - case reflect.Ptr: + case reflect.Pointer: if t.Elem() != nil { path = t.Elem().PkgPath() } diff --git a/go.mod b/go.mod index 018d32370..f7caf94d5 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,17 @@ module go.k6.io/k6/v2 -go 1.25.0 - -toolchain go1.25.11 +go 1.26.0 require ( - buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260331160422-eae785f0a21d.1 + buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260707164124-2360da55afce.1 github.com/Azure/go-ntlmssp v0.1.1 github.com/Masterminds/semver/v3 v3.5.0 github.com/PuerkitoBio/goquery v1.12.0 github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 - github.com/andybalholm/brotli v1.2.1 + github.com/andybalholm/brotli v1.2.3 github.com/bufbuild/protocompile v0.14.1 github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d - github.com/evanw/esbuild v0.28.0 + github.com/evanw/esbuild v0.28.2 github.com/fatih/color v1.19.0 github.com/go-json-experiment/json v0.0.0-20250211171154-1ae217ad3535 github.com/go-sourcemap/sourcemap v2.1.4+incompatible @@ -21,21 +19,22 @@ require ( github.com/golang/protobuf v1.5.4 github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 - github.com/grafana/k6-cloud-openapi-client-go v0.0.2 + github.com/grafana/k6-cloud-openapi-client-go v0.0.3-0.20260716125012-97c96146fa7c github.com/grafana/k6provider v0.5.0 - github.com/grafana/sobek v0.0.0-20260429085637-a66d4790012b + github.com/grafana/sobek v0.0.0-20260908083152-4698bc773ae7 + github.com/grafana/sobek-webapi-encoding v0.1.0 github.com/grafana/xk6-dashboard-assets v0.1.2 github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/influxdata/influxdb1-client v0.0.0-20190402204710-8ff2fc3824fc github.com/jhump/protoreflect v1.18.0 - github.com/klauspost/compress v1.18.5 + github.com/klauspost/compress v1.20.0 github.com/mattn/go-colorable v0.1.15 - github.com/mattn/go-isatty v0.0.22 - github.com/mccutchen/go-httpbin/v2 v2.23.0 + github.com/mattn/go-isatty v0.0.24 + github.com/mccutchen/go-httpbin/v2 v2.25.0 github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd github.com/mstoykov/envconfig v1.5.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c - github.com/prometheus/client_golang v1.23.2 + github.com/prometheus/client_golang v1.24.1 github.com/prometheus/client_model v0.6.2 github.com/r3labs/sse/v2 v2.10.0 github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e @@ -43,28 +42,28 @@ require ( github.com/spf13/afero v1.1.2 github.com/spf13/cobra v1.4.0 github.com/spf13/pflag v1.0.5 - github.com/stretchr/testify v1.11.1 + github.com/stretchr/testify v1.12.1 github.com/tidwall/gjson v1.19.0 - go.opentelemetry.io/otel v1.44.0 - go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.44.0 - go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.44.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.44.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.44.0 - go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.44.0 - go.opentelemetry.io/otel/metric v1.44.0 - go.opentelemetry.io/otel/sdk v1.44.0 - go.opentelemetry.io/otel/sdk/metric v1.44.0 - go.opentelemetry.io/otel/trace v1.44.0 - go.opentelemetry.io/proto/otlp v1.10.0 + go.opentelemetry.io/otel v1.46.0 + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.46.0 + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.46.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0 + go.opentelemetry.io/otel/metric v1.46.0 + go.opentelemetry.io/otel/sdk v1.46.0 + go.opentelemetry.io/otel/sdk/metric v1.46.0 + go.opentelemetry.io/otel/trace v1.46.0 + go.opentelemetry.io/proto/otlp v1.11.0 go.uber.org/goleak v1.3.0 - golang.org/x/crypto v0.53.0 - golang.org/x/crypto/x509roots/fallback v0.0.0-20260609182332-5f2de1a9f1e2 - golang.org/x/net v0.56.0 - golang.org/x/sync v0.21.0 - golang.org/x/term v0.44.0 + golang.org/x/crypto v0.56.0 + golang.org/x/crypto/x509roots/fallback v0.0.0-20260811175631-f44d03d253a1 + golang.org/x/net v0.58.0 + golang.org/x/sync v0.22.0 + golang.org/x/term v0.45.0 golang.org/x/time v0.15.0 - google.golang.org/grpc v1.81.1 - google.golang.org/protobuf v1.36.11 + google.golang.org/grpc v1.84.0 + google.golang.org/protobuf v1.36.12 gopkg.in/guregu/null.v3 v3.3.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -76,12 +75,11 @@ require ( github.com/cenkalti/backoff/v5 v5.0.3 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/chromedp/sysutil v1.1.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/dlclark/regexp2 v1.12.0 // indirect - github.com/go-logr/logr v1.4.3 // indirect + github.com/dlclark/regexp2/v2 v2.7.2 // indirect + github.com/go-logr/logr v1.4.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jhump/protoreflect/v2 v2.0.0-beta.1 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect @@ -89,17 +87,16 @@ require ( github.com/nxadm/tail v1.4.11 // indirect github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.39.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/common v0.66.1 // indirect - github.com/prometheus/procfs v0.16.1 // indirect + github.com/prometheus/common v0.70.1 // indirect + github.com/prometheus/procfs v0.21.1 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect - go.yaml.in/yaml/v2 v2.4.2 // indirect - golang.org/x/sys v0.46.0 // indirect - golang.org/x/text v0.38.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect + golang.org/x/sys v0.47.0 // indirect + golang.org/x/text v0.41.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 // indirect gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect gopkg.in/validator.v2 v2.0.1 // indirect ) diff --git a/go.sum b/go.sum index 14b9318ca..b44c0d109 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ buf.build/gen/go/gogo/protobuf/protocolbuffers/go v1.36.11-20240617172848-e1dbca2775a7.1 h1:FlXwksX9wjddXoyW88Jw3tz1DEGbXBSGZW6fg0frZ0M= buf.build/gen/go/gogo/protobuf/protocolbuffers/go v1.36.11-20240617172848-e1dbca2775a7.1/go.mod h1:mwDA6SccUlW4ebUkJTpKoHZzCrLFh/WI48oQRvUTGAA= -buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260331160422-eae785f0a21d.1 h1:OyFGRpH4F78kDv9OdkRyzfrBnnJO97nYCP5dIfkOzDk= -buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260331160422-eae785f0a21d.1/go.mod h1:6rM4oiNLtvSABJBFC+GReCtGUaWLYwhsadnb9FgJ/2k= +buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260707164124-2360da55afce.1 h1:M4YtW8f34aXlYH7qlusABkaxVkhn3n6gOVjATbhqVfQ= +buf.build/gen/go/prometheus/prometheus/protocolbuffers/go v1.36.11-20260707164124-2360da55afce.1/go.mod h1:6rM4oiNLtvSABJBFC+GReCtGUaWLYwhsadnb9FgJ/2k= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/Azure/go-ntlmssp v0.1.1 h1:l+FM/EEMb0U9QZE7mKNEDw5Mu3mFiaa2GKOoTSsNDPw= github.com/Azure/go-ntlmssp v0.1.1/go.mod h1:NYqdhxd/8aAct/s4qSYZEerdPuH1liG2/X9DiVTbhpk= @@ -12,8 +12,8 @@ github.com/PuerkitoBio/goquery v1.12.0 h1:pAcL4g3WRXekcB9AU/y1mbKez2dbY2AajVhtkO github.com/PuerkitoBio/goquery v1.12.0/go.mod h1:802ej+gV2y7bbIhOIoPY5sT183ZW0YFofScC4q/hIpQ= github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 h1:k+1+doEm31k0rRjCjLnGG3YRkuO9ljaEyS2ajZd6GK8= github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5/go.mod h1:5Q4+CyR7+Q3VMG8f78ou+QSX/BNUNUx5W48eFRat8DQ= -github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro= -github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= +github.com/andybalholm/brotli v1.2.3 h1:8H1qwOkl2LPfjf3YezB90JnCliZb6SInJ/OJkEbA5NQ= +github.com/andybalholm/brotli v1.2.3/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM= github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= @@ -34,16 +34,15 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.12.0 h1:0j4c5qQmnC6XOWNjP3PIXURXN2gWx76rd3KvgdPkCz8= -github.com/dlclark/regexp2 v1.12.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/dlclark/regexp2/v2 v2.7.2 h1:nBhTfGMNPCDloto6XZRFJ5wpi+lym2eUMHRBY0diX30= +github.com/dlclark/regexp2/v2 v2.7.2/go.mod h1:avUrQvPaLz2DrFNHJF0taWAFFX2C1GMSSoeiqFjcBmU= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/evanw/esbuild v0.28.0 h1:V96ghtc5p5JnNUQIUsc5H3kr+AcFcMqOJll2ZmJW6Lo= -github.com/evanw/esbuild v0.28.0/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= +github.com/evanw/esbuild v0.28.2 h1:A2uETn4jrQTcXaT/shwTDTYBxDjl7fV7nXmUrJxfA2w= +github.com/evanw/esbuild v0.28.2/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48= github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w= github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -55,8 +54,8 @@ github.com/go-json-experiment/json v0.0.0-20250211171154-1ae217ad3535/go.mod h1: github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= -github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8= +github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q= @@ -65,6 +64,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gofrs/uuid/v5 v5.3.1 h1:aPx49MwJbekCzOyhZDjJVb0hx3A0KLjlbLx6p2gY0p0= github.com/gofrs/uuid/v5 v5.3.1/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= @@ -92,18 +93,20 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/grafana/k6-cloud-openapi-client-go v0.0.2 h1:YzMFCaKiLA8UFQjCrrhrtkxvkzcRt8ENMeo+f7ndV7k= -github.com/grafana/k6-cloud-openapi-client-go v0.0.2/go.mod h1:RBPBP7qIR/K6qzQEQYESVhp/XJspiBTOyBEBCbPXrvI= +github.com/grafana/k6-cloud-openapi-client-go v0.0.3-0.20260716125012-97c96146fa7c h1:a6t4uyt2GDUWUa3zML524CuKtuqPvGaYE3JfBO5Xww0= +github.com/grafana/k6-cloud-openapi-client-go v0.0.3-0.20260716125012-97c96146fa7c/go.mod h1:RhZGBq6e90zSi0EME83c6S1qkOzD8pP0MNETqieWX0k= github.com/grafana/k6provider v0.5.0 h1:SwItWPOMQHfRytezQBKpeXLxjNmcyD/x9PTA28pEbwg= github.com/grafana/k6provider v0.5.0/go.mod h1:R9WKthnwk96E47m6w/mnC13hhBYTn5iMdP5caFt/t5k= -github.com/grafana/sobek v0.0.0-20260429085637-a66d4790012b h1:mM/qn1luOrRZHT3G+405JMdCx4mGxeLKpOkVBa5+lFw= -github.com/grafana/sobek v0.0.0-20260429085637-a66d4790012b/go.mod h1:8pB+ag4SAbqtDxh1LNTeUI62/5f8mmEACImwbDHoUC0= +github.com/grafana/sobek v0.0.0-20260908083152-4698bc773ae7 h1:f26plQRDZSSPpQe7XgbFaFQQpGk8aplm7FFTXia+n+s= +github.com/grafana/sobek v0.0.0-20260908083152-4698bc773ae7/go.mod h1:+udxaTNa4JhADyCEyHjZDviHJyW8M9u7BX33b9zeskc= +github.com/grafana/sobek-webapi-encoding v0.1.0 h1:2qf2pUiI6j33vl2etSE0Id4mfSQ4Jfa0VVfUuSccAkI= +github.com/grafana/sobek-webapi-encoding v0.1.0/go.mod h1:IegnWm83cyZ8qTnfL6Rivmx7dpMVE+w3CblopwfL/go= github.com/grafana/xk6-dashboard-assets v0.1.2 h1:n2wqytPICn2ZYsKa9HE6GlvFXk+WjByMfT4eM+ms3gE= github.com/grafana/xk6-dashboard-assets v0.1.2/go.mod h1:SeoRjvmFF8UhLIFDfvjqqwBtE8MtaIPYIDWqVJZEHDo= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 h1:5VipnvEpbqr2gA2VbM+nYVbkIF28c5ZQfqCBQ5g2xfk= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0/go.mod h1:Hyl3n6Twe1hvtd9XUXDec4pTvgMSEixRuQKPTMH2bNs= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 h1:/Tnpcb2E0Pz/tN9s3bfEY2Q8ePCEX9iuS+cneUwncnw= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0/go.mod h1:zOBXOsUaBSjKgmH4OGzV1esUpR3oUSCPYVd2cUBjKYY= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -115,24 +118,23 @@ github.com/jhump/protoreflect/v2 v2.0.0-beta.1 h1:Dw1rslK/VotaUGYsv53XVWITr+5RCP github.com/jhump/protoreflect/v2 v2.0.0-beta.1/go.mod h1:D9LBEowZyv8/iSu97FU2zmXG3JxVTmNw21mu63niFzU= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE= -github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA= +github.com/klauspost/compress v1.20.0/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY= github.com/mattn/go-colorable v0.1.15/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= -github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4= -github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= -github.com/mccutchen/go-httpbin/v2 v2.23.0 h1:7hmWWSeVGlIhYvLpa1cFssF6lsQp+UKoZ5i5FvidIqI= -github.com/mccutchen/go-httpbin/v2 v2.23.0/go.mod h1:8hkN5rHf0QvJYEovZ8u/Dudtqyj6Z5gxQaW7scQfT0Y= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/mccutchen/go-httpbin/v2 v2.25.0 h1:YpPBoAIKYIyyPEwXZkEqBVIKwon02TK7CELwAkt1mLU= +github.com/mccutchen/go-httpbin/v2 v2.25.0/go.mod h1:8hkN5rHf0QvJYEovZ8u/Dudtqyj6Z5gxQaW7scQfT0Y= github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd h1:AC3N94irbx2kWGA8f/2Ks7EQl2LxKIRQYuT9IJDwgiI= github.com/mstoykov/atlas v0.0.0-20220811071828-388f114305dd/go.mod h1:9vRHVuLCjoFfE3GT06X0spdOAO+Zzo4AMjdIwUHBvAk= github.com/mstoykov/envconfig v1.5.0 h1:E2FgWf73BQt0ddgn7aoITkQHmgwAcHup1s//MsS5/f8= @@ -159,17 +161,16 @@ github.com/petermattis/goid v0.0.0-20260113132338-7c7de50cc741/go.mod h1:pxMtw7c github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= -github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_golang v1.24.1 h1:JnJkREXzWxUdCuPFpIWZiPispT9xVV59uiuyR2bPlnU= +github.com/prometheus/client_golang v1.24.1/go.mod h1:F+oSRECHg4sse5ucfYpYDeIv/hu68Zo0uoHKetWnzcE= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= -github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= -github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= -github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= -github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/prometheus/common v0.70.1 h1:1HvjP4D5oL3t8RsPlwxA9onvvStjtIHYE5XuuwOi/PY= +github.com/prometheus/common v0.70.1/go.mod h1:VdFUQDMZK3VLkurFUVhia6uys/0suUp86TJz5qbJRhc= +github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI= +github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY= github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0= github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= @@ -193,8 +194,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= github.com/tidwall/gjson v1.19.0 h1:xwxm7n691Uf3u5OFjzngavjGTh55KX5q/9w9xHW88JU= github.com/tidwall/gjson v1.19.0/go.mod h1:V37/opeE/JbLUOfH0QTXiNez2l0RUjYUhpT4szFQAfc= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= @@ -208,40 +209,40 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= -go.opentelemetry.io/otel v1.44.0 h1:JjwHmHpA4iZ3wBxluu2fbbE7j4kqlE8jXyAyPXH7HqU= -go.opentelemetry.io/otel v1.44.0/go.mod h1:BMgjTHL9WPRlRjL2oZCBTL4whCGtXch2H4BhOPIAyYc= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.44.0 h1:SUplec5dp06reu1zaXmOXdvqH398taqrDXqUl99jxSc= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.44.0/go.mod h1:ho2g4N+ane+swq5I/VBkKWnRDY4kUINH3FuqyZqX/Ug= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.44.0 h1:RuynHbfU8JUEw7DyONgkVYg2SVtsoF28y0LGIr69jgA= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.44.0/go.mod h1:qZF+/lBs71APw8mlnEZcqZHMzqrYrsFiJOv83lX1OGo= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.44.0 h1:4YsVu3B8+3qtWYYrsUYgn0OG78pN0rnNPRGX4SbokQI= -go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.44.0/go.mod h1:+wnlSn0mD1ADVMe3v9Z/WIaiz6q6gL2J/ejaAmdmv80= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.44.0 h1:qazEJlUOQzhCpzQpFETGby7EdqjI1wsd0W+6Gg1SCTU= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.44.0/go.mod h1:fOD2Yefuxixkx3ahVNf0O/PERb6r4OlbxfATVnYvzCo= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.44.0 h1:lgh3PiVrRUWMLOVSkQicxzZll5NjF1r+AtsX1XRIHw0= -go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.44.0/go.mod h1:5Cnhth3m/AgOeTgE3ex12pPmiu/gGtZit03kSzx9X7s= -go.opentelemetry.io/otel/metric v1.44.0 h1:1w0gILTcHdr3YI+ixLyjemwrVnsMURbTZFrSYCdDdmc= -go.opentelemetry.io/otel/metric v1.44.0/go.mod h1:8O7hanEPBNgEMmybD3s2VBKcgWOCsA6tzHBPODAiquo= -go.opentelemetry.io/otel/metric/x v0.66.0 h1:YkCrx1zLOChi9ZcZ6euupOcsgzbVlec7D/xoEU1+cTA= -go.opentelemetry.io/otel/metric/x v0.66.0/go.mod h1:d1+BDj9t96do0/1LoU1ayfCv79ZgNE41qbhBvnMOBZk= -go.opentelemetry.io/otel/sdk v1.44.0 h1:nHYwb9lK+fJPU/dnT6s7W7Z8itMWyqrnVfbheVYrZ58= -go.opentelemetry.io/otel/sdk v1.44.0/go.mod h1:Osuydd3Se74nqjAKxid74N5eC+jfEqfTegHRnq58oK0= -go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRks6si09iEfI= -go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA= -go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk= -go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE= -go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpuCSL2g= -go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk= +go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc= +go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.46.0 h1:qkDYCAFiZXLcs1L4aY+tP2wguQ4kURANqHOQMA2et2s= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.46.0/go.mod h1:tkipS4DRzmpAmvg+Gw4++O1IdDq6TVDnvnYU6cmbQVs= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0 h1:AP23h/mFgb/lc7tdck1Kfn9qxsM8TAeNPCU5C3pzaps= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0/go.mod h1:K4EqCe1b4kGk5WR690ntg9LaBfsPoV32FwthbyoptuA= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0 h1:OFnwLJr+pF3iHrlGSzbxyuo6/6HyBlnlN1CWEJmBVcw= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.46.0/go.mod h1:716wFneO0ov19A2beH5hjfh9AK5z/VWNAtDijp1Y0/g= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.46.0 h1:w53CDeOA/Kurp7yRsegSr6pbbr759dOvJ+yNmWM6Hxs= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.46.0/go.mod h1:BOmGMCbAtvcJiSJ+hLuhgPLdDbimnraSl8irz3iY8sY= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0 h1:KrC1YrQeSt46ITMWAbgQx1M1eV1/1TKzttrBzymPmss= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.46.0/go.mod h1:zDSEzoEqsOrgBeGvH66KRgxh90VonFyJqBHA0Pk3+rM= +go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8= +go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o= +go.opentelemetry.io/otel/metric/x v0.68.0 h1:TA/cBT23D3MnxYPwHL7YFOdYGdx0A0v+s7Mzotpd1dU= +go.opentelemetry.io/otel/metric/x v0.68.0/go.mod h1:agudOmvWhwUTjgibWDzxD2PoWYnpw5Ht5jISYOD2Hd4= +go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI= +go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM= +go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE= +go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4= +go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c= +go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI= +go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk= +go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= -go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= -go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= -go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= -go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ= +go.yaml.in/yaml/v2 v2.4.4/go.mod h1:gMZqIpDtDqOfM0uNfy0SkpRhvUryYH0Z6wdMYcacYXQ= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -250,10 +251,10 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= -golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto= -golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio= -golang.org/x/crypto/x509roots/fallback v0.0.0-20260609182332-5f2de1a9f1e2 h1:nQAdbnDzK0eYA4IKMp9xUaZq2UOkAYU5kBWQOSqwjP8= -golang.org/x/crypto/x509roots/fallback v0.0.0-20260609182332-5f2de1a9f1e2/go.mod h1:+UoQFNBq2p2wO+Q6ddVtYc25GZ6VNdOMyyrd4nrqrKs= +golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y= +golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I= +golang.org/x/crypto/x509roots/fallback v0.0.0-20260811175631-f44d03d253a1 h1:0LcVce0NOnpZF3lueZ6uAorLcATPGDmPolJYvQBE0ic= +golang.org/x/crypto/x509roots/fallback v0.0.0-20260811175631-f44d03d253a1/go.mod h1:+UoQFNBq2p2wO+Q6ddVtYc25GZ6VNdOMyyrd4nrqrKs= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -285,8 +286,8 @@ golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= -golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o= -golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec= +golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -299,8 +300,8 @@ golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM= -golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= +golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= +golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -326,8 +327,8 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= -golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -337,8 +338,8 @@ golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM= -golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc= -golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y= +golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0= +golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= @@ -348,8 +349,8 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= -golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE= -golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -377,25 +378,25 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7 google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa h1:Kjn0N0tCrDgiAFW+lGO4JZ3ck44CehvJQMAwj9QF0G8= -google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:q4lMZS6kskjT5HvCPrnnypcDPVJqT/f4nfxmkE7gryY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:mZHHdPZl0dbGHCflZgAq/Q468DWVFcU2whhB2KAo8fk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8= +google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688 h1:ax2KzoSRIZU/M0cIxri3pKxy99vniH1PVxWC6si/eZI= +google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688/go.mod h1:1RJ9BQGyNdZwkGc1eTqkErfRZ6RJyYPHZo73BZ1vQqI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 h1:cYNAzI2sUwhmCcoj9TxvihSrqsxt6uIkj3rDRhSDmW4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688/go.mod h1:DjtHYE8FKJLivXcBEjGwndXfIC23G0VpXiXKqG179uA= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= -google.golang.org/grpc v1.81.1 h1:VnnIIZ88UzOOKLukQi+ImGz8O1Wdp8nAGGnvOfEIWQQ= -google.golang.org/grpc v1.81.1/go.mod h1:xGH9GfzOyMTGIOXBJmXt+BX/V0kcdQbdcuwQ/zNw42I= +google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0= +google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= -google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y= gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/api/server_test.go b/internal/api/server_test.go index eb2ecab59..8f3c9b688 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -31,7 +31,7 @@ func TestLogger(t *testing.T) { t.Run("path="+path, func(t *testing.T) { t.Parallel() rw := httptest.NewRecorder() - r := httptest.NewRequest(method, "http://example.com"+path, nil) + r := httptest.NewRequestWithContext(t.Context(), method, "http://example.com"+path, nil) l, hook := logtest.NewNullLogger() l.Level = logrus.DebugLevel @@ -63,7 +63,7 @@ func TestPing(t *testing.T) { mux := handlePing(logger) rw := httptest.NewRecorder() - r := httptest.NewRequest(http.MethodGet, "/ping", nil) + r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/ping", nil) mux.ServeHTTP(rw, r) res := rw.Result() diff --git a/internal/build/version.go b/internal/build/version.go index 24427c399..ae28d1419 100644 --- a/internal/build/version.go +++ b/internal/build/version.go @@ -3,4 +3,4 @@ package build // Version contains the current version of k6 // represented using Semantic Versioning expression. -const Version = "2.1.0" +const Version = "2.3.0" diff --git a/internal/cloudapi/clientcfg/clientcfg.go b/internal/cloudapi/clientcfg/clientcfg.go new file mode 100644 index 000000000..25957a2a5 --- /dev/null +++ b/internal/cloudapi/clientcfg/clientcfg.go @@ -0,0 +1,42 @@ +// Package clientcfg builds the k6cloud SDK client Configuration shared by +// k6's internal cloud API clients (internal/cloudapi/v6 and +// internal/cloudapi/provisioning). Both need the same User-Agent, +// single-server list, HTTP transport, and retry settings; only the server +// description differs between them, so it is passed in. +package clientcfg + +import ( + "net/http" + "time" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" +) + +const ( + // RetryInterval is the default cloud request retry interval. + RetryInterval = 500 * time.Millisecond + // MaxRetries specifies max retry attempts. + MaxRetries = 3 +) + +// New builds a k6cloud.Configuration for a single-server cloud API client. +// host is the server URL, version sets the k6cloud/ User-Agent, +// description labels the server entry, and timeout bounds each HTTP +// request. Retry settings use the shared MaxRetries/RetryInterval. +func New(host, version, description string, timeout time.Duration) *k6cloud.Configuration { + cfg := k6cloud.NewConfiguration() + cfg.UserAgent = "k6cloud/" + version + cfg.Servers = k6cloud.ServerConfigurations{ + { + URL: host, + Description: description, + }, + } + cfg.HTTPClient = &http.Client{ + Timeout: timeout, + Transport: http.DefaultTransport, + } + cfg.MaxRetries = MaxRetries + cfg.RetryInterval = RetryInterval + return cfg +} diff --git a/internal/cloudapi/httperr/httperr.go b/internal/cloudapi/httperr/httperr.go new file mode 100644 index 000000000..3d34b02aa --- /dev/null +++ b/internal/cloudapi/httperr/httperr.go @@ -0,0 +1,31 @@ +// Package httperr contains HTTP-status-code error helpers shared by k6 +// Cloud's API clients (cloudapi, internal/cloudapi/v6). It only covers the +// part of their CheckResponse-style functions that's genuinely identical +// across them - classifying 401/403 - everything else about decoding an +// error response body differs between API versions. +package httperr + +import ( + "errors" + "net/http" +) + +var ( + // ErrNotAuthenticated maps HTTP 401. + ErrNotAuthenticated = errors.New("failed to authenticate with k6 Cloud") + // ErrNotAuthorized maps HTTP 403. + ErrNotAuthorized = errors.New("not allowed to upload result to k6 Cloud") +) + +// ClassifyStatus returns ErrNotAuthenticated for HTTP 401, ErrNotAuthorized +// for HTTP 403, and nil for any other status code. +func ClassifyStatus(statusCode int) error { + switch statusCode { + case http.StatusUnauthorized: + return ErrNotAuthenticated + case http.StatusForbidden: + return ErrNotAuthorized + default: + return nil + } +} diff --git a/internal/cloudapi/provisioning/api.go b/internal/cloudapi/provisioning/api.go new file mode 100644 index 000000000..f49c581de --- /dev/null +++ b/internal/cloudapi/provisioning/api.go @@ -0,0 +1,313 @@ +package provisioning + +import ( + "bytes" + "context" + "crypto/rand" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "math" + "net/http" + "time" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" +) + +// defaultWaitPollInterval is the default interval between status polls +// when waiting for a test run to become ready. +const defaultWaitPollInterval = 2 * time.Second + +// StartLocalExecutionRequest contains the parameters for starting a local +// execution test run via the provisioning API. +type StartLocalExecutionRequest struct { + // Options is the pre-marshalled lib.Options JSON produced by cmd. + // Kept as json.RawMessage to preserve typed precision (custom + // marshallers on lib.Options work correctly). + Options json.RawMessage + + // MaxVUs is the maximum number of virtual users during the test. + MaxVUs int64 + + // TotalDuration is the total test duration in seconds. + TotalDuration int64 + + // ArchiveSize is the archive file size in bytes, or 0 when there is + // no archive (--no-archive-upload); 0 is sent as explicit JSON null. + // A real archive is never empty, so 0 unambiguously means "no archive". + ArchiveSize int64 +} + +// StartLocalExecutionResponse holds the fields returned by the +// start_local_execution endpoint that downstream consumers need. +type StartLocalExecutionResponse struct { + TestRunID int64 + ArchiveUploadURL *string // nil when no archive upload expected + RuntimeConfig RuntimeConfig + TestRunDetailsPageURL string +} + +// RuntimeConfig carries the runtime configuration returned by the +// provisioning API for a local-execution test run. +type RuntimeConfig struct { + Metrics MetricsConfig + TestRunToken string + Secrets SecretsConfig + Logs LogsConfig +} + +// MetricsConfig holds the metrics push configuration from the +// provisioning API's runtime_config.metrics object. +type MetricsConfig struct { + PushURL string + PushInterval *string + PushConcurrency *int32 + AggregationPeriod *string + AggregationWaitPeriod *string + AggregationMinSamples *int32 + MaxSamplesPerPackage *int32 +} + +// SecretsConfig holds the secrets configuration from the +// provisioning API's runtime_config.secrets object. +type SecretsConfig struct { + Endpoint string + ResponsePath string +} + +// LogsConfig holds the log-push configuration from the provisioning +// API's runtime_config.logs object. tail_url is intentionally omitted: +// --local-execution pushes logs, it doesn't tail them. +type LogsConfig struct { + PushURL string + Level string + Limit int32 + PushPeriodSeconds string + MessageMaxSize int32 + AllowedLabels []string +} + +// UploadArchive PUTs pre-serialised archive bytes to the given +// presigned S3 URL. The URL carries auth in query params, so no +// Authorization header is set. Retries on 5xx and transport errors. +func (c *Client) UploadArchive(ctx context.Context, uploadURL string, body []byte) error { + req, err := http.NewRequestWithContext(ctx, http.MethodPut, uploadURL, bytes.NewReader(body)) + if err != nil { + return fmt.Errorf("creating upload request: %w", err) + } + req.Header.Set("Content-Type", "application/x-tar") + req.ContentLength = int64(len(body)) + + resp, err := c.doWithRetry(req) + if err != nil { + return fmt.Errorf("uploading archive: %w", err) + } + defer func() { + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + }() + + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + respBody, readErr := io.ReadAll(resp.Body) + if readErr != nil || len(respBody) == 0 { + return fmt.Errorf("archive upload failed: %d %s", + resp.StatusCode, http.StatusText(resp.StatusCode)) + } + return fmt.Errorf("archive upload failed: %d %s: %s", + resp.StatusCode, http.StatusText(resp.StatusCode), respBody) + } + + return nil +} + +// WaitForTestRunReady polls GET /cloud/v6/test_runs/{id} until the +// backend status becomes "initializing" (signalling that k6 can begin +// local execution). Returns an error if the test run reaches "aborted" +// or "completed" first. Cancellable via context. Logs status transitions +// at Debug level once per change. +// +// If pollInterval is <= 0 the defaultWaitPollInterval is used. +func (c *Client) WaitForTestRunReady(ctx context.Context, testRunID int64, pollInterval time.Duration) error { + if pollInterval <= 0 { + pollInterval = defaultWaitPollInterval + } + + var lastStatus string + + for { + progress, err := c.v6Client.FetchTest(ctx, testRunID) + if err != nil { + return fmt.Errorf("fetching test status: %w", err) + } + + status := progress.Status.String() + + switch v6.Status(status) { + case v6.StatusInitializing: + return nil + + case v6.StatusAborted: + var abortMsg string + for _, e := range progress.StatusHistory { + if e.Status == v6.StatusAborted && e.Message != "" { + abortMsg = e.Message + break + } + } + if abortMsg != "" { + return fmt.Errorf("test run aborted before starting: %s", abortMsg) + } + return fmt.Errorf("test run aborted before starting") + + case v6.StatusCompleted: + return fmt.Errorf("test run completed before k6 could start") + + default: + // created, queued, or any unrecognised state — keep polling. + if status != lastStatus { + c.logger.WithField("status", progress.FormatStatus()).Debug("test status") + lastStatus = status + } + } + + select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(pollInterval): + // continue polling + } + } +} + +// StartLocalExecution starts a local-execution test run via the +// provisioning API. It generates a K6-Idempotency-Key header for +// safe retries. The caller provides options as pre-marshalled JSON. +func (c *Client) StartLocalExecution( + ctx context.Context, loadTestID int64, req StartLocalExecutionRequest, +) (*StartLocalExecutionResponse, error) { + // Generate idempotency key: 8 random bytes hex-encoded (16 chars). + var key [8]byte + if _, err := rand.Read(key[:]); err != nil { + return nil, fmt.Errorf("generating idempotency key: %w", err) + } + + // SDK adapter: unmarshal json.RawMessage → map[string]any. + var opts map[string]any + if err := json.Unmarshal(req.Options, &opts); err != nil { + return nil, fmt.Errorf("unmarshalling options for SDK: %w", err) + } + + maxVUs, err := toInt32(req.MaxVUs) + if err != nil { + return nil, fmt.Errorf("max_vus: %w", err) + } + totalDuration, err := toInt32(req.TotalDuration) + if err != nil { + return nil, fmt.Errorf("total_duration: %w", err) + } + + sdkReq := k6cloud.NewStartLocalExecutionTestRequest(opts, maxVUs, totalDuration) + + if req.ArchiveSize > 0 { + v, err := toInt32(req.ArchiveSize) + if err != nil { + return nil, fmt.Errorf("archive_size: %w", err) + } + sdkReq.SetArchiveSize(v) + } else { + sdkReq.SetArchiveSizeNil() + } + + res, hr, err := c.apiClient.ProvisioningAPI. + StartLocalExecutionTest(c.authCtx(ctx), loadTestID). + K6IdempotencyKey(hex.EncodeToString(key[:])). + StartLocalExecutionTestRequest(sdkReq). + Execute() + defer closeResponse(hr, &err) + + if hr != nil { + if respErr := CheckResponse(hr); respErr != nil { + return nil, respErr + } + } + if err != nil { + return nil, err + } + if res == nil { + // Should not happen once the status/error checks above pass, but + // the SDK getters return zero values on a nil receiver, so guard + // against silently producing an empty result and report it clearly. + return nil, errors.New("unexpected nil response from start_local_execution") + } + + return mapStartLocalExecutionResponse(res), nil +} + +// mapStartLocalExecutionResponse converts the SDK response type into +// the package's domain type. +func mapStartLocalExecutionResponse(res *k6cloud.StartLocalExecutionTestResponse) *StartLocalExecutionResponse { + rc := res.GetRuntimeConfig() + m := rc.GetMetrics() + s := rc.GetSecrets() + l := rc.GetLogs() + + resp := &StartLocalExecutionResponse{ + TestRunID: res.GetTestRunId(), + TestRunDetailsPageURL: res.GetTestRunDetailsPageUrl(), + RuntimeConfig: RuntimeConfig{ + TestRunToken: rc.GetTestRunToken(), + Metrics: MetricsConfig{ + PushURL: m.GetPushUrl(), + PushInterval: m.PushInterval.Get(), + PushConcurrency: m.PushConcurrency.Get(), + AggregationPeriod: m.AggregationPeriod.Get(), + AggregationWaitPeriod: m.AggregationWaitPeriod.Get(), + AggregationMinSamples: m.AggregationMinSamples.Get(), + MaxSamplesPerPackage: m.MaxSamplesPerPackage.Get(), + }, + Secrets: SecretsConfig{ + Endpoint: s.GetEndpoint(), + ResponsePath: s.GetResponsePath(), + }, + Logs: LogsConfig{ + PushURL: l.GetPushUrl(), + Level: l.GetLevel(), + Limit: l.GetLimit(), + PushPeriodSeconds: l.GetPushPeriodSeconds(), + MessageMaxSize: l.GetMessageMaxSize(), + AllowedLabels: l.GetAllowedLabels(), + }, + }, + } + + if url := res.ArchiveUploadUrl.Get(); url != nil { + resp.ArchiveUploadURL = url + } + + return resp +} + +// toInt32 safely converts an int64 to int32, returning an error if +// the value overflows. +func toInt32(v int64) (int32, error) { + if v < math.MinInt32 || v > math.MaxInt32 { + return 0, fmt.Errorf("value %d overflows int32", v) + } + return int32(v), nil +} + +// closeResponse drains and closes an HTTP response body. It mirrors +// the v6 package's closeResponse helper. +func closeResponse(res *http.Response, rerr *error) { + if res == nil { + return + } + _, _ = io.Copy(io.Discard, res.Body) + if err := res.Body.Close(); err != nil && *rerr == nil { + *rerr = err + } +} diff --git a/internal/cloudapi/provisioning/api_test.go b/internal/cloudapi/provisioning/api_test.go new file mode 100644 index 000000000..fd7f21c47 --- /dev/null +++ b/internal/cloudapi/provisioning/api_test.go @@ -0,0 +1,636 @@ +package provisioning + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "sync/atomic" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/errext" + provtest "go.k6.io/k6/v2/internal/cloudapi/provisioning/test" + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" + "go.k6.io/k6/v2/internal/lib/testutils" +) + +func TestStartLocalExecution(t *testing.T) { + t.Parallel() + + var ( + method string + path string + body []byte + idempKey string + authHeader string + stackIDHdr string + userAgent string + ) + + srv := provtest.NewServer(t) + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, r *http.Request) { + method = r.Method + path = r.URL.Path + idempKey = r.Header.Get("K6-Idempotency-Key") + authHeader = r.Header.Get("Authorization") + stackIDHdr = r.Header.Get("X-Stack-Id") + userAgent = r.Header.Get("User-Agent") + var err error + body, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + + resp := provtest.DefaultStartLocalExecutionResponse() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(resp) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + opts := json.RawMessage(`{"vus":10,"duration":"30s"}`) + archiveSize := int64(1000) + req := StartLocalExecutionRequest{ + Options: opts, + MaxVUs: 50, + TotalDuration: 630, + ArchiveSize: archiveSize, + } + + got, err := client.StartLocalExecution(t.Context(), provtest.DefaultLoadTestID, req) + require.NoError(t, err) + + // --- Response assertions --- + assert.Equal(t, provtest.DefaultTestRunID, got.TestRunID) + require.NotNil(t, got.ArchiveUploadURL) + assert.Contains(t, *got.ArchiveUploadURL, "s3.amazonaws.com") + assert.Contains(t, got.TestRunDetailsPageURL, "/runs/") + assert.Equal(t, "https://ingest.k6.io/v1/metrics", got.RuntimeConfig.Metrics.PushURL) + assert.Equal(t, "test-run-token-abc", got.RuntimeConfig.TestRunToken) + assert.Equal(t, "https://api.k6.io/provisioning/v1/test_runs/123/decrypt_secret?name={key}", got.RuntimeConfig.Secrets.Endpoint) + assert.Equal(t, "plaintext", got.RuntimeConfig.Secrets.ResponsePath) + + // Metrics tuning fields. + require.NotNil(t, got.RuntimeConfig.Metrics.PushInterval) + assert.Equal(t, "2s", *got.RuntimeConfig.Metrics.PushInterval) + require.NotNil(t, got.RuntimeConfig.Metrics.PushConcurrency) + assert.Equal(t, int32(5), *got.RuntimeConfig.Metrics.PushConcurrency) + require.NotNil(t, got.RuntimeConfig.Metrics.AggregationPeriod) + assert.Equal(t, "3s", *got.RuntimeConfig.Metrics.AggregationPeriod) + require.NotNil(t, got.RuntimeConfig.Metrics.AggregationWaitPeriod) + assert.Equal(t, "1s", *got.RuntimeConfig.Metrics.AggregationWaitPeriod) + require.NotNil(t, got.RuntimeConfig.Metrics.MaxSamplesPerPackage) + assert.Equal(t, int32(2000), *got.RuntimeConfig.Metrics.MaxSamplesPerPackage) + + // Logs config. + assert.Equal(t, "https://logs.k6.io", got.RuntimeConfig.Logs.PushURL) + assert.Equal(t, "info", got.RuntimeConfig.Logs.Level) + assert.Equal(t, int32(900), got.RuntimeConfig.Logs.Limit) + assert.Equal(t, "3s", got.RuntimeConfig.Logs.PushPeriodSeconds) + assert.Equal(t, int32(10000), got.RuntimeConfig.Logs.MessageMaxSize) + assert.Equal(t, []string{"lz", "level"}, got.RuntimeConfig.Logs.AllowedLabels) + + // --- Request assertions --- + assert.Equal(t, http.MethodPost, method) + assert.Equal(t, "/provisioning/v1/load_tests/456/start_local_execution", path) + assert.Equal(t, "Bearer test-token", authHeader) + assert.Equal(t, "7", stackIDHdr) + assert.Equal(t, "k6cloud/0.42.0", userAgent) + assert.Regexp(t, `^[0-9a-f]+$`, idempKey) + assert.GreaterOrEqual(t, len(idempKey), 1) + + // Request body shape. + var reqBody map[string]any + require.NoError(t, json.Unmarshal(body, &reqBody)) + assert.Equal(t, float64(50), reqBody["max_vus"]) + assert.Equal(t, float64(630), reqBody["total_duration"]) + assert.Equal(t, float64(1000), reqBody["archive_size"]) + + reqOpts, ok := reqBody["options"].(map[string]any) + require.True(t, ok, "options should be an object") + assert.Equal(t, float64(10), reqOpts["vus"]) + assert.Equal(t, "30s", reqOpts["duration"]) +} + +func TestStartLocalExecution_NoArchive_ArchiveSizeNull(t *testing.T) { + t.Parallel() + + var body []byte + + srv := provtest.NewServer(t) + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, r *http.Request) { + var err error + body, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + resp := provtest.DefaultStartLocalExecutionResponse() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(resp) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + req := StartLocalExecutionRequest{ + Options: json.RawMessage(`{"vus":1}`), + MaxVUs: 1, + TotalDuration: 60, + ArchiveSize: 0, // no archive + } + + _, err = client.StartLocalExecution(t.Context(), provtest.DefaultLoadTestID, req) + require.NoError(t, err) + + // Verify archive_size is explicit JSON null (not omitted, not zero). + var raw map[string]json.RawMessage + require.NoError(t, json.Unmarshal(body, &raw)) + + archiveRaw, exists := raw["archive_size"] + require.True(t, exists, "archive_size key must be present in request body") + assert.Equal(t, "null", string(archiveRaw), "archive_size must be JSON null") +} + +func TestStartLocalExecution_5xxRetried(t *testing.T) { + t.Parallel() + + var attempts atomic.Int32 + + srv := provtest.NewServer(t) + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + if attempts.Add(1) == 1 { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(`{"error":{"code":"server_error","message":"temporary"}}`)) + return + } + resp := provtest.DefaultStartLocalExecutionResponse() + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(resp) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + req := StartLocalExecutionRequest{ + Options: json.RawMessage(`{"vus":1}`), + MaxVUs: 1, + TotalDuration: 60, + ArchiveSize: 0, + } + + got, err := client.StartLocalExecution(t.Context(), provtest.DefaultLoadTestID, req) + require.NoError(t, err) + assert.Equal(t, provtest.DefaultTestRunID, got.TestRunID) + assert.Equal(t, int32(2), attempts.Load(), "expected exactly 2 attempts (1 failure + 1 success)") +} + +func TestUploadArchive(t *testing.T) { + t.Parallel() + + var ( + method string + reqPath string + contentType string + authHeader string + contentLength int64 + gotBody []byte + hitCount atomic.Int32 + ) + + srv := provtest.NewServer(t) + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, r *http.Request) { + hitCount.Add(1) + method = r.Method + reqPath = r.URL.Path + contentType = r.Header.Get("Content-Type") + authHeader = r.Header.Get("Authorization") + contentLength = r.ContentLength + var err error + gotBody, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + archiveBody := []byte("fake-archive-tar-content-12345") + err = client.UploadArchive(t.Context(), srv.PresignedUploadURL(), archiveBody) + require.NoError(t, err) + + assert.Equal(t, http.MethodPut, method) + assert.Equal(t, provtest.PresignedUploadPath, reqPath) + assert.Equal(t, "application/x-tar", contentType) + assert.Empty(t, authHeader, "presigned URLs must not carry an Authorization header") + assert.Equal(t, archiveBody, gotBody) + assert.Equal(t, int64(len(archiveBody)), contentLength) + assert.Equal(t, int32(1), hitCount.Load(), "server should be hit exactly once") +} + +func TestUploadArchive_RetriesOn5xx(t *testing.T) { + t.Parallel() + + var ( + attempts atomic.Int32 + body1 []byte + body2 []byte + ) + + srv := provtest.NewServer(t) + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + if attempts.Add(1) == 1 { + body1 = b + w.WriteHeader(http.StatusInternalServerError) + return + } + body2 = b + w.WriteHeader(http.StatusOK) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + archiveBody := []byte("archive-bytes-for-retry-test") + err = client.UploadArchive(t.Context(), srv.PresignedUploadURL(), archiveBody) + require.NoError(t, err) + + assert.Equal(t, int32(2), attempts.Load(), "expected exactly 2 attempts (1 failure + 1 success)") + assert.Equal(t, body1, body2, "body replay should produce identical bytes on retry") +} + +func TestUploadArchive_4xxNotRetried(t *testing.T) { + t.Parallel() + + var attempts atomic.Int32 + + srv := provtest.NewServer(t) + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + attempts.Add(1) + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte("Request has expired")) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + archiveBody := []byte("archive-bytes-for-4xx-test") + err = client.UploadArchive(t.Context(), srv.PresignedUploadURL(), archiveBody) + require.Error(t, err) + + assert.Contains(t, err.Error(), "403") + assert.Contains(t, err.Error(), "Request has expired") + assert.Equal(t, int32(1), attempts.Load(), "4xx must not be retried") +} + +func TestStartLocalExecution_4xxNotRetried(t *testing.T) { + t.Parallel() + + var attempts atomic.Int32 + + srv := provtest.NewServer(t) + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + attempts.Add(1) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"error":{"code":"validation_error","message":"invalid options"}}`)) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + req := StartLocalExecutionRequest{ + Options: json.RawMessage(`{"vus":1}`), + MaxVUs: 1, + TotalDuration: 60, + ArchiveSize: 0, + } + + _, err = client.StartLocalExecution(t.Context(), provtest.DefaultLoadTestID, req) + require.Error(t, err) + + var respErr *ResponseError + require.ErrorAs(t, err, &respErr) + assert.Equal(t, http.StatusBadRequest, respErr.StatusCode) + assert.Equal(t, int32(1), attempts.Load(), "4xx must not be retried") +} + +func TestWaitForTestRunReady(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.NoError(t, err) + + assert.Equal(t, int32(1), srv.FetchTestRunHitCount(), "server should be hit exactly once") +} + +func TestWaitForTestRunReady_PollsUntilInitializing(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusCreated}, + {Status: v6.StatusQueued}, + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.NoError(t, err) + + assert.Equal(t, int32(3), srv.FetchTestRunHitCount(), "server should be hit 3 times") +} + +func TestWaitForTestRunReady_AbortedReturnsErrorWithMessage(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + { + Status: v6.StatusAborted, + StatusHistory: []v6.StatusEvent{ + {Status: v6.StatusAborted, Message: "quota exceeded"}, + }, + }, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.Error(t, err) + assert.Contains(t, err.Error(), "quota exceeded") +} + +func TestWaitForTestRunReady_AbortedNoHistoryReturnsGenericError(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusAborted}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.Error(t, err) + assert.Contains(t, err.Error(), "test run aborted before starting") +} + +func TestWaitForTestRunReady_CompletedReturnsError(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusCompleted}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.Error(t, err) + assert.Contains(t, err.Error(), "completed") +} + +func TestWaitForTestRunReady_UnknownStatusKeepsPolling(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.Status("weird-state")}, + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.NoError(t, err) + + assert.Equal(t, int32(2), srv.FetchTestRunHitCount(), "server should be hit twice") +} + +func TestWaitForTestRunReady_LogsTransitionsOnce(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusCreated}, + {Status: v6.StatusCreated}, + {Status: v6.StatusQueued}, + {Status: v6.StatusQueued}, + {Status: v6.StatusInitializing}, + }) + + logger, hook := testutils.NewLoggerWithHook(t, logrus.DebugLevel) + client, err := NewClient(logger, "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.WaitForTestRunReady(t.Context(), provtest.DefaultTestRunID, 1*time.Microsecond) + require.NoError(t, err) + + entries := hook.Drain() + // Filter to only debug-level entries with a "status" field. + var statusEntries []logrus.Entry + for _, e := range entries { + if e.Level == logrus.DebugLevel { + if _, ok := e.Data["status"]; ok { + statusEntries = append(statusEntries, e) + } + } + } + + // Should have exactly 2 log entries: one for "created", one for "queued". + // "initializing" is a terminal success — no log for it. + require.Len(t, statusEntries, 2, + "expected exactly 2 status transition log entries (created, queued)") + assert.Equal(t, "Created", statusEntries[0].Data["status"]) + assert.Equal(t, "Queued", statusEntries[1].Data["status"]) +} + +func TestWaitForTestRunReady_ContextCancellation(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + // Server always returns "created" — polling never terminates on its own. + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusCreated}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(t.Context()) + + // Cancel after a small delay to let a few polls happen. + go func() { + time.Sleep(5 * time.Millisecond) + cancel() + }() + + err = client.WaitForTestRunReady(ctx, provtest.DefaultTestRunID, 1*time.Microsecond) + require.Error(t, err) + assert.ErrorIs(t, err, context.Canceled) +} + +func TestNotifyTestRunCompleted(t *testing.T) { + t.Parallel() + + var ( + method string + reqPath string + authHeader string + contentType string + userAgent string + body []byte + hitCount atomic.Int32 + ) + + srv := provtest.NewServer(t) + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, r *http.Request) { + hitCount.Add(1) + method = r.Method + reqPath = r.URL.Path + authHeader = r.Header.Get("Authorization") + contentType = r.Header.Get("Content-Type") + userAgent = r.Header.Get("User-Agent") + var err error + body, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.NotifyTestRunCompleted(t.Context(), provtest.DefaultTestRunID, "scoped-token-xyz", nil) + require.NoError(t, err) + + // --- Request assertions --- + assert.Equal(t, http.MethodPost, method) + assert.Equal(t, "/provisioning/v1/test_runs/123/notify", reqPath) + assert.Equal(t, "Bearer scoped-token-xyz", authHeader) + assert.Equal(t, "application/json", contentType) + assert.Equal(t, "k6cloud/0.42.0", userAgent) + assert.Equal(t, int32(1), hitCount.Load(), "server should be hit exactly once") + + // Request body shape: event_type present, error null. + var reqBody map[string]any + require.NoError(t, json.Unmarshal(body, &reqBody)) + assert.Equal(t, "script_execution_completed", reqBody["event_type"]) + assert.Nil(t, reqBody["error"]) +} + +func TestNotifyTestRunCompleted_WithErrorInRequestBody(t *testing.T) { + t.Parallel() + + var body []byte + + srv := provtest.NewServer(t) + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, r *http.Request) { + var err error + body, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + testErr := errext.WithAbortReasonIfNone(errors.New("TypeError: foo is not a function"), errext.AbortedByScriptError) + err = client.NotifyTestRunCompleted(t.Context(), provtest.DefaultTestRunID, "scoped-token-xyz", testErr) + require.NoError(t, err) + + // Request body should include the mapped error. + var reqBody struct { + EventType string `json:"event_type"` + Error *struct { + Code int `json:"code"` + Reason string `json:"reason"` + } `json:"error"` + } + require.NoError(t, json.Unmarshal(body, &reqBody)) + assert.Equal(t, "script_execution_completed", reqBody.EventType) + require.NotNil(t, reqBody.Error) + assert.Equal(t, 8035, reqBody.Error.Code) + assert.Contains(t, reqBody.Error.Reason, "TypeError") +} + +func TestNotifyTestRunCompleted_4xxResponseReturnsError(t *testing.T) { + t.Parallel() + + var hitCount atomic.Int32 + + srv := provtest.NewServer(t) + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + hitCount.Add(1) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"error":{"code":"validation_error","message":"bad event_type"}}`)) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.NotifyTestRunCompleted(t.Context(), provtest.DefaultTestRunID, "scoped-token-xyz", nil) + require.Error(t, err) + assert.Contains(t, err.Error(), "400") + assert.Contains(t, err.Error(), "bad event_type") + assert.Equal(t, int32(1), hitCount.Load(), "4xx must not be retried") +} + +func TestNotifyTestRunCompleted_5xxRetried(t *testing.T) { + t.Parallel() + + var hitCount atomic.Int32 + + srv := provtest.NewServer(t) + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + if hitCount.Add(1) == 1 { + w.WriteHeader(http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + err = client.NotifyTestRunCompleted(t.Context(), provtest.DefaultTestRunID, "scoped-token-xyz", nil) + require.NoError(t, err) + assert.Equal(t, int32(2), hitCount.Load(), "expected exactly 2 attempts (1 failure + 1 success)") +} diff --git a/internal/cloudapi/provisioning/client.go b/internal/cloudapi/provisioning/client.go new file mode 100644 index 000000000..a3c3edf55 --- /dev/null +++ b/internal/cloudapi/provisioning/client.go @@ -0,0 +1,140 @@ +package provisioning + +import ( + "context" + "fmt" + "io" + "net/http" + "strconv" + "time" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + "github.com/sirupsen/logrus" + + "go.k6.io/k6/v2/internal/cloudapi/clientcfg" + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" +) + +// Client orchestrates the provisioning and v6 cloud APIs for a +// local-execution test run: creating/finding the load test, starting the +// run, uploading the archive, polling for readiness, and posting the +// completion notification. It wraps a k6cloud.APIClient for the +// provisioning endpoints and an embedded v6.Client for test-run status +// queries. It is distinct from HTTPClient, which is the scoped-token +// Bearer HTTP layer injected into the expv2 metrics push. +type Client struct { + apiClient *k6cloud.APIClient + v6Client *v6.Client + token string + stackID int64 + host string + version string + + logger logrus.FieldLogger +} + +// NewClient returns a new provisioning Client. It internally constructs +// both a k6cloud.APIClient (for provisioning endpoints) and a v6.Client +// (for v6 operations like FetchTest). The stackID identifies the +// Grafana Cloud stack and is required for the stack-scoped requests. +func NewClient( + logger logrus.FieldLogger, + token, host, version string, + stackID int64, + timeout time.Duration, +) (*Client, error) { + if token == "" { + return nil, fmt.Errorf("token is required to create provisioning API client") + } + + cfg := clientcfg.New(host, version, "k6 Cloud API (provisioning).", timeout) + + v6c, err := v6.NewClient(logger, token, host, version, timeout) + if err != nil { + return nil, fmt.Errorf("creating v6 client: %w", err) + } + + c := &Client{ + apiClient: k6cloud.NewAPIClient(cfg), + v6Client: v6c, + token: token, + stackID: stackID, + host: host, + version: version, + logger: logger, + } + // Propagate the stack ID to the embedded v6 client. v6.SetStackID + // validates that it fits the int32 the SDK's X-Stack-Id requires — + // that is the single place the int32 boundary is enforced. + if err := c.v6Client.SetStackID(stackID); err != nil { + return nil, err + } + // The provisioning endpoints carry the stack ID as a decimal-string + // default header, so no int32 narrowing is needed here. + c.apiClient.GetConfig().DefaultHeader["X-Stack-Id"] = strconv.FormatInt(stackID, 10) + return c, nil +} + +// authCtx returns a context carrying the API access token. +func (c *Client) authCtx(ctx context.Context) context.Context { + return context.WithValue(ctx, k6cloud.ContextAccessToken, c.token) +} + +// doWithRetry executes the request via the provisioning Client's HTTP +// client. It is a thin wrapper around the package-level doWithRetry; see +// that function for the retry semantics. +func (c *Client) doWithRetry(req *http.Request) (*http.Response, error) { + return doWithRetry(c.apiClient.GetConfig().HTTPClient, req) +} + +// doWithRetry runs req through httpClient, retrying on 5xx responses and +// transport errors up to clientcfg.MaxRetries times, resetting the body +// from req.GetBody before each attempt so a retried request with a body is +// not sent empty. The retry wait honours req.Context() so a cancelled +// request stops waiting promptly. 4xx responses are NOT retried. +func doWithRetry(httpClient *http.Client, req *http.Request) (*http.Response, error) { + var ( + lastErr error + lastResp *http.Response + ) + + for attempt := 1; attempt <= clientcfg.MaxRetries; attempt++ { + // The vendored SDK resets the body on its own internal retries, + // but this direct Do loop must do it itself. + if req.GetBody != nil { + body, err := req.GetBody() + if err != nil { + return nil, err + } + req.Body = body + } + + lastResp, lastErr = httpClient.Do(req) + if lastErr != nil { + if attempt < clientcfg.MaxRetries { + select { + case <-time.After(clientcfg.RetryInterval): + case <-req.Context().Done(): + return lastResp, req.Context().Err() + } + continue + } + break + } + + if lastResp.StatusCode >= 500 && attempt < clientcfg.MaxRetries { + _, _ = io.Copy(io.Discard, lastResp.Body) + _ = lastResp.Body.Close() + select { + case <-time.After(clientcfg.RetryInterval): + case <-req.Context().Done(): + return lastResp, req.Context().Err() + } + continue + } + + break + } + + return lastResp, lastErr +} diff --git a/internal/cloudapi/provisioning/client_test.go b/internal/cloudapi/provisioning/client_test.go new file mode 100644 index 000000000..9725569b9 --- /dev/null +++ b/internal/cloudapi/provisioning/client_test.go @@ -0,0 +1,38 @@ +package provisioning + +import ( + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNewClient(t *testing.T) { + t.Parallel() + + logger := logrus.New() + token := "test-token" + host := "https://api.k6.io" + version := "0.1.0" + stackID := int64(123) + timeout := 30 * time.Second + + c, err := NewClient(logger, token, host, version, stackID, timeout) + require.NoError(t, err) + + assert.Equal(t, logger, c.logger) + assert.Equal(t, token, c.token) + assert.Equal(t, stackID, c.stackID) + + // The underlying k6cloud.APIClient should be configured correctly. + require.NotNil(t, c.apiClient) + cfg := c.apiClient.GetConfig() + require.Len(t, cfg.Servers, 1) + assert.Equal(t, host, cfg.Servers[0].URL) + assert.Equal(t, timeout, cfg.HTTPClient.Timeout) + + // The internal v6 client should be constructed with the same auth params. + assert.NotNil(t, c.v6Client) +} diff --git a/internal/cloudapi/provisioning/doc.go b/internal/cloudapi/provisioning/doc.go new file mode 100644 index 000000000..703e6f3c2 --- /dev/null +++ b/internal/cloudapi/provisioning/doc.go @@ -0,0 +1,5 @@ +// Package provisioning owns the local-execution flow's interactions with the +// provisioning API endpoints and the v6 cloud API. It provides a Client +// for orchestrating load-test provisioning (start, archive upload, polling, +// notification) and wraps the v6 client for test-run status queries. +package provisioning diff --git a/internal/cloudapi/provisioning/errors.go b/internal/cloudapi/provisioning/errors.go new file mode 100644 index 000000000..75ea27e37 --- /dev/null +++ b/internal/cloudapi/provisioning/errors.go @@ -0,0 +1,51 @@ +package provisioning + +import ( + "errors" + "fmt" + "io" + "net/http" + + "go.k6.io/k6/v2/internal/cloudapi/httperr" +) + +var errUnknown = errors.New("an error occurred communicating with the provisioning API") + +// ResponseError represents an error returned by the provisioning API. +type ResponseError struct { + StatusCode int + Body string +} + +func (e *ResponseError) Error() string { + return fmt.Sprintf("provisioning API error (%d): %s", e.StatusCode, e.Body) +} + +// CheckResponse checks an HTTP response from the provisioning API. +// It returns nil if the status code is in the 2xx range, otherwise it +// returns a *ResponseError with the status code and response body. +func CheckResponse(resp *http.Response) error { + if resp == nil { + return errUnknown + } + + if c := resp.StatusCode; c >= 200 && c <= 299 { + return nil + } + + // Classify 401/403 to the shared sentinels so provisioning reports + // auth failures consistently with the v1 and v6 clients. + if classified := httperr.ClassifyStatus(resp.StatusCode); classified != nil { + return classified + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + return errUnknown + } + + return &ResponseError{ + StatusCode: resp.StatusCode, + Body: string(data), + } +} diff --git a/internal/cloudapi/provisioning/errors_test.go b/internal/cloudapi/provisioning/errors_test.go new file mode 100644 index 000000000..d166a2e6d --- /dev/null +++ b/internal/cloudapi/provisioning/errors_test.go @@ -0,0 +1,62 @@ +package provisioning + +import ( + "io" + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCheckResponse(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + statusCode int + body string + wantErr bool + wantStatus int + }{ + {"2xx returns nil", 200, "{}", false, 0}, + {"4xx wraps as ResponseError", 400, `{"error":"bad"}`, true, 400}, + {"5xx wraps as ResponseError", 500, `{"error":"oops"}`, true, 500}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + resp := &http.Response{ + StatusCode: tc.statusCode, + Body: io.NopCloser(strings.NewReader(tc.body)), + } + + err := CheckResponse(resp) + if !tc.wantErr { + assert.NoError(t, err) + return + } + + require.Error(t, err) + var respErr *ResponseError + require.ErrorAs(t, err, &respErr) + assert.Equal(t, tc.wantStatus, respErr.StatusCode) + }) + } +} + +func TestResponseError_Error_FormatsStatusAndBody(t *testing.T) { + t.Parallel() + + re := &ResponseError{ + StatusCode: 422, + Body: "validation failed", + } + + msg := re.Error() + assert.Contains(t, msg, "422") + assert.Contains(t, msg, "validation failed") +} diff --git a/internal/cloudapi/provisioning/http_client.go b/internal/cloudapi/provisioning/http_client.go new file mode 100644 index 000000000..0ce17edea --- /dev/null +++ b/internal/cloudapi/provisioning/http_client.go @@ -0,0 +1,78 @@ +package provisioning + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + + "github.com/sirupsen/logrus" +) + +// HTTPClient is the scoped-token Bearer HTTP layer injected into the +// cloud Output's expv2 metrics push (and the notify call) when running +// in provisioning mode. It signs requests with the scoped +// test_run_token, retries on 5xx and transport errors via the shared +// doWithRetry helper, and decodes JSON responses into a caller-provided +// struct. Unlike Client, it drives no provisioning/v6 orchestration and +// implements only Do — not BaseURL — because the metrics URL is set +// explicitly by the caller (no client-side derivation). +type HTTPClient struct { + httpClient *http.Client + token string // scoped test_run_token + version string // for User-Agent + logger logrus.FieldLogger +} + +// NewHTTPClient constructs an HTTPClient for metrics push and notify +// in provisioning mode. version is used for the User-Agent header, +// matching the v1/v6 client convention `k6cloud/`. +func NewHTTPClient(httpClient *http.Client, token, version string, logger logrus.FieldLogger) *HTTPClient { + return &HTTPClient{httpClient: httpClient, token: token, version: version, logger: logger} +} + +// Do executes the request with Bearer auth, retries on 5xx and +// transport errors, and decodes the response body into v if non-nil. +func (p *HTTPClient) Do(req *http.Request, v any) error { + // Ensure GetBody is set so the body can be replayed on retries. + if req.Body != nil && req.GetBody == nil { + body, err := io.ReadAll(req.Body) + if err != nil { + return fmt.Errorf("reading request body: %w", err) + } + _ = req.Body.Close() + + req.GetBody = func() (io.ReadCloser, error) { + return io.NopCloser(bytes.NewReader(body)), nil + } + req.Body, _ = req.GetBody() + req.ContentLength = int64(len(body)) + } + + req.Header.Set("Authorization", "Bearer "+p.token) + req.Header.Set("User-Agent", "k6cloud/"+p.version) + + resp, err := doWithRetry(p.httpClient, req) + if err != nil { + return err + } + + defer func() { + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + }() + + if err := CheckResponse(resp); err != nil { + return err + } + + if v != nil { + if err := json.NewDecoder(resp.Body).Decode(v); err != nil && !errors.Is(err, io.EOF) { + return fmt.Errorf("decoding response: %w", err) + } + } + + return nil +} diff --git a/internal/cloudapi/provisioning/http_client_test.go b/internal/cloudapi/provisioning/http_client_test.go new file mode 100644 index 000000000..9e09428be --- /dev/null +++ b/internal/cloudapi/provisioning/http_client_test.go @@ -0,0 +1,191 @@ +package provisioning + +import ( + "bytes" + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cloudapi/clientcfg" + "go.k6.io/k6/v2/internal/lib/testutils" +) + +func TestHTTPClient(t *testing.T) { + t.Parallel() + + var ( + gotAuth string + gotUA string + hits atomic.Int32 + ) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + gotAuth = r.Header.Get("Authorization") + gotUA = r.Header.Get("User-Agent") + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]string{"foo": "bar"}) + })) + defer srv.Close() + + c := NewHTTPClient(srv.Client(), "scoped-token", "v1.2.3", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL, nil) + require.NoError(t, err) + + var target struct { + Foo string `json:"foo"` + } + err = c.Do(req, &target) + require.NoError(t, err) + + assert.Equal(t, "bar", target.Foo) + assert.Equal(t, "Bearer scoped-token", gotAuth) + assert.Equal(t, "k6cloud/v1.2.3", gotUA) + assert.Equal(t, int32(1), hits.Load()) +} + +func TestHTTPClient_NilVDoesNotDecode(t *testing.T) { + t.Parallel() + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + c := NewHTTPClient(srv.Client(), "tok", "v0.1.0", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL, nil) + require.NoError(t, err) + + err = c.Do(req, nil) + assert.NoError(t, err) +} + +func TestHTTPClient_RetriesOn5xx(t *testing.T) { + t.Parallel() + + var hits atomic.Int32 + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + if hits.Add(1) == 1 { + w.WriteHeader(http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + + c := NewHTTPClient(srv.Client(), "tok", "v0.1.0", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL, nil) + require.NoError(t, err) + + err = c.Do(req, nil) + assert.NoError(t, err) + assert.Equal(t, int32(2), hits.Load()) +} + +func TestHTTPClient_NoRetryOn4xx(t *testing.T) { + t.Parallel() + + var hits atomic.Int32 + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + hits.Add(1) + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"error":"bad input"}`)) + })) + defer srv.Close() + + c := NewHTTPClient(srv.Client(), "tok", "v0.1.0", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL, nil) + require.NoError(t, err) + + err = c.Do(req, nil) + require.Error(t, err) + assert.Contains(t, err.Error(), "400") + assert.Contains(t, err.Error(), "bad input") + assert.Equal(t, int32(1), hits.Load()) +} + +func TestHTTPClient_ContextCancelStopsRetryWait(t *testing.T) { + t.Parallel() + + // Server always fails with 5xx so the client enters the retry wait. + hit := make(chan struct{}, 10) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + hit <- struct{}{} + w.WriteHeader(http.StatusInternalServerError) + })) + defer srv.Close() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + c := NewHTTPClient(srv.Client(), "tok", "v0.1.0", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, srv.URL, nil) + require.NoError(t, err) + + // Cancel as soon as the first attempt reaches the server, so the + // retry wait (RetryInterval) is interrupted by the cancellation. + go func() { + <-hit + cancel() + }() + + start := time.Now() + err = c.Do(req, nil) + elapsed := time.Since(start) + + require.Error(t, err) + assert.ErrorIs(t, err, context.Canceled) + assert.Less(t, elapsed, clientcfg.RetryInterval, + "cancellation should interrupt the retry wait instead of sleeping the full interval") +} + +func TestHTTPClient_RequestBodyReplayedOnRetry(t *testing.T) { + t.Parallel() + + var ( + hits atomic.Int32 + body1 []byte + body2 []byte + ) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + if hits.Add(1) == 1 { + body1 = b + w.WriteHeader(http.StatusInternalServerError) + return + } + body2 = b + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + + payload := []byte(`{"metrics":[1,2,3]}`) + c := NewHTTPClient(srv.Client(), "tok", "v0.1.0", testutils.NewLogger(t)) + + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, srv.URL, bytes.NewReader(payload)) + require.NoError(t, err) + + err = c.Do(req, nil) + require.NoError(t, err) + assert.Equal(t, int32(2), hits.Load()) + assert.Equal(t, body1, body2, "body replay should produce identical bytes on retry") +} diff --git a/internal/cloudapi/provisioning/notify.go b/internal/cloudapi/provisioning/notify.go new file mode 100644 index 000000000..e7d38caf2 --- /dev/null +++ b/internal/cloudapi/provisioning/notify.go @@ -0,0 +1,85 @@ +package provisioning + +import ( + "context" + "errors" + "fmt" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + + "go.k6.io/k6/v2/errext" +) + +// notifyError represents a script execution error in the notify +// request body, mapping k6 abort reasons to canonical k6-cloud codes. +type notifyError struct { + Code int32 + Reason string +} + +// mapTestErrorToNotifyCode translates a k6 test error (with an +// optional errext.AbortReason) into the canonical k6-cloud notify +// error code. Returns nil when the test completed without error or +// when the abort reason indicates a normal completion +// (AbortedByThresholdsAfterTestEnd). +func mapTestErrorToNotifyCode(testErr error) *notifyError { + if testErr == nil { + return nil + } + + if hasAbortReason, ok := errors.AsType[errext.HasAbortReason](testErr); ok { + switch hasAbortReason.AbortReason() { + case errext.AbortedByUser, errext.AbortedByScriptAbort: + return ¬ifyError{Code: 8036, Reason: testErr.Error()} + case errext.AbortedByThreshold: + return ¬ifyError{Code: 8036, Reason: testErr.Error()} + case errext.AbortedByThresholdsAfterTestEnd: + return nil + case errext.AbortedByScriptError: + return ¬ifyError{Code: 8035, Reason: testErr.Error()} + case errext.AbortedByTimeout, errext.AbortedByOutput: + return ¬ifyError{Code: 8034, Reason: testErr.Error()} + } + } + + return ¬ifyError{Code: 0, Reason: testErr.Error()} +} + +// NotifyTestRunCompleted posts a script_execution_completed event to +// POST /provisioning/v1/test_runs/{id}/notify using the generated SDK +// method. The scoped testRunToken is passed via context (not the +// long-lived API token). testErr is mapped to an optional error code +// in the request body. Retries on 5xx via the SDK's built-in retry; +// does not retry on 4xx. +func (c *Client) NotifyTestRunCompleted( + ctx context.Context, testRunID int64, testRunToken string, testErr error, +) error { + // Use the scoped test-run token instead of the long-lived API token. + // The SDK reads ContextAccessToken from the context and sets + // Authorization: Bearer . + scopedCtx := context.WithValue(ctx, k6cloud.ContextAccessToken, testRunToken) + + model := k6cloud.NewScriptExecutionCompletedNotificationApiModel("script_execution_completed") + if ne := mapTestErrorToNotifyCode(testErr); ne != nil { + model.SetError(*k6cloud.NewScriptExecutionError(ne.Code, ne.Reason)) + } else { + model.SetErrorNil() + } + + hr, err := c.apiClient.ProvisioningAPI. + TestRunsNotify(scopedCtx, testRunID). + ScriptExecutionCompletedNotificationApiModel(model). + Execute() + defer closeResponse(hr, &err) + + if hr != nil { + if respErr := CheckResponse(hr); respErr != nil { + return fmt.Errorf("notify test run completed: %w", respErr) + } + } + if err != nil { + return fmt.Errorf("notify test run completed: %w", err) + } + + return nil +} diff --git a/internal/cloudapi/provisioning/notify_test.go b/internal/cloudapi/provisioning/notify_test.go new file mode 100644 index 000000000..d8624f519 --- /dev/null +++ b/internal/cloudapi/provisioning/notify_test.go @@ -0,0 +1,56 @@ +package provisioning + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/errext" +) + +// abortErr constructs an error with an attached AbortReason, +// matching the pattern used elsewhere in k6's test suite. +func abortErr(reason errext.AbortReason, msg string) error { + return errext.WithAbortReasonIfNone(errors.New(msg), reason) +} + +func TestMapTestErrorToNotifyCode(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + err error + wantNil bool + wantCode int32 + wantReasonContains string + }{ + {"nil returns nil", nil, true, 0, ""}, + {"AbortedByUser → 8036", abortErr(errext.AbortedByUser, "user pressed Ctrl+C"), false, 8036, "user"}, + {"AbortedByScriptAbort → 8036", abortErr(errext.AbortedByScriptAbort, "abort()"), false, 8036, "abort"}, + {"AbortedByScriptError → 8035", abortErr(errext.AbortedByScriptError, "TypeError"), false, 8035, "TypeError"}, + {"AbortedByOutput → 8034", abortErr(errext.AbortedByOutput, "cloud push failed"), false, 8034, "cloud"}, + {"AbortedByTimeout → 8034", abortErr(errext.AbortedByTimeout, "timeout"), false, 8034, "timeout"}, + {"AbortedByThreshold → 8036", abortErr(errext.AbortedByThreshold, "threshold crossed"), false, 8036, "threshold"}, + {"AbortedByThresholdsAfterTestEnd → nil (test completed)", abortErr(errext.AbortedByThresholdsAfterTestEnd, "post-end"), true, 0, ""}, + {"generic error → 0 (Unknown)", errors.New("something else"), false, 0, "something else"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + got := mapTestErrorToNotifyCode(tc.err) + + if tc.wantNil { + assert.Nil(t, got) + return + } + + require.NotNil(t, got, "expected non-nil notifyError") + assert.Equal(t, tc.wantCode, got.Code) + assert.Contains(t, got.Reason, tc.wantReasonContains) + }) + } +} diff --git a/internal/cloudapi/provisioning/provision.go b/internal/cloudapi/provisioning/provision.go new file mode 100644 index 000000000..bdcd4f8ed --- /dev/null +++ b/internal/cloudapi/provisioning/provision.go @@ -0,0 +1,110 @@ +package provisioning + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "time" + + "go.k6.io/k6/v2/lib" +) + +// ProvisionParams contains the parameters for provisioning a local +// execution test run. +type ProvisionParams struct { + // Name is the load test name. + Name string + + // ProjectID is the cloud project ID. + ProjectID int64 + + // MaxVUs is the maximum number of virtual users. + MaxVUs int64 + + // TotalDuration is the total test duration in seconds. + TotalDuration int64 + + // Options is the pre-marshalled lib.Options JSON from cmd. + Options json.RawMessage + + // Archive is the test archive. Nil when --no-archive-upload is set. + Archive *lib.Archive + + // PollInterval is the interval between test-run status polls. + // Zero or negative uses the default interval. + PollInterval time.Duration +} + +// ProvisionResult contains the result of a successful provisioning +// operation. +type ProvisionResult struct { + // TestRunID is the ID of the provisioned test run. + TestRunID int64 + + // TestRunDetailsPageURL is the URL of the test run details page. + TestRunDetailsPageURL string + + // RuntimeConfig carries the metrics, secrets, and token + // configuration returned by the provisioning API. + RuntimeConfig RuntimeConfig +} + +// ProvisionLocalExecution orchestrates the full local-execution +// provisioning flow: CreateOrFindLoadTest → StartLocalExecution → +// optional UploadArchive → WaitForTestRunReady. +func (c *Client) ProvisionLocalExecution(ctx context.Context, params ProvisionParams) (*ProvisionResult, error) { + loadTestID, err := c.v6Client.CreateOrFindLoadTest(ctx, params.ProjectID, params.Name) + if err != nil { + return nil, fmt.Errorf("create or find load test: %w", err) + } + + // Serialise archive once to get its byte-length for the + // start_local_execution body. The same buffer is reused for + // the S3 upload to avoid a second serialisation. + var ( + archiveSize int64 + archiveBytes []byte + ) + if params.Archive != nil { + var buf bytes.Buffer + if err := params.Archive.Write(&buf); err != nil { + return nil, fmt.Errorf("serialising archive: %w", err) + } + archiveSize = int64(buf.Len()) + archiveBytes = buf.Bytes() + } + + sleReq := StartLocalExecutionRequest{ + Options: params.Options, + MaxVUs: params.MaxVUs, + TotalDuration: params.TotalDuration, + ArchiveSize: archiveSize, + } + + sleResp, err := c.StartLocalExecution(ctx, loadTestID, sleReq) + if err != nil { + return nil, fmt.Errorf("start local execution: %w", err) + } + + switch { + case params.Archive != nil && sleResp.ArchiveUploadURL != nil: + if err := c.UploadArchive(ctx, *sleResp.ArchiveUploadURL, archiveBytes); err != nil { + return nil, fmt.Errorf("upload archive: %w", err) + } + case params.Archive != nil && sleResp.ArchiveUploadURL == nil: + // We had an archive to upload but the API returned no upload URL; + // proceed without uploading rather than failing the run. + c.logger.Warn("archive present but provisioning API returned no upload URL; skipping archive upload") + } + + if err := c.WaitForTestRunReady(ctx, sleResp.TestRunID, params.PollInterval); err != nil { + return nil, fmt.Errorf("wait for test run ready: %w", err) + } + + return &ProvisionResult{ + TestRunID: sleResp.TestRunID, + TestRunDetailsPageURL: sleResp.TestRunDetailsPageURL, + RuntimeConfig: sleResp.RuntimeConfig, + }, nil +} diff --git a/internal/cloudapi/provisioning/provision_test.go b/internal/cloudapi/provisioning/provision_test.go new file mode 100644 index 000000000..93346be7f --- /dev/null +++ b/internal/cloudapi/provisioning/provision_test.go @@ -0,0 +1,512 @@ +package provisioning + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "sync" + "sync/atomic" + "testing" + "time" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + provtest "go.k6.io/k6/v2/internal/cloudapi/provisioning/test" + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" + "go.k6.io/k6/v2/internal/lib/testutils" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/fsext" +) + +const ( + testLoadTestID int64 = 42 + testTestRunID int64 = 100 + testProjectID int64 = 1 +) + +// callRecorder tracks the sequence of HTTP endpoint calls for test assertions. +type callRecorder struct { + mu sync.Mutex + calls []string +} + +func (cr *callRecorder) record(name string) { + cr.mu.Lock() + defer cr.mu.Unlock() + cr.calls = append(cr.calls, name) +} + +func (cr *callRecorder) order() []string { + cr.mu.Lock() + defer cr.mu.Unlock() + return append([]string{}, cr.calls...) +} + +func (cr *callRecorder) count(name string) int { + cr.mu.Lock() + defer cr.mu.Unlock() + n := 0 + for _, c := range cr.calls { + if c == name { + n++ + } + } + return n +} + +func newTestArchive(t *testing.T) *lib.Archive { + t.Helper() + fs := fsext.NewMemMapFs() + require.NoError(t, fsext.WriteFile(fs, "/a.js", []byte(`// test`), 0o644)) + return &lib.Archive{ + Type: "js", + K6Version: "0.42.0", + FilenameURL: &url.URL{Scheme: "file", Path: "/a.js"}, + PwdURL: &url.URL{Scheme: "file", Path: "/"}, + Data: []byte(`// test`), + Filesystems: map[string]fsext.Fs{"file": fs}, + } +} + +// writeCreateResponse writes a v6 create-load-test response using testLoadTestID. +func writeCreateResponse(w http.ResponseWriter) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(testLoadTestID) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + if err := json.NewEncoder(w).Encode(res); err != nil { + panic(fmt.Errorf("encoding create response: %w", err)) + } +} + +// writeSLEResponse writes a StartLocalExecution response using +// testTestRunID and the given archive upload URL. +func writeSLEResponse(w http.ResponseWriter, uploadURL string) { + resp := provtest.DefaultStartLocalExecutionResponse() + resp.SetTestRunId(testTestRunID) + u := uploadURL + resp.ArchiveUploadUrl = *k6cloud.NewNullableString(&u) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("https://app.k6.io/runs/%d", testTestRunID)) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(resp); err != nil { + panic(fmt.Errorf("encoding SLE response: %w", err)) + } +} + +func TestProvisionLocalExecution_WithArchive(t *testing.T) { + t.Parallel() + + var rec callRecorder + + srv := provtest.NewServer(t) + + // 1. Create-or-find → load test ID 42. + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("create") + writeCreateResponse(w) + }) + + // 2. Start local execution → test run ID 100. + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("start") + writeSLEResponse(w, srv.PresignedUploadURL()) + }) + + // 3. S3 presigned upload → 200. + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + rec.record("upload") + w.WriteHeader(http.StatusOK) + }) + + // 4. Test-run poll → "initializing" immediately. + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + result, err := client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 50, + TotalDuration: 630, + Options: json.RawMessage(`{"vus":10,"duration":"30s"}`), + Archive: newTestArchive(t), + PollInterval: time.Microsecond, + }) + require.NoError(t, err) + + // --- ProvisionResult assertions --- + assert.Equal(t, testTestRunID, result.TestRunID) + assert.Contains(t, result.TestRunDetailsPageURL, "/runs/100") + assert.Equal(t, "https://ingest.k6.io/v1/metrics", result.RuntimeConfig.Metrics.PushURL) + assert.Equal(t, "test-run-token-abc", result.RuntimeConfig.TestRunToken) + assert.Equal(t, + "https://api.k6.io/provisioning/v1/test_runs/123/decrypt_secret?name={key}", + result.RuntimeConfig.Secrets.Endpoint) + assert.Equal(t, "plaintext", result.RuntimeConfig.Secrets.ResponsePath) + + // --- Call counts --- + assert.Equal(t, 1, rec.count("create")) + assert.Equal(t, 1, rec.count("start")) + assert.Equal(t, 1, rec.count("upload")) + assert.GreaterOrEqual(t, srv.FetchTestRunHitCount(), int32(1)) + + // --- Call order (create → start → upload; poll follows implicitly) --- + calls := rec.order() + require.Len(t, calls, 3) + assert.Equal(t, []string{"create", "start", "upload"}, calls) +} + +func TestProvisionLocalExecution_NoArchive(t *testing.T) { + t.Parallel() + + var sleBody []byte + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + writeCreateResponse(w) + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, r *http.Request) { + var err error + sleBody, err = io.ReadAll(r.Body) + if err != nil { + http.Error(w, "read body", http.StatusInternalServerError) + return + } + writeSLEResponse(w, srv.PresignedUploadURL()) + }) + + var uploadHits atomic.Int32 + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + uploadHits.Add(1) + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + result, err := client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 10, + TotalDuration: 60, + Options: json.RawMessage(`{"vus":1}`), + Archive: nil, + PollInterval: time.Microsecond, + }) + require.NoError(t, err) + + // archive_size must be explicit JSON null in the SLE request body. + var raw map[string]json.RawMessage + require.NoError(t, json.Unmarshal(sleBody, &raw)) + archiveRaw, exists := raw["archive_size"] + require.True(t, exists, "archive_size key must be present") + assert.Equal(t, "null", string(archiveRaw)) + + // S3 stub must NOT be called. + assert.Equal(t, int32(0), uploadHits.Load(), "upload must not be called when archive is nil") + + // Polling must still happen (unconditional). + assert.GreaterOrEqual(t, srv.FetchTestRunHitCount(), int32(1)) + + // ProvisionResult populated. + assert.Equal(t, testTestRunID, result.TestRunID) + assert.NotEmpty(t, result.TestRunDetailsPageURL) + assert.NotEmpty(t, result.RuntimeConfig.TestRunToken) +} + +func TestProvisionLocalExecution_409ConflictPropagatesToFindByName(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + + // Create: POST → 409; GET (findByName) → matching test with ID 42. + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusConflict) + _ = json.NewEncoder(w).Encode(map[string]any{ + "error": map[string]string{"code": "conflict", "message": "already exists"}, + }) + case http.MethodGet: + lt := k6cloud.NewLoadTestApiModelWithDefaults() + lt.SetId(testLoadTestID) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(map[string]any{"value": []any{lt}}) + } + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + writeSLEResponse(w, srv.PresignedUploadURL()) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + result, err := client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 50, + TotalDuration: 630, + Options: json.RawMessage(`{"vus":10}`), + Archive: newTestArchive(t), + PollInterval: time.Microsecond, + }) + require.NoError(t, err) + + assert.Equal(t, testTestRunID, result.TestRunID) + assert.Contains(t, result.TestRunDetailsPageURL, "/runs/100") + assert.Equal(t, "test-run-token-abc", result.RuntimeConfig.TestRunToken) +} + +func TestProvisionLocalExecution_ArchivePresentButNoUploadURL(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + writeCreateResponse(w) + }) + + // start_local_execution returns no archive upload URL (null). + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + resp := provtest.DefaultStartLocalExecutionResponse() + resp.SetTestRunId(testTestRunID) + resp.ArchiveUploadUrl = *k6cloud.NewNullableString(nil) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("https://app.k6.io/runs/%d", testTestRunID)) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + require.NoError(t, json.NewEncoder(w).Encode(resp)) + }) + + // The upload endpoint must not be hit when no URL is returned. + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + t.Error("archive upload must not be attempted when no upload URL is returned") + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + // Archive provided but API returns no upload URL: provisioning should + // still succeed (a warning is logged) and skip the upload. + result, err := client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 50, + TotalDuration: 630, + Options: json.RawMessage(`{"vus":10}`), + Archive: newTestArchive(t), + PollInterval: time.Microsecond, + }) + require.NoError(t, err) + assert.Equal(t, testTestRunID, result.TestRunID) +} + +func TestProvisionLocalExecution_ErrorAtCreate(t *testing.T) { + t.Parallel() + + var rec callRecorder + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("create") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _ = json.NewEncoder(w).Encode(map[string]any{ + "error": map[string]string{"code": "server_error", "message": "internal error"}, + }) + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("start") + w.WriteHeader(http.StatusOK) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + rec.record("upload") + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + _, err = client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 10, + TotalDuration: 60, + Options: json.RawMessage(`{"vus":1}`), + PollInterval: time.Microsecond, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "create or find load test") + + // Subsequent endpoints must not be called. + assert.Equal(t, 0, rec.count("start")) + assert.Equal(t, 0, rec.count("upload")) + assert.Equal(t, int32(0), srv.FetchTestRunHitCount()) +} + +func TestProvisionLocalExecution_ErrorAtStart(t *testing.T) { + t.Parallel() + + var rec callRecorder + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("create") + writeCreateResponse(w) + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + rec.record("start") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(`{"error":{"code":"server_error","message":"start failed"}}`)) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + rec.record("upload") + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + _, err = client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 10, + TotalDuration: 60, + Options: json.RawMessage(`{"vus":1}`), + PollInterval: time.Microsecond, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "start local execution") + + // S3 + poll must not be called. + assert.Equal(t, 0, rec.count("upload")) + assert.Equal(t, int32(0), srv.FetchTestRunHitCount()) +} + +func TestProvisionLocalExecution_ErrorAtUpload(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + writeCreateResponse(w) + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + writeSLEResponse(w, srv.PresignedUploadURL()) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte("Request has expired")) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + _, err = client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 50, + TotalDuration: 630, + Options: json.RawMessage(`{"vus":10}`), + Archive: newTestArchive(t), + PollInterval: time.Microsecond, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "upload archive") + + // Poll must not be called. + assert.Equal(t, int32(0), srv.FetchTestRunHitCount()) +} + +func TestProvisionLocalExecution_ErrorAtPolling(t *testing.T) { + t.Parallel() + + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(testProjectID, func(w http.ResponseWriter, _ *http.Request) { + writeCreateResponse(w) + }) + + srv.HandleStartLocalExecution(testLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + writeSLEResponse(w, srv.PresignedUploadURL()) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(testTestRunID, []v6.TestProgress{ + { + Status: v6.StatusAborted, + StatusHistory: []v6.StatusEvent{ + {Status: v6.StatusAborted, Message: "quota exceeded"}, + }, + }, + }) + + client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "0.42.0", 7, 5*time.Second) + require.NoError(t, err) + + _, err = client.ProvisionLocalExecution(t.Context(), ProvisionParams{ + Name: "my-test", + ProjectID: testProjectID, + MaxVUs: 50, + TotalDuration: 630, + Options: json.RawMessage(`{"vus":10}`), + Archive: newTestArchive(t), + PollInterval: time.Microsecond, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "wait for test run ready") + assert.Contains(t, err.Error(), "quota exceeded") +} diff --git a/internal/cloudapi/provisioning/test/server.go b/internal/cloudapi/provisioning/test/server.go new file mode 100644 index 000000000..6b80d27d4 --- /dev/null +++ b/internal/cloudapi/provisioning/test/server.go @@ -0,0 +1,213 @@ +// Package provtest provides an HTTP test server that simulates the +// provisioning API. Routes are registered by individual specs as they +// add API methods; this skeleton starts with an empty mux. +package provtest + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + + cloudapi "go.k6.io/k6/v2/internal/cloudapi/v6" +) + +const ( + // DefaultLoadTestID is the load test ID used by default handlers. + DefaultLoadTestID int64 = 456 + // DefaultTestRunID is the test run ID returned by default handlers. + DefaultTestRunID int64 = 123 + // PresignedUploadPath is the default path used for the presigned archive upload stub. + PresignedUploadPath = "/upload/archive.tar" +) + +// Server is a test HTTP server for the provisioning API. +type Server struct { + *httptest.Server + Mux *http.ServeMux + + fetchTestRunHits *atomic.Int32 +} + +// NewServer creates and starts a test server with an empty route table. +// The server is automatically closed when the test finishes. +func NewServer(t *testing.T) *Server { + t.Helper() + + mux := http.NewServeMux() + srv := httptest.NewServer(mux) + t.Cleanup(srv.Close) + + return &Server{ + Server: srv, + Mux: mux, + } +} + +// HandleCreateLoadTest registers a handler for +// /cloud/v6/projects/{projectID}/load_tests, covering both the POST +// (create) and GET (find-by-name) operations of +// v6.Client.CreateOrFindLoadTest. The handler should inspect r.Method +// to distinguish between create (POST) and find-by-name (GET). +func (s *Server) HandleCreateLoadTest(projectID int64, handler http.HandlerFunc) { + s.Mux.HandleFunc( + fmt.Sprintf("/cloud/v6/projects/%d/load_tests", projectID), + handler, + ) +} + +// HandleStartLocalExecution registers a handler for +// POST /provisioning/v1/load_tests/{loadTestID}/start_local_execution. +// If handler is nil, a default handler returning a successful response is used. +func (s *Server) HandleStartLocalExecution(loadTestID int64, handler http.HandlerFunc) { + if handler == nil { + handler = func(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, DefaultStartLocalExecutionResponse()) + } + } + s.Mux.HandleFunc( + fmt.Sprintf("POST /provisioning/v1/load_tests/%d/start_local_execution", loadTestID), + handler, + ) +} + +// DefaultStartLocalExecutionResponse returns a fully-populated +// StartLocalExecutionTestResponse suitable for test assertions. +func DefaultStartLocalExecutionResponse() *k6cloud.StartLocalExecutionTestResponse { + metrics := k6cloud.NewMetricsRuntimeConfig( + "https://ingest.k6.io/v1/metrics", + *k6cloud.NewNullableString(new("2s")), + *k6cloud.NewNullableInt32(new(int32(5))), + *k6cloud.NewNullableString(new("3s")), + *k6cloud.NewNullableString(new("1s")), + *k6cloud.NewNullableInt32(new(int32(50))), + *k6cloud.NewNullableInt32(new(int32(2000))), + ) + + secrets := k6cloud.NewSecretsRuntimeConfig( + "https://api.k6.io/provisioning/v1/test_runs/123/decrypt_secret?name={key}", + "plaintext", + ) + + traces := k6cloud.NewTracesRuntimeConfig( + "https://traces.k6.io", + map[string]string{}, + "http", + ) + files := k6cloud.NewFilesRuntimeConfig( + "https://files.k6.io", + "/screenshots", + map[string]string{}, + "POST", + ) + logs := k6cloud.NewLogsRuntimeConfig( + "https://logs.k6.io", + "https://logs.k6.io/tail", + 900, + "3s", + "info", + 10000, + []string{"lz", "level"}, + ) + + rc := k6cloud.NewRuntimeConfig( + *metrics, + *traces, + *files, + *logs, + *secrets, + "test-run-token-abc", + ) + + uploadURL := "https://s3.amazonaws.com/bucket/archive.tar?presigned=1" + return k6cloud.NewStartLocalExecutionTestResponse( + DefaultTestRunID, + *rc, + *k6cloud.NewNullableString(&uploadURL), + fmt.Sprintf("https://app.k6.io/runs/%d", DefaultTestRunID), + ) +} + +func writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + if err := json.NewEncoder(w).Encode(v); err != nil { + panic(fmt.Errorf("provtest: encoding JSON: %w", err)) + } +} + +// HandlePresignedUpload registers a handler for PUT requests at the given path, +// simulating a presigned S3 upload endpoint. +func (s *Server) HandlePresignedUpload(path string, handler http.HandlerFunc) { + s.Mux.HandleFunc("PUT "+path, handler) +} + +// PresignedUploadURL returns the full URL for the presigned upload stub +// using the default PresignedUploadPath. +func (s *Server) PresignedUploadURL() string { + return s.URL + PresignedUploadPath +} + +// HandleFetchTestRun registers a handler for +// GET /cloud/v6/test_runs/{testRunID}. The handler returns successive +// TestProgress values from the provided sequence. Once the sequence is +// exhausted, the last element is repeated. +// +// It also tracks the number of times the handler was called; use +// FetchTestRunHitCount to query it. +func (s *Server) HandleFetchTestRun(testRunID int64, sequence []cloudapi.TestProgress) { + var idx atomic.Int32 + var hits atomic.Int32 + + s.fetchTestRunHits = &hits + + s.Mux.HandleFunc( + fmt.Sprintf("GET /cloud/v6/test_runs/%d", testRunID), + func(w http.ResponseWriter, _ *http.Request) { + hits.Add(1) + + i := int(idx.Load()) + if i < len(sequence)-1 { + idx.Add(1) + } + tp := sequence[i] + + res := k6cloud.NewTestRunApiModelWithDefaults() + res.SetStatus(tp.Status.String()) + res.SetResult(tp.Result.String()) + res.SetEstimatedDuration(tp.EstimatedDuration) + res.SetExecutionDuration(tp.ExecutionDuration) + res.SetStatusHistory(cloudapi.ToStatusModel(tp.StatusHistory)) + res.SetDistribution([]k6cloud.DistributionZoneApiModel{}) + res.SetResultDetails( + k6cloud.ResultDetailsApiModelAsTestRunApiModelResultDetails( + k6cloud.NewResultDetailsApiModel(""), + ), + ) + res.SetOptions(map[string]any{}) + writeJSON(w, http.StatusOK, res) + }, + ) +} + +// FetchTestRunHitCount returns the number of times the FetchTestRun +// handler was called. Returns 0 if HandleFetchTestRun was never called. +func (s *Server) FetchTestRunHitCount() int32 { + if s.fetchTestRunHits == nil { + return 0 + } + return s.fetchTestRunHits.Load() +} + +// HandleNotify registers a handler for +// POST /provisioning/v1/test_runs/{testRunID}/notify. +func (s *Server) HandleNotify(testRunID int64, handler http.HandlerFunc) { + s.Mux.HandleFunc( + fmt.Sprintf("POST /provisioning/v1/test_runs/%d/notify", testRunID), + handler, + ) +} diff --git a/internal/cloudapi/v6/api.go b/internal/cloudapi/v6/api.go index 1b435596f..908a0db32 100644 --- a/internal/cloudapi/v6/api.go +++ b/internal/cloudapi/v6/api.go @@ -19,20 +19,34 @@ import ( // Project is a Grafana Cloud k6 project. type Project struct { - ID int32 `json:"id"` + ID int64 `json:"id"` Name string `json:"name"` IsDefault bool `json:"is_default"` } // LoadTest is a Grafana Cloud k6 load test. type LoadTest struct { - ID int32 `json:"id"` - ProjectID int32 `json:"project_id"` + ID int64 `json:"id"` + ProjectID int64 `json:"project_id"` Name string `json:"name"` Created time.Time `json:"created"` Updated time.Time `json:"updated"` } +// LoadZone is a Grafana Cloud k6 load zone. +type LoadZone struct { + ID int64 `json:"id"` + // K6LoadZoneID is the identifier used to reference the load zone from + // k6 scripts (e.g. "amazon:us:ashburn"). + K6LoadZoneID string `json:"k6_load_zone_id"` + Name string `json:"name"` + // Public reports whether the load zone is a public (Grafana-managed) + // zone as opposed to a private/custom one. + Public bool `json:"public"` + // Available reports whether the load zone is currently usable. + Available bool `json:"available"` +} + // ListProjects retrieves the list of projects for the configured stack. func (c *Client) ListProjects(ctx context.Context) ([]Project, error) { const pageSize int32 = 1000 @@ -87,7 +101,7 @@ func (c *Client) listProjectsPage( } // ListLoadTests retrieves the list of load tests in the given project. -func (c *Client) ListLoadTests(ctx context.Context, projectID int32) ([]LoadTest, error) { +func (c *Client) ListLoadTests(ctx context.Context, projectID int64) ([]LoadTest, error) { const pageSize int32 = 1000 tests := []LoadTest{} @@ -121,7 +135,7 @@ func (c *Client) ListLoadTests(ctx context.Context, projectID int32) ([]LoadTest } func (c *Client) listLoadTestsPage( - ctx context.Context, projectID, skip, top int32, + ctx context.Context, projectID int64, skip, top int32, ) (*k6cloud.LoadTestListResponse, error) { res, hr, err := c.apiClient.LoadTestsAPI. ProjectsLoadTestsRetrieve(c.authCtx(ctx), projectID). @@ -141,6 +155,35 @@ func (c *Client) listLoadTestsPage( return res, nil } +// ListLoadZones retrieves the list of load zones available to the configured stack. +func (c *Client) ListLoadZones(ctx context.Context) (_ []LoadZone, err error) { + res, hr, err := c.apiClient.LoadZonesAPI. + LoadZonesList(c.authCtx(ctx)). + XStackId(c.stackID). + Execute() + defer closeResponse(hr, &err) + + if err := CheckResponse(hr, err); err != nil { + return nil, err + } + if res == nil { + return nil, errUnknown + } + + zones := make([]LoadZone, 0, len(res.Value)) + for _, zone := range res.Value { + zones = append(zones, LoadZone{ + ID: zone.Id, + K6LoadZoneID: zone.K6LoadZoneId, + Name: zone.Name, + Public: zone.Public, + Available: zone.Available, + }) + } + + return zones, nil +} + // ValidateToken validates the cloud authentication token. func (c *Client) ValidateToken(ctx context.Context, stackURL string) (_ *k6cloud.AuthenticationResponse, err error) { if stackURL == "" { @@ -167,7 +210,7 @@ func (c *Client) ValidateToken(ctx context.Context, stackURL string) (_ *k6cloud } // ValidateOptions validates cloud test options. -func (c *Client) ValidateOptions(ctx context.Context, projectID int32, opts lib.Options) (err error) { +func (c *Client) ValidateOptions(ctx context.Context, projectID int64, opts lib.Options) (err error) { // Round-trip [lib.Options] through JSON so every script option // reaches the backend via [k6cloud.Options.AdditionalProperties]. buf, err := json.Marshal(opts) @@ -200,9 +243,40 @@ func (c *Client) ValidateOptions(ctx context.Context, projectID int32, opts lib. return nil } +// CreateOrFindLoadTest creates a load test by name in the given project without +// uploading a script. If the API returns 409 (conflict), it falls back to +// looking up the existing test by name and returns its ID. +func (c *Client) CreateOrFindLoadTest(ctx context.Context, projectID int64, name string) (_ int64, err error) { + res, hr, err := c.apiClient.LoadTestsAPI. + ProjectsLoadTestsCreate(c.authCtx(ctx), projectID). + XStackId(c.stackID). + Name(name). + Execute() + defer closeResponse(hr, &err) + + if err := CheckResponse(hr, err); err != nil { + var rerr ResponseError + if errors.As(err, &rerr) && rerr.Response != nil && rerr.Response.StatusCode == http.StatusConflict { + lt, err := c.findTestByName(ctx, projectID, name) + if err != nil { + return 0, err + } + return lt.GetId(), nil + } + return 0, err + } + if res == nil { + // The SDK getters return zero values on a nil receiver, so guard + // against silently producing a bogus 0 id and report it clearly. + return 0, errors.New("unexpected nil response from CreateOrFindLoadTest") + } + + return res.GetId(), nil +} + // UploadTest creates or updates a cloud load test's script. func (c *Client) UploadTest( - ctx context.Context, name string, projectID int32, arc *lib.Archive, + ctx context.Context, name string, projectID int64, arc *lib.Archive, ) (*k6cloud.LoadTestApiModel, error) { lt, err := c.createTest(ctx, name, projectID, arc) if err == nil { @@ -228,7 +302,7 @@ func (c *Client) UploadTest( // createTest creates a new cloud load test in the given project. func (c *Client) createTest( - ctx context.Context, name string, projectID int32, arc *lib.Archive, + ctx context.Context, name string, projectID int64, arc *lib.Archive, ) (_ *k6cloud.LoadTestApiModel, err error) { res, hr, err := c.apiClient.LoadTestsAPI. ProjectsLoadTestsCreate(c.authCtx(ctx), projectID). @@ -249,7 +323,7 @@ func (c *Client) createTest( } func (c *Client) findTestByName( - ctx context.Context, projectID int32, name string, + ctx context.Context, projectID int64, name string, ) (_ *k6cloud.LoadTestApiModel, err error) { res, hr, err := c.apiClient.LoadTestsAPI. ProjectsLoadTestsRetrieve(c.authCtx(ctx), projectID). @@ -274,7 +348,7 @@ func (c *Client) findTestByName( return &tests[0], nil } -func (c *Client) updateScript(ctx context.Context, testID int32, arc *lib.Archive) (err error) { +func (c *Client) updateScript(ctx context.Context, testID int64, arc *lib.Archive) (err error) { res, err := c.apiClient.LoadTestsAPI. LoadTestsScriptUpdate(c.authCtx(ctx), testID). XStackId(c.stackID). @@ -286,7 +360,7 @@ func (c *Client) updateScript(ctx context.Context, testID int32, arc *lib.Archiv } // StartTest starts a cloud load test run. -func (c *Client) StartTest(ctx context.Context, loadTestID int32) (_ *k6cloud.StartLoadTestResponse, err error) { +func (c *Client) StartTest(ctx context.Context, loadTestID int64) (_ *k6cloud.StartLoadTestResponse, err error) { var key [8]byte if _, err := rand.Read(key[:]); err != nil { return nil, err @@ -310,7 +384,7 @@ func (c *Client) StartTest(ctx context.Context, loadTestID int32) (_ *k6cloud.St } // StopTest aborts a running cloud test run. -func (c *Client) StopTest(ctx context.Context, testRunID int32) (err error) { +func (c *Client) StopTest(ctx context.Context, testRunID int64) (err error) { hr, err := c.apiClient.TestRunsAPI. TestRunsAbort(c.authCtx(ctx), testRunID). XStackId(c.stackID). @@ -327,7 +401,7 @@ func (c *Client) StopTest(ctx context.Context, testRunID int32) (err error) { } // FetchTest fetches the current progress of a cloud test run. -func (c *Client) FetchTest(ctx context.Context, testRunID int32) (_ *TestProgress, err error) { +func (c *Client) FetchTest(ctx context.Context, testRunID int64) (_ *TestProgress, err error) { res, hr, err := c.apiClient.TestRunsAPI. TestRunsRetrieve(c.authCtx(ctx), testRunID). XStackId(c.stackID). diff --git a/internal/cloudapi/v6/api_test.go b/internal/cloudapi/v6/api_test.go index 40a6d5e93..68cb6add9 100644 --- a/internal/cloudapi/v6/api_test.go +++ b/internal/cloudapi/v6/api_test.go @@ -3,6 +3,7 @@ package cloudapi import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -51,8 +52,8 @@ func TestValidateToken(t *testing.T) { resp, err := client.ValidateToken(t.Context(), "https://stack.grafana.net") require.NoError(t, err) require.NotNil(t, resp) - assert.Equal(t, int32(123), resp.StackId) - assert.Equal(t, int32(456), resp.DefaultProjectId) + assert.Equal(t, int64(123), resp.StackId) + assert.Equal(t, int64(456), resp.DefaultProjectId) }) t.Run("unauthorized token should fail", func(t *testing.T) { @@ -195,7 +196,42 @@ func TestListLoadTests(t *testing.T) { Created: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), Updated: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), }, tests[0]) - assert.Equal(t, int32(2), tests[1].ID) + assert.Equal(t, int64(2), tests[1].ID) +} + +func TestListLoadZones(t *testing.T) { + t.Parallel() + + loadZone := func(id int64, k6LoadZoneID, name string, public, available bool) map[string]any { + return map[string]any{ + "id": id, + "k6_load_zone_id": k6LoadZoneID, + "name": name, + "public": public, + "available": available, + "custom_load_runner_image": nil, + } + } + + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/cloud/v6/load_zones", r.URL.Path) + writeJSON(t, w, http.StatusOK, map[string]any{ + "value": []any{ + loadZone(1, "amazon:us:ashburn", "US East (Ashburn)", true, true), + loadZone(2, "my-cluster", "My private cluster", false, false), + }, + }) + })) + + zones, err := client.ListLoadZones(t.Context()) + require.NoError(t, err) + require.Len(t, zones, 2) + assert.Equal(t, + LoadZone{ID: 1, K6LoadZoneID: "amazon:us:ashburn", Name: "US East (Ashburn)", Public: true, Available: true}, + zones[0]) + assert.Equal(t, + LoadZone{ID: 2, K6LoadZoneID: "my-cluster", Name: "My private cluster", Public: false, Available: false}, + zones[1]) } func TestRetryWithConnectionClose(t *testing.T) { @@ -219,7 +255,7 @@ func TestRetryWithConnectionClose(t *testing.T) { client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "1.0", 5*time.Second) require.NoError(t, err) - client.SetStackID(1) + require.NoError(t, client.SetStackID(1)) opts := lib.Options{VUs: null.IntFrom(10)} require.NoError(t, client.ValidateOptions(t.Context(), 1, opts)) @@ -240,7 +276,7 @@ func TestValidateOptions(t *testing.T) { var body struct { Options json.RawMessage `json:"options"` - ProjectID int32 `json:"project_id"` + ProjectID int64 `json:"project_id"` } client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.NoError(t, json.NewDecoder(r.Body).Decode(&body)) @@ -258,7 +294,7 @@ func TestValidateOptions(t *testing.T) { want, err := json.Marshal(opts) require.NoError(t, err) assert.JSONEq(t, string(want), string(body.Options)) - assert.Equal(t, int32(42), body.ProjectID) + assert.Equal(t, int64(42), body.ProjectID) } func TestUploadTest(t *testing.T) { @@ -353,13 +389,122 @@ func TestUploadTest(t *testing.T) { }) } +func TestCreateOrFindLoadTest(t *testing.T) { + t.Parallel() + + var ( + method string + path string + stackID string + authHeader string + formName string + hasScript bool + ) + + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + method = r.Method + path = r.URL.Path + stackID = r.Header.Get("X-Stack-Id") + authHeader = r.Header.Get("Authorization") + + require.NoError(t, r.ParseMultipartForm(1<<20)) + formName = r.FormValue("name") + _, _, err := r.FormFile("script") + hasScript = err == nil + + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(12345) + writeJSON(t, w, http.StatusOK, res) + })) + + id, err := client.CreateOrFindLoadTest(t.Context(), 99, "my-test") + require.NoError(t, err) + assert.Equal(t, int64(12345), id) + assert.Equal(t, http.MethodPost, method) + assert.Equal(t, "/cloud/v6/projects/99/load_tests", path) + assert.Equal(t, "my-test", formName) + assert.False(t, hasScript, "request must not include a script part") + assert.Equal(t, "1", stackID) + assert.Equal(t, "Bearer test-token", authHeader) +} + +func TestCreateOrFindLoadTest_ConflictFallsBackToFindByName(t *testing.T) { + t.Parallel() + + var ( + getHit bool + getName string + getTop string + ) + + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + writeError(t, w, http.StatusConflict, "conflict", "already exists") + case http.MethodGet: + getHit = true + getName = r.URL.Query().Get("name") + getTop = r.URL.Query().Get("$top") + lt := k6cloud.NewLoadTestApiModelWithDefaults() + lt.SetId(67890) + writeJSON(t, w, http.StatusOK, map[string]any{"value": []any{lt}}) + } + })) + + id, err := client.CreateOrFindLoadTest(t.Context(), 99, "my-test") + require.NoError(t, err) + assert.Equal(t, int64(67890), id) + assert.True(t, getHit, "GET fallback should have been called") + assert.Equal(t, "my-test", getName) + assert.Equal(t, "1", getTop) +} + +func TestCreateOrFindLoadTest_ConflictWithNoMatchReturnsError(t *testing.T) { + t.Parallel() + + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + writeError(t, w, http.StatusConflict, "conflict", "already exists") + case http.MethodGet: + writeJSON(t, w, http.StatusOK, map[string]any{"value": []any{}}) + } + })) + + _, err := client.CreateOrFindLoadTest(t.Context(), 99, "my-test") + assert.ErrorIs(t, err, errTestNotExists) +} + +func TestCreateOrFindLoadTest_NonConflictHTTPErrorReturned(t *testing.T) { + t.Parallel() + + var getHit bool + client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodPost: + writeError(t, w, http.StatusInternalServerError, "server_error", "internal error") + case http.MethodGet: + getHit = true + w.WriteHeader(http.StatusOK) + } + })) + + _, err := client.CreateOrFindLoadTest(t.Context(), 99, "my-test") + require.Error(t, err) + + var rerr ResponseError + assert.True(t, errors.As(err, &rerr)) + assert.Equal(t, http.StatusInternalServerError, rerr.Response.StatusCode) + assert.False(t, getHit, "GET fallback should not be called on non-conflict error") +} + func TestStartTest(t *testing.T) { t.Parallel() res := k6cloud.NewStartLoadTestResponseWithDefaults() res.SetId(7) res.SetDistribution([]k6cloud.DistributionZoneApiModel{}) - res.SetResultDetails(map[string]any{}) + res.SetResultDetails(*k6cloud.NewResultDetailsApiModel("")) res.SetOptions(map[string]any{}) client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -419,7 +564,7 @@ func TestFetchTest(t *testing.T) { res.SetExecutionDuration(want.ExecutionDuration) res.SetStatusHistory(ToStatusModel(want.StatusHistory)) res.SetDistribution([]k6cloud.DistributionZoneApiModel{*k6cloud.NewDistributionZoneApiModelWithDefaults()}) - res.SetResultDetails(map[string]any{"foo": "bar"}) + res.SetResultDetails(k6cloud.ResultDetailsApiModelAsTestRunApiModelResultDetails(k6cloud.NewResultDetailsApiModel("failed"))) res.SetOptions(map[string]any{"vus": 10}) client := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -440,7 +585,7 @@ func newTestClient(t *testing.T, handler http.Handler) *Client { client, err := NewClient(testutils.NewLogger(t), "test-token", srv.URL, "1.0", 5*time.Second) require.NoError(t, err) - client.SetStackID(1) + require.NoError(t, client.SetStackID(1)) return client } diff --git a/internal/cloudapi/v6/client.go b/internal/cloudapi/v6/client.go index 007412e14..61b49354a 100644 --- a/internal/cloudapi/v6/client.go +++ b/internal/cloudapi/v6/client.go @@ -5,18 +5,15 @@ import ( "errors" "fmt" "io" + "math" "net/http" "time" k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" "github.com/sirupsen/logrus" -) - -const ( - // RetryInterval is the default cloud request retry interval - RetryInterval = 500 * time.Millisecond - // MaxRetries specifies max retry attempts - MaxRetries = 3 + "go.k6.io/k6/v2/errext" + "go.k6.io/k6/v2/internal/cloudapi/clientcfg" + "go.k6.io/k6/v2/internal/cloudapi/httperr" ) // Client handles communication with the k6 Cloud API. @@ -35,23 +32,7 @@ func NewClient(logger logrus.FieldLogger, token, host, version string, timeout t return nil, fmt.Errorf("token is required to create cloud API client") } - cfg := &k6cloud.Configuration{ - DefaultHeader: make(map[string]string), - UserAgent: "k6cloud/" + version, - Servers: k6cloud.ServerConfigurations{ - { - URL: host, - Description: "Global k6 Cloud API.", - }, - }, - OperationServers: map[string]k6cloud.ServerConfigurations{}, - HTTPClient: &http.Client{ - Timeout: timeout, - Transport: &bodyResetTransport{base: http.DefaultTransport}, - }, - MaxRetries: MaxRetries, - RetryInterval: RetryInterval, - } + cfg := clientcfg.New(host, version, "Global k6 Cloud API.", timeout) c := &Client{ apiClient: k6cloud.NewAPIClient(cfg), @@ -62,9 +43,15 @@ func NewClient(logger logrus.FieldLogger, token, host, version string, timeout t return c, nil } -// SetStackID sets the stack ID for the client. -func (c *Client) SetStackID(stackID int32) { - c.stackID = stackID +// SetStackID sets the stack ID for the client. It returns an error if +// stackID does not fit in the int32 range the underlying SDK requires for +// the X-Stack-Id header. +func (c *Client) SetStackID(stackID int64) error { + if stackID < math.MinInt32 || stackID > math.MaxInt32 { + return fmt.Errorf("stack ID %d overflows int32", stackID) + } + c.stackID = int32(stackID) + return nil } // BaseURL returns configured host. @@ -72,31 +59,12 @@ func (c *Client) BaseURL() string { return c.baseURL } -// bodyResetTransport resets req.Body from GetBody before each round trip. -// The vendored SDK retries 5xx/429 by re-calling Do on the same request -// without resetting its Body. After Connection: close the drained body -// causes "ContentLength=N with Body length 0". -type bodyResetTransport struct{ base http.RoundTripper } - -func (rt *bodyResetTransport) RoundTrip(req *http.Request) (*http.Response, error) { - if req.GetBody == nil { - return rt.base.RoundTrip(req) - } - body, err := req.GetBody() - if err != nil { - return nil, err - } - req.Body = body - return rt.base.RoundTrip(req) -} - // CheckResponse checks the parsed response. // It returns nil if the code is in the successful range, // otherwise it tries to parse the body and return a parsed error. func CheckResponse(r *http.Response, err error) error { if err != nil { - var aerr *k6cloud.GenericOpenAPIError - if !errors.As(err, &aerr) { + if _, ok := errors.AsType[*k6cloud.GenericOpenAPIError](err); !ok { return err } } @@ -114,21 +82,28 @@ func CheckResponse(r *http.Response, err error) error { return err } + var respErr error var payload ResponseError if err := json.Unmarshal(data, &payload); err != nil { - if r.StatusCode == http.StatusUnauthorized { - return errNotAuthenticated + if classified := httperr.ClassifyStatus(r.StatusCode); classified != nil { + respErr = classified + } else { + respErr = fmt.Errorf( + "unexpected HTTP error from %s: %d %s", + r.Request.URL, + r.StatusCode, + http.StatusText(r.StatusCode), + ) } - if r.StatusCode == http.StatusForbidden { - return errNotAuthorized - } - return fmt.Errorf( - "unexpected HTTP error from %s: %d %s", - r.Request.URL, - r.StatusCode, - http.StatusText(r.StatusCode), - ) + } else { + payload.Response = r + respErr = payload + } + + if r.StatusCode == http.StatusUnauthorized { + return errext.WithHint(respErr, + "Authenticate by running `k6 cloud login` "+ + "or verify the active Grafana Cloud token (K6_CLOUD_TOKEN, options.cloud.token)") } - payload.Response = r - return payload + return respErr } diff --git a/internal/cloudapi/v6/client_test.go b/internal/cloudapi/v6/client_test.go index d007351fd..02ea89586 100644 --- a/internal/cloudapi/v6/client_test.go +++ b/internal/cloudapi/v6/client_test.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "go.k6.io/k6/v2/internal/cloudapi/httperr" ) func TestCheckResponse(t *testing.T) { @@ -43,7 +44,7 @@ func TestCheckResponse(t *testing.T) { StatusCode: http.StatusUnauthorized, Body: io.NopCloser(strings.NewReader("invalid json")), }, - expectedError: errNotAuthenticated.Error(), + expectedError: httperr.ErrNotAuthenticated.Error(), }, { name: "forbidden 403 with invalid JSON", @@ -51,7 +52,7 @@ func TestCheckResponse(t *testing.T) { StatusCode: http.StatusForbidden, Body: io.NopCloser(strings.NewReader("invalid json")), }, - expectedError: errNotAuthorized.Error(), + expectedError: httperr.ErrNotAuthorized.Error(), }, { name: "server error 500 with invalid JSON", diff --git a/internal/cloudapi/v6/config.go b/internal/cloudapi/v6/config.go deleted file mode 100644 index c03259ba0..000000000 --- a/internal/cloudapi/v6/config.go +++ /dev/null @@ -1,262 +0,0 @@ -package cloudapi - -import ( - "bytes" - "encoding/json" - "time" - - "github.com/mstoykov/envconfig" - "gopkg.in/guregu/null.v3" - - "go.k6.io/k6/v2/lib/types" -) - -// Config holds all the necessary data and options for sending metrics to the k6 Cloud. -// -//nolint:lll -type Config struct { - // TODO: refactor common stuff between cloud execution and output - Token null.String `json:"token" envconfig:"K6_CLOUD_TOKEN"` - StackID null.Int `json:"stackID" envconfig:"K6_CLOUD_STACK_ID"` - ProjectID null.Int `json:"projectID" envconfig:"K6_CLOUD_PROJECT_ID"` - Name null.String `json:"name" envconfig:"K6_CLOUD_NAME"` - - Host null.String `json:"host" envconfig:"K6_CLOUD_HOST"` - Timeout types.NullDuration `json:"timeout" envconfig:"K6_CLOUD_TIMEOUT"` - - LogsTailURL null.String `json:"-" envconfig:"K6_CLOUD_LOGS_TAIL_URL"` - WebAppURL null.String `json:"webAppURL" envconfig:"K6_CLOUD_WEB_APP_URL"` - TestRunDetails null.String `json:"testRunDetails" envconfig:"K6_CLOUD_TEST_RUN_DETAILS"` - NoCompress null.Bool `json:"noCompress" envconfig:"K6_CLOUD_NO_COMPRESS"` - StopOnError null.Bool `json:"stopOnError" envconfig:"K6_CLOUD_STOP_ON_ERROR"` - APIVersion null.Int `json:"apiVersion" envconfig:"K6_CLOUD_API_VERSION"` - - // PushRefID is the identifier used by k6 Cloud to correlate all the things that - // belong to the same test run/execution. Currently, it is equivalent to the test run id. - // But, in the future, or in future solutions (e.g. Synthetic Monitoring), there might be - // no test run id, and we may still need an identifier to correlate all the things. - PushRefID null.String `json:"pushRefID" envconfig:"K6_CLOUD_PUSH_REF_ID"` - - // Defines the max allowed number of time series in a single batch. - MaxTimeSeriesInBatch null.Int `json:"maxTimeSeriesInBatch" envconfig:"K6_CLOUD_MAX_TIME_SERIES_IN_BATCH"` - - // The time interval between periodic API calls for sending samples to the cloud ingest service. - MetricPushInterval types.NullDuration `json:"metricPushInterval" envconfig:"K6_CLOUD_METRIC_PUSH_INTERVAL"` - - // This is how many concurrent pushes will be done at the same time to the cloud - MetricPushConcurrency null.Int `json:"metricPushConcurrency" envconfig:"K6_CLOUD_METRIC_PUSH_CONCURRENCY"` - - // If specified and is greater than 0, sample aggregation with that period is enabled - AggregationPeriod types.NullDuration `json:"aggregationPeriod" envconfig:"K6_CLOUD_AGGREGATION_PERIOD"` - - // If aggregation is enabled, this specifies how long we'll wait for period samples to accumulate before trying to aggregate them. - AggregationWaitPeriod types.NullDuration `json:"aggregationWaitPeriod" envconfig:"K6_CLOUD_AGGREGATION_WAIT_PERIOD"` - - // Indicates whether to send traces to the k6 Insights backend service. - TracesEnabled null.Bool `json:"tracesEnabled" envconfig:"K6_CLOUD_TRACES_ENABLED"` - - // The host of the k6 Insights backend service. - TracesHost null.String `json:"traceHost" envconfig:"K6_CLOUD_TRACES_HOST"` - - // This is how many concurrent pushes will be done at the same time to the cloud - TracesPushConcurrency null.Int `json:"tracesPushConcurrency" envconfig:"K6_CLOUD_TRACES_PUSH_CONCURRENCY"` - - // The time interval between periodic API calls for sending samples to the cloud ingest service. - TracesPushInterval types.NullDuration `json:"tracesPushInterval" envconfig:"K6_CLOUD_TRACES_PUSH_INTERVAL"` -} - -// NewConfig creates a new Config instance with default values for some fields. -func NewConfig() Config { - return Config{ - StackID: null.NewInt(0, false), - APIVersion: null.NewInt(2, false), - Host: null.NewString("https://api.k6.io", false), - LogsTailURL: null.NewString("wss://cloudlogs.k6.io/api/v1/tail", false), - WebAppURL: null.NewString("https://app.k6.io", false), - MetricPushInterval: types.NewNullDuration(1*time.Second, false), - MetricPushConcurrency: null.NewInt(1, false), - Timeout: types.NewNullDuration(1*time.Minute, false), - - // The set value (1000) is selected for performance reasons. - // Any change to this value should be first discussed with internal stakeholders. - MaxTimeSeriesInBatch: null.NewInt(1000, false), - - // TODO: the following values were used by the previous default version (v1). - // We decided to keep the same values mostly for having a smoother migration to v2. - // Because the previous version's aggregation config, a few lines below, is overwritten - // by the remote service with the same values that we are now setting here for v2. - // When the migration will be completed we may evaluate to re-discuss them - // as we may evaluate to reduce these values - especially the waiting period. - // A more specific request about waiting period is mentioned in the link below: - // https://github.com/grafana/k6/blob/44e1e63aadb66784ff0a12b8d9821a0fdc9e7467/output/cloud/expv2/collect.go#L72-L77 - AggregationPeriod: types.NewNullDuration(3*time.Second, false), - AggregationWaitPeriod: types.NewNullDuration(8*time.Second, false), - - TracesEnabled: null.NewBool(true, false), - TracesHost: null.NewString("grpc-k6-api-prod-prod-us-east-0.grafana.net:443", false), - TracesPushInterval: types.NewNullDuration(1*time.Second, false), - TracesPushConcurrency: null.NewInt(1, false), - } -} - -// Apply saves config non-zero config values from the passed config in the receiver. -// -//nolint:cyclop -func (c Config) Apply(cfg Config) Config { - if cfg.Token.Valid { - c.Token = cfg.Token - } - if cfg.ProjectID.Valid && cfg.ProjectID.Int64 > 0 { - c.ProjectID = cfg.ProjectID - } - if cfg.Name.Valid && cfg.Name.String != "" { - c.Name = cfg.Name - } - if cfg.Host.Valid && cfg.Host.String != "" { - c.Host = cfg.Host - } - if cfg.LogsTailURL.Valid && cfg.LogsTailURL.String != "" { - c.LogsTailURL = cfg.LogsTailURL - } - if cfg.PushRefID.Valid { - c.PushRefID = cfg.PushRefID - } - if cfg.WebAppURL.Valid { - c.WebAppURL = cfg.WebAppURL - } - if cfg.TestRunDetails.Valid { - c.TestRunDetails = cfg.TestRunDetails - } - if cfg.NoCompress.Valid { - c.NoCompress = cfg.NoCompress - } - if cfg.StopOnError.Valid { - c.StopOnError = cfg.StopOnError - } - if cfg.Timeout.Valid { - c.Timeout = cfg.Timeout - } - if cfg.APIVersion.Valid { - c.APIVersion = cfg.APIVersion - } - if cfg.MaxTimeSeriesInBatch.Valid { - c.MaxTimeSeriesInBatch = cfg.MaxTimeSeriesInBatch - } - if cfg.MetricPushInterval.Valid { - c.MetricPushInterval = cfg.MetricPushInterval - } - if cfg.MetricPushConcurrency.Valid { - c.MetricPushConcurrency = cfg.MetricPushConcurrency - } - if cfg.TracesEnabled.Valid { - c.TracesEnabled = cfg.TracesEnabled - } - if cfg.TracesHost.Valid { - c.TracesHost = cfg.TracesHost - } - if cfg.TracesPushInterval.Valid { - c.TracesPushInterval = cfg.TracesPushInterval - } - if cfg.TracesPushConcurrency.Valid { - c.TracesPushConcurrency = cfg.TracesPushConcurrency - } - if cfg.AggregationPeriod.Valid { - c.AggregationPeriod = cfg.AggregationPeriod - } - if cfg.AggregationWaitPeriod.Valid { - c.AggregationWaitPeriod = cfg.AggregationWaitPeriod - } - return c -} - -// GetConsolidatedConfig combines the default config values with the JSON config -// values and environment variables and returns the final result. -func GetConsolidatedConfig( - jsonRawConf json.RawMessage, - env map[string]string, - configArg string, - cloudConfig json.RawMessage, -) (Config, error) { - result := NewConfig() - if jsonRawConf != nil { - jsonConf := Config{} - if err := json.Unmarshal(jsonRawConf, &jsonConf); err != nil { - return result, err - } - result = result.Apply(jsonConf) - } - - if err := mergeFromCloudOption(cloudConfig, &result); err != nil { - return result, err - } - - envConfig := Config{} - if err := envconfig.Process("", &envConfig, func(key string) (string, bool) { - v, ok := env[key] - return v, ok - }); err != nil { - // TODO: get rid of envconfig and actually use the env parameter... - return result, err - } - result = result.Apply(envConfig) - - if configArg != "" { - result.Name = null.StringFrom(configArg) - } - - return result, nil -} - -// mergeFromCloudOption merges three fields from the JSON in a cloud key of -// the provided external map. Used for options.cloud settings. -func mergeFromCloudOption( - cloudConfig json.RawMessage, - conf *Config, -) error { - if cloudConfig == nil { - return nil - } - - // Original comment - // TODO: Important! Separate configs and fix the whole 2 configs mess! - tmpConfig := Config{} - if err := json.Unmarshal(cloudConfig, &tmpConfig); err != nil { - return err - } - // Only take out the ProjectID, Name and Token from the options.cloud map: - if tmpConfig.ProjectID.Valid { - conf.ProjectID = tmpConfig.ProjectID - } - if tmpConfig.Name.Valid { - conf.Name = tmpConfig.Name - } - if tmpConfig.Token.Valid { - conf.Token = tmpConfig.Token - } - - return nil -} - -// GetTemporaryCloudConfig returns a temporary cloud config. -// Original comment -// TODO: Fix this -// We reuse cloud.Config for parsing options.cloud, but this probably shouldn't be -// done, as the idea of options.ext is that they are extensible without touching k6. But in -// order for this to happen, we shouldn't actually marshal cloud.Config on top of it, because -// it will be missing some fields that aren't actually mentioned in the struct. -// So in order for use to copy the fields that we need for k6 cloud's api we unmarshal in -// map[string]any and copy what we need if it isn't set already -func GetTemporaryCloudConfig(cloudConfig json.RawMessage) (map[string]any, error) { - tmpCloudConfig := make(map[string]any, 3) - if cloudConfig == nil { - return tmpCloudConfig, nil - } - - dec := json.NewDecoder(bytes.NewReader(cloudConfig)) - dec.UseNumber() // otherwise float64 are used - if err := dec.Decode(&tmpCloudConfig); err != nil { - return nil, err - } - - return tmpCloudConfig, nil -} diff --git a/internal/cloudapi/v6/config_test.go b/internal/cloudapi/v6/config_test.go deleted file mode 100644 index 73c2cacf8..000000000 --- a/internal/cloudapi/v6/config_test.go +++ /dev/null @@ -1,93 +0,0 @@ -package cloudapi - -import ( - "encoding/json" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "gopkg.in/guregu/null.v3" - - "go.k6.io/k6/v2/lib/types" -) - -func TestConfigApply(t *testing.T) { - t.Parallel() - empty := Config{} - defaults := NewConfig() - - assert.Equal(t, empty, empty.Apply(empty)) - assert.Equal(t, empty, empty.Apply(defaults)) - assert.Equal(t, defaults, defaults.Apply(defaults)) - assert.Equal(t, defaults, defaults.Apply(empty)) - assert.Equal(t, defaults, defaults.Apply(empty).Apply(empty)) - - full := Config{ - Token: null.NewString("Token", true), - ProjectID: null.NewInt(1, true), - Name: null.NewString("Name", true), - Host: null.NewString("Host", true), - Timeout: types.NewNullDuration(5*time.Second, true), - LogsTailURL: null.NewString("LogsTailURL", true), - PushRefID: null.NewString("PushRefID", true), - WebAppURL: null.NewString("foo", true), - NoCompress: null.NewBool(true, true), - StopOnError: null.NewBool(true, true), - APIVersion: null.NewInt(2, true), - AggregationPeriod: types.NewNullDuration(2*time.Second, true), - AggregationWaitPeriod: types.NewNullDuration(4*time.Second, true), - MaxTimeSeriesInBatch: null.NewInt(3, true), - MetricPushInterval: types.NewNullDuration(1*time.Second, true), - MetricPushConcurrency: null.NewInt(3, true), - TracesEnabled: null.NewBool(true, true), - TracesHost: null.NewString("TracesHost", true), - TracesPushInterval: types.NewNullDuration(10*time.Second, true), - TracesPushConcurrency: null.NewInt(6, true), - } - - assert.Equal(t, full, full.Apply(empty)) - assert.Equal(t, full, full.Apply(defaults)) - assert.Equal(t, full, full.Apply(full)) - assert.Equal(t, full, empty.Apply(full)) - assert.Equal(t, full, defaults.Apply(full)) -} - -func TestGetConsolidatedConfig(t *testing.T) { - t.Parallel() - config, err := GetConsolidatedConfig(json.RawMessage(`{"token":"jsonraw"}`), nil, "", nil) - require.NoError(t, err) - require.Equal(t, "jsonraw", config.Token.String) - - config, err = GetConsolidatedConfig( - json.RawMessage(`{"token":"jsonraw"}`), - nil, - "", - json.RawMessage(`{"token":"ext"}`), - ) - require.NoError(t, err) - require.Equal(t, "ext", config.Token.String) - - config, err = GetConsolidatedConfig( - json.RawMessage(`{"token":"jsonraw"}`), - map[string]string{"K6_CLOUD_TOKEN": "envvalue"}, - "", - json.RawMessage(`{"token":"ext"}`), - ) - require.NoError(t, err) - require.Equal(t, "envvalue", config.Token.String) -} - -func TestGetConsolidatedConfig_EnvHasHigherPriority(t *testing.T) { - t.Parallel() - - config, err := GetConsolidatedConfig( - json.RawMessage(`{"token":"jsonraw"}`), - map[string]string{"K6_CLOUD_TOKEN": "envvalue"}, - "", - json.RawMessage(`{"token":"cloud"}`), - ) - require.NoError(t, err) - - require.Equal(t, "envvalue", config.Token.String) -} diff --git a/internal/cloudapi/v6/errors.go b/internal/cloudapi/v6/errors.go index a378c32bf..5cfbd361c 100644 --- a/internal/cloudapi/v6/errors.go +++ b/internal/cloudapi/v6/errors.go @@ -10,10 +10,8 @@ import ( ) var ( - errNotAuthorized = errors.New("not allowed to upload result to k6 Cloud") - errNotAuthenticated = errors.New("failed to authenticate with k6 Cloud") - errTestNotExists = errors.New("load test not found") - errUnknown = errors.New("an error occurred communicating with k6 Cloud") + errTestNotExists = errors.New("load test not found") + errUnknown = errors.New("an error occurred communicating with k6 Cloud") ) // ResponseError represents an error cause by talking to the API diff --git a/internal/cloudapi/v6/errors_test.go b/internal/cloudapi/v6/errors_test.go index a9c966ec7..1e2b20429 100644 --- a/internal/cloudapi/v6/errors_test.go +++ b/internal/cloudapi/v6/errors_test.go @@ -31,7 +31,7 @@ func TestResponseError_Error(t *testing.T) { respErr: ResponseError{ APIError: k6cloud.ErrorApiModel{ Message: "validation failed", - Target: *k6cloud.NewNullableString(k6cloud.PtrString("field_name")), + Target: *k6cloud.NewNullableString(new("field_name")), }, }, expected: "validation failed (target: 'field_name')", @@ -44,11 +44,11 @@ func TestResponseError_Error(t *testing.T) { Details: []k6cloud.ErrorDetailsApiModel{ { Message: "field is required", - Target: *k6cloud.NewNullableString(k6cloud.PtrString("first_property")), + Target: *k6cloud.NewNullableString(new("first_property")), }, { Message: "field must be positive", - Target: *k6cloud.NewNullableString(k6cloud.PtrString("second_property")), + Target: *k6cloud.NewNullableString(new("second_property")), }, }, }, diff --git a/internal/cloudapi/v6/v6test/server.go b/internal/cloudapi/v6/v6test/server.go index 724adb813..60c056bdf 100644 --- a/internal/cloudapi/v6/v6test/server.go +++ b/internal/cloudapi/v6/v6test/server.go @@ -23,8 +23,8 @@ import ( ) const ( - defaultLoadTestID int32 = 456 - defaultTestRunID int32 = 123 + defaultLoadTestID int64 = 456 + defaultTestRunID int64 = 123 ) // Server is a test HTTP server for the v6 cloud API. @@ -39,7 +39,7 @@ type Server struct { type Config struct { // TestRunID is the id reported by the start-test response and used // in the test-run routes. Defaults to [defaultTestRunID] when zero. - TestRunID int32 + TestRunID int64 // ProgressCallback returns the [cloudapi.TestProgress] reported by // each test fetch. When nil, a finished passing run is reported. @@ -55,6 +55,15 @@ type Config struct { // LoadTests is the load test list returned by the load-tests endpoint. LoadTests []cloudapi.LoadTest + + // LoadZones is the load zone list returned by the load-zones endpoint. + LoadZones []cloudapi.LoadZone + + // ErrorStatus, when non-zero, makes every request fail with this HTTP + // status and the real k6 Cloud API's error body for it, instead of + // the endpoint's normal canned response. Requires the value to be present + // in the forcedErrorMessages map or will panic + ErrorStatus int } // NewServer creates a test server that serves v6 API endpoints. @@ -70,6 +79,7 @@ func NewServer(t *testing.T, cfg Config) *Server { mux := http.NewServeMux() mux.HandleFunc("GET /cloud/v6/projects", s.handleListProjects) mux.HandleFunc("GET /cloud/v6/projects/{projectID}/load_tests", s.handleListLoadTests) + mux.HandleFunc("GET /cloud/v6/load_zones", s.handleListLoadZones) mux.HandleFunc("POST /cloud/v6/validate_options", s.handleValidateOptions) mux.HandleFunc("POST /cloud/v6/projects/{projectID}/load_tests", func(w http.ResponseWriter, r *http.Request) { if s.cfg.InspectArchive != nil { @@ -90,13 +100,36 @@ func NewServer(t *testing.T, cfg Config) *Server { s.handleAbortTestRun, ) - srv := httptest.NewServer(mux) + var handler http.Handler = mux + if cfg.ErrorStatus != 0 { + handler = forcedErrorHandler(cfg.ErrorStatus) + } + + srv := httptest.NewServer(handler) s.Server = srv t.Cleanup(srv.Close) return s } +// forcedErrorHandler short-circuits every request with the given status and +// its real error body, regardless of route. +func forcedErrorHandler(status int) http.Handler { + forcedErrorMessages := map[int]string{ + http.StatusUnauthorized: "Invalid token", + } + + msg, ok := forcedErrorMessages[status] + if !ok { + panic(fmt.Sprintf("v6test: no error body registered for status %d", status)) + } + + return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewErrorResponseApiModel(*k6cloud.NewErrorApiModel(msg, "error")) + writeJSON(w, status, res) + }) +} + func (s *Server) handleListProjects(w http.ResponseWriter, _ *http.Request) { projects := make([]k6cloud.ProjectApiModel, len(s.cfg.Projects)) now := time.Unix(0, 0).UTC() @@ -115,7 +148,7 @@ func (s *Server) handleListProjects(w http.ResponseWriter, _ *http.Request) { } func (s *Server) handleListLoadTests(w http.ResponseWriter, r *http.Request) { - projectID, err := strconv.ParseInt(r.PathValue("projectID"), 10, 32) + projectID, err := strconv.ParseInt(r.PathValue("projectID"), 10, 64) if err != nil { http.Error(w, "invalid project ID", http.StatusBadRequest) return @@ -123,14 +156,14 @@ func (s *Server) handleListLoadTests(w http.ResponseWriter, r *http.Request) { tests := make([]k6cloud.LoadTestApiModel, 0, len(s.cfg.LoadTests)) for _, test := range s.cfg.LoadTests { - if test.ProjectID != int32(projectID) { + if test.ProjectID != projectID { continue } tests = append(tests, *k6cloud.NewLoadTestApiModel( test.ID, test.ProjectID, test.Name, - *k6cloud.NewNullableInt32(nil), + *k6cloud.NewNullableInt64(nil), test.Created, test.Updated, )) @@ -139,6 +172,19 @@ func (s *Server) handleListLoadTests(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, res) } +func (s *Server) handleListLoadZones(w http.ResponseWriter, _ *http.Request) { + zones := make([]k6cloud.LoadZoneApiModel, len(s.cfg.LoadZones)) + for i, zone := range s.cfg.LoadZones { + m := k6cloud.NewLoadZoneApiModel( + zone.ID, zone.Name, zone.K6LoadZoneID, + zone.Available, *k6cloud.NewNullableString(nil), zone.Public, + ) + zones[i] = *m + } + res := k6cloud.NewLoadZonesListApiModel(zones) + writeJSON(w, http.StatusOK, res) +} + func (s *Server) handleValidateOptions(w http.ResponseWriter, _ *http.Request) { vuh, zero := float32(0.5), float32(0) res := k6cloud.NewValidateOptionsResponse( @@ -166,7 +212,7 @@ func (s *Server) handleStartTest(w http.ResponseWriter, _ *http.Request) { res.SetTestRunDetailsPageUrl(fmt.Sprintf("https://stack.grafana.com/a/k6-app/runs/%d", s.cfg.TestRunID)) // SDK decoder requires these keys on the other end even when empty. res.SetDistribution([]k6cloud.DistributionZoneApiModel{}) - res.SetResultDetails(map[string]any{}) + res.SetResultDetails(*k6cloud.NewResultDetailsApiModel("")) res.SetOptions(map[string]any{}) writeJSON(w, http.StatusOK, res) } @@ -190,7 +236,7 @@ func (s *Server) handleGetTestRun(w http.ResponseWriter, _ *http.Request) { res.SetStatusHistory(cloudapi.ToStatusModel(tp.StatusHistory)) // SDK decoder requires these keys on the other end even when empty. res.SetDistribution([]k6cloud.DistributionZoneApiModel{}) - res.SetResultDetails(map[string]any{}) + res.SetResultDetails(k6cloud.ResultDetailsApiModelAsTestRunApiModelResultDetails(k6cloud.NewResultDetailsApiModel(""))) res.SetOptions(map[string]any{}) writeJSON(w, http.StatusOK, res) } diff --git a/internal/cmd/archive_test.go b/internal/cmd/archive_test.go index 31410f23d..01ed78079 100644 --- a/internal/cmd/archive_test.go +++ b/internal/cmd/archive_test.go @@ -183,7 +183,8 @@ func TestArchiveContainsEnv(t *testing.T) { // then unpacked metadata should contain the environment variables with the proper values require.NoError(t, json.Unmarshal(data, &metadata)) - require.Len(t, metadata.Env, 2) + require.Len(t, metadata.Env, 3) + require.Equal(t, "async-metric-context", metadata.Env["K6_FEATURES"]) require.Contains(t, metadata.Env, "ENV1") require.Contains(t, metadata.Env, "ENV2") diff --git a/internal/cmd/build_full_test.go b/internal/cmd/build_full_test.go new file mode 100644 index 000000000..0a5ef2297 --- /dev/null +++ b/internal/cmd/build_full_test.go @@ -0,0 +1,5 @@ +//go:build !lean + +package cmd + +const leanBuild = false diff --git a/internal/cmd/build_lean_test.go b/internal/cmd/build_lean_test.go new file mode 100644 index 000000000..3be456480 --- /dev/null +++ b/internal/cmd/build_lean_test.go @@ -0,0 +1,5 @@ +//go:build lean + +package cmd + +const leanBuild = true diff --git a/internal/cmd/cloud.go b/internal/cmd/cloud.go index cf781bd33..66e9ce7ef 100644 --- a/internal/cmd/cloud.go +++ b/internal/cmd/cloud.go @@ -7,7 +7,6 @@ import ( "encoding/json" "errors" "fmt" - "math" "net/url" "os" "path/filepath" @@ -66,48 +65,126 @@ func checkCloudLoginFor(conf cloudapi.Config, prefix string) error { return nil } -// cmdCloud handles the `k6 cloud` sub-command -type cmdCloud struct { - gs *state.GlobalState +// newCloudV6ClientFromConfig builds a v6 cloud API client from the on-disk and +// environment configuration. It verifies that a complete login is configured +// (using authPrefix in the error message) and wires the resolved stack ID into +// the client. It returns the client alongside the consolidated cloud config. +func newCloudV6ClientFromConfig( + gs *state.GlobalState, authPrefix string, +) (*cloudapiv6.Client, cloudapi.Config, error) { + currentDiskConf, err := readDiskConfig(gs) + if err != nil { + return nil, cloudapi.Config{}, err + } - showCloudLogs bool - exitOnRunning bool - uploadOnly bool + cloudConfig, warn, err := cloudapi.GetConsolidatedConfig( + currentDiskConf.Collectors["cloud"], gs.Env, "", nil) + if err != nil { + return nil, cloudapi.Config{}, err + } + if warn != "" { + gs.Logger.Warn(warn) + } + + if err := checkCloudLoginFor(cloudConfig, authPrefix); err != nil { + return nil, cloudapi.Config{}, err + } + + client, err := cloudapiv6.NewClient( + gs.Logger, + cloudConfig.Token.String, + cloudConfig.Hostv6.String, + build.Version, + cloudConfig.Timeout.TimeDuration(), + ) + if err != nil { + return nil, cloudapi.Config{}, err + } + + if err := client.SetStackID(cloudConfig.StackID.Int64); err != nil { + return nil, cloudapi.Config{}, err + } + + return client, cloudConfig, nil +} + +// cloudStackName returns a human-readable name for the configured stack, +// falling back to "stack-" when the stack URL is not available. +func cloudStackName(conf cloudapi.Config) string { + if conf.StackURL.Valid { + return conf.StackURL.String + } + return fmt.Sprintf("stack-%d", conf.StackID.Int64) } -func (c *cmdCloud) preRun(cmd *cobra.Command, _ []string) error { - // TODO: refactor (https://github.com/grafana/k6/issues/883) - // - // We deliberately parse the env variables, to validate for wrong - // values, even if we don't subsequently use them (if the respective - // CLI flag was specified, since it has a higher priority). - if showCloudLogsEnv, ok := c.gs.Env["K6_SHOW_CLOUD_LOGS"]; ok { +// parseCloudEnvOverrides parses and validates the K6_SHOW_CLOUD_LOGS and +// K6_EXIT_ON_RUNNING environment variables. A nil result means the +// corresponding variable is not set. +// +// It is also used on its own to validate the variables for wrong values by +// commands that don't subsequently use them. +func parseCloudEnvOverrides(env map[string]string) (showCloudLogs, exitOnRunning *bool, err error) { + if showCloudLogsEnv, ok := env["K6_SHOW_CLOUD_LOGS"]; ok { showCloudLogsValue, err := strconv.ParseBool(showCloudLogsEnv) if err != nil { - return fmt.Errorf("parsing K6_SHOW_CLOUD_LOGS returned an error: %w", err) - } - if !cmd.Flags().Changed("show-logs") { - c.showCloudLogs = showCloudLogsValue + return nil, nil, fmt.Errorf("parsing K6_SHOW_CLOUD_LOGS returned an error: %w", err) } + showCloudLogs = &showCloudLogsValue } - if exitOnRunningEnv, ok := c.gs.Env["K6_EXIT_ON_RUNNING"]; ok { + if exitOnRunningEnv, ok := env["K6_EXIT_ON_RUNNING"]; ok { exitOnRunningValue, err := strconv.ParseBool(exitOnRunningEnv) if err != nil { - return fmt.Errorf("parsing K6_EXIT_ON_RUNNING returned an error: %w", err) - } - if !cmd.Flags().Changed("exit-on-running") { - c.exitOnRunning = exitOnRunningValue + return nil, nil, fmt.Errorf("parsing K6_EXIT_ON_RUNNING returned an error: %w", err) } + exitOnRunning = &exitOnRunningValue + } + return showCloudLogs, exitOnRunning, nil +} + +// applyCloudEnvOverrides applies the K6_SHOW_CLOUD_LOGS and K6_EXIT_ON_RUNNING +// environment variables onto the provided flag values, unless the corresponding +// CLI flag was explicitly set (in which case it takes precedence). +// +// TODO: refactor (https://github.com/grafana/k6/issues/883) +func applyCloudEnvOverrides(gs *state.GlobalState, cmd *cobra.Command, showCloudLogs, exitOnRunning *bool) error { + envShowCloudLogs, envExitOnRunning, err := parseCloudEnvOverrides(gs.Env) + if err != nil { + return err + } + + if envShowCloudLogs != nil && !cmd.Flags().Changed("show-logs") { + *showCloudLogs = *envShowCloudLogs + } + if envExitOnRunning != nil && !cmd.Flags().Changed("exit-on-running") { + *exitOnRunning = *envExitOnRunning } return nil } +// cloudTestRunOptions holds the flag values that control how runCloudTest +// behaves. +type cloudTestRunOptions struct { + // showCloudLogs streams the cloud logs to the terminal while the test runs. + showCloudLogs bool + + // exitOnRunning makes the command return as soon as the test reaches the + // running status, instead of waiting for it to finish. + exitOnRunning bool + + // uploadOnly stops after uploading the test archive, without starting a + // test run. + uploadOnly bool +} + +// runCloudTest builds the test archive, uploads it to Grafana Cloud, and (unless +// opts.uploadOnly is set) starts the run and tracks its progress until completion. +// // TODO: split apart some more // //nolint:funlen,gocognit,cyclop -func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { - test, err := loadAndConfigureLocalTest(c.gs, cmd, args, getPartialConfig) +func runCloudTest(gs *state.GlobalState, cmd *cobra.Command, args []string, opts cloudTestRunOptions) error { + test, err := loadAndConfigureLocalTest(gs, cmd, args, getPartialConfig) if err != nil { return err } @@ -124,15 +201,15 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { // TODO: validate for usage of execution segment // TODO: validate for externally controlled executor (i.e. executors that aren't distributable) // TODO: move those validations to a separate function and reuse validateConfig()? - printBanner(c.gs) + printBanner(gs) progressBar := pb.New( pb.WithConstLeft("Init"), pb.WithConstProgress(0, "Loading test script..."), ) - printBar(c.gs, progressBar) + printBar(gs, progressBar) - modifyAndPrintBar(c.gs, progressBar, pb.WithConstProgress(0, "Building the archive...")) + modifyAndPrintBar(gs, progressBar, pb.WithConstProgress(0, "Building the archive...")) arc := test.makeArchive() tmpCloudConfig, err := cloudapi.GetTemporaryCloudConfig(arc.Options.Cloud) @@ -142,7 +219,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { // Cloud config cloudConfig, warn, err := cloudapi.GetConsolidatedConfig( - test.derivedConfig.Collectors["cloud"], c.gs.Env, "", arc.Options.Cloud) + test.derivedConfig.Collectors["cloud"], gs.Env, "", arc.Options.Cloud) if err != nil { return err } @@ -152,7 +229,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { // Display config warning if needed if warn != "" { - modifyAndPrintBar(c.gs, progressBar, pb.WithConstProgress(0, "Warning: "+warn)) + modifyAndPrintBar(gs, progressBar, pb.WithConstProgress(0, "Warning: "+warn)) } if cloudConfig.Token.Valid { @@ -181,31 +258,31 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { name = filepath.Base(test.sourceRootPath) } - globalCtx, globalCancel := context.WithCancel(c.gs.Ctx) + globalCtx, globalCancel := context.WithCancel(gs.Ctx) defer globalCancel() - logger := c.gs.Logger + logger := gs.Logger // Start cloud test run - modifyAndPrintBar(c.gs, progressBar, pb.WithConstProgress(0, "Validating script options")) + modifyAndPrintBar(gs, progressBar, pb.WithConstProgress(0, "Validating script options")) client, err := cloudapiv6.NewClient( logger, cloudConfig.Token.String, cloudConfig.Hostv6.String, build.Version, cloudConfig.Timeout.TimeDuration()) if err != nil { return err } - projectID, err := prepCloudTestRun(globalCtx, c.gs, client, &cloudConfig, tmpCloudConfig, arc) + projectID, err := prepCloudTestRun(globalCtx, gs, client, &cloudConfig, tmpCloudConfig, arc) if err != nil { return err } - modifyAndPrintBar(c.gs, progressBar, pb.WithConstProgress(0, "Uploading archive")) + modifyAndPrintBar(gs, progressBar, pb.WithConstProgress(0, "Uploading archive")) loadTest, err := client.UploadTest(globalCtx, name, projectID, arc) if err != nil { return fmt.Errorf("uploading test: %w", err) } - if c.uploadOnly { + if opts.uploadOnly { et, err := lib.NewExecutionTuple(test.derivedConfig.ExecutionSegment, test.derivedConfig.ExecutionSegmentSequence) if err != nil { return err @@ -216,10 +293,10 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { return err } printExecutionDescription( - c.gs, "cloud", test.sourceRootPath, testURL, test.derivedConfig, et, executionPlan, nil, + gs, "cloud", test.sourceRootPath, testURL, test.derivedConfig, et, executionPlan, nil, ) - modifyAndPrintBar(c.gs, progressBar, pb.WithConstLeft("Run "), pb.WithConstProgress(1.0, "Archived")) - c.printTestStatus("Archived") + modifyAndPrintBar(gs, progressBar, pb.WithConstLeft("Run "), pb.WithConstProgress(1.0, "Archived")) + printCloudTestStatus(gs, "Archived") return nil } @@ -247,7 +324,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { onHardStop := func(sig os.Signal) { logger.WithField("sig", sig).Error("Aborting k6 in response to signal, we won't wait for the test to end.") } - stopSignalHandling := handleTestAbortSignals(c.gs, gracefulStop, onHardStop) + stopSignalHandling := handleTestAbortSignals(gs, gracefulStop, onHardStop) defer stopSignalHandling() et, err := lib.NewExecutionTuple(test.derivedConfig.ExecutionSegment, test.derivedConfig.ExecutionSegmentSequence) @@ -257,11 +334,11 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { testURL := run.GetTestRunDetailsPageUrl() executionPlan := test.derivedConfig.Scenarios.GetFullExecutionRequirements(et) printExecutionDescription( - c.gs, "cloud", test.sourceRootPath, testURL, test.derivedConfig, et, executionPlan, nil, + gs, "cloud", test.sourceRootPath, testURL, test.derivedConfig, et, executionPlan, nil, ) modifyAndPrintBar( - c.gs, progressBar, + gs, progressBar, pb.WithConstLeft("Run "), pb.WithConstProgress(0, "Initializing the cloud test"), ) @@ -271,7 +348,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { defer progressBarWG.Wait() defer progressCancel() go func() { - showProgress(progressCtx, c.gs, []*pb.ProgressBar{progressBar}, logger) + showProgress(progressCtx, gs, []*pb.ProgressBar{progressBar}, logger) progressBarWG.Done() }() @@ -296,8 +373,8 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { ) ticker := time.NewTicker(time.Millisecond * 2000) - if c.showCloudLogs { - refID := strconv.FormatInt(int64(testRunID), 10) + if opts.showCloudLogs { + refID := strconv.FormatInt(testRunID, 10) go func() { logger.Debug("Connecting to cloud logs server...") if err := cloudConfig.StreamLogsToLogger(globalCtx, logger, refID, 0); err != nil { @@ -318,7 +395,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { testProgressLock.Unlock() if newTestProgress.IsFinished() || - (c.exitOnRunning && newTestProgress.IsRunning()) { + (opts.exitOnRunning && newTestProgress.IsRunning()) { globalCancel() break } @@ -333,7 +410,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { return errext.WithExitCodeIfNone(errors.New("Test progress error"), exitcodes.CloudFailedToGetProgress) } - c.printTestStatus(testProgress.FormatStatus()) + printCloudTestStatus(gs, testProgress.FormatStatus()) if testProgress.ThresholdsFailed() { //nolint:staticcheck @@ -347,27 +424,29 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error { return nil } -func (c *cmdCloud) printTestStatus(status string) { - if !c.gs.Flags.Quiet { - valueColor := getColor(c.gs.Flags.NoColor || !c.gs.Stdout.IsTTY, color.FgCyan) - printToStdout(c.gs, fmt.Sprintf( +func printCloudTestStatus(gs *state.GlobalState, status string) { + if !gs.Flags.Quiet { + valueColor := getColor(gs.Flags.NoColor || !gs.Stdout.IsTTY, color.FgCyan) + printToStdout(gs, fmt.Sprintf( " test status: %s\n", valueColor.Sprint(status), )) } else { - c.gs.Logger.WithField("run_status", status).Debug("Test finished") + gs.Logger.WithField("run_status", status).Debug("Test finished") } } -func (c *cmdCloud) flagSet() *pflag.FlagSet { +// cloudCmdFlagSet returns the flag set used by the cloud run command, +// binding the --exit-on-running and --show-logs flags to the provided values. +func cloudCmdFlagSet(showCloudLogs, exitOnRunning *bool) *pflag.FlagSet { flags := pflag.NewFlagSet("", pflag.ContinueOnError) flags.SortFlags = false flags.AddFlagSet(optionFlagSet()) flags.AddFlagSet(runtimeOptionFlagSet(false)) // TODO: Figure out a better way to handle the CLI flags - flags.BoolVar(&c.exitOnRunning, "exit-on-running", c.exitOnRunning, + flags.BoolVar(exitOnRunning, "exit-on-running", *exitOnRunning, "exits when test reaches the running status") - flags.BoolVar(&c.showCloudLogs, "show-logs", c.showCloudLogs, + flags.BoolVar(showCloudLogs, "show-logs", *showCloudLogs, "enable showing of logs when a test is executed in the cloud") return flags } @@ -393,12 +472,6 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e } func getCmdCloud(gs *state.GlobalState) *cobra.Command { - c := &cmdCloud{ - gs: gs, - showCloudLogs: true, - exitOnRunning: false, - } - exampleText := getExampleText(gs, ` # Authenticate with Grafana Cloud $ {{.}} cloud login @@ -426,7 +499,7 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command { defaultUsageTemplate := (&cobra.Command{}).UsageTemplate() defaultUsageTemplate = strings.ReplaceAll(defaultUsageTemplate, "FlagUsages", "FlagUsagesWrapped 120") - runCmd := getCmdCloudRun(c) + runCmd := getCmdCloudRun(gs) runCmd.SetUsageTemplate(defaultUsageTemplate) runCmd.SetHelpTemplate((&cobra.Command{}).HelpTemplate()) cloudCmd.AddCommand(runCmd) @@ -436,19 +509,19 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command { loginCmd.SetHelpTemplate((&cobra.Command{}).HelpTemplate()) cloudCmd.AddCommand(loginCmd) - uploadCmd := getCmdCloudUpload(c) + uploadCmd := getCmdCloudUpload(gs) uploadCmd.SetUsageTemplate(defaultUsageTemplate) uploadCmd.SetHelpTemplate((&cobra.Command{}).HelpTemplate()) cloudCmd.AddCommand(uploadCmd) - projectCmd := getCmdCloudProject(c) + projectCmd := getCmdCloudProject(gs) cloudCmd.AddCommand(projectCmd) - testCmd := getCmdCloudTest(c) - cloudCmd.AddCommand(testCmd) + loadZoneCmd := getCmdCloudLoadZone(gs) + cloudCmd.AddCommand(loadZoneCmd) - cloudCmd.Flags().SortFlags = false - cloudCmd.Flags().AddFlagSet(c.flagSet()) + testCmd := getCmdCloudTest(gs) + cloudCmd.AddCommand(testCmd) // Use custom template similar to root - hardcode flags to avoid showing global flags cloudTemplate := getCloudUsageTemplate() @@ -465,33 +538,18 @@ func prepCloudTestRun( ctx context.Context, gs *state.GlobalState, client *cloudapiv6.Client, cloudConfig *cloudapi.Config, tmpCloudConfig map[string]any, arc *lib.Archive, -) (int32, error) { - toInt32 := func(v int64) (int32, error) { - if v < math.MinInt32 || v > math.MaxInt32 { - return 0, fmt.Errorf("value %d overflows int32", v) - } - return int32(v), nil - } - - stackID, err := toInt32(cloudConfig.StackID.Int64) - if err != nil { +) (int64, error) { + if err := client.SetStackID(cloudConfig.StackID.Int64); err != nil { return 0, err } - client.SetStackID(stackID) - projectID, err := toInt32(cloudConfig.ProjectID.Int64) - if err != nil { - return 0, err - } + projectID := cloudConfig.ProjectID.Int64 if projectID == 0 { if err := resolveAndSetProjectID(gs, cloudConfig, tmpCloudConfig, arc); err != nil { return 0, err } - projectID, err = toInt32(cloudConfig.ProjectID.Int64) - if err != nil { - return 0, err - } + projectID = cloudConfig.ProjectID.Int64 } if err := client.ValidateOptions(ctx, projectID, arc.Options); err != nil { @@ -552,11 +610,11 @@ func resolveAndSetProjectID( return nil } -func resolveCloudTestURL(stackURL string, testID int32) (string, error) { +func resolveCloudTestURL(stackURL string, testID int64) (string, error) { u, err := url.Parse(stackURL) if err != nil { return "", fmt.Errorf("invalid stack URL: %w", err) } - return u.JoinPath("a", "k6-app", "tests", strconv.FormatInt(int64(testID), 10)).String(), nil + return u.JoinPath("a", "k6-app", "tests", strconv.FormatInt(testID, 10)).String(), nil } diff --git a/internal/cmd/cloud_load_zone.go b/internal/cmd/cloud_load_zone.go new file mode 100644 index 000000000..3e28ac591 --- /dev/null +++ b/internal/cmd/cloud_load_zone.go @@ -0,0 +1,44 @@ +//go:build !lean + +package cmd + +import ( + "github.com/spf13/cobra" + "go.k6.io/k6/v2/cmd/state" +) + +type cmdCloudLoadZone struct { + globalState *state.GlobalState +} + +func getCmdCloudLoadZone(gs *state.GlobalState) *cobra.Command { + c := &cmdCloudLoadZone{ + globalState: gs, + } + + exampleText := getExampleText(gs, ` + # List all load zones in the configured stack + $ {{.}} cloud load-zone list + + # List load zones in JSON format + $ {{.}} cloud load-zone list --json`[1:]) + + cloudLoadZoneCommand := &cobra.Command{ + Use: "load-zone", + Short: "Work with Grafana Cloud k6 load zones", + Long: `Work with Grafana Cloud k6 load zones.`, + + Example: exampleText, + } + + cloudUsageTemplate := getCloudUsageTemplate() + + listCmd := getCmdCloudLoadZoneList(c) + listCmd.SetUsageTemplate(cloudUsageTemplate) + listCmd.SetHelpTemplate(cloudUsageTemplate) + cloudLoadZoneCommand.AddCommand(listCmd) + + cloudLoadZoneCommand.SetUsageTemplate(cloudUsageTemplate) + + return cloudLoadZoneCommand +} diff --git a/internal/cmd/cloud_load_zone_list.go b/internal/cmd/cloud_load_zone_list.go new file mode 100644 index 000000000..ec6f000fe --- /dev/null +++ b/internal/cmd/cloud_load_zone_list.go @@ -0,0 +1,103 @@ +//go:build !lean + +package cmd + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" + "text/tabwriter" + + "github.com/spf13/cobra" + + "go.k6.io/k6/v2/cmd/state" + cloudapiv6 "go.k6.io/k6/v2/internal/cloudapi/v6" +) + +type cmdCloudLoadZoneList struct { + globalState *state.GlobalState + isJSON bool +} + +func getCmdCloudLoadZoneList(loadZoneCmd *cmdCloudLoadZone) *cobra.Command { + c := &cmdCloudLoadZoneList{ + globalState: loadZoneCmd.globalState, + } + + exampleText := getExampleText(loadZoneCmd.globalState, ` + # List all load zones in the configured stack + $ {{.}} cloud load-zone list`[1:]) + + listCmd := &cobra.Command{ + Use: "list", + Short: "List Grafana Cloud k6 load zones", + Long: `List all load zones in the configured Grafana Cloud k6 stack.`, + Example: exampleText, + Args: cobra.NoArgs, + RunE: c.run, + } + + listCmd.Flags().BoolVar(&c.isJSON, "json", false, "output load zone list in JSON format") + + return listCmd +} + +func (c *cmdCloudLoadZoneList) run(_ *cobra.Command, _ []string) error { + client, cloudConfig, err := newCloudV6ClientFromConfig( + c.globalState, "Listing cloud load zones requires auth settings") + if err != nil { + return err + } + + loadZones, err := client.ListLoadZones(c.globalState.Ctx) + if err != nil { + return err + } + + if c.isJSON { + return c.outputJSON(loadZones) + } + + stackHeader := fmt.Sprintf("Load zones for %s:\n\n", cloudStackName(cloudConfig)) + + if len(loadZones) == 0 { + printToStdout(c.globalState, stackHeader+"No load zones found.\n") + return nil + } + + printToStdout(c.globalState, stackHeader+formatLoadZoneTable(loadZones)) + return nil +} + +func (c *cmdCloudLoadZoneList) outputJSON(loadZones []cloudapiv6.LoadZone) error { + buf := &bytes.Buffer{} + enc := json.NewEncoder(buf) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + if err := enc.Encode(loadZones); err != nil { + return fmt.Errorf("failed to encode load zone list: %w", err) + } + + printToStdout(c.globalState, buf.String()) + return nil +} + +func formatLoadZoneTable(loadZones []cloudapiv6.LoadZone) string { + var buf strings.Builder + w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0) + _, _ = fmt.Fprintln(w, "ID\tNAME\tTYPE\tAVAILABLE") + for _, z := range loadZones { + zoneType := "private" + if z.Public { + zoneType = "public" + } + available := "no" + if z.Available { + available = "yes" + } + _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", z.K6LoadZoneID, z.Name, zoneType, available) + } + _ = w.Flush() + return buf.String() +} diff --git a/internal/cmd/cloud_loadtest.go b/internal/cmd/cloud_loadtest.go index 812f8f270..d0bc8d3e4 100644 --- a/internal/cmd/cloud_loadtest.go +++ b/internal/cmd/cloud_loadtest.go @@ -11,12 +11,12 @@ type cmdCloudTest struct { globalState *state.GlobalState } -func getCmdCloudTest(cloudCmd *cmdCloud) *cobra.Command { +func getCmdCloudTest(gs *state.GlobalState) *cobra.Command { c := &cmdCloudTest{ - globalState: cloudCmd.gs, + globalState: gs, } - exampleText := getExampleText(cloudCmd.gs, ` + exampleText := getExampleText(gs, ` # List tests in the default project $ {{.}} cloud test list diff --git a/internal/cmd/cloud_loadtest_list.go b/internal/cmd/cloud_loadtest_list.go index 906afc0c1..c8b0588da 100644 --- a/internal/cmd/cloud_loadtest_list.go +++ b/internal/cmd/cloud_loadtest_list.go @@ -6,7 +6,6 @@ import ( "bytes" "encoding/json" "fmt" - "math" "strings" "text/tabwriter" @@ -15,7 +14,6 @@ import ( "go.k6.io/k6/v2/cloudapi" "go.k6.io/k6/v2/cmd/state" - "go.k6.io/k6/v2/internal/build" v6cloudapi "go.k6.io/k6/v2/internal/cloudapi/v6" ) @@ -54,47 +52,16 @@ func getCmdCloudTestList(testCmd *cmdCloudTest) *cobra.Command { } func (c *cmdCloudTestList) run(cmd *cobra.Command, _ []string) error { - currentDiskConf, err := readDiskConfig(c.globalState) + client, cloudConfig, err := newCloudV6ClientFromConfig( + c.globalState, "Listing cloud tests requires auth settings") if err != nil { return err } - currentJSONConfigRaw := currentDiskConf.Collectors["cloud"] - - cloudConfig, warn, err := cloudapi.GetConsolidatedConfig( - currentJSONConfigRaw, c.globalState.Env, "", nil) - if err != nil { - return err - } - if warn != "" { - c.globalState.Logger.Warn(warn) - } - - if err := checkCloudLoginFor(cloudConfig, "Listing cloud tests requires auth settings"); err != nil { - return err - } - projectID, err := c.resolveProjectID(cloudConfig, cmd.Flags().Changed("project-id")) if err != nil { return err } - - client, err := v6cloudapi.NewClient( - c.globalState.Logger, - cloudConfig.Token.String, - cloudConfig.Hostv6.String, - build.Version, - cloudConfig.Timeout.TimeDuration(), - ) - if err != nil { - return err - } - - if cloudConfig.StackID.Int64 < math.MinInt32 || cloudConfig.StackID.Int64 > math.MaxInt32 { - return fmt.Errorf("stack ID %d overflows int32", cloudConfig.StackID.Int64) - } - client.SetStackID(int32(cloudConfig.StackID.Int64)) - tests, err := client.ListLoadTests(c.globalState.Ctx, projectID) if err != nil { return err @@ -123,7 +90,7 @@ func (c *cmdCloudTestList) run(cmd *cobra.Command, _ []string) error { // shared resolveDefaultProjectID helper (config projectID, then the stack's // default project). Unlike `k6 cloud run`, listing needs a concrete project, so // a zero result is treated as an error. -func (c *cmdCloudTestList) resolveProjectID(cloudConfig cloudapi.Config, projectIDSet bool) (int32, error) { +func (c *cmdCloudTestList) resolveProjectID(cloudConfig cloudapi.Config, projectIDSet bool) (int64, error) { if projectIDSet { if c.projectID <= 0 { return 0, errNoProjectConfigured @@ -139,11 +106,7 @@ func (c *cmdCloudTestList) resolveProjectID(cloudConfig cloudapi.Config, project return 0, errNoProjectConfigured } - if id < math.MinInt32 || id > math.MaxInt32 { - return 0, fmt.Errorf("project ID %d overflows int32", id) - } - - return int32(id), nil + return id, nil } func (c *cmdCloudTestList) outputJSON(tests []v6cloudapi.LoadTest) error { diff --git a/internal/cmd/cloud_login.go b/internal/cmd/cloud_login.go index c31de4cbb..90f92f5c1 100644 --- a/internal/cmd/cloud_login.go +++ b/internal/cmd/cloud_login.go @@ -273,7 +273,6 @@ func authenticateUserToken( "Authentication failed as provided token or stack might not be valid."+ " Learn more: https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication."+ " Server error for details: %w", - err) } config.StackURL = null.StringFrom(stackURL) @@ -310,7 +309,7 @@ func validateTokenV6( return "", 0, 0, err } - return normalizedURL, int64(authResp.StackId), int64(authResp.DefaultProjectId), nil + return normalizedURL, authResp.StackId, authResp.DefaultProjectId, nil } // normalizeStackURL converts a stack slug to a full URL if needed and removes trailing slashes. diff --git a/internal/cmd/cloud_project.go b/internal/cmd/cloud_project.go index 931ba19ed..430374d9a 100644 --- a/internal/cmd/cloud_project.go +++ b/internal/cmd/cloud_project.go @@ -11,12 +11,12 @@ type cmdCloudProject struct { globalState *state.GlobalState } -func getCmdCloudProject(cloudCmd *cmdCloud) *cobra.Command { +func getCmdCloudProject(gs *state.GlobalState) *cobra.Command { c := &cmdCloudProject{ - globalState: cloudCmd.gs, + globalState: gs, } - exampleText := getExampleText(cloudCmd.gs, ` + exampleText := getExampleText(gs, ` # List all projects in the configured stack $ {{.}} cloud project list diff --git a/internal/cmd/cloud_project_list.go b/internal/cmd/cloud_project_list.go index f529eec3a..a6bde8d37 100644 --- a/internal/cmd/cloud_project_list.go +++ b/internal/cmd/cloud_project_list.go @@ -6,15 +6,12 @@ import ( "bytes" "encoding/json" "fmt" - "math" "strings" "text/tabwriter" "github.com/spf13/cobra" - "go.k6.io/k6/v2/cloudapi" "go.k6.io/k6/v2/cmd/state" - "go.k6.io/k6/v2/internal/build" cloudapiv6 "go.k6.io/k6/v2/internal/cloudapi/v6" ) @@ -47,42 +44,12 @@ func getCmdCloudProjectList(projectCmd *cmdCloudProject) *cobra.Command { } func (c *cmdCloudProjectList) run(_ *cobra.Command, _ []string) error { - currentDiskConf, err := readDiskConfig(c.globalState) + client, cloudConfig, err := newCloudV6ClientFromConfig( + c.globalState, "Listing cloud projects requires auth settings") if err != nil { return err } - currentJSONConfigRaw := currentDiskConf.Collectors["cloud"] - - cloudConfig, warn, err := cloudapi.GetConsolidatedConfig( - currentJSONConfigRaw, c.globalState.Env, "", nil) - if err != nil { - return err - } - if warn != "" { - c.globalState.Logger.Warn(warn) - } - - if err := checkCloudLoginFor(cloudConfig, "Listing cloud projects requires auth settings"); err != nil { - return err - } - - client, err := cloudapiv6.NewClient( - c.globalState.Logger, - cloudConfig.Token.String, - cloudConfig.Hostv6.String, - build.Version, - cloudConfig.Timeout.TimeDuration(), - ) - if err != nil { - return err - } - - if cloudConfig.StackID.Int64 < math.MinInt32 || cloudConfig.StackID.Int64 > math.MaxInt32 { - return fmt.Errorf("stack ID %d overflows int32", cloudConfig.StackID.Int64) - } - client.SetStackID(int32(cloudConfig.StackID.Int64)) - projects, err := client.ListProjects(c.globalState.Ctx) if err != nil { return err @@ -92,11 +59,7 @@ func (c *cmdCloudProjectList) run(_ *cobra.Command, _ []string) error { return c.outputJSON(projects) } - stackName := cloudConfig.StackURL.String - if !cloudConfig.StackURL.Valid { - stackName = fmt.Sprintf("stack-%d", cloudConfig.StackID.Int64) - } - stackHeader := fmt.Sprintf("Projects for %s:\n\n", stackName) + stackHeader := fmt.Sprintf("Projects for %s:\n\n", cloudStackName(cloudConfig)) if len(projects) == 0 { printToStdout(c.globalState, stackHeader+ diff --git a/internal/cmd/cloud_run.go b/internal/cmd/cloud_run.go index 692d31b5c..872d2ddde 100644 --- a/internal/cmd/cloud_run.go +++ b/internal/cmd/cloud_run.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" + "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/errext" "go.k6.io/k6/v2/errext/exitcodes" "go.k6.io/k6/v2/internal/execution" @@ -36,22 +37,29 @@ type cmdCloudRun struct { // noCloudSecrets stores the state of the --no-cloud-secrets flag. noCloudSecrets bool + // noCloudLogs stores the state of the --no-cloud-logs flag. + noCloudLogs bool + // runCmd holds an instance of the k6 run command that we store // in order to be able to call its run method to support // the --local-execution flag mode. runCmd *cmdRun - // deprecatedCloudCmd holds an instance of the k6 cloud command that we store - // in order to be able to call its run method to support the cloud execution - // feature, and to have access to its flagSet if necessary. - deprecatedCloudCmd *cmdCloud + // gs holds the global state, used to run the test in the cloud. + gs *state.GlobalState + + // showCloudLogs stores the state of the --show-logs flag. + showCloudLogs bool + + // exitOnRunning stores the state of the --exit-on-running flag. + exitOnRunning bool } -func getCmdCloudRun(cloudCmd *cmdCloud) *cobra.Command { +func getCmdCloudRun(gs *state.GlobalState) *cobra.Command { // We instantiate the run command here to be able to call its run method // when the --local-execution flag is set. runCmd := &cmdRun{ - gs: cloudCmd.gs, + gs: gs, // We override the loadConfiguredTest func to use the local execution // configuration which enforces the use of the cloud output among other @@ -61,17 +69,18 @@ func getCmdCloudRun(cloudCmd *cmdCloud) *cobra.Command { execution.Controller, error, ) { - test, err := loadAndConfigureLocalTest(cloudCmd.gs, cmd, args, getCloudRunLocalExecutionConfig) + test, err := loadAndConfigureLocalTest(gs, cmd, args, getCloudRunLocalExecutionConfig) return test, local.NewController(), err }, } cloudRunCmd := &cmdCloudRun{ - deprecatedCloudCmd: cloudCmd, - runCmd: runCmd, + gs: gs, + runCmd: runCmd, + showCloudLogs: true, } - exampleText := getExampleText(cloudCmd.gs, ` + exampleText := getExampleText(gs, ` # Run a test script in Grafana Cloud $ {{.}} cloud run script.js @@ -96,12 +105,12 @@ func getCmdCloudRun(cloudCmd *cmdCloud) *cobra.Command { thisCmd.Flags().SortFlags = false thisCmd.Flags().AddFlagSet(cloudRunCmd.flagSet()) - thisCmd.Flags().AddFlagSet(cloudCmd.flagSet()) + thisCmd.Flags().AddFlagSet(cloudCmdFlagSet(&cloudRunCmd.showCloudLogs, &cloudRunCmd.exitOnRunning)) return thisCmd } -func (c *cmdCloudRun) preRun(cmd *cobra.Command, args []string) error { +func (c *cmdCloudRun) preRun(cmd *cobra.Command, _ []string) error { if c.localExecution { if cmd.Flags().Changed("exit-on-running") { return errext.WithExitCodeIfNone( @@ -134,7 +143,14 @@ func (c *cmdCloudRun) preRun(cmd *cobra.Command, args []string) error { ) } - return c.deprecatedCloudCmd.preRun(cmd, args) + if c.noCloudLogs { + return errext.WithExitCodeIfNone( + fmt.Errorf("the --no-cloud-logs flag can only be used in conjunction with the --local-execution flag"), + exitcodes.InvalidConfig, + ) + } + + return applyCloudEnvOverrides(c.gs, cmd, &c.showCloudLogs, &c.exitOnRunning) } func (c *cmdCloudRun) run(cmd *cobra.Command, args []string) error { @@ -160,7 +176,10 @@ func (c *cmdCloudRun) run(cmd *cobra.Command, args []string) error { // When running the `k6 cloud run` command explicitly disable the usage report. c.noUsageReport = true - return c.deprecatedCloudCmd.run(cmd, args) + return runCloudTest(c.gs, cmd, args, cloudTestRunOptions{ + showCloudLogs: c.showCloudLogs, + exitOnRunning: c.exitOnRunning, + }) } func (c *cmdCloudRun) flagSet() *pflag.FlagSet { @@ -194,6 +213,12 @@ func (c *cmdCloudRun) flagSet() *pflag.FlagSet { c.noCloudSecrets, "only when using the local-execution mode, don't automatically configure the cloud secret source", ) + flags.BoolVar( + &c.noCloudLogs, + "no-cloud-logs", + c.noCloudLogs, + "only when using the local-execution mode, don't push logs to the cloud", + ) flags.StringArray("features", nil, "enable feature flags (comma-separated)") return flags diff --git a/internal/cmd/cloud_upload.go b/internal/cmd/cloud_upload.go index 9ec2aa997..c452086f2 100644 --- a/internal/cmd/cloud_upload.go +++ b/internal/cmd/cloud_upload.go @@ -13,21 +13,15 @@ const cloudUploadCommandName = "upload" type cmdCloudUpload struct { globalState *state.GlobalState - - // deprecatedCloudCmd holds an instance of the k6 cloud command that we store - // in order to be able to call its run method to support the cloud upload - // feature - deprecatedCloudCmd *cmdCloud } -func getCmdCloudUpload(cloudCmd *cmdCloud) *cobra.Command { +func getCmdCloudUpload(gs *state.GlobalState) *cobra.Command { c := &cmdCloudUpload{ - globalState: cloudCmd.gs, - deprecatedCloudCmd: cloudCmd, + globalState: gs, } // uploadCloudCommand represents the 'cloud upload' command - exampleText := getExampleText(cloudCmd.gs, ` + exampleText := getExampleText(gs, ` # Upload a test to Grafana Cloud without running it $ {{.}} cloud upload script.js`[1:]) @@ -46,14 +40,16 @@ func getCmdCloudUpload(cloudCmd *cmdCloud) *cobra.Command { return uploadCloudCommand } -func (c *cmdCloudUpload) preRun(cmd *cobra.Command, args []string) error { - return c.deprecatedCloudCmd.preRun(cmd, args) +func (c *cmdCloudUpload) preRun(_ *cobra.Command, _ []string) error { + // The upload command doesn't expose the --show-logs/--exit-on-running flags, + // but we still validate the corresponding env variables for wrong values. + _, _, err := parseCloudEnvOverrides(c.globalState.Env) + return err } // run is the code that runs when the user executes `k6 cloud upload` func (c *cmdCloudUpload) run(cmd *cobra.Command, args []string) error { - c.deprecatedCloudCmd.uploadOnly = true - return c.deprecatedCloudCmd.run(cmd, args) + return runCloudTest(c.globalState, cmd, args, cloudTestRunOptions{uploadOnly: true}) } func (c *cmdCloudUpload) flagSet() *pflag.FlagSet { diff --git a/internal/cmd/config.go b/internal/cmd/config.go index 53e1388dd..480c4708b 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "maps" "path/filepath" "strings" "time" @@ -17,6 +18,7 @@ import ( "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/errext" "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/internal/features" "go.k6.io/k6/v2/lib" "go.k6.io/k6/v2/lib/executor" "go.k6.io/k6/v2/lib/fsext" @@ -33,7 +35,7 @@ func configFlagSet() *pflag.FlagSet { flags.Bool( "no-usage-report", false, - "don't send anonymous usage"+"stats (https://grafana.com/docs/k6/latest/set-up/usage-collection/)", + "don't send anonymous usage stats (https://grafana.com/docs/k6/latest/set-up/usage-collection/)", ) flags.StringArray("features", nil, "enable feature flags (comma-separated)") return flags @@ -49,6 +51,9 @@ type Config struct { NoUsageReport null.Bool `json:"noUsageReport" envconfig:"K6_NO_USAGE_REPORT"` WebDashboard null.Bool `json:"webDashboard" envconfig:"K6_WEB_DASHBOARD"` + // once carries --once between the layers. Unexported keeps json and envconfig out. + once bool + // NoArchiveUpload is an option that is only used when running in local-execution mode with the cloud run // command. // @@ -202,27 +207,34 @@ func readEnvConfig(envMap map[string]string) (Config, error) { return conf, err } -// getConsolidatedConfig assemble the final consolidated configuration from all of the different sources: -// - start with the CLI-provided options to get shadowed (non-Valid) defaults in there -// - add the global file config options -// - add the Runner-provided options (they may come from Bundle too if applicable) -// - add the environment variables -// - merge the user-supplied CLI flags back in on top, to give them the greatest priority -// - set some defaults if they weren't previously specified -// TODO: add better validation, more explicit default values and improve consistency between formats -// TODO: accumulate all errors and differentiate between the layers? -func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib.Options) (Config, error) { +// readConfigLayers reads file and environment options before merging them. +func readConfigLayers(gs *state.GlobalState) (Config, Config, error) { fileConf, err := readDiskConfig(gs) if err != nil { err = fmt.Errorf("failed to load the configuration file from the local file system: %w", err) - return Config{}, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) + return Config{}, Config{}, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) } envConf, err := readEnvConfig(gs.Env) if err != nil { - return Config{}, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) + return Config{}, Config{}, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) } + return fileConf, envConf, nil +} + +// getConsolidatedConfig assemble the final consolidated configuration from all of the different sources: +// - start with the CLI-provided options to get shadowed (non-Valid) defaults in there +// - add the global file config options +// - add the Runner-provided options (they may come from Bundle too if applicable) +// - add the environment variables +// - merge the user-supplied CLI flags back in on top, to give them the greatest priority +// - set some defaults if they weren't previously specified +// TODO: add better validation, more explicit default values and improve consistency between formats +// TODO: accumulate all errors and differentiate between the layers? +func getConsolidatedConfig( + gs *state.GlobalState, cliConf, fileConf, envConf Config, runnerOpts lib.Options, flags *features.Flags, +) (Config, error) { conf := cliConf.Apply(fileConf) warnOnShortHandOverride(conf.Options, runnerOpts, "script", gs.Logger) @@ -234,6 +246,15 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib warnOnShortHandOverride(conf.Options, cliConf.Options, "cli", gs.Logger) conf = conf.Apply(cliConf) + if flags != nil && flags.MergeRunTags { + conf.RunTags = mergeRunTags( + fileConf.RunTags, + runnerOpts.RunTags, + envConf.RunTags, + cliConf.RunTags, + ) + } + conf = applyDefault(conf) // TODO(imiric): Move this validation where it makes sense in the configuration @@ -241,13 +262,27 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib // for CLI flags in cmd.getOptions, in case other configuration sources // (e.g. env vars) overrode our default value. This is not done in // lib.Options.Validate to avoid circular imports. - if _, err = metrics.GetResolversForTrendColumns(conf.SummaryTrendStats); err != nil { + if _, err := metrics.GetResolversForTrendColumns(conf.SummaryTrendStats); err != nil { return Config{}, err } return conf, nil } +// mergeRunTags accumulates RunTags from each layer in precedence order (lowest first). +// On key collision, later (higher-priority) layers overwrite earlier ones. +// Returns nil when every layer is empty to preserve the codebase's "tags":null serialization. +func mergeRunTags(layers ...map[string]string) map[string]string { + merged := map[string]string{} + for _, l := range layers { + maps.Copy(merged, l) + } + if len(merged) == 0 { + return nil + } + return merged +} + func warnOnShortHandOverride(a, b lib.Options, bName string, logger logrus.FieldLogger) { if a.Scenarios != nil && (b.Duration.Valid || b.Iterations.Valid || b.Stages != nil || b.Scenarios != nil) { @@ -284,6 +319,9 @@ func applyDefault(conf Config) Config { if !conf.TeardownTimeout.Valid { conf.TeardownTimeout.Duration = types.Duration(60 * time.Second) } + if !conf.HandleSummaryTimeout.Valid { + conf.HandleSummaryTimeout.Duration = types.Duration(120 * time.Second) + } return conf } diff --git a/internal/cmd/config_consolidation_test.go b/internal/cmd/config_consolidation_test.go index 64aedcc05..852989bb1 100644 --- a/internal/cmd/config_consolidation_test.go +++ b/internal/cmd/config_consolidation_test.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "strings" "testing" "time" @@ -11,6 +12,7 @@ import ( "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/internal/cmd/tests" + "go.k6.io/k6/v2/internal/features" "go.k6.io/k6/v2/lib" "go.k6.io/k6/v2/lib/executor" "go.k6.io/k6/v2/lib/fsext" @@ -105,11 +107,12 @@ func getFS(files []file) fsext.Fs { } type opts struct { - cli []string - env []string - runner *lib.Options - fs fsext.Fs - cmds []string + cli []string + env []string + runner *lib.Options + fs fsext.Fs + cmds []string + features *features.Flags } // exp contains the different events or errors we expect our test case to trigger. @@ -349,6 +352,66 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase { assert.Equal(t, exp, c.RunTags) }, }, + // merge-run-tags off: each layer that sets tags replaces the previous one wholesale. + { + opts{ + fs: defaultConfig(`{"tags": {"file_only": "f", "shared": "from-file"}}`), + runner: &lib.Options{RunTags: map[string]string{"script_only": "s", "shared": "from-script"}}, + env: []string{"K6_TAGS=env_only:e,shared:from-env"}, + cli: []string{"--tag", "cli_only=c", "--tag", "shared=from-cli"}, + }, + exp{}, + func(t *testing.T, c Config) { + assert.Equal(t, map[string]string{ + "cli_only": "c", + "shared": "from-cli", + }, c.RunTags) + }, + }, + // merge-run-tags on: every layer contributes; higher-priority wins on key collision. + // Precedence: file < script < env < CLI. + { + opts{ + fs: defaultConfig(`{"tags": {"file_only": "f", "shared": "from-file"}}`), + runner: &lib.Options{RunTags: map[string]string{"script_only": "s", "shared": "from-script"}}, + env: []string{"K6_TAGS=env_only:e,shared:from-env"}, + cli: []string{"--tag", "cli_only=c", "--tag", "shared=from-cli"}, + features: &features.Flags{MergeRunTags: true}, + }, + exp{}, + func(t *testing.T, c Config) { + assert.Equal(t, map[string]string{ + "file_only": "f", + "script_only": "s", + "env_only": "e", + "cli_only": "c", + "shared": "from-cli", + }, c.RunTags) + }, + }, + // merge-run-tags on with only file + CLI tags (the original PR scenario). + { + opts{ + fs: defaultConfig(`{"tags": {"codeTagKey": "codeTagValue"}}`), + cli: []string{"--tag", "clitagkey=clitagvalue"}, + features: &features.Flags{MergeRunTags: true}, + }, + exp{}, + func(t *testing.T, c Config) { + assert.Equal(t, map[string]string{ + "codeTagKey": "codeTagValue", + "clitagkey": "clitagvalue", + }, c.RunTags) + }, + }, + // merge-run-tags on with no tags anywhere: result stays nil. + { + opts{features: &features.Flags{MergeRunTags: true}}, + exp{}, + func(t *testing.T, c Config) { + assert.Nil(t, c.RunTags) + }, + }, // Test summary trend stats {opts{}, exp{}, func(t *testing.T, c Config) { @@ -504,10 +567,14 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase { } func runTestCase(t *testing.T, testCase configConsolidationTestCase, subCmd string) { + if leanBuild && strings.HasPrefix(subCmd, "cloud") { + t.Skip("cloud configuration is excluded from lean builds") + } t.Logf("Test for `k6 %s` with opts=%#v and exp=%#v\n", subCmd, testCase.options, testCase.expected) ts := tests.NewGlobalTestState(t) - ts.CmdArgs = append([]string{"k6", subCmd}, testCase.options.cli...) + // subCmd may name a nested command, e.g. "cloud run". + ts.CmdArgs = append(append([]string{"k6"}, strings.Fields(subCmd)...), testCase.options.cli...) ts.Env = state.BuildEnvMap(testCase.options.env) if testCase.options.fs != nil { ts.FS = testCase.options.fs @@ -544,7 +611,11 @@ func runTestCase(t *testing.T, testCase configConsolidationTestCase, subCmd stri if testCase.options.runner != nil { opts = *testCase.options.runner } - consolidatedConfig, err := getConsolidatedConfig(ts.GlobalState, cliConf, opts) + var consolidatedConfig Config + fileConf, envConf, err := readConfigLayers(ts.GlobalState) + if err == nil { + consolidatedConfig, err = getConsolidatedConfig(ts.GlobalState, cliConf, fileConf, envConf, opts, testCase.options.features) + } if testCase.expected.consolidationError { require.Error(t, err) return @@ -583,7 +654,7 @@ func TestConfigConsolidation(t *testing.T) { for tcNum, testCase := range getConfigConsolidationTestCases() { subCommands := testCase.options.cmds if subCommands == nil { // handle the most common case - subCommands = []string{"run", "archive", "cloud"} + subCommands = []string{"run", "archive", "cloud run"} } for fsNum, subCmd := range subCommands { t.Run( diff --git a/internal/cmd/deps.go b/internal/cmd/deps.go index 98d6dda9a..f0ebf1208 100644 --- a/internal/cmd/deps.go +++ b/internal/cmd/deps.go @@ -43,8 +43,7 @@ func getCmdDeps(gs *state.GlobalState) *cobra.Command { func (c *depsCmd) run(cmd *cobra.Command, args []string) error { test, err := loadLocalTestWithoutRunner(c.gs, cmd, args) if err != nil { - var unsatisfiedErr binaryIsNotSatisfyingDependenciesError - if !errors.As(err, &unsatisfiedErr) { + if _, ok := errors.AsType[binaryIsNotSatisfyingDependenciesError](err); !ok { return err } } diff --git a/internal/cmd/launcher.go b/internal/cmd/launcher.go index 07b06345d..f4203c62c 100644 --- a/internal/cmd/launcher.go +++ b/internal/cmd/launcher.go @@ -96,8 +96,7 @@ func (b *customBinary) run(ctx context.Context, gs *state.GlobalState) error { for { select { case err := <-done: - var exitError *exec.ExitError - if errors.As(err, &exitError) { + if exitError, ok := errors.AsType[*exec.ExitError](err); ok { return errext.WithExitCodeIfNone(errAlreadyReported, exitcodes.ExitCode(exitError.ExitCode())) //nolint:gosec } return err diff --git a/internal/cmd/launcher_test.go b/internal/cmd/launcher_test.go index 847f1456f..c399127a4 100644 --- a/internal/cmd/launcher_test.go +++ b/internal/cmd/launcher_test.go @@ -444,6 +444,9 @@ func TestGetProviderConfig(t *testing.T) { } config := getProviderConfig(ts.GlobalState) + if leanBuild { + tc.expectConfig.BuildServiceAuth = "" + } assert.Equal(t, tc.expectConfig, config) }) diff --git a/internal/cmd/lean_behavior_test.go b/internal/cmd/lean_behavior_test.go new file mode 100644 index 000000000..94bbd22fd --- /dev/null +++ b/internal/cmd/lean_behavior_test.go @@ -0,0 +1,44 @@ +//go:build lean + +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd/tests" + "go.k6.io/k6/v2/output" +) + +func TestLeanUnavailableOutputs(t *testing.T) { + t.Parallel() + constructors, err := getAllOutputConstructors() + require.NoError(t, err) + for _, name := range []string{"cloud", "influxdb", "experimental-prometheus-rw", "opentelemetry", "web-dashboard"} { + t.Run(name, func(t *testing.T) { + t.Parallel() + constructor, ok := constructors[name] + require.True(t, ok) + out, err := constructor(output.Params{}) + require.ErrorContains(t, err, "not available in this lean build") + assert.Nil(t, out) + }) + } + for _, name := range []string{"json", "csv", "timescaledb-breakingit"} { + assert.Contains(t, constructors, name) + } +} + +func TestLeanCommandStubs(t *testing.T) { + t.Parallel() + ts := tests.NewGlobalTestState(t) + cloud := getCmdCloud(ts.GlobalState) + assert.Contains(t, cloud.Short, "not available in this lean build") + assert.Empty(t, cloud.Commands()) + stats := getCmdStats(ts.GlobalState) + assert.True(t, stats.Hidden) + require.ErrorContains(t, stats.RunE(stats, nil), "not available in this lean build") + assert.ErrorIs(t, getCmdCloudLogin(ts.GlobalState).RunE(nil, nil), errCloudNotAvailable) +} diff --git a/internal/cmd/once.go b/internal/cmd/once.go new file mode 100644 index 000000000..e108818ef --- /dev/null +++ b/internal/cmd/once.go @@ -0,0 +1,134 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "maps" + "slices" + "strings" + + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/errext" + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/executor" +) + +// --once writes the load itself, so a CLI shortcut would fight the scenario it writes. +func checkOnceConflicts(flags *pflag.FlagSet) error { + if !getNullBool(flags, "once").Bool { + return nil + } + for _, name := range []string{ + "vus", "duration", "iterations", "stage", + "execution-segment", "execution-segment-sequence", + } { + if flags.Changed(name) { + return errext.WithExitCodeIfNone( + fmt.Errorf("--once cannot be combined with --%s", name), exitcodes.InvalidConfig) + } + } + return nil +} + +// A shortcut left in a lower layer drops or rewrites the scenario applyOnce writes. +func dropOnceShortcuts(logger logrus.FieldLogger, layers map[string]*lib.Options) error { + var unset lib.Options + + // k6 merges the layers in this order, and the warning follows it. + for _, name := range []string{"config", "script", "environment"} { + o, ok := layers[name] + if !ok { + return fmt.Errorf("missing %q layer", name) + } + + var dropped []string + drop := func(opt string, set bool) { + if set { + dropped = append(dropped, opt) + } + } + drop("vus", o.VUs.Valid) + drop("duration", o.Duration.Valid) + drop("iterations", o.Iterations.Valid) + drop("stages", o.Stages != nil) + drop("executionSegment", o.ExecutionSegment != nil) + drop("executionSegmentSequence", o.ExecutionSegmentSequence != nil) + + // Clearing an unset shortcut changes nothing, so only the naming above needs a check. + o.VUs = unset.VUs + o.Duration = unset.Duration + o.Iterations = unset.Iterations + o.Stages = unset.Stages + o.ExecutionSegment = unset.ExecutionSegment + o.ExecutionSegmentSequence = unset.ExecutionSegmentSequence + + if len(dropped) > 0 { + logger.Warnf("--once overrode %s in %q configuration", strings.Join(dropped, ", "), name) + } + } + + return nil +} + +// Bare --once never guesses which scenario runs, so several scenarios are ambiguous. +func rejectAmbiguousOnce(scenarios lib.ScenarioConfigs) error { + if len(scenarios) <= 1 { + return nil + } + names := slices.Sorted(maps.Keys(scenarios)) + return errext.WithExitCodeIfNone( + fmt.Errorf("--once can run only with a single scenario, but got: %s", + strings.Join(names, ", ")), exitcodes.InvalidConfig) +} + +// Runs before DeriveScenariosFromShortcuts, so the archive gets the same scenarios. +func applyOnce(opts lib.Options) (lib.Options, error) { + scenarios := opts.Scenarios + if len(scenarios) == 0 { + scenarios = lib.ScenarioConfigs{lib.DefaultScenarioName: nil} + } + + rewritten := make(lib.ScenarioConfigs, len(scenarios)) + for name, src := range scenarios { + sc, err := onceScenario(name, src) + if err != nil { + return opts, err + } + rewritten[name] = sc + } + opts.Scenarios = rewritten + + return opts, nil +} + +func onceScenario(name string, src lib.ExecutorConfig) (lib.ExecutorConfig, error) { + once := executor.NewSharedIterationsConfig(name) + once.VUs = null.IntFrom(1) + once.Iterations = null.IntFrom(1) + if src == nil { + return once, nil + } + + // GetExec collapses an unset exec and an explicit "default" to the same "default", + // and the interface exposes no raw getter, so read the null.String through JSON. + data, err := json.Marshal(src) + if err != nil { + return nil, fmt.Errorf("reading the %q scenario: %w", src.GetName(), err) + } + var raw struct { + Exec null.String `json:"exec"` + } + if err := json.Unmarshal(data, &raw); err != nil { + return nil, fmt.Errorf("reading the exec of the %q scenario: %w", src.GetName(), err) + } + once.Exec = raw.Exec + once.Env = src.GetEnv() + once.Tags = src.GetTags() + once.Options = src.GetScenarioOptions() + + return once, nil +} diff --git a/internal/cmd/once_test.go b/internal/cmd/once_test.go new file mode 100644 index 000000000..3c9c0d276 --- /dev/null +++ b/internal/cmd/once_test.go @@ -0,0 +1,257 @@ +package cmd + +import ( + "path/filepath" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/errext" + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/internal/cmd/tests" + "go.k6.io/k6/v2/internal/lib/testutils" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/executor" + "go.k6.io/k6/v2/lib/fsext" + "go.k6.io/k6/v2/lib/types" +) + +func TestApplyOncePreservesScenarioName(t *testing.T) { + t.Parallel() + + opts := lib.Options{Scenarios: lib.ScenarioConfigs{ + "ui": executor.NewConstantVUsConfig("ui"), + "api": executor.NewConstantVUsConfig("api"), + }} + + res, err := applyOnce(opts) + require.NoError(t, err) + require.Len(t, res.Scenarios, 2) + + for _, name := range []string{"ui", "api"} { + sc, ok := res.Scenarios[name].(executor.SharedIterationsConfig) + require.True(t, ok) + assert.Equal(t, name, sc.GetName()) + assert.Equal(t, null.IntFrom(1), sc.VUs) + assert.Equal(t, null.IntFrom(1), sc.Iterations) + } +} + +func mustExtractSingleSharedIterScenario(t *testing.T, opts lib.Options) executor.SharedIterationsConfig { + t.Helper() + + require.Len(t, opts.Scenarios, 1) + for _, sc := range opts.Scenarios { + shared, ok := sc.(executor.SharedIterationsConfig) + require.True(t, ok) + return shared + } + return executor.SharedIterationsConfig{} +} + +func TestApplyOncePreservesScenarioExec(t *testing.T) { + t.Parallel() + + for _, tt := range []struct { + name string + exec null.String + }{ + {"custom", null.StringFrom("api")}, + {"unset", null.String{}}, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + src := executor.NewSharedIterationsConfig("ui") + src.Exec = tt.exec + opts := lib.Options{Scenarios: lib.ScenarioConfigs{"ui": src}} + + res, err := applyOnce(opts) + require.NoError(t, err) + assert.Equal(t, tt.exec, mustExtractSingleSharedIterScenario(t, res).Exec) + }) + } +} + +func TestApplyOncePreservesScenarioFields(t *testing.T) { + t.Parallel() + + for _, tt := range []struct { + name string + setup func(*executor.SharedIterationsConfig) + check func(*testing.T, executor.SharedIterationsConfig) + }{ + { + name: "env", + setup: func(s *executor.SharedIterationsConfig) { s.Env = map[string]string{"GREETING": "hi"} }, + check: func(t *testing.T, s executor.SharedIterationsConfig) { + t.Helper() + assert.Equal(t, map[string]string{"GREETING": "hi"}, s.Env) + }, + }, + { + name: "tags", + setup: func(s *executor.SharedIterationsConfig) { s.Tags = map[string]string{"team": "core"} }, + check: func(t *testing.T, s executor.SharedIterationsConfig) { + t.Helper() + assert.Equal(t, map[string]string{"team": "core"}, s.Tags) + }, + }, + { + name: "options", + setup: func(s *executor.SharedIterationsConfig) { + s.Options = &lib.ScenarioOptions{Browser: map[string]any{"type": "chromium"}} + }, + check: func(t *testing.T, s executor.SharedIterationsConfig) { + t.Helper() + assert.Equal(t, &lib.ScenarioOptions{Browser: map[string]any{"type": "chromium"}}, s.Options) + }, + }, + } { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + src := executor.NewSharedIterationsConfig("ui") + tt.setup(&src) + opts := lib.Options{Scenarios: lib.ScenarioConfigs{"ui": src}} + + res, err := applyOnce(opts) + require.NoError(t, err) + + sc := mustExtractSingleSharedIterScenario(t, res) + tt.check(t, sc) + }) + } +} + +func TestGetConsolidatedConfigOnceDropsLowerLayerShortcuts(t *testing.T) { + t.Parallel() + + ts := tests.NewGlobalTestState(t) + require.NoError(t, ts.FS.MkdirAll(filepath.Dir(ts.Flags.ConfigFilePath), 0o755)) + require.NoError(t, fsext.WriteFile(ts.FS, ts.Flags.ConfigFilePath, + []byte(`{"vus": 7, "stages": [{"duration": "10s", "target": 5}]}`), 0o644)) + ts.Env["K6_ITERATIONS"] = "5" + + segment, err := lib.NewExecutionSegmentFromString("0:1/2") + require.NoError(t, err) + runnerOpts := lib.Options{ + Duration: types.NullDurationFrom(time.Second), + ExecutionSegment: segment, + } + + fileConf, envConf, err := readConfigLayers(ts.GlobalState) + require.NoError(t, err) + require.NoError(t, dropOnceShortcuts(ts.Logger, map[string]*lib.Options{ + "config": &fileConf.Options, + "script": &runnerOpts, + "environment": &envConf.Options, + })) + conf, err := getConsolidatedConfig(ts.GlobalState, Config{}, fileConf, envConf, runnerOpts, nil) + require.NoError(t, err) + + assert.False(t, conf.VUs.Valid) + assert.False(t, conf.Duration.Valid) + assert.False(t, conf.Iterations.Valid) + assert.Nil(t, conf.Stages) + assert.Nil(t, conf.ExecutionSegment) +} + +func TestOncePreservesScenarioAcrossLayers(t *testing.T) { + t.Parallel() + + ts := tests.NewGlobalTestState(t) + require.NoError(t, ts.FS.MkdirAll(filepath.Dir(ts.Flags.ConfigFilePath), 0o755)) + require.NoError(t, fsext.WriteFile(ts.FS, ts.Flags.ConfigFilePath, []byte(`{"scenarios": { + "fromfile": { "executor": "constant-vus", "vus": 3, "duration": "2s", "exec": "api", + "env": { "GREETING": "hi" }, "tags": { "team": "core" }, + "options": { "browser": { "type": "chromium" } } } + }}`), 0o644)) + ts.Env["K6_ITERATIONS"] = "5" + + runnerOpts := lib.Options{Duration: types.NullDurationFrom(time.Second)} + + fileConf, envConf, err := readConfigLayers(ts.GlobalState) + require.NoError(t, err) + require.NoError(t, dropOnceShortcuts(ts.Logger, map[string]*lib.Options{ + "config": &fileConf.Options, + "script": &runnerOpts, + "environment": &envConf.Options, + })) + conf, err := getConsolidatedConfig(ts.GlobalState, Config{}, fileConf, envConf, runnerOpts, nil) + require.NoError(t, err) + + opts, err := applyOnce(conf.Options) + require.NoError(t, err) + + sc := mustExtractSingleSharedIterScenario(t, opts) + assert.Equal(t, "fromfile", sc.GetName()) + assert.Equal(t, null.IntFrom(1), sc.VUs) + assert.Equal(t, null.IntFrom(1), sc.Iterations) + assert.Equal(t, null.StringFrom("api"), sc.Exec) + assert.Equal(t, map[string]string{"GREETING": "hi"}, sc.Env) + assert.Equal(t, map[string]string{"team": "core"}, sc.Tags) + assert.Equal(t, &lib.ScenarioOptions{Browser: map[string]any{"type": "chromium"}}, sc.Options) +} + +func TestDropOnceShortcutsWarnsOnlyWhenItDropsShortcuts(t *testing.T) { + t.Parallel() + + logger, hook := testutils.NewLoggerWithHook(t, logrus.WarnLevel) + + require.NoError(t, dropOnceShortcuts(logger, map[string]*lib.Options{ + "config": {}, + "script": {}, + "environment": {}, + })) + assert.Empty(t, hook.Drain()) + + segment, err := lib.NewExecutionSegmentFromString("0:1/2") + require.NoError(t, err) + require.NoError(t, dropOnceShortcuts(logger, map[string]*lib.Options{ + "config": {VUs: null.IntFrom(1), Stages: []lib.Stage{ + {Duration: types.NullDurationFrom(time.Second), Target: null.IntFrom(1)}, + }}, + "script": { + VUs: null.IntFrom(1), Duration: types.NullDurationFrom(time.Second), + ExecutionSegment: segment, + }, + "environment": {Iterations: null.IntFrom(1)}, + })) + + entries := hook.Drain() + require.Len(t, entries, 3) + assert.Equal(t, `--once overrode vus, stages in "config" configuration`, entries[0].Message) + assert.Equal(t, `--once overrode vus, duration, executionSegment in "script" configuration`, entries[1].Message) + assert.Equal(t, `--once overrode iterations in "environment" configuration`, entries[2].Message) +} + +func TestGetOptionsRejectsOnceWithShortcut(t *testing.T) { + t.Parallel() + + for _, tt := range []struct{ flag, value string }{ + {"vus", "1"}, + {"duration", "10s"}, + {"iterations", "5"}, + {"stage", "10s:5"}, + {"execution-segment", "1/2:1"}, + } { + t.Run(tt.flag, func(t *testing.T) { + t.Parallel() + + flags := optionFlagSet() + require.NoError(t, flags.Parse([]string{"--once", "--" + tt.flag, tt.value})) + + _, err := getOptions(flags) + require.EqualError(t, err, "--once cannot be combined with --"+tt.flag) + + var ec errext.HasExitCode + require.ErrorAs(t, err, &ec) + assert.Equal(t, exitcodes.InvalidConfig, ec.ExitCode()) + }) + } +} diff --git a/internal/cmd/options.go b/internal/cmd/options.go index 5dd9e55bd..c687f80cc 100644 --- a/internal/cmd/options.go +++ b/internal/cmd/options.go @@ -24,6 +24,8 @@ func optionFlagSet() *pflag.FlagSet { flags := pflag.NewFlagSet("", 0) flags.SortFlags = false + flags.Bool("once", false, "run the test with 1 VU and 1 iteration") + flags.StringSlice("scenario", nil, "run scenarios by name (comma-separated)") flags.Int64P("vus", "u", 1, "number of virtual users") flags.DurationP("duration", "d", 0, "test duration limit") flags.Int64P("iterations", "i", 0, "script total iteration limit (among all VUs)") @@ -79,6 +81,13 @@ func optionFlagSet() *pflag.FlagSet { //nolint:funlen,gocognit,cyclop // this needs breaking up but probably should wait for croconf func getOptions(flags *pflag.FlagSet) (lib.Options, error) { + if err := checkOnceConflicts(flags); err != nil { + return lib.Options{}, err + } + if err := checkScenarioConflicts(flags); err != nil { + return lib.Options{}, err + } + opts := lib.Options{ VUs: getNullInt64(flags, "vus"), Duration: getNullDuration(flags, "duration"), diff --git a/internal/cmd/outputs.go b/internal/cmd/outputs.go index 5ed1de523..a641fc2ac 100644 --- a/internal/cmd/outputs.go +++ b/internal/cmd/outputs.go @@ -119,6 +119,8 @@ func createOutputs( } } + attachCloudLogDrainer(out, gs) + result = append(result, out) } diff --git a/internal/cmd/outputs_cloud.go b/internal/cmd/outputs_cloud.go index cbe7a6f49..dc2469218 100644 --- a/internal/cmd/outputs_cloud.go +++ b/internal/cmd/outputs_cloud.go @@ -8,17 +8,22 @@ import ( "errors" "fmt" "path/filepath" + "strconv" "strings" "time" + "github.com/mstoykov/envconfig" "github.com/sirupsen/logrus" "gopkg.in/guregu/null.v3" "go.k6.io/k6/v2/cloudapi" "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/internal/build" + "go.k6.io/k6/v2/internal/cloudapi/provisioning" + cloudlog "go.k6.io/k6/v2/internal/log/cloud" cloudsecrets "go.k6.io/k6/v2/internal/secretsource/cloud" "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/types" "go.k6.io/k6/v2/metrics" ) @@ -96,8 +101,13 @@ func createCloudTest(gs *state.GlobalState, test *loadedAndConfiguredTest) error } if conf.PushRefID.Valid && conf.PushRefID.String != "" { + if err := applyExternalProvisioningCreds(gs, test, &conf); err != nil { + return err + } test.preInitState.RuntimeOptions.Env[testRunIDKey] = conf.PushRefID.String gs.Logger.Debug("using PushRefID, skipping CreateTestRun") + // Externally-provisioned run: the run id is the PushRefID. + configureCloudLogPusher(gs, conf, conf.PushRefID.String) return nil } @@ -106,65 +116,171 @@ func createCloudTest(gs *state.GlobalState, test *loadedAndConfiguredTest) error testArchive = test.makeArchive() } - testRun := &cloudapi.TestRun{ - Name: conf.Name.String, - ProjectID: conf.ProjectID.Int64, - VUsMax: int64(lib.GetMaxPossibleVUs(executionPlan)), //nolint:gosec - Thresholds: thresholds, - Duration: int64(duration / time.Second), - Archive: testArchive, - } - logger := gs.Logger.WithFields(logrus.Fields{"output": builtinOutputCloud.String()}) - apiClient := cloudapi.NewClient( - logger, conf.Token.String, conf.Host.String, build.Version, conf.Timeout.TimeDuration()) - if conf.StackID.Valid { - apiClient.SetStackID(conf.StackID.Int64) + // The stack ID is required (checkCloudLogin enforces it) and passed as + // int64; provisioning.NewClient handles the int32 boundary internally. + provClient, err := provisioning.NewClient( + logger, conf.Token.String, conf.Hostv6.String, build.Version, + conf.StackID.Int64, + conf.Timeout.TimeDuration(), + ) + if err != nil { + return err } - if testRun.ProjectID == 0 { + if conf.ProjectID.Int64 == 0 { projectID, err := resolveDefaultProjectID(gs, &conf) if err != nil { return err } if projectID > 0 { - testRun.ProjectID = projectID + conf.ProjectID = null.IntFrom(projectID) } } - response, err := apiClient.CreateTestRun(testRun) + // Marshal the resolved lib.Options as json.RawMessage for the request + // body's `options` field. No map[string]any round-trip — preserves + // typed precision from lib.Options custom marshalers. + optionsJSON, err := json.Marshal(test.derivedConfig.Options) + if err != nil { + return fmt.Errorf("marshalling options: %w", err) + } + + ctx := gs.Ctx + result, err := provClient.ProvisionLocalExecution(ctx, provisioning.ProvisionParams{ + Name: conf.Name.String, + ProjectID: conf.ProjectID.Int64, + MaxVUs: int64(lib.GetMaxPossibleVUs(executionPlan)), //nolint:gosec + TotalDuration: int64(duration / time.Second), + Options: json.RawMessage(optionsJSON), + Archive: testArchive, + }) if err != nil { return err } - // We store the test run id in the environment, so it can be used later. - test.preInitState.RuntimeOptions.Env[testRunIDKey] = response.ReferenceID + // Stash testRunID in env (existing pattern). + test.preInitState.RuntimeOptions.Env[testRunIDKey] = strconv.FormatInt(result.TestRunID, 10) + + // Apply runtime_config metrics tuning to the Config. Malformed duration + // strings from the backend are logged as warnings, not failures. + conf = conf.Apply(buildConfigFromRuntimeConfig(logger, result.RuntimeConfig)) + + // Set the Config fields that the cloud Output reads for metrics push. + conf.MetricsPushURL = null.StringFrom(result.RuntimeConfig.Metrics.PushURL) + conf.TestRunToken = null.StringFrom(result.RuntimeConfig.TestRunToken) + + // Set the test run details page URL for the output banner. + conf.TestRunDetails = null.StringFrom(result.TestRunDetailsPageURL) - if response.SecretsConfig != nil && gs.CloudSecretSource != nil { + // Register secrets config (always present in the provisioning response). + if gs.CloudSecretSource != nil { gs.CloudSecretSource.SetConfig(&cloudsecrets.Config{ - Token: response.TestRunToken, - Endpoint: response.SecretsConfig.Endpoint, - ResponsePath: response.SecretsConfig.ResponsePath, + Token: result.RuntimeConfig.TestRunToken, + Endpoint: result.RuntimeConfig.Secrets.Endpoint, + ResponsePath: result.RuntimeConfig.Secrets.ResponsePath, }) } - // If the Cloud API returned configuration overrides, we apply them to the current configuration. - // Then, we serialize the overridden configuration back, so it can be used by the Cloud output. - if response.ConfigOverride != nil { - logger.WithFields(logrus.Fields{"override": response.ConfigOverride}).Debug("overriding config options") + // Configure the log pusher for the self-provisioned run. The run id is + // the provisioning result's TestRunID. + configureCloudLogPusher(gs, conf, strconv.FormatInt(result.TestRunID, 10)) - raw, err := cloudConfToRawMessage(conf.Apply(*response.ConfigOverride)) - if err != nil { - return fmt.Errorf("could not serialize overridden cloud configuration: %w", err) - } + // Serialise overridden Config back into the collectors map (existing pattern). + raw, err := cloudConfToRawMessage(conf) + if err != nil { + return fmt.Errorf("could not serialize overridden cloud configuration: %w", err) + } + if test.derivedConfig.Collectors == nil { + test.derivedConfig.Collectors = make(map[string]json.RawMessage) + } + test.derivedConfig.Collectors[builtinOutputCloud.String()] = raw - if test.derivedConfig.Collectors == nil { - test.derivedConfig.Collectors = make(map[string]json.RawMessage) - } - test.derivedConfig.Collectors[builtinOutputCloud.String()] = raw + return nil +} + +// applyExternalProvisioningCreds handles the externally-provisioned +// `--local-execution` flow, signalled by K6_CLOUD_PUSH_REF_ID: an external +// service created and started the run and hands its scoped push credentials +// to this local k6 via env. It reads K6_CLOUD_METRICS_PUSH_URL and +// K6_CLOUD_TEST_RUN_TOKEN explicitly here — they are deliberately NOT +// env-bound on cloudapi.Config, so a stray env var can never override the +// values the self-provision flow obtains from the provisioning response. +// It requires both-or-neither and, when both are present, sets them on conf +// (so downstream consumers such as the log pusher see them) and bakes them +// into the serialized cloud config that the Output reads. It also reads the +// K6_CLOUD_LOGS_* log-push config, which the external flow supplies the same +// way. +func applyExternalProvisioningCreds( + gs *state.GlobalState, test *loadedAndConfiguredTest, conf *cloudapi.Config, +) error { + // The log-push config is env-supplied for an external run and likewise not + // env-bound on the Config, so read it explicitly here (before the checks + // below, which require the token when a logs URL is set). + if err := applyExternalLogsConfig(gs, conf); err != nil { + return err + } + pushURL := gs.Env["K6_CLOUD_METRICS_PUSH_URL"] + token := gs.Env["K6_CLOUD_TEST_RUN_TOKEN"] + if (pushURL == "") != (token == "") { + return errors.New("both K6_CLOUD_METRICS_PUSH_URL and " + + "K6_CLOUD_TEST_RUN_TOKEN must be set together") + } + // A logs push URL is authenticated with the same scoped token, so reject a + // logs URL supplied without it rather than silently streaming nothing. + if token == "" && conf.LogsPushURL.Valid && conf.LogsPushURL.String != "" { + return errors.New("K6_CLOUD_LOGS_PUSH_URL requires K6_CLOUD_TEST_RUN_TOKEN") + } + if pushURL == "" { + return nil } + conf.MetricsPushURL = null.StringFrom(pushURL) + conf.TestRunToken = null.StringFrom(token) + raw, err := cloudConfToRawMessage(*conf) + if err != nil { + return fmt.Errorf("could not serialize cloud configuration: %w", err) + } + if test.derivedConfig.Collectors == nil { + test.derivedConfig.Collectors = make(map[string]json.RawMessage) + } + test.derivedConfig.Collectors[builtinOutputCloud.String()] = raw + return nil +} + +// externalLogsConfig mirrors the K6_CLOUD_LOGS_* env vars an external +// orchestrator uses to hand the log-push settings to a local k6. These are +// read explicitly (the Logs* fields are not env-bound on cloudapi.Config) so a +// stray env value can't override the run-scoped logs config in the +// self-provisioned flow. +type externalLogsConfig struct { + PushURL null.String `envconfig:"K6_CLOUD_LOGS_PUSH_URL"` + Level null.String `envconfig:"K6_CLOUD_LOGS_LEVEL"` + Limit null.Int `envconfig:"K6_CLOUD_LOGS_LIMIT"` + PushPeriod types.NullDuration `envconfig:"K6_CLOUD_LOGS_PUSH_PERIOD"` + MsgMaxSize null.Int `envconfig:"K6_CLOUD_LOGS_MESSAGE_MAX_SIZE"` + AllowedLabels []string `envconfig:"K6_CLOUD_LOGS_ALLOWED_LABELS"` +} + +// applyExternalLogsConfig reads the K6_CLOUD_LOGS_* env vars into conf for the +// externally-provisioned flow, where there is no provisioning response to +// supply them. It uses the same envconfig parsing as GetConsolidatedConfig, so +// malformed values fail the same way. +func applyExternalLogsConfig(gs *state.GlobalState, conf *cloudapi.Config) error { + var elc externalLogsConfig + if err := envconfig.Process("", &elc, func(key string) (string, bool) { + v, ok := gs.Env[key] + return v, ok + }); err != nil { + return err + } + conf.LogsPushURL = elc.PushURL + conf.LogsLevel = elc.Level + conf.LogsLimit = elc.Limit + conf.LogsPushPeriod = elc.PushPeriod + conf.LogsMessageMaxSize = elc.MsgMaxSize + conf.LogsAllowedLabels = elc.AllowedLabels return nil } @@ -215,3 +331,115 @@ func resolveCloudTestName(name null.String, scriptPath string) (null.String, err } return name, nil } + +// buildConfigFromRuntimeConfig maps provisioning RuntimeConfig fields +// to cloudapi.Config fields. +// +// If the backend returns an unparseable duration string, a warning is +// logged and the cloudapi.Config default is kept. Graceful degradation +// is preferable to blocking the user for a backend-side bug. +func buildConfigFromRuntimeConfig( + logger logrus.FieldLogger, rc provisioning.RuntimeConfig, +) cloudapi.Config { + cfg := cloudapi.Config{} + + // metrics.push_interval (string nullable) → MetricPushInterval (NullDuration) + if v := rc.Metrics.PushInterval; v != nil { + if d, err := time.ParseDuration(*v); err == nil { + cfg.MetricPushInterval = types.NewNullDuration(d, true) + } else { + logger.WithError(err).WithField("field", "push_interval"). + Warn("invalid duration in runtime_config; using default") + } + } + // metrics.push_concurrency (int32 nullable) → MetricPushConcurrency (null.Int) + if v := rc.Metrics.PushConcurrency; v != nil { + cfg.MetricPushConcurrency = null.IntFrom(int64(*v)) + } + // metrics.aggregation_period (string nullable) → AggregationPeriod (NullDuration) + if v := rc.Metrics.AggregationPeriod; v != nil { + if d, err := time.ParseDuration(*v); err == nil { + cfg.AggregationPeriod = types.NewNullDuration(d, true) + } else { + logger.WithError(err).WithField("field", "aggregation_period"). + Warn("invalid duration in runtime_config; using default") + } + } + // metrics.aggregation_wait_period (string nullable) → AggregationWaitPeriod + if v := rc.Metrics.AggregationWaitPeriod; v != nil { + if d, err := time.ParseDuration(*v); err == nil { + cfg.AggregationWaitPeriod = types.NewNullDuration(d, true) + } else { + logger.WithError(err).WithField("field", "aggregation_wait_period"). + Warn("invalid duration in runtime_config; using default") + } + } + // metrics.max_samples_per_package (int32 nullable) → MaxTimeSeriesInBatch + // (semantically the same field — per-batch series cap) + if v := rc.Metrics.MaxSamplesPerPackage; v != nil { + cfg.MaxTimeSeriesInBatch = null.IntFrom(int64(*v)) + } + + // rc.Metrics.AggregationMinSamples is intentionally not mapped. + // expv2 has no min-samples aggregation knob; adding one would + // require changes to output/cloud/expv2/{collect.go, flush.go}. + + // logs.push_url (string) → LogsPushURL + if v := rc.Logs.PushURL; v != "" { + cfg.LogsPushURL = null.StringFrom(v) + } + // logs.level (string) → LogsLevel + if v := rc.Logs.Level; v != "" { + cfg.LogsLevel = null.StringFrom(v) + } + // logs.limit (int32) → LogsLimit + if v := rc.Logs.Limit; v != 0 { + cfg.LogsLimit = null.IntFrom(int64(v)) + } + // logs.push_period_seconds (string) → LogsPushPeriod (NullDuration) + if v := rc.Logs.PushPeriodSeconds; v != "" { + if d, err := time.ParseDuration(v); err == nil { + cfg.LogsPushPeriod = types.NewNullDuration(d, true) + } else { + logger.WithError(err).WithField("field", "push_period_seconds"). + Warn("invalid duration in runtime_config; using default") + } + } + // logs.message_max_size (int32) → LogsMessageMaxSize + if v := rc.Logs.MessageMaxSize; v != 0 { + cfg.LogsMessageMaxSize = null.IntFrom(int64(v)) + } + // logs.allowed_labels ([]string) → LogsAllowedLabels + if v := rc.Logs.AllowedLabels; len(v) > 0 { + cfg.LogsAllowedLabels = v + } + + return cfg +} + +// configureCloudLogPusher points the registered cloud log pusher at the +// run. It is a no-op unless the pusher was registered (i.e. +// --local-execution without --no-cloud-logs). +func configureCloudLogPusher(gs *state.GlobalState, conf cloudapi.Config, testRunID string) { + if gs.CloudLogPusher == nil { + return + } + gs.CloudLogPusher.SetConfig(logPusherConfig(conf, testRunID)) +} + +// logPusherConfig translates the resolved cloudapi.Config log-push fields +// into the cloud log pusher's config. testRunID is the run identifier used +// as the required test_run_id stream label: result.TestRunID in the +// self-provisioned flow, PushRefID in the externally-provisioned flow. +func logPusherConfig(conf cloudapi.Config, testRunID string) cloudlog.Config { + return cloudlog.Config{ + PushURL: conf.LogsPushURL.String, + Token: conf.TestRunToken.String, + TestRunID: testRunID, + Level: conf.LogsLevel.String, + Limit: int(conf.LogsLimit.Int64), + PushPeriod: conf.LogsPushPeriod.TimeDuration(), + MsgMaxSize: int(conf.LogsMessageMaxSize.Int64), + AllowedLabels: conf.LogsAllowedLabels, + } +} diff --git a/internal/cmd/outputs_cloud_test.go b/internal/cmd/outputs_cloud_test.go new file mode 100644 index 000000000..28e43dbb5 --- /dev/null +++ b/internal/cmd/outputs_cloud_test.go @@ -0,0 +1,279 @@ +//go:build !lean + +package cmd + +import ( + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/cloudapi" + "go.k6.io/k6/v2/cmd/state" + "go.k6.io/k6/v2/internal/cloudapi/provisioning" + "go.k6.io/k6/v2/internal/lib/testutils" + cloudlog "go.k6.io/k6/v2/internal/log/cloud" + "go.k6.io/k6/v2/lib/types" +) + +func TestBuildConfigFromRuntimeConfig(t *testing.T) { + t.Parallel() + + pushInterval := "2s" + var pushConcurrency int32 = 5 + aggPeriod := "3s" + aggWaitPeriod := "1s" + var aggMinSamples int32 = 50 + var maxSamples int32 = 2000 + + rc := provisioning.RuntimeConfig{ + Metrics: provisioning.MetricsConfig{ + PushURL: "https://ingest.example.com/v1/metrics", + PushInterval: &pushInterval, + PushConcurrency: &pushConcurrency, + AggregationPeriod: &aggPeriod, + AggregationWaitPeriod: &aggWaitPeriod, + AggregationMinSamples: &aggMinSamples, + MaxSamplesPerPackage: &maxSamples, + }, + TestRunToken: "test-token", + Secrets: provisioning.SecretsConfig{ + Endpoint: "https://secrets.example.com", + ResponsePath: "plaintext", + }, + Logs: provisioning.LogsConfig{ + PushURL: "https://logs.example.com/push", + Level: "info", + Limit: 900, + PushPeriodSeconds: "3s", + MessageMaxSize: 10000, + AllowedLabels: []string{"lz", "level", "test_run_id"}, + }, + } + + logger := testutils.NewLogger(t) + cfg := buildConfigFromRuntimeConfig(logger, rc) + + // push_interval → MetricPushInterval + assert.True(t, cfg.MetricPushInterval.Valid) + assert.Equal(t, types.NewNullDuration(2*time.Second, true), cfg.MetricPushInterval) + + // push_concurrency → MetricPushConcurrency + assert.True(t, cfg.MetricPushConcurrency.Valid) + assert.Equal(t, null.IntFrom(5), cfg.MetricPushConcurrency) + + // aggregation_period → AggregationPeriod + assert.True(t, cfg.AggregationPeriod.Valid) + assert.Equal(t, types.NewNullDuration(3*time.Second, true), cfg.AggregationPeriod) + + // aggregation_wait_period → AggregationWaitPeriod + assert.True(t, cfg.AggregationWaitPeriod.Valid) + assert.Equal(t, types.NewNullDuration(1*time.Second, true), cfg.AggregationWaitPeriod) + + // max_samples_per_package → MaxTimeSeriesInBatch + assert.True(t, cfg.MaxTimeSeriesInBatch.Valid) + assert.Equal(t, null.IntFrom(2000), cfg.MaxTimeSeriesInBatch) + + // logs.push_url → LogsPushURL + assert.Equal(t, null.StringFrom("https://logs.example.com/push"), cfg.LogsPushURL) + + // logs.level → LogsLevel + assert.Equal(t, null.StringFrom("info"), cfg.LogsLevel) + + // logs.limit → LogsLimit + assert.Equal(t, null.IntFrom(900), cfg.LogsLimit) + + // logs.push_period_seconds → LogsPushPeriod (parsed from "3s") + assert.Equal(t, types.NewNullDuration(3*time.Second, true), cfg.LogsPushPeriod) + + // logs.message_max_size → LogsMessageMaxSize + assert.Equal(t, null.IntFrom(10000), cfg.LogsMessageMaxSize) + + // logs.allowed_labels → LogsAllowedLabels + assert.Equal(t, []string{"lz", "level", "test_run_id"}, cfg.LogsAllowedLabels) +} + +func TestBuildConfigFromRuntimeConfig_InvalidDurationLogsWarning(t *testing.T) { + t.Parallel() + + badDuration := "not a duration" + rc := provisioning.RuntimeConfig{ + Metrics: provisioning.MetricsConfig{ + PushInterval: &badDuration, + }, + } + + logger := logrus.New() + logger.SetLevel(logrus.DebugLevel) + hook := testutils.NewLogHook() + logger.AddHook(hook) + + cfg := buildConfigFromRuntimeConfig(logger, rc) + + // MetricPushInterval should be left unset (invalid) when parse fails. + assert.False(t, cfg.MetricPushInterval.Valid, "expected MetricPushInterval to remain unset for unparseable duration") + + // A warning should have been logged. + entries := hook.Drain() + require.NotEmpty(t, entries, "expected at least one log entry for invalid duration") + + found := false + for _, e := range entries { + if e.Level == logrus.WarnLevel && e.Data["field"] == "push_interval" { + found = true + break + } + } + assert.True(t, found, "expected a warning log entry for field push_interval") +} + +func TestBuildConfigFromRuntimeConfig_InvalidLogsPushPeriodLogsWarning(t *testing.T) { + t.Parallel() + + rc := provisioning.RuntimeConfig{ + Logs: provisioning.LogsConfig{ + PushPeriodSeconds: "not a duration", + }, + } + + logger := logrus.New() + logger.SetLevel(logrus.DebugLevel) + hook := testutils.NewLogHook() + logger.AddHook(hook) + + cfg := buildConfigFromRuntimeConfig(logger, rc) + + // LogsPushPeriod should be left unset (invalid) when parse fails. + assert.False(t, cfg.LogsPushPeriod.Valid, "expected LogsPushPeriod to remain unset for unparseable duration") + + // A warning should have been logged (not an error). + entries := hook.Drain() + require.NotEmpty(t, entries, "expected at least one log entry for invalid duration") + + found := false + for _, e := range entries { + assert.NotEqual(t, logrus.ErrorLevel, e.Level, "invalid duration must warn, not error") + if e.Level == logrus.WarnLevel && e.Data["field"] == "push_period_seconds" { + found = true + } + } + assert.True(t, found, "expected a warning log entry for field push_period_seconds") +} + +func TestLogPusherConfig(t *testing.T) { + t.Parallel() + + conf := cloudapi.Config{ + LogsPushURL: null.StringFrom("https://logs.example.com/push"), + TestRunToken: null.StringFrom("scoped-token"), + LogsLevel: null.StringFrom("warn"), + LogsLimit: null.IntFrom(500), + LogsPushPeriod: types.NewNullDuration(2*time.Second, true), + LogsMessageMaxSize: null.IntFrom(2048), + LogsAllowedLabels: []string{"lz", "test_run_id"}, + } + + got := logPusherConfig(conf, "42") + + assert.Equal(t, cloudlog.Config{ + PushURL: "https://logs.example.com/push", + Token: "scoped-token", + TestRunID: "42", + Level: "warn", + Limit: 500, + PushPeriod: 2 * time.Second, + MsgMaxSize: 2048, + AllowedLabels: []string{"lz", "test_run_id"}, + }, got) +} + +func TestBuildConfigFromRuntimeConfig_NilFieldsLeftUnset(t *testing.T) { + t.Parallel() + + rc := provisioning.RuntimeConfig{ + Metrics: provisioning.MetricsConfig{ + PushURL: "https://ingest.example.com/v1/metrics", + // All nullable fields left nil. + }, + } + + logger := testutils.NewLogger(t) + cfg := buildConfigFromRuntimeConfig(logger, rc) + + assert.False(t, cfg.MetricPushInterval.Valid, "MetricPushInterval should be unset when input is nil") + assert.False(t, cfg.MetricPushConcurrency.Valid, "MetricPushConcurrency should be unset when input is nil") + assert.False(t, cfg.AggregationPeriod.Valid, "AggregationPeriod should be unset when input is nil") + assert.False(t, cfg.AggregationWaitPeriod.Valid, "AggregationWaitPeriod should be unset when input is nil") + assert.False(t, cfg.MaxTimeSeriesInBatch.Valid, "MaxTimeSeriesInBatch should be unset when input is nil") + + // The logs fields must also stay unset when provisioning omits them, so a + // stray K6_CLOUD_LOGS_* env value cannot survive Apply in the + // self-provisioned flow (the fields are programmatic-only). + assert.False(t, cfg.LogsPushURL.Valid, "LogsPushURL should be unset when input is nil") + assert.False(t, cfg.LogsLevel.Valid, "LogsLevel should be unset when input is nil") + assert.False(t, cfg.LogsLimit.Valid, "LogsLimit should be unset when input is nil") + assert.False(t, cfg.LogsPushPeriod.Valid, "LogsPushPeriod should be unset when input is nil") + assert.False(t, cfg.LogsMessageMaxSize.Valid, "LogsMessageMaxSize should be unset when input is nil") + assert.Empty(t, cfg.LogsAllowedLabels, "LogsAllowedLabels should be unset when input is nil") +} + +func TestBuildConfigFromRuntimeConfig_AggregationMinSamplesIgnored(t *testing.T) { + t.Parallel() + + var aggMinSamples int32 = 100 + rc := provisioning.RuntimeConfig{ + Metrics: provisioning.MetricsConfig{ + AggregationMinSamples: &aggMinSamples, + }, + } + + logger := logrus.New() + logger.SetLevel(logrus.DebugLevel) + hook := testutils.NewLogHook() + logger.AddHook(hook) + + cfg := buildConfigFromRuntimeConfig(logger, rc) + + // No Config field should have been set. + assert.False(t, cfg.MetricPushInterval.Valid) + assert.False(t, cfg.MetricPushConcurrency.Valid) + assert.False(t, cfg.AggregationPeriod.Valid) + assert.False(t, cfg.AggregationWaitPeriod.Valid) + assert.False(t, cfg.MaxTimeSeriesInBatch.Valid) + + // No warning or error should be logged (intentional drop, not an error). + entries := hook.Drain() + for _, e := range entries { + assert.NotEqual(t, logrus.WarnLevel, e.Level, "unexpected warning logged for intentionally dropped field") + assert.NotEqual(t, logrus.ErrorLevel, e.Level, "unexpected error logged for intentionally dropped field") + } +} + +// TestApplyExternalLogsConfig verifies the externally-provisioned flow reads +// the log-push knobs from the K6_CLOUD_LOGS_* env vars into the config (the +// fields are not env-bound on cloudapi.Config, so cmd reads them explicitly). +func TestApplyExternalLogsConfig(t *testing.T) { + t.Parallel() + + gs := &state.GlobalState{Env: map[string]string{ + "K6_CLOUD_LOGS_PUSH_URL": "https://api.k6.io/logs/v1/test_runs/42", + "K6_CLOUD_LOGS_LEVEL": "info", + "K6_CLOUD_LOGS_LIMIT": "900", + "K6_CLOUD_LOGS_PUSH_PERIOD": "3s", + "K6_CLOUD_LOGS_MESSAGE_MAX_SIZE": "10000", + "K6_CLOUD_LOGS_ALLOWED_LABELS": "lz,level,test_run_id", + }} + + var conf cloudapi.Config + require.NoError(t, applyExternalLogsConfig(gs, &conf)) + + assert.Equal(t, null.StringFrom("https://api.k6.io/logs/v1/test_runs/42"), conf.LogsPushURL) + assert.Equal(t, null.StringFrom("info"), conf.LogsLevel) + assert.Equal(t, null.IntFrom(900), conf.LogsLimit) + assert.Equal(t, types.NewNullDuration(3*time.Second, true), conf.LogsPushPeriod) + assert.Equal(t, null.IntFrom(10000), conf.LogsMessageMaxSize) + assert.Equal(t, []string{"lz", "level", "test_run_id"}, conf.LogsAllowedLabels) +} diff --git a/internal/cmd/outputs_full.go b/internal/cmd/outputs_full.go index 2efed473e..ae701b283 100644 --- a/internal/cmd/outputs_full.go +++ b/internal/cmd/outputs_full.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" + "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/ext" "go.k6.io/k6/v2/internal/dashboard" "go.k6.io/k6/v2/internal/output/breakingit" @@ -45,7 +46,7 @@ func getAllOutputConstructors() (map[string]output.Constructor, error) { builtinOutputExperimentalPrometheusRW.String(): func(params output.Params) (output.Output, error) { return remotewrite.New(params) }, - "web-dashboard": dashboard.New, + "web-dashboard": dashboard.New, builtinOutputTimescaledbBreakingit.String(): breakingit.New, builtinOutputExperimentalOpentelemetry.String(): func(params output.Params) (output.Output, error) { params.Logger.Warnf("OpenTelemetry output has been graduated as a stable output."+ @@ -73,3 +74,11 @@ func getAllOutputConstructors() (map[string]output.Constructor, error) { return result, nil } + +// attachCloudLogDrainer gives the cloud output the local-execution log pusher +// so it flushes buffered logs before the run is notified complete. +func attachCloudLogDrainer(out output.Output, gs *state.GlobalState) { + if co, ok := out.(*cloud.Output); ok && gs.CloudLogPusher != nil { + co.SetLogDrainer(gs.CloudLogPusher) + } +} diff --git a/internal/cmd/outputs_lean.go b/internal/cmd/outputs_lean.go index 839358e82..c58dc328a 100644 --- a/internal/cmd/outputs_lean.go +++ b/internal/cmd/outputs_lean.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" + "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/ext" "go.k6.io/k6/v2/internal/output/breakingit" "go.k6.io/k6/v2/internal/output/csv" @@ -71,3 +72,5 @@ func getAllOutputConstructors() (map[string]output.Constructor, error) { return result, nil } + +func attachCloudLogDrainer(_ output.Output, _ *state.GlobalState) {} diff --git a/internal/cmd/report.go b/internal/cmd/report.go index 58062ca53..111e26168 100644 --- a/internal/cmd/report.go +++ b/internal/cmd/report.go @@ -7,20 +7,42 @@ import ( "net/http" "runtime" "strconv" + "time" + "go.k6.io/k6/v2/cmd/state" "go.k6.io/k6/v2/internal/build" "go.k6.io/k6/v2/internal/execution" "go.k6.io/k6/v2/internal/usage" ) +// envLookup adapts an environment map to the lookup-function shape the report +// helpers share with the run path's LookupEnv. +func envLookup(env map[string]string) func(string) (string, bool) { + return func(key string) (string, bool) { + val, ok := env[key] + return val, ok + } +} + +// addEnvironmentInfo stamps the fields identifying this k6 binary and where it runs. +func addEnvironmentInfo(m map[string]any, lookupEnv func(string) (string, bool)) { + m["k6_version"] = build.Version + m["goos"] = runtime.GOOS + m["goarch"] = runtime.GOARCH + m["is_ci"] = isCI(lookupEnv) +} + +// createReport assembles the anonymous usage report for a run from its scheduler +// and recorded usage. Extension identities are intentionally not collected. func createReport(u *usage.Usage, execScheduler *execution.Scheduler) map[string]any { execState := execScheduler.GetState() m := u.Map() + // The fork deliberately excludes detailed extension-usage reporting. + delete(m, "extensions") + + addEnvironmentInfo(m, execState.Test.LookupEnv) - m["k6_version"] = build.Version m["duration"] = execState.GetCurrentTestRunDuration().String() - m["goos"] = runtime.GOOS - m["goarch"] = runtime.GOARCH m["vus_max"] = uint64(execState.GetInitializedVUsCount()) //nolint:gosec m["iterations"] = execState.GetFullIterationCount() executors := make(map[string]int) @@ -28,26 +50,45 @@ func createReport(u *usage.Usage, execScheduler *execution.Scheduler) map[string executors[ec.GetType()]++ } m["executors"] = executors - m["is_ci"] = isCI(execState.Test.LookupEnv) return m } -func reportUsage(ctx context.Context, execScheduler *execution.Scheduler, test *loadedAndConfiguredTest) error { - m := createReport(test.preInitState.Usage, execScheduler) +// reportUsage sends the report built by create, logging the attempt and outcome +// at debug, all bounded by a timeout. +func reportUsage(ctx context.Context, gs *state.GlobalState, create func(ctx context.Context) map[string]any) { + reportCtx, cancel := context.WithTimeout(ctx, 3*time.Second) + defer cancel() + + gs.Logger.Debug("Sending usage report...") + if err := postUsageReport(reportCtx, envLookup(gs.Env), create(reportCtx)); err != nil { + gs.Logger.WithError(err).Debug("Error sending usage report") + } else { + gs.Logger.Debug("Usage report sent successfully") + } +} + +// defaultUsageReportURL is the production endpoint the anonymous usage report +// is sent to when K6_USAGE_REPORT_URL is not set. +const defaultUsageReportURL = "https://stats.grafana.org/k6-usage-report" + +func postUsageReport(ctx context.Context, lookupEnv func(string) (string, bool), m map[string]any) error { body, err := json.Marshal(m) if err != nil { return err } - const usageStatsURL = "https://stats.grafana.org/k6-usage-report" + usageStatsURL := defaultUsageReportURL + if url, ok := lookupEnv(state.UsageReportURL); ok && url != "" { + usageStatsURL = url + } req, err := http.NewRequestWithContext(ctx, http.MethodPost, usageStatsURL, bytes.NewBuffer(body)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") - res, err := http.DefaultClient.Do(req) //nolint:gosec + res, err := http.DefaultClient.Do(req) if err == nil { _ = res.Body.Close() } diff --git a/internal/cmd/report_privacy_test.go b/internal/cmd/report_privacy_test.go new file mode 100644 index 000000000..8659e8413 --- /dev/null +++ b/internal/cmd/report_privacy_test.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/cmd/state" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestReportUsageDoesNotIdentifyInstallation(t *testing.T) { + t.Parallel() + + for _, storedID := range []string{"", "123e4567-e89b-42d3-a456-426614174000"} { + t.Run(storedID, func(t *testing.T) { + t.Parallel() + reports := make(chan map[string]any, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var report map[string]any + if err := json.NewDecoder(r.Body).Decode(&report); err != nil { + t.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + reports <- report + w.WriteHeader(http.StatusNoContent) + })) + defer server.Close() + + gs := &state.GlobalState{ + FS: fsext.NewMemMapFs(), UserOSConfigDir: "/config", + Logger: logrus.New(), + Env: map[string]string{state.UsageReportURL: server.URL}, + } + idPath := filepath.Join(gs.UserOSConfigDir, "k6", "installation-id") + if storedID != "" { + require.NoError(t, gs.FS.MkdirAll(filepath.Dir(idPath), 0o700)) + require.NoError(t, fsext.WriteFile(gs.FS, idPath, []byte(storedID), 0o600)) + } + reportUsage(context.Background(), gs, func(context.Context) map[string]any { + report := map[string]any{"iterations": 1} + addEnvironmentInfo(report, envLookup(gs.Env)) + return report + }) + + select { + case report := <-reports: + assert.Equal(t, float64(1), report["iterations"]) + assert.NotContains(t, report, "installation_id") + assert.NotContains(t, report, "build_origin") + assert.NotContains(t, report, "extensions") + default: + t.Fatal("usage report was not received") + } + if storedID == "" { + exists, err := fsext.Exists(gs.FS, idPath) + require.NoError(t, err) + assert.False(t, exists, "must not create an installation identifier") + } else { + stored, err := fsext.ReadFile(gs.FS, idPath) + require.NoError(t, err) + assert.Equal(t, storedID, string(stored)) + } + }) + } +} diff --git a/internal/cmd/report_test.go b/internal/cmd/report_test.go index b93dabe72..42d517365 100644 --- a/internal/cmd/report_test.go +++ b/internal/cmd/report_test.go @@ -50,8 +50,9 @@ func TestCreateReport(t *testing.T) { time.Sleep(10 * time.Millisecond) s.GetState().MarkEnded() - m := createReport(usage.New(), s) - require.NoError(t, err) + u := usage.New() + require.NoError(t, u.Values("extensions", map[string]string{"module": "example.invalid/private"})) + m := createReport(u, s) assert.Equal(t, build.Version, m["k6_version"]) assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"]) @@ -59,6 +60,7 @@ func TestCreateReport(t *testing.T) { assert.EqualValues(t, 170, m["iterations"]) assert.NotEqual(t, "0s", m["duration"]) assert.EqualValues(t, false, m["is_ci"]) + assert.NotContains(t, m, "extensions") }) t.Run("CI=false", func(t *testing.T) { @@ -73,7 +75,6 @@ func TestCreateReport(t *testing.T) { require.NoError(t, err) m := createReport(usage.New(), s) - require.NoError(t, err) assert.Equal(t, build.Version, m["k6_version"]) assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"]) @@ -95,7 +96,6 @@ func TestCreateReport(t *testing.T) { require.NoError(t, err) m := createReport(usage.New(), s) - require.NoError(t, err) assert.Equal(t, build.Version, m["k6_version"]) assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"]) diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 380cef6cf..9e4dd9d10 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -84,10 +84,11 @@ func ExecuteWithGlobalState(gs *state.GlobalState) { type rootCommand struct { globalState *state.GlobalState - cmd *cobra.Command - stopLoggersCh chan struct{} - loggersWg sync.WaitGroup - loggerIsRemote bool + cmd *cobra.Command + stopLoggersCh chan struct{} + loggersWg sync.WaitGroup + loggerIsRemote bool + enableCloudLogPush bool } // newRootCommand creates a root command with a default launcher @@ -148,14 +149,20 @@ func (c *rootCommand) persistentPreRunE(cmd *cobra.Command, _ []string) error { return errext.WithExitCodeIfNone(errAlreadyReported, exitcodes.InvalidConfig) } - // For 'k6 cloud run --local-execution', automatically register the cloud secret source - // so scripts can call secrets.get() without any extra flags. - // This must happen before setupLoggers, which calls createSecretSources internally. + // For 'k6 cloud run --local-execution', wire up the cloud secret source and + // the cloud log push. Both must happen before setupLoggers, which calls + // createSecretSources and registers the log pusher on the logger. if isCloudRunWithLocalExecution(cmd) { - f := cmd.Flag("no-cloud-secrets") - if f == nil || f.Value.String() != "true" { + // Register the cloud secret source so scripts can call secrets.get() + // without any extra flags. + if f := cmd.Flag("no-cloud-secrets"); f == nil || f.Value.String() != "true" { c.globalState.Flags.SecretSource = append(c.globalState.Flags.SecretSource, "cloud") } + + // Push k6's own logs to the cloud unless --no-cloud-logs is set; the + // decision is applied in setupLoggers. + f := cmd.Flag("no-cloud-logs") + c.enableCloudLogPush = f == nil || f.Value.String() != "true" } err := c.setupLoggers(c.stopLoggersCh) @@ -209,8 +216,7 @@ func (c *rootCommand) execute() { return } - var ecerr errext.HasExitCode - if errors.As(err, &ecerr) { + if ecerr, ok := errors.AsType[errext.HasExitCode](err); ok { exitCode = int(ecerr.ExitCode()) } @@ -250,8 +256,7 @@ func handleUnsatisfiedDependencies(err error, c *rootCommand) (exitcodes.ExitCod err = customBinary.run(c.globalState.Ctx, c.globalState) // this only happens if we actually ran the binary and it exited afterwads, in which case we propagate the exit code - var ecerr errext.HasExitCode - if errors.As(err, &ecerr) { + if ecerr, ok := errors.AsType[errext.HasExitCode](err); ok { return ecerr.ExitCode(), err } @@ -305,6 +310,10 @@ func rootCmdPersistentFlagSet(gs *state.GlobalState) *pflag.FlagSet { flags.BoolVar(&gs.Flags.NoColor, "no-color", gs.Flags.NoColor, "disable colored output") flags.Lookup("no-color").DefValue = strconv.FormatBool(gs.DefaultFlags.NoColor) + flags.BoolVar(&gs.Flags.LogNanosecondTimestamps, "log-ns-timestamps", + gs.Flags.LogNanosecondTimestamps, "switch log timestamp precision from second to nanosecond") + flags.Lookup("log-ns-timestamps").DefValue = strconv.FormatBool(gs.DefaultFlags.LogNanosecondTimestamps) + // TODO: support configuring these through environment variables as well? // either with croconf or through the hack above... flags.BoolVarP(&gs.Flags.Verbose, "verbose", "v", gs.DefaultFlags.Verbose, "enable verbose logging") @@ -376,19 +385,7 @@ func (c *rootCommand) setupLoggers(stop <-chan struct{}) error { return fmt.Errorf("unsupported log output '%s'", line) } - switch c.globalState.Flags.LogFormat { - case "raw": - c.globalState.Logger.SetFormatter(&RawFormatter{}) - c.globalState.Logger.Debug("Logger format: RAW") - case "json": - c.globalState.Logger.SetFormatter(&logrus.JSONFormatter{}) - c.globalState.Logger.Debug("Logger format: JSON") - default: - c.globalState.Logger.SetFormatter(&logrus.TextFormatter{ - ForceColors: loggerForceColors, DisableColors: c.globalState.Flags.NoColor, - }) - c.globalState.Logger.Debug("Logger format: TEXT") - } + setupLogFormat(c, loggerForceColors) secretsources, err := createSecretSources(c.globalState) if err != nil { @@ -413,6 +410,10 @@ func (c *rootCommand) setupLoggers(stop <-chan struct{}) error { c.setLoggerHook(ctx, hook) } + // Attach the cloud log pusher after the secrets-redaction hook so it only + // ever observes redacted entries (see setupCloudLogPusher). + c.setupCloudLogPusher(stop) + // Sometimes the Go runtime uses the standard log output to // log some messages directly. // It does when an invalid char is found in a Cookie. @@ -427,6 +428,28 @@ func (c *rootCommand) setupLoggers(stop <-chan struct{}) error { return nil } +func setupLogFormat(c *rootCommand, loggerForceColors bool) { + timestampFormat := "" + if c.globalState.Flags.LogNanosecondTimestamps { + timestampFormat = time.RFC3339Nano + } + + switch c.globalState.Flags.LogFormat { + case "raw": + c.globalState.Logger.SetFormatter(&RawFormatter{}) + c.globalState.Logger.Debug("Logger format: RAW") + case "json": + c.globalState.Logger.SetFormatter(&logrus.JSONFormatter{TimestampFormat: timestampFormat}) + c.globalState.Logger.Debug("Logger format: JSON") + default: + c.globalState.Logger.SetFormatter(&logrus.TextFormatter{ + ForceColors: loggerForceColors, DisableColors: c.globalState.Flags.NoColor, + TimestampFormat: timestampFormat, + }) + c.globalState.Logger.Debug("Logger format: TEXT") + } +} + func (c *rootCommand) setLoggerHook(ctx context.Context, h log.AsyncHook) { c.loggersWg.Go(func() { h.Listen(ctx) diff --git a/internal/cmd/root_cloud_full.go b/internal/cmd/root_cloud_full.go new file mode 100644 index 000000000..4254ef4fc --- /dev/null +++ b/internal/cmd/root_cloud_full.go @@ -0,0 +1,26 @@ +//go:build !lean + +package cmd + +import ( + "context" + + cloudlog "go.k6.io/k6/v2/internal/log/cloud" +) + +// setupCloudLogPusher registers the cloud log pusher on the logger and starts +// its listener. It runs only for full builds and only when cloud log push is enabled. +func (c *rootCommand) setupCloudLogPusher(stop <-chan struct{}) { + if !c.enableCloudLogPush { + return + } + p := cloudlog.New(c.globalState.FallbackLogger) + c.globalState.CloudLogPusher = p + c.globalState.Logger.AddHook(p) + pctx, pcancel := context.WithCancel(context.Background()) + c.loggersWg.Go(func() { p.Listen(pctx) }) + c.loggersWg.Go(func() { + <-stop + pcancel() + }) +} diff --git a/internal/cmd/root_cloud_lean.go b/internal/cmd/root_cloud_lean.go new file mode 100644 index 000000000..2b94b9322 --- /dev/null +++ b/internal/cmd/root_cloud_lean.go @@ -0,0 +1,5 @@ +//go:build lean + +package cmd + +func (c *rootCommand) setupCloudLogPusher(_ <-chan struct{}) {} diff --git a/internal/cmd/root_test.go b/internal/cmd/root_test.go index 3b5650b2f..1a568f254 100644 --- a/internal/cmd/root_test.go +++ b/internal/cmd/root_test.go @@ -60,6 +60,9 @@ func TestRootCommandHelpDisplayCommands(t *testing.T) { ts.CmdArgs = []string{"k6", "help"} newRootCommand(ts.GlobalState).execute() + if leanBuild && tc.name == "should have cloud command" { + tc.wantStdoutContains = "cloud Cloud functionality is not available in this lean build" + } if tc.wantStdoutContains != "" { assert.Contains(t, ts.Stdout.String(), tc.wantStdoutContains) } diff --git a/internal/cmd/run.go b/internal/cmd/run.go index 0d0cdc3b2..5c9054ef6 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -72,6 +72,18 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { globalCtx, globalCancel := context.WithCancel(c.gs.Ctx) defer globalCancel() + // shutdownTracerProvider is set once the tracer provider is created below. + // It must run after the Exit event has been emitted and handled (e.g. by + // the browser module ending any open spans), but before globalCtx is + // cancelled by globalCancel above, since it derives its own timeout from + // globalCtx. + var shutdownTracerProvider func() + defer func() { + if shutdownTracerProvider != nil { + shutdownTracerProvider() + } + }() + // lingerCtx is cancelled by Ctrl+C, and is used to wait for that event when // k6 was started with the --linger option. lingerCtx, lingerCancel := context.WithCancel(globalCtx) @@ -118,7 +130,7 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { if err = c.setupTracerProvider(globalCtx, test); err != nil { return err } - waitTracesFlushed := func() { + shutdownTracerProvider = func() { ctx, cancel := context.WithTimeout(globalCtx, waitForTracerProviderStopTimeout) defer cancel() if tpErr := test.preInitState.TracerProvider.Shutdown(ctx); tpErr != nil { @@ -220,10 +232,8 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { } summaryMeta := summary.Meta{ - Script: string(test.source.Data), - IsCloud: slices.ContainsFunc(outputs, func(o output.Output) bool { - return isCloudOutput(o) - }), + Script: string(test.source.Data), + IsCloud: slices.ContainsFunc(outputs, isCloudOutput), } // Instantiates the summary output @@ -375,24 +385,10 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { } defer func() { - logger.Debug("Waiting for metrics and traces processing to finish...") + logger.Debug("Waiting for metrics processing to finish...") close(samples) - - ww := [...]func(){ - waitOutputsFlushed, - waitTracesFlushed, - } - var wg sync.WaitGroup - wg.Add(len(ww)) - for _, w := range ww { - go func() { - w() - wg.Done() - }() - } - wg.Wait() - - logger.Debug("Metrics and traces processing finished!") + waitOutputsFlushed() + logger.Debug("Metrics processing finished!") }() printExecutionDescription( @@ -458,15 +454,9 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { // usage report after the context is done. if !conf.NoUsageReport.Bool { backgroundProcesses.Go(func() { - reportCtx, reportCancel := context.WithTimeout(globalCtx, 3*time.Second) - defer reportCancel() - logger.Debug("Sending usage report...") - - if rerr := reportUsage(reportCtx, execScheduler, test); rerr != nil { - logger.WithError(rerr).Debug("Error sending usage report") - } else { - logger.Debug("Usage report sent successfully") - } + reportUsage(globalCtx, c.gs, func(context.Context) map[string]any { + return createReport(test.preInitState.Usage, execScheduler) + }) }) } @@ -474,6 +464,7 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { if err != nil { err = common.UnwrapSobekInterruptedError(err) logger.WithError(err).Debug("Test finished with an error") + setExecutionResult(executionState, err) return err } @@ -487,17 +478,31 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) { // ensure we return an error indicating that the test run was marked as failed, and the proper // exit code is used. if testRunState.TestStatus.Failed() { - return errext.WithExitCodeIfNone( + err = errext.WithExitCodeIfNone( fmt.Errorf("test run was marked as failed"), exitcodes.MarkedAsFailed, ) + setExecutionResult(executionState, err) + return err } logger.Debug("Test finished cleanly") + setExecutionResult(executionState, nil) return nil } +func setExecutionResult(executionState *lib.ExecutionState, err error) { + exitCode := 0 + if err != nil { + exitCode = -1 + if ecerr, ok := errors.AsType[errext.HasExitCode](err); ok { + exitCode = int(ecerr.ExitCode()) + } + } + executionState.SetExecutionResult(exitCode) +} + func getSummaryMode(runtimeOptions lib.RuntimeOptions) (summary.Mode, bool, error) { sm, err := summary.ValidateMode(runtimeOptions.SummaryMode.String) if err != nil { diff --git a/internal/cmd/scenarios.go b/internal/cmd/scenarios.go new file mode 100644 index 000000000..b343419ef --- /dev/null +++ b/internal/cmd/scenarios.go @@ -0,0 +1,115 @@ +package cmd + +import ( + "fmt" + "maps" + "slices" + "strings" + + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" + + "go.k6.io/k6/v2/errext" + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/metrics" +) + +func checkScenarioConflicts(flags *pflag.FlagSet) error { + if !flags.Changed("scenario") { + return nil + } + for _, name := range []string{"vus", "duration", "iterations", "stage"} { + if flags.Changed(name) { + return errext.WithExitCodeIfNone( + fmt.Errorf("--scenario cannot be combined with --%s", name), exitcodes.InvalidConfig) + } + } + return nil +} + +func dropScenarioShortcuts(logger logrus.FieldLogger, layers map[string]*lib.Options) error { + var unset lib.Options + for _, name := range []string{"config", "script", "environment"} { + opts, ok := layers[name] + if !ok { + return fmt.Errorf("missing %q layer", name) + } + + var dropped []string + if opts.VUs.Valid { + dropped = append(dropped, "vus") + } + if opts.Duration.Valid { + dropped = append(dropped, "duration") + } + if opts.Iterations.Valid { + dropped = append(dropped, "iterations") + } + if opts.Stages != nil { + dropped = append(dropped, "stages") + } + opts.VUs = unset.VUs + opts.Duration = unset.Duration + opts.Iterations = unset.Iterations + opts.Stages = unset.Stages + if len(dropped) > 0 { + logger.Warnf("--scenario overrode %s in %q configuration", strings.Join(dropped, ", "), name) + } + } + return nil +} + +func selectScenarios(opts lib.Options, names []string) (lib.Options, error) { + selected := make(lib.ScenarioConfigs, len(names)) + for _, name := range names { + if sc, ok := opts.Scenarios[name]; ok { + selected[name] = sc + continue + } + + available := strings.Join(slices.Sorted(maps.Keys(opts.Scenarios)), ", ") + err := fmt.Errorf("scenario %q not found; available scenarios: %s", name, available) + if len(opts.Scenarios) == 0 { + err = fmt.Errorf("scenario %q not found; the script does not have any named scenario", name) + } + + return opts, errext.WithExitCodeIfNone(err, exitcodes.InvalidConfig) + } + + opts.Scenarios = selected + + return opts, nil +} + +func dropScenarioThresholds(logger logrus.FieldLogger, opts *lib.Options, configured lib.ScenarioConfigs) { + opts.Thresholds = maps.Clone(opts.Thresholds) + var dropped []string + for metricName := range opts.Thresholds { + _, tags, err := metrics.ParseMetricName(metricName) + if err != nil { + // Only --no-thresholds permits malformed filters past threshold validation. + continue + } + // Match AddSubmetric's normalization and last-value-wins handling of repeated tags. + for _, tag := range slices.Backward(tags) { + key, value, _ := strings.Cut(tag, ":") + if strings.Trim(strings.TrimSpace(key), `"'`) != "scenario" { + continue + } + name := strings.Trim(strings.TrimSpace(value), `"'`) + _, exists := configured[name] + _, selected := opts.Scenarios[name] + if exists && !selected { + delete(opts.Thresholds, metricName) + dropped = append(dropped, metricName) + } + break + } + } + if len(dropped) > 0 { + slices.Sort(dropped) + logger.Warnf("--scenario skipped thresholds for excluded scenarios: %s; "+ + "these thresholds remain skipped even if another scenario emits matching tags", strings.Join(dropped, "; ")) + } +} diff --git a/internal/cmd/scenarios_test.go b/internal/cmd/scenarios_test.go new file mode 100644 index 000000000..a76f695e6 --- /dev/null +++ b/internal/cmd/scenarios_test.go @@ -0,0 +1,112 @@ +package cmd + +import ( + "maps" + "slices" + "strings" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/errext" + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/internal/lib/testutils" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/executor" + "go.k6.io/k6/v2/lib/types" + "go.k6.io/k6/v2/metrics" +) + +func TestSelectScenarios(t *testing.T) { + t.Parallel() + + ui := executor.NewConstantVUsConfig("ui") + ui.VUs = null.IntFrom(7) + ui.Duration = types.NullDurationFrom(30 * time.Second) + ui.Exec = null.StringFrom("uiFn") + ui.Env = map[string]string{"GREETING": "hi"} + ui.Tags = map[string]string{"team": "core"} + ui.Options = &lib.ScenarioOptions{Browser: map[string]any{"type": "chromium"}} + + api := executor.NewConstantVUsConfig("api") + + all := lib.ScenarioConfigs{"ui": ui, "api": api, "db": executor.NewConstantVUsConfig("db")} + + res, err := selectScenarios(lib.Options{Scenarios: all}, []string{"ui", "api"}) + require.NoError(t, err) + assert.Equal(t, lib.ScenarioConfigs{"ui": ui, "api": api}, res.Scenarios) + assert.Len(t, all, 3) + + _, err = selectScenarios(lib.Options{Scenarios: all}, []string{"ui", "gone"}) + require.EqualError(t, err, `scenario "gone" not found; available scenarios: api, db, ui`) + var ec errext.HasExitCode + require.ErrorAs(t, err, &ec) + assert.Equal(t, exitcodes.InvalidConfig, ec.ExitCode()) + + _, err = selectScenarios(lib.Options{}, []string{"default"}) + require.EqualError(t, err, `scenario "default" not found; the script does not have any named scenario`) +} + +func TestGetOptionsScenarioShortcuts(t *testing.T) { + t.Parallel() + + for _, tt := range []struct { + flag, value string + allowed bool + }{ + {"vus", "2", false}, + {"duration", "1s", false}, + {"iterations", "2", false}, + {"stage", "1s:2", false}, + {"execution-segment", "0:1/2", true}, + {"execution-segment-sequence", "0,1/2,1", true}, + } { + t.Run(tt.flag, func(t *testing.T) { + t.Parallel() + flags := optionFlagSet() + require.NoError(t, flags.Parse([]string{"--scenario", "ui", "--" + tt.flag, tt.value})) + _, err := getOptions(flags) + if tt.allowed { + require.NoError(t, err) + return + } + require.EqualError(t, err, "--scenario cannot be combined with --"+tt.flag) + var ec errext.HasExitCode + require.ErrorAs(t, err, &ec) + assert.Equal(t, exitcodes.InvalidConfig, ec.ExitCode()) + }) + } +} + +func TestDropScenarioThresholds(t *testing.T) { + t.Parallel() + + kept := []string{ + "iterations", "iterations{name:scenario:api}", "iterations{scenario:ui}", + "iterations{scenario:unknown}", "iterations{scenario:api,scenario:ui}", + "iterations{scenario:api", + } + dropped := []string{ + "iterations{ 'scenario' : 'api', name:login}", + "iterations{scenario:api}", "iterations{scenario:ui,scenario:api}", + } + thresholds := make(map[string]metrics.Thresholds) + for _, name := range slices.Concat(kept, dropped) { + thresholds[name] = metrics.NewThresholds([]string{"count>0"}) + } + opts := lib.Options{Scenarios: lib.ScenarioConfigs{"ui": nil}, Thresholds: thresholds} + logger, hook := testutils.NewLoggerWithHook(t, logrus.WarnLevel) + dropScenarioThresholds(logger, &opts, lib.ScenarioConfigs{"ui": nil, "api": nil}) + + assert.ElementsMatch(t, kept, slices.Collect(maps.Keys(opts.Thresholds))) + assert.Len(t, thresholds, len(kept)+len(dropped)) + assert.Equal(t, []string{"--scenario skipped thresholds for excluded scenarios: " + + strings.Join(dropped, "; ") + "; these thresholds remain skipped even if another scenario emits matching tags"}, hook.Lines()) + + dropScenarioThresholds(logger, &opts, opts.Scenarios) + assert.Empty(t, hook.Lines()) +} diff --git a/internal/cmd/subcommand.go b/internal/cmd/subcommand.go index 1127a1b76..4bcc13cd7 100644 --- a/internal/cmd/subcommand.go +++ b/internal/cmd/subcommand.go @@ -1,7 +1,6 @@ package cmd import ( - "cmp" "fmt" "slices" "strings" @@ -107,7 +106,7 @@ func registryStubs(gs *state.GlobalState, baked []*cobra.Command) []*cobra.Comma gs.Logger.WithError(err).Debug("k6 extension catalog") } if subs == nil && first == 0 { // first == 0: not TAB completion, network allowed - url := cmp.Or(gs.Env[state.ProvisionCatalogURL], defaultCatalogURL()) + url := catalogURL(gs) _, _ = fmt.Fprint(gs.Stderr, "Loading the subcommand list...") tail := "\n\n" if gs.Stderr.IsTTY { diff --git a/internal/cmd/test_load.go b/internal/cmd/test_load.go index 3197ee10c..8307f44f4 100644 --- a/internal/cmd/test_load.go +++ b/internal/cmd/test_load.go @@ -488,9 +488,7 @@ func extractUnknownModules(err error) (dependencies, error) { return deps, nil } - var u modules.UnknownModulesError - - if errors.As(err, &u) { + if u, ok := errors.AsType[modules.UnknownModulesError](err); ok { for _, name := range u.List() { deps[name] = nil } @@ -529,6 +527,7 @@ func detectTestType(data []byte) string { return testTypeJS } +//nolint:funlen // Keep CLI feature ordering together. func (lt *loadedTest) consolidateDeriveAndValidateConfig( gs *state.GlobalState, cmd *cobra.Command, cliConfGetter func(flags *pflag.FlagSet) (Config, error), // TODO: obviate @@ -541,14 +540,66 @@ func (lt *loadedTest) consolidateDeriveAndValidateConfig( if err != nil { return nil, err } + // --once is CLI-only, so it rides on the CLI configuration layer. + cliConfig.once = getNullBool(cmd.Flags(), "once").Bool + } + + var scenarioNames []string + if cmd.Flags().Changed("scenario") { + var err error + scenarioNames, err = cmd.Flags().GetStringSlice("scenario") + if err != nil { + return nil, err + } + if len(scenarioNames) == 0 { + return nil, errext.WithExitCodeIfNone( + errors.New("--scenario requires at least one scenario name"), + exitcodes.InvalidConfig) + } } gs.Logger.Debug("Consolidating config layers...") - consolidatedConfig, err := getConsolidatedConfig(gs, cliConfig, lt.initRunner.GetOptions()) + fileConf, envConf, err := readConfigLayers(gs) + if err != nil { + return nil, err + } + runnerOpts := lt.initRunner.GetOptions() + // Lower layers drop their shortcuts before the merge; getOptions rejects the CLI ones. + layers := map[string]*lib.Options{ + "config": &fileConf.Options, + "script": &runnerOpts, + "environment": &envConf.Options, + } + if cliConfig.once { + err = dropOnceShortcuts(gs.Logger, layers) + } else if scenarioNames != nil { + err = dropScenarioShortcuts(gs.Logger, layers) + } + if err != nil { + return nil, err + } + consolidatedConfig, err := getConsolidatedConfig( + gs, cliConfig, fileConf, envConf, runnerOpts, lt.preInitState.FeatureFlags) if err != nil { return nil, err } + configuredScenarios := consolidatedConfig.Scenarios + switch { + case scenarioNames != nil: + consolidatedConfig.Options, err = selectScenarios(consolidatedConfig.Options, scenarioNames) + case cliConfig.once: + err = rejectAmbiguousOnce(consolidatedConfig.Scenarios) + } + if err != nil { + return nil, err + } + if cliConfig.once { + if consolidatedConfig.Options, err = applyOnce(consolidatedConfig.Options); err != nil { + return nil, err + } + } + gs.Logger.Debug("Parsing thresholds and validating config...") // Parse the thresholds, only if the --no-threshold flag is not set. // If parsing the threshold expressions failed, consider it as an @@ -567,6 +618,10 @@ func (lt *loadedTest) consolidateDeriveAndValidateConfig( } } + if scenarioNames != nil { + dropScenarioThresholds(gs.Logger, &consolidatedConfig.Options, configuredScenarios) + } + derivedConfig, err := deriveAndValidateConfig(consolidatedConfig, lt.initRunner.IsExecutable, gs.Logger) if err != nil { return nil, err diff --git a/internal/cmd/tests/build_full_test.go b/internal/cmd/tests/build_full_test.go new file mode 100644 index 000000000..074499be2 --- /dev/null +++ b/internal/cmd/tests/build_full_test.go @@ -0,0 +1,5 @@ +//go:build !lean + +package tests + +const leanBuild = false diff --git a/internal/cmd/tests/build_lean_test.go b/internal/cmd/tests/build_lean_test.go new file mode 100644 index 000000000..e875dd263 --- /dev/null +++ b/internal/cmd/tests/build_lean_test.go @@ -0,0 +1,5 @@ +//go:build lean + +package tests + +const leanBuild = true diff --git a/internal/cmd/tests/build_requirements_test.go b/internal/cmd/tests/build_requirements_test.go new file mode 100644 index 000000000..22e3c770c --- /dev/null +++ b/internal/cmd/tests/build_requirements_test.go @@ -0,0 +1,10 @@ +package tests + +import "testing" + +func requireFullBuild(t testing.TB) { + t.Helper() + if leanBuild { + t.Skip("requires cloud, REST API, or an output excluded from the lean build") + } +} diff --git a/internal/cmd/tests/cmd_cloud_load_zone_list_test.go b/internal/cmd/tests/cmd_cloud_load_zone_list_test.go new file mode 100644 index 000000000..25edcb479 --- /dev/null +++ b/internal/cmd/tests/cmd_cloud_load_zone_list_test.go @@ -0,0 +1,112 @@ +package tests + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + cloudapiv6 "go.k6.io/k6/v2/internal/cloudapi/v6" + "go.k6.io/k6/v2/internal/cloudapi/v6/v6test" + "go.k6.io/k6/v2/internal/cmd" +) + +func TestCloudLoadZoneList(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + t.Run("lists load zones successfully", func(t *testing.T) { + t.Parallel() + + ts := newCloudLoadZoneListTestState(t, testLoadZones()) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, fmt.Sprintf("Load zones for stack-%d:", validStackID)) + assert.Contains(t, stdout, "ID NAME TYPE AVAILABLE") + assert.Contains(t, stdout, "amazon:us:ashburn US East (Ashburn) public yes") + assert.Contains(t, stdout, "my-cluster My private cluster private no") + }) + + t.Run("empty load zone list", func(t *testing.T) { + t.Parallel() + + ts := newCloudLoadZoneListTestState(t, nil) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, fmt.Sprintf("Load zones for stack-%d:", validStackID)) + assert.Contains(t, stdout, "No load zones found.") + }) + + t.Run("--help includes usage", func(t *testing.T) { + t.Parallel() + + ts := NewGlobalTestState(t) + ts.CmdArgs = []string{"k6", "cloud", "load-zone", "list", "--help"} + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, "Usage:\n k6 cloud load-zone list [flags]") + assert.Contains(t, stdout, "--json") + assert.NotContains(t, stdout, "Global Flags:") + assert.NotContains(t, stdout, "--config") + assert.NotContains(t, stdout, "\n\n\nExamples:") + }) + + t.Run("--json outputs JSON", func(t *testing.T) { + t.Parallel() + + ts := newCloudLoadZoneListTestState(t, testLoadZones(), "--json") + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + + var loadZones []cloudapiv6.LoadZone + require.NoError(t, json.Unmarshal([]byte(stdout), &loadZones)) + assert.Equal(t, testLoadZones(), loadZones) + }) + + t.Run("--json with empty list outputs empty array", func(t *testing.T) { + t.Parallel() + + ts := newCloudLoadZoneListTestState(t, nil, "--json") + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + + var loadZones []cloudapiv6.LoadZone + require.NoError(t, json.Unmarshal([]byte(stdout), &loadZones)) + assert.Empty(t, loadZones) + }) +} + +func testLoadZones() []cloudapiv6.LoadZone { + return []cloudapiv6.LoadZone{ + {ID: 1, K6LoadZoneID: "amazon:us:ashburn", Name: "US East (Ashburn)", Public: true, Available: true}, + {ID: 2, K6LoadZoneID: "my-cluster", Name: "My private cluster", Public: false, Available: false}, + } +} + +func newCloudLoadZoneListTestState( + t *testing.T, loadZones []cloudapiv6.LoadZone, args ...string, +) *GlobalTestState { + t.Helper() + + srv := v6test.NewServer(t, v6test.Config{LoadZones: loadZones}) + + ts := NewGlobalTestState(t) + ts.CmdArgs = append([]string{"k6", "cloud", "load-zone", "list"}, args...) + ts.Env["K6_CLOUD_TOKEN"] = validToken + ts.Env["K6_CLOUD_STACK_ID"] = fmt.Sprintf("%d", validStackID) + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + return ts +} diff --git a/internal/cmd/tests/cmd_cloud_login_test.go b/internal/cmd/tests/cmd_cloud_login_test.go index bad98f35f..784696d00 100644 --- a/internal/cmd/tests/cmd_cloud_login_test.go +++ b/internal/cmd/tests/cmd_cloud_login_test.go @@ -22,6 +22,7 @@ const ( ) func TestCloudLoginWithArgs(t *testing.T) { + requireFullBuild(t) t.Parallel() testCases := []struct { diff --git a/internal/cmd/tests/cmd_cloud_project_list_test.go b/internal/cmd/tests/cmd_cloud_project_list_test.go index cdb16fa0b..49388fc82 100644 --- a/internal/cmd/tests/cmd_cloud_project_list_test.go +++ b/internal/cmd/tests/cmd_cloud_project_list_test.go @@ -14,6 +14,7 @@ import ( ) func TestCloudProjectList(t *testing.T) { + requireFullBuild(t) t.Parallel() t.Run("lists projects successfully", func(t *testing.T) { diff --git a/internal/cmd/tests/cmd_cloud_run_features_test.go b/internal/cmd/tests/cmd_cloud_run_features_test.go index 3333c445f..95bc208c4 100644 --- a/internal/cmd/tests/cmd_cloud_run_features_test.go +++ b/internal/cmd/tests/cmd_cloud_run_features_test.go @@ -85,6 +85,7 @@ func uploadAndCaptureArchive( } func TestCloudRunPropagatesFeatureFlagsToWorker(t *testing.T) { + requireFullBuild(t) t.Parallel() archiveEnv, archive := uploadAndCaptureArchive(t, @@ -105,6 +106,7 @@ func TestCloudRunPropagatesFeatureFlagsToWorker(t *testing.T) { } func TestCloudRunClearsInheritedFeatureFlags(t *testing.T) { + requireFullBuild(t) t.Parallel() archiveEnv, _ := uploadAndCaptureArchive(t, @@ -115,6 +117,7 @@ func TestCloudRunClearsInheritedFeatureFlags(t *testing.T) { } func TestCloudRunPropagatesFeatureFlagFromEnv(t *testing.T) { + requireFullBuild(t) t.Parallel() // K6_FEATURES set in the process environment (not -e, not --features) must be @@ -128,6 +131,7 @@ func TestCloudRunPropagatesFeatureFlagFromEnv(t *testing.T) { } func TestCloudRunPropagatesFeatureFlagFromLegacyAliasEnv(t *testing.T) { + requireFullBuild(t) t.Parallel() // A legacy alias env var set in the process environment must resolve to its @@ -140,6 +144,7 @@ func TestCloudRunPropagatesFeatureFlagFromLegacyAliasEnv(t *testing.T) { } func TestCloudRunIgnoresFeatureFlagFromScriptOptions(t *testing.T) { + requireFullBuild(t) t.Parallel() // Features in the script's exported options are unsupported, so they must @@ -153,6 +158,6 @@ export default function () {};` archiveEnv, _ := uploadAndCaptureArchive(t, []string{"k6", "cloud", "run", "test.js"}, nil, script) - _, ok := archiveEnv["K6_FEATURES"] - assert.False(t, ok, "options.features must not convey to the worker") + assert.Equal(t, "async-metric-context", archiveEnv["K6_FEATURES"], + "only the fork default, not options.features, should convey to the worker") } diff --git a/internal/cmd/tests/cmd_cloud_run_push_credentials_test.go b/internal/cmd/tests/cmd_cloud_run_push_credentials_test.go new file mode 100644 index 000000000..78c3de812 --- /dev/null +++ b/internal/cmd/tests/cmd_cloud_run_push_credentials_test.go @@ -0,0 +1,360 @@ +package tests + +import ( + "fmt" + "io" + "net/http" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + provtest "go.k6.io/k6/v2/internal/cloudapi/provisioning/test" + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +// Sentinel credentials. Each has a single, distinct source so a test can +// tell exactly which one k6 used to push metrics. +const ( + orgToken = "org-longlived-token" // K6_CLOUD_TOKEN: legacy / relay push + scopedToken = "test-run-token-abc" // from the provisioning response + bogusToken = "bogus-env-token" // a stray value in the environment + extToken = "ext-scoped-token" // an externally-supplied scoped token + staleConfigToken = "stale-config-token" // a stale value in a k6 config file +) + +const pushCredScript = ` +export const options = { cloud: { name: 'push creds', projectID: 123456 } }; +export default function () {};` + +// cloudRequestRecorder captures the bearer token of every request the mock +// cloud sees, so a test can assert which token k6 pushed metrics with. +type cloudRequestRecorder struct { + mu sync.Mutex + requests []recordedRequest +} + +type recordedRequest struct{ path, token string } + +func (r *cloudRequestRecorder) handler(w http.ResponseWriter, req *http.Request) { + _, _ = io.Copy(io.Discard, req.Body) + r.mu.Lock() + r.requests = append(r.requests, recordedRequest{req.URL.Path, bearerToken(req)}) + r.mu.Unlock() + w.WriteHeader(http.StatusOK) +} + +func (r *cloudRequestRecorder) tokensForPath(sub string) []string { + r.mu.Lock() + defer r.mu.Unlock() + var out []string + for _, req := range r.requests { + if strings.Contains(req.path, sub) { + out = append(out, req.token) + } + } + return out +} + +func (r *cloudRequestRecorder) allTokens() []string { + r.mu.Lock() + defer r.mu.Unlock() + out := make([]string, 0, len(r.requests)) + for _, req := range r.requests { + out = append(out, req.token) + } + return out +} + +// bearerToken extracts the credential from the Authorization header, +// tolerating both schemes k6 uses: "Bearer " for the provisioning/v6 +// push and "Token " for the legacy v1 push. +func bearerToken(req *http.Request) string { + auth := req.Header.Get("Authorization") + auth = strings.TrimPrefix(auth, "Bearer ") + auth = strings.TrimPrefix(auth, "Token ") + return auth +} + +func failHandler(t *testing.T, msg string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + assert.Fail(t, msg) + w.WriteHeader(http.StatusInternalServerError) + }) +} + +// assertPushedWith asserts that at least one request hit a path containing +// sub, and that every such request carried the expected bearer token. +func assertPushedWith(t *testing.T, rec *cloudRequestRecorder, sub, token string) { + t.Helper() + tokens := rec.tokensForPath(sub) + require.NotEmptyf(t, tokens, "expected a metrics push to a path containing %q", sub) + for _, tk := range tokens { + assert.Equalf(t, token, tk, "push to %q used an unexpected bearer token", sub) + } +} + +func assertTokenNeverUsed(t *testing.T, rec *cloudRequestRecorder, token string) { + t.Helper() + assert.NotContainsf(t, rec.allTokens(), token, "token %q must never be sent", token) +} + +func assertPathNeverHit(t *testing.T, rec *cloudRequestRecorder, sub string) { + t.Helper() + assert.Emptyf(t, rec.tokensForPath(sub), "no request should hit a path containing %q", sub) +} + +// setupSelfProvisionedRun wires a self-provisioned `k6 cloud run +// --local-execution` against a provisioning mock whose start_local_execution +// returns the scoped token and a push URL that points back at the mock +// (/v1/metrics). +func setupSelfProvisionedRun( + t *testing.T, +) (*GlobalTestState, *provtest.Server, *cloudRequestRecorder, *atomic.Bool) { + t.Helper() + + ts := makeTestState(t, pushCredScript, []string{"--local-execution"}) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + + srv := provtest.NewServer(t) + rec := &cloudRequestRecorder{} + var notified atomic.Bool + + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + resp := provtest.DefaultStartLocalExecutionResponse() + resp.SetArchiveUploadUrl(srv.PresignedUploadURL()) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + // Point the logs push URL at the mock too: --local-execution activates + // the cloud log pusher, which would otherwise egress to the real logs + // host baked into DefaultStartLocalExecutionResponse. + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{{Status: v6.StatusInitializing}}) + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + notified.Store(true) + w.WriteHeader(http.StatusOK) + }) + + // Record the provisioning-mode push (/v1/metrics) and any stray push + // (e.g. an overridden URL landing on the catch-all). + srv.Mux.HandleFunc("/v1/metrics", rec.handler) + srv.Mux.HandleFunc("/", rec.handler) + + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + return ts, srv, rec, ¬ified +} + +// TestCloudMetricsPushCredentials specifies how the cloud output resolves +// the endpoint and bearer token it pushes metrics with, across the three +// ways a run can be provisioned: +// +// - Self-provisioned (k6 cloud run --local-execution): the push URL and +// scoped token come from the provisioning API's start_local_execution +// response. They are set programmatically and are never overridden by +// the environment or a config file. +// - Externally-provisioned (--local-execution + K6_CLOUD_PUSH_REF_ID): an +// orchestrator that already created the run supplies the scoped push URL +// and token via K6_CLOUD_METRICS_PUSH_URL and K6_CLOUD_TEST_RUN_TOKEN, +// which are required together. +// - Legacy (k6 run --out cloud, or a PushRefID relay without scoped +// creds): metrics go to the endpoint derived from the host using the +// long-lived org token. +// +// Each case runs the real command against an in-process mock cloud +// (httptest, no network) and asserts which bearer token k6 pushes with, +// using distinct sentinel tokens so the source is unambiguous. +func TestCloudMetricsPushCredentials(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + // Self-provisioned: creds come from the provisioning response and must + // not be overridden by stray env vars or config-file values. + + t.Run("self-provisioned run pushes with the scoped token from provisioning", func(t *testing.T) { + t.Parallel() + ts, _, rec, notified := setupSelfProvisionedRun(t) + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", scopedToken) + assert.True(t, notified.Load(), "a self-provisioned run notifies at the end") + }) + + t.Run("self-provisioned run ignores a stray token env var", func(t *testing.T) { + t.Parallel() + ts, _, rec, notified := setupSelfProvisionedRun(t) + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = bogusToken + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", scopedToken) + assertTokenNeverUsed(t, rec, bogusToken) + assert.True(t, notified.Load()) + }) + + t.Run("self-provisioned run ignores a stray metrics-push-URL env var", func(t *testing.T) { + t.Parallel() + ts, srv, rec, _ := setupSelfProvisionedRun(t) + ts.Env["K6_CLOUD_METRICS_PUSH_URL"] = srv.URL + "/overridden-metrics" + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", scopedToken) + assertPathNeverHit(t, rec, "/overridden-metrics") + }) + + t.Run("self-provisioned run ignores stray scoped-cred env vars", func(t *testing.T) { + t.Parallel() + ts, srv, rec, _ := setupSelfProvisionedRun(t) + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = bogusToken + ts.Env["K6_CLOUD_METRICS_PUSH_URL"] = srv.URL + "/overridden-metrics" + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", scopedToken) + assertTokenNeverUsed(t, rec, bogusToken) + assertPathNeverHit(t, rec, "/overridden-metrics") + }) + + t.Run("self-provisioned run ignores a stale token in the config file", func(t *testing.T) { + t.Parallel() + ts, _, rec, _ := setupSelfProvisionedRun(t) + cfg := []byte(`{"collectors":{"cloud":{"testRunToken":"` + staleConfigToken + `"}}}`) + require.NoError(t, ts.FS.MkdirAll(filepath.Dir(ts.Flags.ConfigFilePath), 0o755)) + require.NoError(t, fsext.WriteFile(ts.FS, ts.Flags.ConfigFilePath, cfg, 0o644)) + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", scopedToken) + assertTokenNeverUsed(t, rec, staleConfigToken) + }) + + // Legacy: `k6 run --out cloud` and PushRefID relays push to the + // host-derived endpoint with the long-lived org token, unaffected by the + // scoped-cred env vars. + + t.Run("k6 run --out cloud ignores a stray token env var", func(t *testing.T) { + t.Parallel() + ts := getSingleFileTestState(t, pushCredScript, []string{"-v", "--log-output=stdout", "--out=cloud"}, 0) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = bogusToken + + rec := &cloudRequestRecorder{} + const refID = 1337 + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/v1/tests$": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, `{"reference_id": "%d", "config": {}}`, refID) + }), + fmt.Sprintf("POST ^/v1/tests/%d$", refID): http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }), + "POST ^/v2/metrics/": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v2/metrics/", orgToken) + assertTokenNeverUsed(t, rec, bogusToken) + }) + + t.Run("a PushRefID relay without scoped creds pushes with the org token", func(t *testing.T) { + t.Parallel() + ts := makeTestState(t, pushCredScript, []string{"--local-execution"}) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + + rec := &cloudRequestRecorder{} + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/v1/tests$": failHandler(t, "CreateTestRun must not be called with PushRefID"), + "POST ^/provisioning/v1/": failHandler(t, "provisioning API must not be called with PushRefID"), + "POST ^/cloud/v6/": failHandler(t, "v6 API must not be called with PushRefID"), + "POST ^/v2/metrics/": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v2/metrics/", orgToken) + }) + + t.Run("k6 run --out cloud with PushRefID ignores a lone scoped-cred env var", func(t *testing.T) { + t.Parallel() + ts := getSingleFileTestState(t, pushCredScript, []string{"-v", "--log-output=stdout", "--out=cloud"}, 0) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "1337" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = extToken // lone scoped cred, no push URL + + rec := &cloudRequestRecorder{} + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/v1/tests$": failHandler(t, "CreateTestRun must not be called with PushRefID"), + "POST ^/v2/metrics/": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v2/metrics/", orgToken) + }) + + // Externally-provisioned: an orchestrator supplies the scoped creds via + // env; both are required together. + + t.Run("externally-provisioned run pushes with the scoped creds from env", func(t *testing.T) { + t.Parallel() + ts := makeTestState(t, pushCredScript, []string{"--local-execution"}) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + + rec := &cloudRequestRecorder{} + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/provisioning/v1/": failHandler(t, "provisioning API must not be called with PushRefID"), + "POST ^/cloud/v6/": failHandler(t, "v6 API must not be called with PushRefID"), + "POST ^/v1/metrics": http.HandlerFunc(rec.handler), + "POST ^/v2/metrics/": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + ts.Env["K6_CLOUD_METRICS_PUSH_URL"] = srv.URL + "/v1/metrics" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = extToken + + cmd.ExecuteWithGlobalState(ts.GlobalState) + assertPushedWith(t, rec, "/v1/metrics", extToken) + }) + + t.Run("externally-provisioned run requires both scoped creds", func(t *testing.T) { + t.Parallel() + ts := makeTestState(t, pushCredScript, []string{"--local-execution", "--log-output=stdout"}) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = extToken // only one of the pair + ts.ExpectedExitCode = -1 + + srv := getTestServer(t, map[string]http.Handler{}) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + out := ts.Stdout.String() + ts.Stderr.String() + assert.Contains(t, out, "must be set together", + "a partial scoped-cred pair must fail with a clear error") + }) +} diff --git a/internal/cmd/tests/cmd_cloud_run_test.go b/internal/cmd/tests/cmd_cloud_run_test.go index 81dfc17d7..f1a52174f 100644 --- a/internal/cmd/tests/cmd_cloud_run_test.go +++ b/internal/cmd/tests/cmd_cloud_run_test.go @@ -1,7 +1,6 @@ package tests import ( - "bytes" "encoding/json" "fmt" "io" @@ -9,13 +8,18 @@ import ( "os" "path/filepath" "strconv" + "strings" + "sync" + "sync/atomic" "testing" + k6cloud "github.com/grafana/k6-cloud-openapi-client-go/k6" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.k6.io/k6/v2/cloudapi" "go.k6.io/k6/v2/errext/exitcodes" + provtest "go.k6.io/k6/v2/internal/cloudapi/provisioning/test" + v6 "go.k6.io/k6/v2/internal/cloudapi/v6" "go.k6.io/k6/v2/internal/cloudapi/v6/v6test" "go.k6.io/k6/v2/internal/cmd" "go.k6.io/k6/v2/internal/lib/testutils" @@ -23,6 +27,7 @@ import ( ) func TestK6CloudRun(t *testing.T) { + requireFullBuild(t) t.Parallel() runCloudTests(t, setupK6CloudRunCmd) } @@ -49,6 +54,7 @@ func setupK6CloudRunCmd(cliFlags []string) []string { // "projectID": 124 // } func TestCloudRunWithArchive(t *testing.T) { + requireFullBuild(t) t.Parallel() ts := NewGlobalTestState(t) @@ -116,6 +122,7 @@ func TestCloudRunWithArchive(t *testing.T) { } func TestCloudRunCommandIncompatibleFlags(t *testing.T) { + requireFullBuild(t) t.Parallel() testCases := []struct { @@ -153,6 +160,11 @@ func TestCloudRunCommandIncompatibleFlags(t *testing.T) { cliArgs: []string{"--no-cloud-secrets"}, wantStderrContains: "the --no-cloud-secrets flag can only be used in conjunction with the --local-execution flag", }, + { + name: "using --no-cloud-logs without --local-execution should fail", + cliArgs: []string{"--no-cloud-logs"}, + wantStderrContains: "the --no-cloud-logs flag can only be used in conjunction with the --local-execution flag", + }, } for _, tc := range testCases { @@ -170,9 +182,10 @@ func TestCloudRunCommandIncompatibleFlags(t *testing.T) { } func TestCloudRunLocalExecution(t *testing.T) { + requireFullBuild(t) t.Parallel() - t.Run("should upload the test archive with a multipart request as a default", func(t *testing.T) { + t.Run("should upload the test archive via presigned S3 URL as a default", func(t *testing.T) { t.Parallel() script := ` @@ -187,42 +200,95 @@ export default function() {};` ts := makeTestState(t, script, []string{"--local-execution"}) - testServerHandlerFunc := http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { - // When using the local execution mode, the test archive should be uploaded to the cloud - // as a multipart request. - formData, err := parseMultipartRequest(req) - require.NoError(t, err, "expected a correctly formed multipart request") - assert.Contains(t, formData, "name") - assert.Equal(t, "Hello k6 Cloud!", formData["name"]) - assert.Contains(t, formData, "project_id") - assert.Equal(t, "123456", formData["project_id"]) - assert.Contains(t, formData, "file") - assert.NotEmpty(t, formData["file"]) - - resp.WriteHeader(http.StatusOK) - _, err = fmt.Fprint(resp, `{ - "reference_id": "1337", - "test_run_token": "mock-test-run-token", - "secrets_config": { - "endpoint": "https://mock-secrets.example.com/{key}", - "response_path": "plaintext" - }, - "config": { - "testRunDetails": "https://some.other.url/foo/tests/org/1337?bar=baz" - } - }`) - assert.NoError(t, err) + srv := provtest.NewServer(t) + + // v6 CreateOrFindLoadTest: POST /cloud/v6/projects/{projectID}/load_tests + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodPost, r.Method) + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + + // start_local_execution: check request body and return response + var startCalled atomic.Bool + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, r *http.Request) { + startCalled.Store(true) + + body, err := io.ReadAll(r.Body) + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(body, &payload)) + + // Verify options field is present and contains resolved lib.Options + assert.Contains(t, payload, "options") + + // Verify archive_size > 0 + assert.Contains(t, payload, "archive_size") + archiveSize, ok := payload["archive_size"].(float64) + assert.True(t, ok, "archive_size should be a number") + assert.Greater(t, archiveSize, float64(0), "archive_size should be > 0") + + resp := provtest.DefaultStartLocalExecutionResponse() + // Override URLs to point to the test server + uploadURL := srv.URL + provtest.PresignedUploadPath + resp.SetArchiveUploadUrl(uploadURL) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + // Override metrics push URL to point to the test server + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + // Point the logs push_url at the test server too, so the + // configured pusher never contacts the real logs host. + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + + // presigned archive upload + var archiveUploaded atomic.Bool + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, r *http.Request) { + archiveUploaded.Store(true) + body, err := io.ReadAll(r.Body) + require.NoError(t, err) + assert.Greater(t, len(body), 0, "archive upload body should not be empty") + assert.Equal(t, "application/x-tar", r.Header.Get("Content-Type")) + w.WriteHeader(http.StatusOK) + }) + + // v6 FetchTestRun: return "initializing" immediately + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + // notify: verify it's called + var notifyCalled atomic.Bool + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + notifyCalled.Store(true) + w.WriteHeader(http.StatusOK) + }) + + // catch-all for metrics pushes and other calls + srv.Mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) }) - srv := getCloudTestEndChecker(t, 1337, testServerHandlerFunc, cloudapi.RunStatusFinished, cloudapi.ResultStatusPassed) ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL cmd.ExecuteWithGlobalState(ts.GlobalState) stdout := ts.Stdout.String() t.Log(stdout) assert.Contains(t, stdout, "execution: local") - assert.Contains(t, stdout, "output: cloud (https://some.other.url/foo/tests/org/1337?bar=baz)") + assert.Contains(t, stdout, fmt.Sprintf("output: cloud (%s/runs/%d)", srv.URL, provtest.DefaultTestRunID)) + assert.True(t, startCalled.Load(), "start_local_execution should have been called") + assert.True(t, archiveUploaded.Load(), "archive should have been uploaded via presigned URL") + assert.True(t, notifyCalled.Load(), "notify should have been called at test end") }) t.Run("does not upload the archive when --no-archive-upload is provided", func(t *testing.T) { @@ -240,44 +306,72 @@ export default function() {};` ts := makeTestState(t, script, []string{"--local-execution", "--no-archive-upload"}) - testServerHandlerFunc := http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { - body, err := io.ReadAll(req.Body) + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + + // start_local_execution: verify archive_size is null + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) require.NoError(t, err) var payload map[string]any - err = json.Unmarshal(body, &payload) - require.NoError(t, err) + require.NoError(t, json.Unmarshal(body, &payload)) + + // archive_size should be explicitly null (Go JSON unmarshals null as nil) + assert.Contains(t, payload, "archive_size") + assert.Nil(t, payload["archive_size"], "archive_size should be null when --no-archive-upload is provided") + + resp := provtest.DefaultStartLocalExecutionResponse() + // No upload URL when --no-archive-upload is set + resp.ArchiveUploadUrl.Unset() + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + // Point the logs push_url at the test server too, so the + // configured pusher never contacts the real logs host. + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + + // Archive upload should NOT be called + var archiveUploaded atomic.Bool + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + archiveUploaded.Store(true) + w.WriteHeader(http.StatusOK) + }) - assert.Contains(t, payload, "name") - assert.Equal(t, "Hello k6 Cloud!", payload["name"]) - assert.Contains(t, payload, "project_id") - assert.Equal(t, float64(123456), payload["project_id"]) - assert.NotContains(t, payload, "file") - - resp.WriteHeader(http.StatusOK) - _, err = fmt.Fprint(resp, `{ - "reference_id": "1337", - "test_run_token": "mock-test-run-token", - "secrets_config": { - "endpoint": "https://mock-secrets.example.com/{key}", - "response_path": "plaintext" - }, - "config": { - "testRunDetails": "https://some.other.url/foo/tests/org/1337?bar=baz" - } - }`) - assert.NoError(t, err) + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.Mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) }) - srv := getCloudTestEndChecker(t, 1337, testServerHandlerFunc, cloudapi.RunStatusFinished, cloudapi.ResultStatusPassed) ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL cmd.ExecuteWithGlobalState(ts.GlobalState) stdout := ts.Stdout.String() t.Log(stdout) assert.Contains(t, stdout, "execution: local") - assert.Contains(t, stdout, "output: cloud (https://some.other.url/foo/tests/org/1337?bar=baz)") + assert.Contains(t, stdout, fmt.Sprintf("output: cloud (%s/runs/%d)", srv.URL, provtest.DefaultTestRunID)) + assert.False(t, archiveUploaded.Load(), "archive should NOT have been uploaded when --no-archive-upload is set") }) t.Run("the script can read the test run id to the environment", func(t *testing.T) { @@ -297,17 +391,58 @@ export default function() { ts := makeTestState(t, script, []string{"--local-execution", "--log-output=stdout"}) - const testRunID = 1337 - srv := getCloudTestEndChecker(t, testRunID, nil, cloudapi.RunStatusFinished, cloudapi.ResultStatusPassed) + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + resp := provtest.DefaultStartLocalExecutionResponse() + uploadURL := srv.URL + provtest.PresignedUploadPath + resp.SetArchiveUploadUrl(uploadURL) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + // Point the logs push_url at the test server too, so the + // configured pusher never contacts the real logs host. + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.Mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL cmd.ExecuteWithGlobalState(ts.GlobalState) stdout := ts.Stdout.String() t.Log(stdout) assert.Contains(t, stdout, "execution: local") - assert.Contains(t, stdout, "output: cloud (https://app.k6.io/runs/1337)") - assert.Contains(t, stdout, "The test run id is "+strconv.Itoa(testRunID)) + assert.Contains(t, stdout, fmt.Sprintf("output: cloud (%s/runs/%d)", srv.URL, provtest.DefaultTestRunID)) + assert.Contains(t, stdout, "The test run id is "+strconv.Itoa(int(provtest.DefaultTestRunID))) }) t.Run("reuses existing test run when K6_CLOUD_PUSH_REF_ID is set", func(t *testing.T) { @@ -334,9 +469,16 @@ export default function() { "POST ^/v1/tests$": http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { require.Fail(t, "CreateTestRun must not be called when K6_CLOUD_PUSH_REF_ID is set") }), + "POST ^/provisioning/v1/": http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + require.Fail(t, "provisioning API must not be called when K6_CLOUD_PUSH_REF_ID is set") + }), + "POST ^/cloud/v6/projects/": http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + require.Fail(t, "v6 load_tests API must not be called when K6_CLOUD_PUSH_REF_ID is set") + }), }) t.Cleanup(srv.Close) ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL cmd.ExecuteWithGlobalState(ts.GlobalState) @@ -350,6 +492,7 @@ export default function() { } func TestCloudRunLocalExecutionNoCloudSecrets(t *testing.T) { + requireFullBuild(t) t.Parallel() script := ` @@ -363,24 +506,50 @@ export default function() {};` ts := makeTestState(t, script, []string{"--local-execution", "--no-cloud-secrets"}) - testServerHandlerFunc := http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { - resp.WriteHeader(http.StatusOK) - _, err := fmt.Fprint(resp, `{ - "reference_id": "1337", - "test_run_token": "mock-test-run-token", - "secrets_config": { - "endpoint": "https://mock-secrets.example.com/{key}", - "response_path": "plaintext" - }, - "config": { - "testRunDetails": "https://some.other.url/foo/tests/org/1337?bar=baz" - } - }`) - assert.NoError(t, err) + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + resp := provtest.DefaultStartLocalExecutionResponse() + uploadURL := srv.URL + provtest.PresignedUploadPath + resp.SetArchiveUploadUrl(uploadURL) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + // Point the logs push_url at the test server too, so the + // configured pusher never contacts the real logs host. + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.Mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) }) - srv := getCloudTestEndChecker(t, 1337, testServerHandlerFunc, cloudapi.RunStatusFinished, cloudapi.ResultStatusPassed) ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL cmd.ExecuteWithGlobalState(ts.GlobalState) @@ -388,6 +557,473 @@ export default function() {};` assert.Nil(t, ts.CloudSecretSource, "cloud secret source should not be registered when --no-cloud-secrets is set") } +func TestCloudRunLocalExecutionCloudLogPusher(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + // The VU logs one line so the pusher has a deterministic entry to push; + // console.log routes through the same logger the pusher hooks. + script := ` +export const options = { + cloud: { + name: 'Test cloud logs', + projectID: 123456, + }, +}; +export default function() { console.log('hello from the vu'); };` + + t.Run("registers the pusher for --local-execution", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + rec := setupLocalExecutionProvMock(t, ts) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + assert.NotNil(t, ts.CloudLogPusher, + "cloud log pusher should be registered for k6 cloud run --local-execution") + + // The self-provisioned flow must configure the pusher from the + // provisioning response: streams are pushed with the scoped token + // and the result's TestRunID as the test_run_id label. + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + assert.Equal(t, "Bearer test-run-token-abc", auths[0]) + assert.Contains(t, bodies[0], + fmt.Sprintf(`"test_run_id":"%d"`, provtest.DefaultTestRunID)) + }) + + t.Run("configures the pusher from env for an externally-provisioned run", func(t *testing.T) { + t.Parallel() + + rec := &logPushRecorder{} + const pushRefID = "99999" + + srv := getTestServer(t, map[string]http.Handler{ + "POST ^" + logsPushPath + "$": http.HandlerFunc(rec.handler), + "POST ^/v1/tests$": http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + require.Fail(t, "CreateTestRun must not be called when K6_CLOUD_PUSH_REF_ID is set") + }), + "POST ^/provisioning/v1/": http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + require.Fail(t, "provisioning API must not be called when K6_CLOUD_PUSH_REF_ID is set") + }), + }) + t.Cleanup(srv.Close) + + ts := makeTestState(t, script, []string{"--local-execution"}) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + ts.Env["K6_CLOUD_PUSH_REF_ID"] = pushRefID + ts.Env["K6_CLOUD_LOGS_PUSH_URL"] = srv.URL + logsPushPath + // An external orchestrator supplies the scoped metrics push creds + // (PR #6133); they must be set together, and the same token is used + // as the Bearer for the logs push. + ts.Env["K6_CLOUD_METRICS_PUSH_URL"] = srv.URL + "/v1/metrics" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = "ext-token" + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + require.NotNil(t, ts.CloudLogPusher, + "cloud log pusher should be registered for an externally-provisioned --local-execution run") + + // The externally-provisioned flow reads the logs config and token + // from env; the run id is the PushRefID. + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + assert.Equal(t, "Bearer ext-token", auths[0]) + assert.Contains(t, bodies[0], `"test_run_id":"`+pushRefID+`"`) + }) + + t.Run("externally-provisioned run keeps test_run_id with empty allowed labels", func(t *testing.T) { + t.Parallel() + + rec := &logPushRecorder{} + const pushRefID = "99999" + + srv := getTestServer(t, map[string]http.Handler{ + "POST ^" + logsPushPath + "$": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + + ts := makeTestState(t, script, []string{"--local-execution"}) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + ts.Env["K6_CLOUD_PUSH_REF_ID"] = pushRefID + ts.Env["K6_CLOUD_LOGS_PUSH_URL"] = srv.URL + logsPushPath + ts.Env["K6_CLOUD_METRICS_PUSH_URL"] = srv.URL + "/v1/metrics" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = "ext-token" + // An explicitly empty allow-list decodes to []string{}; the required + // test_run_id label must still survive rather than be stripped. + ts.Env["K6_CLOUD_LOGS_ALLOWED_LABELS"] = "" + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + _, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + assert.Contains(t, bodies[0], `"test_run_id":"`+pushRefID+`"`, + "test_run_id must survive an empty K6_CLOUD_LOGS_ALLOWED_LABELS") + }) + + t.Run("does not register the pusher with --no-cloud-logs", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution", "--no-cloud-logs"}) + rec := setupLocalExecutionProvMock(t, ts) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + assert.Nil(t, ts.CloudLogPusher, + "cloud log pusher should not be registered when --no-cloud-logs is set") + _, bodies := rec.snapshot() + assert.Empty(t, bodies, "no logs should be pushed when --no-cloud-logs is set") + }) + + t.Run("does not register the pusher for non-local-execution", func(t *testing.T) { + t.Parallel() + + ts := NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "test.js"), []byte(script), 0o644)) + ts.CmdArgs = []string{"k6", "run", "test.js"} + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + assert.Nil(t, ts.CloudLogPusher, + "cloud log pusher should not be registered for a non-local-execution run") + }) + + // The subtests below mirror TestCloudMetricsPushCredentials (which pins the metrics + // push-credential resolution) for the cloud *log* push: the observable + // is which bearer token / URL k6 streams logs with across the + // self-provisioned, externally-provisioned and legacy paths. The + // sentinel token consts (scopedToken, bogusToken, extToken, + // staleConfigToken, orgToken) are shared with + // cmd_cloud_run_push_credentials_test.go. + + // A2 (log): a stray K6_CLOUD_TEST_RUN_TOKEN in the env must NOT override + // the run-scoped token the self-provisioned flow gets from provisioning. + // The token is programmatic-only (no envconfig), so the stray is inert. + t.Run("self-provisioned log push ignores a stray token env var", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + rec := setupLocalExecutionProvMock(t, ts) + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = bogusToken + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + for _, a := range auths { + assert.Equalf(t, "Bearer "+scopedToken, a, + "log push must use the run-scoped token, not the stray env token %q", bogusToken) + } + }) + + // A3 (log): a stray K6_CLOUD_LOGS_PUSH_URL must NOT override the + // provisioning-supplied logs URL. conf.Apply(runtime_config) overwrites + // the env value. If it leaked, the push would go to the unroutable host + // and the provisioning logs endpoint (rec) would record nothing. + t.Run("self-provisioned log push ignores a stray logs-push-URL env var", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + rec := setupLocalExecutionProvMock(t, ts) + ts.Env["K6_CLOUD_LOGS_PUSH_URL"] = "http://stray.invalid/logs/push" + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, + "log push must hit the provisioning logs URL, not the stray env URL") + for _, a := range auths { + assert.Equal(t, "Bearer "+scopedToken, a) + } + }) + + // A4 (log): both stray env vars at once — scoped token + provisioning URL + // must still win. + t.Run("self-provisioned log push ignores both stray env vars", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + rec := setupLocalExecutionProvMock(t, ts) + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = bogusToken + ts.Env["K6_CLOUD_LOGS_PUSH_URL"] = "http://stray.invalid/logs/push" + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, + "log push must hit the provisioning logs URL with the scoped token") + for _, a := range auths { + assert.Equal(t, "Bearer "+scopedToken, a) + } + }) + + // A8 (log): a stale testRunToken living in the k6 config file must NOT + // override the run-scoped token in the self-provisioned flow. + t.Run("self-provisioned log push ignores a stale config-file token", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + rec := setupLocalExecutionProvMock(t, ts) + cfg := []byte(`{"collectors":{"cloud":{"testRunToken":"` + staleConfigToken + `"}}}`) + require.NoError(t, ts.FS.MkdirAll(filepath.Dir(ts.Flags.ConfigFilePath), 0o755)) + require.NoError(t, fsext.WriteFile(ts.FS, ts.Flags.ConfigFilePath, cfg, 0o644)) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + auths, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + for _, a := range auths { + assert.Equalf(t, "Bearer "+scopedToken, a, + "log push must use the run-scoped token, not the stale config token %q", staleConfigToken) + } + }) + + // B2 (log): partial scoped creds (only the token) must error before any + // log push happens. + t.Run("errors on partial scoped creds and pushes no logs", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution", "--log-output=stdout"}) + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + ts.Env["K6_CLOUD_TEST_RUN_TOKEN"] = extToken // only one of the required pair + ts.ExpectedExitCode = -1 + + rec := &logPushRecorder{} + srv := getTestServer(t, map[string]http.Handler{ + "POST ^" + logsPushPath + "$": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + out := ts.Stdout.String() + ts.Stderr.String() + assert.Contains(t, out, "must be set together", + "expected a clear both-or-neither error for partial scoped creds") + _, bodies := rec.snapshot() + assert.Empty(t, bodies, "no cloud log push should happen when the run errors out") + }) + + // A logs push URL in the externally-provisioned flow is useless without the + // scoped token; it must error rather than silently stream nothing. + t.Run("errors when a logs push URL is set without a token", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution", "--log-output=stdout"}) + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + ts.Env["K6_CLOUD_LOGS_PUSH_URL"] = "http://logs.invalid/push" + // No K6_CLOUD_TEST_RUN_TOKEN (nor the K6_CLOUD_METRICS_PUSH_URL it + // pairs with), so there is no token to authenticate the logs push. + ts.ExpectedExitCode = -1 + + srv := getTestServer(t, map[string]http.Handler{}) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + out := ts.Stdout.String() + ts.Stderr.String() + assert.Contains(t, out, "K6_CLOUD_LOGS_PUSH_URL requires K6_CLOUD_TEST_RUN_TOKEN") + }) + + // A5/A6 (log): the legacy `k6 run --out=cloud` path must not register a + // cloud log pusher at all (log streaming is a --local-execution feature). + t.Run("does not register the pusher for k6 run --out=cloud", func(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, script, []string{"-v", "--log-output=stdout", "--out=cloud"}, 0) + ts.Env["K6_CLOUD_TOKEN"] = orgToken + const refID = 1337 + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/v1/tests$": http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, `{"reference_id": "%d", "config": {}}`, refID) + }), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + assert.Nil(t, ts.CloudLogPusher, + "cloud log streaming is only for cloud run --local-execution, not --out=cloud") + }) + + // A6 (log): a PushRefID relay run with no scoped creds and no logs push + // URL must not push logs anywhere (the pusher stays unconfigured). + t.Run("relay run without a logs push URL pushes no logs", func(t *testing.T) { + t.Parallel() + + ts := makeTestState(t, script, []string{"--local-execution"}) + ts.Env["K6_CLOUD_PUSH_REF_ID"] = "99999" + + rec := &logPushRecorder{} + srv := getTestServer(t, map[string]http.Handler{ + "POST ^/v1/tests$": failHandler(t, "CreateTestRun must not be called with PushRefID"), + "POST ^/provisioning/v1/": failHandler(t, "provisioning API must not be called with PushRefID"), + "POST ^" + logsPushPath + "$": http.HandlerFunc(rec.handler), + }) + t.Cleanup(srv.Close) + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + _, bodies := rec.snapshot() + assert.Empty(t, bodies, + "a relay run with no logs push URL must not push logs anywhere") + }) + + // "and more" (beyond the metrics matrix): the pusher must honour the + // backend-configured log level. The logger runs at debug (-v) so + // console.debug reaches the pusher hook; the provisioning logs config + // (level=info) must then drop it before pushing, while info survives. + t.Run("self-provisioned log push filters lines below the configured level", func(t *testing.T) { + t.Parallel() + + lvlScript := ` +export const options = { cloud: { name: 'Test cloud logs', projectID: 123456 } }; +export default function() { + console.debug('debug-line-must-be-filtered'); + console.log('info-line-must-be-pushed'); +};` + ts := makeTestState(t, lvlScript, []string{"--local-execution", "-v", "--log-output=stdout"}) + rec := setupLocalExecutionProvMock(t, ts) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + _, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + all := strings.Join(bodies, "\n") + assert.Contains(t, all, "info-line-must-be-pushed", + "info-level lines should be pushed") + assert.NotContains(t, all, "debug-line-must-be-filtered", + "debug-level lines must be filtered out by the configured level (info)") + }) + + // A logged secret must reach the cloud only in redacted form: the + // secrets-redaction hook runs before the pusher observes an entry + // (root.go), so the push never carries the raw value. Guards that + // property end to end. + t.Run("redacts secrets before pushing logs", func(t *testing.T) { + t.Parallel() + + const secret = "super-secret-value" + secretScript := ` +import secrets from "k6/secrets"; +export const options = { cloud: { name: 'Test cloud logs', projectID: 123456 } }; +export default async function() { + const s = await secrets.get("mykey"); + console.log("the secret is " + s); +};` + ts := makeTestState(t, secretScript, []string{ + "--local-execution", "--no-cloud-secrets", + "--secret-source=mock=mykey=" + secret, + }) + rec := setupLocalExecutionProvMock(t, ts) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + _, bodies := rec.snapshot() + require.NotEmpty(t, bodies, "expected at least one cloud log push") + all := strings.Join(bodies, "\n") + assert.Contains(t, all, "***SECRET_REDACTED***", + "the log line should reach the cloud only in redacted form") + assert.NotContains(t, all, secret, + "the raw secret must never reach the cloud log push") + }) +} + +// logsPushPath is the path the local-execution mocks use for the cloud +// logs push endpoint, overriding the real host baked into +// DefaultStartLocalExecutionResponse so tests never contact it. +const logsPushPath = "/logs/v1/push" + +// logPushRecorder captures cloud log pushes received by the mock server so +// tests can assert on the pusher wiring (auth header + test_run_id label). +type logPushRecorder struct { + mu sync.Mutex + auths []string + bodies []string +} + +func (r *logPushRecorder) handler(w http.ResponseWriter, req *http.Request) { + body, _ := io.ReadAll(req.Body) + r.mu.Lock() + r.auths = append(r.auths, req.Header.Get("Authorization")) + r.bodies = append(r.bodies, string(body)) + r.mu.Unlock() + w.WriteHeader(http.StatusNoContent) +} + +func (r *logPushRecorder) snapshot() (auths, bodies []string) { + r.mu.Lock() + defer r.mu.Unlock() + return append([]string(nil), r.auths...), append([]string(nil), r.bodies...) +} + +// setupLocalExecutionProvMock wires a provisioning mock server for a +// k6 cloud run --local-execution flow and points ts at it. It mirrors the +// handlers used by TestCloudRunLocalExecutionNoCloudSecrets. The returned +// recorder captures any cloud log pushes; the logs push_url is overridden +// onto the mock server so the real logs host is never contacted. +func setupLocalExecutionProvMock(t *testing.T, ts *GlobalTestState) *logPushRecorder { + t.Helper() + + rec := &logPushRecorder{} + srv := provtest.NewServer(t) + + srv.HandleCreateLoadTest(123456, func(w http.ResponseWriter, _ *http.Request) { + res := k6cloud.NewLoadTestApiModelWithDefaults() + res.SetId(provtest.DefaultLoadTestID) + writeProvJSON(w, http.StatusCreated, res) + }) + + srv.HandleStartLocalExecution(provtest.DefaultLoadTestID, func(w http.ResponseWriter, _ *http.Request) { + resp := provtest.DefaultStartLocalExecutionResponse() + uploadURL := srv.URL + provtest.PresignedUploadPath + resp.SetArchiveUploadUrl(uploadURL) + resp.SetTestRunDetailsPageUrl(fmt.Sprintf("%s/runs/%d", srv.URL, provtest.DefaultTestRunID)) + rc := resp.GetRuntimeConfig() + m := rc.GetMetrics() + m.SetPushUrl(srv.URL + "/v1/metrics") + rc.SetMetrics(m) + l := rc.GetLogs() + l.SetPushUrl(srv.URL + logsPushPath) + rc.SetLogs(l) + resp.SetRuntimeConfig(rc) + writeProvJSON(w, http.StatusOK, resp) + }) + + srv.HandlePresignedUpload(provtest.PresignedUploadPath, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.HandleFetchTestRun(provtest.DefaultTestRunID, []v6.TestProgress{ + {Status: v6.StatusInitializing}, + }) + + srv.HandleNotify(provtest.DefaultTestRunID, func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + srv.Mux.HandleFunc("POST "+logsPushPath, rec.handler) + + srv.Mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + ts.Env["K6_CLOUD_HOST"] = srv.URL + ts.Env["K6_CLOUD_HOST_V6"] = srv.URL + + return rec +} + func makeTestState(tb testing.TB, script string, cliFlags []string) *GlobalTestState { if cliFlags == nil { cliFlags = []string{"-v", "--log-output=stdout"} @@ -402,36 +1038,10 @@ func makeTestState(tb testing.TB, script string, cliFlags []string) *GlobalTestS return ts } -func parseMultipartRequest(r *http.Request) (map[string]string, error) { - // Parse the multipart form data - reader, err := r.MultipartReader() - if err != nil { - return nil, err - } - - // Initialize a map to store the parsed form data - formData := make(map[string]string) - - // Iterate through the parts - for { - part, nextErr := reader.NextPart() - if nextErr == io.EOF { - break - } - if nextErr != nil { - return nil, err - } - - // Read the part content - buf := new(bytes.Buffer) - _, err = io.Copy(buf, part) - if err != nil { - return nil, err - } - - // Store the part content in the map - formData[part.FormName()] = buf.String() +func writeProvJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + if err := json.NewEncoder(w).Encode(v); err != nil { + panic(fmt.Errorf("writeProvJSON: encoding JSON: %w", err)) } - - return formData, nil } diff --git a/internal/cmd/tests/cmd_cloud_test.go b/internal/cmd/tests/cmd_cloud_test.go index 929933ddb..4f326c689 100644 --- a/internal/cmd/tests/cmd_cloud_test.go +++ b/internal/cmd/tests/cmd_cloud_test.go @@ -17,6 +17,7 @@ import ( ) func TestCloudNoArgsShowsHelp(t *testing.T) { + requireFullBuild(t) t.Parallel() ts := NewGlobalTestState(t) @@ -26,6 +27,7 @@ func TestCloudNoArgsShowsHelp(t *testing.T) { stdout := ts.Stdout.String() t.Log(stdout) assert.Contains(t, stdout, "Run and manage Grafana Cloud tests", "expected help text to be shown") + assert.NotContains(t, stdout, "--vus", "run flags belong to `k6 cloud run`, not to `k6 cloud`") } type setupCommandFunc func(cliFlags []string) []string @@ -44,6 +46,20 @@ func runCloudTests(t *testing.T, setupCmd setupCommandFunc) { assert.Contains(t, stdout, `access token not configured`) }) + t.Run("TestCloudInvalidToken", func(t *testing.T) { + t.Parallel() + + ts := getSimpleCloudTestStateWithError(t, nil, setupCmd, nil, nil, http.StatusUnauthorized) + ts.ExpectedExitCode = -1 + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + t.Log(stdout) + assert.Contains(t, stdout, `Invalid token`, "the real API error message should still be shown") + assert.Contains(t, stdout, "hint=\"Authenticate by running `k6 cloud login` or verify the active Grafana Cloud token (K6_CLOUD_TOKEN, options.cloud.token)\"", + "a 401 should come with a recovery hint pointing at `k6 cloud login`") + }) + t.Run("TestCloudStackNotConfigured", func(t *testing.T) { t.Parallel() @@ -111,6 +127,54 @@ func runCloudTests(t *testing.T, setupCmd setupCommandFunc) { assert.Contains(t, stdout, `test status: Running`) }) + t.Run("TestCloudExitOnRunningEnv", func(t *testing.T) { + t.Parallel() + + // Same as TestCloudExitOnRunning, but driven by the environment + // variable instead of the CLI flag. Without the override taking + // effect, the command would keep polling the mock server forever, + // since its progress is always "Running". + ts := getSimpleCloudTestState(t, nil, setupCmd, []string{"--log-output=stdout"}, + v6test.Progress(cloudapiv6.StatusRunning, v6test.ResultNone)) + ts.Env["K6_EXIT_ON_RUNNING"] = "true" + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + t.Log(stdout) + assert.Contains(t, stdout, `execution: cloud`) + assert.Contains(t, stdout, `output: https://stack.grafana.com/a/k6-app/runs/123`) + assert.Contains(t, stdout, `test status: Running`) + }) + + t.Run("TestCloudExitOnRunningFlagOverridesEnv", func(t *testing.T) { + t.Parallel() + + // An explicitly set CLI flag takes precedence over the environment + // variable. If the "false" from the environment won instead, the + // command would poll the always-"Running" mock server forever. + ts := getSimpleCloudTestState(t, nil, setupCmd, []string{"--exit-on-running", "--log-output=stdout"}, + v6test.Progress(cloudapiv6.StatusRunning, v6test.ResultNone)) + ts.Env["K6_EXIT_ON_RUNNING"] = "false" + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + t.Log(stdout) + assert.Contains(t, stdout, `test status: Running`) + }) + + t.Run("TestCloudExitOnRunningInvalidEnv", func(t *testing.T) { + t.Parallel() + + ts := getSimpleCloudTestState(t, nil, setupCmd, nil, nil) + ts.Env["K6_EXIT_ON_RUNNING"] = "invalid" + ts.ExpectedExitCode = -1 + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + t.Log(stdout) + assert.Contains(t, stdout, `parsing K6_EXIT_ON_RUNNING returned an error`) + }) + t.Run("TestCloudURLFromStartResponse", func(t *testing.T) { t.Parallel() @@ -183,7 +247,21 @@ func cloudTestStartSimple(tb testing.TB, testRunID int) http.Handler { }) } -func getSimpleCloudTestState(t *testing.T, script []byte, setupCmd setupCommandFunc, cliFlags []string, progressCallback func() *cloudapiv6.TestProgress) *GlobalTestState { +func getSimpleCloudTestState( + t *testing.T, script []byte, setupCmd setupCommandFunc, cliFlags []string, + progressCallback func() *cloudapiv6.TestProgress, +) *GlobalTestState { + return getSimpleCloudTestStateWithError(t, script, setupCmd, cliFlags, progressCallback, 0) +} + +// getSimpleCloudTestStateWithError is getSimpleCloudTestState plus the +// ability to make the mock v6 API fail every request with errorStatus (e.g. +// http.StatusUnauthorized), for testing how the CLI surfaces a failing +// cloud API call to the user. +func getSimpleCloudTestStateWithError( + t *testing.T, script []byte, setupCmd setupCommandFunc, cliFlags []string, + progressCallback func() *cloudapiv6.TestProgress, errorStatus int, +) *GlobalTestState { if script == nil { script = []byte("export let options = { cloud: { projectID: 1 } };\nexport default function() {}") } @@ -194,6 +272,7 @@ func getSimpleCloudTestState(t *testing.T, script []byte, setupCmd setupCommandF srv := v6test.NewServer(t, v6test.Config{ ProgressCallback: progressCallback, + ErrorStatus: errorStatus, }) ts := NewGlobalTestState(t) diff --git a/internal/cmd/tests/cmd_cloud_test_list_test.go b/internal/cmd/tests/cmd_cloud_test_list_test.go index 84e84b01f..7159cffba 100644 --- a/internal/cmd/tests/cmd_cloud_test_list_test.go +++ b/internal/cmd/tests/cmd_cloud_test_list_test.go @@ -17,6 +17,7 @@ import ( const projectIDForTests = 42 func TestCloudTestList(t *testing.T) { + requireFullBuild(t) t.Parallel() t.Run("lists tests successfully using --project-id", func(t *testing.T) { diff --git a/internal/cmd/tests/cmd_cloud_upload_test.go b/internal/cmd/tests/cmd_cloud_upload_test.go index 4022f9909..038aca105 100644 --- a/internal/cmd/tests/cmd_cloud_upload_test.go +++ b/internal/cmd/tests/cmd_cloud_upload_test.go @@ -18,6 +18,7 @@ import ( ) func TestK6CloudUpload(t *testing.T) { + requireFullBuild(t) t.Parallel() t.Run("TestCloudUploadUserNotAuthenticated", func(t *testing.T) { @@ -33,6 +34,21 @@ func TestK6CloudUpload(t *testing.T) { assert.Contains(t, stdout, `access token not configured`) }) + t.Run("TestCloudUploadInvalidEnv", func(t *testing.T) { + t.Parallel() + + // The upload command doesn't define the --exit-on-running flag, but it + // still validates the corresponding env variable for wrong values. + ts := getSimpleCloudTestState(t, nil, setupK6CloudUploadCmd, nil, nil) + ts.Env["K6_EXIT_ON_RUNNING"] = "invalid" + ts.ExpectedExitCode = -1 + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + t.Log(stdout) + assert.Contains(t, stdout, `parsing K6_EXIT_ON_RUNNING returned an error`) + }) + t.Run("TestCloudUploadWithScript", func(t *testing.T) { t.Parallel() diff --git a/internal/cmd/tests/cmd_once_test.go b/internal/cmd/tests/cmd_once_test.go new file mode 100644 index 000000000..418e7b335 --- /dev/null +++ b/internal/cmd/tests/cmd_once_test.go @@ -0,0 +1,177 @@ +package tests + +import ( + "bytes" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/executor" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunOnce(t *testing.T) { + t.Parallel() + + script := ` + import { Counter } from 'k6/metrics'; + + export const options = { + tags: { top: 'level' }, + thresholds: { 'hits{top:level}': ['count==1'] }, + }; + + const hits = new Counter('hits'); + + export default function () { + hits.add(1); + console.log('once ran'); + } + ` + + ts := getSingleFileTestState(t, script, []string{"--log-output=stdout", "--once"}, 0) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, "1 iterations shared among 1 VUs") + assert.Equal(t, 1, strings.Count(stdout, "once ran")) + assert.Contains(t, stdout, "hits{top:level}") + assert.Contains(t, stdout, "'count==1' count=1") +} + +func TestRunOnceRejectsMultipleScenarios(t *testing.T) { + t.Parallel() + + script := ` + export const options = { scenarios: { + s1: { executor: 'shared-iterations' }, + s2: { executor: 'shared-iterations' } + }}; + export default function() {} + ` + + ts := getSingleFileTestState(t, script, + []string{"--log-output=stdout", "--once"}, exitcodes.InvalidConfig) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + assert.Contains(t, ts.Stdout.String(), + "--once can run only with a single scenario") +} + +const onceCloudScript = ` + export const options = { + cloud: { name: 'once', projectID: 123456 }, + scenarios: { + ui: { executor: 'shared-iterations', vus: 3, iterations: 9 } + } + }; + export default function() { console.log('once ran'); } +` + +func mustExtractSingleSharedIterScenario(t *testing.T, opts lib.Options) executor.SharedIterationsConfig { + t.Helper() + + require.Len(t, opts.Scenarios, 1) + for _, sc := range opts.Scenarios { + shared, ok := sc.(executor.SharedIterationsConfig) + require.True(t, ok) + return shared + } + return executor.SharedIterationsConfig{} +} + +func TestCloudRunOnceLocalExecution(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + ts := makeTestState(t, onceCloudScript, + []string{"--log-output=stdout", "--once", "--local-execution"}) + setupLocalExecutionProvMock(t, ts) + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, "1 iterations shared among 1 VUs") + assert.Equal(t, 1, strings.Count(stdout, "once ran")) +} + +func TestCloudRunOnceUploadsRewrittenArchive(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + _, tarData := uploadAndCaptureArchive(t, + []string{"k6", "cloud", "run", "--once", "test.js"}, nil, onceCloudScript) + + arc, err := lib.ReadArchive(bytes.NewReader(tarData)) + require.NoError(t, err) + + sc := mustExtractSingleSharedIterScenario(t, arc.Options) + assert.Equal(t, null.IntFrom(1), sc.VUs) + assert.Equal(t, null.IntFrom(1), sc.Iterations) +} + +func buildArchive(t *testing.T, script string, flags ...string) []byte { + t.Helper() + + ts := NewGlobalTestState(t) + tarPath := filepath.Join(ts.Cwd, "archive.tar") + require.NoError(t, fsext.WriteFile( + ts.FS, filepath.Join(ts.Cwd, "test.js"), []byte(script), 0o644)) + ts.CmdArgs = append(append([]string{"k6", "archive"}, flags...), "-O", tarPath, "test.js") + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + tarData, err := fsext.ReadFile(ts.FS, tarPath) + require.NoError(t, err) + + return tarData +} + +func TestArchiveOnceWritesRewrittenOptions(t *testing.T) { + t.Parallel() + + script := ` + export const options = { scenarios: { + ui: { executor: 'constant-vus', vus: 3, duration: '2s' } + }}; + export default function() {} + ` + + arc, err := lib.ReadArchive(bytes.NewReader(buildArchive(t, script, "--once"))) + require.NoError(t, err) + + sc := mustExtractSingleSharedIterScenario(t, arc.Options) + assert.Equal(t, null.IntFrom(1), sc.VUs) + assert.Equal(t, null.IntFrom(1), sc.Iterations) +} + +func TestRunOnceRewritesPlainArchive(t *testing.T) { + t.Parallel() + + script := ` + export const options = { scenarios: { + ui: { executor: 'shared-iterations', vus: 3, iterations: 9 } + }}; + export default function() { console.log('once ran'); } + ` + + tarData := buildArchive(t, script) + + ts := NewGlobalTestState(t) + tarPath := filepath.Join(ts.Cwd, "archive.tar") + require.NoError(t, fsext.WriteFile(ts.FS, tarPath, tarData, 0o644)) + ts.CmdArgs = []string{"k6", "run", "--log-output=stdout", "--once", tarPath} + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, "1 iterations shared among 1 VUs") + assert.Equal(t, 1, strings.Count(stdout, "once ran")) +} diff --git a/internal/cmd/tests/cmd_optout_config_file_test.go b/internal/cmd/tests/cmd_optout_config_file_test.go new file mode 100644 index 000000000..830c227c7 --- /dev/null +++ b/internal/cmd/tests/cmd_optout_config_file_test.go @@ -0,0 +1,86 @@ +package tests + +import ( + "bytes" + "net/http" + "net/http/httptest" + "path/filepath" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/cmd/state" + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +// TestConfigFileOptOut covers the config-file opt-out (noUsageReport: true): +// alone it must suppress the run report, while K6_NO_USAGE_REPORT=false must +// win over it and re-enable reporting. +func TestConfigFileOptOut(t *testing.T) { + t.Parallel() + + tt := []struct { + name string + args []string + stdin string + config string // config file content; empty means {"noUsageReport": true} + envOptOut string // K6_NO_USAGE_REPORT; empty leaves it unset + wantReports int32 + }{ + { + name: "config file opt-out suppresses the run report", + args: []string{"run", "-"}, + stdin: `export default function() {};`, + }, + { + name: "env re-enables the run report over the config file", + args: []string{"run", "-"}, + stdin: `export default function() {};`, + envOptOut: "false", + wantReports: 1, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + var reportCount atomic.Int32 + reportServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + reportCount.Add(1) + } + w.WriteHeader(http.StatusOK) + })) + t.Cleanup(reportServer.Close) + + ts := NewGlobalTestState(t) + // The default env opt-out would mask the config file gate. + delete(ts.Env, "K6_NO_USAGE_REPORT") + if tc.envOptOut != "" { + ts.Env["K6_NO_USAGE_REPORT"] = tc.envOptOut + } + ts.Env[state.UsageReportURL] = reportServer.URL + + config := tc.config + if config == "" { + config = `{"noUsageReport": true}` + } + require.NoError(t, ts.FS.MkdirAll(filepath.Dir(ts.Flags.ConfigFilePath), 0o755)) + require.NoError(t, fsext.WriteFile(ts.FS, ts.Flags.ConfigFilePath, []byte(config), 0o600)) + + ts.CmdArgs = append([]string{"k6"}, tc.args...) + if tc.stdin != "" { + ts.Stdin = bytes.NewBufferString(tc.stdin) + } + ts.ReparseFlags() + + cmd.ExecuteWithGlobalState(ts.GlobalState) + + require.Equal(t, tc.wantReports, reportCount.Load(), + "unexpected usage report count with noUsageReport set in the config file") + }) + } +} diff --git a/internal/cmd/tests/cmd_run_async_context_test.go b/internal/cmd/tests/cmd_run_async_context_test.go new file mode 100644 index 000000000..09bc562eb --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_context_test.go @@ -0,0 +1,58 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunAsyncMetricContextWithoutK6Module(t *testing.T) { + t.Parallel() + + script := ` + import exec from 'k6/execution'; + import { Counter } from 'k6/metrics'; + + const callbackPhases = new Counter('callback_phases'); + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); + + export default function () { + let release; + const gate = new Promise(resolve => { release = resolve; }); + exec.vu.metrics.tags.owner = 'registered'; + exec.vu.metrics.metadata.trace = 'registered'; + const pending = gate.then(async () => { + for (const phase of ['first', 'second', 'third']) { + exec.vu.metrics.tags.phase = phase; + exec.vu.metrics.metadata.step = phase; + if (phase === 'second') { + await delay(0); + } else { + await Promise.resolve(); + } + callbackPhases.add(1, {phase_name: phase}); + } + }); + exec.vu.metrics.tags.owner = 'root'; + exec.vu.metrics.metadata.trace = 'root'; + release(); + return pending; + } + ` + + ts := getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + for _, phase := range []string{"first", "second", "third"} { + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "callback_phases", map[string]string{ + "owner": "registered", "phase": phase, "phase_name": phase, + }, map[string]string{"trace": "registered", "step": phase})) + } +} diff --git a/internal/cmd/tests/cmd_run_async_group_concurrent_modules_test.go b/internal/cmd/tests/cmd_run_async_group_concurrent_modules_test.go new file mode 100644 index 000000000..ff43d84a5 --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_group_concurrent_modules_test.go @@ -0,0 +1,234 @@ +package tests + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/internal/lib/testutils/httpmultibin" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunConcurrentAsyncGroupsAcrossModules(t *testing.T) { + t.Parallel() + + httpBin := httpmultibin.NewHTTPMultiBin(t) + grpcBin := NewGRPC(t) + script := httpBin.Replacer.Replace(grpcBin.Replacer.Replace(` + import { check, group } from 'k6'; + import exec from 'k6/execution'; + import http from 'k6/http'; + import { Client, Stream } from 'k6/net/grpc'; + import { WebSocket } from 'k6/websockets'; + + export const options = { + hosts: { 'HTTPBIN_DOMAIN': 'HTTPBIN_IP' }, + }; + + const client = new Client(); + client.load([], './route_guide.proto'); + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); + const phases = ['first', 'second', 'third']; + const changeContext = (name, phase) => { + const value = name + ':' + phase; + exec.vu.metrics.tags.phase = value; + exec.vu.metrics.metadata.step = value; + }; + const crossBoundary = phase => phase === 'second' ? delay(0) : Promise.resolve(); + const recordContext = (name, phase) => check(null, {[name + ':' + phase]: true}); + + export default async function () { + client.connect('GRPCBIN_ADDR', { plaintext: true }); + exec.vu.metrics.tags.owner = 'root'; + exec.vu.metrics.tags.phase = 'root'; + exec.vu.metrics.metadata.trace = 'root'; + exec.vu.metrics.metadata.step = 'root'; + + let release; + const gate = new Promise(resolve => { release = resolve; }); + let started = 0; + const start = (name, operation) => { + exec.vu.metrics.tags.owner = name; + exec.vu.metrics.metadata.trace = name; + const pending = group(name, () => { + started++; + check(null, {[name + ':start']: true}); + return operation(); + }); + exec.vu.metrics.tags.owner = 'root'; + exec.vu.metrics.metadata.trace = 'root'; + return pending; + }; + + const pending = [ + start('http', async () => { + await gate; + for (const phase of phases) { + changeContext('http', phase); + await crossBoundary(phase); + recordContext('http', phase); + } + const response = await http.asyncRequest('GET', 'HTTPBIN_URL/get'); + check(response, {'http:finish': value => value.status === 200}); + }), + start('websocket', () => new Promise((resolve, reject) => { + const ws = new WebSocket('WSBIN_URL/ws-echo'); + ws.addEventListener('open', () => ws.send('hello')); + ws.addEventListener('message', async event => { + try { + check(event, {'websocket:message': value => value.data === 'hello'}); + for (const phase of phases) { + changeContext('websocket', phase); + await crossBoundary(phase); + recordContext('websocket', phase); + } + ws.close(); + } catch (error) { + reject(error); + } + }); + ws.addEventListener('close', () => { + check(null, {'websocket:finish': true}); + resolve(); + }); + ws.addEventListener('error', () => reject(new Error('WebSocket failed'))); + })), + start('grpc', () => new Promise((resolve, reject) => { + const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures'); + let handlingData = false; + let dataHandled = false; + let streamEnded = false; + const finish = () => { + if (dataHandled && streamEnded) resolve(); + }; + stream.on('data', async data => { + if (handlingData) return; + handlingData = true; + try { + check(data, {'grpc:data': value => value.name !== ''}); + for (const phase of phases) { + changeContext('grpc', phase); + await crossBoundary(phase); + recordContext('grpc', phase); + } + dataHandled = true; + finish(); + } catch (error) { + reject(error); + } + }); + stream.on('end', () => { + check(null, {'grpc:finish': true}); + streamEnded = true; + finish(); + }); + stream.on('error', reject); + stream.write({ + lo: { latitude: 407838351, longitude: -746143763 }, + hi: { latitude: 407838351, longitude: -746143763 }, + }); + stream.end(); + })), + start('timer', () => new Promise((resolve, reject) => { + setTimeout(async () => { + try { + for (const phase of phases) { + changeContext('timer', phase); + await crossBoundary(phase); + recordContext('timer', phase); + } + check(null, {'timer:finish': true}); + resolve(); + } catch (error) { + reject(error); + } + }, 1); + })), + start('promise', () => gate.then(async () => { + for (const phase of phases) { + changeContext('promise', phase); + await crossBoundary(phase); + recordContext('promise', phase); + } + check(null, {'promise:finish': true}); + })), + ]; + + // The event loop cannot run any protocol or timer callback before this point. + check(null, {'all-started': () => started === pending.length}); + release(); + await Promise.all(pending); + check(null, {'outside:finish': true}); + client.close(); + } + `)) + + ts := getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + proto, err := os.ReadFile(projectRootPath + "internal/lib/testutils/grpcservice/route_guide.proto") //nolint:forbidigo + require.NoError(t, err) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "route_guide.proto"), proto, 0o644)) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + for _, name := range []string{"http", "websocket", "grpc", "timer", "promise"} { + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": name + ":start", "group": "::" + name, "owner": name, + }, map[string]string{"trace": name})) + assert.Len(t, getSampleValuesWithMetadata(t, results, "group_duration", map[string]string{ + "group": "::" + name, "owner": name, "phase": "root", + }, map[string]string{"trace": name, "step": "root"}), 1) + for _, phase := range []string{"first", "second", "third"} { + value := name + ":" + phase + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": value, "group": "::" + name, "owner": name, "phase": value, + }, map[string]string{"trace": name, "step": value})) + } + } + + for _, expected := range []struct { + check string + group string + phase string + }{ + {"http:finish", "http", "http:third"}, + {"websocket:message", "websocket", "root"}, + {"websocket:finish", "websocket", "root"}, + {"grpc:finish", "grpc", "root"}, + {"timer:finish", "timer", "timer:third"}, + {"promise:finish", "promise", "promise:third"}, + } { + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": expected.check, "group": "::" + expected.group, "owner": expected.group, + "phase": expected.phase, + }, map[string]string{"trace": expected.group, "step": expected.phase})) + } + assert.NotEmpty(t, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": "grpc:data", "group": "::grpc", "owner": "grpc", "phase": "root", + }, map[string]string{"trace": "grpc", "step": "root"})) + + for _, expected := range []struct { + metric string + group string + phase string + }{ + {"http_reqs", "http", "http:third"}, + {"ws_msgs_received", "websocket", "root"}, + {"grpc_streams_msgs_received", "grpc", "root"}, + } { + assert.NotEmpty(t, getSampleValuesWithMetadata(t, results, expected.metric, map[string]string{ + "group": "::" + expected.group, "owner": expected.group, "phase": expected.phase, + }, map[string]string{"trace": expected.group, "step": expected.phase})) + } + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": "all-started", "group": "", "owner": "root", "phase": "root", + }, map[string]string{"trace": "root", "step": "root"})) + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "checks", map[string]string{ + "check": "outside:finish", "group": "", "owner": "root", "phase": "root", + }, map[string]string{"trace": "root", "step": "root"})) +} diff --git a/internal/cmd/tests/cmd_run_async_group_grpc_test.go b/internal/cmd/tests/cmd_run_async_group_grpc_test.go new file mode 100644 index 000000000..73767e2b1 --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_group_grpc_test.go @@ -0,0 +1,66 @@ +package tests + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunAsyncGroupGRPCStreamListeners(t *testing.T) { + t.Parallel() + + tb := NewGRPC(t) + script := tb.Replacer.Replace(` + import { check, group } from 'k6'; + import exec from 'k6/execution'; + import { Client, Stream } from 'k6/net/grpc'; + + const client = new Client(); + client.load([], './route_guide.proto'); + + export default async function () { + client.connect('GRPCBIN_ADDR', { plaintext: true }); + const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures'); + await new Promise((resolve, reject) => { + exec.vu.metrics.tags.owner = 'registered'; + group('grpcgroup', () => { + stream.on('data', data => check(data, { data: value => value.name !== '' })); + }); + exec.vu.metrics.tags.owner = 'active'; + stream.on('end', () => { + check(null, { end: true }); + client.close(); + resolve(); + }); + stream.on('error', reject); + stream.write({ + lo: { latitude: 407838351, longitude: -746143763 }, + hi: { latitude: 407838351, longitude: -746143763 }, + }); + stream.end(); + }); + } + `) + + ts := getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + proto, err := os.ReadFile(projectRootPath + "internal/lib/testutils/grpcservice/route_guide.proto") //nolint:forbidigo + require.NoError(t, err) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "route_guide.proto"), proto, 0o644)) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + assert.NotEmpty(t, getSampleValues(t, results, "checks", map[string]string{ + "check": "data", "group": "::grpcgroup", "owner": "registered", + })) + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "end", "group": "", "owner": "active", + })) +} diff --git a/internal/cmd/tests/cmd_run_async_group_http_test.go b/internal/cmd/tests/cmd_run_async_group_http_test.go new file mode 100644 index 000000000..3a8e87e65 --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_group_http_test.go @@ -0,0 +1,50 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/internal/lib/testutils/httpmultibin" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunAsyncGroupHTTP(t *testing.T) { + t.Parallel() + + tb := httpmultibin.NewHTTPMultiBin(t) + script := tb.Replacer.Replace(` + import http from 'k6/http'; + import { check, group } from 'k6'; + + export const options = { + hosts: { 'HTTPBIN_DOMAIN': 'HTTPBIN_IP' }, + }; + + export default async function () { + await group('async request', async () => { + const response = await http.asyncRequest('GET', 'HTTPBIN_URL/get'); + check(response, { inside: response => response.status === 200 }); + }); + check(null, { after: true }); + } + `) + + ts := getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "inside", "group": "::async request", + })) + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "after", "group": "", + })) + assert.Len(t, getSampleValues(t, results, "http_reqs", map[string]string{ + "group": "::async request", + }), 1) +} diff --git a/internal/cmd/tests/cmd_run_async_group_test.go b/internal/cmd/tests/cmd_run_async_group_test.go new file mode 100644 index 000000000..0c98eaef4 --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_group_test.go @@ -0,0 +1,180 @@ +package tests + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +func runAsyncGroupIntegrationScript(t *testing.T, script string, archived bool) []byte { + t.Helper() + + var ts *GlobalTestState + if archived { + archiveState := NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile( + archiveState.FS, filepath.Join(archiveState.Cwd, "test.js"), []byte(script), 0o644)) + archiveState.CmdArgs = []string{"k6", "archive", "test.js"} + cmd.ExecuteWithGlobalState(archiveState.GlobalState) + + archive, err := fsext.ReadFile(archiveState.FS, "archive.tar") + require.NoError(t, err) + ts = NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "archive.tar"), archive, 0o644)) + ts.CmdArgs = []string{ + "k6", "run", "--features", "async-metric-context", "--out", "json=results.json", + "--summary-mode=disabled", "archive.tar", + } + } else { + ts = getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + } + + cmd.ExecuteWithGlobalState(ts.GlobalState) + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + return results +} + +func TestRunAsyncGroupSetupAndTeardownReactionsKeepStageGroup(t *testing.T) { + t.Parallel() + + script := ` + import { check, group } from 'k6'; + + export const options = { + setupTimeout: '1s', + teardownTimeout: '1s', + systemTags: ['group', 'check'], + }; + + function queueReaction(stage) { + Promise.resolve().then(() => check(null, {[stage]: true})); + group('warmup', () => {}); + } + + export function setup() { queueReaction('setup'); } + export default function() {} + export function teardown() { queueReaction('teardown'); } + ` + + for _, variant := range []struct { + name string + archived bool + }{{"source", false}, {"archive", true}} { + t.Run(variant.name, func(t *testing.T) { + t.Parallel() + results := runAsyncGroupIntegrationScript(t, script, variant.archived) + for _, stage := range []string{"setup", "teardown"} { + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": stage, "group": "::" + stage, + })) + } + }) + } +} + +func TestRunOverlappingAsyncGroupsEmitIndependentTags(t *testing.T) { + t.Parallel() + + script := ` + import { check, group } from 'k6'; + + export const options = { systemTags: ['group', 'check'] }; + + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); + + export default async function() { + const names = ['alpha', 'beta', 'gamma', 'delta']; + const grouped = names.map((name, index) => group(name, async () => { + check(null, {[name + ':start']: true}); + await delay((names.length - index) * 2); + check(null, {[name + ':finish']: true}); + })); + await Promise.all(grouped); + check(null, {'outside:after': true}); + } + ` + + for _, variant := range []struct { + name string + archived bool + }{{"source", false}, {"archive", true}} { + t.Run(variant.name, func(t *testing.T) { + t.Parallel() + results := runAsyncGroupIntegrationScript(t, script, variant.archived) + for _, group := range []string{"alpha", "beta", "gamma", "delta"} { + for _, phase := range []string{"start", "finish"} { + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": group + ":" + phase, "group": "::" + group, + })) + } + assert.Len(t, getSampleValues(t, results, "group_duration", map[string]string{ + "group": "::" + group, + }), 1) + } + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "outside:after", "group": "", + })) + }) + } +} + +func TestRunAsyncGroupSampleTagOverrideDoesNotMutateContext(t *testing.T) { + t.Parallel() + + script := ` + import { group } from 'k6'; + import exec from 'k6/execution'; + import { Counter } from 'k6/metrics'; + + const events = new Counter('events'); + + function setContext(owner, phase) { + exec.vu.metrics.tags.owner = owner; + exec.vu.metrics.tags.phase = phase; + exec.vu.metrics.metadata.trace = owner; + exec.vu.metrics.metadata.step = phase; + } + + export default function () { + setContext('root', 'root'); + + group('checkout', () => { + setContext('checkout', 'body'); + + events.add(1, { kind: 'grouped' }); + events.add(1, { kind: 'ungrouped', group: '' }); + events.add(1, { kind: 'grouped-again' }); + }); + + events.add(1, { kind: 'outside' }); + } + ` + + results := runAsyncGroupIntegrationScript(t, script, false) + for _, sample := range []struct { + kind string + group string + owner string + phase string + }{ + {kind: "grouped", group: "::checkout", owner: "checkout", phase: "body"}, + {kind: "ungrouped", group: "", owner: "checkout", phase: "body"}, + {kind: "grouped-again", group: "::checkout", owner: "checkout", phase: "body"}, + {kind: "outside", group: "", owner: "root", phase: "root"}, + } { + assert.Equal(t, []float64{1}, getSampleValuesWithMetadata(t, results, "events", map[string]string{ + "kind": sample.kind, "group": sample.group, "owner": sample.owner, "phase": sample.phase, + }, map[string]string{"trace": sample.owner, "step": sample.phase})) + } + + assert.Len(t, getSampleValuesWithMetadata(t, results, "group_duration", map[string]string{ + "group": "::checkout", "owner": "root", "phase": "root", + }, map[string]string{"trace": "root", "step": "root"}), 1) +} diff --git a/internal/cmd/tests/cmd_run_async_group_websockets_test.go b/internal/cmd/tests/cmd_run_async_group_websockets_test.go new file mode 100644 index 000000000..fe773b223 --- /dev/null +++ b/internal/cmd/tests/cmd_run_async_group_websockets_test.go @@ -0,0 +1,60 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/internal/lib/testutils/httpmultibin" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestRunAsyncGroupWebSocketListeners(t *testing.T) { + t.Parallel() + + tb := httpmultibin.NewHTTPMultiBin(t) + script := tb.Replacer.Replace(` + import { check, group } from 'k6'; + import exec from 'k6/execution'; + import { WebSocket } from 'k6/websockets'; + + export const options = { + hosts: { 'HTTPBIN_DOMAIN': 'HTTPBIN_IP' }, + }; + + export default function () { + exec.vu.metrics.tags.owner = 'registered'; + const ws = new WebSocket('WSBIN_URL/ws-echo'); + group('wsgroup', () => { + ws.onopen = () => { + check(null, { open: true }); + exec.vu.metrics.tags.callback = 'open'; + ws.send('something'); + }; + ws.addEventListener('message', () => { + check(null, { message: true }); + ws.close(); + }); + }); + exec.vu.metrics.tags.owner = 'active'; + ws.onclose = () => check(null, { close: true }); + } + `) + + ts := getSingleFileTestState(t, script, + []string{"--features", "async-metric-context", "--out", "json=results.json", "--summary-mode=disabled"}, 0) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + for _, check := range []string{"open", "message"} { + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": check, "group": "::wsgroup", "owner": "registered", + })) + } + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "close", "group": "", "owner": "active", + })) +} diff --git a/internal/cmd/tests/cmd_run_features_test.go b/internal/cmd/tests/cmd_run_features_test.go index b1ccee032..c0e8fa714 100644 --- a/internal/cmd/tests/cmd_run_features_test.go +++ b/internal/cmd/tests/cmd_run_features_test.go @@ -64,7 +64,7 @@ func TestRunWithoutFeatureFlag(t *testing.T) { tagged := runAndNativeHistTagged(t, ts) assert.Empty(t, tagged, "no feature tag without activation") - assert.Nil(t, ts.Usage.Map()["features"]) + assert.Equal(t, []string{"async-metric-context"}, ts.Usage.Map()["features"]) for _, e := range ts.LoggerHook.Drain() { assert.NotEqual(t, "native-histograms", e.Data["feature"]) @@ -154,7 +154,8 @@ func TestRunWarnsOnScriptOptionsFeatures(t *testing.T) { tagged := runAndNativeHistTagged(t, ts) assert.Empty(t, tagged, "options.features must not activate the flag") - assert.Nil(t, ts.Usage.Map()["features"], "options.features must not contribute to telemetry") + assert.Equal(t, []string{"async-metric-context"}, ts.Usage.Map()["features"], + "only the fork default, not options.features, should be active") var sawOptionsWarn bool for _, e := range ts.LoggerHook.Drain() { diff --git a/internal/cmd/tests/cmd_run_merge_run_tags_test.go b/internal/cmd/tests/cmd_run_merge_run_tags_test.go new file mode 100644 index 000000000..51cecc3d8 --- /dev/null +++ b/internal/cmd/tests/cmd_run_merge_run_tags_test.go @@ -0,0 +1,89 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +const mergeRunTagsScript = ` + export const options = { + tags: { env: 'staging', team: 'backend' }, + scenarios: { + s: { executor: 'per-vu-iterations', vus: 1, iterations: 1 }, + }, + }; + export default function () {} +` + +// Without the flag, CLI --tag replaces script-level options.tags wholesale. +// Samples should NOT carry the script-level `env` tag. +func TestRunMergeRunTagsFlagOff(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, mergeRunTagsScript, + []string{"--out", "json=results.json", "--no-usage-report", "--tag", "region=us-east"}, 0) + + results := runAndReadResults(t, ts) + + withScriptTag := getSampleValues(t, results, "iterations", map[string]string{"env": "staging"}) + assert.Empty(t, withScriptTag, "script-level tags must be replaced wholesale when the flag is off") + + withCLITag := getSampleValues(t, results, "iterations", map[string]string{"region": "us-east"}) + assert.NotEmpty(t, withCLITag, "CLI --tag must still apply") +} + +// With the flag on, CLI --tag, script options.tags, and env K6_TAGS all contribute. +func TestRunMergeRunTagsFlagOn(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, mergeRunTagsScript, + []string{ + "--out", "json=results.json", "--no-usage-report", + "--features", "merge-run-tags", + "--tag", "region=us-east", + }, 0) + + results := runAndReadResults(t, ts) + + for k, v := range map[string]string{ + "env": "staging", // from script options.tags + "team": "backend", // from script options.tags + "region": "us-east", // from CLI --tag + } { + samples := getSampleValues(t, results, "iterations", map[string]string{k: v}) + assert.NotEmpty(t, samples, "expected merged tag %s=%s", k, v) + } +} + +// Higher-priority layers must win on key collision when the flag is on. +func TestRunMergeRunTagsCLIWinsOnCollision(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, mergeRunTagsScript, + []string{ + "--out", "json=results.json", "--no-usage-report", + "--features", "merge-run-tags", + "--tag", "env=prod", + }, 0) + + results := runAndReadResults(t, ts) + + wins := getSampleValues(t, results, "iterations", map[string]string{"env": "prod"}) + assert.NotEmpty(t, wins, "CLI --tag must win over script-level options.tags on collision") + + loses := getSampleValues(t, results, "iterations", map[string]string{"env": "staging"}) + assert.Empty(t, loses, "lower-priority value must not appear when the higher-priority layer overrides it") +} + +func runAndReadResults(t *testing.T, ts *GlobalTestState) []byte { + t.Helper() + cmd.ExecuteWithGlobalState(ts.GlobalState) + data, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + return data +} diff --git a/internal/cmd/tests/cmd_run_test.go b/internal/cmd/tests/cmd_run_test.go index b70f6cb80..d3219d632 100644 --- a/internal/cmd/tests/cmd_run_test.go +++ b/internal/cmd/tests/cmd_run_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "maps" "net/http" "net/http/httptest" "os" @@ -166,6 +167,9 @@ func TestBinaryNameHelpStdout(t *testing.T) { } for _, tt := range tests { + if leanBuild && tt.cmdName == "cloud" { + continue + } ts.CmdArgs = []string{ts.BinaryName, "help", tt.cmdName, tt.extraCmd} cmd.ExecuteWithGlobalState(ts.GlobalState) stdout := ts.Stdout.String() @@ -521,7 +525,7 @@ func TestExecutionTestOptionsDefaultValues(t *testing.T) { loglines := ts.LoggerHook.Drain() require.Len(t, loglines, 1) - expected := `{"paused":null,"executionSegment":null,"executionSegmentSequence":null,"noSetup":null,"setupTimeout":null,"noTeardown":null,"teardownTimeout":null,"rps":null,"dns":{"ttl":null,"select":null,"policy":null},"maxRedirects":null,"userAgent":null,"batch":null,"batchPerHost":null,"httpDebug":null,"httpErrorData":null,"insecureSkipTLSVerify":null,"tlsCipherSuites":null,"tlsVersion":null,"tlsAuth":null,"throw":null,"thresholds":null,"blacklistIPs":null,"blockHostnames":null,"hosts":null,"noConnectionReuse":null,"noVUConnectionReuse":null,"minIterationDuration":null,"ext":null,"summaryTrendStats":["avg", "min", "med", "max", "p(90)", "p(95)"],"summaryTimeUnit":null,"systemTags":["check","error","error_code","expected_response","group","method","name","proto","scenario","service","status","subproto","tls_version","url"],"tags":null,"metricSamplesBufferSize":null,"noCookiesReset":null,"discardResponseBodies":null,"consoleOutput":null,"scenarios":{"default":{"vus":null,"iterations":1,"executor":"shared-iterations","maxDuration":null,"startTime":null,"env":null,"tags":null,"gracefulStop":null,"exec":null}},"localIPs":null,"features":null}` + expected := `{"paused":null,"executionSegment":null,"executionSegmentSequence":null,"noSetup":null,"setupTimeout":null,"noTeardown":null,"teardownTimeout":null,"rps":null,"dns":{"ttl":null,"select":null,"policy":null},"maxRedirects":null,"userAgent":null,"batch":null,"batchPerHost":null,"httpErrorData":null,"httpDebug":null,"insecureSkipTLSVerify":null,"tlsCipherSuites":null,"tlsVersion":null,"tlsAuth":null,"tlsAIAFetch":null,"throw":null,"thresholds":null,"blacklistIPs":null,"blockHostnames":null,"hosts":null,"noConnectionReuse":null,"noVUConnectionReuse":null,"minIterationDuration":null,"ext":null,"summaryTrendStats":["avg", "min", "med", "max", "p(90)", "p(95)"],"summaryTimeUnit":null,"systemTags":["check","error","error_code","expected_response","group","method","name","proto","scenario","service","status","subproto","tls_version","url"],"tags":{"k6_feature_async_metric_context":"true"},"metricSamplesBufferSize":null,"noCookiesReset":null,"discardResponseBodies":null,"consoleOutput":null,"scenarios":{"default":{"vus":null,"iterations":1,"executor":"shared-iterations","maxDuration":null,"startTime":null,"env":null,"tags":null,"gracefulStop":null,"exec":null}},"localIPs":null,"handleSummaryTimeout":null,"features":null}` assert.JSONEq(t, expected, loglines[0].Message) } @@ -576,6 +580,7 @@ func getCloudTestEndChecker( tb testing.TB, testRunID int, testStart http.Handler, expRunStatus cloudapi.RunStatus, expResultStatus cloudapi.ResultStatus, ) *httptest.Server { + requireFullBuild(tb) testFinished := false if testStart == nil { @@ -712,7 +717,7 @@ func TestSetupTeardownThresholds(t *testing.T) { assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Running thresholds on 4 metrics...")) assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Finalizing thresholds...")) assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Metrics emission of VUs and VUsMax metrics stopped")) - assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Metrics and traces processing finished!")) + assert.True(t, testutils.LogContains(logMsgs, logrus.DebugLevel, "Metrics processing finished!")) } func TestThresholdsFailed(t *testing.T) { @@ -796,7 +801,7 @@ func TestAbortedByThreshold(t *testing.T) { assert.Contains(t, stdOut, "iterations\n ✗ 'count == 1'") assert.Contains(t, stdOut, `teardown() called`) assert.Contains(t, stdOut, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) - assert.Contains(t, stdOut, `level=debug msg="Metrics and traces processing finished!"`) + assert.Contains(t, stdOut, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdOut, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=8 tainted=true`) } @@ -858,7 +863,7 @@ func TestAbortedByUserWithGoodThresholds(t *testing.T) { ✓ 'count == 1' count=1`) assert.Contains(t, stdout, `Stopping k6 in response to signal`) assert.Contains(t, stdout, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) - assert.Contains(t, stdout, `level=debug msg="Metrics and traces processing finished!"`) + assert.Contains(t, stdout, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdout, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=5 tainted=false`) } @@ -982,7 +987,7 @@ func TestAbortedByUserWithRestAPI(t *testing.T) { assert.Contains(t, stdout, `PATCH /v1/status`) assert.Contains(t, stdout, `level=error msg="test run stopped from REST API`) assert.Contains(t, stdout, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) - assert.Contains(t, stdout, `level=debug msg="Metrics and traces processing finished!"`) + assert.Contains(t, stdout, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdout, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=5 tainted=false`) assert.NotContains(t, stdout, `Running thresholds`) assert.NotContains(t, stdout, `Finalizing thresholds`) @@ -1186,6 +1191,41 @@ func runTestWithLinger(t *testing.T, ts *GlobalTestState) { cmd.ExecuteWithGlobalState(ts.GlobalState) } +func TestExecutionResultWithLinger(t *testing.T) { + requireFullBuild(t) + t.Parallel() + + addr := getFreeBindAddr(t) + script := ` + import exec from 'k6/execution'; + export default function () { exec.test.abort('foo'); } + ` + ts := getSingleFileTestState(t, script, + []string{"-v", "--log-output=stdout", "--linger", "--address", addr}, exitcodes.ScriptAborted) + ts.Flags.Address = addr + + sendSignal := injectMockSignalNotifier(ts) + asyncWaitForStdoutAndRun(t, ts, 15, time.Second, "waiting for Ctrl+C to continue", func() { + defer func() { + sendSignal <- syscall.SIGINT + <-sendSignal + }() + + req, err := http.NewRequestWithContext(ts.Ctx, http.MethodGet, fmt.Sprintf("http://%s/v1/status", addr), nil) + require.NoError(t, err) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer func() { assert.NoError(t, resp.Body.Close()) }() + + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + assert.Equal(t, int64(exitcodes.ScriptAborted), + gjson.GetBytes(body, "data.attributes.execution_result.exit_code").Int()) + }) + + cmd.ExecuteWithGlobalState(ts.GlobalState) +} + func TestAbortedByScriptSetupError(t *testing.T) { t.Parallel() script := ` @@ -1272,7 +1312,7 @@ func testAbortedByScriptError(t *testing.T, script string, runTest func(*testing stdout := ts.Stdout.String() t.Log(stdout) assert.Contains(t, stdout, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) - assert.Contains(t, stdout, `level=debug msg="Metrics and traces processing finished!"`) + assert.Contains(t, stdout, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdout, `level=debug msg="Everything has finished, exiting k6 with an error!"`) assert.Contains(t, stdout, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=7 tainted=false`) return ts @@ -1417,7 +1457,7 @@ func testAbortedByScriptTestAbort(t *testing.T, script string, runTest func(*tes assert.Contains(t, stdout, "test aborted: foo") assert.Contains(t, stdout, `level=debug msg="Sending test finished" output=cloud ref=111 run_status=5 tainted=false`) assert.Contains(t, stdout, `level=debug msg="Metrics emission of VUs and VUsMax metrics stopped"`) - assert.Contains(t, stdout, `level=debug msg="Metrics and traces processing finished!"`) + assert.Contains(t, stdout, `level=debug msg="Metrics processing finished!"`) assert.Contains(t, stdout, "bogus summary") } @@ -1602,14 +1642,23 @@ func TestMetricTagAndSetupDataIsolation(t *testing.T) { } func getSampleValues(t *testing.T, jsonOutput []byte, metric string, tags map[string]string) []float64 { + return getSampleValuesWithMetadata(t, jsonOutput, metric, tags, nil) +} + +func getSampleValuesWithMetadata( + t *testing.T, jsonOutput []byte, metric string, tags, metadata map[string]string, +) []float64 { jsonLines := bytes.Split(jsonOutput, []byte("\n")) result := []float64{} - tagsMatch := func(rawTags any) bool { - sampleTags, ok := rawTags.(map[string]any) + valuesMatch := func(rawValues any, expected map[string]string) bool { + if len(expected) == 0 { + return true + } + sampleValues, ok := rawValues.(map[string]any) require.True(t, ok) - for k, v := range tags { - rv, sok := sampleTags[k] + for k, v := range expected { + rv, sok := sampleValues[k] if !sok { return false } @@ -1641,7 +1690,7 @@ func getSampleValues(t *testing.T, jsonOutput []byte, metric string, tags map[st sampleData, ok := line["data"].(map[string]any) require.True(t, ok) - if !tagsMatch(sampleData["tags"]) { + if !valuesMatch(sampleData["tags"], tags) || !valuesMatch(sampleData["metadata"], metadata) { continue } @@ -1987,6 +2036,7 @@ export default function() {};` } func TestPrometheusRemoteWriteOutput(t *testing.T) { + requireFullBuild(t) t.Parallel() ts := NewGlobalTestState(t) @@ -2077,6 +2127,7 @@ func TestUIRenderOutput(t *testing.T) { } func TestUIRenderWebDashboard(t *testing.T) { + requireFullBuild(t) t.Parallel() tests := []struct { @@ -2176,7 +2227,7 @@ func TestBadLogOutput(t *testing.T) { } // HACK: We need this so multiple tests can register differently named modules. -var uniqueModuleNumber uint64 //nolint:gochecknoglobals +var uniqueModuleNumber atomic.Uint64 //nolint:gochecknoglobals // Tests that the appropriate events are emitted in the correct order. func TestEventSystemOK(t *testing.T) { @@ -2184,7 +2235,7 @@ func TestEventSystemOK(t *testing.T) { ts := NewGlobalTestState(t) - moduleName := fmt.Sprintf("k6/x/testevents-%d", atomic.AddUint64(&uniqueModuleNumber, 1)) + moduleName := fmt.Sprintf("k6/x/testevents-%d", uniqueModuleNumber.Add(1)) mod := events.New(event.GlobalEvents, event.VUEvents) modules.Register(moduleName, mod) @@ -2310,7 +2361,7 @@ func TestEventSystemError(t *testing.T) { t.Parallel() ts := NewGlobalTestState(t) - moduleName := fmt.Sprintf("k6/x/testevents-%d", atomic.AddUint64(&uniqueModuleNumber, 1)) + moduleName := fmt.Sprintf("k6/x/testevents-%d", uniqueModuleNumber.Add(1)) mod := events.New(event.GlobalEvents, event.VUEvents) modules.Register(moduleName, mod) @@ -2367,7 +2418,7 @@ func BenchmarkRunEvents(b *testing.B) { for i := 0; i < b.N; i++ { ts := NewGlobalTestState(b) - moduleName := fmt.Sprintf("k6/x/testevents-%d", atomic.AddUint64(&uniqueModuleNumber, 1)) + moduleName := fmt.Sprintf("k6/x/testevents-%d", uniqueModuleNumber.Add(1)) mod := events.New(event.GlobalEvents, event.VUEvents) modules.Register(moduleName, mod) @@ -2537,6 +2588,69 @@ func TestSetupTimeout(t *testing.T) { assert.Contains(t, stderr, "setup() execution timed out after 1 seconds") } +func TestHandleSummaryTimeout(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + env map[string]string + script string + }{ + { + name: "script option", + script: ` + import { sleep } from 'k6'; + + export const options = { + handleSummaryTimeout: '1s', + }; + + export function handleSummary(data) { + sleep(100000); + } + + export default function() {} + `, + }, + { + name: "environment variable", + env: map[string]string{ + "K6_HANDLE_SUMMARY_TIMEOUT": "1s", + }, + script: ` + import { sleep } from 'k6'; + + export function handleSummary(data) { + sleep(100000); + } + + export default function() {} + `, + }, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + ts := NewGlobalTestState(t) + ts.CmdArgs = []string{"k6", "run", "-"} + maps.Copy(ts.Env, tc.env) + ts.Stdin = bytes.NewBufferString(tc.script) + + start := time.Now() + cmd.ExecuteWithGlobalState(ts.GlobalState) + elapsed := time.Since(start) + assert.Greater(t, elapsed, 1*time.Second, "expected more time to have passed because of handleSummaryTimeout") + assert.Less( + t, elapsed, 5*time.Second, + "expected less time to have passed because of handleSummaryTimeout", + ) + + stderr := ts.Stderr.String() + t.Log(stderr) + assert.Contains(t, stderr, "handleSummary() execution timed out after 1 seconds") + }) + } +} + func TestTypeScriptSupport(t *testing.T) { t.Parallel() depScript := ` @@ -3469,3 +3583,78 @@ func TestPLZCloudSecretsEnvVars(t *testing.T) { assert.Contains(t, stderr, `level=info msg="***SECRET_REDACTED***" source=console`) assert.NotContains(t, stderr, "plz-secret-value") } + +func TestLogNanosecondTimestampsFlag(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + additionalArg string + assertFunc func(log string, t *testing.T) + }{ + { + name: "json format with cli switch", + additionalArg: "--log-format=json", + assertFunc: func(log string, t *testing.T) { + parsed := &struct { + Msg string + Time time.Time + }{} + + require.NoError(t, json.Unmarshal([]byte(log), parsed)) + assert.NotZero(t, parsed.Time.Nanosecond(), "Logged timestamp should have nanoseconds") + }, + }, + { + name: "default format with color disabled with cli switch", + additionalArg: "--no-color", + assertFunc: func(log string, t *testing.T) { + pairs := strings.Split(log, " ") + require.NotZero(t, len(pairs)) + + keyValue := strings.Split(pairs[0], "=") + require.Len(t, keyValue, 2) + require.Equal(t, "time", keyValue[0]) + timestamp, err := time.Parse(time.RFC3339Nano, strings.Trim(keyValue[1], "\"")) + require.NoError(t, err) + + assert.NotZero(t, timestamp.Nanosecond(), "Logged timestamp should have nanoseconds") + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + mainScript := ` + export default function () { + console.log("test 1 xdgf"); + }; + ` + + ts := NewGlobalTestState(t) + ts.Flags.LogNanosecondTimestamps = true + + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, "script.js"), []byte(mainScript), 0o644)) + + ts.CmdArgs = []string{"k6", "run", "script.js", tc.additionalArg} + + cmd.ExecuteWithGlobalState(ts.GlobalState) + logs := strings.Trim(ts.Stderr.String(), "\n") + lines := strings.Split(logs, "\n") + require.NotZero(t, len(lines)) + + found := false + for _, line := range lines { + if strings.Contains(line, "test 1 xdgf") { + tc.assertFunc(line, t) + found = true + break + } + } + + require.True(t, found, "expected log line containing marker not found") + }) + } +} diff --git a/internal/cmd/tests/cmd_scenarios_test.go b/internal/cmd/tests/cmd_scenarios_test.go new file mode 100644 index 000000000..538168ad4 --- /dev/null +++ b/internal/cmd/tests/cmd_scenarios_test.go @@ -0,0 +1,152 @@ +package tests + +import ( + "bytes" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/errext/exitcodes" + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/executor" + "go.k6.io/k6/v2/lib/fsext" +) + +const scenariosScript = ` + import exec from 'k6/execution'; + export const options = { + cloud: { name: 'pick', projectID: 123456 }, + vus: 1, duration: '1ms', iterations: 1, stages: [], + thresholds: { + iterations: ['count>0'], + 'iterations{scenario:api}': ['count>0'], + 'iterations{scenario:db}': ['count>0'] + }, + scenarios: { + ui: { executor: 'per-vu-iterations', vus: 2, iterations: 1 }, + api: { executor: 'shared-iterations', vus: 1, iterations: 2 }, + db: { executor: 'shared-iterations', vus: 1, iterations: 1 } + } + }; + export default function() { console.log('ran ' + exec.scenario.name); } +` + +func TestScenariosFilterArchive(t *testing.T) { + t.Parallel() + + for name, build := range map[string]func(*testing.T) []byte{ + "local archive": func(t *testing.T) []byte { + return buildArchive(t, scenariosScript, "--scenario", "ui,api") + }, + "cloud run": func(t *testing.T) []byte { + requireFullBuild(t) + _, data := uploadAndCaptureArchive(t, + []string{"k6", "cloud", "run", "--scenario", "ui,api", "test.js"}, nil, scenariosScript) + return data + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + + tarData := build(t) + arc, err := lib.ReadArchive(bytes.NewReader(tarData)) + require.NoError(t, err) + + require.Len(t, arc.Options.Scenarios, 2) + assert.NotContains(t, arc.Options.Scenarios, "db") + + ui, ok := arc.Options.Scenarios["ui"].(executor.PerVUIterationsConfig) + require.True(t, ok) + assert.Equal(t, null.IntFrom(2), ui.VUs) + assert.Equal(t, null.IntFrom(1), ui.Iterations) + assert.NotContains(t, arc.Options.Thresholds, "iterations{scenario:db}") + assert.Contains(t, arc.Options.Thresholds, "iterations") + assert.Contains(t, arc.Options.Thresholds, "iterations{scenario:api}") + assert.False(t, arc.Options.VUs.Valid) + assert.False(t, arc.Options.Duration.Valid) + assert.False(t, arc.Options.Iterations.Valid) + assert.Nil(t, arc.Options.Stages) + + ts := NewGlobalTestState(t) + tarPath := filepath.Join(ts.Cwd, "archive.tar") + require.NoError(t, fsext.WriteFile(ts.FS, tarPath, tarData, 0o644)) + ts.CmdArgs = []string{"k6", "run", "--log-output=stdout", tarPath} + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Equal(t, 2, strings.Count(stdout, "ran ui")) + assert.Equal(t, 2, strings.Count(stdout, "ran api")) + assert.NotContains(t, stdout, "ran db") + assert.NotContains(t, stdout, "ran default") + assert.NotContains(t, stdout, "--scenario skipped thresholds") + }) + } +} + +func TestRunScenarios(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, scenariosScript, + []string{"--log-output=stdout", "--log-format=raw", "--scenario", "api"}, 0) + ts.Env["K6_ITERATIONS"] = "1" + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Equal(t, 2, strings.Count(stdout, "ran api")) + assert.NotContains(t, stdout, "ran ui") + assert.NotContains(t, stdout, "ran db") + assert.NotContains(t, stdout, "ran default") + assert.Contains(t, stdout, `--scenario overrode vus, duration, iterations, stages in "script" configuration`) + assert.Contains(t, stdout, `--scenario overrode iterations in "environment" configuration`) + assert.Contains(t, stdout, "--scenario skipped thresholds for excluded scenarios: iterations{scenario:db}; "+ + "these thresholds remain skipped even if another scenario emits matching tags") + assert.Equal(t, 1, strings.Count(stdout, "--scenario skipped thresholds")) +} + +func TestScenarioThresholdValidation(t *testing.T) { + t.Parallel() + + script := strings.Replace(scenariosScript, + "'iterations{scenario:db}': ['count>0']", "'iterations{scenario:db}': ['invalid']", 1) + for noThresholds, exitCode := range map[string]exitcodes.ExitCode{ + "false": exitcodes.InvalidConfig, + "true": 0, + } { + t.Run(noThresholds, func(t *testing.T) { + t.Parallel() + ts := getSingleFileTestState(t, script, + []string{"--scenario", "api", "--no-thresholds=" + noThresholds}, exitCode) + cmd.ExecuteWithGlobalState(ts.GlobalState) + }) + } +} + +func TestRunScenariosOnce(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, scenariosScript, + []string{"--log-output=stdout", "--scenario", "ui,api", "--once"}, 0) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Equal(t, 1, strings.Count(stdout, "ran ui")) + assert.Equal(t, 1, strings.Count(stdout, "ran api")) + assert.NotContains(t, stdout, "ran db") +} + +func TestRunRejectsEmptyScenarios(t *testing.T) { + t.Parallel() + + ts := getSingleFileTestState(t, scenariosScript, + []string{"--log-output=stdout", "--scenario="}, exitcodes.InvalidConfig) + cmd.ExecuteWithGlobalState(ts.GlobalState) + + stdout := ts.Stdout.String() + assert.Contains(t, stdout, "requires at least one scenario name") + assert.NotContains(t, stdout, "ran ") +} diff --git a/internal/cmd/tests/cmd_stats_test.go b/internal/cmd/tests/cmd_stats_test.go index e881c1da0..317c8c61b 100644 --- a/internal/cmd/tests/cmd_stats_test.go +++ b/internal/cmd/tests/cmd_stats_test.go @@ -13,6 +13,7 @@ import ( ) func TestStatsCommandWithoutAddressFails(t *testing.T) { + requireFullBuild(t) t.Parallel() ts := NewGlobalTestState(t) @@ -27,6 +28,7 @@ func TestStatsCommandWithoutAddressFails(t *testing.T) { } func TestStatsCommand(t *testing.T) { + requireFullBuild(t) t.Parallel() srv := getTestServer(t, map[string]http.Handler{ diff --git a/internal/cmd/tests/fork_default_group_test.go b/internal/cmd/tests/fork_default_group_test.go new file mode 100644 index 000000000..873e0c6c4 --- /dev/null +++ b/internal/cmd/tests/fork_default_group_test.go @@ -0,0 +1,65 @@ +package tests + +import ( + "maps" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/cmd" + "go.k6.io/k6/v2/lib/fsext" +) + +func TestForkDefaultGroupAndOptOut(t *testing.T) { + t.Parallel() + for _, test := range []struct { + name string + args []string + env map[string]string + enabled bool + }{ + {name: "default", enabled: true}, + {name: "CLI opt-out", args: []string{"--features="}}, + {name: "environment opt-out", env: map[string]string{"K6_FEATURES": ""}}, + {name: "explicit list replaces default", args: []string{"--features=merge-run-tags"}}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + args := append([]string{"--out=json=results.json", "--summary-mode=disabled"}, test.args...) + ts := getSingleFileTestState(t, ` + import { check, group } from 'k6'; + export default async function () { + let supported = false; + try { + await group('step', async () => { + await Promise.resolve(); + check(true, { inside: value => value }); + }); + supported = true; + } catch (error) { + if (!String(error).includes('group() does not support async')) throw error; + } + check(supported, { supported: value => value }); + } + `, args, 0) + maps.Copy(ts.Env, test.env) + cmd.ExecuteWithGlobalState(ts.GlobalState) + results, err := fsext.ReadFile(ts.FS, "results.json") + require.NoError(t, err) + want := float64(0) + if test.enabled { + want = 1 + assert.Equal(t, []float64{1}, getSampleValues(t, results, "checks", map[string]string{ + "check": "inside", "group": "::step", + })) + assert.Len(t, getSampleValues(t, results, "group_duration", map[string]string{"group": "::step"}), 1) + } else { + assert.Empty(t, getSampleValues(t, results, "group_duration", nil)) + } + assert.Equal(t, []float64{want}, getSampleValues(t, results, "checks", map[string]string{ + "check": "supported", "group": "", + })) + }) + } +} diff --git a/internal/cmd/tests/grpc.go b/internal/cmd/tests/grpc.go index e6bdb919f..025fa172c 100644 --- a/internal/cmd/tests/grpc.go +++ b/internal/cmd/tests/grpc.go @@ -30,8 +30,12 @@ func NewGRPC(t testing.TB) *GRPC { } features := grpcservice.LoadFeatures("") - grpcservice.RegisterRouteGuideServer(grpcServer, grpcservice.NewRouteGuideServer(features...)) - grpcservice.RegisterFeatureExplorerServer(grpcServer, grpcservice.NewFeatureExplorerServer(features...)) + routeGuide := grpcservice.NewRouteGuideServer(features...) + routeGuide.Logf = t.Logf + grpcservice.RegisterRouteGuideServer(grpcServer, routeGuide) + featureExplorer := grpcservice.NewFeatureExplorerServer(features...) + featureExplorer.Logf = t.Logf + grpcservice.RegisterFeatureExplorerServer(grpcServer, featureExplorer) reflection.Register(grpcServer) go func() { diff --git a/internal/cmd/tests/test_state.go b/internal/cmd/tests/test_state.go index d4e1dfd6b..864061066 100644 --- a/internal/cmd/tests/test_state.go +++ b/internal/cmd/tests/test_state.go @@ -90,11 +90,12 @@ func NewGlobalTestState(tb testing.TB) *GlobalTestState { defaultFlags := state.GetDefaultFlags(".config", ".cache") ts.GlobalState = &state.GlobalState{ - Ctx: ctx, - FS: fs, - Getwd: func() (string, error) { return ts.Cwd, nil }, - BinaryName: "k6", - CmdArgs: []string{}, + Ctx: ctx, + FS: fs, + Getwd: func() (string, error) { return ts.Cwd, nil }, + UserOSConfigDir: ".config", + BinaryName: "k6", + CmdArgs: []string{}, Env: map[string]string{ "K6_NO_USAGE_REPORT": "true", // Keep `k6 x` command-tree construction off the real registry.k6.io diff --git a/internal/cmd/tests/tests_test.go b/internal/cmd/tests/tests_test.go index 4f3483494..dc6957c05 100644 --- a/internal/cmd/tests/tests_test.go +++ b/internal/cmd/tests/tests_test.go @@ -35,6 +35,7 @@ func TestRootCommand(t *testing.T) { } func TestCloudLoginNotPanicking(t *testing.T) { + requireFullBuild(t) t.Parallel() ts := NewGlobalTestState(t) diff --git a/internal/cmd/x_registry.go b/internal/cmd/x_registry.go index 6c47ca4f3..21c308a0b 100644 --- a/internal/cmd/x_registry.go +++ b/internal/cmd/x_registry.go @@ -1,6 +1,7 @@ package cmd import ( + "cmp" "context" "encoding/json" "errors" @@ -69,6 +70,12 @@ func catalogMajorVersion() string { return v } +// catalogURL returns the extension registry catalog URL, honouring the +// K6_PROVISION_CATALOG_URL override. +func catalogURL(gs *state.GlobalState) string { + return cmp.Or(gs.Env[state.ProvisionCatalogURL], defaultCatalogURL()) +} + // catalogCachePath returns the on-disk location of the k6 extension registry // catalog cache, alongside the build cache so both share the user cache dir. func catalogCachePath(gs *state.GlobalState) string { @@ -79,23 +86,9 @@ func catalogCachePath(gs *state.GlobalState) string { // cache. A missing or stale cache returns (nil, nil); real I/O or parse // failures return an error so the caller can surface them at debug. func readCachedCatalog(gs *state.GlobalState, cachePath string) (registrySubcommands, error) { - maxAge := 24 * time.Hour - if d, err := time.ParseDuration(gs.Env[state.ProvisionCatalogTTL]); err == nil { - maxAge = d - } - info, err := gs.FS.Stat(cachePath) - if errors.Is(err, fs.ErrNotExist) { - return nil, nil - } - if err != nil { - return nil, fmt.Errorf("checking extensions cache: %w", err) - } - if time.Since(info.ModTime()) > maxAge { - return nil, nil - } - raw, err := fsext.ReadFile(gs.FS, cachePath) - if err != nil { - return nil, fmt.Errorf("reading extensions cache: %w", err) + raw, err := readCachedCatalogBytes(gs, cachePath) + if raw == nil || err != nil { + return nil, err } var subs registrySubcommands if err := json.Unmarshal(raw, &subs); err != nil { @@ -117,7 +110,7 @@ func fetchCatalog(ctx context.Context, url string) (registrySubcommands, []byte, } // The URL can be overridden via env for tests and development; that is the // intended behaviour of the catalog override, not an SSRF vector. - resp, err := http.DefaultClient.Do(req) //nolint:gosec + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, nil, fmt.Errorf("fetching extensions catalog: %w", err) } @@ -136,6 +129,32 @@ func fetchCatalog(ctx context.Context, url string) (registrySubcommands, []byte, return subs, raw, nil } +// readCachedCatalogBytes returns the raw catalog cache bytes when the cache +// exists and is fresh, honouring the K6_PROVISION_CATALOG_TTL override. A miss +// (absent or stale cache) returns (nil, nil); real I/O failures return an +// error so the caller can surface them at debug. +func readCachedCatalogBytes(gs *state.GlobalState, cachePath string) ([]byte, error) { + maxAge := 24 * time.Hour + if d, err := time.ParseDuration(gs.Env[state.ProvisionCatalogTTL]); err == nil { + maxAge = d + } + info, err := gs.FS.Stat(cachePath) + if errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("checking extensions cache: %w", err) + } + if time.Since(info.ModTime()) > maxAge { + return nil, nil + } + raw, err := fsext.ReadFile(gs.FS, cachePath) + if err != nil { + return nil, fmt.Errorf("reading extensions cache: %w", err) + } + return raw, nil +} + // writeCachedCatalog persists raw catalog bytes under cachePath, creating any // missing parent directories. func writeCachedCatalog(gs *state.GlobalState, cachePath string, raw []byte) error { diff --git a/internal/dashboard/sse.go b/internal/dashboard/sse.go index e8287ad2c..8ac41b0fc 100644 --- a/internal/dashboard/sse.go +++ b/internal/dashboard/sse.go @@ -60,9 +60,7 @@ func (emitter *eventEmitter) onStart() error { } func (emitter *eventEmitter) onStop(reason error) error { - var err errext.HasAbortReason - - if !errors.As(reason, &err) { + if _, ok := errors.AsType[errext.HasAbortReason](reason); !ok { emitter.wait.Wait() } @@ -80,7 +78,7 @@ func (emitter *eventEmitter) onEvent(name string, data any) { var retry []byte if name == stopEvent { - retry = []byte(strconv.Itoa(maxSafeInteger)) + retry = []byte(strconv.FormatInt(int64(maxSafeInteger), 10)) } id := strconv.FormatInt(emitter.id.Add(1), 10) diff --git a/internal/dashboard/web.go b/internal/dashboard/web.go index ee41fb1cf..41983683d 100644 --- a/internal/dashboard/web.go +++ b/internal/dashboard/web.go @@ -99,7 +99,7 @@ func rootHandler(uiPath string) http.HandlerFunc { http.Redirect( w, r, - path.Join(uiPath, r.URL.Path)+"?endpoint=/", + path.Join(uiPath, "/")+"?endpoint=/", http.StatusTemporaryRedirect, ) diff --git a/internal/execution/scheduler_ext_test.go b/internal/execution/scheduler_ext_test.go index 0a9814222..f11f6011c 100644 --- a/internal/execution/scheduler_ext_test.go +++ b/internal/execution/scheduler_ext_test.go @@ -579,8 +579,8 @@ func TestSchedulerRunCustomConfigNoCrossover(t *testing.T) { checkVar('TESTVAR3', undefined); checkVar('TESTGLOBALVAR', 'global'); - // Intentionally try to pollute the env - __ENV.TESTVAR2 = 'overridden'; + // Intentionally try to pollute the env (will be silently rejected since __ENV is frozen) + try { __ENV.TESTVAR2 = 'overridden'; } catch (e) { /* __ENV is frozen */ } http.get('HTTPBIN_IP_URL/', { tags: { reqtag: 'scenario1' }}); } @@ -943,6 +943,60 @@ func TestSchedulerEndErrors(t *testing.T) { assert.Empty(t, hook.Entries) } +// TestSchedulerRampingVUsStartErrorLoggedOnce checks that, when a VU-start +// failure occurs during a real scheduler run, the failure is logged exactly +// once. The ramping VUs executor used to log the failure itself and then +// return it to the scheduler, which logs every non-nil executor result, +// producing two error-level entries for a single failure. +func TestSchedulerRampingVUsStartErrorLoggedOnce(t *testing.T) { + t.Parallel() + + config := executor.NewRampingVUsConfig("myscenario") + config.GracefulStop = types.NullDurationFrom(0) + config.StartVUs = null.IntFrom(2) + config.Stages = []executor.Stage{ + {Duration: types.NullDurationFrom(time.Second), Target: null.IntFrom(2)}, + } + + runner := &minirunner.MiniRunner{ + Fn: func(_ context.Context, _ *lib.State, _ chan<- metrics.SampleContainer) error { + return nil + }, + Options: lib.Options{ + Scenarios: lib.ScenarioConfigs{config.GetName(): config}, + }, + } + + logger, hook := logtest.NewNullLogger() + ctx, cancel, execScheduler, samples := newTestScheduler(t, runner, logger, lib.Options{}) + defer cancel() + + // Drain every planned VU so the executor cannot fetch one when it tries + // to start VUs, reproducing a "system overload" VU-start failure. + state := execScheduler.GetState() + maxVUs := lib.GetMaxPlannedVUs(config.GetExecutionRequirements(state.ExecutionTuple)) + entry := logrus.NewEntry(logger) + for range maxVUs { + _, err := state.GetPlannedVU(entry, false) + require.NoError(t, err) + } + + // Run through the real scheduler, not executor.Run() directly, since + // that's the path that determines how many times the failure is logged. + runErr := execScheduler.Run(ctx, ctx, samples) + require.Error(t, runErr) + require.Contains(t, runErr.Error(), "could not get a VU from the buffer in") + + var errorEntries []*logrus.Entry + for _, e := range hook.AllEntries() { + if e.Level == logrus.ErrorLevel { + errorEntries = append(errorEntries, e) + } + } + require.Len(t, errorEntries, 1, + "the VU-start failure must be logged exactly once, by the scheduler that receives it from Run()") +} + func TestSchedulerEndIterations(t *testing.T) { t.Parallel() registry := metrics.NewRegistry() @@ -1110,13 +1164,13 @@ func TestDNSResolverCache(t *testing.T) { require.NoError(t, err) mr := mockresolver.New(map[string][]net.IP{"myhost": {net.ParseIP(sr("HTTPBIN_IP"))}}) - var newResolutions uint32 + var newResolutions atomic.Uint32 resolveHook := func(_ string, result []net.IP) { require.Len(t, result, 1) if result[0].String() == "127.0.0.1" { mr.Set("myhost", "127.0.0.254") } else { - atomic.AddUint32(&newResolutions, 1) + newResolutions.Add(1) } } mr.ResolveHook = resolveHook @@ -1131,7 +1185,7 @@ func TestDNSResolverCache(t *testing.T) { select { case err := <-errCh: require.NoError(t, err) - assert.Equal(t, tc.expNewResolutions, atomic.LoadUint32(&newResolutions)) + assert.Equal(t, tc.expNewResolutions, newResolutions.Load()) case <-time.After(10 * time.Second): t.Fatal("timed out") } diff --git a/internal/features/features.go b/internal/features/features.go index c3a68cbcc..53dfd3ef5 100644 --- a/internal/features/features.go +++ b/internal/features/features.go @@ -41,9 +41,12 @@ func (l Lifecycle) String() string { // Flags declares every feature flag as a tagged bool field. type Flags struct { - NativeHistograms bool `lifecycle:"experimental" help:"Use native histograms for trend metrics"` - _ noCopy - activated []string + NativeHistograms bool `lifecycle:"experimental" help:"Use native histograms for trend metrics"` + MergeRunTags bool `lifecycle:"experimental" help:"Merge run tags across config layers instead of replacing"` + FreezeEnv bool `lifecycle:"experimental" help:"Freeze __ENV object to prevent modifications from JS code"` + AsyncMetricContext bool `lifecycle:"experimental" help:"Propagate metric context through asynchronous operations"` + _ noCopy + activated []string } // Activated returns a copy of the sorted canonical names of active features. @@ -80,7 +83,9 @@ func Init( logger logrus.FieldLogger, cli, json Source, runtimeEnv map[string]string, ) (*Flags, error) { flags := &Flags{} - active, err := initInto(reflect.ValueOf(flags).Elem(), logger, cli, json, runtimeEnv, defaultAliases()) + // Fork default, below explicit CLI/env/JSON lists (including empty opt-out lists). + active, err := initInto(reflect.ValueOf(flags).Elem(), logger, cli, json, runtimeEnv, + defaultAliases(), "async-metric-context") if err != nil { return nil, err } @@ -100,6 +105,7 @@ func allOf(t reflect.Type) ([]Flag, error) { func initInto( target reflect.Value, logger logrus.FieldLogger, cli, json Source, runtimeEnv map[string]string, aliases []alias, + defaults ...string, ) ([]string, error) { defs, err := parseDefinitions(target.Type()) if err != nil { @@ -112,7 +118,7 @@ func initInto( } } - return resolveInto(defs, target, cli, resolveEnvSurface(defs, runtimeEnv, logger), json, logger) + return resolveInto(defs, target, cli, resolveEnvSurface(defs, runtimeEnv, logger), json, logger, defaults...) } // definition holds the parsed metadata and field index for one flag field. @@ -166,8 +172,7 @@ func parseDefinitions(t reflect.Type) (*definitions, error) { byName: make(map[string]int), } - for i := range t.NumField() { - f := t.Field(i) + for f := range t.Fields() { if !f.IsExported() { continue } diff --git a/internal/features/fork_defaults_test.go b/internal/features/fork_defaults_test.go new file mode 100644 index 000000000..8284fbfde --- /dev/null +++ b/internal/features/fork_defaults_test.go @@ -0,0 +1,64 @@ +package features + +import ( + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestForkDefaultAsyncMetricContext(t *testing.T) { + t.Parallel() + for _, test := range []struct { + name string + cli Source + json Source + env map[string]string + active []string + }{ + {name: "default", active: []string{"async-metric-context"}}, + {name: "empty CLI opts out", cli: supplied(""), active: []string{}}, + {name: "empty env opts out", env: map[string]string{"K6_FEATURES": ""}, active: []string{}}, + {name: "empty JSON opts out", json: supplied(""), active: []string{}}, + {name: "explicit list replaces default", cli: supplied("merge-run-tags"), active: []string{"merge-run-tags"}}, + { + name: "explicit list includes default", cli: supplied("merge-run-tags,async-metric-context"), + active: []string{"async-metric-context", "merge-run-tags"}, + }, + { + name: "env overrides JSON", json: supplied("async-metric-context"), + env: map[string]string{"K6_FEATURES": ""}, active: []string{}, + }, + { + name: "CLI overrides env", cli: supplied(""), + env: map[string]string{"K6_FEATURES": "async-metric-context"}, active: []string{}, + }, + { + name: "CLI re-enables", cli: supplied("async-metric-context"), + env: map[string]string{"K6_FEATURES": ""}, active: []string{"async-metric-context"}, + }, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + flags, err := Init(logrus.New(), test.cli, test.json, test.env) + require.NoError(t, err) + assert.Equal(t, test.active, flags.Activated()) + wantAsync := false + for _, name := range test.active { + wantAsync = wantAsync || name == "async-metric-context" + } + assert.Equal(t, wantAsync, flags.AsyncMetricContext) + assert.Equal(t, wantAsync, flags.Tags()["k6_feature_async_metric_context"] == "true") + }) + } + definitions, err := All() + require.NoError(t, err) + for _, definition := range definitions { + if definition.Name == "async-metric-context" { + assert.Equal(t, Experimental, definition.Lifecycle) + return + } + } + t.Fatal("async-metric-context definition missing") +} diff --git a/internal/features/resolve.go b/internal/features/resolve.go index 94b9be092..d8e93e1cb 100644 --- a/internal/features/resolve.go +++ b/internal/features/resolve.go @@ -31,6 +31,7 @@ func resolveInto(defs *definitions, target reflect.Value, cli, env, json Source, logger logrus.FieldLogger, + defaults ...string, ) ([]string, error) { if target.Type() != defs.typ { return nil, fmt.Errorf("features: resolveInto target type %s does not match definition type %s", @@ -48,8 +49,7 @@ func resolveInto(defs *definitions, case json.Supplied: winner, source = json, "json" default: - forceGA(defs, target) - return []string{}, nil + winner, source = Source{Values: defaults}, "default" } activated := make(map[string]struct{}) @@ -90,7 +90,10 @@ func resolveInto(defs *definitions, activated[name] = struct{}{} - logActivation(logger, name, defs.definitions[idx].flag.Lifecycle) + // Defaults must not add unsolicited INFO output, particularly for quiet runs. + if source != "default" { + logActivation(logger, name, defs.definitions[idx].flag.Lifecycle) + } } setFields(defs, target, activated) diff --git a/internal/js/bundle.go b/internal/js/bundle.go index eab3ff4fa..3a3eeb342 100644 --- a/internal/js/bundle.go +++ b/internal/js/bundle.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/grafana/sobek" + sobekencoding "github.com/grafana/sobek-webapi-encoding" "github.com/sirupsen/logrus" "go.k6.io/k6/v2/errext" @@ -245,7 +246,7 @@ func beautifyOptionsJSONUnmarshalError(data []byte, err error) error { if errors.As(err, &unmarshalTypError) { e := unmarshalTypError previousNewLineIndex := max(bytes.LastIndexByte(data[:e.Offset], '\n'), 0) - nextNewLineIndex := max(min(bytes.IndexByte(data[e.Offset:], '\n'), len(data)-1), (int)(e.Offset)) + nextNewLineIndex := max(min(bytes.IndexByte(data[e.Offset:], '\n'), len(data)-1), int(e.Offset)) info := strings.TrimSpace(string(data[previousNewLineIndex:nextNewLineIndex])) err = fmt.Errorf("parsing options from script got error while parsing %q: %w", info, e) @@ -265,6 +266,9 @@ func (b *Bundle) Instantiate(ctx context.Context, vuID uint64) (*BundleInstance, local: event.NewEventSystem(100, b.preInitState.Logger), }, } + if b.preInitState.FeatureFlags != nil && b.preInitState.FeatureFlags.AsyncMetricContext { + vuImpl.runtime.SetAsyncContextTracker(common.NewMetricContextTracker(vuImpl.State)) + } vuImpl.eventLoop = eventloop.New(vuImpl) bi, err := b.instantiate(vuImpl, vuID) if err != nil { @@ -373,8 +377,7 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*BundleInstance } if err != nil { - var exception *sobek.Exception - if errors.As(err, &exception) { + if exception, ok := errors.AsType[*sobek.Exception](err); ok { err = &scriptExceptionError{inner: exception} } return nil, err @@ -392,26 +395,30 @@ func (b *Bundle) instantiate(vuImpl *moduleVUImpl, vuID uint64) (*BundleInstance } // registerGlobals registers the globals for the runtime. -// e.g. timers and webcrypto. +// e.g. timers, webcrypto, and encoding. func registerGlobals(vuImpl *moduleVUImpl) error { err := timers.SetupGlobally(vuImpl) if err != nil { return err } - return webcrypto.SetupGlobally(vuImpl) + err = webcrypto.SetupGlobally(vuImpl) + if err != nil { + return err + } + + return sobekencoding.RegisterGlobally(vuImpl.runtime) } func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID uint64, logger logrus.FieldLogger) error { rt.SetFieldNameMapper(common.FieldNameMapper{}) rt.SetRandSource(common.NewRandSource()) - env := make(map[string]string, len(b.preInitState.RuntimeOptions.Env)) - maps.Copy(env, b.preInitState.RuntimeOptions.Env) - err := rt.Set("__ENV", env) - if err != nil { + if err := setupEnvObject(rt, b.preInitState.RuntimeOptions.Env, b.preInitState.FeatureFlags); err != nil { return err } + + var err error err = rt.Set("__VU", vuID) if err != nil { return err diff --git a/internal/js/bundle_test.go b/internal/js/bundle_test.go index 268e483cb..ea8d8761b 100644 --- a/internal/js/bundle_test.go +++ b/internal/js/bundle_test.go @@ -20,6 +20,7 @@ import ( "gopkg.in/guregu/null.v3" "go.k6.io/k6/v2/internal/build" + "go.k6.io/k6/v2/internal/features" "go.k6.io/k6/v2/internal/lib/consts" "go.k6.io/k6/v2/internal/lib/testutils" "go.k6.io/k6/v2/internal/loader" @@ -55,6 +56,7 @@ func getSimpleBundle(tb testing.TB, filename, data string, opts ...any) (*Bundle fs := fsext.NewMemMapFs() var rtOpts *lib.RuntimeOptions var logger logrus.FieldLogger + var ff *features.Flags for _, o := range opts { switch opt := o.(type) { case fsext.Fs: @@ -63,11 +65,14 @@ func getSimpleBundle(tb testing.TB, filename, data string, opts ...any) (*Bundle rtOpts = &opt case logrus.FieldLogger: logger = opt + case *features.Flags: + ff = opt default: tb.Fatalf("unknown test option %q", opt) } } preInitState := getTestPreInitState(tb, logger, rtOpts) + preInitState.FeatureFlags = ff filenameURL := &url.URL{Path: filename, Scheme: "file"} @@ -88,17 +93,21 @@ func getSimpleBundleFromArchive(tb testing.TB, arc *lib.Archive, opts ...any) (* tb.Helper() var rtOpts *lib.RuntimeOptions var logger logrus.FieldLogger + var ff *features.Flags for _, o := range opts { switch opt := o.(type) { case lib.RuntimeOptions: rtOpts = &opt case logrus.FieldLogger: logger = opt + case *features.Flags: + ff = opt default: tb.Fatalf("unknown test option %q", opt) } } preInitState := getTestPreInitState(tb, logger, rtOpts) + preInitState.FeatureFlags = ff fss := arc.Filesystems moduleResolver := NewModuleResolver(arc.PwdURL, preInitState, fss) @@ -904,36 +913,32 @@ func TestBundleNotSharable(t *testing.T) { t.Parallel() data := ` export default function() { - if (__ITER == 0) { - if (typeof __ENV.something !== "undefined") { - throw new Error("invalid something: " + __ENV.something + " should be undefined"); - } - __ENV.something = __VU; - } else if (__ENV.something != __VU) { - throw new Error("invalid something: " + __ENV.something+ " should be "+ __VU); + if (typeof __ENV.something !== "undefined") { + throw new Error("invalid something: " + __ENV.something + " should be undefined"); + } + try { __ENV.something = __VU; throw new Error("should have frozen __ENV"); } catch (e) { + if (!(e instanceof TypeError)) { throw e; } } } ` - b1, err := getSimpleBundle(t, "/script.js", data) + ff := &features.Flags{FreezeEnv: true} + b1, err := getSimpleBundle(t, "/script.js", data, ff) require.NoError(t, err) logger := testutils.NewLogger(t) - b2, err := getSimpleBundleFromArchive(t, b1.makeArchive(), logger) + b2, err := getSimpleBundleFromArchive(t, b1.makeArchive(), logger, ff) require.NoError(t, err) bundles := map[string]*Bundle{"Source": b1, "Archive": b2} - var vus, iters uint64 = 10, 1000 + var vus uint64 = 10 for name, b := range bundles { t.Run(name, func(t *testing.T) { t.Parallel() for i := range vus { bi, err := b.Instantiate(context.Background(), i) require.NoError(t, err) - for j := range iters { - require.NoError(t, bi.Runtime.Set("__ITER", j)) - _, err := bi.getCallableExport(consts.DefaultFn)(sobek.Undefined()) - require.NoError(t, err) - } + _, err = bi.getCallableExport(consts.DefaultFn)(sobek.Undefined()) + require.NoError(t, err) } }) } @@ -1033,6 +1038,55 @@ func TestGlobalTimers(t *testing.T) { } } +func TestGlobalTextEncoding(t *testing.T) { + t.Parallel() + data := ` + const text = "Hello, 世界 👋"; + const encoded = new TextEncoder().encode(text); + if (!(encoded instanceof Uint8Array)) { + throw new Error("TextEncoder.encode() did not return a Uint8Array"); + } + + const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); + if (!decoder.fatal || !decoder.ignoreBOM) { + throw new Error("TextDecoder options were not applied"); + } + if (decoder.decode(encoded) !== text) { + throw new Error("text encoding round trip failed"); + } + + let threwTypeError = false; + try { + decoder.decode(new Uint8Array([0xff])); + } catch (error) { + threwTypeError = error instanceof TypeError; + } + if (!threwTypeError) { + throw new Error("fatal TextDecoder did not throw a TypeError"); + } + + export default function() {} + ` + + b1, err := getSimpleBundle(t, "/script.js", data) + require.NoError(t, err) + logger := testutils.NewLogger(t) + + b2, err := getSimpleBundleFromArchive(t, b1.makeArchive(), logger) + require.NoError(t, err) + + bundles := map[string]*Bundle{"Source": b1, "Archive": b2} + for name, b := range bundles { + t.Run(name, func(t *testing.T) { + t.Parallel() + for _, vuID := range []uint64{1, 2} { + _, err := b.Instantiate(context.Background(), vuID) + require.NoError(t, err) + } + }) + } +} + func TestTopLevelAwaitErrors(t *testing.T) { t.Parallel() data := ` diff --git a/internal/js/env.go b/internal/js/env.go new file mode 100644 index 000000000..cd9860ced --- /dev/null +++ b/internal/js/env.go @@ -0,0 +1,37 @@ +package js + +import ( + "fmt" + "maps" + + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/internal/features" + "go.k6.io/k6/v2/js/common" +) + +// setupEnvObject configures the global __ENV object on the given runtime. +// When freeze is true, each property is defined as non-writable, non-configurable +// and the object is frozen so assignments like __ENV.foo = "bar" are rejected. +// When freeze is false, the old mutable map behavior is used (default). +func setupEnvObject(rt *sobek.Runtime, env map[string]string, flags *features.Flags) error { + if flags != nil && flags.FreezeEnv { + envObj := rt.NewObject() + for k, v := range env { + if err := envObj.DefineDataProperty( + k, rt.ToValue(v), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE, + ); err != nil { + return fmt.Errorf("setting __ENV property %s: %w", k, err) + } + } + if err := common.FreezeObject(rt, envObj); err != nil { + return fmt.Errorf("freezing __ENV: %w", err) + } + return rt.Set("__ENV", envObj) + } + + // Old behavior: mutable map + envMap := make(map[string]string, len(env)) + maps.Copy(envMap, env) + return rt.Set("__ENV", envMap) +} diff --git a/internal/js/init_and_modules_test.go b/internal/js/init_and_modules_test.go index 9bf9dbd2f..852f861f3 100644 --- a/internal/js/init_and_modules_test.go +++ b/internal/js/init_and_modules_test.go @@ -36,13 +36,13 @@ func (cm *CheckModule) VuCtx(_ context.Context) { cm.vuCtxCalled++ } -var uniqueModuleNumber int64 //nolint:gochecknoglobals // we need this so multiple test can register differently named modules +var uniqueModuleNumber atomic.Int64 //nolint:gochecknoglobals // we need this so multiple test can register differently named modules func TestNewJSRunnerWithCustomModule(t *testing.T) { t.Parallel() checkModule := &CheckModule{t: t} - moduleName := fmt.Sprintf("k6/x/check-%d", atomic.AddInt64(&uniqueModuleNumber, 1)) + moduleName := fmt.Sprintf("k6/x/check-%d", uniqueModuleNumber.Add(1)) modules.Register(moduleName, checkModule) script := fmt.Sprintf(` diff --git a/internal/js/initcontext_test.go b/internal/js/initcontext_test.go index 77eaa2e84..6e9ee3ee6 100644 --- a/internal/js/initcontext_test.go +++ b/internal/js/initcontext_test.go @@ -346,13 +346,13 @@ func TestRequestWithBinaryFile(t *testing.T) { Options: lib.Options{}, Logger: logger, Transport: &http.Transport{ - DialContext: (netext.NewDialer( + DialContext: netext.NewDialer( net.Dialer{ Timeout: 10 * time.Second, KeepAlive: 60 * time.Second, }, netext.NewResolver(net.LookupIP, 0, types.DNSfirst, types.DNSpreferIPv4), - )).DialContext, + ).DialContext, }, BufferPool: lib.NewBufferPool(), Samples: make(chan metrics.SampleContainer, 500), @@ -487,13 +487,13 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) { Options: lib.Options{}, Logger: logger, Transport: &http.Transport{ - DialContext: (netext.NewDialer( + DialContext: netext.NewDialer( net.Dialer{ Timeout: 10 * time.Second, KeepAlive: 60 * time.Second, }, netext.NewResolver(net.LookupIP, 0, types.DNSfirst, types.DNSpreferIPv4), - )).DialContext, + ).DialContext, }, BufferPool: lib.NewBufferPool(), Samples: make(chan metrics.SampleContainer, 500), diff --git a/internal/js/modules/k6/asyncgroup.go b/internal/js/modules/k6/asyncgroup.go new file mode 100644 index 000000000..b4770bde7 --- /dev/null +++ b/internal/js/modules/k6/asyncgroup.go @@ -0,0 +1,136 @@ +package k6 + +import ( + "reflect" + "time" + + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/js/modules" + "go.k6.io/k6/v2/metrics" +) + +// asyncGroup retains the invocation state used by the settlement handlers. +type asyncGroup struct { + vu modules.VU + start time.Time + tagsAndMeta metrics.TagsAndMeta + // settled prevents duplicate emission and tells Group when a thenable settled synchronously. + settled bool +} + +func (g *asyncGroup) emitDuration(failed bool) { + if g.settled { + return + } + g.settled = true + + // Group only creates asyncGroup for an active VU, whose state remains allocated across + // iterations and cancellation. + emitGroupDuration(g.vu, g.start, time.Now(), g.tagsAndMeta, failed) +} + +func emitGroupDuration(vu modules.VU, start, end time.Time, tagsAndMeta metrics.TagsAndMeta, failed bool) { + state := vu.State() + metrics.PushIfNotDone(vu.Context(), state.Samples, metrics.Sample{ + TimeSeries: metrics.TimeSeries{ + Metric: state.BuiltinMetrics.GroupDuration, + Tags: tagsAndMeta.Tags, + }, + Time: end, + Value: metrics.D(end.Sub(start)), + Metadata: tagsAndMeta.Metadata, + GroupFailed: failed, + }) +} + +func (g *asyncGroup) onFulfilled(call sobek.FunctionCall) sobek.Value { + g.emitDuration(false) + return call.Argument(0) +} + +func (g *asyncGroup) onRejected(call sobek.FunctionCall) sobek.Value { + g.emitDuration(true) + // Sobek uses a panic to throw an arbitrary JavaScript value from a Go callback. Returning a Go + // error here would wrap the value and change the promise's rejection reason. + panic(call.Argument(0)) +} + +// runAsyncGroup attaches duration emission to a callback's Promise-like result. +func (mi *K6) runAsyncGroup( + start time.Time, + tagsAndMeta metrics.TagsAndMeta, + thenFn sobek.Callable, + thenable sobek.Value, +) (sobek.Value, bool, error) { + g := &asyncGroup{ + vu: mi.vu, + start: start, + tagsAndMeta: tagsAndMeta, + } + result, err := g.run(thenFn, thenable) + return result, g.settled, err +} + +func (g *asyncGroup) run(thenFn sobek.Callable, thenable sobek.Value) (sobek.Value, error) { + rt := g.vu.Runtime() + if thenable.ExportType() == reflect.TypeFor[*sobek.Promise]() { + return thenFn( + thenable, + rt.ToValue(g.onFulfilled), + rt.ToValue(g.onRejected), + ) + } + + // A generic thenable is not required to return a Promise from then(). Assimilate its settlement + // into a Sobek Promise so group() remains awaitable and inherits Promise resolution semantics, + // including first-settlement-wins and recursively adopting another Promise. + promise, resolve, reject := rt.NewPromise() + settle := func(settler func(any) error) func(sobek.FunctionCall) sobek.Value { + return func(call sobek.FunctionCall) sobek.Value { + if err := settler(call.Argument(0)); err != nil { + panic(err) + } + return sobek.Undefined() + } + } + + _, err := thenFn( + thenable, + rt.ToValue(settle(resolve)), + rt.ToValue(settle(reject)), + ) + if err != nil { + // A JavaScript throw while invoking then() rejects the assimilating Promise. If the thenable + // already settled, Sobek's resolving functions ignore this later rejection. + exception, ok := err.(*sobek.Exception) //nolint:errorlint // preserve the JavaScript value + if !ok { + return nil, err + } + if err = reject(exception.Value()); err != nil { + return nil, err + } + } + + // Continue through the common settlement path: reject may have been ignored if the thenable + // settled first, and the handlers below own duration emission. + promiseValue := rt.ToValue(promise) + promiseThen, ok := asThenable(promiseValue) + if !ok { + panic("new Sobek promise has no callable then method") + } + return promiseThen( + promiseValue, + rt.ToValue(g.onFulfilled), + rt.ToValue(g.onRejected), + ) +} + +// Property access follows JavaScript semantics and may throw. +func asThenable(v sobek.Value) (sobek.Callable, bool) { + obj, ok := v.(*sobek.Object) + if !ok { + return nil, false + } + return sobek.AssertFunction(obj.Get("then")) +} diff --git a/internal/js/modules/k6/asyncgroup_test.go b/internal/js/modules/k6/asyncgroup_test.go new file mode 100644 index 000000000..905804777 --- /dev/null +++ b/internal/js/modules/k6/asyncgroup_test.go @@ -0,0 +1,919 @@ +package k6 + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/metrics" +) + +func checkGroups(t *testing.T, tc *testCase) map[string]string { + t.Helper() + + checks := make(map[string]string) + checksMetric := tc.testRuntime.VU.State().BuiltinMetrics.Checks + for _, sampleContainer := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sampleContainer.GetSamples() { + if sample.Metric != checksMetric { + continue + } + name, ok := sample.Tags.Get(metrics.TagCheck.String()) + require.True(t, ok) + _, duplicate := checks[name] + require.Falsef(t, duplicate, "duplicate check sample %q", name) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + checks[name] = group + } + } + return checks +} + +func newOrderRecorder(t *testing.T, tc *testCase) *[]string { + t.Helper() + order := new([]string) + require.NoError(t, tc.testRuntime.VU.Runtime().Set("mark", func(s string) { + *order = append(*order, s) + })) + return order +} + +// Regression test for #2728 and #3392. +func TestThenContinuationKeepsGroup(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const delay = () => { return Promise.resolve(); }; + await k6.group("coolgroup", () => { + k6.check(null, {sync: true}); + return delay(1).then(() => { k6.check(null, {continuation: true}); }); + }); + k6.check(null, {after: true}); + })() + `) + require.NoError(t, err) + + assert.Equal(t, map[string]string{ + "sync": "::coolgroup", + "continuation": "::coolgroup", + "after": lib.RootGroupPath, + }, checkGroups(t, tc)) +} + +// Regression test for #2728's manual group re-entry workaround. +func TestManualGroupInThenContinuation(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const delay = () => { return Promise.resolve(); }; + await delay(1).then(() => { + k6.check(null, {before: true}); + return k6.group("coolgroup", () => { k6.check(null, {inside: true}); }); + }); + k6.check(null, {after: true}); + })() + `) + require.NoError(t, err) + + assert.Equal(t, map[string]string{ + "before": lib.RootGroupPath, + "inside": "::coolgroup", + "after": lib.RootGroupPath, + }, checkGroups(t, tc)) +} + +// Regression test for #2848. +func TestAsyncGroupExecutionOrder(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + script string + expected []string + }{ + { + name: "synchronous work outside group", + script: ` + (async () => { + await k6.group("somename", async () => { + mark("A"); + await Promise.resolve(); + mark("B"); + }); + mark("after-group"); + })(); + mark("C"); + `, + expected: []string{"A", "C", "B", "after-group"}, + }, + { + name: "queued promise reaction", + script: ` + (async () => { + await k6.group("somename", async () => { + mark("A"); + await Promise.resolve(); + mark("B"); + }); + mark("after-group"); + })(); + Promise.resolve("D").then(() => { mark("D"); }); + mark("C"); + `, + expected: []string{"A", "C", "B", "D", "after-group"}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + tc := testCaseRuntime(t) + order := newOrderRecorder(t, tc) + _, err := tc.testRuntime.RunOnEventLoop(test.script) + require.NoError(t, err) + require.Equal(t, test.expected, *order) + }) + } +} + +// Regression test for #5435. +func TestConcurrentAsyncGroupsDurationsAndTags(t *testing.T) { + t.Parallel() + + tc := testCaseRuntime(t) + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + const check = (name) => k6.check(null, {[name]: true}); + const slow = k6.group("coolgroup", async () => { + check("slow:start"); + await delay(10); + check("slow:afterFirst"); + await delay(40).then(() => {}); + check("slow:afterSecond"); + }); + const fast = k6.group("fastgroup", async () => { + check("fast:start"); + await delay(10).then(() => {}); + check("fast:afterFirst"); + }); + await Promise.all([slow, fast]); + check("after"); + })() + `) + require.NoError(t, err) + + builtinMetrics := tc.testRuntime.VU.State().BuiltinMetrics + durByGroup := map[string]float64{} + durationCounts := map[string]int{} + checkGroups := map[string]string{} + var checkCount int + for _, sampleContainer := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sampleContainer.GetSamples() { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + switch sample.Metric { + case builtinMetrics.GroupDuration: + durByGroup[group] = sample.Value + durationCounts[group]++ + case builtinMetrics.Checks: + checkCount++ + name, ok := sample.Tags.Get(metrics.TagCheck.String()) + require.True(t, ok) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + checkGroups[name] = group + } + } + } + assert.Equal(t, 6, checkCount) + assert.Equal(t, map[string]int{"::coolgroup": 1, "::fastgroup": 1}, durationCounts) + + assert.Equal(t, map[string]string{ + "slow:start": "::coolgroup", + "slow:afterFirst": "::coolgroup", + "slow:afterSecond": "::coolgroup", + "fast:start": "::fastgroup", + "fast:afterFirst": "::fastgroup", + "after": lib.RootGroupPath, + }, checkGroups) + + slow, okSlow := durByGroup["::coolgroup"] + fast, okFast := durByGroup["::fastgroup"] + + require.True(t, okSlow, "missing group_duration for ::coolgroup, got %v", durByGroup) + require.True(t, okFast, "missing group_duration for ::fastgroup, got %v", durByGroup) + + t.Logf("group_duration ::coolgroup (slow) = %.2fms, ::fastgroup (fast) = %.2fms", slow, fast) + + assert.Greater(t, slow, fast, + "slow group (::coolgroup=%.2fms) must last clearly longer than fast group (::fastgroup=%.2fms)", + slow, fast) +} + +func TestConcurrentAsyncGroupsKeepIndependentTags(t *testing.T) { + t.Parallel() + + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + const check = (name) => k6.check(null, {[name]: true}); + const names = ["alpha", "beta", "gamma", "delta"]; + const grouped = names.map((name, index) => k6.group(name, async () => { + check(name + ":start"); + await delay((index + 1) * 5); + check(name + ":afterTimer"); + await Promise.resolve(); + check(name + ":afterPromise"); + })); + await Promise.all(grouped); + check("after"); + })() + `) + require.NoError(t, err) + + durationCounts := make(map[string]int) + checkGroups := make(map[string]string) + var checkCount int + builtinMetrics := tc.testRuntime.VU.State().BuiltinMetrics + for _, sampleContainer := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sampleContainer.GetSamples() { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + switch sample.Metric { + case builtinMetrics.GroupDuration: + durationCounts[group]++ + case builtinMetrics.Checks: + checkCount++ + name, ok := sample.Tags.Get(metrics.TagCheck.String()) + require.True(t, ok) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + checkGroups[name] = group + } + } + } + require.Equal(t, 13, checkCount) + require.Len(t, checkGroups, 13) + + for check, group := range checkGroups { + if check == "after" { + assert.Equal(t, lib.RootGroupPath, group) + continue + } + + name, _, ok := strings.Cut(check, ":") + require.True(t, ok, "invalid check name %q", check) + expectedGroup, pathErr := lib.NewGroupPath(lib.RootGroupPath, name) + require.NoError(t, pathErr) + assert.Equalf(t, expectedGroup, group, "check %q saw the wrong group", check) + } + + assert.Equal(t, map[string]int{ + "::alpha": 1, + "::beta": 1, + "::gamma": 1, + "::delta": 1, + }, durationCounts) +} + +func TestAsyncGroupScopesCompleteMetricContext(t *testing.T) { + t.Parallel() + + tc := testCaseRuntime(t) + state := tc.testRuntime.VU.State() + setContext := func(phase string) { + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetTag("phase", phase) + tagsAndMeta.SetMetadata("trace", phase) + }) + } + setContext("root") + require.NoError(t, tc.testRuntime.VU.Runtime().Set("setContext", setContext)) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + await k6.group("g", async () => { + setContext("inside"); + k6.check(null, {beforeAwait: true}); + await Promise.resolve(); + k6.check(null, {afterAwait: true}); + }); + k6.check(null, {outside: true}); + })() + `) + require.NoError(t, err) + + wantPhase := map[string]string{ + "beforeAwait": "inside", + "afterAwait": "inside", + "outside": "root", + } + for _, sampleContainer := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sampleContainer.GetSamples() { + if sample.Metric != state.BuiltinMetrics.Checks { + continue + } + name, _ := sample.Tags.Get(metrics.TagCheck.String()) + phase, _ := sample.Tags.Get("phase") + assert.Equal(t, wantPhase[name], phase, "check %q used the wrong tag context", name) + assert.Equal(t, wantPhase[name], sample.Metadata["trace"], + "check %q used the wrong metadata context", name) + delete(wantPhase, name) + } + } + require.Empty(t, wantPhase) + + current := state.Tags.GetCurrentValues() + phase, _ := current.Tags.Get("phase") + require.Equal(t, "root", phase) + require.Equal(t, "root", current.Metadata["trace"]) +} + +func TestDisabledAsyncMetricContextKeepsMutableTagsLive(t *testing.T) { + t.Parallel() + + tc := testCaseRuntimeWithAsyncMetricContext(t, false) + state := tc.testRuntime.VU.State() + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetTag("phase", "before") + tagsAndMeta.SetMetadata("trace", "before") + }) + require.NoError(t, tc.testRuntime.VU.Runtime().Set("setContext", func() { + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetTag("phase", "after") + tagsAndMeta.SetMetadata("trace", "after") + }) + })) + + _, err := tc.testRuntime.RunOnEventLoop(`k6.group("g", () => setContext())`) + require.NoError(t, err) + + current := state.Tags.GetCurrentValues() + phase, _ := current.Tags.Get("phase") + require.Equal(t, "after", phase) + require.Equal(t, "after", current.Metadata["trace"]) + + samples := metrics.GetBufferedSamples(tc.samples) + require.Len(t, samples, 1) + sample, ok := samples[0].(metrics.Sample) + require.True(t, ok) + phase, _ = sample.Tags.Get("phase") + require.Equal(t, "after", phase) + require.Equal(t, "after", sample.Metadata["trace"]) +} + +// Regression test for #5673. +func TestAsyncContextGroupPropagation(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + const delay = (t) => new Promise((r) => setTimeout(r, t)); + (async () => { + await k6.group("coolgroup", async () => { + k6.check(null, {sync: true}); + await delay(5); + k6.check(null, {afterAwait: true}); + await delay(5).then(() => { k6.check(null, {continuation: true}); }); + }); + k6.check(null, {after: true}); + })() + `) + require.NoError(t, err) + + assert.Equal(t, map[string]string{ + "sync": "::coolgroup", + "afterAwait": "::coolgroup", + "continuation": "::coolgroup", + "after": lib.RootGroupPath, + }, checkGroups(t, tc)) +} + +func TestGroupTrackerRestoresStageGroup(t *testing.T) { + t.Parallel() + + for _, stage := range []string{"setup", "teardown"} { + t.Run(stage, func(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + stageGroup, err := lib.NewGroupPath(lib.RootGroupPath, stage) + require.NoError(t, err) + tc.testRuntime.VU.State().Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetSystemTagOrMeta(metrics.TagGroup, stageGroup) + }) + + _, err = tc.testRuntime.RunOnEventLoop(` + Promise.resolve().then(() => k6.check(null, {reaction: true})); + k6.group("warmup", () => {}); + `) + require.NoError(t, err) + + assert.Equal(t, map[string]string{"reaction": stageGroup}, checkGroups(t, tc)) + group, _ := tc.testRuntime.VU.State().Tags.GetCurrentValues().Tags.Get( + metrics.TagGroup.String()) + require.Equal(t, stageGroup, group) + }) + } +} + +func TestGroupSupportsPromiseLikeThenable(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + order := newOrderRecorder(t, tc) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const grouped = k6.group("custom", () => ({ + then(onFulfilled) { + setTimeout(() => { + mark("settled"); + onFulfilled(42); + }, 1); + } + })); + mark(grouped instanceof Promise ? "promise" : "not-promise"); + const value = await grouped; + mark("after:" + value); + })(); + `) + require.NoError(t, err) + require.Equal(t, []string{"promise", "settled", "after:42"}, *order) +} + +func TestGroupThenablePreservesSettlementIdentity(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const fulfillment = {}; + const fulfilled = await k6.group("fulfilled", () => ({ + then(onFulfilled) { return Promise.resolve(fulfillment).then(onFulfilled); } + })); + if (fulfilled !== fulfillment) { + throw new Error("fulfillment identity changed"); + } + + const rejection = {}; + let caught; + try { + await k6.group("rejected", () => ({ + then(_, onRejected) { return Promise.reject(rejection).then(undefined, onRejected); } + })); + } catch (reason) { + caught = reason; + } + if (caught !== rejection) { + throw new Error("rejection identity changed"); + } + })(); + `) + require.NoError(t, err) +} + +// Primitive results must not be boxed for thenable detection. +func TestGroupSynchronousResultStaysSynchronous(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + result, err := tc.testRuntime.RunOnEventLoop(` + let thenCalled = false; + Number.prototype.then = function() { thenCalled = true; }; + const result = k6.group("sync", () => 42); + if (thenCalled) { throw new Error("synchronous result was treated as a thenable"); } + result; + `) + require.NoError(t, err) + require.Equal(t, int64(42), result.Export()) + group, _ := tc.testRuntime.VU.State().Tags.GetCurrentValues().Tags.Get(metrics.TagGroup.String()) + require.Equal(t, lib.RootGroupPath, group) + + samples := metrics.GetBufferedSamples(tc.samples) + require.Len(t, samples, 1) + sample, ok := samples[0].(metrics.Sample) + require.True(t, ok) + require.Equal(t, tc.testRuntime.VU.State().BuiltinMetrics.GroupDuration, sample.Metric) + metricGroup, ok := sample.Tags.Get(metrics.TagGroup.String()) + require.True(t, ok) + require.Equal(t, "::sync", metricGroup) +} + +// group_duration uses the tag and metadata snapshot captured before the callback runs. +func TestGroupDurationUsesEntryTagsAndMetadata(t *testing.T) { + t.Parallel() + + for name, script := range map[string]string{ + "sync": `k6.group("g", () => setPhase("after"));`, + "async": `(async () => { + await k6.group("g", async () => { + setPhase("after"); + await Promise.resolve(); + }); + })();`, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + state := tc.testRuntime.VU.State() + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetTag("phase", "before") + tagsAndMeta.SetMetadata("trace", "before") + }) + require.NoError(t, tc.testRuntime.VU.Runtime().Set("setPhase", func(phase string) { + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetTag("phase", phase) + tagsAndMeta.SetMetadata("trace", phase) + }) + })) + + _, err := tc.testRuntime.RunOnEventLoop(script) + require.NoError(t, err) + + samples := metrics.GetBufferedSamples(tc.samples) + require.Len(t, samples, 1) + sample, ok := samples[0].(metrics.Sample) + require.True(t, ok) + phase, ok := sample.Tags.Get("phase") + require.True(t, ok) + require.Equal(t, "before", phase) + require.Equal(t, "before", sample.Metadata["trace"]) + }) + } +} + +// A throwing then getter must restore the parent and still record duration. +func TestGroupThrowingThenGetterRestoresParent(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + try { + k6.group("bad", () => ({ + get then() { throw new Error("boom"); } + })); + } catch (_) {} + k6.check(null, {afterFailure: true}); + k6.group("next", () => k6.check(null, {next: true})); + k6.check(null, {afterNext: true}); + `) + require.NoError(t, err) + + builtinMetrics := tc.testRuntime.VU.State().BuiltinMetrics + var badGroupDurations int + checks := make(map[string]string) + for _, sc := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sc.GetSamples() { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + switch sample.Metric { + case builtinMetrics.GroupDuration: + if group == "::bad" { + badGroupDurations++ + } + case builtinMetrics.Checks: + name, ok := sample.Tags.Get(metrics.TagCheck.String()) + require.True(t, ok) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + checks[name] = group + } + } + } + assert.Equal(t, map[string]string{ + "afterFailure": lib.RootGroupPath, + "next": "::next", + "afterNext": lib.RootGroupPath, + }, checks) + require.Equal(t, 1, badGroupDurations) +} + +// The Go implementation must not depend on the old private JavaScript globals. +func TestGroupNotTamperableViaGlobals(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + globalThis.__k6group = function () { throw new Error("hijacked group()"); }; + globalThis.__k6AsyncGroup = { getGroupTag: function () { return "tampered"; } }; + k6.group("real", () => { k6.check(null, {inside: true}); }); + `) + require.NoError(t, err) + assert.Equal(t, map[string]string{"inside": "::real"}, checkGroups(t, tc)) +} + +// A synchronously throwing then() rejects the returned promise and still records duration. +func TestGroupThenableThrowsStillEmitsDuration(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const reason = {}; + let caught; + try { + await k6.group("g", () => ({ then() { throw reason; } })); + } catch (error) { + caught = error; + } + if (caught !== reason) { + throw new Error("rejection identity changed"); + } + })(); + `) + require.NoError(t, err) + + gd := tc.testRuntime.VU.State().BuiltinMetrics.GroupDuration + expectedGroup, err2 := lib.NewGroupPath(lib.RootGroupPath, "g") + require.NoError(t, err2) + + var found bool + for _, sc := range metrics.GetBufferedSamples(tc.samples) { + for _, s := range sc.GetSamples() { + if s.Metric == gd { + g, _ := s.Tags.Get("group") + if g == expectedGroup { + found = true + } + } + } + } + require.True(t, found, "group_duration for %q must be emitted even when the thenable throws synchronously", expectedGroup) +} + +func TestGroupSynchronousThenableRejectionEmitsDurationOnce(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + const rejection = new Error('rejected'); + let caught; + try { + await k6.group('g', () => ({ + then(_, reject) { reject(rejection); }, + })); + } catch (reason) { + caught = reason; + } + if (caught !== rejection) { + throw new Error('rejection identity changed'); + } + k6.check(null, {after: true}); + })(); + `) + require.NoError(t, err) + + var durations int + for _, sc := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range sc.GetSamples() { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + switch sample.Metric { + case tc.testRuntime.VU.State().BuiltinMetrics.GroupDuration: + if group == "::g" { + durations++ + } + case tc.testRuntime.VU.State().BuiltinMetrics.Checks: + assert.Equal(t, lib.RootGroupPath, group) + } + } + } + require.Equal(t, 1, durations) +} + +// Compare benchmark deltas: RunOnEventLoop adds the same compile and drain +// overhead to every case. + +func benchRun(b *testing.B, tc *testCase, src string) { + b.Helper() + if _, err := tc.testRuntime.RunOnEventLoop(src); err != nil { + b.Fatal(err) + } +} + +// Prevent group_duration emission from filling the sample buffer. +func benchDrainSamples(tc *testCase) func() { + done := make(chan struct{}) + go func() { + for { + select { + case <-done: + return + case <-tc.samples: + } + } + }() + return func() { close(done) } +} + +func BenchmarkNoGroupSync(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => 42;`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkNoGroupSyncFeatureDisabled(b *testing.B) { + tc := testCaseRuntimeWithAsyncMetricContext(b, false) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => 42;`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkGroupSync(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => { k6.group("x", () => {}); };`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkGroupAsync(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => k6.group("x", async () => { await Promise.resolve(); });`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkPromiseOnce(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => Promise.resolve(1);`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkAwaitPromiseOnce(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = async () => { await Promise.resolve(); };`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkAwaitPromiseOnceFeatureDisabled(b *testing.B) { + tc := testCaseRuntimeWithAsyncMetricContext(b, false) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = async () => { await Promise.resolve(); };`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkAwaitGroupPromise(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = async () => { await k6.group("x", () => Promise.resolve()); };`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +func BenchmarkGroupPromise(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, `globalThis.work = () => k6.group("x", () => Promise.resolve(1));`) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +const benchPromiseWorkload = `globalThis.work = async () => { + let value = 0; + for (let i = 0; i < 100; i++) { + value = await Promise.resolve(value + 1); + } + return value; +};` + +func BenchmarkPromiseWorkload(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, benchPromiseWorkload) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +const benchSparseAsyncGroup = `globalThis.work = async () => { + let value = 0; + for (let i = 0; i < 100; i++) { + if (i === 50) { + value = await k6.group("x", async () => Promise.resolve(value + 1)); + } else { + value = await Promise.resolve(value + 1); + } + } + return value; +};` + +func BenchmarkSparseAsyncGroup(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, benchSparseAsyncGroup) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +const benchSparsePromiseGroup = `globalThis.work = async () => { + let value = 0; + for (let i = 0; i < 100; i++) { + if (i === 50) { + value = await k6.group("x", () => Promise.resolve(value + 1)); + } else { + value = await Promise.resolve(value + 1); + } + } + return value; +};` + +func BenchmarkSparsePromiseGroup(b *testing.B) { + tc := testCaseRuntime(b) + defer benchDrainSamples(tc)() + benchRun(b, tc, benchSparsePromiseGroup) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +const benchPromiseChain = `globalThis.work = () => { + let p = Promise.resolve(0); + for (let i = 0; i < 20; i++) { + p = p.then((v) => v + 1); + } + return p; +};` + +func BenchmarkPromiseChainFeatureDisabled(b *testing.B) { + tc := testCaseRuntimeWithAsyncMetricContext(b, false) + benchRun(b, tc, benchPromiseChain) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} + +// Compare with BenchmarkPromiseChainFeatureDisabled to measure tracker overhead. +func BenchmarkPromiseChainWithTracker(b *testing.B) { + tc := testCaseRuntime(b) + benchRun(b, tc, benchPromiseChain) + + b.ReportAllocs() + b.ResetTimer() + for b.Loop() { + benchRun(b, tc, `work()`) + } +} diff --git a/internal/js/modules/k6/browser/browser/browser_context_mapping.go b/internal/js/modules/k6/browser/browser/browser_context_mapping.go index 456cf2c52..a77c24225 100644 --- a/internal/js/modules/k6/browser/browser/browser_context_mapping.go +++ b/internal/js/modules/k6/browser/browser/browser_context_mapping.go @@ -20,13 +20,13 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin } rt := vu.Runtime() - return mapping{ - "addCookies": func(cookies []*common.Cookie) *sobek.Promise { + return finishMapping(mapping{ + "addCookies": passiveCall(func(cookies []*common.Cookie) *sobek.Promise { return promise(vu, func() (any, error) { return nil, bc.AddCookies(cookies) //nolint:wrapcheck }) - }, - "addInitScript": func(script sobek.Value) *sobek.Promise { + }), + "addInitScript": passiveCall(func(script sobek.Value) *sobek.Promise { return promise(vu, func() (any, error) { if k6common.IsNullish(script) { return nil, nil @@ -54,32 +54,33 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin return nil, bc.AddInitScript(source) //nolint:wrapcheck }) - }, - "browser": func() mapping { - // the browser is grabbed from VU. - return mapBrowser(vu) - }, - "clearCookies": func() *sobek.Promise { + }), + "browser": passiveCall(func() mapping { + return mapBrowser(vu, func() (*common.Browser, error) { + return bc.Browser(), nil + }) + }), + "clearCookies": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, bc.ClearCookies() //nolint:wrapcheck }) - }, - "clearPermissions": func() *sobek.Promise { + }), + "clearPermissions": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, bc.ClearPermissions() //nolint:wrapcheck }) - }, - "close": func() *sobek.Promise { + }), + "close": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, bc.Close() //nolint:wrapcheck }) - }, - "cookies": func(urls ...string) *sobek.Promise { + }), + "cookies": passiveCall(func(urls ...string) *sobek.Promise { return promise(vu, func() (any, error) { return bc.Cookies(urls...) //nolint:wrapcheck }) - }, - "grantPermissions": func(permissions []string, opts sobek.Value) (*sobek.Promise, error) { + }), + "grantPermissions": passiveCall(func(permissions []string, opts sobek.Value) (*sobek.Promise, error) { popts, err := exportTo[common.GrantPermissionsOptions](vu.Runtime(), opts) if err != nil { return nil, fmt.Errorf("parsing grant permission options: %w", err) @@ -87,10 +88,10 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin return promise(vu, func() (any, error) { return nil, bc.GrantPermissions(permissions, popts) }), nil - }, - "setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout, - "setDefaultTimeout": bc.SetDefaultTimeout, - "setGeolocation": func(geolocation sobek.Value) (*sobek.Promise, error) { + }), + "setDefaultNavigationTimeout": passiveCall(bc.SetDefaultNavigationTimeout), + "setDefaultTimeout": passiveCall(bc.SetDefaultTimeout), + "setGeolocation": passiveCall(func(geolocation sobek.Value) (*sobek.Promise, error) { gl, err := exportTo[common.Geolocation](vu.Runtime(), geolocation) if err != nil { return nil, fmt.Errorf("parsing geo location: %w", err) @@ -98,8 +99,8 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin return promise(vu, func() (any, error) { return nil, bc.SetGeolocation(&gl) }), nil - }, - "setHTTPCredentials": func(httpCredentials sobek.Value) (*sobek.Promise, error) { + }), + "setHTTPCredentials": passiveCall(func(httpCredentials sobek.Value) (*sobek.Promise, error) { creds, err := exportTo[common.Credentials](rt, httpCredentials) if err != nil { return nil, fmt.Errorf("parsing HTTP credentials: %w", err) @@ -107,13 +108,13 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin return promise(vu, func() (any, error) { return nil, bc.SetHTTPCredentials(creds) //nolint:staticcheck }), nil - }, - "setOffline": func(offline bool) *sobek.Promise { + }), + "setOffline": passiveCall(func(offline bool) *sobek.Promise { return promise(vu, func() (any, error) { return nil, bc.SetOffline(offline) //nolint:wrapcheck }) - }, - "waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) { + }), + "waitForEvent": passiveCall(func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) { rt := vu.Runtime() ctx := vu.Context() @@ -127,8 +128,9 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin // Waits until the event that satisfies the predicate. if popts.PredicateFn != nil { + captured := k6common.CaptureMetricContext(vu.State()) pred = func(p *common.Page) (bool, error) { - return queueTask(ctx, vu.get(ctx, p.TargetID()), func() (bool, error) { + return queueTaskWithMetricContext(ctx, vu.get(ctx, p.TargetID()), captured, func() (bool, error) { v, err := popts.PredicateFn(rt.ToValue(p)) if err != nil { return false, err @@ -150,8 +152,8 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin } return mapPage(vu, p), nil }), nil - }, - "pages": func() *sobek.Object { + }), + "pages": passiveCall(func() *sobek.Object { var ( mpages []mapping pages = bc.Pages() @@ -165,17 +167,25 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin } return rt.ToValue(mpages).ToObject(rt) - }, - "newPage": func() *sobek.Promise { + }), + "newPage": passiveCall(func() *sobek.Promise { + var setNetworkTags func(*common.Page) + if k6common.AsyncMetricContextEnabled(vu.State()) { + tagsAndMeta := vu.State().Tags.GetCurrentValues() + setNetworkTags = func(page *common.Page) { page.SetNetworkFallbackTagsAndMeta(tagsAndMeta) } + } return promise(vu, func() (any, error) { page, err := bc.NewPage() if err != nil { return nil, err //nolint:wrapcheck } + if setNetworkTags != nil { + setNetworkTags(page) + } return mapPage(vu, page), nil }) - }, - } + }), + }) } // waitForEventOptions are the options used by the browserContext.waitForEvent API. diff --git a/internal/js/modules/k6/browser/browser/browser_mapping.go b/internal/js/modules/k6/browser/browser/browser_mapping.go index be408278f..827e6a020 100644 --- a/internal/js/modules/k6/browser/browser/browser_mapping.go +++ b/internal/js/modules/k6/browser/browser/browser_mapping.go @@ -6,43 +6,49 @@ import ( "github.com/grafana/sobek" "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" + k6common "go.k6.io/k6/v2/js/common" ) -// mapBrowser to the JS module. +// browserProvider is a function that provides a browser instance. +type browserProvider func() (*common.Browser, error) + +// mapBrowser to the JS module API using the provider to get the browser instance. +// This lets the same mapping serve both the managed browser and a browser +// obtained via chromium.connectOverCDP, since they share the same API. // -//nolint:gocognit -func mapBrowser(vu moduleVU) mapping { - return mapping{ - "context": func() (mapping, error) { - b, err := vu.browser() +//nolint:gocognit,funlen +func mapBrowser(vu moduleVU, browser browserProvider) mapping { + m := mapping{ + "context": passiveCall(func() (mapping, error) { + b, err := browser() if err != nil { return nil, err } return mapBrowserContext(vu, b.Context()), nil - }, - "closeContext": func() *sobek.Promise { + }), + "closeContext": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { - b, err := vu.browser() + b, err := browser() if err != nil { return nil, err } return nil, b.CloseContext() //nolint:wrapcheck }) - }, - "isConnected": func() (bool, error) { - b, err := vu.browser() + }), + "isConnected": passiveCall(func() (bool, error) { + b, err := browser() if err != nil { return false, err } return b.IsConnected(), nil - }, - "newContext": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "newContext": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parseBrowserContextOptions(vu.Runtime(), opts) if err != nil { return nil, fmt.Errorf("parsing browser.newContext options: %w", err) } return promise(vu, func() (any, error) { - b, err := vu.browser() + b, err := browser() if err != nil { return nil, err } @@ -56,28 +62,33 @@ func mapBrowser(vu moduleVU) mapping { return mapBrowserContext(vu, bctx), nil }), nil - }, - "userAgent": func() (string, error) { - b, err := vu.browser() + }), + "userAgent": passiveCall(func() (string, error) { + b, err := browser() if err != nil { return "", err } return b.UserAgent(), nil - }, - "version": func() (string, error) { - b, err := vu.browser() + }), + "version": passiveCall(func() (string, error) { + b, err := browser() if err != nil { return "", err } return b.Version(), nil - }, - "newPage": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "newPage": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parseBrowserContextOptions(vu.Runtime(), opts) if err != nil { return nil, fmt.Errorf("parsing browser.newPage options: %w", err) } + var setNetworkTags func(*common.Page) + if k6common.AsyncMetricContextEnabled(vu.State()) { + tagsAndMeta := vu.State().Tags.GetCurrentValues() + setNetworkTags = func(page *common.Page) { page.SetNetworkFallbackTagsAndMeta(tagsAndMeta) } + } return promise(vu, func() (any, error) { - b, err := vu.browser() + b, err := browser() if err != nil { return nil, err } @@ -85,14 +96,50 @@ func mapBrowser(vu moduleVU) mapping { if err != nil { return nil, err //nolint:wrapcheck } + if setNetworkTags != nil { + setNetworkTags(page) + } if err := initBrowserContext(b.Context(), vu.testRunID); err != nil { return nil, err } return mapPage(vu, page), nil }), nil - }, + }), + } + + addUserManagedClose(vu, m, browser) + + return finishMapping(m) +} + +// addUserManagedClose adds a close() method to the browser mapping when the +// browser is user-managed (created via chromium.connectOverCDP). close() +// untracks the browser from the registry (so the IterEnd/Exit sweep does not +// double-close) and closes it. Managed browsers don't get a close(): the +// registry owns their lifecycle. +func addUserManagedClose(vu moduleVU, m mapping, browser browserProvider) { + // In the init context (e.g. the module-level browser export) there's no + // state and no iteration browser to resolve; the managed browser never + // exposes close() anyway. + if vu.State() == nil { + return + } + b, err := browser() + if err != nil { + return + } + iter, ok := vu.userManagedIter(b) + if !ok { + return } + m["close"] = passiveCall(func() *sobek.Promise { + return promise(vu, func() (any, error) { + vu.untrackUserManagedBrowser(iter, b) + b.Close() + return nil, nil + }) + }) } func initBrowserContext(bctx *common.BrowserContext, testRunID string) error { diff --git a/internal/js/modules/k6/browser/browser/chromium_mapping.go b/internal/js/modules/k6/browser/browser/chromium_mapping.go new file mode 100644 index 000000000..53fd16cf6 --- /dev/null +++ b/internal/js/modules/k6/browser/browser/chromium_mapping.go @@ -0,0 +1,40 @@ +package browser + +import ( + "fmt" + + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/internal/js/modules/k6/browser/chromium" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" +) + +// mapChromium maps the Chromium browser type API to the JS module. +func mapChromium(vu moduleVU, bt *chromium.BrowserType) mapping { + return finishMapping(mapping{ + "connectOverCDP": passiveCall(func(wsEndpoint string) *sobek.Promise { + return promise(vu, func() (any, error) { + iter := vu.State().Iteration + + // Clone the BrowserType for this call so concurrent + // connectOverCDP calls in the same iteration (e.g., via + // Promise.all) don't race on its mutable state. + connBT := bt.Clone() + + // Link the connection to the iteration trace and connect. + tracedCtx := vu.startConnectTrace(vu.Context(), iter) + b, err := connBT.ConnectOverCDP(tracedCtx, wsEndpoint) + if err != nil { + return nil, fmt.Errorf("connecting to Chromium over CDP: %w", err) + } + + // Register for guaranteed cleanup at IterEnd / Exit. + vu.trackUserManagedBrowser(iter, b) + + return mapBrowser(vu, func() (*common.Browser, error) { + return b, nil + }), nil + }) + }), + }) +} diff --git a/internal/js/modules/k6/browser/browser/console_message_mapping.go b/internal/js/modules/k6/browser/browser/console_message_mapping.go index 1a6d3e037..ec1f75e01 100644 --- a/internal/js/modules/k6/browser/browser/console_message_mapping.go +++ b/internal/js/modules/k6/browser/browser/console_message_mapping.go @@ -8,8 +8,8 @@ import ( func mapConsoleMessage(vu moduleVU, event common.PageEvent) mapping { cm := event.ConsoleMessage - return mapping{ - "args": func() []mapping { + return finishMapping(mapping{ + "args": passiveCall(func() []mapping { args := cm.Args margs := make([]mapping, 0, len(args)) for _, arg := range args { @@ -18,17 +18,17 @@ func mapConsoleMessage(vu moduleVU, event common.PageEvent) mapping { } return margs - }, + }), // page(), text() and type() are defined as // functions in order to match Playwright's API - "page": func() mapping { + "page": passiveCall(func() mapping { return mapPage(vu, cm.Page) - }, - "text": func() string { + }), + "text": passiveCall(func() string { return cm.Text - }, - "type": func() string { + }), + "type": passiveCall(func() string { return cm.Type - }, - } + }), + }) } diff --git a/internal/js/modules/k6/browser/browser/element_handle_mapping.go b/internal/js/modules/k6/browser/browser/element_handle_mapping.go index 7868f5da4..9ab237e8d 100644 --- a/internal/js/modules/k6/browser/browser/element_handle_mapping.go +++ b/internal/js/modules/k6/browser/browser/element_handle_mapping.go @@ -4,17 +4,24 @@ import ( "errors" "fmt" maps0 "maps" + "strings" + "time" "github.com/grafana/sobek" "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" + k6common "go.k6.io/k6/v2/js/common" ) // mapElementHandle to the JS module. func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:gocognit,funlen,cyclop + if eh == nil { + return nil + } + rt := vu.Runtime() maps := mapping{ - "boundingBox": func() *sobek.Promise { + "boundingBox": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { box, err := eh.BoundingBox() // We want to avoid errors when an element is not visible or detached and instead @@ -24,19 +31,19 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return box, err }) - }, - "check": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "check": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleSetCheckedOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing check options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Check(popts) //nolint:wrapcheck }), nil - }, - "click": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleClickOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "click": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleClickOptions(rt, opts, eh.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing element click options: %w", err) } @@ -44,8 +51,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: err := eh.Click(popts) return nil, err //nolint:wrapcheck }), nil - }, - "contentFrame": func() *sobek.Promise { + }), + "contentFrame": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { f, err := eh.ContentFrame() if err != nil { @@ -53,36 +60,36 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return mapFrame(vu, f), nil }) - }, - "dblclick": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleDblclickOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dblclick": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleDblclickOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing element double click options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Dblclick(popts) //nolint:wrapcheck }), nil - }, - "dispatchEvent": func(typ string, eventInit sobek.Value) *sobek.Promise { + }), + "dispatchEvent": networkCall(func(typ string, eventInit sobek.Value) *sobek.Promise { return promise(vu, func() (any, error) { return nil, eh.DispatchEvent(typ, exportArg(eventInit)) //nolint:wrapcheck }) - }, - "fill": func(value string, opts sobek.Value) (*sobek.Promise, error) { + }), + "fill": networkCall(func(value string, opts sobek.Value) (*sobek.Promise, error) { popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseElementHandleBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing element fill options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Fill(value, popts) //nolint:wrapcheck }), nil - }, - "focus": func() *sobek.Promise { + }), + "focus": networkCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, eh.Focus() //nolint:wrapcheck }) - }, - "getAttribute": func(name string) *sobek.Promise { + }), + "getAttribute": passiveCall(func(name string) *sobek.Promise { return promise(vu, func() (any, error) { s, ok, err := eh.GetAttribute(name) if err != nil { @@ -93,66 +100,66 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return s, nil }) - }, - "hover": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleHoverOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "hover": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleHoverOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing element hover options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Hover(popts) //nolint:wrapcheck }), nil - }, - "innerHTML": func() *sobek.Promise { + }), + "innerHTML": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.InnerHTML() //nolint:wrapcheck }) - }, - "innerText": func() *sobek.Promise { + }), + "innerText": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.InnerText() //nolint:wrapcheck }) - }, - "inputValue": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "inputValue": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseElementHandleBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing element input value options: %w", err) } return promise(vu, func() (any, error) { return eh.InputValue(popts) //nolint:wrapcheck }), nil - }, - "isChecked": func() *sobek.Promise { + }), + "isChecked": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsChecked() //nolint:wrapcheck }) - }, - "isDisabled": func() *sobek.Promise { + }), + "isDisabled": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsDisabled() //nolint:wrapcheck }) - }, - "isEditable": func() *sobek.Promise { + }), + "isEditable": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsEditable() //nolint:wrapcheck }) - }, - "isEnabled": func() *sobek.Promise { + }), + "isEnabled": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsEnabled() //nolint:wrapcheck }) - }, - "isHidden": func() *sobek.Promise { + }), + "isHidden": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsHidden() //nolint:wrapcheck }) - }, - "isVisible": func() *sobek.Promise { + }), + "isVisible": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return eh.IsVisible() //nolint:wrapcheck }) - }, - "ownerFrame": func() *sobek.Promise { + }), + "ownerFrame": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { f, err := eh.OwnerFrame() if err != nil { @@ -160,19 +167,19 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return mapFrame(vu, f), nil }) - }, - "press": func(key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandlePressOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "press": networkCall(func(key string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandlePressOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing press %q options: %w", key, err) } return promise(vu, func() (any, error) { return nil, eh.Press(key, popts) //nolint:wrapcheck }), nil - }, - "screenshot": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleScreenshotOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "screenshot": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleScreenshotOptions(rt, opts, eh.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing element handle screenshot options: %w", err) } @@ -186,50 +193,50 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: return &ab, nil }), nil - }, - "scrollIntoViewIfNeeded": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "scrollIntoViewIfNeeded": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseElementHandleBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing scrollIntoViewIfNeeded options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.ScrollIntoViewIfNeeded(popts) //nolint:wrapcheck }), nil - }, - "selectOption": func(values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + }), + "selectOption": networkCall(func(values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { convValues, err := ConvertSelectOptionValues(vu.Runtime(), values) if err != nil { return nil, fmt.Errorf("parsing select options values: %w", err) } popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseElementHandleBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing selectOption options: %w", err) } return promise(vu, func() (any, error) { return eh.SelectOption(convValues, popts) //nolint:wrapcheck }), nil - }, - "selectText": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "selectText": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseElementHandleBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing selectText options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.SelectText(popts) //nolint:wrapcheck }), nil - }, - "setChecked": func(checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setChecked": networkCall(func(checked bool, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleSetCheckedOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing setChecked options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.SetChecked(checked, popts) //nolint:wrapcheck }), nil - }, - "setInputFiles": func(files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetInputFilesOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setInputFiles": networkCall(func(files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleSetInputFilesOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } var pfiles common.Files @@ -239,17 +246,17 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: return promise(vu, func() (any, error) { return nil, eh.SetInputFiles(&pfiles, popts) //nolint:wrapcheck }), nil - }, - "tap": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleTapOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "tap": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleTapOptions(rt, opts, eh.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing element tap options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Tap(popts) //nolint:wrapcheck }), nil - }, - "textContent": func() *sobek.Promise { + }), + "textContent": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { s, ok, err := eh.TextContent() if err != nil { @@ -260,37 +267,37 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return s, nil }) - }, - "type": func(text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleTypeOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "type": networkCall(func(text string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleTypeOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Type(text, popts) //nolint:wrapcheck }), nil - }, - "uncheck": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "uncheck": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleSetCheckedOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing uncheck options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.Uncheck(popts) //nolint:wrapcheck }), nil - }, - "waitForElementState": func(state string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleWaitForElementStateOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForElementState": passiveCall(func(state string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseElementHandleWaitForElementStateOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing waitForElementState options: %w", err) } return promise(vu, func() (any, error) { return nil, eh.WaitForElementState(state, popts) //nolint:wrapcheck }), nil - }, - "waitForSelector": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameWaitForSelectorOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForSelector": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameWaitForSelectorOptions(rt, opts, eh.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing waitForSelector %q options: %w", selector, err) } return promise(vu, func() (any, error) { @@ -300,9 +307,9 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return mapElementHandle(vu, eh), nil }), nil - }, + }), } - maps["$"] = func(selector string) *sobek.Promise { + maps["$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { eh, err := eh.Query(selector, common.StrictModeOff) if err != nil { @@ -318,8 +325,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: return ehm, nil }) - } - maps["$$"] = func(selector string) *sobek.Promise { + }) + maps["$$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { ehs, err := eh.QueryAll(selector) if err != nil { @@ -332,10 +339,333 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: } return mehs, nil }) - } + }) - jsHandleMap := mapJSHandle(vu, eh) + jsHandleMap := newJSHandleMapping(vu, eh) maps0.Copy(maps, jsHandleMap) - return maps + return withPageNetworkCalls(vu, eh.Page(), maps) +} + +// parseElementHandleTypeOptions parses the element handle type options from a Sobek value. +// +//nolint:unparam +func parseElementHandleTypeOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleTypeOptions, error) { + ehtopts := common.NewElementHandleTypeOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehtopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "delay": + ehtopts.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + ehtopts.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + ehtopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return ehtopts, nil +} + +// parseElementHandlePressOptions parses the element handle press options from a Sobek value. +// +//nolint:unparam +func parseElementHandlePressOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandlePressOptions, error) { + ehpopts := common.NewElementHandlePressOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehpopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "delay": + ehpopts.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + ehpopts.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + ehpopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return ehpopts, nil +} + +// parseElementHandleWaitForElementStateOptions parses the element handle wait for element state +// options from a Sobek value. +// +//nolint:unparam +func parseElementHandleWaitForElementStateOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleWaitForElementStateOptions, error) { + ehopts := common.NewElementHandleWaitForElementStateOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "timeout" { + ehopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return ehopts, nil +} + +// parseElementHandleScreenshotOptions parses the element handle screenshot options from a Sobek +// value. +// +//nolint:unparam +func parseElementHandleScreenshotOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleScreenshotOptions, error) { + ehopts := common.NewElementHandleScreenshotOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehopts, nil + } + formatSpecified := false + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "omitBackground": + ehopts.OmitBackground = obj.Get(k).ToBoolean() + case "path": + ehopts.Path = obj.Get(k).String() + case "quality": + ehopts.Quality = obj.Get(k).ToInteger() + case "type": + if f, ok := common.ImageIDFromString(obj.Get(k).String()); ok { + ehopts.Format = f + formatSpecified = true + } + case "timeout": + ehopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + + // Infer file format by path if format not explicitly specified (default is PNG) + if ehopts.Path != "" && !formatSpecified { + if strings.HasSuffix(ehopts.Path, ".jpg") || strings.HasSuffix(ehopts.Path, ".jpeg") { + ehopts.Format = common.ImageFormatJPEG + } + } + return ehopts, nil +} + +// parseElementHandleHoverOptions parses the element handle hover options from a Sobek value. +func parseElementHandleHoverOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleHoverOptions, error) { + ehhopts := common.NewElementHandleHoverOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehhopts, nil + } + err := parseElementHandleBasePointerOptions(&ehhopts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return ehhopts, err + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "modifiers" { + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return ehhopts, err + } + ehhopts.Modifiers = m + } + } + + return ehhopts, nil +} + +// parseElementHandleSetCheckedOptions parses the element handle set checked options from a Sobek +// value. +func parseElementHandleSetCheckedOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleSetCheckedOptions, error) { + ehscopts := common.NewElementHandleSetCheckedOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehscopts, nil + } + err := parseElementHandleBasePointerOptions(&ehscopts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return ehscopts, err + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "strict" { + ehscopts.Strict = obj.Get(k).ToBoolean() + } + } + + return ehscopts, nil +} + +// parseElementHandleDblclickOptions parses the element handle double click options from a Sobek +// value. +func parseElementHandleDblclickOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleDblclickOptions, error) { + ehdopts := common.NewElementHandleDblclickOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ehdopts, nil + } + + err := parseElementHandleBasePointerOptions(&ehdopts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return ehdopts, err + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "button": + ehdopts.Button = obj.Get(k).String() + case "delay": + ehdopts.Delay = obj.Get(k).ToInteger() + case "modifiers": + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return ehdopts, err + } + ehdopts.Modifiers = m + } + } + return ehdopts, nil +} + +// parseElementHandleTapOptions parses the element handle tap options from a Sobek value. +func parseElementHandleTapOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleTapOptions, error) { + ehtopts := common.NewElementHandleTapOptions(defaultTimeout) + + if k6common.IsNullish(opts) { + return ehtopts, nil + } + + err := parseElementHandleBasePointerOptions(&ehtopts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return ehtopts, err + } + + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "modifiers" { + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return ehtopts, err + } + ehtopts.Modifiers = m + } + } + return ehtopts, nil +} + +// parseElementHandleSetInputFilesOptions parses the element handle set input files options from a +// Sobek value. +func parseElementHandleSetInputFilesOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleSetInputFilesOptions, error) { + ehsifopts := common.NewElementHandleSetInputFilesOptions(defaultTimeout) + + if k6common.IsNullish(opts) { + return ehsifopts, nil + } + + err := parseElementHandleBaseOptions(&ehsifopts.ElementHandleBaseOptions, rt, opts) + if err != nil { + return ehsifopts, err + } + + return ehsifopts, nil +} + +// parseElementHandleClickOptions parses the element handle click options from a Sobek value. +func parseElementHandleClickOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.ElementHandleClickOptions, error) { + ehcopts := common.NewElementHandleClickOptions(defaultTimeout) + + if k6common.IsNullish(opts) { + return ehcopts, nil + } + + err := parseElementHandleBasePointerOptions(&ehcopts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return ehcopts, err + } + + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "button": + ehcopts.Button = obj.Get(k).String() + case "clickCount": + ehcopts.ClickCount = obj.Get(k).ToInteger() + case "delay": + ehcopts.Delay = obj.Get(k).ToInteger() + case "modifiers": + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return ehcopts, fmt.Errorf("parsing element handle click option modifiers: %w", err) + } + ehcopts.Modifiers = m + } + } + return ehcopts, nil +} + +// parseElementHandleBasePointerOptions parses the element handle base pointer options from a +// Sobek value into o. +func parseElementHandleBasePointerOptions( + o *common.ElementHandleBasePointerOptions, rt *sobek.Runtime, opts sobek.Value, +) error { + if k6common.IsNullish(opts) { + return nil + } + + if err := parseElementHandleBaseOptions(&o.ElementHandleBaseOptions, rt, opts); err != nil { + return err + } + + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "position": + var p map[string]float64 + if err := rt.ExportTo(obj.Get(k), &p); err != nil { + return err + } + o.Position = &common.Position{} + o.Position.X = p["x"] + o.Position.Y = p["y"] + case "trial": + o.Trial = obj.Get(k).ToBoolean() + } + } + return nil +} + +// parseElementHandleBaseOptions parses the element handle base options from a Sobek value into o. +func parseElementHandleBaseOptions(o *common.ElementHandleBaseOptions, rt *sobek.Runtime, opts sobek.Value) error { + if k6common.IsNullish(opts) { + return nil + } + + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "force": + o.Force = obj.Get(k).ToBoolean() + case "noWaitAfter": + o.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + + return nil } diff --git a/internal/js/modules/k6/browser/browser/element_handle_mapping_test.go b/internal/js/modules/k6/browser/browser/element_handle_mapping_test.go new file mode 100644 index 000000000..69695322a --- /dev/null +++ b/internal/js/modules/k6/browser/browser/element_handle_mapping_test.go @@ -0,0 +1,685 @@ +package browser + +import ( + "testing" + "time" + + "github.com/grafana/sobek" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" +) + +func TestParseElementHandleBaseOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleBaseOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleBaseOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({force: true, noWaitAfter: true, timeout: 5000})`, + want: &common.ElementHandleBaseOptions{ + Force: true, + NoWaitAfter: true, + Timeout: 5 * time.Second, + }, + }, + { + name: "partial_option", + input: `({timeout: 1000})`, + want: &common.ElementHandleBaseOptions{ + Force: false, + NoWaitAfter: false, + Timeout: 1 * time.Second, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts := common.NewElementHandleBaseOptions(defaultTimeout) + err = parseElementHandleBaseOptions(opts, vu.Runtime(), v) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleBasePointerOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleBasePointerOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleBasePointerOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1.5, y: 2.5}})`, + want: func() *common.ElementHandleBasePointerOptions { + o := common.NewElementHandleBasePointerOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1.5, Y: 2.5} + return o + }(), + }, + { + name: "invalid_position", + input: `({position: "invalid"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts := common.NewElementHandleBasePointerOptions(defaultTimeout) + err = parseElementHandleBasePointerOptions(opts, vu.Runtime(), v) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleClickOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleClickOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleClickOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({button: "right", clickCount: 3, delay: 100, modifiers: ["Shift", "Control"]})`, + want: func() *common.ElementHandleClickOptions { + o := common.NewElementHandleClickOptions(defaultTimeout) + o.Button, o.ClickCount, o.Delay = "right", 3, 100 + o.Modifiers = []string{"Shift", "Control"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}})`, + want: func() *common.ElementHandleClickOptions { + o := common.NewElementHandleClickOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "parsing element handle click option modifiers", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleClickOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleDblclickOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleDblclickOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleDblclickOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({button: "middle", delay: 50, modifiers: ["Alt"]})`, + want: func() *common.ElementHandleDblclickOptions { + o := common.NewElementHandleDblclickOptions(defaultTimeout) + o.Button, o.Delay, o.Modifiers = "middle", 50, []string{"Alt"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}})`, + want: func() *common.ElementHandleDblclickOptions { + o := common.NewElementHandleDblclickOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: 123})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleDblclickOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleHoverOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleHoverOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleHoverOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({modifiers: ["Shift"]})`, + want: func() *common.ElementHandleHoverOptions { + o := common.NewElementHandleHoverOptions(defaultTimeout) + o.Modifiers = []string{"Shift"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}})`, + want: func() *common.ElementHandleHoverOptions { + o := common.NewElementHandleHoverOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleHoverOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleTapOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleTapOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleTapOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({modifiers: ["Control"]})`, + want: func() *common.ElementHandleTapOptions { + o := common.NewElementHandleTapOptions(defaultTimeout) + o.Modifiers = []string{"Control"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}})`, + want: func() *common.ElementHandleTapOptions { + o := common.NewElementHandleTapOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleTapOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleSetCheckedOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleSetCheckedOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleSetCheckedOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({strict: true})`, + want: func() *common.ElementHandleSetCheckedOptions { + o := common.NewElementHandleSetCheckedOptions(defaultTimeout) + o.Strict = true + return o + }(), + }, + { + name: "inherited_base_pointer_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}})`, + want: func() *common.ElementHandleSetCheckedOptions { + o := common.NewElementHandleSetCheckedOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + return o + }(), + }, + { + name: "invalid_position", + input: `({position: "invalid"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleSetCheckedOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleSetInputFilesOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleSetInputFilesOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleSetInputFilesOptions(defaultTimeout), + }, + { + name: "inherited_base_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000})`, + want: func() *common.ElementHandleSetInputFilesOptions { + o := common.NewElementHandleSetInputFilesOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleSetInputFilesOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandlePressOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandlePressOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandlePressOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({delay: 100, noWaitAfter: true, timeout: 2000})`, + want: &common.ElementHandlePressOptions{ + Delay: 100, + NoWaitAfter: true, + Timeout: 2 * time.Second, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandlePressOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleTypeOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleTypeOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleTypeOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({delay: 50, noWaitAfter: true, timeout: 3000})`, + want: &common.ElementHandleTypeOptions{ + Delay: 50, + NoWaitAfter: true, + Timeout: 3 * time.Second, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleTypeOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleWaitForElementStateOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleWaitForElementStateOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleWaitForElementStateOptions(defaultTimeout), + }, + { + name: "timeout_option", + input: `({timeout: 1500})`, + want: &common.ElementHandleWaitForElementStateOptions{Timeout: 1500 * time.Millisecond}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleWaitForElementStateOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseElementHandleScreenshotOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.ElementHandleScreenshotOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewElementHandleScreenshotOptions(defaultTimeout), + }, + { + name: "explicit_type_overrides_path_inference", + input: `({omitBackground: true, path: "img.png", quality: 80, type: "jpeg", timeout: 2000})`, + want: &common.ElementHandleScreenshotOptions{ + OmitBackground: true, + Path: "img.png", + Quality: 80, + Format: common.ImageFormatJPEG, + Timeout: 2 * time.Second, + }, + }, + { + name: "format_inferred_by_jpg_path", + input: `({path: "photo.jpg"})`, + want: func() *common.ElementHandleScreenshotOptions { + o := common.NewElementHandleScreenshotOptions(defaultTimeout) + o.Path, o.Format = "photo.jpg", common.ImageFormatJPEG + return o + }(), + }, + { + name: "format_inferred_by_jpeg_path", + input: `({path: "photo.jpeg"})`, + want: func() *common.ElementHandleScreenshotOptions { + o := common.NewElementHandleScreenshotOptions(defaultTimeout) + o.Path, o.Format = "photo.jpeg", common.ImageFormatJPEG + return o + }(), + }, + { + name: "png_path_stays_default_format", + input: `({path: "photo.png"})`, + want: func() *common.ElementHandleScreenshotOptions { + o := common.NewElementHandleScreenshotOptions(defaultTimeout) + o.Path = "photo.png" + return o + }(), + }, + { + name: "unknown_type_ignored", + input: `({type: "gif"})`, + want: common.NewElementHandleScreenshotOptions(defaultTimeout), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseElementHandleScreenshotOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestMapElementHandleNil(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + value := vu.Runtime().ToValue(mapElementHandle(moduleVU{VU: vu}, nil)) + assert.True(t, sobek.IsNull(value)) +} diff --git a/internal/js/modules/k6/browser/browser/frame_locator_mapping.go b/internal/js/modules/k6/browser/browser/frame_locator_mapping.go index af6cd224c..dd86aea8d 100644 --- a/internal/js/modules/k6/browser/browser/frame_locator_mapping.go +++ b/internal/js/modules/k6/browser/browser/frame_locator_mapping.go @@ -11,8 +11,8 @@ import ( // mapFrameLocator API to the JS module. func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { rt := vu.Runtime() - return mapping{ - "getByAltText": func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { + return finishMapping(mapping{ + "getByAltText": passiveCall(func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(alt) { return nil, errors.New("missing required argument 'altText'") } @@ -20,8 +20,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByAltText(palt, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByLabel": func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByLabel": passiveCall(func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(label) { return nil, errors.New("missing required argument 'label'") } @@ -29,8 +29,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByLabel(plabel, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByPlaceholder": func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByPlaceholder": passiveCall(func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(placeholder) { return nil, errors.New("missing required argument 'placeholder'") } @@ -38,8 +38,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByPlaceholder(pplaceholder, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByRole": func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByRole": passiveCall(func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(role) { return nil, errors.New("missing required argument 'role'") } @@ -47,8 +47,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByRole(role.String(), popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTestId": func(testID sobek.Value) (*sobek.Object, error) { + }), + "getByTestId": passiveCall(func(testID sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(testID) { return nil, errors.New("missing required argument 'testId'") } @@ -56,8 +56,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByTestID(ptestID)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByText": func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByText": passiveCall(func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(text) { return nil, errors.New("missing required argument 'text'") } @@ -65,8 +65,8 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByText(ptext, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTitle": func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByTitle": passiveCall(func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(title) { return nil, errors.New("missing required argument 'title'") } @@ -74,13 +74,13 @@ func mapFrameLocator(vu moduleVU, fl *common.FrameLocator) mapping { ml := mapLocator(vu, fl.GetByTitle(ptitle, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "locator": func(selector string, opts sobek.Value) mapping { + }), + "locator": passiveCall(func(selector string, opts sobek.Value) mapping { return mapLocator(vu, fl.Locator(selector, parseLocatorOptions(rt, opts))) - }, - "frameLocator": func(selector string) *sobek.Object { + }), + "frameLocator": passiveCall(func(selector string) *sobek.Object { mfl := mapFrameLocator(vu, fl.FrameLocator(selector)) return rt.ToValue(mfl).ToObject(rt) - }, - } + }), + }) } diff --git a/internal/js/modules/k6/browser/browser/frame_mapping.go b/internal/js/modules/k6/browser/browser/frame_mapping.go index 8156df1cc..307c57dbc 100644 --- a/internal/js/modules/k6/browser/browser/frame_mapping.go +++ b/internal/js/modules/k6/browser/browser/frame_mapping.go @@ -3,6 +3,7 @@ package browser import ( "errors" "fmt" + "time" "github.com/grafana/sobek" @@ -16,25 +17,25 @@ import ( func mapFrame(vu moduleVU, f *common.Frame) mapping { rt := vu.Runtime() maps := mapping{ - "check": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + "check": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameCheckOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing new frame check options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Check(selector, popts) //nolint:wrapcheck }), nil - }, - "childFrames": func() []mapping { + }), + "childFrames": passiveCall(func() []mapping { cfs := f.ChildFrames() mcfs := make([]mapping, 0, len(cfs)) for _, fr := range cfs { mcfs = append(mcfs, mapFrame(vu, fr)) } return mcfs - }, - "click": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, f.Timeout()) + }), + "click": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameClickOptions(rt, opts, f.Timeout()) if err != nil { return nil, err } @@ -43,32 +44,32 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { err := f.Click(selector, popts) return nil, err //nolint:wrapcheck }), nil - }, - "content": func() *sobek.Promise { + }), + "content": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return f.Content() //nolint:wrapcheck }) - }, - "dblclick": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDblClickOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dblclick": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameDblclickOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Dblclick(selector, popts) //nolint:wrapcheck }), nil - }, - "dispatchEvent": func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDispatchEventOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dispatchEvent": networkCall(func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameDispatchEventOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame dispatch event options: %w", err) } earg := exportArg(eventInit) return promise(vu, func() (any, error) { return nil, f.DispatchEvent(selector, typ, earg, popts) //nolint:wrapcheck }), nil - }, - "evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluate": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluate requires a page function") } @@ -77,8 +78,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return promise(vu, func() (any, error) { return f.Evaluate(funcString, gopts...) }), nil - }, - "evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluateHandle": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluateHandle requires a page function") } @@ -91,26 +92,26 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return mapJSHandle(vu, jsh), nil }), nil - }, - "fill": func(selector, value string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameFillOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "fill": networkCall(func(selector, value string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameFillOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Fill(selector, value, popts) //nolint:wrapcheck }), nil - }, - "focus": func(selector string, opts sobek.Value) (*sobek.Promise, error) { + }), + "focus": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { popts := common.NewFrameBaseOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing focus options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Focus(selector, popts) //nolint:wrapcheck }), nil - }, - "frameElement": func() *sobek.Promise { + }), + "frameElement": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { fe, err := f.FrameElement() if err != nil { @@ -118,10 +119,10 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return mapElementHandle(vu, fe), nil }) - }, - "getAttribute": func(selector, name string, opts sobek.Value) (*sobek.Promise, error) { + }), + "getAttribute": passiveCall(func(selector, name string, opts sobek.Value) (*sobek.Promise, error) { popts := common.NewFrameBaseOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing getAttribute options: %w", err) } return promise(vu, func() (any, error) { @@ -134,8 +135,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return s, nil }), nil - }, - "getByAltText": func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByAltText": passiveCall(func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(alt) { return nil, errors.New("missing required argument 'altText'") } @@ -143,8 +144,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByAltText(palt, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByLabel": func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByLabel": passiveCall(func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(label) { return nil, errors.New("missing required argument 'label'") } @@ -152,8 +153,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByLabel(plabel, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByPlaceholder": func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByPlaceholder": passiveCall(func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(placeholder) { return nil, errors.New("missing required argument 'placeholder'") } @@ -161,8 +162,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByPlaceholder(pplaceholder, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByRole": func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByRole": passiveCall(func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(role) { return nil, errors.New("missing required argument 'role'") } @@ -170,8 +171,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByRole(role.String(), popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTestId": func(testID sobek.Value) (*sobek.Object, error) { + }), + "getByTestId": passiveCall(func(testID sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(testID) { return nil, errors.New("missing required argument 'testId'") } @@ -179,8 +180,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByTestID(ptestID)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByText": func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByText": passiveCall(func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(text) { return nil, errors.New("missing required argument 'text'") } @@ -188,8 +189,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByText(ptext, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTitle": func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByTitle": passiveCall(func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(title) { return nil, errors.New("missing required argument 'title'") } @@ -197,13 +198,10 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { ml := mapLocator(vu, f.GetByTitle(ptitle, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "goto": func(url string, opts sobek.Value) (*sobek.Promise, error) { - gopts := common.NewFrameGotoOptions( - f.Referrer(), - f.NavigationTimeout(), - ) - if err := gopts.Parse(vu.Context(), opts); err != nil { + }), + "goto": networkCall(func(url string, opts sobek.Value) (*sobek.Promise, error) { + gopts, err := parseFrameGotoOptions(rt, opts, f.Referrer(), f.NavigationTimeout()) + if err != nil { return nil, fmt.Errorf("parsing frame navigation options to %q: %w", url, err) } return promise(vu, func() (any, error) { @@ -214,124 +212,124 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return mapResponse(vu, resp), nil }), nil - }, - "hover": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameHoverOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "hover": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameHoverOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Hover(selector, popts) //nolint:wrapcheck }), nil - }, - "innerHTML": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInnerHTMLOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "innerHTML": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInnerHTMLOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner HTML options: %w", err) } return promise(vu, func() (any, error) { return f.InnerHTML(selector, popts) //nolint:wrapcheck }), nil - }, - "innerText": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInnerTextOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "innerText": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInnerTextOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner text options: %w", err) } return promise(vu, func() (any, error) { return f.InnerText(selector, popts) //nolint:wrapcheck }), nil - }, - "inputValue": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInputValueOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "inputValue": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInputValueOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing input value options: %w", err) } return promise(vu, func() (any, error) { return f.InputValue(selector, popts) //nolint:wrapcheck }), nil - }, - "isChecked": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsCheckedOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isChecked": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsCheckedOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing isChecked options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsChecked(selector, popts) //nolint:wrapcheck }), nil - }, - "isDetached": f.IsDetached, - "isDisabled": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsDisabledOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isDetached": passiveCall(f.IsDetached), + "isDisabled": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsDisabledOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing isDisabled options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsDisabled(selector, popts) //nolint:wrapcheck }), nil - }, - "isEditable": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsEditableOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isEditable": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsEditableOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parse isEditable options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsEditable(selector, popts) //nolint:wrapcheck }), nil - }, - "isEnabled": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsEnabledOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isEnabled": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsEnabledOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parse isEnabled options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsEnabled(selector, popts) //nolint:wrapcheck }), nil - }, - "isHidden": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsHiddenOptions() - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isHidden": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsHiddenOptions(rt, opts) + if err != nil { return nil, fmt.Errorf("parse isHidden options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsHidden(selector, popts) //nolint:wrapcheck }), nil - }, - "isVisible": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsVisibleOptions() - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isVisible": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsVisibleOptions(rt, opts) + if err != nil { return nil, fmt.Errorf("parse isVisible options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return f.IsVisible(selector, popts) //nolint:wrapcheck }), nil - }, - "locator": func(selector string, opts sobek.Value) mapping { + }), + "locator": passiveCall(func(selector string, opts sobek.Value) mapping { return mapLocator(vu, f.Locator(selector, parseLocatorOptions(rt, opts))) - }, - "frameLocator": func(selector string) *sobek.Object { + }), + "frameLocator": passiveCall(func(selector string) *sobek.Object { mfl := mapFrameLocator(vu, f.FrameLocator(selector)) return rt.ToValue(mfl).ToObject(rt) - }, - "name": f.Name, - "page": func() mapping { + }), + "name": passiveCall(f.Name), + "page": passiveCall(func() mapping { return mapPage(vu, f.Page()) - }, - "parentFrame": func() mapping { + }), + "parentFrame": passiveCall(func() mapping { return mapFrame(vu, f.ParentFrame()) - }, - "press": func(selector, key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFramePressOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "press": networkCall(func(selector, key string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFramePressOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parse press options of selector %q on key %q: %w", selector, key, err) } return promise(vu, func() (any, error) { return nil, f.Press(selector, key, popts) //nolint:wrapcheck }), nil - }, - "selectOption": func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSelectOptionOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "selectOption": networkCall(func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSelectOptionOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } @@ -343,29 +341,29 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return promise(vu, func() (any, error) { return f.SelectOption(selector, convValues, popts) //nolint:wrapcheck }), nil - }, - "setChecked": func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setChecked": networkCall(func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameCheckOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame set check options: %w", err) } return promise(vu, func() (any, error) { return nil, f.SetChecked(selector, checked, popts) //nolint:wrapcheck }), nil - }, - "setContent": func(html string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetContentOptions(f.Page().NavigationTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setContent": networkCall(func(html string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSetContentOptions(rt, opts, f.Page().NavigationTimeout()) + if err != nil { return nil, fmt.Errorf("parsing setContent options: %w", err) } return promise(vu, func() (any, error) { return nil, f.SetContent(html, popts) //nolint:wrapcheck }), nil - }, - "setInputFiles": func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetInputFilesOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setInputFiles": networkCall(func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSetInputFilesOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } @@ -377,19 +375,19 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return promise(vu, func() (any, error) { return nil, f.SetInputFiles(selector, pfiles, popts) //nolint:wrapcheck }), nil - }, - "tap": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTapOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "tap": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTapOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame tap options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Tap(selector, popts) //nolint:wrapcheck }), nil - }, - "textContent": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTextContentOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "textContent": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTextContentOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing text content options: %w", err) } @@ -403,36 +401,36 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return s, nil }), nil - }, - "title": func() *sobek.Promise { + }), + "title": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return f.Title() }) - }, - "type": func(selector, text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTypeOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "type": networkCall(func(selector, text string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTypeOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return promise(vu, func() (any, error) { return nil, f.Type(selector, text, popts) //nolint:wrapcheck }), nil - }, - "uncheck": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameUncheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "uncheck": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameUncheckOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame uncheck options %q: %w", selector, err) } return promise(vu, func() (any, error) { return nil, f.Uncheck(selector, popts) //nolint:wrapcheck }), nil - }, - "url": f.URL, - "waitForFunction": func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) { + }), + "url": passiveCall(f.URL), + "waitForFunction": networkCall(func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) { js, popts, pargs, err := parseWaitForFunctionArgs( - vu.Context(), f.Timeout(), pageFunc, opts, args..., + rt, f.Timeout(), pageFunc, opts, args..., ) if err != nil { return nil, fmt.Errorf("frame waitForFunction: %w", err) @@ -441,23 +439,23 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return promise(vu, func() (result any, reason error) { return f.WaitForFunction(js, popts, pargs...) //nolint:wrapcheck }), nil - }, - "waitForLoadState": func(state string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameWaitForLoadStateOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForLoadState": passiveCall(func(state string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameWaitForLoadStateOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing waitForLoadState %q options: %w", state, err) } return promise(vu, func() (any, error) { return nil, f.WaitForLoadState(state, popts) //nolint:wrapcheck }), nil - }, - "waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) { - return mapWaitForNavigation(vu, f, opts) - }, - "waitForSelector": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameWaitForSelectorOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForNavigation": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + return mapWaitForNavigation(rt, vu, f, opts) + }), + "waitForSelector": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameWaitForSelectorOptions(rt, opts, f.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing wait for selector %q options: %w", selector, err) } @@ -468,18 +466,18 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return mapElementHandle(vu, eh), nil }), nil - }, - "waitForTimeout": func(timeout int64) *sobek.Promise { + }), + "waitForTimeout": passiveCall(func(timeout int64) *sobek.Promise { return promise(vu, func() (any, error) { f.WaitForTimeout(timeout) return nil, nil }) - }, - "waitForURL": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - return mapWaitForURL(vu, f, url, opts) - }, + }), + "waitForURL": passiveCall(func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + return mapWaitForURL(rt, vu, f, url, opts) + }), } - maps["$"] = func(selector string) *sobek.Promise { + maps["$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { eh, err := f.Query(selector, common.StrictModeOff) if err != nil { @@ -495,8 +493,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return ehm, nil }) - } - maps["$$"] = func(selector string) *sobek.Promise { + }) + maps["$$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { ehs, err := f.QueryAll(selector) if err != nil { @@ -509,7 +507,499 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { } return mehs, nil }) + }) + + return withPageNetworkCalls(vu, f.Page(), maps) +} + +// parseFrameCheckOptions parses the frame check options from a Sobek value. +func parseFrameCheckOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameCheckOptions, error) { + copts := common.NewFrameCheckOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + err := parseElementHandleBasePointerOptions(&copts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return copts, err + } + + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameDblclickOptions parses the frame dblclick options from a Sobek value. +func parseFrameDblclickOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameDblclickOptions, error) { + copts := common.NewFrameDblClickOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + dcopts, err := parseElementHandleDblclickOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandleDblclickOptions = *dcopts + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameSetInputFilesOptions parses the frame setInputFiles options from a Sobek value. +func parseFrameSetInputFilesOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameSetInputFilesOptions, error) { + copts := common.NewFrameSetInputFilesOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + sopts, err := parseElementHandleSetInputFilesOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandleSetInputFilesOptions = *sopts + + return copts, nil +} + +// parseFrameTypeOptions parses the frame type options from a Sobek value. +func parseFrameTypeOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameTypeOptions, error) { + copts := common.NewFrameTypeOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + ehopts, err := parseElementHandleTypeOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandleTypeOptions = *ehopts + + return copts, nil +} + +// parseFramePressOptions parses the frame press options from a Sobek value. +func parseFramePressOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FramePressOptions, error) { + copts := common.NewFramePressOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + ehopts, err := parseElementHandlePressOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandlePressOptions = *ehopts + + return copts, nil +} + +// parseFrameFillOptions parses the frame fill options from a Sobek value. +func parseFrameFillOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameFillOptions, error) { + copts := common.NewFrameFillOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + err := parseElementHandleBaseOptions(&copts.ElementHandleBaseOptions, rt, opts) + if err != nil { + return copts, err + } + + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameUncheckOptions parses the frame uncheck options from a Sobek value. +func parseFrameUncheckOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameUncheckOptions, error) { + copts := common.NewFrameUncheckOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + err := parseElementHandleBasePointerOptions(&copts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return copts, err + } + + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameTapOptions parses the frame tap options from a Sobek value. +func parseFrameTapOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameTapOptions, error) { + copts := common.NewFrameTapOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + err := parseElementHandleBasePointerOptions(&copts.ElementHandleBasePointerOptions, rt, opts) + if err != nil { + return copts, err + } + + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "modifiers": + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return copts, err + } + copts.Modifiers = m + case "strict": + copts.Strict = obj.Get(k).ToBoolean() + } + } + + return copts, nil +} + +// parseFrameSelectOptionOptions parses the frame selectOption options from a Sobek value. +func parseFrameSelectOptionOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameSelectOptionOptions, error) { + copts := common.NewFrameSelectOptionOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + err := parseElementHandleBaseOptions(&copts.ElementHandleBaseOptions, rt, opts) + if err != nil { + return copts, err + } + + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameHoverOptions parses the frame hover options from a Sobek value. +func parseFrameHoverOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameHoverOptions, error) { + copts := common.NewFrameHoverOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return copts, nil + } + + ehopts, err := parseElementHandleHoverOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandleHoverOptions = *ehopts + copts.Strict = parseStrict(rt, opts) + + return copts, nil +} + +// parseFrameGotoOptions parses the frame goto options from a Sobek value. +func parseFrameGotoOptions( + rt *sobek.Runtime, opts sobek.Value, defaultReferrer string, defaultTimeout time.Duration, +) (*common.FrameGotoOptions, error) { + gopts := common.NewFrameGotoOptions(defaultReferrer, defaultTimeout) + if k6common.IsNullish(opts) { + return gopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "referer": + gopts.Referer = obj.Get(k).String() + case "timeout": + gopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "waitUntil": + lifeCycle := obj.Get(k).String() + if err := gopts.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { + return gopts, fmt.Errorf("parsing goto options: %w", err) + } + } + } + return gopts, nil +} + +// parseFrameSetContentOptions parses the frame setContent options from a Sobek value. +func parseFrameSetContentOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameSetContentOptions, error) { + scopts := common.NewFrameSetContentOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return scopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "timeout": + scopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "waitUntil": + lifeCycle := obj.Get(k).String() + if err := scopts.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { + return scopts, fmt.Errorf("parsing setContent options: %w", err) + } + } + } + return scopts, nil +} + +// parseFrameWaitForLoadStateOptions parses the frame waitForLoadState options from a Sobek value. +// +//nolint:unparam +func parseFrameWaitForLoadStateOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameWaitForLoadStateOptions, error) { + wlsopts := common.NewFrameWaitForLoadStateOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return wlsopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "timeout" { + wlsopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return wlsopts, nil +} + +// parseFrameInnerTextOptions parses the frame innerText options from a Sobek value. +func parseFrameInnerTextOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameInnerTextOptions, error) { + itopts := common.NewFrameInnerTextOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return itopts, nil + } + if err := parseFrameBaseOptions(&itopts.FrameBaseOptions, rt, opts); err != nil { + return itopts, err + } + return itopts, nil +} + +// parseFrameInnerHTMLOptions parses the frame innerHTML options from a Sobek value. +func parseFrameInnerHTMLOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameInnerHTMLOptions, error) { + ihopts := common.NewFrameInnerHTMLOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ihopts, nil + } + if err := parseFrameBaseOptions(&ihopts.FrameBaseOptions, rt, opts); err != nil { + return ihopts, err + } + return ihopts, nil +} + +// parseFrameIsCheckedOptions parses the frame isChecked options from a Sobek value. +func parseFrameIsCheckedOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameIsCheckedOptions, error) { + icopts := common.NewFrameIsCheckedOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return icopts, nil + } + if err := parseFrameBaseOptions(&icopts.FrameBaseOptions, rt, opts); err != nil { + return icopts, err + } + return icopts, nil +} + +// parseFrameIsDisabledOptions parses the frame isDisabled options from a Sobek value. +func parseFrameIsDisabledOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameIsDisabledOptions, error) { + idopts := common.NewFrameIsDisabledOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return idopts, nil + } + if err := parseFrameBaseOptions(&idopts.FrameBaseOptions, rt, opts); err != nil { + return idopts, err + } + return idopts, nil +} + +// parseFrameInputValueOptions parses the frame inputValue options from a Sobek value. +func parseFrameInputValueOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameInputValueOptions, error) { + ivopts := common.NewFrameInputValueOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ivopts, nil + } + if err := parseFrameBaseOptions(&ivopts.FrameBaseOptions, rt, opts); err != nil { + return ivopts, err + } + return ivopts, nil +} + +// parseFrameIsEditableOptions parses the frame isEditable options from a Sobek value. +func parseFrameIsEditableOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameIsEditableOptions, error) { + ieopts := common.NewFrameIsEditableOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ieopts, nil + } + if err := parseFrameBaseOptions(&ieopts.FrameBaseOptions, rt, opts); err != nil { + return ieopts, err + } + return ieopts, nil +} + +// parseFrameIsEnabledOptions parses the frame isEnabled options from a Sobek value. +func parseFrameIsEnabledOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameIsEnabledOptions, error) { + ieopts := common.NewFrameIsEnabledOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return ieopts, nil + } + if err := parseFrameBaseOptions(&ieopts.FrameBaseOptions, rt, opts); err != nil { + return ieopts, err + } + return ieopts, nil +} + +// parseFrameTextContentOptions parses the frame textContent options from a Sobek value. +func parseFrameTextContentOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameTextContentOptions, error) { + tcopts := common.NewFrameTextContentOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return tcopts, nil + } + if err := parseFrameBaseOptions(&tcopts.FrameBaseOptions, rt, opts); err != nil { + return tcopts, err + } + return tcopts, nil +} + +// parseFrameDispatchEventOptions parses the frame dispatchEvent options from a Sobek value. +func parseFrameDispatchEventOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameDispatchEventOptions, error) { + deopts := common.NewFrameDispatchEventOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return deopts, nil + } + if err := parseFrameBaseOptions(&deopts.FrameBaseOptions, rt, opts); err != nil { + return deopts, err + } + return deopts, nil +} + +// parseFrameWaitForSelectorOptions parses the frame waitForSelector options from a Sobek value. +func parseFrameWaitForSelectorOptions( + rt *sobek.Runtime, opts sobek.Value, + defaultTimeout time.Duration, +) (*common.FrameWaitForSelectorOptions, error) { + wsopts := common.NewFrameWaitForSelectorOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return wsopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "state": + state := obj.Get(k).String() + if s, ok := common.DOMElementStateIDFromString(state); ok { + wsopts.State = s + } else { + return wsopts, fmt.Errorf("%q is not a valid DOM state", state) + } + case "strict": + wsopts.Strict = obj.Get(k).ToBoolean() + case "timeout": + wsopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return wsopts, nil +} + +// parseFrameIsHiddenOptions parses the frame isHidden options from a Sobek value. +// +//nolint:unparam +func parseFrameIsHiddenOptions( + rt *sobek.Runtime, opts sobek.Value, +) (*common.FrameIsHiddenOptions, error) { + tcopts := common.NewFrameIsHiddenOptions() + tcopts.Strict = parseStrict(rt, opts) + return tcopts, nil +} + +// parseFrameIsVisibleOptions parses the frame isVisible options from a Sobek value. +// +//nolint:unparam +func parseFrameIsVisibleOptions( + rt *sobek.Runtime, opts sobek.Value, +) (*common.FrameIsVisibleOptions, error) { + tcopts := common.NewFrameIsVisibleOptions() + tcopts.Strict = parseStrict(rt, opts) + return tcopts, nil +} + +// parseFrameBaseOptions parses the frame base options from a Sobek value into o. +// +//nolint:unparam +func parseFrameBaseOptions( + o *common.FrameBaseOptions, rt *sobek.Runtime, opts sobek.Value, +) error { + if k6common.IsNullish(opts) { + return nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "strict": + o.Strict = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + return nil +} + +func parseStrict(rt *sobek.Runtime, opts sobek.Value) bool { + var strict bool + + if !k6common.IsNullish(opts) { + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "strict" { + strict = obj.Get(k).ToBoolean() + } + } } - return maps + return strict } diff --git a/internal/js/modules/k6/browser/browser/frame_mapping_test.go b/internal/js/modules/k6/browser/browser/frame_mapping_test.go new file mode 100644 index 000000000..d2986a8df --- /dev/null +++ b/internal/js/modules/k6/browser/browser/frame_mapping_test.go @@ -0,0 +1,1498 @@ +package browser + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" +) + +func TestParseFrameBaseOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameBaseOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameBaseOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: &common.FrameBaseOptions{Strict: true, Timeout: 5 * time.Second}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts := common.NewFrameBaseOptions(defaultTimeout) + err = parseFrameBaseOptions(opts, vu.Runtime(), v) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameCheckOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameCheckOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameCheckOptions(defaultTimeout), + }, + { + name: "inherited_and_own_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, strict: true})`, + want: func() *common.FrameCheckOptions { + o := common.NewFrameCheckOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Strict = true + return o + }(), + }, + { + name: "invalid_position", + input: `({position: "invalid"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameCheckOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameClickOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameClickOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameClickOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({button: "right", clickCount: 2, delay: 50, modifiers: ["Shift"]})`, + want: func() *common.FrameClickOptions { + o := common.NewFrameClickOptions(defaultTimeout) + o.Button, o.ClickCount, o.Delay, o.Modifiers = "right", 2, 50, []string{"Shift"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields_and_strict", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, strict: true})`, + want: func() *common.FrameClickOptions { + o := common.NewFrameClickOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Strict = true + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "parsing element handle click option modifiers", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameClickOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameDblclickOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameDblclickOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameDblClickOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({button: "middle", delay: 50, modifiers: ["Alt"]})`, + want: func() *common.FrameDblclickOptions { + o := common.NewFrameDblClickOptions(defaultTimeout) + o.Button, o.Delay, o.Modifiers = "middle", 50, []string{"Alt"} + return o + }(), + }, + { + name: "inherited_base_pointer_fields_and_strict", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, strict: true})`, + want: func() *common.FrameDblclickOptions { + o := common.NewFrameDblClickOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Strict = true + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: 123})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameDblclickOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameFillOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameFillOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameFillOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({force: true, noWaitAfter: true, timeout: 5000, strict: true})`, + want: func() *common.FrameFillOptions { + o := common.NewFrameFillOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Strict = true + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameFillOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameGotoOptions(t *testing.T) { + t.Parallel() + + const ( + defaultReferrer = "https://referrer.example.com/" + defaultTimeout = 30 * time.Second + ) + + tests := []struct { + name string + input string + want *common.FrameGotoOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameGotoOptions(defaultReferrer, defaultTimeout), + }, + { + name: "all_options", + input: `({referer: "https://example.com/", timeout: 1000, waitUntil: "networkidle"})`, + want: &common.FrameGotoOptions{ + Referer: "https://example.com/", + Timeout: time.Second, + WaitUntil: common.LifecycleEventNetworkIdle, + }, + }, + { + name: "invalid_waitUntil", + input: `({waitUntil: "none"})`, + wantErr: "parsing goto options", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameGotoOptions(vu.Runtime(), v, defaultReferrer, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameHoverOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameHoverOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameHoverOptions(defaultTimeout), + }, + { + name: "inherited_and_own_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, modifiers: ["Control"], strict: true})`, + want: func() *common.FrameHoverOptions { + o := common.NewFrameHoverOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Modifiers = []string{"Control"} + o.Strict = true + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameHoverOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameInnerHTMLOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameInnerHTMLOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameInnerHTMLOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameInnerHTMLOptions { + o := common.NewFrameInnerHTMLOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameInnerHTMLOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameInnerTextOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameInnerTextOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameInnerTextOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameInnerTextOptions { + o := common.NewFrameInnerTextOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameInnerTextOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameInputValueOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameInputValueOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameInputValueOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameInputValueOptions { + o := common.NewFrameInputValueOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameInputValueOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsCheckedOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameIsCheckedOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsCheckedOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameIsCheckedOptions { + o := common.NewFrameIsCheckedOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsCheckedOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsDisabledOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameIsDisabledOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsDisabledOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameIsDisabledOptions { + o := common.NewFrameIsDisabledOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsDisabledOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsEditableOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameIsEditableOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsEditableOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameIsEditableOptions { + o := common.NewFrameIsEditableOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsEditableOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsEnabledOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameIsEnabledOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsEnabledOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameIsEnabledOptions { + o := common.NewFrameIsEnabledOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsEnabledOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsInViewportOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameIsInViewportOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsInViewportOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000, ratio: 0.5})`, + want: func() *common.FrameIsInViewportOptions { + o := common.NewFrameIsInViewportOptions(defaultTimeout) + o.Strict, o.Timeout, o.Ratio = true, 5*time.Second, 0.5 + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsInViewportOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsHiddenOptions(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + want *common.FrameIsHiddenOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsHiddenOptions(), + }, + { + name: "strict", + input: `({strict: true})`, + want: &common.FrameIsHiddenOptions{Strict: true}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsHiddenOptions(vu.Runtime(), v) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameIsVisibleOptions(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + want *common.FrameIsVisibleOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameIsVisibleOptions(), + }, + { + name: "strict", + input: `({strict: true})`, + want: &common.FrameIsVisibleOptions{Strict: true}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameIsVisibleOptions(vu.Runtime(), v) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFramePressOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FramePressOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFramePressOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({delay: 50, noWaitAfter: true, timeout: 5000})`, + want: func() *common.FramePressOptions { + o := common.NewFramePressOptions(defaultTimeout) + o.Delay, o.NoWaitAfter, o.Timeout = 50, true, 5*time.Second + return o + }(), + }, + { + // FramePressOptions never parsed `strict` even before this migration + // (no Parse override existed; it relied on promotion from + // ElementHandlePressOptions, which has no Strict field at all). + // This is intentionally preserved dormant behaviour, not a bug. + name: "strict_field_is_dormant", + input: `({strict: true})`, + want: common.NewFramePressOptions(defaultTimeout), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFramePressOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameSelectOptionOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameSelectOptionOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameSelectOptionOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({force: true, noWaitAfter: true, timeout: 5000, strict: true})`, + want: func() *common.FrameSelectOptionOptions { + o := common.NewFrameSelectOptionOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Strict = true + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameSelectOptionOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameSetContentOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameSetContentOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameSetContentOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({timeout: 1000, waitUntil: "networkidle"})`, + want: &common.FrameSetContentOptions{ + Timeout: time.Second, + WaitUntil: common.LifecycleEventNetworkIdle, + }, + }, + { + name: "invalid_waitUntil", + input: `({waitUntil: "none"})`, + wantErr: "parsing setContent options", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameSetContentOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameSetInputFilesOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameSetInputFilesOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameSetInputFilesOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000})`, + want: func() *common.FrameSetInputFilesOptions { + o := common.NewFrameSetInputFilesOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + return o + }(), + }, + { + // FrameSetInputFilesOptions never parsed `strict` even before this + // migration (pure delegation to ElementHandleSetInputFilesOptions.Parse, + // which has no Strict field). Intentionally preserved dormant behaviour. + name: "strict_field_is_dormant", + input: `({strict: true})`, + want: common.NewFrameSetInputFilesOptions(defaultTimeout), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameSetInputFilesOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameTapOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameTapOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameTapOptions(defaultTimeout), + }, + { + name: "inherited_and_own_fields", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, modifiers: ["Meta"], strict: true})`, + want: func() *common.FrameTapOptions { + o := common.NewFrameTapOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Modifiers = []string{"Meta"} + o.Strict = true + return o + }(), + }, + { + name: "invalid_modifiers", + input: `({modifiers: "not-an-array"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameTapOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameTextContentOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameTextContentOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameTextContentOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameTextContentOptions { + o := common.NewFrameTextContentOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameTextContentOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameTypeOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameTypeOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameTypeOptions(defaultTimeout), + }, + { + name: "own_fields", + input: `({delay: 50, noWaitAfter: true, timeout: 5000})`, + want: func() *common.FrameTypeOptions { + o := common.NewFrameTypeOptions(defaultTimeout) + o.Delay, o.NoWaitAfter, o.Timeout = 50, true, 5*time.Second + return o + }(), + }, + { + // FrameTypeOptions never parsed `strict` even before this migration + // (no Parse override existed; it relied on promotion from + // ElementHandleTypeOptions, which has no Strict field at all). + // This is intentionally preserved dormant behaviour, not a bug. + name: "strict_field_is_dormant", + input: `({strict: true})`, + want: common.NewFrameTypeOptions(defaultTimeout), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameTypeOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameUncheckOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameUncheckOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameUncheckOptions(defaultTimeout), + }, + { + name: "inherited_base_pointer_fields_and_strict", + input: `({force: true, noWaitAfter: true, timeout: 5000, trial: true, position: {x: 1, y: 2}, strict: true})`, + want: func() *common.FrameUncheckOptions { + o := common.NewFrameUncheckOptions(defaultTimeout) + o.Force, o.NoWaitAfter, o.Timeout = true, true, 5*time.Second + o.Trial, o.Position = true, &common.Position{X: 1, Y: 2} + o.Strict = true + return o + }(), + }, + { + name: "invalid_position", + input: `({position: "invalid"})`, + wantErr: "could not convert", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameUncheckOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameWaitForLoadStateOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameWaitForLoadStateOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameWaitForLoadStateOptions(defaultTimeout), + }, + { + name: "timeout", + input: `({timeout: 5000})`, + want: &common.FrameWaitForLoadStateOptions{Timeout: 5 * time.Second}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameWaitForLoadStateOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameWaitForSelectorOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameWaitForSelectorOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameWaitForSelectorOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({state: "hidden", strict: true, timeout: 5000})`, + want: &common.FrameWaitForSelectorOptions{ + State: common.DOMElementStateHidden, + Strict: true, + Timeout: 5 * time.Second, + }, + }, + { + name: "invalid_state", + input: `({state: "bogus"})`, + wantErr: `"bogus" is not a valid DOM state`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameWaitForSelectorOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameDispatchEventOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameDispatchEventOptions + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameDispatchEventOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({strict: true, timeout: 5000})`, + want: func() *common.FrameDispatchEventOptions { + o := common.NewFrameDispatchEventOptions(defaultTimeout) + o.Strict, o.Timeout = true, 5*time.Second + return o + }(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameDispatchEventOptions(vu.Runtime(), v, defaultTimeout) + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameWaitForFunctionOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameWaitForFunctionOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameWaitForFunctionOptions(defaultTimeout), + }, + { + name: "polling_string", + input: `({timeout: 5000, polling: "mutation"})`, + want: &common.FrameWaitForFunctionOptions{ + Polling: common.PollingMutation, + Timeout: 5 * time.Second, + }, + }, + { + name: "polling_number", + input: `({polling: 100})`, + want: &common.FrameWaitForFunctionOptions{ + Polling: common.PollingInterval, + Interval: 100, + Timeout: defaultTimeout, + }, + }, + { + name: "invalid_polling", + input: `({polling: "bogus"})`, + wantErr: "wrong polling option value", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameWaitForFunctionOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameWaitForURLOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameWaitForURLOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameWaitForURLOptions(defaultTimeout), + }, + { + name: "all_options", + input: `({timeout: 1000, waitUntil: "networkidle"})`, + want: &common.FrameWaitForURLOptions{ + Timeout: time.Second, + WaitUntil: common.LifecycleEventNetworkIdle, + }, + }, + { + name: "invalid_waitUntil", + input: `({waitUntil: "none"})`, + wantErr: "parsing waitForURL options", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameWaitForURLOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} + +func TestParseFrameWaitForNavigationOptions(t *testing.T) { + t.Parallel() + + const defaultTimeout = 30 * time.Second + + tests := []struct { + name string + input string + want *common.FrameWaitForNavigationOptions + wantErr string + }{ + { + name: "defaults_on_null", + input: `null`, + want: common.NewFrameWaitForNavigationOptions(defaultTimeout), + }, + { + name: "string_url_is_quoted", + input: `({url: "https://example.com/", timeout: 1000, waitUntil: "networkidle"})`, + want: &common.FrameWaitForNavigationOptions{ + URL: `'https://example.com/'`, + Timeout: time.Second, + WaitUntil: common.LifecycleEventNetworkIdle, + }, + }, + { + name: "invalid_waitUntil", + input: `({waitUntil: "none"})`, + wantErr: "parsing waitForNavigation options", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + v, err := vu.Runtime().RunString(tt.input) + require.NoError(t, err) + + opts, err := parseFrameWaitForNavigationOptions(vu.Runtime(), v, defaultTimeout) + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, opts) + }) + } +} diff --git a/internal/js/modules/k6/browser/browser/helpers.go b/internal/js/modules/k6/browser/browser/helpers.go index 400609fd7..1dbabd9fe 100644 --- a/internal/js/modules/k6/browser/browser/helpers.go +++ b/internal/js/modules/k6/browser/browser/helpers.go @@ -93,11 +93,17 @@ func newRegExMatcher(ctx context.Context, vu moduleVU, tq *taskqueue.TaskQueue) // - Otherwise, rejects the promise with the error fn returns // and emits a browser_errors metric sample. func promise(vu moduleVU, fn func() (result any, reason error)) *sobek.Promise { + // Capture on the JS thread: an async group's context may be restored before + // fn fails in its goroutine. Never enter a runtime metric context there. + var tagsAndMeta k6metrics.TagsAndMeta + if state := vu.State(); k6common.AsyncMetricContextEnabled(state) { + tagsAndMeta = state.Tags.GetCurrentValues() + } p, resolve, reject := promises.New(vu) go func() { v, err := fn() if err != nil { - emitBrowserError(vu, err) + emitBrowserError(vu, err, tagsAndMeta) reject(k6ext.BrowserError(err)) return } @@ -109,13 +115,15 @@ func promise(vu moduleVU, fn func() (result any, reason error)) *sobek.Promise { // emitBrowserError pushes a browser_errors metric sample with the error message as a tag. // When K6_BROWSER_SCREENSHOT_ON_ERROR is enabled, it also captures a screenshot of the // active page and includes it as a base64-encoded "screenshot" tag on the metric. -func emitBrowserError(vu moduleVU, err error) { +func emitBrowserError(vu moduleVU, err error, tagsAndMeta k6metrics.TagsAndMeta) { state := vu.State() if state == nil || vu.browserErrors == nil { return } - tags := state.Tags.GetCurrentValues().Tags - tags = tags.With("error", err.Error()) + if tagsAndMeta.Tags == nil { + tagsAndMeta = state.Tags.GetCurrentValues() + } + tags := tagsAndMeta.Tags.With("error", err.Error()) if vu.screenshotOnError { if screenshot := captureErrorScreenshot(vu); screenshot != "" { @@ -130,6 +138,7 @@ func emitBrowserError(vu moduleVU, err error) { TimeSeries: k6metrics.TimeSeries{Metric: vu.browserErrors, Tags: tags}, Value: 1, Time: now, + Metadata: tagsAndMeta.Metadata, }, }, }) @@ -190,6 +199,17 @@ func queueTask[T any]( } } +func queueTaskWithMetricContext[T any]( + ctx context.Context, + tq *taskqueue.TaskQueue, + captured k6common.CapturedMetricContext, + fn func() (T, error), +) (future func() (T, error)) { + return queueTask(ctx, tq, func() (T, error) { + return k6common.RunWithMetricContext(captured, fn) + }) +} + // newTaskQueue returns a new [taskqueue.TaskQueue] that is closed after // the returned cancel function is called or when the VU's context is done. // diff --git a/internal/js/modules/k6/browser/browser/js_handle_mapping.go b/internal/js/modules/k6/browser/browser/js_handle_mapping.go index bf3149f23..55b081245 100644 --- a/internal/js/modules/k6/browser/browser/js_handle_mapping.go +++ b/internal/js/modules/k6/browser/browser/js_handle_mapping.go @@ -10,16 +10,31 @@ import ( // mapJSHandle to the JS module. func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping { + // Register network operations against the handle's owning page so that + // network-classified calls (evaluate/evaluateHandle) invoked directly on a + // standalone handle are attributed like they are on element handles. Page() + // is not part of the JS-facing JSHandleAPI, so reach it via the concrete + // implementations (BaseJSHandle/ElementHandle). It is nil for worker or + // isolated-world handles, in which case withPageNetworkCalls falls back to + // finishing the mapping without a network-operation begin. + var page *common.Page + if ph, ok := jsh.(interface{ Page() *common.Page }); ok { + page = ph.Page() + } + return withPageNetworkCalls(vu, page, newJSHandleMapping(vu, jsh)) +} + +func newJSHandleMapping(vu moduleVU, jsh common.JSHandleAPI) mapping { return mapping{ - "asElement": func() mapping { + "asElement": passiveCall(func() mapping { return mapElementHandle(vu, jsh.AsElement()) - }, - "dispose": func() *sobek.Promise { + }), + "dispose": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, jsh.Dispose() }) - }, - "evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluate": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluate requires a page function") } @@ -28,8 +43,8 @@ func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping { return promise(vu, func() (any, error) { return jsh.Evaluate(funcString, gopts...) }), nil - }, - "evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluateHandle": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluateHandle requires a page function") } @@ -42,8 +57,8 @@ func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping { } return mapJSHandle(vu, h), nil }), nil - }, - "getProperties": func() *sobek.Promise { + }), + "getProperties": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { props, err := jsh.GetProperties() if err != nil { @@ -56,11 +71,11 @@ func mapJSHandle(vu moduleVU, jsh common.JSHandleAPI) mapping { } return dst, nil }) - }, - "jsonValue": func() *sobek.Promise { + }), + "jsonValue": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return jsh.JSONValue() //nolint:wrapcheck }) - }, + }), } } diff --git a/internal/js/modules/k6/browser/browser/keyboard_mapping.go b/internal/js/modules/k6/browser/browser/keyboard_mapping.go index 1ae0fdb57..bc1be9c7a 100644 --- a/internal/js/modules/k6/browser/browser/keyboard_mapping.go +++ b/internal/js/modules/k6/browser/browser/keyboard_mapping.go @@ -9,18 +9,22 @@ import ( ) func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { + return finishMapping(newKeyboardMapping(vu, kb)) +} + +func newKeyboardMapping(vu moduleVU, kb *common.Keyboard) mapping { return mapping{ - "down": func(key string) *sobek.Promise { + "down": networkCall(func(key string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, kb.Down(key) //nolint:wrapcheck }) - }, - "up": func(key string) *sobek.Promise { + }), + "up": networkCall(func(key string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, kb.Up(key) //nolint:wrapcheck }) - }, - "press": func(key string, opts sobek.Value) (*sobek.Promise, error) { + }), + "press": networkCall(func(key string, opts sobek.Value) (*sobek.Promise, error) { kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) if err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) @@ -28,8 +32,8 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { return promise(vu, func() (any, error) { return nil, kb.Press(key, kbopts) }), nil - }, - "type": func(text string, opts sobek.Value) (*sobek.Promise, error) { + }), + "type": networkCall(func(text string, opts sobek.Value) (*sobek.Promise, error) { kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) if err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) @@ -37,11 +41,11 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { return promise(vu, func() (any, error) { return nil, kb.Type(text, kbopts) }), nil - }, - "insertText": func(text string) *sobek.Promise { + }), + "insertText": networkCall(func(text string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, kb.InsertText(text) //nolint:wrapcheck }) - }, + }), } } diff --git a/internal/js/modules/k6/browser/browser/locator_mapping.go b/internal/js/modules/k6/browser/browser/locator_mapping.go index e036a920e..57a7564bc 100644 --- a/internal/js/modules/k6/browser/browser/locator_mapping.go +++ b/internal/js/modules/k6/browser/browser/locator_mapping.go @@ -3,6 +3,7 @@ package browser import ( "errors" "fmt" + "time" "github.com/grafana/sobek" @@ -15,8 +16,8 @@ import ( //nolint:gocognit,funlen,cyclop func mapLocator(vu moduleVU, lo *common.Locator) mapping { rt := vu.Runtime() - return mapping{ - "all": func() *sobek.Promise { + maps := mapping{ + "all": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { all, err := lo.All() if err != nil { @@ -29,10 +30,10 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { } return res, nil }) - }, - "boundingBox": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "boundingBox": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewFrameBaseOptions(lo.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing locator bounding box options: %w", err) } return promise(vu, func() (any, error) { @@ -40,22 +41,22 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { // We want to avoid errors when an element is not visible or detached and instead // opt to return a nil rectangle -- this matches Playwright's behaviour. if errors.Is(err, common.ErrElementNotVisible) || errors.Is(err, common.ErrElementNotAttachedToDOM) { - return nil, nil + return nil, nil //nolint:nilnil // Absence maps to JavaScript null. } return box, err }), nil - }, - "clear": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameFillOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "clear": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameFillOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing clear options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Clear(copts) //nolint:wrapcheck }), nil - }, - "click": func(opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, lo.Timeout()) + }), + "click": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameClickOptions(rt, opts, lo.Timeout()) if err != nil { return nil, err } @@ -63,26 +64,26 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { return promise(vu, func() (any, error) { return nil, lo.Click(popts) //nolint:wrapcheck }), nil - }, - "contentFrame": func() *sobek.Object { + }), + "contentFrame": passiveCall(func() *sobek.Object { ml := mapFrameLocator(vu, lo.ContentFrame()) return rt.ToValue(ml).ToObject(rt) - }, - "count": func() *sobek.Promise { + }), + "count": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return lo.Count() //nolint:wrapcheck }) - }, - "dblclick": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameDblClickOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "dblclick": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameDblclickOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Dblclick(copts) //nolint:wrapcheck }), nil - }, - "evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluate": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluate requires a page function") } @@ -91,8 +92,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { return promise(vu, func() (any, error) { return lo.Evaluate(funcString, gopts...) }), nil - }, - "evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluateHandle": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluateHandle requires a page function") } @@ -105,119 +106,119 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { } return mapJSHandle(vu, jsh), nil }), nil - }, - "setChecked": func(checked bool, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameCheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "setChecked": networkCall(func(checked bool, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameCheckOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing set checked options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.SetChecked(checked, copts) //nolint:wrapcheck }), nil - }, - "check": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameCheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "check": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameCheckOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing check options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Check(copts) //nolint:wrapcheck }), nil - }, - "uncheck": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameUncheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "uncheck": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameUncheckOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing uncheck options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Uncheck(copts) //nolint:wrapcheck }), nil - }, - "isChecked": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameIsCheckedOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "isChecked": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameIsCheckedOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing is checked options: %w", err) } return promise(vu, func() (any, error) { return lo.IsChecked(copts) //nolint:wrapcheck }), nil - }, - "isEditable": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameIsEditableOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "isEditable": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameIsEditableOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing is editable options: %w", err) } return promise(vu, func() (any, error) { return lo.IsEditable(copts) //nolint:wrapcheck }), nil - }, - "isEnabled": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameIsEnabledOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "isEnabled": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameIsEnabledOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing is enabled options: %w", err) } return promise(vu, func() (any, error) { return lo.IsEnabled(copts) //nolint:wrapcheck }), nil - }, - "isDisabled": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameIsDisabledOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "isDisabled": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameIsDisabledOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing is disabled options: %w", err) } return promise(vu, func() (any, error) { return lo.IsDisabled(copts) //nolint:wrapcheck }), nil - }, - "isInViewport": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameIsInViewportOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "isInViewport": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameIsInViewportOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing is in viewport options: %w", err) } return promise(vu, func() (any, error) { return lo.IsInViewport(copts) //nolint:wrapcheck }), nil - }, - "isVisible": func() *sobek.Promise { + }), + "isVisible": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return lo.IsVisible() //nolint:wrapcheck }) - }, - "isHidden": func() *sobek.Promise { + }), + "isHidden": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return lo.IsHidden() //nolint:wrapcheck }) - }, - "fill": func(value string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameFillOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "fill": networkCall(func(value string, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameFillOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Fill(value, copts) //nolint:wrapcheck }), nil - }, - "filter": func(opts sobek.Value) mapping { + }), + "filter": passiveCall(func(opts sobek.Value) mapping { return mapLocator(vu, lo.Filter(&common.LocatorFilterOptions{ LocatorOptions: parseLocatorOptions(rt, opts), })) - }, - "first": func() *sobek.Object { + }), + "first": passiveCall(func() *sobek.Object { ml := mapLocator(vu, lo.First()) return rt.ToValue(ml).ToObject(rt) - }, - "focus": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "focus": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { copts := common.NewFrameBaseOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(copts, rt, opts); err != nil { return nil, fmt.Errorf("parsing focus options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Focus(copts) //nolint:wrapcheck }), nil - }, - "getAttribute": func(name string, opts sobek.Value) (*sobek.Promise, error) { + }), + "getAttribute": passiveCall(func(name string, opts sobek.Value) (*sobek.Promise, error) { copts := common.NewFrameBaseOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(copts, rt, opts); err != nil { return nil, fmt.Errorf("parsing get attribute options: %w", err) } return promise(vu, func() (any, error) { @@ -226,12 +227,12 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { return nil, err //nolint:wrapcheck } if !ok { - return nil, nil + return nil, nil //nolint:nilnil // Absence maps to JavaScript null. } return s, nil }), nil - }, - "getByAltText": func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByAltText": passiveCall(func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(alt) { return nil, errors.New("missing required argument 'altText'") } @@ -239,8 +240,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByAltText(palt, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByLabel": func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByLabel": passiveCall(func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(label) { return nil, errors.New("missing required argument 'label'") } @@ -248,8 +249,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByLabel(plabel, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByPlaceholder": func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByPlaceholder": passiveCall(func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(placeholder) { return nil, errors.New("missing required argument 'placeholder'") } @@ -257,8 +258,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByPlaceholder(pplaceholder, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByRole": func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByRole": passiveCall(func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(role) { return nil, errors.New("missing required argument 'role'") } @@ -266,8 +267,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByRole(role.String(), popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTestId": func(testID sobek.Value) (*sobek.Object, error) { + }), + "getByTestId": passiveCall(func(testID sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(testID) { return nil, errors.New("missing required argument 'testId'") } @@ -275,8 +276,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByTestID(ptestID)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByText": func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByText": passiveCall(func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(text) { return nil, errors.New("missing required argument 'text'") } @@ -284,8 +285,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByText(ptext, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTitle": func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByTitle": passiveCall(func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(title) { return nil, errors.New("missing required argument 'title'") } @@ -293,43 +294,43 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { ml := mapLocator(vu, lo.GetByTitle(ptitle, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "locator": func(selector string, opts sobek.Value) mapping { + }), + "locator": passiveCall(func(selector string, opts sobek.Value) mapping { return mapLocator(vu, lo.Locator(selector, parseLocatorOptions(rt, opts))) - }, - "frameLocator": func(selector string) *sobek.Object { + }), + "frameLocator": passiveCall(func(selector string) *sobek.Object { mfl := mapFrameLocator(vu, lo.FrameLocator(selector)) return rt.ToValue(mfl).ToObject(rt) - }, - "innerHTML": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameInnerHTMLOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "innerHTML": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameInnerHTMLOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner HTML options: %w", err) } return promise(vu, func() (any, error) { return lo.InnerHTML(copts) //nolint:wrapcheck }), nil - }, - "innerText": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameInnerTextOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "innerText": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameInnerTextOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner text options: %w", err) } return promise(vu, func() (any, error) { return lo.InnerText(copts) //nolint:wrapcheck }), nil - }, - "last": func() *sobek.Object { + }), + "last": passiveCall(func() *sobek.Object { ml := mapLocator(vu, lo.Last()) return rt.ToValue(ml).ToObject(rt) - }, - "nth": func(nth int) *sobek.Object { + }), + "nth": passiveCall(func(nth int) *sobek.Object { ml := mapLocator(vu, lo.Nth(nth)) return rt.ToValue(ml).ToObject(rt) - }, - "textContent": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTextContentOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "textContent": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameTextContentOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing text content options: %w", err) } return promise(vu, func() (any, error) { @@ -338,23 +339,23 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { return nil, err //nolint:wrapcheck } if !ok { - return nil, nil + return nil, nil //nolint:nilnil // Absence maps to JavaScript null. } return s, nil }), nil - }, - "inputValue": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameInputValueOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "inputValue": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameInputValueOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing input value options: %w", err) } return promise(vu, func() (any, error) { return lo.InputValue(copts) //nolint:wrapcheck }), nil - }, - "selectOption": func(values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameSelectOptionOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "selectOption": networkCall(func(values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameSelectOptionOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } convValues, err := ConvertSelectOptionValues(vu.Runtime(), values) @@ -364,73 +365,74 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { return promise(vu, func() (any, error) { return lo.SelectOption(convValues, copts) //nolint:wrapcheck }), nil - }, - "press": func(key string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFramePressOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "press": networkCall(func(key string, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFramePressOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing press options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Press(key, copts) //nolint:wrapcheck }), nil - }, + }), - "pressSequentially": func(text string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTypeOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + "pressSequentially": networkCall(func(text string, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameTypeOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing locator press sequentially options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.PressSequentially(text, copts) //nolint:wrapcheck }), nil - }, + }), - "type": func(text string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTypeOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + "type": networkCall(func(text string, opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameTypeOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Type(text, copts) //nolint:wrapcheck }), nil - }, - "hover": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameHoverOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "hover": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameHoverOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Hover(copts) //nolint:wrapcheck }), nil - }, - "tap": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTapOptions(lo.DefaultTimeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "tap": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameTapOptions(rt, opts, lo.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing locator tap options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.Tap(copts) //nolint:wrapcheck }), nil - }, - "dispatchEvent": func(typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDispatchEventOptions(lo.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dispatchEvent": networkCall(func(typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameDispatchEventOptions(rt, opts, lo.DefaultTimeout()) + if err != nil { return nil, fmt.Errorf("parsing locator dispatch event options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.DispatchEvent(typ, exportArg(eventInit), popts) //nolint:wrapcheck }), nil - }, - "waitFor": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameWaitForSelectorOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + }), + "waitFor": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + copts, err := parseFrameWaitForSelectorOptions(rt, opts, lo.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing wait for options: %w", err) } return promise(vu, func() (any, error) { return nil, lo.WaitFor(copts) //nolint:wrapcheck }), nil - }, + }), } + return withPageNetworkCalls(vu, lo.Page(), maps) } func parseLocatorOptions(rt *sobek.Runtime, opts sobek.Value) *common.LocatorOptions { @@ -452,3 +454,26 @@ func parseLocatorOptions(rt *sobek.Runtime, opts sobek.Value) *common.LocatorOpt return &popts } + +// parseFrameIsInViewportOptions parses the frame isInViewport options from a Sobek value. +func parseFrameIsInViewportOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameIsInViewportOptions, error) { + vopts := common.NewFrameIsInViewportOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return vopts, nil + } + + err := parseFrameBaseOptions(&vopts.FrameBaseOptions, rt, opts) + if err != nil { + return vopts, err + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "ratio" { + vopts.Ratio = obj.Get(k).ToFloat() + } + } + + return vopts, nil +} diff --git a/internal/js/modules/k6/browser/browser/mapping.go b/internal/js/modules/k6/browser/browser/mapping.go index a9890ea46..202db5d66 100644 --- a/internal/js/modules/k6/browser/browser/mapping.go +++ b/internal/js/modules/k6/browser/browser/mapping.go @@ -1,9 +1,9 @@ package browser import ( - "context" "fmt" "reflect" + "sync" "time" "github.com/grafana/sobek" @@ -19,18 +19,210 @@ import ( // and customization over our API. type mapping = map[string]any -// mapBrowserToSobek maps the browser API to the JS module. +type mappingCallKind uint8 + +const ( + mappingCallPassive mappingCallKind = iota + 1 + mappingCallNetwork +) + +type mappingCall struct { + fn any + kind mappingCallKind +} + +// Browser requests can arrive after the mapped call that initiated them settles. Requiring every +// callable to choose a kind prevents a passive waiter from silently becoming a competing operation. +func passiveCall(fn any) mappingCall { + return mappingCall{fn: fn, kind: mappingCallPassive} +} + +func networkCall(fn any) mappingCall { + return mappingCall{fn: fn, kind: mappingCallNetwork} +} + +func finishMapping(m mapping) mapping { + return finishMappingCalls(moduleVU{}, m, nil, "") +} + +func withPageNetworkCalls(vu moduleVU, page *common.Page, m mapping) mapping { + var begin func() (finishAndRetain, abortAndDiscard func()) + if page != nil && k6common.AsyncMetricContextEnabled(vu.State()) { + begin = func() (finishAndRetain, abortAndDiscard func()) { + state := vu.State() + if state == nil || state.Tags == nil { + return func() {}, func() {} + } + return page.BeginNetworkOperation(state.Tags.GetCurrentValues()) + } + } + return finishMappingCalls(vu, m, begin, "") +} + +func finishMappingCalls( + vu moduleVU, + m mapping, + begin func() (finishAndRetain, abortAndDiscard func()), + path string, +) mapping { + for name, value := range m { + callPath := name + if path != "" { + callPath = path + "." + name + } + switch value := value.(type) { + case mapping: + m[name] = finishMappingCalls(vu, value, begin, callPath) + case mappingCall: + fn := reflect.ValueOf(value.fn) + if !fn.IsValid() || fn.Kind() != reflect.Func { + panic(fmt.Sprintf("browser mapping %q classifies non-callable %T", callPath, value.fn)) + } + switch value.kind { + case mappingCallPassive: + m[name] = value.fn + case mappingCallNetwork: + if begin == nil { + m[name] = value.fn + break + } + m[name], _ = aroundMappingCall(vu, value.fn, begin) + default: + panic(fmt.Sprintf("browser mapping call %q has invalid classification", callPath)) + } + default: + valueType := reflect.TypeOf(value) + if valueType != nil && valueType.Kind() == reflect.Func { + panic(fmt.Sprintf("browser mapping call %q must be classified", callPath)) + } + m[name] = value + } + } + return m +} + +func aroundMappingCalls( + vu moduleVU, + m mapping, + begin func() (finishAndRetain, abortAndDiscard func()), +) mapping { + return finishMappingCalls(vu, m, begin, "") +} + +func aroundMappingCall( + vu moduleVU, + value any, + begin func() (finishAndRetain, abortAndDiscard func()), +) (any, bool) { + original := reflect.ValueOf(value) + if original.Kind() != reflect.Func { + return nil, false + } + wrapped := reflect.MakeFunc(original.Type(), func(args []reflect.Value) []reflect.Value { + finishAndRetain, abortAndDiscard := begin() + operationLaunched := false + defer func() { + if !operationLaunched { + abortAndDiscard() + } + }() + + results := callMapping(original, args) + if mappingCallFailed(results) { + return results + } + operationLaunched = wrapMappingPromise(vu, results, finishAndRetain) + if !operationLaunched { + finishAndRetain() + operationLaunched = true + } + return results + }) + return wrapped.Interface(), true +} + +func callMapping(fn reflect.Value, args []reflect.Value) []reflect.Value { + if fn.Type().IsVariadic() { + return fn.CallSlice(args) + } + return fn.Call(args) +} + +func mappingCallFailed(results []reflect.Value) bool { + for _, result := range results { + if err, ok := reflect.TypeAssert[error](result); ok && err != nil { + return true + } + } + return false +} + +func wrapMappingPromise(vu moduleVU, results []reflect.Value, finishAndRetain func()) bool { + for i, result := range results { + promise, ok := reflect.TypeAssert[*sobek.Promise](result) + if !ok || promise == nil { + continue + } + results[i] = reflect.ValueOf(endOperationOnPromiseSettlement(vu, promise, finishAndRetain)) + return true + } + return false +} + +func endOperationOnPromiseSettlement( + vu moduleVU, + promise *sobek.Promise, + finishAndRetain func(), +) *sobek.Promise { + rt := vu.Runtime() + wrapped, resolve, reject := rt.NewPromise() + finishAndRetain = sync.OnceFunc(finishAndRetain) + + onFulfilled := func(call sobek.FunctionCall) sobek.Value { + finishAndRetain() + if err := resolve(call.Argument(0)); err != nil { + k6common.Throw(rt, err) + } + return sobek.Undefined() + } + onRejected := func(call sobek.FunctionCall) sobek.Value { + // Rejection means the launched operation settled unsuccessfully, not that it never + // reached the browser. Retain its context for related CDP requests observed afterward. + finishAndRetain() + if err := reject(call.Argument(0)); err != nil { + k6common.Throw(rt, err) + } + return sobek.Undefined() + } + + then, ok := sobek.AssertFunction(rt.ToValue(promise).ToObject(rt).Get("then")) + if !ok { + panic("browser promise has no callable then method") + } + if _, err := then( + rt.ToValue(promise), + rt.ToValue(onFulfilled), + rt.ToValue(onRejected), + ); err != nil { + finishAndRetain() + if rejectErr := reject(err); rejectErr != nil { + k6common.Throw(rt, rejectErr) + } + } + return wrapped +} + +// mapToSobek maps a browser-module mapping to a Sobek object. // The motivation of this mapping was to support $ and $$ wildcard // methods. // See issue #661 for more details. -func mapBrowserToSobek(vu moduleVU) *sobek.Object { +func mapToSobek(vu moduleVU, m mapping) *sobek.Object { var ( rt = vu.Runtime() obj = rt.NewObject() ) - for k, v := range mapBrowser(vu) { - err := obj.Set(k, rt.ToValue(v)) - if err != nil { + for k, v := range m { + if err := obj.Set(k, rt.ToValue(v)); err != nil { k6common.Throw(rt, k6ext.BrowserError(fmt.Errorf("mapping: %w", err))) } } @@ -39,12 +231,21 @@ func mapBrowserToSobek(vu moduleVU) *sobek.Object { } func parseFrameClickOptions( - ctx context.Context, opts sobek.Value, defaultTimeout time.Duration, + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, ) (*common.FrameClickOptions, error) { copts := common.NewFrameClickOptions(defaultTimeout) - if err := copts.Parse(ctx, opts); err != nil { - return nil, fmt.Errorf("parsing click options: %w", err) + if k6common.IsNullish(opts) { + return copts, nil } + + ehcopts, err := parseElementHandleClickOptions(rt, opts, defaultTimeout) + if err != nil { + return copts, err + } + + copts.ElementHandleClickOptions = *ehcopts + copts.Strict = parseStrict(rt, opts) + return copts, nil } diff --git a/internal/js/modules/k6/browser/browser/mapping_test.go b/internal/js/modules/k6/browser/browser/mapping_test.go index 01e865dcd..c0b94565b 100644 --- a/internal/js/modules/k6/browser/browser/mapping_test.go +++ b/internal/js/modules/k6/browser/browser/mapping_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/grafana/sobek" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" @@ -48,6 +49,230 @@ func customMappings() map[string]string { } } +func TestAroundMappingCallsIncludesAllNestedMappings(t *testing.T) { + t.Parallel() + + var calls []string + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: sobek.New()}} + m := aroundMappingCalls(vu, mapping{ + "top": networkCall(func(value string) string { + calls = append(calls, "top:"+value) + return value + }), + "skipped": networkCall(func() { + calls = append(calls, "skipped") + }), + "nested": mapping{ + "call": networkCall(func(value int) int { + calls = append(calls, "nested") + return value + }), + }, + }, func() (func(), func()) { + calls = append(calls, "begin") + return func() { calls = append(calls, "finish and retain") }, + func() { calls = append(calls, "abort and discard") } + }) + + require.Equal(t, "value", m["top"].(func(string) string)("value")) + m["skipped"].(func())() + nested := m["nested"].(mapping) + require.Equal(t, 42, nested["call"].(func(int) int)(42)) + require.Equal(t, []string{ + "begin", "top:value", "finish and retain", + "begin", "skipped", "finish and retain", + "begin", "nested", "finish and retain", + }, calls) +} + +func TestAroundMappingCallsLeavesPassiveCallsUnwrapped(t *testing.T) { + t.Parallel() + + started := false + called := false + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: sobek.New()}} + m := aroundMappingCalls(vu, mapping{ + "call": passiveCall(func() { called = true }), + }, func() (func(), func()) { + started = true + return func() {}, func() {} + }) + + m["call"].(func())() + assert.True(t, called) + assert.False(t, started) +} + +func TestFinishMappingRequiresExplicitCallClassification(t *testing.T) { + t.Parallel() + + require.PanicsWithValue(t, `browser mapping call "nested.call" must be classified`, func() { + finishMapping(mapping{ + "nested": mapping{ + "call": func() {}, + }, + }) + }) +} + +func TestFinishMappingPreservesReturnedMappings(t *testing.T) { + t.Parallel() + + rt := sobek.New() + m := finishMapping(mapping{ + "context": passiveCall(func() mapping { + return finishMapping(mapping{ + "pages": passiveCall(func() int { return 1 }), + }) + }), + }) + require.NoError(t, rt.Set("browser", m)) + + value, err := rt.RunString(`browser.context().pages()`) + require.NoError(t, err) + require.Equal(t, int64(1), value.ToInteger()) +} + +func TestAroundMappingCallsAbortsFailedCallBeforeLaunch(t *testing.T) { + t.Parallel() + + expectedErr := assert.AnError + abortedAndDiscarded := false + finishedAndRetained := false + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: sobek.New()}} + m := aroundMappingCalls(vu, mapping{ + "call": networkCall(func() (*sobek.Promise, error) { return nil, expectedErr }), + }, func() (func(), func()) { + return func() { finishedAndRetained = true }, func() { abortedAndDiscarded = true } + }) + + promise, err := m["call"].(func() (*sobek.Promise, error))() + assert.Nil(t, promise) + assert.ErrorIs(t, err, expectedErr) + assert.False(t, finishedAndRetained) + assert.True(t, abortedAndDiscarded) +} + +func TestAroundMappingCallsKeepsOperationUntilPromiseSettles(t *testing.T) { + t.Parallel() + + rt := sobek.New() + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: rt}} + promise, resolve, _ := rt.NewPromise() + active := false + m := aroundMappingCalls(vu, mapping{ + "call": networkCall(func() *sobek.Promise { return promise }), + }, func() (func(), func()) { + active = true + return func() { active = false }, func() { active = false } + }) + + wrapped := m["call"].(func() *sobek.Promise)() + assert.True(t, active) + require.NoError(t, resolve("result")) + _, err := rt.RunString(`0`) + require.NoError(t, err) + assert.False(t, active) + assert.Equal(t, sobek.PromiseStateFulfilled, wrapped.State()) + assert.Equal(t, "result", wrapped.Result().String()) +} + +func TestAroundMappingCallsRetainsOperationWhenPromiseRejects(t *testing.T) { + t.Parallel() + + rt := sobek.New() + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: rt}} + promise, _, reject := rt.NewPromise() + abortedAndDiscarded := false + finishedAndRetained := false + m := aroundMappingCalls(vu, mapping{ + "call": networkCall(func() *sobek.Promise { return promise }), + }, func() (func(), func()) { + return func() { finishedAndRetained = true }, func() { abortedAndDiscarded = true } + }) + + wrapped := m["call"].(func() *sobek.Promise)() + require.NoError(t, reject("failure")) + _, err := rt.RunString(`0`) + require.NoError(t, err) + assert.True(t, finishedAndRetained) + assert.False(t, abortedAndDiscarded) + assert.Equal(t, sobek.PromiseStateRejected, wrapped.State()) + assert.Equal(t, "failure", wrapped.Result().String()) +} + +func BenchmarkAroundMappingCalls(b *testing.B) { + for name, mapped := range map[string]bool{ + "direct": false, + "wrapped": true, + } { + b.Run(name, func(b *testing.B) { + rt := sobek.New() + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: rt}} + fn := func(value int64) int64 { return value } + if mapped { + fn = aroundMappingCalls(vu, mapping{"call": networkCall(fn)}, func() (func(), func()) { + return func() {}, func() {} + })["call"].(func(int64) int64) + } + require.NoError(b, rt.Set("call", fn)) + call, err := rt.RunString(`() => call(42)`) + require.NoError(b, err) + invoke, ok := sobek.AssertFunction(call) + require.True(b, ok) + + b.ReportAllocs() + for b.Loop() { + _, err = invoke(sobek.Undefined()) + if err != nil { + b.Fatal(err) + } + } + }) + } +} + +func BenchmarkAroundMappingPromiseCalls(b *testing.B) { + for name, mapped := range map[string]bool{ + "direct": false, + "wrapped": true, + } { + b.Run(name, func(b *testing.B) { + rt := sobek.New() + vu := moduleVU{VU: &k6modulestest.VU{RuntimeField: rt}} + fn := func() *sobek.Promise { + promise, resolve, _ := rt.NewPromise() + if err := resolve(42); err != nil { + b.Fatal(err) + } + return promise + } + if mapped { + fn = aroundMappingCalls(vu, mapping{"call": networkCall(fn)}, func() (func(), func()) { + return func() {}, func() {} + })["call"].(func() *sobek.Promise) + } + require.NoError(b, rt.Set("call", fn)) + call, err := rt.RunString(`() => call()`) + require.NoError(b, err) + invoke, ok := sobek.AssertFunction(call) + require.True(b, ok) + drain, err := sobek.Compile("drain.js", "0", false) + require.NoError(b, err) + + b.ReportAllocs() + for b.Loop() { + if _, err = invoke(sobek.Undefined()); err != nil { + b.Fatal(err) + } + if _, err = rt.RunProgram(drain); err != nil { + b.Fatal(err) + } + } + }) + } +} + // TestMappings tests that all the methods of the API (api/) are // to the module. This is to ensure that we don't forget to map // a new method to the module. @@ -82,8 +307,7 @@ func TestMappings(t *testing.T) { mapped = tt.mapp() tested = make(map[string]bool) ) - for i := 0; i < typ.NumMethod(); i++ { - method := typ.Method(i) + for method := range typ.Methods() { require.NotNil(t, method) // sobek uses methods that starts with lowercase. @@ -126,7 +350,8 @@ func TestMappings(t *testing.T) { "browser": { apiInterface: (*browserAPI)(nil), mapp: func() mapping { - return mapBrowser(moduleVU{VU: vu}) + mvu := moduleVU{VU: vu} + return mapBrowser(mvu, mvu.browser) }, }, "browserContext": { diff --git a/internal/js/modules/k6/browser/browser/metric_event_mapping.go b/internal/js/modules/k6/browser/browser/metric_event_mapping.go index de76bf36b..37d1244c1 100644 --- a/internal/js/modules/k6/browser/browser/metric_event_mapping.go +++ b/internal/js/modules/k6/browser/browser/metric_event_mapping.go @@ -11,8 +11,8 @@ func mapMetricEvent(vu moduleVU, event common.PageEvent) mapping { rt := vu.Runtime() em := event.Metric - return mapping{ - "tag": func(urls common.TagMatches) error { + return finishMapping(mapping{ + "tag": passiveCall(func(urls common.TagMatches) error { callback := func(pattern, url string) (bool, error) { matched, err := rt.RunString(pattern + `.test('` + url + `')`) if err != nil { @@ -22,6 +22,6 @@ func mapMetricEvent(vu moduleVU, event common.PageEvent) mapping { } return em.Tag(callback, urls) //nolint:wrapcheck - }, - } + }), + }) } diff --git a/internal/js/modules/k6/browser/browser/module.go b/internal/js/modules/k6/browser/browser/module.go index 5bb0f218b..74b1e04c4 100644 --- a/internal/js/modules/k6/browser/browser/module.go +++ b/internal/js/modules/k6/browser/browser/module.go @@ -13,6 +13,7 @@ import ( "github.com/grafana/sobek" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/chromium" "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" "go.k6.io/k6/v2/internal/js/modules/k6/browser/env" "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" @@ -31,11 +32,11 @@ type ( // RootModule is the global module instance that will create module // instances for each VU. RootModule struct { - PidRegistry *pidRegistry - remoteRegistry *remoteRegistry - initOnce *sync.Once - tracesMetadata map[string]string - filePersister filePersister + PidRegistry *pidRegistry + remoteRegistry *remoteRegistry + initOnce *sync.Once + tracesMetadata map[string]string + filePersister filePersister testRunID string browserErrors *k6metrics.Metric screenshotOnError bool @@ -44,6 +45,7 @@ type ( // JSModule exposes the properties available to the JS script. JSModule struct { Browser *sobek.Object + Chromium *sobek.Object Devices map[string]common.Device NetworkProfiles map[string]common.NetworkProfile `js:"networkProfiles"` } @@ -79,24 +81,27 @@ func (m *RootModule) NewModuleInstance(vu k6modules.VU) k6modules.Instance { m.initialize(vu) }) + mvu := moduleVU{ + VU: vu, + pidRegistry: m.PidRegistry, + browserRegistry: newBrowserRegistry( + context.Background(), + vu, + m.remoteRegistry, + m.PidRegistry, + m.tracesMetadata, + ), + taskQueueRegistry: newTaskQueueRegistry(vu), + filePersister: m.filePersister, + testRunID: m.testRunID, + browserErrors: m.browserErrors, + screenshotOnError: m.screenshotOnError, + } + return &ModuleInstance{ mod: &JSModule{ - Browser: mapBrowserToSobek(moduleVU{ - VU: vu, - pidRegistry: m.PidRegistry, - browserRegistry: newBrowserRegistry( - context.Background(), - vu, - m.remoteRegistry, - m.PidRegistry, - m.tracesMetadata, - ), - taskQueueRegistry: newTaskQueueRegistry(vu), - filePersister: m.filePersister, - testRunID: m.testRunID, - browserErrors: m.browserErrors, - screenshotOnError: m.screenshotOnError, - }), + Browser: mapToSobek(mvu, mapBrowser(mvu, mvu.browser)), + Chromium: mapToSobek(mvu, mapChromium(mvu, chromium.NewBrowserType(vu))), Devices: common.GetDevices(), NetworkProfiles: common.GetNetworkProfiles(), }, diff --git a/internal/js/modules/k6/browser/browser/module_test.go b/internal/js/modules/k6/browser/browser/module_test.go index b23f8d523..d658f9184 100644 --- a/internal/js/modules/k6/browser/browser/module_test.go +++ b/internal/js/modules/k6/browser/browser/module_test.go @@ -21,4 +21,5 @@ func TestModuleNew(t *testing.T) { require.NotNil(t, m.mod.Browser, "Browser should be set") require.NotNil(t, m.mod.Devices, "Devices should be set") require.NotNil(t, m.mod.NetworkProfiles, "Profiles should be set") + require.NotNil(t, m.mod.Chromium, "Chromium should be set") } diff --git a/internal/js/modules/k6/browser/browser/mouse_mapping.go b/internal/js/modules/k6/browser/browser/mouse_mapping.go index 7016bafe4..8bc1c37f9 100644 --- a/internal/js/modules/k6/browser/browser/mouse_mapping.go +++ b/internal/js/modules/k6/browser/browser/mouse_mapping.go @@ -12,8 +12,12 @@ import ( func mapMouse(vu moduleVU, m *common.Mouse) mapping { rt := vu.Runtime() + return finishMapping(newMouseMapping(vu, m, rt)) +} + +func newMouseMapping(vu moduleVU, m *common.Mouse, rt *sobek.Runtime) mapping { return mapping{ - "click": func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { + "click": networkCall(func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { popts, err := parseMouseClickOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing mouse click options: %w", err) @@ -21,8 +25,8 @@ func mapMouse(vu moduleVU, m *common.Mouse) mapping { return promise(vu, func() (any, error) { return nil, m.Click(x, y, popts) //nolint:wrapcheck }), nil - }, - "dblClick": func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { + }), + "dblClick": networkCall(func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { popts, err := parseMouseDblClickOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing mouse double click options: %w", err) @@ -30,8 +34,8 @@ func mapMouse(vu moduleVU, m *common.Mouse) mapping { return promise(vu, func() (any, error) { return nil, m.DblClick(x, y, popts) //nolint:wrapcheck }), nil - }, - "down": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "down": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parseMouseDownUpOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing mouse down options: %w", err) @@ -39,8 +43,8 @@ func mapMouse(vu moduleVU, m *common.Mouse) mapping { return promise(vu, func() (any, error) { return nil, m.Down(popts) //nolint:wrapcheck }), nil - }, - "up": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "up": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parseMouseDownUpOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing mouse up options: %w", err) @@ -48,8 +52,8 @@ func mapMouse(vu moduleVU, m *common.Mouse) mapping { return promise(vu, func() (any, error) { return nil, m.Up(popts) //nolint:wrapcheck }), nil - }, - "move": func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { + }), + "move": networkCall(func(x float64, y float64, opts sobek.Value) (*sobek.Promise, error) { popts, err := parseMouseMoveOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing mouse move options: %w", err) @@ -57,7 +61,7 @@ func mapMouse(vu moduleVU, m *common.Mouse) mapping { return promise(vu, func() (any, error) { return nil, m.Move(x, y, popts) //nolint:wrapcheck }), nil - }, + }), } } diff --git a/internal/js/modules/k6/browser/browser/page_mapping.go b/internal/js/modules/k6/browser/browser/page_mapping.go index a80bf13d4..40593cff8 100644 --- a/internal/js/modules/k6/browser/browser/page_mapping.go +++ b/internal/js/modules/k6/browser/browser/page_mapping.go @@ -22,22 +22,22 @@ import ( func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop rt := vu.Runtime() maps := mapping{ - "bringToFront": func() *sobek.Promise { + "bringToFront": networkCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return nil, p.BringToFront() //nolint:wrapcheck }) - }, - "check": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "check": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameCheckOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing new frame check options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Check(selector, popts) //nolint:wrapcheck }), nil - }, - "click": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, p.MainFrame().Timeout()) + }), + "click": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameClickOptions(rt, opts, p.MainFrame().Timeout()) if err != nil { return nil, err } @@ -46,8 +46,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop err := p.Click(selector, popts) return nil, err //nolint:wrapcheck }), nil - }, - "close": func(opts sobek.Value) *sobek.Promise { + }), + "close": networkCall(func(opts sobek.Value) *sobek.Promise { // TODO when opts are implemented for this function, parse them here before calling promise() // in a goroutine off the event loop. As that will race with anything running on the event loop. return promise(vu, func() (any, error) { @@ -60,35 +60,35 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return nil, err //nolint:wrapcheck }) - }, - "content": func() *sobek.Promise { + }), + "content": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return p.Content() //nolint:wrapcheck }) - }, - "context": func() mapping { + }), + "context": passiveCall(func() mapping { return mapBrowserContext(vu, p.Context()) - }, - "dblclick": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDblClickOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dblclick": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameDblclickOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Dblclick(selector, popts) //nolint:wrapcheck }), nil - }, - "dispatchEvent": func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDispatchEventOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "dispatchEvent": networkCall(func(selector, typ string, eventInit, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameDispatchEventOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing page dispatch event options: %w", err) } earg := exportArg(eventInit) return promise(vu, func() (any, error) { return nil, p.DispatchEvent(selector, typ, earg, popts) //nolint:wrapcheck }), nil - }, - "emulateMedia": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "emulateMedia": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parsePageEmulateMediaOptions(rt, opts, common.NewPageEmulateMediaOptions(p)) if err != nil { return nil, fmt.Errorf("parsing emulateMedia options: %w", err) @@ -96,13 +96,13 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (any, error) { return nil, p.EmulateMedia(popts) //nolint:wrapcheck }), nil - }, - "emulateVisionDeficiency": func(typ string) *sobek.Promise { + }), + "emulateVisionDeficiency": passiveCall(func(typ string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, p.EmulateVisionDeficiency(typ) //nolint:wrapcheck }) - }, - "evaluate": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluate": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluate requires a page function") } @@ -111,8 +111,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (any, error) { return p.Evaluate(funcString, gopts...) }), nil - }, - "evaluateHandle": func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { + }), + "evaluateHandle": networkCall(func(pageFunc sobek.Value, gargs ...sobek.Value) (*sobek.Promise, error) { if sobekEmptyString(pageFunc) { return nil, fmt.Errorf("evaluateHandle requires a page function") } @@ -125,36 +125,36 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapJSHandle(vu, jsh), nil }), nil - }, - "fill": func(selector string, value string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameFillOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "fill": networkCall(func(selector string, value string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameFillOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Fill(selector, value, popts) //nolint:wrapcheck }), nil - }, - "focus": func(selector string, opts sobek.Value) (*sobek.Promise, error) { + }), + "focus": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { popts := common.NewFrameBaseOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing focus options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Focus(selector, popts) //nolint:wrapcheck }), nil - }, - "frames": func() *sobek.Object { + }), + "frames": passiveCall(func() *sobek.Object { frs := p.Frames() mfrs := make([]mapping, 0, len(frs)) for _, fr := range frs { mfrs = append(mfrs, mapFrame(vu, fr)) } return rt.ToValue(mfrs).ToObject(rt) - }, - "getAttribute": func(selector string, name string, opts sobek.Value) (*sobek.Promise, error) { + }), + "getAttribute": passiveCall(func(selector string, name string, opts sobek.Value) (*sobek.Promise, error) { popts := common.NewFrameBaseOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + if err := parseFrameBaseOptions(popts, rt, opts); err != nil { return nil, fmt.Errorf("parsing getAttribute options: %w", err) } return promise(vu, func() (any, error) { @@ -167,8 +167,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return s, nil }), nil - }, - "getByRole": func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByRole": passiveCall(func(role sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(role) { return nil, errors.New("missing required argument 'role'") } @@ -176,8 +176,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByRole(role.String(), popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByAltText": func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByAltText": passiveCall(func(alt sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(alt) { return nil, errors.New("missing required argument 'altText'") } @@ -185,8 +185,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByAltText(palt, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByLabel": func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByLabel": passiveCall(func(label sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(label) { return nil, errors.New("missing required argument 'label'") } @@ -194,8 +194,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByLabel(plabel, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByPlaceholder": func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByPlaceholder": passiveCall(func(placeholder sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(placeholder) { return nil, errors.New("missing required argument 'placeholder'") } @@ -203,8 +203,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByPlaceholder(pplaceholder, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTitle": func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByTitle": passiveCall(func(title sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(title) { return nil, errors.New("missing required argument 'title'") } @@ -212,8 +212,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByTitle(ptitle, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByTestId": func(testID sobek.Value) (*sobek.Object, error) { + }), + "getByTestId": passiveCall(func(testID sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(testID) { return nil, errors.New("missing required argument 'testId'") } @@ -221,8 +221,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByTestID(ptestID)) return rt.ToValue(ml).ToObject(rt), nil - }, - "getByText": func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { + }), + "getByText": passiveCall(func(text sobek.Value, opts sobek.Value) (*sobek.Object, error) { if k6common.IsNullish(text) { return nil, errors.New("missing required argument 'text'") } @@ -230,13 +230,14 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ml := mapLocator(vu, p.GetByText(ptext, popts)) return rt.ToValue(ml).ToObject(rt), nil - }, - "goto": func(url string, opts sobek.Value) (*sobek.Promise, error) { - gopts := common.NewFrameGotoOptions( + }), + "goto": networkCall(func(url string, opts sobek.Value) (*sobek.Promise, error) { + gopts, err := parseFrameGotoOptions( + rt, opts, p.Referrer(), p.NavigationTimeout(), ) - if err := gopts.Parse(vu.Context(), opts); err != nil { + if err != nil { return nil, fmt.Errorf("parsing page navigation options to %q: %w", url, err) } return promise(vu, func() (any, error) { @@ -247,128 +248,128 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return mapResponse(vu, resp), nil }), nil - }, - "hover": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameHoverOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "hover": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameHoverOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Hover(selector, popts) //nolint:wrapcheck }), nil - }, - "innerHTML": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInnerHTMLOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "innerHTML": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInnerHTMLOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner HTML options: %w", err) } return promise(vu, func() (any, error) { return p.InnerHTML(selector, popts) //nolint:wrapcheck }), nil - }, - "innerText": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInnerTextOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "innerText": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInnerTextOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing inner text options: %w", err) } return promise(vu, func() (any, error) { return p.InnerText(selector, popts) //nolint:wrapcheck }), nil - }, - "inputValue": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameInputValueOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "inputValue": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameInputValueOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing input value options: %w", err) } return promise(vu, func() (any, error) { return p.InputValue(selector, popts) //nolint:wrapcheck }), nil - }, - "isChecked": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsCheckedOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isChecked": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsCheckedOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parse isChecked options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsChecked(selector, popts) //nolint:wrapcheck }), nil - }, - "isClosed": p.IsClosed, - "isDisabled": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsDisabledOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isClosed": passiveCall(p.IsClosed), + "isDisabled": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsDisabledOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parse isDisabled options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsDisabled(selector, popts) //nolint:wrapcheck }), nil - }, - "isEditable": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsEditableOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isEditable": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsEditableOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parse isEditabled options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsEditable(selector, popts) //nolint:wrapcheck }), nil - }, - "isEnabled": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsEnabledOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isEnabled": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsEnabledOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parse isEnabled options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsEnabled(selector, popts) //nolint:wrapcheck }), nil - }, - "isHidden": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsHiddenOptions() - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isHidden": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsHiddenOptions(rt, opts) + if err != nil { return nil, fmt.Errorf("parse isHidden options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsHidden(selector, popts) //nolint:wrapcheck }), nil - }, - "isVisible": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameIsVisibleOptions() - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "isVisible": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameIsVisibleOptions(rt, opts) + if err != nil { return nil, fmt.Errorf("parsing isVisible options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return p.IsVisible(selector, popts) //nolint:wrapcheck }), nil - }, - "keyboard": mapKeyboard(vu, p.GetKeyboard()), - "locator": func(selector string, opts sobek.Value) *sobek.Object { + }), + "keyboard": newKeyboardMapping(vu, p.GetKeyboard()), + "locator": passiveCall(func(selector string, opts sobek.Value) *sobek.Object { ml := mapLocator(vu, p.Locator(selector, parseLocatorOptions(rt, opts))) return rt.ToValue(ml).ToObject(rt) - }, - "frameLocator": func(selector string) *sobek.Object { + }), + "frameLocator": passiveCall(func(selector string) *sobek.Object { mfl := mapFrameLocator(vu, p.FrameLocator(selector)) return rt.ToValue(mfl).ToObject(rt) - }, - "mainFrame": func() *sobek.Object { + }), + "mainFrame": passiveCall(func() *sobek.Object { mf := mapFrame(vu, p.MainFrame()) return rt.ToValue(mf).ToObject(rt) - }, - "mouse": mapMouse(vu, p.GetMouse()), - "on": mapPageOn(vu, p), - "opener": func() *sobek.Promise { + }), + "mouse": newMouseMapping(vu, p.GetMouse(), rt), + "on": passiveCall(mapPageOn(vu, p)), + "opener": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return p.Opener(), nil }) - }, - "press": func(selector string, key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFramePressOptions(p.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "press": networkCall(func(selector string, key string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFramePressOptions(rt, opts, p.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing press options of selector %q: %w", selector, err) } return promise(vu, func() (any, error) { return nil, p.Press(selector, key, popts) //nolint:wrapcheck }), nil - }, - "reload": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "reload": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewPageReloadOptions(common.LifecycleEventLoad, p.NavigationTimeout()) if err := popts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing reload options: %w", err) @@ -383,8 +384,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapResponse(vu, resp), nil }), nil - }, - "goBack": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "goBack": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewPageGoBackForwardOptions(common.LifecycleEventLoad, p.NavigationTimeout()) if err := popts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing page go back options: %w", err) @@ -399,8 +400,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapResponse(vu, resp), nil }), nil - }, - "goForward": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "goForward": networkCall(func(opts sobek.Value) (*sobek.Promise, error) { popts := common.NewPageGoBackForwardOptions(common.LifecycleEventLoad, p.NavigationTimeout()) if err := popts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing page go forward options: %w", err) @@ -415,9 +416,9 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapResponse(vu, resp), nil }), nil - }, - "route": mapPageRoute(vu, p), - "screenshot": func(opts sobek.Value) (*sobek.Promise, error) { + }), + "route": passiveCall(mapPageRoute(vu, p)), + "screenshot": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { popts, err := parsePageScreenshotOptions(rt, opts) if err != nil { return nil, fmt.Errorf("parsing page screenshot options: %w", err) @@ -440,10 +441,10 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }() return promise, nil - }, - "selectOption": func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSelectOptionOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "selectOption": networkCall(func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSelectOptionOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } @@ -454,36 +455,36 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (any, error) { return p.SelectOption(selector, convValues, popts) //nolint:wrapcheck }), nil - }, - "setChecked": func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setChecked": networkCall(func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameCheckOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame set check options: %w", err) } return promise(vu, func() (any, error) { return nil, p.SetChecked(selector, checked, popts) //nolint:wrapcheck }), nil - }, - "setContent": func(html string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetContentOptions(p.MainFrame().NavigationTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setContent": networkCall(func(html string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSetContentOptions(rt, opts, p.MainFrame().NavigationTimeout()) + if err != nil { return nil, fmt.Errorf("parsing setContent options: %w", err) } return promise(vu, func() (any, error) { return nil, p.SetContent(html, popts) //nolint:wrapcheck }), nil - }, - "setDefaultNavigationTimeout": p.SetDefaultNavigationTimeout, - "setDefaultTimeout": p.SetDefaultTimeout, - "setExtraHTTPHeaders": func(headers map[string]string) *sobek.Promise { + }), + "setDefaultNavigationTimeout": passiveCall(p.SetDefaultNavigationTimeout), + "setDefaultTimeout": passiveCall(p.SetDefaultTimeout), + "setExtraHTTPHeaders": passiveCall(func(headers map[string]string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, p.SetExtraHTTPHeaders(headers) //nolint:wrapcheck }) - }, - "setInputFiles": func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetInputFilesOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "setInputFiles": networkCall(func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameSetInputFilesOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } @@ -495,8 +496,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (any, error) { return nil, p.SetInputFiles(selector, pfiles, popts) //nolint:wrapcheck }), nil - }, - "setViewportSize": func(viewportSize sobek.Value) (*sobek.Promise, error) { + }), + "setViewportSize": networkCall(func(viewportSize sobek.Value) (*sobek.Promise, error) { s, err := parseSize(rt, viewportSize) if err != nil { return nil, fmt.Errorf("parsing viewport size: %w", err) @@ -504,19 +505,19 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (any, error) { return nil, p.SetViewportSize(s) //nolint:wrapcheck }), nil - }, - "tap": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTapOptions(p.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "tap": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTapOptions(rt, opts, p.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing page tap options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Tap(selector, popts) //nolint:wrapcheck }), nil - }, - "textContent": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTextContentOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "textContent": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTextContentOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing text content options: %w", err) } @@ -530,58 +531,58 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return s, nil }), nil - }, - "throttleCPU": func(cpuProfile common.CPUProfile) *sobek.Promise { + }), + "throttleCPU": passiveCall(func(cpuProfile common.CPUProfile) *sobek.Promise { return promise(vu, func() (any, error) { return nil, p.ThrottleCPU(cpuProfile) //nolint:wrapcheck }) - }, - "throttleNetwork": func(networkProfile common.NetworkProfile) *sobek.Promise { + }), + "throttleNetwork": passiveCall(func(networkProfile common.NetworkProfile) *sobek.Promise { return promise(vu, func() (any, error) { return nil, p.ThrottleNetwork(networkProfile) //nolint:wrapcheck }) - }, - "title": func() *sobek.Promise { + }), + "title": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return p.Title() //nolint:wrapcheck }) - }, - "touchscreen": mapTouchscreen(vu, p.GetTouchscreen()), - "type": func(selector string, text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTypeOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "touchscreen": newTouchscreenMapping(vu, p.GetTouchscreen()), + "type": networkCall(func(selector string, text string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameTypeOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return promise(vu, func() (any, error) { return nil, p.Type(selector, text, popts) //nolint:wrapcheck }), nil - }, - "uncheck": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameUncheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "uncheck": networkCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameUncheckOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame uncheck options %q: %w", selector, err) } return promise(vu, func() (any, error) { return nil, p.Uncheck(selector, popts) //nolint:wrapcheck }), nil - }, - "unroute": func(url string) (*sobek.Promise, error) { + }), + "unroute": passiveCall(func(url string) (*sobek.Promise, error) { return promise(vu, func() (any, error) { return nil, p.Unroute(url) }), nil - }, - "unrouteAll": func() (*sobek.Promise, error) { + }), + "unrouteAll": passiveCall(func() (*sobek.Promise, error) { return promise(vu, func() (any, error) { return nil, p.UnrouteAll() }), nil - }, - "url": p.URL, - "viewportSize": p.ViewportSize, - "waitForFunction": func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) { + }), + "url": passiveCall(p.URL), + "viewportSize": passiveCall(p.ViewportSize), + "waitForFunction": networkCall(func(pageFunc, opts sobek.Value, args ...sobek.Value) (*sobek.Promise, error) { js, popts, pargs, err := parseWaitForFunctionArgs( - vu.Context(), p.Timeout(), pageFunc, opts, args..., + rt, p.Timeout(), pageFunc, opts, args..., ) if err != nil { return nil, fmt.Errorf("page waitForFunction: %w", err) @@ -590,23 +591,23 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise(vu, func() (result any, reason error) { return p.WaitForFunction(js, popts, pargs...) //nolint:wrapcheck }), nil - }, - "waitForLoadState": func(state string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameWaitForLoadStateOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForLoadState": passiveCall(func(state string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameWaitForLoadStateOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing waitForLoadState %q options: %w", state, err) } return promise(vu, func() (any, error) { return nil, p.WaitForLoadState(state, popts) //nolint:wrapcheck }), nil - }, - "waitForNavigation": func(opts sobek.Value) (*sobek.Promise, error) { - return mapWaitForNavigation(vu, p, opts) - }, - "waitForSelector": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameWaitForSelectorOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + }), + "waitForNavigation": passiveCall(func(opts sobek.Value) (*sobek.Promise, error) { + return mapWaitForNavigation(rt, vu, p, opts) + }), + "waitForSelector": passiveCall(func(selector string, opts sobek.Value) (*sobek.Promise, error) { + popts, err := parseFrameWaitForSelectorOptions(rt, opts, p.MainFrame().Timeout()) + if err != nil { return nil, fmt.Errorf("parsing wait for selector %q options: %w", selector, err) } @@ -617,17 +618,17 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapElementHandle(vu, eh), nil }), nil - }, - "waitForTimeout": func(timeout int64) *sobek.Promise { + }), + "waitForTimeout": passiveCall(func(timeout int64) *sobek.Promise { return promise(vu, func() (any, error) { p.WaitForTimeout(timeout) return nil, nil }) - }, - "waitForURL": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - return mapWaitForURL(vu, p, url, opts) - }, - "waitForResponse": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + }), + "waitForURL": passiveCall(func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + return mapWaitForURL(rt, vu, p, url, opts) + }), + "waitForResponse": passiveCall(func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { popts, err := parsePageWaitForResponseOptions(vu.Context(), opts, p.Timeout()) if err != nil { return nil, fmt.Errorf("parsing waitForResponse options: %w", err) @@ -647,8 +648,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop defer stop() return p.WaitForResponse(val, popts, newRegExMatcher(ctx, vu, tq)) }), nil - }, - "waitForRequest": func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { + }), + "waitForRequest": passiveCall(func(url sobek.Value, opts sobek.Value) (*sobek.Promise, error) { popts, err := parsePageWaitForRequestOptions(vu.Context(), opts, p.Timeout()) if err != nil { return nil, fmt.Errorf("parsing waitForRequest options: %w", err) @@ -668,8 +669,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop defer stop() return p.WaitForRequest(val, popts, newRegExMatcher(ctx, vu, tq)) }), nil - }, - "waitForEvent": func(event common.PageEventName, opts sobek.Value) (*sobek.Promise, error) { + }), + "waitForEvent": passiveCall(func(event common.PageEventName, opts sobek.Value) (*sobek.Promise, error) { // AVOID using a default case to force handling new event types explicitly // so that the linter can catch unhandled event types as non-exhaustive switch. // Otherwise, we might miss mapping new [PageEvent] types added in the future. @@ -700,12 +701,14 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop ctx := vu.Context() tq := vu.get(ctx, p.TargetID()) + captured := k6common.CaptureMetricContext(vu.State()) + return promise(vu, func() (any, error) { rpe, err := p.WaitForEvent(event, popts, func(pe common.PageEvent) (bool, error) { if fn == nil { return true, nil } - return queueTask(ctx, tq, func() (bool, error) { + return queueTaskWithMetricContext(ctx, tq, captured, func() (bool, error) { m, err := mapPageEvent(vu, pe) if err != nil { return false, err @@ -722,8 +725,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mapPageEvent(vu, rpe) }), nil - }, - "workers": func() *sobek.Object { + }), + "workers": passiveCall(func() *sobek.Object { workers := p.Workers() mws := make([]mapping, 0, len(workers)) for _, w := range workers { @@ -731,9 +734,9 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop mws = append(mws, mw) } return rt.ToValue(mws).ToObject(rt) - }, + }), } - maps["$"] = func(selector string) *sobek.Promise { + maps["$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { eh, err := p.Query(selector) if err != nil { @@ -749,8 +752,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return ehm, nil }) - } - maps["$$"] = func(selector string) *sobek.Promise { + }) + maps["$$"] = passiveCall(func(selector string) *sobek.Promise { return promise(vu, func() (any, error) { ehs, err := p.QueryAll(selector) if err != nil { @@ -763,9 +766,11 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return mehs, nil }) - } + }) - return maps + // CDP request events run outside the event loop, so keep the context of each mapped call available + // until its returned promise settles. Loader-associated requests take precedence when possible. + return withPageNetworkCalls(vu, p, maps) } // mapPageOn enables using various page.on event handlers with the page.on method. @@ -794,9 +799,10 @@ func mapPageOn(vu moduleVU, p *common.Page) func(common.PageEventName, sobek.Cal ctx := vu.Context() tq := vu.get(ctx, p.TargetID()) + captured := k6common.CaptureMetricContext(vu.State()) return p.On(eventName, func(event common.PageEvent) error { - wait := queueTask(ctx, tq, func() (sobek.Value, error) { + wait := queueTaskWithMetricContext(ctx, tq, captured, func() (sobek.Value, error) { _, err := handle(sobek.Undefined(), vu.Runtime().ToValue(pageEvent.mapp(vu, event))) if err != nil { return nil, fmt.Errorf("executing page.on('%s') handler: %w", eventName, err) @@ -814,10 +820,9 @@ func mapPageOn(vu moduleVU, p *common.Page) func(common.PageEventName, sobek.Cal } func parseWaitForFunctionArgs( - ctx context.Context, timeout time.Duration, pageFunc, opts sobek.Value, gargs ...sobek.Value, + rt *sobek.Runtime, timeout time.Duration, pageFunc, opts sobek.Value, gargs ...sobek.Value, ) (string, *common.FrameWaitForFunctionOptions, []any, error) { - popts := common.NewFrameWaitForFunctionOptions(timeout) - err := popts.Parse(ctx, opts) + popts, err := parseFrameWaitForFunctionOptions(rt, opts, timeout) if err != nil { return "", nil, nil, fmt.Errorf("parsing waitForFunction options: %w", err) } @@ -831,6 +836,40 @@ func parseWaitForFunctionArgs( return js, popts, exportArgs(gargs), nil } +// parseFrameWaitForFunctionOptions parses the frame waitForFunction options from a Sobek value. +func parseFrameWaitForFunctionOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameWaitForFunctionOptions, error) { + wfopts := common.NewFrameWaitForFunctionOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return wfopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + v := obj.Get(k) + switch k { + case "timeout": + wfopts.Timeout = time.Duration(v.ToInteger()) * time.Millisecond + case "polling": + switch v.ExportType().Kind() { + case reflect.Int64: + wfopts.Polling = common.PollingInterval + wfopts.Interval = v.ToInteger() + case reflect.String: + if p, ok := common.PollingIDFromString(v.ToString().String()); ok { + wfopts.Polling = p + break + } + fallthrough + default: + return wfopts, fmt.Errorf("wrong polling option value: %q; "+ + `possible values: "raf", "mutation" or number`, v) + } + } + } + return wfopts, nil +} + // parseStringOrRegex parses a sobek.Value to return either a quoted string if it was a string, // or a raw string if it was a JS RegExp object or another type. // @@ -940,9 +979,10 @@ func mapPageRoute(vu moduleVU, p *common.Page) func(sobek.Value, sobek.Callable) ppath := parseStringOrRegex(path, false) tq := vu.get(ctx, p.TargetID()) + captured := k6common.CaptureMetricContext(vu.State()) route := func(r *common.Route) error { - _, err := queueTask(ctx, tq, func() (any, error) { + _, err := queueTaskWithMetricContext(ctx, tq, captured, func() (any, error) { return cb(sobek.Undefined(), vu.Runtime().ToValue(mapRoute(vu, r))) })() if errors.Is(err, context.Canceled) { @@ -960,7 +1000,7 @@ func mapPageRoute(vu moduleVU, p *common.Page) func(sobek.Value, sobek.Callable) } } -func mapWaitForURL(vu moduleVU, target interface { +func mapWaitForURL(rt *sobek.Runtime, vu moduleVU, target interface { Timeout() time.Duration WaitForURL(urlPattern string, opts *common.FrameWaitForURLOptions, rm common.RegExMatcher) error }, url sobek.Value, opts sobek.Value, @@ -968,8 +1008,8 @@ func mapWaitForURL(vu moduleVU, target interface { if k6common.IsNullish(url) { return nil, errors.New("missing required argument 'url'") } - popts := common.NewFrameWaitForURLOptions(target.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameWaitForURLOptions(rt, opts, target.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing waitForURL options: %w", err) } @@ -982,13 +1022,36 @@ func mapWaitForURL(vu moduleVU, target interface { }), nil } -func mapWaitForNavigation(vu moduleVU, target interface { +// parseFrameWaitForURLOptions parses the frame waitForURL options from a Sobek value. +func parseFrameWaitForURLOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameWaitForURLOptions, error) { + wuopts := common.NewFrameWaitForURLOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return wuopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "timeout": + wuopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "waitUntil": + lifeCycle := obj.Get(k).String() + if err := wuopts.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { + return wuopts, fmt.Errorf("parsing waitForURL options: %w", err) + } + } + } + return wuopts, nil +} + +func mapWaitForNavigation(rt *sobek.Runtime, vu moduleVU, target interface { Timeout() time.Duration WaitForNavigation(*common.FrameWaitForNavigationOptions, common.RegExMatcher) (*common.Response, error) }, opts sobek.Value, ) (*sobek.Promise, error) { - popts := common.NewFrameWaitForNavigationOptions(target.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameWaitForNavigationOptions(rt, opts, target.Timeout()) + if err != nil { return nil, fmt.Errorf("parsing frame wait for navigation options: %w", err) } @@ -1012,6 +1075,39 @@ func mapWaitForNavigation(vu moduleVU, target interface { }), nil } +// parseFrameWaitForNavigationOptions parses the frame waitForNavigation options from a Sobek value. +func parseFrameWaitForNavigationOptions( + rt *sobek.Runtime, opts sobek.Value, defaultTimeout time.Duration, +) (*common.FrameWaitForNavigationOptions, error) { + wnopts := common.NewFrameWaitForNavigationOptions(defaultTimeout) + if k6common.IsNullish(opts) { + return wnopts, nil + } + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "url": + var val string + switch obj.Get(k).ExportType() { + case reflect.TypeFor[string](): + val = fmt.Sprintf("'%s'", obj.Get(k).String()) // Strings require quotes + default: // JS Regex, CSS, numbers or booleans + val = obj.Get(k).String() // No quotes + } + + wnopts.URL = val + case "timeout": + wnopts.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "waitUntil": + lifeCycle := obj.Get(k).String() + if err := wnopts.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { + return wnopts, fmt.Errorf("parsing waitForNavigation options: %w", err) + } + } + } + return wnopts, nil +} + func parsePageWaitForResponseOptions( ctx context.Context, opts sobek.Value, defaultTimeout time.Duration, ) (*common.PageWaitForResponseOptions, error) { diff --git a/internal/js/modules/k6/browser/browser/registry.go b/internal/js/modules/k6/browser/browser/registry.go index d6f5befdd..1ddc54f2e 100644 --- a/internal/js/modules/k6/browser/browser/registry.go +++ b/internal/js/modules/k6/browser/browser/registry.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "math/big" + "slices" "strconv" "strings" "sync" @@ -178,6 +179,12 @@ type browserRegistry struct { mu sync.RWMutex m map[int64]*common.Browser + // userManaged holds browsers created via chromium.connectOverCDP, keyed by + // iteration. Unlike m (browsers k6 launches/connects from scenario options), + // these are connected by the script at runtime. k6 still sweeps them on + // IterEnd and Exit. A slice (not a single slot) so a repeat connect in the + // same iteration does not clobber the first. + userManaged map[int64][]*common.Browser buildFn browserBuildFunc } @@ -229,6 +236,7 @@ func newBrowserRegistry( vu: vu, tracesMetadata: tracesMetadata, m: make(map[int64]*common.Browser), + userManaged: make(map[int64][]*common.Browser), buildFn: builder, } @@ -259,19 +267,20 @@ func (r *browserRegistry) handleIterEvents( ) for e := range eventsCh { - // If browser module is imported in the test, NewModuleInstance will be called for - // every VU. Because on VU init stage we can not distinguish to which scenario it - // belongs or access its options (because state is nil), we have to always subscribe - // to each VU iter events, including VUs that do not make use of the browser in their - // iterations. - // Therefore, if we get an event that does not correspond to a browser iteration, then - // skip this iteration. We can't just unsubscribe as the VU might be reused in a later - // scenario that does have browser setup. - // TODO try to maybe do this only once per scenario - if !isBrowserIter(r.vu) { - e.Done() - continue - } + // If the browser module is imported, NewModuleInstance runs for every VU, so + // this registry subscribes to iteration events for every VU. At VU init we + // cannot tell which scenario a VU belongs to (state is nil), so we cannot + // subscribe selectively; we also cannot unsubscribe later, since a VU may be + // reused by a later scenario that does use the browser. + // + // This handler used to skip non-browser iterations entirely (an early + // `continue` when !isBrowserIter), because only managed browser scenarios had + // anything to clean up. That is no longer safe: chromium.connectOverCDP lets a + // script create a browser in any iteration, without options.browser. So we now + // process every iteration event and gate only the managed-browser work + // (buildFn on IterStart, deleteBrowser on IterEnd) behind isBrowserIter below; + // the user-managed-browser sweep (closeUserManaged) and endIterationTrace run + // unconditionally on IterEnd. // The context in the VU is not thread safe. It can // be safely accessed during an iteration but not @@ -288,8 +297,17 @@ func (r *browserRegistry) handleIterEvents( continue } + browserIter := isBrowserIter(r.vu) + switch e.Type { case k6event.IterStart: + // Managed browsers are only built for browser scenarios. Connected + // (connectOverCDP) browsers are built lazily at connection time, so + // there is nothing to do at IterStart for them. + if !browserIter { + break + } + // Because VU.State is nil when browser registry is initialized, // we have to initialize traces registry on the first VU iteration // so we can get access to the k6 TracerProvider. @@ -314,8 +332,16 @@ func (r *browserRegistry) handleIterEvents( } r.setBrowser(data.Iteration, b) case k6event.IterEnd: - r.deleteBrowser(data.Iteration) - r.tr.endIterationTrace(data.Iteration) + // Always sweep user-managed browsers and end any iteration trace, + // even for non-browser iterations (connectOverCDP runs in + // iterations without options.browser). + if browserIter { + r.deleteBrowser(data.Iteration) + } + r.closeUserManaged(data.Iteration) + if r.tr != nil { + r.tr.endIterationTrace(data.Iteration) + } default: r.vu.State().Logger.Warnf("received unexpected event type: %v", e.Type) } @@ -397,6 +423,63 @@ func (r *browserRegistry) clear() { b.Close() delete(r.m, id) } + for iter, browsers := range r.userManaged { + for _, b := range browsers { + b.Close() + } + delete(r.userManaged, iter) + } +} + +func (r *browserRegistry) trackUserManagedBrowser(iter int64, b *common.Browser) { + r.mu.Lock() + defer r.mu.Unlock() + + r.userManaged[iter] = append(r.userManaged[iter], b) +} + +func (r *browserRegistry) untrackUserManagedBrowser(iter int64, b *common.Browser) { + r.mu.Lock() + defer r.mu.Unlock() + + s := r.userManaged[iter] + for i, x := range s { + if x == b { + r.userManaged[iter] = append(s[:i], s[i+1:]...) + break + } + } + if len(r.userManaged[iter]) == 0 { + delete(r.userManaged, iter) + } +} + +// userManagedIter returns the iteration a user-managed browser is tracked under +// and whether it is tracked at all. It's used to decide whether a browser +// mapping should expose close() (only user-managed browsers do). +func (r *browserRegistry) userManagedIter(b *common.Browser) (int64, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + + for iter, browsers := range r.userManaged { + if slices.Contains(browsers, b) { + return iter, true + } + } + return 0, false +} + +// closeUserManaged gracefully closes and removes all user-managed browsers for iter. +// Close is done outside the lock because it performs CDP round-trips. +func (r *browserRegistry) closeUserManaged(iter int64) { + r.mu.Lock() + browsers := r.userManaged[iter] + delete(r.userManaged, iter) + r.mu.Unlock() + + for _, b := range browsers { + b.Close() + } } // initTracesRegistry must only be called within an iteration execution, @@ -412,6 +495,26 @@ func (r *browserRegistry) initTracesRegistry() { }) } +// startConnectTrace ensures the per-VU traces registry exists and reuses or +// starts the iteration root span for iter, mirroring the IterStart path used by +// managed browsers. It returns a context carrying both the tracer and the span, +// so browser API calls made over a connectOverCDP browser parent under the +// iteration trace. +func (r *browserRegistry) startConnectTrace(vuCtx context.Context, iter int64) context.Context { + r.initTracesRegistry() + + tracerCtx := common.WithTracer(vuCtx, r.tr.tracer) + // startIterationTrace already reuses an existing span for the same + // iteration, so a repeat connect does not create a second root span. + // Fill in the VU and scenario so connect-only iteration spans carry the + // same test.vu/test.scenario attributes as the managed IterStart path. + return r.tr.startIterationTrace(tracerCtx, k6event.IterData{ + Iteration: iter, + VUID: r.vu.State().VUID, + ScenarioName: k6ext.GetScenarioName(vuCtx), + }) +} + func (r *browserRegistry) stopTracesRegistry() { // Because traces registry is initialized on iterStart event, it is not // initialized for the initial NewModuleInstance call, whose VU does not diff --git a/internal/js/modules/k6/browser/browser/registry_test.go b/internal/js/modules/k6/browser/browser/registry_test.go index 537d4c16d..b04c7d7fd 100644 --- a/internal/js/modules/k6/browser/browser/registry_test.go +++ b/internal/js/modules/k6/browser/browser/registry_test.go @@ -9,7 +9,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" "go.k6.io/k6/v2/internal/js/modules/k6/browser/env" "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" @@ -317,6 +319,66 @@ func TestBrowserRegistry(t *testing.T) { // Verify there are no browsers left assert.Equal(t, 0, browserRegistry.browserCount()) }) + + // IterEnd must be a no-op for a non-browser iteration that never called + // chromium.connectOverCDP: nothing was built (IterStart is skipped), nothing + // is tracked in userManaged, and the traces registry was never initialized. + // This locks in the `if r.tr != nil` guard and the unconditional + // closeUserManaged sweep on IterEnd — without them, IterEnd would panic here. + t.Run("iterend_noop_on_bare_non_browser_iter", func(t *testing.T) { + t.Parallel() + + var ( + vu = k6test.NewVU(t) + browserRegistry = newBrowserRegistry(context.Background(), vu, remoteRegistry, &pidRegistry{}, nil) + ) + + vu.ActivateVU() + + // Unset the browser type option so this represents a non-browser VU. + delete(vu.StateField.Options.Scenarios["default"].GetScenarioOptions().Browser, "type") + + vu.StartIteration(t, k6test.WithIteration(0)) + + // Nothing built and no traces registry: IterStart is skipped entirely for + // a non-browser iter, and connectOverCDP (which would init the registry) + // was never called. + require.Equal(t, 0, browserRegistry.browserCount()) + require.Nil(t, browserRegistry.tr, "traces registry must not be initialized for a bare non-browser iter") + + // The regression under test: IterEnd reaches closeUserManaged (nothing + // tracked) and the trace guard (r.tr is nil). Neither may panic. + require.NotPanics(t, func() { + vu.EndIteration(t, k6test.WithIteration(0)) + }) + + require.Equal(t, 0, browserRegistry.browserCount()) + require.Nil(t, browserRegistry.tr, "traces registry must remain uninitialized after IterEnd") + }) +} + +func TestStartConnectTraceAttributes(t *testing.T) { + t.Parallel() + + rec := k6test.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(rec)) + + vu := k6test.NewVU(t, k6test.WithTracerProvider(tp)) + vu.ActivateVU() + vu.StartIteration(t) + vu.State().VUID = 42 // non-zero so the test.vu assertion is meaningful + + r := &browserRegistry{ + vu: vu, + m: make(map[int64]*common.Browser), + userManaged: make(map[int64][]*common.Browser), + } + r.startConnectTrace(vu.Context(), vu.State().Iteration) + + span, ok := rec.Find("iteration") + require.True(t, ok, "expected an 'iteration' root span") + require.Equal(t, int64(42), span.AttrInt64(t, "test.vu")) + require.Equal(t, "default", span.AttrString(t, "test.scenario")) } func TestParseTracesMetadata(t *testing.T) { diff --git a/internal/js/modules/k6/browser/browser/request_mapping.go b/internal/js/modules/k6/browser/browser/request_mapping.go index 9c179a385..430d040ea 100644 --- a/internal/js/modules/k6/browser/browser/request_mapping.go +++ b/internal/js/modules/k6/browser/browser/request_mapping.go @@ -16,17 +16,19 @@ func mapRequestEvent(vu moduleVU, event common.PageEvent) mapping { func mapRequest(vu moduleVU, r *common.Request) mapping { rt := vu.Runtime() maps := mapping{ - "allHeaders": func() *sobek.Promise { + "allHeaders": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() return r.AllHeaders(), nil }) - }, - "frame": func() *sobek.Object { + }), + "frame": passiveCall(func() *sobek.Object { mf := mapFrame(vu, r.Frame()) return rt.ToValue(mf).ToObject(rt) - }, - "headerValue": func(name string) *sobek.Promise { + }), + "headerValue": passiveCall(func(name string) *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() v, ok := r.HeaderValue(name) if !ok { return nil, nil @@ -34,31 +36,32 @@ func mapRequest(vu moduleVU, r *common.Request) mapping { return v, nil }) - }, - "headers": r.Headers, - "headersArray": func() *sobek.Promise { + }), + "headers": passiveCall(r.Headers), + "headersArray": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() return r.HeadersArray(), nil }) - }, - "isNavigationRequest": r.IsNavigationRequest, - "method": r.Method, - "postData": func() any { + }), + "isNavigationRequest": passiveCall(r.IsNavigationRequest), + "method": passiveCall(r.Method), + "postData": passiveCall(func() any { p := r.PostData() if p == "" { return nil } return p - }, - "postDataBuffer": func() any { + }), + "postDataBuffer": passiveCall(func() any { p := r.PostDataBuffer() if len(p) == 0 { return nil } return rt.NewArrayBuffer(p) - }, - "resourceType": r.ResourceType, - "response": func() *sobek.Promise { + }), + "resourceType": passiveCall(r.ResourceType), + "response": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { resp := r.Response() if resp == nil { @@ -66,12 +69,12 @@ func mapRequest(vu moduleVU, r *common.Request) mapping { } return mapResponse(vu, resp), nil }) - }, - "size": r.Size, - "timing": r.Timing, - "url": r.URL, - "failure": r.Failure, + }), + "size": passiveCall(r.Size), + "timing": passiveCall(r.Timing), + "url": passiveCall(r.URL), + "failure": passiveCall(r.Failure), } - return maps + return finishMapping(maps) } diff --git a/internal/js/modules/k6/browser/browser/response_mapping.go b/internal/js/modules/k6/browser/browser/response_mapping.go index 404e3a94c..3cf494a38 100644 --- a/internal/js/modules/k6/browser/browser/response_mapping.go +++ b/internal/js/modules/k6/browser/browser/response_mapping.go @@ -18,12 +18,13 @@ func mapResponse(vu moduleVU, r *common.Response) mapping { return nil } maps := mapping{ - "allHeaders": func() *sobek.Promise { + "allHeaders": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() return r.AllHeaders(), nil }) - }, - "body": func() *sobek.Promise { + }), + "body": passiveCall(func() *sobek.Promise { rt := vu.Runtime() promise, res, rej := rt.NewPromise() callback := vu.RegisterCallback() @@ -41,63 +42,66 @@ func mapResponse(vu moduleVU, r *common.Response) mapping { }) }() return promise - }, - "frame": func() mapping { + }), + "frame": passiveCall(func() mapping { return mapFrame(vu, r.Frame()) - }, - "headerValue": func(name string) *sobek.Promise { + }), + "headerValue": passiveCall(func(name string) *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() v, ok := r.HeaderValue(name) if !ok { return nil, nil } return v, nil }) - }, - "headerValues": func(name string) *sobek.Promise { + }), + "headerValues": passiveCall(func(name string) *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() return r.HeaderValues(name), nil }) - }, - "headers": r.Headers, - "headersArray": func() *sobek.Promise { + }), + "headers": passiveCall(r.Headers), + "headersArray": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { + r.WaitForRawHeaders() return r.HeadersArray(), nil }) - }, - "json": func() *sobek.Promise { + }), + "json": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return r.JSON() //nolint: wrapcheck }) - }, - "ok": r.Ok, - "request": func() mapping { + }), + "ok": passiveCall(r.Ok), + "request": passiveCall(func() mapping { return mapRequest(vu, r.Request()) - }, - "securityDetails": func() *sobek.Promise { + }), + "securityDetails": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return r.SecurityDetails(), nil }) - }, - "serverAddr": func() *sobek.Promise { + }), + "serverAddr": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return r.ServerAddr(), nil }) - }, - "size": func() *sobek.Promise { + }), + "size": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return r.Size(), nil }) - }, - "status": r.Status, - "statusText": r.StatusText, - "url": r.URL, - "text": func() *sobek.Promise { + }), + "status": passiveCall(r.Status), + "statusText": passiveCall(r.StatusText), + "url": passiveCall(r.URL), + "text": passiveCall(func() *sobek.Promise { return promise(vu, func() (any, error) { return r.Text() //nolint:wrapcheck }) - }, + }), } - return maps + return finishMapping(maps) } diff --git a/internal/js/modules/k6/browser/browser/route_mapping.go b/internal/js/modules/k6/browser/browser/route_mapping.go index 44b099201..6eac4181a 100644 --- a/internal/js/modules/k6/browser/browser/route_mapping.go +++ b/internal/js/modules/k6/browser/browser/route_mapping.go @@ -13,13 +13,13 @@ import ( // mapRoute to the JS module. func mapRoute(vu moduleVU, route *common.Route) mapping { - return mapping{ - "abort": func(reason string) *sobek.Promise { + return finishMapping(mapping{ + "abort": passiveCall(func(reason string) *sobek.Promise { return promise(vu, func() (any, error) { return nil, route.Abort(reason) }) - }, - "continue": func(opts sobek.Value) *sobek.Promise { + }), + "continue": passiveCall(func(opts sobek.Value) *sobek.Promise { copts, err := parseContinueOptions(vu.Context(), opts) return promise(vu, func() (any, error) { if err != nil { @@ -27,8 +27,8 @@ func mapRoute(vu moduleVU, route *common.Route) mapping { } return nil, route.Continue(copts) }) - }, - "fulfill": func(opts sobek.Value) *sobek.Promise { + }), + "fulfill": passiveCall(func(opts sobek.Value) *sobek.Promise { fopts, err := parseFulfillOptions(vu.Context(), opts) return promise(vu, func() (any, error) { if err != nil { @@ -36,11 +36,11 @@ func mapRoute(vu moduleVU, route *common.Route) mapping { } return nil, route.Fulfill(fopts) }) - }, - "request": func() mapping { + }), + "request": passiveCall(func() mapping { return mapRequest(vu, route.Request()) - }, - } + }), + }) } func parseContinueOptions(ctx context.Context, opts sobek.Value) (common.ContinueOptions, error) { diff --git a/internal/js/modules/k6/browser/browser/touchscreen_mapping.go b/internal/js/modules/k6/browser/browser/touchscreen_mapping.go index 01ea47aa6..275ab78d4 100644 --- a/internal/js/modules/k6/browser/browser/touchscreen_mapping.go +++ b/internal/js/modules/k6/browser/browser/touchscreen_mapping.go @@ -8,11 +8,15 @@ import ( // mapTouchscreen to the JS module. func mapTouchscreen(vu moduleVU, ts *common.Touchscreen) mapping { + return finishMapping(newTouchscreenMapping(vu, ts)) +} + +func newTouchscreenMapping(vu moduleVU, ts *common.Touchscreen) mapping { return mapping{ - "tap": func(x float64, y float64) *sobek.Promise { + "tap": networkCall(func(x float64, y float64) *sobek.Promise { return promise(vu, func() (result any, reason error) { return nil, ts.Tap(x, y) //nolint:wrapcheck }) - }, + }), } } diff --git a/internal/js/modules/k6/browser/browser/worker_mapping.go b/internal/js/modules/k6/browser/browser/worker_mapping.go index 6c94cef6f..4d5732423 100644 --- a/internal/js/modules/k6/browser/browser/worker_mapping.go +++ b/internal/js/modules/k6/browser/browser/worker_mapping.go @@ -6,7 +6,7 @@ import ( // mapWorker to the JS module. func mapWorker(_ moduleVU, w *common.Worker) mapping { - return mapping{ + return finishMapping(mapping{ "url": w.URL(), - } + }) } diff --git a/internal/js/modules/k6/browser/chromium/browser_type.go b/internal/js/modules/k6/browser/chromium/browser_type.go index cf900290f..3acc85ced 100644 --- a/internal/js/modules/k6/browser/chromium/browser_type.go +++ b/internal/js/modules/k6/browser/chromium/browser_type.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "math/rand" // nosemgrep: math-random-used // This is used to generate id for easier debugging + "net/url" "os/exec" "path/filepath" "sort" @@ -51,8 +52,28 @@ func NewBrowserType(vu k6modules.VU) *BrowserType { } } +// Clone returns a new BrowserType that reuses the receiver's already +// initialized, thread-safe fields (vu, hooks, k6Metrics, envLookupper) but +// gets its own randSrc and Ctx. +// +// This exists, so callers that need a fresh BrowserType per operation (e.g., +// concurrent ConnectOverCDP calls in the same iteration) don't have to go +// through NewBrowserType, which calls vu.InitEnv() and therefore only works +// during the script init scope, not during an iteration. +// Sharing a single BrowserType across concurrent operations instead would race +// on randSrc (used in initContext) and on the Ctx field (written in Connect). +func (b *BrowserType) Clone() *BrowserType { + return &BrowserType{ + vu: b.vu, + hooks: b.hooks, + k6Metrics: b.k6Metrics, + randSrc: rand.New(rand.NewSource(time.Now().UnixNano())), //nolint: gosec + envLookupper: b.envLookupper, + } +} + func (b *BrowserType) init( - ctx context.Context, isRemoteBrowser bool, + ctx context.Context, isRemoteBrowser bool, scenarioOpts map[string]any, ) (context.Context, *common.BrowserOptions, *log.Logger, error) { ctx = b.initContext(ctx) @@ -68,8 +89,7 @@ func (b *BrowserType) init( browserOpts = common.NewLocalBrowserOptions() } - opts := k6ext.GetScenarioOpts(b.vu.Context(), b.vu) - if err = browserOpts.Parse(ctx, logger, opts, b.envLookupper); err != nil { + if err = browserOpts.Parse(ctx, logger, scenarioOpts, b.envLookupper); err != nil { return nil, nil, nil, fmt.Errorf("error parsing browser options: %w", err) } ctx = common.WithBrowserOptions(ctx, browserOpts) @@ -92,6 +112,67 @@ func (b *BrowserType) initContext(ctx context.Context) context.Context { return ctx } +// ConnectOverCDP attaches the k6 browser to an existing, user-managed browser +// over CDP, without requiring scenario browser options. +// +// The passed context must be the context the caller wants to control both the +// connection and the browser lifetime; when it is canceled (iteration ends), +// the connection is torn down. +func (b *BrowserType) ConnectOverCDP(ctx context.Context, wsEndpoint string) (*common.Browser, error) { + // Validate wsEndpoint up front so an empty or malformed value + // fails fast with a clear error, instead of a confusing lower-level + // connection error. + if err := validateWSEndpoint(wsEndpoint); err != nil { + return nil, err + } + + // Parse browser options from the environment (K6_BROWSER_TIMEOUT, + // K6_BROWSER_DEBUG, the log category filter, ...) like Connect does. Pass a + // fixed chromium type instead of scenario options: connectOverCDP works + // without a browser scenario, and Parse requires the type to be set. + ctx, browserOpts, logger, err := b.init(ctx, true, map[string]any{"type": "chromium"}) + if err != nil { + return nil, fmt.Errorf("initializing browser type: %w", err) + } + + bp, err := b.connect(ctx, ctx, wsEndpoint, browserOpts, logger) + if err != nil { + err = &k6ext.UserFriendlyError{ + Err: err, + Timeout: browserOpts.Timeout, + } + return nil, fmt.Errorf("%w", err) + } + + return bp, nil +} + +// validateWSEndpoint returns an error if wsEndpoint is not a usable CDP +// WebSocket URL, catching common mistakes (an empty value, a non-ws scheme, +// a missing host, etc.) before we attempt to connect. +func validateWSEndpoint(wsEndpoint string) error { + if strings.TrimSpace(wsEndpoint) == "" { + return errors.New("WebSocket endpoint cannot be empty") + } + + u, err := url.Parse(wsEndpoint) + if err != nil { + return fmt.Errorf("invalid WebSocket endpoint %q: %w", wsEndpoint, err) + } + + if u.Scheme != "ws" && u.Scheme != "wss" { + return fmt.Errorf( + "invalid WebSocket endpoint %q: scheme must be ws or wss, got %q", wsEndpoint, u.Scheme, + ) + } + + if u.Hostname() == "" { + return fmt.Errorf("invalid WebSocket endpoint %q: host is missing", wsEndpoint) + } + + return nil +} + // Connect attaches k6 browser to an existing browser instance. // // vuCtx is the context coming from the VU itself. The k6 vu/iteration controls @@ -104,7 +185,7 @@ func (b *BrowserType) initContext(ctx context.Context) context.Context { // the iteration to end (e.g. during a SIGTERM) and unblocks k6 to then fire off // the events which allows the connection to close. func (b *BrowserType) Connect(ctx, vuCtx context.Context, wsEndpoint string) (*common.Browser, error) { - vuCtx, browserOpts, logger, err := b.init(vuCtx, true) + vuCtx, browserOpts, logger, err := b.init(vuCtx, true, k6ext.GetScenarioOpts(b.vu.Context(), b.vu)) if err != nil { return nil, fmt.Errorf("initializing browser type: %w", err) } @@ -170,7 +251,7 @@ func (b *BrowserType) link( // the iteration to end (e.g. during a SIGTERM) and unblocks k6 to then fire off // the events which allows the chromium subprocess to shutdown. func (b *BrowserType) Launch(ctx, vuCtx context.Context) (_ *common.Browser, browserProcessID int, _ error) { - vuCtx, browserOpts, logger, err := b.init(vuCtx, false) + vuCtx, browserOpts, logger, err := b.init(vuCtx, false, k6ext.GetScenarioOpts(b.vu.Context(), b.vu)) if err != nil { return nil, 0, fmt.Errorf("initializing browser type: %w", err) } @@ -190,7 +271,7 @@ func (b *BrowserType) Launch(ctx, vuCtx context.Context) (_ *common.Browser, bro func (b *BrowserType) launch( ctx, vuCtx context.Context, opts *common.BrowserOptions, logger *log.Logger, ) (_ *common.Browser, pid int, _ error) { - flags, err := prepareFlags(opts, &(b.vu.State()).Options) + flags, err := prepareFlags(opts, &b.vu.State().Options) if err != nil { return nil, 0, fmt.Errorf("%w", err) } diff --git a/internal/js/modules/k6/browser/chromium/browser_type_test.go b/internal/js/modules/k6/browser/chromium/browser_type_test.go index a014bbfde..af1836d1c 100644 --- a/internal/js/modules/k6/browser/chromium/browser_type_test.go +++ b/internal/js/modules/k6/browser/chromium/browser_type_test.go @@ -1,6 +1,7 @@ package chromium import ( + "context" "io/fs" "net" "path/filepath" @@ -9,6 +10,7 @@ import ( "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" "go.k6.io/k6/v2/internal/js/modules/k6/browser/env" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" k6lib "go.k6.io/k6/v2/lib" k6types "go.k6.io/k6/v2/lib/types" @@ -17,6 +19,56 @@ import ( "github.com/stretchr/testify/require" ) +// TestBrowserTypeInitParsesEnvWithoutScenario locks in the connectOverCDP use +// case: a plain script has no options.browser, so init must parse browser +// options from the environment using only the fixed chromium type that +// ConnectOverCDP passes, without requiring scenario options. +func TestBrowserTypeInitParsesEnvWithoutScenario(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + bt := NewBrowserType(vu) + vu.ActivateVU() + + _, opts, _, err := bt.init(context.Background(), true, map[string]any{"type": "chromium"}) + require.NoError(t, err) + require.NotNil(t, opts) +} + +// TestValidateWSEndpoint pins the fast-fail validation for connectOverCDP +// endpoints. +func TestValidateWSEndpoint(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + endpoint string + wantErr string // substring; empty means the endpoint must be accepted + }{ + {"valid ws", "ws://localhost:9222", ""}, + {"valid wss with path", "wss://example.com/devtools/browser/abc", ""}, + {"empty", "", "cannot be empty"}, + {"blank", " ", "cannot be empty"}, + {"wrong scheme", "http://localhost:9222", "scheme must be ws or wss"}, + {"missing scheme", "localhost:9222", "scheme must be ws or wss"}, + {"no host with path", "ws:///devtools/browser/id", "host is missing"}, + {"opaque no host", "ws:localhost:9222", "host is missing"}, + {"port only no host", "ws://:9222", "host is missing"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + err := validateWSEndpoint(tc.endpoint) + if tc.wantErr == "" { + require.NoError(t, err) + return + } + require.ErrorContains(t, err, tc.wantErr) + }) + } +} + func TestBrowserTypePrepareFlags(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/common/browser.go b/internal/js/modules/k6/browser/common/browser.go index ea1480144..46adcd256 100644 --- a/internal/js/modules/k6/browser/common/browser.go +++ b/internal/js/modules/k6/browser/common/browser.go @@ -43,15 +43,7 @@ type Browser struct { // A *Connection is saved to this field, see: connect(). conn connection - // This mutex is only needed in an edge case where we have multiple - // instances of k6 connecting to the same chrome instance. In this - // case when a page is created by the first k6 instance, the second - // instance of k6 will also receive an onAttachedToTarget event. When - // this occurs there's a small chance that at the same time a new - // context is being created by the second k6 instance. So the read - // occurs in getDefaultBrowserContextOrMatchedID which is called by - // onAttachedToTarget, and the write in NewContext. This mutex protects - // the read/write race condition for this one case. + // contextMu protects concurrent access to the browser contexts. contextMu sync.RWMutex context *BrowserContext defaultContext *BrowserContext @@ -160,12 +152,16 @@ func (b *Browser) connect() error { return fmt.Errorf("connecting to browser DevTools URL: %w", err) } - // We don't need to lock this because `connect()` is called only in NewBrowser - b.defaultContext, err = NewBrowserContext(b.vuCtx, b, "", DefaultBrowserContextOptions(), b.logger) + defaultContext, err := NewBrowserContext(b.vuCtx, b, "", DefaultBrowserContextOptions(), b.logger) if err != nil { return fmt.Errorf("browser connect: %w", err) } - b.runOnClose = append(b.runOnClose, b.defaultContext.cleanup) + // The connection's recvLoop reads defaultContext through + // connectionOnAttachedToTarget, so publish it under contextMu. + b.contextMu.Lock() + b.defaultContext = defaultContext + b.contextMu.Unlock() + b.runOnClose = append(b.runOnClose, defaultContext.cleanup) return b.initEvents() } @@ -177,7 +173,9 @@ func (b *Browser) disposeContext(id cdp.BrowserContextID) error { if err := action.Do(cdp.WithExecutor(b.vuCtx, b.conn)); err != nil { return fmt.Errorf("disposing browser context ID %s: %w", id, err) } + b.contextMu.Lock() b.context = nil + b.contextMu.Unlock() return nil } @@ -266,23 +264,16 @@ func (b *Browser) initEvents() error { } // connectionOnAttachedToTarget is called when Connection receives an attachedToTarget -// event. Returning false will stop the event from being processed by the connection. +// event. Returning false makes the connection release the target instead of +// adopting it. Targets from the connection's own browser context and from +// the default browser context are accepted. func (b *Browser) connectionOnAttachedToTarget(eva *target.EventAttachedToTarget) bool { - // This allows to attach targets to the same browser context as the current - // one, and to the default browser context. - // - // We don't want to hold the lock for the entire function - // (connectionOnAttachedToTarget) run duration, because we want to avoid - // possible lock contention issues with the browser context being closed while - // we're waiting for it. So, we do the lock management in a function with its - // own defer. - isAllowedBrowserContext := func() bool { - b.contextMu.RLock() - defer b.contextMu.RUnlock() - return b.context == nil || b.context.id == eva.TargetInfo.BrowserContextID - } - - return isAllowedBrowserContext() + b.contextMu.RLock() + defer b.contextMu.RUnlock() + if b.context != nil && b.context.id == eva.TargetInfo.BrowserContextID { + return true + } + return b.defaultContext.id == eva.TargetInfo.BrowserContextID } // onAttachedToTarget is called when a new page is attached to the browser. @@ -295,9 +286,6 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) error { browserCtx = b.getDefaultBrowserContextOrMatchedID(targetPage.BrowserContextID) ) - if !b.isAttachedPageValid(ev, browserCtx) { - return nil // Ignore this page. - } session := b.conn.getSession(ev.SessionID) if session == nil { b.logger.Debugf("Browser:onAttachedToTarget", @@ -305,6 +293,13 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) error { ev.SessionID, targetPage.TargetID) return nil // ignore } + if !b.isAttachedPageValid(ev, browserCtx) { + // Never ignore an attached target without detaching from it: the + // browser keeps the target paused until every attached client + // releases it, and detaching drops this client's hold. + detachSession(session) + return nil // Ignore this page. + } var ( isPage = targetPage.Type == "page" @@ -320,10 +315,10 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) error { } p, err := NewPage(b.vuCtx, session, browserCtx, targetPage.TargetID, opener, isPage, b.logger) if err != nil && b.isPageAttachmentErrorIgnorable(ev, session, err) { - if b.closing.Load() { - b.logger.Debugf("Browser:onAttachedToTarget", "new page failed; browser is closing: sid:%v", ev.SessionID) - detachSession(b.browserCtx, session) - } + // Always release: isPageAttachmentErrorIgnorable can also return + // true when only this VU's context ended, while the browser + // instance stays alive and shared with other VUs. + detachSession(session) return nil // Ignore this page. } if err != nil { @@ -349,7 +344,7 @@ func (b *Browser) onAttachedToTarget(ev *target.EventAttachedToTarget) error { ) } - detachSession(b.browserCtx, session) + detachSession(session) return nil } @@ -624,15 +619,26 @@ func (b *Browser) Close() { b.logger.Errorf("Browser:Close", "closing the browser: %v", err) } } - // Wait for all outstanding events (e.g. Target.detachedFromTarget) to be - // processed, and for the process to exit gracefully. Otherwise kill it - // forcefully after the timeout. + + // Wait for outstanding teardown to drain before closing the connection, so + // in-flight CDP messages (e.g. Target.detachedFromTarget) are delivered + // rather than dropped by an early WebSocket close. timeout := time.Second - select { - case <-b.browserProc.processDone: - case <-time.After(timeout): - b.logger.Debugf("Browser:Close", "killing browser process with PID %d after %s", b.browserProc.Pid(), timeout) - b.browserProc.Terminate() + if b.browserOpts.isRemoteBrowser { + // A remote browser has no local process to wait on, so waiting on + // processDone would always hit the full timeout. Instead, wait, up to the + // timeout, for our targets' detach events to be delivered — observed as + // the pages draining to zero — then close. + b.waitForPagesToDetach(timeout) + } else { + // Wait for the process to exit gracefully; otherwise kill it forcefully + // after the timeout. + select { + case <-b.browserProc.processDone: + case <-time.After(timeout): + b.logger.Debugf("Browser:Close", "killing browser process with PID %d after %s", b.browserProc.Pid(), timeout) + b.browserProc.Terminate() + } } // This is unintuitive, since the process exited, so the connection would've // been closed as well. The reason we still call conn.Close() here is to @@ -645,24 +651,55 @@ func (b *Browser) Close() { b.conn.Close() } +// waitForPagesToDetach waits, up to timeout, for the browser's own pages to +// drain to zero. Closing pages triggers Target.detachedFromTarget events; a +// page leaves b.pages only once its event has been received and processed. +// +// This is a barrier on our own targets' teardown, NOT on the connection going +// quiet: a remote browser we don't own keeps emitting unsolicited events right +// up to conn.Close(), and those may still be dropped. That's harmless for us +// (we've decided we're done), but the guarantee is only "our page detaches are +// processed", not "all in-flight messages are delivered". It's a bounded, +// event-driven alternative to waiting on a process exit, which a remote browser +// doesn't have. +func (b *Browser) waitForPagesToDetach(timeout time.Duration) { + ticker := time.NewTicker(20 * time.Millisecond) + defer ticker.Stop() + + deadline := time.After(timeout) + for len(b.getPages()) > 0 { + select { + case <-deadline: + b.logger.Debugf("Browser:Close", + "timed out after %s waiting for %d page(s) to detach; closing anyway", + timeout, len(b.getPages())) + return + case <-ticker.C: + } + } +} + // CloseContext is a short-cut function to close the current browser's context. // If there is no active browser context, it returns an error. func (b *Browser) CloseContext() error { - if b.context == nil { + context := b.Context() + if context == nil { return errors.New("cannot close context as none is active in browser") } - return b.context.Close() + return context.Close() } // Context returns the current browser context or nil. func (b *Browser) Context() *BrowserContext { + b.contextMu.RLock() + defer b.contextMu.RUnlock() return b.context } // IsConnected returns whether the WebSocket connection to the browser process // is active or not. func (b *Browser) IsConnected() bool { - return b.browserProc.isConnected() + return !b.closing.Load() && b.browserProc.isConnected() } // NewContext creates a new incognito-like browser context. @@ -670,7 +707,10 @@ func (b *Browser) NewContext(opts *BrowserContextOptions) (*BrowserContext, erro _, span := TraceAPICall(b.vuCtx, "", "browser.newContext") defer span.End() - if b.context != nil { + if b.closing.Load() { + return nil, spanRecordErrorf(span, "browser has been closed") + } + if b.Context() != nil { return nil, spanRecordErrorf(span, "existing browser context must be closed before creating a new one") } if opts == nil { diff --git a/internal/js/modules/k6/browser/common/browser_test.go b/internal/js/modules/k6/browser/common/browser_test.go index d1af513ce..2bdfeb860 100644 --- a/internal/js/modules/k6/browser/common/browser_test.go +++ b/internal/js/modules/k6/browser/common/browser_test.go @@ -3,9 +3,11 @@ package common import ( "context" "errors" + "sync" "testing" "time" + "github.com/chromedp/cdproto" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/target" "github.com/stretchr/testify/require" @@ -189,6 +191,158 @@ func TestBrowserNewPageInContext(t *testing.T) { }) } +func TestConnectionOnAttachedToTarget(t *testing.T) { + t.Parallel() + + const ( + ownContextID cdp.BrowserContextID = "own" + foreignContextID cdp.BrowserContextID = "foreign" + ) + + newTestBrowser := func(t *testing.T, withOwnContext bool) *Browser { + t.Helper() + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + b := newBrowser(context.Background(), ctx, cancel, nil, NewLocalBrowserOptions(), log.NewNullLogger()) + + var err error + b.defaultContext, err = NewBrowserContext(k6ext.WithVU(ctx, k6test.NewVU(t)), b, "", nil, nil) + require.NoError(t, err) + if withOwnContext { + b.context, err = NewBrowserContext(k6ext.WithVU(ctx, k6test.NewVU(t)), b, ownContextID, nil, nil) + require.NoError(t, err) + } + + return b + } + + tests := []struct { + name string + withOwnContext bool + targetContextID cdp.BrowserContextID + want bool + }{ + {"own_context_target", true, ownContextID, true}, + {"foreign_target", true, foreignContextID, false}, + {"foreign_target_without_own_context", false, foreignContextID, false}, + {"default_context_target", false, "", true}, + {"default_context_target_with_own_context", true, "", true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + b := newTestBrowser(t, tt.withOwnContext) + ev := &target.EventAttachedToTarget{ + TargetInfo: &target.Info{BrowserContextID: tt.targetContextID}, + } + require.Equal(t, tt.want, b.connectionOnAttachedToTarget(ev)) + }) + } +} + +func TestBrowserContextDisposeAndLookupRace(t *testing.T) { + t.Parallel() + + const browserContextID cdp.BrowserContextID = "context" + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + b := newBrowser(context.Background(), ctx, cancel, nil, NewLocalBrowserOptions(), log.NewNullLogger()) + b.context = &BrowserContext{id: browserContextID} + b.defaultContext = &BrowserContext{} + b.conn = fakeConn{ + execute: func(context.Context, string, any, any) error { + return nil + }, + } + + const iterations = 100 + const workers = 3 + var ( + wg sync.WaitGroup + disposeErrors = make(chan error, workers*iterations) + lookupContexts = make(chan *BrowserContext, workers*iterations) + ) + + for range workers { // make multiple goroutines + wg.Add(2) + go func() { // writes to context + defer wg.Done() + for range iterations { + disposeErrors <- b.disposeContext(browserContextID) + } + }() + + go func() { // reads from context + defer wg.Done() + for range iterations { + lookupContexts <- b.getDefaultBrowserContextOrMatchedID(browserContextID) + b.Context() + } + }() + } + + wg.Wait() + close(disposeErrors) + close(lookupContexts) + + for err := range disposeErrors { + require.NoError(t, err) + } + for browserContext := range lookupContexts { + require.NotNil(t, browserContext) + } + require.Nil(t, b.Context()) + require.Same(t, b.defaultContext, b.getDefaultBrowserContextOrMatchedID(browserContextID)) +} + +// TestBrowserRejectedTarget ensures a target the connection accepts but the +// browser rejects (isAttachedPageValid) is resumed and then detached from, +// like the connection-level rejection in TestConnectionRejectedTarget. +func TestBrowserRejectedTarget(t *testing.T) { + t.Parallel() + + const rejectedSessionID = target.SessionID("session_id_0123456789") + + wsURL, received := newAttachedToTargetServer(t, + attachedToTargetEvent(rejectedSessionID, "service_worker", ""), + 300*time.Millisecond) + + vuCtx, vuCancel := context.WithCancel(context.Background()) + t.Cleanup(vuCancel) + b := newBrowser(context.Background(), vuCtx, vuCancel, nil, NewLocalBrowserOptions(), log.NewNullLogger()) + var err error + b.defaultContext, err = NewBrowserContext(k6ext.WithVU(vuCtx, k6test.NewVU(t)), b, "", nil, nil) + require.NoError(t, err) + + conn, err := NewConnection( + b.browserCtx, wsURL, log.NewNullLogger(), b.connectionOnAttachedToTarget, + ) + require.NoError(t, err) + t.Cleanup(conn.Close) + b.conn = conn + + evCh := make(chan Event) + conn.on(b.browserCtx, []string{cdproto.EventTargetAttachedToTarget}, evCh) + + action := target.SetDiscoverTargets(true) + require.NoError(t, action.Do(cdp.WithExecutor(b.browserCtx, conn))) + + select { + case event := <-evCh: + ev, ok := event.data.(*target.EventAttachedToTarget) + require.True(t, ok) + require.NoError(t, b.onAttachedToTarget(ev)) + case <-time.After(5 * time.Second): + t.Fatal("timed out waiting for the attached target event") + } + + requireResumeThenDetach(t, received, rejectedSessionID) +} + type fakeConn struct { connection execute func(context.Context, string, any, any) error diff --git a/internal/js/modules/k6/browser/common/connection.go b/internal/js/modules/k6/browser/common/connection.go index 5217c614f..72509f440 100644 --- a/internal/js/modules/k6/browser/common/connection.go +++ b/internal/js/modules/k6/browser/common/connection.go @@ -134,9 +134,12 @@ type Connection struct { sessionsMu sync.RWMutex sessions map[target.SessionID]*Session - // onTargetAttachedToTarget is called when a new target is attached to the browser. - // Returning false will prevent the session from being created. - // If onTargetAttachedToTarget is nil, the session will be created. + // onTargetAttachedToTarget is called when a new target is attached to + // the browser. The target's session is registered before the call, so + // that the response to the release below can be routed back to it. + // Returning false releases the target: it is resumed and detached from + // instead of being adopted. If onTargetAttachedToTarget is nil, every + // target is adopted. onTargetAttachedToTarget func(*target.EventAttachedToTarget) bool } @@ -329,7 +332,36 @@ func (c *Connection) findTargetIDForLog(id target.SessionID) target.ID { return s.targetID } -//nolint:funlen,gocognit,cyclop +// deliverToSession routes msg to session's readCh. It reports whether +// recvLoop should stop reading from the connection entirely. +// +// getSession only holds the sessions lock for the lookup, so by the time +// this runs, session may already be closed (e.g. by closeSession, which +// can be called concurrently from any goroutine, not just recvLoop): its +// readLoop may have already exited on its own done channel, leaving no +// receiver on readCh. Without the <-session.done case, an unbuffered send +// there would then block forever, since neither c.closeCh nor c.done fire +// just because one session closed - stalling recvLoop, and with it every +// other session on the connection. +func (c *Connection) deliverToSession(session *Session, msg *cdproto.Message) (stop bool) { + select { + case session.readCh <- msg: + case <-session.done: + c.logger.Debugf("Connection:deliverToSession:<-session.done", "sid:%v tid:%v wsURL:%q", + session.id, session.targetID, c.wsURL) + case code := <-c.closeCh: + c.logger.Debugf("Connection:deliverToSession:<-c.closeCh", "sid:%v tid:%v wsURL:%v crashed:%t", + session.id, session.targetID, c.wsURL, session.crashed) + _ = c.close(code) + case <-c.done: + c.logger.Debugf("Connection:deliverToSession:<-c.done", "sid:%v tid:%v wsURL:%v crashed:%t", + session.id, session.targetID, c.wsURL, session.crashed) + return true + } + return false +} + +//nolint:funlen,gocognit func (c *Connection) recvLoop() { c.logger.Debugf("Connection:recvLoop", "wsURL:%q", c.wsURL) for { @@ -363,21 +395,21 @@ func (c *Connection) recvLoop() { eva := ev.(*target.EventAttachedToTarget) //nolint:forcetypeassert sid, tid := eva.SessionID, eva.TargetInfo.TargetID + c.sessionsMu.Lock() + session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgIDGen) + c.logger.Debugf("Connection:recvLoop:EventAttachedToTarget", "sid:%v tid:%v wsURL:%q", sid, tid, c.wsURL) + c.sessions[sid] = session + c.sessionsMu.Unlock() + if c.onTargetAttachedToTarget != nil { - // If onTargetAttachedToTarget is set, it will be called to determine - // if a session should be created for the target. + // If onTargetAttachedToTarget is set, it will be called to + // determine if the target should be adopted or released. ok := c.onTargetAttachedToTarget(eva) if !ok { - c.stopWaitingForDebugger(sid) + detachSession(session) continue } } - - c.sessionsMu.Lock() - session := NewSession(c.ctx, c, sid, tid, c.logger, c.msgIDGen) - c.logger.Debugf("Connection:recvLoop:EventAttachedToTarget", "sid:%v tid:%v wsURL:%q", sid, tid, c.wsURL) - c.sessions[sid] = session - c.sessionsMu.Unlock() } else if msg.Method == cdproto.EventTargetDetachedFromTarget { ev, err := cdproto.UnmarshalMessage(&msg) if err != nil { @@ -401,7 +433,6 @@ func (c *Connection) recvLoop() { switch { case msg.SessionID != "" && (msg.Method != "" || msg.ID != 0): - // TODO: possible data race - session can get removed after getting it here session := c.getSession(msg.SessionID) if session == nil { continue @@ -413,15 +444,7 @@ func (c *Connection) recvLoop() { continue } - select { - case session.readCh <- &msg: - case code := <-c.closeCh: - c.logger.Debugf("Connection:recvLoop:<-c.closeCh", "sid:%v tid:%v wsURL:%v crashed:%t", - session.id, session.targetID, c.wsURL, session.crashed) - _ = c.close(code) - case <-c.done: - c.logger.Debugf("Connection:recvLoop:<-c.done", "sid:%v tid:%v wsURL:%v crashed:%t", - session.id, session.targetID, c.wsURL, session.crashed) + if c.deliverToSession(session, &msg) { return } @@ -445,29 +468,43 @@ func (c *Connection) recvLoop() { } } -// stopWaitingForDebugger tells the browser to stop waiting for the -// debugger to attach to the page's session. -// -// Whether we're not sharing pages among browser contexts, Chromium -// still does so (since we're auto-attaching all browser targets). -// This means that if we don't stop waiting for the debugger, the -// browser will wait for the debugger to attach to the new page -// indefinitely, even if the page is not part of the browser context -// we're using. +// detachSession resumes a rejected target's session, awaits the response, +// then detaches from it. Detaching alone should release the target, but the +// browser has a bug where a detached target can stay paused, so the resume +// must land first — the same workaround as Playwright's CRSession.detach +// (crConnection.ts). The wait is bounded so an unresponsive target is still +// detached from. Best-effort: errors are not returned. Target.detachFromTarget +// is browser-level: the session goes in the params, not the message's +// session ID. // -// We don't return an error because the browser might have already -// closed the connection. In that case, handling the error would -// be redundant. This operation is best-effort. -func (c *Connection) stopWaitingForDebugger(sid target.SessionID) { - msg := &cdproto.Message{ - ID: c.msgIDGen.newID(), - SessionID: sid, - Method: cdproto.MethodType(cdpruntime.CommandRunIfWaitingForDebugger), - } - err := c.send(c.ctx, msg, nil, nil) - if err != nil { - c.logger.Errorf("Connection:stopWaitingForDebugger", "sid:%v wsURL:%q, err:%v", sid, c.wsURL, err) - } +// The session is also closed locally here, rather than left for the +// browser's own Target.detachedFromTarget event to reap: that event isn't +// guaranteed to arrive (e.g. if the detach command itself errors), and +// waiting for it would leak the session's goroutine and its entry in the +// connection's sessions map until the whole connection closes. +func detachSession(session *Session) { + go func() { + c := session.conn + defer c.closeSession(session.id, session.targetID) + + ctx, cancel := context.WithTimeout(c.ctx, DefaultTimeout) + defer cancel() + _ = session.Execute(ctx, cdpruntime.CommandRunIfWaitingForDebugger, nil, nil) + + buf, err := jsonv2.Marshal(&target.DetachFromTargetParams{SessionID: session.id}, defaultJSONV2Options) + if err != nil { + c.logger.Errorf("Connection:detachSession", "sid:%v wsURL:%q, err:%v", session.id, c.wsURL, err) + return + } + msg := &cdproto.Message{ + ID: c.msgIDGen.newID(), + Method: cdproto.MethodType(target.CommandDetachFromTarget), + Params: buf, + } + if err := c.send(c.ctx, msg, nil, nil); err != nil { + c.logger.Errorf("Connection:detachSession", "sid:%v wsURL:%q, err:%v", session.id, c.wsURL, err) + } + }() } func (c *Connection) send( diff --git a/internal/js/modules/k6/browser/common/connection_test.go b/internal/js/modules/k6/browser/common/connection_test.go index 6fd6db437..d61505e18 100644 --- a/internal/js/modules/k6/browser/common/connection_test.go +++ b/internal/js/modules/k6/browser/common/connection_test.go @@ -4,14 +4,18 @@ import ( "context" "fmt" "net/url" + "sync" "testing" + "time" "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" "go.k6.io/k6/v2/internal/js/modules/k6/browser/tests/ws" "github.com/chromedp/cdproto" "github.com/chromedp/cdproto/cdp" + cdpruntime "github.com/chromedp/cdproto/runtime" "github.com/chromedp/cdproto/target" + jsonv2 "github.com/go-json-experiment/json" "github.com/go-json-experiment/json/jsontext" "github.com/gorilla/websocket" "github.com/stretchr/testify/assert" @@ -78,6 +82,314 @@ func TestConnectionSendRecv(t *testing.T) { }) } +// attachedToTargetEvent returns a Target.attachedToTarget event payload for +// a paused target, as the browser sends when auto-attaching a new target. +func attachedToTargetEvent(sessionID target.SessionID, targetType, browserContextID string) string { + return fmt.Sprintf(` + { + "sessionId": %q, + "targetInfo": { + "targetId": "target_id_0123456789", + "type": %q, + "title": "", + "url": "about:blank", + "attached": true, + "browserContextId": %q + }, + "waitingForDebugger": true + }`, sessionID, targetType, browserContextID) +} + +// resumeRespondedSentinel marks, in the received stream, the moment the +// server wrote its response to Runtime.runIfWaitingForDebugger. Anything +// the client sends after seeing that response arrives later in the stream. +const resumeRespondedSentinel = cdproto.MethodType("test:resume-responded") + +// newAttachedToTargetServer starts a fake CDP websocket server that records +// every command it receives and emits the given Target.attachedToTarget +// event when the client sends Target.setDiscoverTargets. The response to +// Runtime.runIfWaitingForDebugger is delayed by resumeDelay, and a +// resumeRespondedSentinel is recorded when it is written. +func newAttachedToTargetServer( + t *testing.T, attachedEvent string, resumeDelay time.Duration, +) (string, chan cdproto.Message) { + t.Helper() + + received := make(chan cdproto.Message, 16) + handler := func(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) { + if msg.Method == "" { + return + } + received <- *msg + switch msg.Method { + case cdproto.MethodType(cdproto.CommandTargetSetDiscoverTargets): + writeCh <- cdproto.Message{ + Method: cdproto.EventTargetAttachedToTarget, + Params: jsontext.Value(attachedEvent), + } + writeCh <- cdproto.Message{ + ID: msg.ID, + Result: jsontext.Value([]byte("{}")), + } + case cdproto.MethodType(cdpruntime.CommandRunIfWaitingForDebugger): + response := cdproto.Message{ + ID: msg.ID, + SessionID: msg.SessionID, + Result: jsontext.Value([]byte("{}")), + } + // Respond asynchronously so a delayed response does not also + // delay reading the messages the client sends in the meantime. + go func() { + select { + case <-time.After(resumeDelay): + case <-done: + return + } + received <- cdproto.Message{Method: resumeRespondedSentinel} + select { + case writeCh <- response: + case <-done: + } + }() + } + } + + server := ws.NewServer(t, ws.WithCDPHandler("/cdp", handler, nil)) + u, err := url.Parse(server.ServerHTTP.URL) + require.NoError(t, err) + + return fmt.Sprintf("ws://%s/cdp", u.Host), received +} + +// requireResumeThenDetach consumes the server-received messages until the +// rejected target's session is detached from, asserting the client resumed +// the target, awaited the server's response, and only then detached, with +// both commands correctly addressed. +func requireResumeThenDetach(t *testing.T, received <-chan cdproto.Message, sid target.SessionID) { + t.Helper() + + timeout := time.After(5 * time.Second) + var sawResume, sawResponse bool + for { + select { + case msg := <-received: + switch msg.Method { + case cdproto.MethodType(cdpruntime.CommandRunIfWaitingForDebugger): + // The resume targets the paused session directly. + require.Equal(t, sid, msg.SessionID) + sawResume = true + case resumeRespondedSentinel: + sawResponse = true + case cdproto.MethodType(target.CommandDetachFromTarget): + // Target.detachFromTarget is a browser-level command: the + // session goes in the params, not in the message session ID. + require.Empty(t, msg.SessionID) + var params target.DetachFromTargetParams + require.NoError(t, jsonv2.Unmarshal(msg.Params, ¶ms, defaultJSONV2Options)) + require.Equal(t, sid, params.SessionID) + require.True(t, sawResume, "expected Runtime.runIfWaitingForDebugger before the detach") + require.True(t, sawResponse, + "expected the detach to be sent only after the resume response was written") + return + } + case <-timeout: + t.Fatal("timed out waiting for the rejected target to be released") + } + } +} + +// TestConnectionRejectedTarget ensures a target rejected by the attach filter +// is resumed and then detached from. The browser keeps a new target paused +// until every client attached with waitForDebuggerOnStart releases it. +// Detaching should be enough to release the hold, but the browser has a bug +// where a detached target can stay paused, so the resume must come first — +// the same workaround Playwright uses (crConnection.ts, CRSession.detach). +// Wire order is not enough: only awaiting the response guarantees the +// browser processed the resume before the detach, so the server delays the +// resume response to catch a client that does not wait for it. +func TestConnectionRejectedTarget(t *testing.T) { + t.Parallel() + + const rejectedSessionID = target.SessionID("session_id_0123456789") + + wsURL, received := newAttachedToTargetServer(t, + attachedToTargetEvent(rejectedSessionID, "page", "browser_context_id_0123456789"), + 300*time.Millisecond) + + ctx := context.Background() + rejectAll := func(*target.EventAttachedToTarget) bool { return false } + conn, err := NewConnection(ctx, wsURL, log.NewNullLogger(), rejectAll) + require.NoError(t, err) + t.Cleanup(conn.Close) + + action := target.SetDiscoverTargets(true) + require.NoError(t, action.Do(cdp.WithExecutor(ctx, conn))) + + requireResumeThenDetach(t, received, rejectedSessionID) +} + +// TestConnectionRejectedTargetClosedLocallyOnDetachError ensures a rejected +// target's session is closed locally even when the outgoing detach command +// itself errors (as it can against a real browser). Waiting for the +// browser's own Target.detachedFromTarget event to reap the session would +// leak it whenever that event never arrives. +func TestConnectionRejectedTargetClosedLocallyOnDetachError(t *testing.T) { + t.Parallel() + + const rejectedSessionID = target.SessionID("session_id_0123456789") + + received := make(chan cdproto.Message, 16) + handler := func(_ *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, _ chan struct{}) { + if msg.Method == "" { + return + } + received <- *msg + switch msg.Method { + case cdproto.MethodType(cdproto.CommandTargetSetDiscoverTargets): + writeCh <- cdproto.Message{ + Method: cdproto.EventTargetAttachedToTarget, + Params: jsontext.Value(attachedToTargetEvent(rejectedSessionID, "page", "browser_context_id_0123456789")), + } + writeCh <- cdproto.Message{ID: msg.ID, Result: jsontext.Value([]byte("{}"))} + case cdproto.MethodType(cdpruntime.CommandRunIfWaitingForDebugger): + writeCh <- cdproto.Message{ID: msg.ID, SessionID: msg.SessionID, Result: jsontext.Value([]byte("{}"))} + case cdproto.MethodType(target.CommandDetachFromTarget): + // Simulate the browser rejecting the detach itself. + writeCh <- cdproto.Message{ID: msg.ID, Error: &cdproto.Error{Message: "No session with given id"}} + } + } + + server := ws.NewServer(t, ws.WithCDPHandler("/cdp", handler, nil)) + u, err := url.Parse(server.ServerHTTP.URL) + require.NoError(t, err) + wsURL := fmt.Sprintf("ws://%s/cdp", u.Host) + + ctx := context.Background() + rejectAll := func(*target.EventAttachedToTarget) bool { return false } + conn, err := NewConnection(ctx, wsURL, log.NewNullLogger(), rejectAll) + require.NoError(t, err) + t.Cleanup(conn.Close) + + action := target.SetDiscoverTargets(true) + require.NoError(t, action.Do(cdp.WithExecutor(ctx, conn))) + + require.Eventually(t, func() bool { + return conn.getSession(rejectedSessionID) == nil + }, 5*time.Second, 10*time.Millisecond, + "session should be closed locally even though the detach command errored") +} + +// newTestConnection dials a minimal fake CDP server that acknowledges +// every command with an empty success reply. +func newTestConnection(t *testing.T) *Connection { + t.Helper() + + handler := func(_ *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, _ chan struct{}) { + writeCh <- cdproto.Message{ID: msg.ID, SessionID: msg.SessionID, Result: jsontext.Value([]byte("{}"))} + } + server := ws.NewServer(t, ws.WithCDPHandler("/cdp", handler, nil)) + u, err := url.Parse(server.ServerHTTP.URL) + require.NoError(t, err) + + conn, err := NewConnection(context.Background(), fmt.Sprintf("ws://%s/cdp", u.Host), log.NewNullLogger(), nil) + require.NoError(t, err) + t.Cleanup(conn.Close) + + return conn +} + +// TestConnectionDeliverToSessionAfterClose is a deterministic regression +// test for a race in recvLoop's dispatch to a session's readCh: getSession +// only holds the sessions lock for the map lookup, so a session obtained +// this way can already be closed - by closeSession, callable from any +// goroutine, not just recvLoop - by the time the dispatch actually runs. +// Without the <-session.done case, delivering to an already-closed +// session's readCh blocks forever: its readLoop has already exited, so +// there is no reader left, and neither c.closeCh nor c.done fire just +// because one session closed. That would stall recvLoop - the single +// goroutine reading every message for the whole connection - along with +// every other session sharing it. +// +// The session's readLoop is deliberately never started (done is closed by +// hand instead of via NewSession + closeSession), so there is provably no +// reader on readCh, ever - this removes any dependency on readLoop's own +// exit timing and drives the exact interleaving deterministically, rather +// than hoping to catch it. +func TestConnectionDeliverToSessionAfterClose(t *testing.T) { + t.Parallel() + + const sid = target.SessionID("session_id_0123456789") + const tid = target.ID("target_id_0123456789") + + conn := newTestConnection(t) + + session := &Session{ + BaseEventEmitter: NewBaseEventEmitter(context.Background()), + conn: conn, + id: sid, + targetID: tid, + readCh: make(chan *cdproto.Message), + done: make(chan struct{}), + msgIDGen: conn.msgIDGen, + logger: log.NewNullLogger(), + } + close(session.done) // no readLoop was ever started to close it itself + + done := make(chan bool, 1) + go func() { + done <- conn.deliverToSession(session, &cdproto.Message{Method: "Test.event", SessionID: sid}) + }() + + select { + case stop := <-done: + require.False(t, stop, "delivering to a closed session must not signal a connection-level stop") + case <-time.After(2 * time.Second): + t.Fatal("deliverToSession blocked forever delivering to an already-closed session") + } +} + +// TestConnectionCloseSessionConcurrent proves closeSession is safe to call +// concurrently, and repeatedly, for the same session - the exact shape of +// detachSession's own closeSession call racing the browser's genuine +// Target.detachedFromTarget event arriving and being handled by recvLoop's +// own closeSession call, or any other pair of callers. Under -race, a +// data race here would fail the test; closeSession's own locking must +// also make it safe to call twice - only the first caller may release the +// session, every other caller must see it already gone. +func TestConnectionCloseSessionConcurrent(t *testing.T) { + t.Parallel() + + const sid = target.SessionID("session_id_0123456789") + const tid = target.ID("target_id_0123456789") + const callers = 50 + + conn := newTestConnection(t) + + conn.sessionsMu.Lock() + conn.sessions[sid] = NewSession(context.Background(), conn, sid, tid, log.NewNullLogger(), conn.msgIDGen) + conn.sessionsMu.Unlock() + + var wg sync.WaitGroup + results := make([]bool, callers) + for i := range callers { + wg.Add(1) + go func(i int) { + defer wg.Done() + results[i] = conn.closeSession(sid, tid) + }(i) + } + wg.Wait() + + var released int + for _, ok := range results { + if ok { + released++ + } + } + require.Equal(t, 1, released, "exactly one concurrent closeSession call should release the session") + require.Nil(t, conn.getSession(sid), "the session must be gone from the connection after closing") +} + func TestConnectionCreateSession(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/common/device.go b/internal/js/modules/k6/browser/common/device.go index fbd3aca27..844f91577 100644 --- a/internal/js/modules/k6/browser/common/device.go +++ b/internal/js/modules/k6/browser/common/device.go @@ -1,3 +1,4 @@ +//nolint:goconst package common // Device represents an end-user device (computer, tablet, phone etc.) diff --git a/internal/js/modules/k6/browser/common/element_handle.go b/internal/js/modules/k6/browser/common/element_handle.go index 9ca06a655..51efcf30b 100644 --- a/internal/js/modules/k6/browser/common/element_handle.go +++ b/internal/js/modules/k6/browser/common/element_handle.go @@ -50,6 +50,14 @@ type ElementHandle struct { frame *Frame } +// Page returns the page that owns the element handle. +func (h *ElementHandle) Page() *Page { + if h == nil || h.frame == nil { + return nil + } + return h.frame.Page() +} + // String returns a string representation of ElementHandle. // It exists mostly for debugging where we don't want fmt.Sprintf to just // go through a complex object and try to stringify it. @@ -1391,9 +1399,13 @@ func (h *ElementHandle) Screenshot( span.SetAttributes(attribute.String("screenshot.path", opts.Path)) - s := newScreenshotter(spanCtx, sp, h.logger) + s, cancel := newScreenshotter(spanCtx, opts.Timeout, sp, h.logger) + defer cancel() buf, err := s.screenshotElement(h, opts) if err != nil { + if errors.Is(s.ctx.Err(), context.DeadlineExceeded) && h.ctx.Err() == nil { + err = &k6ext.UserFriendlyError{Err: err, Timeout: opts.Timeout} + } return nil, spanRecordErrorf(span, "taking screenshot of elementHandle: %w", err) } @@ -1868,8 +1880,7 @@ func errorFromDOMError(v any) error { default: return fmt.Errorf("unexpected DOM error type %T", v) } - var uerr *k6ext.UserFriendlyError - if errors.As(err, &uerr) { + if _, ok := errors.AsType[*k6ext.UserFriendlyError](err); ok { return err } if strings.Contains(serr, "timed out") { diff --git a/internal/js/modules/k6/browser/common/element_handle_options.go b/internal/js/modules/k6/browser/common/element_handle_options.go index 45716b0e8..815dec1de 100644 --- a/internal/js/modules/k6/browser/common/element_handle_options.go +++ b/internal/js/modules/k6/browser/common/element_handle_options.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "reflect" - "strings" "time" "github.com/grafana/sobek" @@ -64,10 +63,6 @@ type ScrollIntoViewOptions struct { Inline ScrollPosition `json:"inline"` } -type ElementHandleCheckOptions struct { - ElementHandleBasePointerOptions -} - type ElementHandleClickOptions struct { ElementHandleBasePointerOptions Button string `json:"button"` @@ -201,17 +196,6 @@ func (o *ElementHandleBasePointerOptions) Parse(ctx context.Context, opts sobek. return nil } -func NewElementHandleCheckOptions(defaultTimeout time.Duration) *ElementHandleCheckOptions { - return &ElementHandleCheckOptions{ - ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), - } -} - -// Parse parses the ElementHandleCheckOptions from the given opts. -func (o *ElementHandleCheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - return o.ElementHandleBasePointerOptions.Parse(ctx, opts) -} - // NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption. func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOptions { return &ElementHandleSetInputFilesOptions{ @@ -421,14 +405,6 @@ func (o *ElementHandlePressOptions) Parse(ctx context.Context, opts sobek.Value) return nil } -func (o *ElementHandlePressOptions) ToBaseOptions() *ElementHandleBaseOptions { - o2 := ElementHandleBaseOptions{} - o2.Force = false - o2.NoWaitAfter = o.NoWaitAfter - o2.Timeout = o.Timeout - return &o2 -} - func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHandleScreenshotOptions { return &ElementHandleScreenshotOptions{ Path: "", @@ -439,43 +415,6 @@ func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHan } } -// Parse parses the ElementHandleScreenshotOptions from the given opts. -func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error { - if common.IsNullish(opts) { - return nil - } - - rt := k6ext.Runtime(ctx) - formatSpecified := false - obj := opts.ToObject(rt) - for _, k := range obj.Keys() { - switch k { - case "omitBackground": - o.OmitBackground = obj.Get(k).ToBoolean() - case "path": - o.Path = obj.Get(k).String() - case "quality": - o.Quality = obj.Get(k).ToInteger() - case "type": - if f, ok := imageFormatToID[obj.Get(k).String()]; ok { - o.Format = f - formatSpecified = true - } - case "timeout": - o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond - } - } - - // Infer file format by path if format not explicitly specified (default is PNG) - if o.Path != "" && !formatSpecified { - if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") { - o.Format = ImageFormatJPEG - } - } - - return nil -} - func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHandleSetCheckedOptions { return &ElementHandleSetCheckedOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -483,25 +422,6 @@ func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHan } } -// Parse parses the ElementHandleSetCheckedOptions from the given opts. -func (o *ElementHandleSetCheckedOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "strict" { - o.Strict = opts.Get(k).ToBoolean() - } - } - } - return nil -} - func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapOptions { return &ElementHandleTapOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -509,27 +429,6 @@ func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapO } } -// Parse parses the ElementHandleTapOptions from the given opts. -func (o *ElementHandleTapOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "modifiers" { - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - } - } - } - return nil -} - func NewElementHandleTypeOptions(defaultTimeout time.Duration) *ElementHandleTypeOptions { return &ElementHandleTypeOptions{ Delay: 0, @@ -557,34 +456,12 @@ func (o *ElementHandleTypeOptions) Parse(ctx context.Context, opts sobek.Value) return nil } -func (o *ElementHandleTypeOptions) ToBaseOptions() *ElementHandleBaseOptions { - o2 := ElementHandleBaseOptions{} - o2.Force = false - o2.NoWaitAfter = o.NoWaitAfter - o2.Timeout = o.Timeout - return &o2 -} - func NewElementHandleWaitForElementStateOptions(defaultTimeout time.Duration) *ElementHandleWaitForElementStateOptions { return &ElementHandleWaitForElementStateOptions{ Timeout: defaultTimeout, } } -// Parse parses the ElementHandleWaitForElementStateOptions from the given opts. -func (o *ElementHandleWaitForElementStateOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "timeout" { - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - // ElementHandleDispatchEventOptions are options for ElementHandle.dispatchEvent. type ElementHandleDispatchEventOptions struct { *ElementHandleBaseOptions diff --git a/internal/js/modules/k6/browser/common/extra_info_tracker.go b/internal/js/modules/k6/browser/common/extra_info_tracker.go new file mode 100644 index 000000000..b445c3a0a --- /dev/null +++ b/internal/js/modules/k6/browser/common/extra_info_tracker.go @@ -0,0 +1,184 @@ +package common + +import ( + "github.com/chromedp/cdproto/network" +) + +// requestInfo aligns CDP ExtraInfo events with their Response objects for a +// single requestId, modelled after Playwright's ResponseExtraInfoTracker. +// +// CDP dispatches events on two independent channels: +// - Channel A: requestWillBeSent, responseReceived, loadingFinished/loadingFailed +// - Channel B: requestWillBeSentExtraInfo, responseReceivedExtraInfo +// +// They are not synchronised, so events arrive in any order between the +// channels, but within each channel they arrive in order for a given +// requestId. A single requestId can produce multiple request/response pairs +// (a redirect chain reuses the requestId), so we pair by index: the i-th +// tracked response pairs with the i-th request/response ExtraInfo event. +// +// Only responses that expect ExtraInfo are tracked (see processResponse). +// Because the ExtraInfo events are likewise only emitted for those responses, +// the indexes stay aligned even when some hops (e.g. cached redirects) carry no +// ExtraInfo. The raw request headers are attached to the response's Request, +// matching Playwright. +type requestInfo struct { + // Indexed queues of parsed raw headers. Slots are set to nil after + // pairing to avoid applying the same headers twice. + requestExtraHeaders []map[string][]string + responseExtraHeaders []map[string][]string + + // Responses that expect ExtraInfo, in arrival order. + responses []*Response + + loadingFinished bool + loadingFailed bool + servedFromCache bool +} + +// extraInfoTracker pairs requestWillBeSentExtraInfo / responseReceivedExtraInfo +// events with their corresponding Response (and its Request) using index-based +// matching (modelled after Playwright's ResponseExtraInfoTracker). +type extraInfoTracker struct { + requests map[network.RequestID]*requestInfo +} + +func newExtraInfoTracker() *extraInfoTracker { + return &extraInfoTracker{ + requests: make(map[network.RequestID]*requestInfo), + } +} + +// getOrCreate returns the requestInfo for the given requestId, creating one if +// it doesn't exist yet. +func (t *extraInfoTracker) getOrCreate(reqID network.RequestID) *requestInfo { + info, ok := t.requests[reqID] + if !ok { + info = &requestInfo{} + t.requests[reqID] = info + } + return info +} + +// requestWillBeSentExtraInfo records parsed raw request headers and tries to +// pair them with an already-registered response at the same index. +func (t *extraInfoTracker) requestWillBeSentExtraInfo(reqID network.RequestID, headers map[string][]string) { + info := t.getOrCreate(reqID) + info.requestExtraHeaders = append(info.requestExtraHeaders, headers) + t.patchHeaders(info, len(info.requestExtraHeaders)-1) + t.checkFinished(reqID, info) +} + +// responseReceivedExtraInfo records parsed raw response headers and tries to +// pair them with an already-registered response at the same index. +func (t *extraInfoTracker) responseReceivedExtraInfo(reqID network.RequestID, headers map[string][]string) { + info := t.getOrCreate(reqID) + info.responseExtraHeaders = append(info.responseExtraHeaders, headers) + t.patchHeaders(info, len(info.responseExtraHeaders)-1) + t.checkFinished(reqID, info) +} + +// requestServedFromCache marks the requestId as served from cache. Chrome can +// report hasExtraInfo=true for cached responses but never emit the matching +// ExtraInfo event (crbug.com/1340398), so such responses must not be tracked, +// otherwise the index would slip and later headers would land on the wrong +// response. +func (t *extraInfoTracker) requestServedFromCache(reqID network.RequestID) { + info := t.getOrCreate(reqID) + info.servedFromCache = true +} + +// processResponse registers a Response that expects ExtraInfo. Responses that +// do not expect it (hasExtraInfo=false, or served from cache) are not tracked, +// so they keep their provisional headers as the raw ones. +func (t *extraInfoTracker) processResponse(reqID network.RequestID, resp *Response, hasExtraInfo bool) { + info, ok := t.requests[reqID] + if !hasExtraInfo || (ok && info.servedFromCache) { + // No raw headers will arrive, so the provisional headers are final. + // Unblock any readers waiting for the raw headers. + resp.resolveRawHeaders() + resp.request.resolveRawHeaders() + return + } + if !ok { + info = t.getOrCreate(reqID) + } + info.responses = append(info.responses, resp) + t.patchHeaders(info, len(info.responses)-1) +} + +// patchHeaders attaches the raw request and response headers at the given index +// when both the response and the corresponding ExtraInfo event are available. +// Consumed ExtraInfo slots are set to nil to avoid applying them twice. +func (t *extraInfoTracker) patchHeaders(info *requestInfo, index int) { + if index < 0 || index >= len(info.responses) { + return + } + resp := info.responses[index] + if resp == nil { + return + } + if index < len(info.requestExtraHeaders) { + if extra := info.requestExtraHeaders[index]; extra != nil { + resp.request.addExtraHeaders(extra) + info.requestExtraHeaders[index] = nil + } + } + if index < len(info.responseExtraHeaders) { + if extra := info.responseExtraHeaders[index]; extra != nil { + resp.addExtraHeaders(extra) + info.responseExtraHeaders[index] = nil + } + } +} + +// loadingFinished records that loading finished and stops tracking once every +// tracked response has been paired with its ExtraInfo event. +func (t *extraInfoTracker) loadingFinished(reqID network.RequestID) { + info, ok := t.requests[reqID] + if !ok { + return + } + info.loadingFinished = true + t.resolvePending(info) + t.checkFinished(reqID, info) +} + +// loadingFailed records that loading failed and stops tracking once every +// tracked response has been paired with its ExtraInfo event. +func (t *extraInfoTracker) loadingFailed(reqID network.RequestID) { + info, ok := t.requests[reqID] + if !ok { + return + } + info.loadingFailed = true + t.resolvePending(info) + t.checkFinished(reqID, info) +} + +// resolvePending unblocks readers waiting on the raw headers of any tracked +// response (and its request) whose ExtraInfo never arrived. It is a backstop +// run on the terminal event so a reader never waits forever; the entry itself +// is kept (see checkFinished) so a late ExtraInfo event can still be applied. +func (t *extraInfoTracker) resolvePending(info *requestInfo) { + for _, resp := range info.responses { + if resp == nil { + continue + } + resp.resolveRawHeaders() + resp.request.resolveRawHeaders() + } +} + +// checkFinished removes the tracking entry once loading has finished/failed and +// every tracked response has its response ExtraInfo. Deletion is deferred until +// then so a late-arriving ExtraInfo event (CDP may emit it after +// loadingFinished) can still be paired instead of being dropped. +func (t *extraInfoTracker) checkFinished(reqID network.RequestID, info *requestInfo) { + if !info.loadingFinished && !info.loadingFailed { + return + } + if len(info.responses) <= len(info.responseExtraHeaders) { + delete(t.requests, reqID) + } +} diff --git a/internal/js/modules/k6/browser/common/fork_timings_test.go b/internal/js/modules/k6/browser/common/fork_timings_test.go new file mode 100644 index 000000000..b80f64a89 --- /dev/null +++ b/internal/js/modules/k6/browser/common/fork_timings_test.go @@ -0,0 +1,68 @@ +package common + +import ( + "testing" + "time" + + "github.com/chromedp/cdproto/network" + "github.com/stretchr/testify/assert" + + "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" +) + +func TestForkResponseTimings(t *testing.T) { + t.Parallel() + now := time.Now() + req := &Request{ + wallTime: now.Add(-time.Second), + headersEndWall: now.Add(-100 * time.Millisecond), + responseEndWall: now, + } + for _, tc := range []struct { + name string + response *Response + want browserResponseTimings + }{ + { + name: "TLS phases exclude connection establishment from duration", + response: &Response{timing: &network.ResourceTiming{ + ConnectStart: 10, SslStart: 30, ConnectEnd: 70, + SendStart: 80, SendEnd: 90, ReceiveHeadersEnd: 140, + }}, + want: browserResponseTimings{ + blocked: 80, connecting: 20, tlsHandshaking: 40, + sending: 10, waiting: 50, receiving: 100, durationMs: 160, + }, + }, + { + name: "plain HTTP", + response: &Response{timing: &network.ResourceTiming{ + ConnectStart: 10, SslStart: -1, ConnectEnd: 30, + SendStart: 50, SendEnd: 60, ReceiveHeadersEnd: 80, + }}, + want: browserResponseTimings{ + blocked: 50, connecting: 20, + sending: 10, waiting: 20, receiving: 100, durationMs: 130, + }, + }, + { + name: "negative phases are clamped", + response: &Response{timing: &network.ResourceTiming{ + ConnectStart: 10, SslStart: -1, ConnectEnd: 5, + SendStart: -2, SendEnd: -3, ReceiveHeadersEnd: -4, + }}, + want: browserResponseTimings{receiving: 100, durationMs: 100}, + }, + {name: "no response", want: browserResponseTimings{durationMs: 1000}}, + { + name: "no timing", response: &Response{}, + want: browserResponseTimings{durationMs: 1000}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + nm := &NetworkManager{logger: log.NewNullLogger()} + assert.Equal(t, tc.want, nm.responseTimings(req, tc.response, now)) + }) + } +} diff --git a/internal/js/modules/k6/browser/common/frame.go b/internal/js/modules/k6/browser/common/frame.go index 45e56632a..7e6320406 100644 --- a/internal/js/modules/k6/browser/common/frame.go +++ b/internal/js/modules/k6/browser/common/frame.go @@ -56,6 +56,11 @@ var domElementStateToID = map[string]DOMElementState{ //nolint:gochecknoglobals "hidden": DOMElementStateHidden, } +func DOMElementStateIDFromString(format string) (DOMElementState, bool) { + id, exists := domElementStateToID[format] + return id, exists +} + // MarshalJSON marshals the enum as a quoted JSON string. func (s DOMElementState) MarshalJSON() ([]byte, error) { buffer := bytes.NewBufferString(`"`) @@ -496,6 +501,9 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio return nil, err } if handle == nil { + if opts.State == DOMElementStateHidden || opts.State == DOMElementStateDetached { + return nil, nil //nolint:nilnil + } return nil, fmt.Errorf("waiting for selector %q did not result in any nodes", selector) } @@ -1660,6 +1668,15 @@ func (f *Frame) QueryAll(selector string) ([]*ElementHandle, error) { // Page returns page that owns frame. func (f *Frame) Page() *Page { + if f == nil { + return nil + } + if f.page != nil { + return f.page + } + if f.manager == nil { + return nil + } return f.manager.page } diff --git a/internal/js/modules/k6/browser/common/frame_manager.go b/internal/js/modules/k6/browser/common/frame_manager.go index ac6ba0bc2..35cf4185d 100644 --- a/internal/js/modules/k6/browser/common/frame_manager.go +++ b/internal/js/modules/k6/browser/common/frame_manager.go @@ -456,6 +456,9 @@ func (m *FrameManager) removeFramesRecursively(frame *Frame) error { if err := frame.detach(); err != nil { return fmt.Errorf("removing frames recursively: detaching frame: %w", err) } + if m.page != nil { + m.page.clearNetworkLoaderContext(cdp.FrameID(frame.ID())) + } m.framesMu.Lock() m.logger.Debugf("FrameManager:removeFramesRecursively:delParentFrame", diff --git a/internal/js/modules/k6/browser/common/frame_options.go b/internal/js/modules/k6/browser/common/frame_options.go index 8066ccc15..e9f88c290 100644 --- a/internal/js/modules/k6/browser/common/frame_options.go +++ b/internal/js/modules/k6/browser/common/frame_options.go @@ -2,16 +2,9 @@ package common import ( "bytes" - "context" "encoding/json" "fmt" - "reflect" "time" - - "github.com/grafana/sobek" - - "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" - "go.k6.io/k6/v2/js/common" ) type FrameBaseOptions struct { @@ -165,6 +158,11 @@ var pollingTypeToID = map[string]PollingType{ //nolint:gochecknoglobals "interval": PollingInterval, } +func PollingIDFromString(format string) (PollingType, bool) { + id, exists := pollingTypeToID[format] + return id, exists +} + // MarshalJSON marshals the enum as a quoted JSON string. func (p PollingType) MarshalJSON() ([]byte, error) { buffer := bytes.NewBufferString(`"`) @@ -214,23 +212,6 @@ func NewFrameBaseOptions(defaultTimeout time.Duration) *FrameBaseOptions { } } -// Parse parses the frame base options. -func (o *FrameBaseOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "strict": - o.Strict = opts.Get(k).ToBoolean() - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - func NewFrameCheckOptions(defaultTimeout time.Duration) *FrameCheckOptions { return &FrameCheckOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -238,15 +219,6 @@ func NewFrameCheckOptions(defaultTimeout time.Duration) *FrameCheckOptions { } } -// Parse parses the frame check options. -func (o *FrameCheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameClickOptions(defaultTimeout time.Duration) *FrameClickOptions { return &FrameClickOptions{ ElementHandleClickOptions: *NewElementHandleClickOptions(defaultTimeout), @@ -254,15 +226,6 @@ func NewFrameClickOptions(defaultTimeout time.Duration) *FrameClickOptions { } } -// Parse parses the frame click options. -func (o *FrameClickOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleClickOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameDblClickOptions(defaultTimeout time.Duration) *FrameDblclickOptions { return &FrameDblclickOptions{ ElementHandleDblclickOptions: *NewElementHandleDblclickOptions(defaultTimeout), @@ -270,15 +233,6 @@ func NewFrameDblClickOptions(defaultTimeout time.Duration) *FrameDblclickOptions } } -// Parse parses the frame dblclick options. -func (o *FrameDblclickOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleDblclickOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameFillOptions(defaultTimeout time.Duration) *FrameFillOptions { return &FrameFillOptions{ ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout), @@ -286,15 +240,6 @@ func NewFrameFillOptions(defaultTimeout time.Duration) *FrameFillOptions { } } -// Parse parses the frame fill options. -func (o *FrameFillOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameGotoOptions(defaultReferer string, defaultTimeout time.Duration) *FrameGotoOptions { return &FrameGotoOptions{ Referer: defaultReferer, @@ -303,28 +248,6 @@ func NewFrameGotoOptions(defaultReferer string, defaultTimeout time.Duration) *F } } -// Parse parses the frame goto options. -func (o *FrameGotoOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "referer": - o.Referer = opts.Get(k).String() - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - case "waitUntil": - lifeCycle := opts.Get(k).String() - if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { - return fmt.Errorf("parsing goto options: %w", err) - } - } - } - } - return nil -} - func NewFrameHoverOptions(defaultTimeout time.Duration) *FrameHoverOptions { return &FrameHoverOptions{ ElementHandleHoverOptions: *NewElementHandleHoverOptions(defaultTimeout), @@ -332,157 +255,64 @@ func NewFrameHoverOptions(defaultTimeout time.Duration) *FrameHoverOptions { } } -// Parse parses the frame hover options. -func (o *FrameHoverOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleHoverOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameInnerHTMLOptions(defaultTimeout time.Duration) *FrameInnerHTMLOptions { return &FrameInnerHTMLOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame innerHTML options. -func (o *FrameInnerHTMLOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameInnerTextOptions(defaultTimeout time.Duration) *FrameInnerTextOptions { return &FrameInnerTextOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame innerText options. -func (o *FrameInnerTextOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameInputValueOptions(defaultTimeout time.Duration) *FrameInputValueOptions { return &FrameInputValueOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame inputValue options. -func (o *FrameInputValueOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameIsCheckedOptions(defaultTimeout time.Duration) *FrameIsCheckedOptions { return &FrameIsCheckedOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame isChecked options. -func (o *FrameIsCheckedOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameIsDisabledOptions(defaultTimeout time.Duration) *FrameIsDisabledOptions { return &FrameIsDisabledOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame isDisabled options. -func (o *FrameIsDisabledOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameIsEditableOptions(defaultTimeout time.Duration) *FrameIsEditableOptions { return &FrameIsEditableOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame isEditable options. -func (o *FrameIsEditableOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameIsEnabledOptions(defaultTimeout time.Duration) *FrameIsEnabledOptions { return &FrameIsEnabledOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame isEnabled options. -func (o *FrameIsEnabledOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameIsInViewportOptions(defaultTimeout time.Duration) *FrameIsInViewportOptions { return &FrameIsInViewportOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame isInViewport options. -func (o *FrameIsInViewportOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - obj := opts.ToObject(k6ext.Runtime(ctx)) - for _, k := range obj.Keys() { - if k == "ratio" { - o.Ratio = obj.Get(k).ToFloat() - } - } - } - return nil -} - // NewFrameIsHiddenOptions creates and returns a new instance of FrameIsHiddenOptions. func NewFrameIsHiddenOptions() *FrameIsHiddenOptions { return &FrameIsHiddenOptions{} } -// Parse parses FrameIsHiddenOptions from sobek.Value. -func (o *FrameIsHiddenOptions) Parse(ctx context.Context, opts sobek.Value) error { - o.Strict = parseStrict(ctx, opts) - return nil -} - // NewFrameIsVisibleOptions creates and returns a new instance of FrameIsVisibleOptions. func NewFrameIsVisibleOptions() *FrameIsVisibleOptions { return &FrameIsVisibleOptions{} } -// Parse parses FrameIsVisibleOptions from sobek.Value. -func (o *FrameIsVisibleOptions) Parse(ctx context.Context, opts sobek.Value) error { - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFramePressOptions(defaultTimeout time.Duration) *FramePressOptions { return &FramePressOptions{ ElementHandlePressOptions: *NewElementHandlePressOptions(defaultTimeout), @@ -505,15 +335,6 @@ func NewFrameSelectOptionOptions(defaultTimeout time.Duration) *FrameSelectOptio } } -// Parse parses the frame selectOption options. -func (o *FrameSelectOptionOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameSetContentOptions(defaultTimeout time.Duration) *FrameSetContentOptions { return &FrameSetContentOptions{ Timeout: defaultTimeout, @@ -521,28 +342,6 @@ func NewFrameSetContentOptions(defaultTimeout time.Duration) *FrameSetContentOpt } } -// Parse parses the frame setContent options. -func (o *FrameSetContentOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - case "waitUntil": - lifeCycle := opts.Get(k).String() - if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { - return fmt.Errorf("parsing setContent options: %w", err) - } - } - } - } - - return nil -} - // NewFrameSetInputFilesOptions creates a new FrameSetInputFilesOptions. func NewFrameSetInputFilesOptions(defaultTimeout time.Duration) *FrameSetInputFilesOptions { return &FrameSetInputFilesOptions{ @@ -551,14 +350,6 @@ func NewFrameSetInputFilesOptions(defaultTimeout time.Duration) *FrameSetInputFi } } -// Parse parses FrameSetInputFilesOptions from sobek.Value. -func (o *FrameSetInputFilesOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleSetInputFilesOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameTapOptions(defaultTimeout time.Duration) *FrameTapOptions { return &FrameTapOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -567,44 +358,12 @@ func NewFrameTapOptions(defaultTimeout time.Duration) *FrameTapOptions { } } -// Parse parses the frame tap options. -func (o *FrameTapOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "modifiers": - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - case "strict": - o.Strict = opts.Get(k).ToBoolean() - } - } - } - return nil -} - func NewFrameTextContentOptions(defaultTimeout time.Duration) *FrameTextContentOptions { return &FrameTextContentOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } } -// Parse parses the frame textContent options. -func (o *FrameTextContentOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.FrameBaseOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameTypeOptions(defaultTimeout time.Duration) *FrameTypeOptions { return &FrameTypeOptions{ ElementHandleTypeOptions: *NewElementHandleTypeOptions(defaultTimeout), @@ -626,15 +385,6 @@ func NewFrameUncheckOptions(defaultTimeout time.Duration) *FrameUncheckOptions { } } -// Parse parses the frame uncheck options. -func (o *FrameUncheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameWaitForFunctionOptions(defaultTimeout time.Duration) *FrameWaitForFunctionOptions { return &FrameWaitForFunctionOptions{ Polling: PollingRaf, @@ -643,60 +393,12 @@ func NewFrameWaitForFunctionOptions(defaultTimeout time.Duration) *FrameWaitForF } } -// Parse JavaScript waitForFunction options. -func (o *FrameWaitForFunctionOptions) Parse(ctx context.Context, opts sobek.Value) error { - if common.IsNullish(opts) { - return nil - } - - rt := k6ext.Runtime(ctx) - obj := opts.ToObject(rt) - for _, k := range obj.Keys() { - v := obj.Get(k) - switch k { - case "timeout": - o.Timeout = time.Duration(v.ToInteger()) * time.Millisecond - case "polling": - switch v.ExportType().Kind() { - case reflect.Int64: - o.Polling = PollingInterval - o.Interval = v.ToInteger() - case reflect.String: - if p, ok := pollingTypeToID[v.ToString().String()]; ok { - o.Polling = p - break - } - fallthrough - default: - return fmt.Errorf("wrong polling option value: %q; "+ - `possible values: "raf", "mutation" or number`, v) - } - } - } - - return nil -} - func NewFrameWaitForLoadStateOptions(defaultTimeout time.Duration) *FrameWaitForLoadStateOptions { return &FrameWaitForLoadStateOptions{ Timeout: defaultTimeout, } } -// Parse parses the frame waitForLoadState options. -func (o *FrameWaitForLoadStateOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "timeout" { - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - func NewFrameWaitForNavigationOptions(defaultTimeout time.Duration) *FrameWaitForNavigationOptions { return &FrameWaitForNavigationOptions{ URL: "", @@ -705,36 +407,6 @@ func NewFrameWaitForNavigationOptions(defaultTimeout time.Duration) *FrameWaitFo } } -// Parse parses the frame waitForNavigation options. -func (o *FrameWaitForNavigationOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "url": - var val string - switch opts.Get(k).ExportType() { - case reflect.TypeFor[string](): - val = fmt.Sprintf("'%s'", opts.Get(k).String()) // Strings require quotes - default: // JS Regex, CSS, numbers or booleans - val = opts.Get(k).String() // No quotes - } - - o.URL = val - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - case "waitUntil": - lifeCycle := opts.Get(k).String() - if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { - return fmt.Errorf("parsing waitForNavigation options: %w", err) - } - } - } - } - return nil -} - func NewFrameWaitForSelectorOptions(defaultTimeout time.Duration) *FrameWaitForSelectorOptions { return &FrameWaitForSelectorOptions{ State: DOMElementStateVisible, @@ -743,58 +415,16 @@ func NewFrameWaitForSelectorOptions(defaultTimeout time.Duration) *FrameWaitForS } } -// Parse parses the frame waitForSelector options. -func (o *FrameWaitForSelectorOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "state": - state := opts.Get(k).String() - if s, ok := domElementStateToID[state]; ok { - o.State = s - } else { - return fmt.Errorf("%q is not a valid DOM state", state) - } - case "strict": - o.Strict = opts.Get(k).ToBoolean() - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - - return nil -} - // FrameDispatchEventOptions are options for Frame.dispatchEvent. type FrameDispatchEventOptions struct { - *FrameBaseOptions + FrameBaseOptions } // NewFrameDispatchEventOptions returns a new FrameDispatchEventOptions. func NewFrameDispatchEventOptions(defaultTimeout time.Duration) *FrameDispatchEventOptions { return &FrameDispatchEventOptions{ - FrameBaseOptions: NewFrameBaseOptions(defaultTimeout), - } -} - -func parseStrict(ctx context.Context, opts sobek.Value) bool { - var strict bool - - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "strict" { - strict = opts.Get(k).ToBoolean() - } - } + FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), } - - return strict } // FrameWaitForURLOptions are options for Frame.waitForURL and Page.waitForURL. @@ -810,23 +440,3 @@ func NewFrameWaitForURLOptions(defaultTimeout time.Duration) *FrameWaitForURLOpt WaitUntil: LifecycleEventLoad, } } - -// Parse parses the frame waitForURL options. -func (o *FrameWaitForURLOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - case "waitUntil": - lifeCycle := opts.Get(k).String() - if err := o.WaitUntil.UnmarshalText([]byte(lifeCycle)); err != nil { - return fmt.Errorf("parsing waitForURL options: %w", err) - } - } - } - } - return nil -} diff --git a/internal/js/modules/k6/browser/common/frame_options_test.go b/internal/js/modules/k6/browser/common/frame_options_test.go deleted file mode 100644 index f173ab4ef..000000000 --- a/internal/js/modules/k6/browser/common/frame_options_test.go +++ /dev/null @@ -1,121 +0,0 @@ -package common - -import ( - "testing" - "time" - - "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestFrameGotoOptionsParse(t *testing.T) { - t.Parallel() - - t.Run("ok", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "timeout": "1000", - "waitUntil": "networkidle", - }) - gotoOpts := NewFrameGotoOptions("https://example.com/", 0) - err := gotoOpts.Parse(vu.Context(), opts) - require.NoError(t, err) - - assert.Equal(t, "https://example.com/", gotoOpts.Referer) - assert.Equal(t, time.Second, gotoOpts.Timeout) - assert.Equal(t, LifecycleEventNetworkIdle, gotoOpts.WaitUntil) - }) - - t.Run("err/invalid_waitUntil", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "waitUntil": "none", - }) - navOpts := NewFrameGotoOptions("", 0) - err := navOpts.Parse(vu.Context(), opts) - - assert.EqualError(t, err, - `parsing goto options: `+ - `invalid lifecycle event: "none"; must be one of: `+ - `load, domcontentloaded, networkidle`) - }) -} - -func TestFrameSetContentOptionsParse(t *testing.T) { - t.Parallel() - - t.Run("ok", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "waitUntil": "networkidle", - }) - scOpts := NewFrameSetContentOptions(30 * time.Second) - err := scOpts.Parse(vu.Context(), opts) - require.NoError(t, err) - - assert.Equal(t, 30*time.Second, scOpts.Timeout) - assert.Equal(t, LifecycleEventNetworkIdle, scOpts.WaitUntil) - }) - - t.Run("err/invalid_waitUntil", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "waitUntil": "none", - }) - navOpts := NewFrameSetContentOptions(0) - err := navOpts.Parse(vu.Context(), opts) - - assert.EqualError(t, err, - `parsing setContent options: `+ - `invalid lifecycle event: "none"; must be one of: `+ - `load, domcontentloaded, networkidle`) - }) -} - -func TestFrameWaitForNavigationOptionsParse(t *testing.T) { - t.Parallel() - - t.Run("ok", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "url": "https://example.com/", - "timeout": "1000", - "waitUntil": "networkidle", - }) - navOpts := NewFrameWaitForNavigationOptions(0) - err := navOpts.Parse(vu.Context(), opts) - require.NoError(t, err) - - assert.Equal(t, "'https://example.com/'", navOpts.URL) - assert.Equal(t, time.Second, navOpts.Timeout) - assert.Equal(t, LifecycleEventNetworkIdle, navOpts.WaitUntil) - }) - - t.Run("err/invalid_waitUntil", func(t *testing.T) { - t.Parallel() - - vu := k6test.NewVU(t) - opts := vu.ToSobekValue(map[string]any{ - "waitUntil": "none", - }) - navOpts := NewFrameWaitForNavigationOptions(0) - err := navOpts.Parse(vu.Context(), opts) - - assert.EqualError(t, err, - `parsing waitForNavigation options: `+ - `invalid lifecycle event: "none"; must be one of: `+ - `load, domcontentloaded, networkidle`) - }) -} diff --git a/internal/js/modules/k6/browser/common/frame_session.go b/internal/js/modules/k6/browser/common/frame_session.go index 3edf039b2..83a1932ca 100644 --- a/internal/js/modules/k6/browser/common/frame_session.go +++ b/internal/js/modules/k6/browser/common/frame_session.go @@ -17,6 +17,7 @@ import ( "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" + k6common "go.k6.io/k6/v2/js/common" k6modules "go.k6.io/k6/v2/js/modules" k6metrics "go.k6.io/k6/v2/metrics" @@ -42,6 +43,25 @@ type CPUProfile struct { Rate float64 } +// webVitalKey identifies a buffered web vital by the URL of the page it was +// measured on and the metric name. Keying by URL keeps distinct pages (and +// subframes) as separate series, while repeated reports for the same page and +// metric collapse onto a single buffered entry. +type webVitalKey struct { + url string + name string +} + +// webVitalSample is a buffered web vital metric sample together with the +// attributes needed to emit its web_vital trace span when the buffer is +// flushed. +type webVitalSample struct { + sample k6metrics.Sample + name string + spanID string + rating string +} + /* FrameSession is used for managing a frame's life-cycle, or in other words its full session. It manages all the event listening while deferring the state storage to the Frame and FrameManager @@ -87,6 +107,15 @@ type FrameSession struct { // onFrameNavigated, but subsequent calls to onFrameNavigated in the same // mainframe never again create a navigation span. initialNavDone bool + + // Web vital metrics are buffered per (url, metric name) and flushed once + // per page: at each main-frame navigation boundary and at page close. This + // keeps a single sample per page and metric even when a page reports a + // vital more than once. The mutex guards against the initial navigation's + // flush, which runs on the page-construction goroutine rather than the + // event-loop goroutine. + bufferedWebVitalsMu sync.Mutex + bufferedWebVitals map[webVitalKey]webVitalSample } // NewFrameSession initializes and returns a new FrameSession. @@ -119,6 +148,7 @@ func NewFrameSession( isolatedWorlds: make(map[string]bool), eventCh: make(chan Event), childSessions: make(map[cdp.FrameID]*FrameSession), + bufferedWebVitals: make(map[webVitalKey]webVitalSample), vu: k6ext.GetVU(ctx), k6Metrics: k6Metrics, logger: l, @@ -235,7 +265,7 @@ func (fs *FrameSession) initDomains() error { return nil } -//nolint:cyclop +//nolint:cyclop,funlen func (fs *FrameSession) initEvents() { fs.logger.Debugf("NewFrameSession:initEvents", "sid:%v tid:%v", fs.session.ID(), fs.targetID) @@ -255,6 +285,8 @@ func (fs *FrameSession) initEvents() { // If there is an active span for main frame, // end it before exiting so it can be flushed if fs.mainFrameSpan != nil { + // Flush the last page's buffered web vitals while its span is live. + fs.flushWebVitals() // The url needs to be added here instead of at the start of the span // because at the start of the span we don't know the correct url for // the page we're navigating to. At the end of the span we do have this @@ -366,35 +398,67 @@ func (fs *FrameSession) parseAndEmitWebVitalMetric(object string) error { } state := fs.vu.State() - tags := state.Tags.GetCurrentValues().Tags + // Web Vitals are reported asynchronously through a CDP binding, long after the navigation that + // produced them. Reading live tags here would attribute the sample to whatever group/tags happen + // to be active at report time. When async metric context is enabled, prefer the context captured + // by the navigation operation instead, mirroring how NetworkManager attributes delayed requests. + tagsAndMeta := state.Tags.GetCurrentValues() + if k6common.AsyncMetricContextEnabled(state) { + if captured, ok := fs.page.getNetworkTagsAndMeta(); ok { + tagsAndMeta = captured + } + } + tags := tagsAndMeta.Tags if state.Options.SystemTags.Has(k6metrics.TagURL) { tags = handleURLTag(fs.page, wv.URL, http.MethodGet, tags) } tags = tags.With("rating", wv.Rating) - now := time.Now() - pushIfNotDone(fs.vu.Context(), fs.logger, state.Samples, k6metrics.ConnectedSamples{ - Samples: []k6metrics.Sample{ - { - TimeSeries: k6metrics.TimeSeries{Metric: metric, Tags: tags}, - Value: value, - Time: now, - }, + // Buffer the latest value for this page and metric instead of emitting it + // immediately. The buffer is flushed once per page so each page contributes + // a single sample per metric. + fs.bufferedWebVitalsMu.Lock() + fs.bufferedWebVitals[webVitalKey{url: wv.URL, name: wv.Name}] = webVitalSample{ + sample: k6metrics.Sample{ + TimeSeries: k6metrics.TimeSeries{Metric: metric, Tags: tags}, + Value: value, + Time: time.Now(), }, - }) - - _, span := TraceEvent( - fs.ctx, fs.targetID.String(), "web_vital", wv.SpanID, trace.WithAttributes( - attribute.String("web_vital.name", wv.Name), - attribute.Float64("web_vital.value", value), - attribute.String("web_vital.rating", wv.Rating), - )) - defer span.End() + name: wv.Name, + spanID: wv.SpanID, + rating: wv.Rating, + } + fs.bufferedWebVitalsMu.Unlock() return nil } +// flushWebVitals emits every buffered web vital metric sample and its +// web_vital trace span, then clears the buffer. Clearing makes the flush +// idempotent, so a page is emitted exactly once even when flushed at both a +// navigation boundary and at teardown. +func (fs *FrameSession) flushWebVitals() { + fs.bufferedWebVitalsMu.Lock() + defer fs.bufferedWebVitalsMu.Unlock() + + for key, wv := range fs.bufferedWebVitals { + pushIfNotDone(fs.vu.Context(), fs.logger, fs.vu.State().Samples, k6metrics.ConnectedSamples{ + Samples: []k6metrics.Sample{wv.sample}, + }) + + _, span := TraceEvent( + fs.ctx, fs.targetID.String(), "web_vital", wv.spanID, trace.WithAttributes( + attribute.String("web_vital.name", wv.name), + attribute.Float64("web_vital.value", wv.sample.Value), + attribute.String("web_vital.rating", wv.rating), + )) + span.End() + + delete(fs.bufferedWebVitals, key) + } +} + func (fs *FrameSession) onEventJavascriptDialogOpening(event *cdppage.EventJavascriptDialogOpening) { fs.logger.Debugf("FrameSession:onEventJavascriptDialogOpening", "sid:%v tid:%v url:%v dialogType:%s", @@ -510,7 +574,7 @@ func (fs *FrameSession) initOptions() error { if fs.isMainFrame() { optActions = append(optActions, emulation.SetFocusEmulationEnabled(true)) - if err := fs.updateViewport(); err != nil { + if err := fs.updateViewport(fs.ctx, fs.page.emulatedSize); err != nil { fs.logger.Debugf("NewFrameSession:initOptions:updateViewport", "sid:%v tid:%v, err:%v", fs.session.ID(), fs.targetID, err) @@ -822,6 +886,11 @@ func (fs *FrameSession) processNavigationSpan(id cdp.FrameID) { return } + // Flush the outgoing page's buffered web vitals while its navigation span + // is still live. This must run after the main-frame guard above so that a + // subframe load does not drain the main page's buffer. + fs.flushWebVitals() + // End the navigation span if it is non-nil if fs.mainFrameSpan != nil { // The url needs to be added here instead of at the start of the span @@ -969,7 +1038,7 @@ func (fs *FrameSession) onAttachedToTarget(event *target.EventAttachedToTarget) default: fs.logger.Debugf("FrameSession:onAttachedToTarget", "unsupported target type %q sid:%v", ti.Type, session.ID()) - detachSession(fs.teardownCtx, session) + detachSession(session) } if err == nil { return @@ -1020,7 +1089,7 @@ func (fs *FrameSession) attachIFrameToTarget(ti *target.Info, session *Session) if fs.page.isClosing() { fs.logger.Debugf("FrameSession:attachIFrameToTarget", "rejected frame; page is closing: tid=%v", ti.TargetID) - detachSession(fs.teardownCtx, session) + detachSession(session) return nil } @@ -1059,7 +1128,7 @@ func (fs *FrameSession) attachIFrameToTarget(ti *target.Info, session *Session) if errors.Is(err, errPageClosing) { fs.logger.Debugf("FrameSession:attachIFrameToTarget", "rejected frame; page is closing: tid=%v", ti.TargetID) - detachSession(fs.teardownCtx, session) + detachSession(session) return nil } return err @@ -1073,7 +1142,7 @@ func (fs *FrameSession) attachWorkerToTarget(ti *target.Info, session *Session) if fs.page.isClosing() { fs.logger.Debugf("FrameSession:attachWorkerToTarget", "rejected worker; page is closing: tid=%v", ti.TargetID) - detachSession(fs.teardownCtx, session) + detachSession(session) return nil } @@ -1221,7 +1290,7 @@ func (fs *FrameSession) updateRequestInterception(enable bool) error { return fs.networkManager.setRequestInterception(enable) } -func (fs *FrameSession) updateViewport() error { +func (fs *FrameSession) updateViewport(ctx context.Context, emulatedSize *EmulatedSize) error { fs.logger.Debugf("NewFrameSession:updateViewport", "sid:%v tid:%v", fs.session.ID(), fs.targetID) // other frames don't have viewports and, @@ -1234,7 +1303,6 @@ func (fs *FrameSession) updateViewport() error { } opts := fs.page.browserCtx.opts - emulatedSize := fs.page.emulatedSize if emulatedSize == nil { return nil } @@ -1253,7 +1321,7 @@ func (fs *FrameSession) updateViewport() error { WithScreenOrientation(&orientation). WithScreenWidth(screen.Width). WithScreenHeight(screen.Height) - if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil { + if err := action.Do(cdp.WithExecutor(ctx, fs.session)); err != nil { return fmt.Errorf("emulating viewport: %w", err) } @@ -1268,7 +1336,7 @@ func (fs *FrameSession) updateViewport() error { Width: viewport.Width, Height: viewport.Height, }) - if err := action2.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil { + if err := action2.Do(cdp.WithExecutor(ctx, fs.session)); err != nil { return fmt.Errorf("setting window bounds: %w", err) } } @@ -1288,11 +1356,3 @@ func (fs *FrameSession) executionContextForID( return nil, fmt.Errorf("no execution context found for id: %v", executionContextID) } - -// detachSession unblocks a target waiting for debugger and detaches from it. -// Prevents the browser from hanging on rejected targets during close -func detachSession(ctx context.Context, session *Session) { - _ = session.ExecuteWithoutExpectationOnReply(ctx, cdpruntime.CommandRunIfWaitingForDebugger, nil, nil) - _ = session.ExecuteWithoutExpectationOnReply(ctx, target.CommandDetachFromTarget, - &target.DetachFromTargetParams{SessionID: session.id}, nil) -} diff --git a/internal/js/modules/k6/browser/common/frame_session_test.go b/internal/js/modules/k6/browser/common/frame_session_test.go new file mode 100644 index 000000000..39129036b --- /dev/null +++ b/internal/js/modules/k6/browser/common/frame_session_test.go @@ -0,0 +1,88 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/features" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" + k6metrics "go.k6.io/k6/v2/metrics" +) + +// reportWebVitalGroup builds a FrameSession backed by a test VU, captures a +// navigation's metric context under navGroup, then changes the live group tag +// to liveGroup before reporting an LCP web vital. It returns the group tag on +// the resulting buffered sample. asyncEnabled toggles the async-metric-context +// feature flag. +func reportWebVitalGroup(t *testing.T, asyncEnabled bool, navGroup, liveGroup string) string { + t.Helper() + + registry := k6metrics.NewRegistry() + k6m := k6ext.RegisterCustomMetrics(registry) + vu := k6test.NewVU(t) + vu.ActivateVU() + + state := vu.State() + state.FeatureFlags = &features.Flags{AsyncMetricContext: asyncEnabled} + + page := &Page{} + // Capture the navigation's metric context, then settle the operation so it + // becomes the page's most-recently-completed network operation, mirroring a + // page load finishing before the browser reports its web vitals. + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag(k6metrics.TagGroup.String(), navGroup) + }) + finishAndRetain, _ := page.BeginNetworkOperation(state.Tags.GetCurrentValues()) + finishAndRetain() + + // The VU moves on: the live group tag no longer reflects the navigation that + // produced the web vital. + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag(k6metrics.TagGroup.String(), liveGroup) + }) + + fs := &FrameSession{ + ctx: vu.Context(), + vu: vu, + page: page, + k6Metrics: k6m, + bufferedWebVitals: make(map[webVitalKey]webVitalSample), + logger: log.NewNullLogger(), + } + + const lcp = `{"Name":"LCP","Value":123.4,"Rating":"good","URL":"https://example.test/"}` + require.NoError(t, fs.parseAndEmitWebVitalMetric(lcp)) + require.Len(t, fs.bufferedWebVitals, 1) + + var group string + for _, wv := range fs.bufferedWebVitals { + g, ok := wv.sample.Tags.Get(k6metrics.TagGroup.String()) + require.True(t, ok) + group = g + } + return group +} + +// TestParseAndEmitWebVitalMetricUsesNavigationContext verifies that, with the +// async-metric-context feature enabled, a web vital reported asynchronously is +// attributed to the group active during the navigation that produced it, rather +// than whatever group happens to be active when the CDP binding event fires. +func TestParseAndEmitWebVitalMetricUsesNavigationContext(t *testing.T) { + t.Parallel() + + t.Run("enabled uses captured navigation group", func(t *testing.T) { + t.Parallel() + group := reportWebVitalGroup(t, true, "::nav", "::after") + assert.Equal(t, "::nav", group) + }) + + t.Run("disabled uses live group", func(t *testing.T) { + t.Parallel() + group := reportWebVitalGroup(t, false, "::nav", "::after") + assert.Equal(t, "::after", group) + }) +} diff --git a/internal/js/modules/k6/browser/common/frame_test.go b/internal/js/modules/k6/browser/common/frame_test.go index b5c61b8ce..6938850a9 100644 --- a/internal/js/modules/k6/browser/common/frame_test.go +++ b/internal/js/modules/k6/browser/common/frame_test.go @@ -111,11 +111,6 @@ func (e *executionContextTestStub) eval( // this needs to be a pointer as otherw return e.evalFn(apiCtx, opts, js, args...) } -// toPtr is a helper function to convert a value to a pointer. -func toPtr[T any](v T) *T { - return &v -} - func TestBuildAttributeSelector(t *testing.T) { t.Parallel() @@ -153,28 +148,28 @@ func TestBuildAttributeSelector(t *testing.T) { name: "quoted_single_exact_false", attrName: "data-test", attrValue: "'Foo Bar'", - opts: &GetByBaseOptions{Exact: toPtr(false)}, + opts: &GetByBaseOptions{Exact: new(false)}, want: "internal:attr=[data-test='Foo Bar'i]", }, { name: "quoted_single_exact_true", attrName: "data-test", attrValue: "'Foo Bar'", - opts: &GetByBaseOptions{Exact: toPtr(true)}, + opts: &GetByBaseOptions{Exact: new(true)}, want: "internal:attr=[data-test='Foo Bar's]", }, { name: "quoted_double_exact_true", attrName: "data-test", attrValue: "\"Foo Bar\"", - opts: &GetByBaseOptions{Exact: toPtr(true)}, + opts: &GetByBaseOptions{Exact: new(true)}, want: "internal:attr=[data-test=\"Foo Bar\"s]", }, { name: "quoted_double_exact_false", attrName: "data-test", attrValue: "\"Foo Bar\"", - opts: &GetByBaseOptions{Exact: toPtr(false)}, + opts: &GetByBaseOptions{Exact: new(false)}, want: "internal:attr=[data-test=\"Foo Bar\"i]", }, { diff --git a/internal/js/modules/k6/browser/common/http.go b/internal/js/modules/k6/browser/common/http.go index 4da6a6ad6..ea9447abd 100644 --- a/internal/js/modules/k6/browser/common/http.go +++ b/internal/js/modules/k6/browser/common/http.go @@ -21,6 +21,7 @@ import ( "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" k6modules "go.k6.io/k6/v2/js/modules" + k6metrics "go.k6.io/k6/v2/metrics" ) // These ResourceTypes are duplicates of CDP's network.ResourceType. We want to work @@ -71,16 +72,23 @@ type RequestFailure struct { // Request represents a browser HTTP request. type Request struct { - ctx context.Context - frame *Frame - responseMu sync.RWMutex - response *Response - redirectChain []*Request - requestID network.RequestID - documentID string - url *url.URL - method string - headers map[string][]string + ctx context.Context + frame *Frame + responseMu sync.RWMutex + response *Response + redirectChain []*Request + requestID network.RequestID + documentID string + url *url.URL + method string + headers map[string][]string + extraHeaders map[string][]string + extraHeadersMu sync.RWMutex + // rawHeadersCh is closed once the raw (ExtraInfo) headers have been + // resolved: either applied, or known not to arrive. Readers that need the + // raw headers wait on it; see waitForRawHeaders. + rawHeadersCh chan struct{} + rawHeadersOnce sync.Once // For now we're only going to work with the 0th entry of postDataEntries. // We've not been able to reproduce a situation where more than one entry // occupies the slice. Once we have a better idea of when more than one @@ -113,6 +121,8 @@ type Request struct { responseEndWall time.Time // encodedDataLength: actual bytes received over the wire (compressed + headers), from LoadingFinished. encodedDataLength int64 + tagsAndMeta k6metrics.TagsAndMeta + networkOperation *networkOperationContext } // NewRequestParams are input parameters for NewRequest. @@ -122,6 +132,8 @@ type NewRequestParams struct { redirectChain []*Request interceptionID fetch.RequestID allowInterception bool + tagsAndMeta k6metrics.TagsAndMeta + networkOperation *networkOperationContext } // NewRequest creates a new HTTP request. @@ -135,8 +147,7 @@ func NewRequest(ctx context.Context, logger *log.Logger, rp NewRequestParams) (* u, err := url.Parse(ev.Request.URL) if err != nil { - var uerr *url.Error - if errors.As(err, &uerr) { + if uerr, ok := errors.AsType[*url.Error](err); ok { err = uerr.Err } return nil, fmt.Errorf("parsing URL %q: %w", ev.Request.URL, err) @@ -176,6 +187,9 @@ func NewRequest(ctx context.Context, logger *log.Logger, rp NewRequestParams) (* documentID: documentID.String(), headers: make(map[string][]string, len(ev.Request.Headers)), ctx: ctx, + rawHeadersCh: make(chan struct{}), + tagsAndMeta: rp.tagsAndMeta, + networkOperation: rp.networkOperation, } for n, v := range ev.Request.Headers { if s, ok := v.(string); ok { @@ -221,6 +235,29 @@ func validateResourceType(logger *log.Logger, t string) string { return t } +func (r *Request) addExtraHeaders(extra map[string][]string) { + r.extraHeadersMu.Lock() + r.extraHeaders = extra + r.extraHeadersMu.Unlock() + r.resolveRawHeaders() +} + +// resolveRawHeaders unblocks readers waiting for the raw (ExtraInfo) headers. +// It is called once the raw headers have been applied, or once it is known +// that none will arrive (so the provisional headers are the final ones). +func (r *Request) resolveRawHeaders() { + r.rawHeadersOnce.Do(func() { close(r.rawHeadersCh) }) +} + +// WaitForRawHeaders blocks until the raw headers are resolved or the request +// context is done, whichever comes first. +func (r *Request) WaitForRawHeaders() { + select { + case <-r.rawHeadersCh: + case <-r.ctx.Done(): + } +} + func (r *Request) getFrame() *Frame { return r.frame } @@ -238,9 +275,21 @@ func (r *Request) headersSize() int64 { size += len(r.method) size += len(r.url.Path) size += 8 // httpVersion - for n, v := range r.headers { - size += len(n) + len(strings.Join(v, "")) + 4 // 4 = ': ' + '\r\n' + r.extraHeadersMu.RLock() + if len(r.extraHeaders) != 0 { + for name, values := range r.extraHeaders { + for _, value := range values { + size += len(name) + len(value) + 4 // 4 = ': ' + '\r\n' + } + } + } else { + for name, values := range r.headers { + for _, value := range values { + size += len(name) + len(value) + 4 // 4 = ': ' + '\r\n' + } + } } + r.extraHeadersMu.RUnlock() return int64(size) } @@ -292,11 +341,18 @@ func (r *Request) setLoadedFromCache(fromMemoryCache bool) { // AllHeaders returns all the request headers. func (r *Request) AllHeaders() map[string]string { - // TODO: fix this data to include "ExtraInfo" header data headers := make(map[string]string) - for n, v := range r.headers { - headers[strings.ToLower(n)] = strings.Join(v, ",") + r.extraHeadersMu.RLock() + if len(r.extraHeaders) != 0 { + for name, values := range r.extraHeaders { + headers[strings.ToLower(name)] = strings.Join(values, "\n") + } + } else { + for name, values := range r.headers { + headers[strings.ToLower(name)] = strings.Join(values, "\n") + } } + r.extraHeadersMu.RUnlock() return headers } @@ -324,11 +380,17 @@ func (r *Request) Headers() map[string]string { // HeadersArray returns the request headers as an array of objects. func (r *Request) HeadersArray() []HTTPHeader { headers := make([]HTTPHeader, 0) - for n, vals := range r.headers { - for _, v := range vals { - headers = append(headers, HTTPHeader{Name: n, Value: v}) + r.extraHeadersMu.RLock() + source := r.headers + if len(r.extraHeaders) != 0 { + source = r.extraHeaders + } + for name, values := range source { + for _, v := range values { + headers = append(headers, HTTPHeader{Name: name, Value: v}) } } + r.extraHeadersMu.RUnlock() return headers } @@ -465,6 +527,10 @@ type Response struct { bodyMu sync.RWMutex body []byte headers map[string][]string + extraHeaders map[string][]string + extraHeadersMu sync.RWMutex + rawHeadersCh chan struct{} + rawHeadersOnce sync.Once fromDiskCache bool fromServiceWorker bool fromPrefetchCache bool @@ -496,6 +562,7 @@ func NewHTTPResponse( statusText: resp.StatusText, body: nil, headers: make(map[string][]string, len(resp.Headers)), + rawHeadersCh: make(chan struct{}), fromDiskCache: resp.FromDiskCache, fromServiceWorker: resp.FromServiceWorker, fromPrefetchCache: resp.FromPrefetchCache, @@ -527,6 +594,29 @@ func NewHTTPResponse( return &r } +func (r *Response) addExtraHeaders(extra map[string][]string) { + r.extraHeadersMu.Lock() + r.extraHeaders = extra + r.extraHeadersMu.Unlock() + r.resolveRawHeaders() +} + +// resolveRawHeaders unblocks readers waiting for the raw (ExtraInfo) headers. +// It is called once the raw headers have been applied, or once it is known +// that none will arrive (so the provisional headers are the final ones). +func (r *Response) resolveRawHeaders() { + r.rawHeadersOnce.Do(func() { close(r.rawHeadersCh) }) +} + +// WaitForRawHeaders blocks until the raw headers are resolved or the response +// context is done, whichever comes first. +func (r *Response) WaitForRawHeaders() { + select { + case <-r.rawHeadersCh: + case <-r.ctx.Done(): + } +} + func (r *Response) fetchBody() error { cached := func() bool { r.bodyMu.RLock() @@ -574,20 +664,39 @@ func (r *Response) headersSize() int64 { size += 8 // httpVersion size += 3 // statusCode size += len(r.statusText) - for n, v := range r.headers { - size += len(n) + len(strings.Join(v, "")) + 4 // 4 = ': ' + '\r\n' + r.extraHeadersMu.RLock() + if len(r.extraHeaders) != 0 { + for name, values := range r.extraHeaders { + for _, value := range values { + size += len(name) + len(value) + 4 // 4 = ': ' + '\r\n' + } + } + } else { + for name, values := range r.headers { + for _, value := range values { + size += len(name) + len(value) + 4 // 4 = ': ' + '\r\n' + } + } } + r.extraHeadersMu.RUnlock() size += 2 // '\r\n' return int64(size) } // AllHeaders returns all the response headers. func (r *Response) AllHeaders() map[string]string { - // TODO: fix this data to include "ExtraInfo" header data headers := make(map[string]string) - for n, v := range r.headers { - headers[strings.ToLower(n)] = strings.Join(v, ",") + r.extraHeadersMu.RLock() + if len(r.extraHeaders) != 0 { + for name, values := range r.extraHeaders { + headers[strings.ToLower(name)] = strings.Join(values, "\n") + } + } else { + for name, values := range r.headers { + headers[strings.ToLower(name)] = strings.Join(values, "\n") + } } + r.extraHeadersMu.RUnlock() return headers } @@ -637,10 +746,13 @@ func (r *Response) HeaderValue(name string) (string, bool) { return v, ok } -// HeaderValues returns the values of the given header. +// HeaderValues returns the values of the given header. The header name is +// matched case-insensitively, and headers that carry multiple values (such as +// Set-Cookie) are returned as separate entries. AllHeaders joins multiple +// values with a newline, so we split on the same separator here. func (r *Response) HeaderValues(name string) []string { headers := r.AllHeaders() - return strings.Split(headers[name], ",") + return strings.Split(headers[strings.ToLower(name)], "\n") } // FromCache returns whether this response was served from disk cache. @@ -670,11 +782,17 @@ func (r *Response) Headers() map[string]string { // HeadersArray returns the response headers as an array of objects. func (r *Response) HeadersArray() []HTTPHeader { headers := make([]HTTPHeader, 0) - for n, vals := range r.headers { - for _, v := range vals { - headers = append(headers, HTTPHeader{Name: n, Value: v}) + r.extraHeadersMu.RLock() + source := r.headers + if len(r.extraHeaders) != 0 { + source = r.extraHeaders + } + for name, values := range source { + for _, v := range values { + headers = append(headers, HTTPHeader{Name: name, Value: v}) } } + r.extraHeadersMu.RUnlock() return headers } diff --git a/internal/js/modules/k6/browser/common/http_test.go b/internal/js/modules/k6/browser/common/http_test.go index 21fd6828a..1c52d79da 100644 --- a/internal/js/modules/k6/browser/common/http_test.go +++ b/internal/js/modules/k6/browser/common/http_test.go @@ -127,6 +127,53 @@ func TestResponse(t *testing.T) { }) } +func TestResponseHeaderValuesExtraInfo(t *testing.T) { + t.Parallel() + + ts := cdp.MonotonicTime(time.Now()) + vu := k6test.NewVU(t) + vu.ActivateVU() + req := &Request{ + offset: 0, + } + res := NewHTTPResponse(vu.Context(), req, &network.Response{ + URL: "https://test/post", + Headers: network.Headers(map[string]any{"content-type": "text/plain"}), + }, &ts) + + // Simulate the raw wire headers arriving via responseReceivedExtraInfo, + // where a multi-valued header (Set-Cookie) is carried as separate values. + res.addExtraHeaders(map[string][]string{ + "Set-Cookie": {"a=1; Path=/", "b=2; Path=/"}, + }) + + t.Run("AllHeaders()_joins_multivalue_with_newline", func(t *testing.T) { + t.Parallel() + assert.Equal(t, "a=1; Path=/\nb=2; Path=/", res.AllHeaders()["set-cookie"]) + }) + + t.Run("HeaderValues()_splits_multivalue", func(t *testing.T) { + t.Parallel() + assert.Equal(t, []string{"a=1; Path=/", "b=2; Path=/"}, res.HeaderValues("set-cookie")) + }) + + t.Run("HeaderValues()_is_case_insensitive", func(t *testing.T) { + t.Parallel() + assert.Equal(t, []string{"a=1; Path=/", "b=2; Path=/"}, res.HeaderValues("Set-Cookie")) + }) + + t.Run("HeadersArray()_keeps_values_separate", func(t *testing.T) { + t.Parallel() + var cookies []string + for _, h := range res.HeadersArray() { + if strings.EqualFold(h.Name, "Set-Cookie") { + cookies = append(cookies, h.Value) + } + } + assert.ElementsMatch(t, []string{"a=1; Path=/", "b=2; Path=/"}, cookies) + }) +} + func TestValidateResourceType(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/common/js/injected_script.js b/internal/js/modules/k6/browser/common/js/injected_script.js index 535702ba4..dc3347a60 100644 --- a/internal/js/modules/k6/browser/common/js/injected_script.js +++ b/internal/js/modules/k6/browser/common/js/injected_script.js @@ -2597,7 +2597,7 @@ class InjectedScript { case "visible": return visible ? element : continuePolling; case "hidden": - return !visible ? element : continuePolling; + return !visible ? element || true : continuePolling; } }; diff --git a/internal/js/modules/k6/browser/common/js/web_vital_init.js b/internal/js/modules/k6/browser/common/js/web_vital_init.js index 7e1e3e970..e97e70376 100644 --- a/internal/js/modules/k6/browser/common/js/web_vital_init.js +++ b/internal/js/modules/k6/browser/common/js/web_vital_init.js @@ -19,12 +19,12 @@ function print(metric) { } function load() { - webVitals.onCLS(print); - webVitals.onLCP(print); + webVitals.onCLS(print, {reportAllChanges: true}); + webVitals.onLCP(print, {reportAllChanges: true}); - webVitals.onFCP(print); - webVitals.onINP(print); - webVitals.onTTFB(print); + webVitals.onFCP(print, {reportAllChanges: true}); + webVitals.onINP(print, {reportAllChanges: true}); + webVitals.onTTFB(print, {reportAllChanges: true}); } load(); diff --git a/internal/js/modules/k6/browser/common/js_handle.go b/internal/js/modules/k6/browser/common/js_handle.go index 46e448367..055b18fc5 100644 --- a/internal/js/modules/k6/browser/common/js_handle.go +++ b/internal/js/modules/k6/browser/common/js_handle.go @@ -87,6 +87,19 @@ func (h *BaseJSHandle) AsElement() *ElementHandle { return nil } +// Page returns the page that owns this handle, or nil when the handle is not +// associated with a frame (e.g. a worker or isolated-world execution context). +func (h *BaseJSHandle) Page() *Page { + if h == nil || h.execCtx == nil { + return nil + } + frame := h.execCtx.Frame() + if frame == nil { + return nil + } + return frame.Page() +} + // Dispose releases the remote object. func (h *BaseJSHandle) Dispose() error { err := h.dispose() diff --git a/internal/js/modules/k6/browser/common/kill_other.go b/internal/js/modules/k6/browser/common/kill_other.go index 27e6d447e..991e294ef 100644 --- a/internal/js/modules/k6/browser/common/kill_other.go +++ b/internal/js/modules/k6/browser/common/kill_other.go @@ -1,5 +1,4 @@ //go:build !linux -// +build !linux package common diff --git a/internal/js/modules/k6/browser/common/locator.go b/internal/js/modules/k6/browser/common/locator.go index b28b5bb9a..e748d1f31 100644 --- a/internal/js/modules/k6/browser/common/locator.go +++ b/internal/js/modules/k6/browser/common/locator.go @@ -26,6 +26,14 @@ type Locator struct { log *log.Logger } +// Page returns the page that owns the locator. +func (l *Locator) Page() *Page { + if l == nil || l.frame == nil { + return nil + } + return l.frame.Page() +} + // LocatorOptions allows modifying the [Locator] behavior. type LocatorOptions struct { // Matches only elements that contain the specified text. diff --git a/internal/js/modules/k6/browser/common/mouse_options_test.go b/internal/js/modules/k6/browser/common/mouse_options_test.go new file mode 100644 index 000000000..72ca714e2 --- /dev/null +++ b/internal/js/modules/k6/browser/common/mouse_options_test.go @@ -0,0 +1,70 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewMouseClickOptions(t *testing.T) { + t.Parallel() + + opts := NewMouseClickOptions() + + assert.Equal(t, "left", opts.Button) + assert.Equal(t, int64(1), opts.ClickCount) + assert.Equal(t, int64(0), opts.Delay) +} + +func TestNewMouseDblClickOptions(t *testing.T) { + t.Parallel() + + opts := NewMouseDblClickOptions() + + assert.Equal(t, "left", opts.Button) + assert.Equal(t, int64(0), opts.Delay) +} + +func TestNewMouseDownUpOptions(t *testing.T) { + t.Parallel() + + opts := NewMouseDownUpOptions() + + assert.Equal(t, "left", opts.Button) + assert.Equal(t, int64(1), opts.ClickCount) +} + +func TestNewMouseMoveOptions(t *testing.T) { + t.Parallel() + + opts := NewMouseMoveOptions() + + assert.Equal(t, int64(1), opts.Steps) +} + +func TestMouseClickOptionsToMouseDownUpOptions(t *testing.T) { + t.Parallel() + + src := &MouseClickOptions{Button: "right", ClickCount: 3, Delay: 50} + + got := src.ToMouseDownUpOptions() + + // Button and ClickCount are carried over. MouseDownUpOptions has no Delay + // field, so Delay is intentionally dropped. + assert.Equal(t, "right", got.Button) + assert.Equal(t, int64(3), got.ClickCount) +} + +func TestMouseDblClickOptionsToMouseClickOptions(t *testing.T) { + t.Parallel() + + src := &MouseDblClickOptions{Button: "right", Delay: 50} + + got := src.ToMouseClickOptions() + + // Button and Delay are copied, while ClickCount is always forced to 2 + // because this represents a double click. + assert.Equal(t, "right", got.Button) + assert.Equal(t, int64(50), got.Delay) + assert.Equal(t, int64(2), got.ClickCount) +} diff --git a/internal/js/modules/k6/browser/common/network_manager.go b/internal/js/modules/k6/browser/common/network_manager.go index fed09ba91..7dbc58ac3 100644 --- a/internal/js/modules/k6/browser/common/network_manager.go +++ b/internal/js/modules/k6/browser/common/network_manager.go @@ -17,6 +17,7 @@ import ( "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" + k6common "go.k6.io/k6/v2/js/common" k6modules "go.k6.io/k6/v2/js/modules" k6lib "go.k6.io/k6/v2/lib" k6netext "go.k6.io/k6/v2/lib/netext" @@ -33,7 +34,7 @@ import ( // Credentials holds HTTP authentication credentials. type Credentials struct { Username string `js:"username"` - Password string `js:"password"` //nolint:gosec + Password string `js:"password"` } // IsEmpty returns true if the credentials are empty. @@ -80,6 +81,7 @@ type NetworkManager struct { eventsWillBeSentMu sync.RWMutex reqIDToRequestPausedEvent map[network.RequestID]*fetch.EventRequestPaused eventsPausedMu sync.RWMutex + extraInfoTracker *extraInfoTracker attemptedAuth map[fetch.RequestID]bool @@ -125,6 +127,7 @@ func NewNetworkManager( reqIDToRequest: make(map[network.RequestID]*Request), reqIDToRequestWillBeSentEvent: make(map[network.RequestID]*network.EventRequestWillBeSent), reqIDToRequestPausedEvent: make(map[network.RequestID]*fetch.EventRequestPaused), + extraInfoTracker: newExtraInfoTracker(), attemptedAuth: make(map[fetch.RequestID]bool), extraHTTPHeaders: make(map[string]string), networkProfile: NewNetworkProfile(), @@ -208,10 +211,61 @@ func (m *NetworkManager) deleteRequestByID(reqID network.RequestID) { delete(m.reqIDToRequest, reqID) } +func (m *NetworkManager) networkTagsAndMeta( + state *k6lib.State, + event *network.EventRequestWillBeSent, + redirectChain []*Request, +) (k6metrics.TagsAndMeta, *networkOperationContext) { + if !k6common.AsyncMetricContextEnabled(state) { + return k6metrics.TagsAndMeta{}, nil + } + if len(redirectChain) > 0 { + previous := redirectChain[len(redirectChain)-1] + operation := previous.networkOperation + if m.frameManager != nil && m.frameManager.page != nil { + _, bindLoaderContext := requestLoaderContext(event) + if bindLoaderContext { + m.frameManager.page.bindNetworkLoaderOperation(event.FrameID, event.LoaderID, operation) + } + } + return previous.tagsAndMeta, operation + } + if m.frameManager != nil && m.frameManager.page != nil { + preferLoaderContext, bindLoaderContext := requestLoaderContext(event) + if tagsAndMeta, operation, ok := m.frameManager.page.getNetworkTagsAndMetaForRequest( + event.FrameID, event.LoaderID, preferLoaderContext, bindLoaderContext, + ); ok { + return tagsAndMeta, operation + } + } + return state.Tags.GetCurrentValues(), nil +} + +func requestLoaderContext(event *network.EventRequestWillBeSent) (prefer, bind bool) { + bind = event.Type == network.ResourceTypeDocument + initiatorType := network.InitiatorTypeOther + if event.Initiator != nil { + initiatorType = event.Initiator.Type + } + // A loader identifies navigation and parser-driven resource traffic, but it is also shared by + // unrelated fetches later in the document. Keep script traffic on the operation fallback. + prefer = bind || initiatorType == network.InitiatorTypeParser || + initiatorType == network.InitiatorTypePreload || + initiatorType == network.InitiatorTypeSignedExchange + return prefer, bind +} + func (m *NetworkManager) emitRequestMetrics(req *Request) { state := m.vu.State() + if state == nil { + return // CDP events can arrive after iteration teardown. + } - tags := state.Tags.GetCurrentValues().Tags + tagsAndMeta := req.tagsAndMeta + if tagsAndMeta.Tags == nil { + tagsAndMeta = state.Tags.GetCurrentValues() + } + tags := tagsAndMeta.Tags if state.Options.SystemTags.Has(k6metrics.TagMethod) { tags = tags.With("method", req.method) } @@ -226,13 +280,17 @@ func (m *NetworkManager) emitRequestMetrics(req *Request) { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserDataSent, Tags: tags}, Value: float64(req.Size().Total()), Time: req.wallTime, + Metadata: tagsAndMeta.Metadata, }, }, }) } -func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { +func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { //nolint:funlen state := m.vu.State() + if state == nil { + return // CDP events can arrive after iteration teardown. + } // In some scenarios we might not receive a ResponseReceived CDP event, in // which case the response won't be created. So to emit as much metric data @@ -269,7 +327,11 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { "response is nil url:%s method:%s", req.url, req.method) } - tags := state.Tags.GetCurrentValues().Tags + tagsAndMeta := req.tagsAndMeta + if tagsAndMeta.Tags == nil { + tagsAndMeta = state.Tags.GetCurrentValues() + } + tags := tagsAndMeta.Tags if state.Options.SystemTags.Has(k6metrics.TagMethod) { tags = tags.With("method", req.method) } @@ -291,49 +353,7 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { tags = tags.With("from_service_worker", strconv.FormatBool(fromSvcWrk)) tags = tags.With("resource_type", req.ResourceType()) - // CDP ResourceTiming: RequestTime is baseline (seconds); other fields are ms relative to it. - // k6 time metrics are emitted in milliseconds (see metrics.D), so we keep all timings in ms here. - // blocked = time until we could start sending (includes DNS, queue, connecting, tls) — same idea as k6 GetConn→GotConn. - var blocked, connecting, tlsHandshaking, sending, waiting, receiving, durationMs float64 - if resp != nil && resp.timing != nil { - t := resp.timing - blocked = t.SendStart - if blocked < 0 { - blocked = 0 - } - if t.SslStart > 0 { - connecting = t.SslStart - t.ConnectStart - tlsHandshaking = t.ConnectEnd - t.SslStart - } else { - connecting = t.ConnectEnd - t.ConnectStart - } - if connecting < 0 { - connecting = 0 - } - if tlsHandshaking < 0 { - tlsHandshaking = 0 - } - - sending = t.SendEnd - t.SendStart - if sending < 0 { - sending = 0 - } - waiting = t.ReceiveHeadersEnd - t.SendEnd - if waiting < 0 { - waiting = 0 - } - // receiving = time from headers received to response complete. - // Primary approach: convert ResourceTiming.RequestTime (monotonic seconds since boot) - // to a Go time.Time using cdp.MonotonicTimeEpoch, then compute responseEnd in ms - // relative to RequestTime. This keeps everything in the ResourceTiming time base, - // avoiding the dependency on headersEndWall (which may be zero for cross-session - // iframe requests where onResponseReceived can't find the request). - const maxReasonableMs = 300000 // 5 minutes - receiving = m.calcReceiving(req, t, maxReasonableMs) - durationMs = sending + waiting + receiving // same as k6 http: exclude blocked/connecting/tls - } else { - durationMs = k6metrics.D(wallTime.Sub(req.wallTime)) - } + timings := m.responseTimings(req, resp, wallTime) // Requests served from cache (disk/memory/prefetch cache or a service // worker) involved no network activity, so they are not emitted as @@ -352,43 +372,51 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { Samples: []k6metrics.Sample{ { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqDuration, Tags: tags}, - Value: durationMs, + Value: timings.durationMs, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqBlocked, Tags: tags}, - Value: blocked, + Value: timings.blocked, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqConnecting, Tags: tags}, - Value: connecting, + Value: timings.connecting, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqTLSHandshaking, Tags: tags}, - Value: tlsHandshaking, + Value: timings.tlsHandshaking, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqSending, Tags: tags}, - Value: sending, + Value: timings.sending, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqWaiting, Tags: tags}, - Value: waiting, + Value: timings.waiting, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqReceiving, Tags: tags}, - Value: receiving, + Value: timings.receiving, Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserDataReceived, Tags: tags}, Value: float64(bodySize), Time: wallTime, + Metadata: tagsAndMeta.Metadata, }, }, }) @@ -398,12 +426,13 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqFailed, Tags: tags}, Value: failed, Time: wallTime, + Metadata: tagsAndMeta.Metadata, } if failed == 1 { reqHeaders, reqBody := req.formatErrorRequestData() sample.HTTPErrorReqHeaders = reqHeaders sample.HTTPErrorReqBody = reqBody - // Fetch response body so error reporting includes it (formatErrorResponseData only uses already-loaded body otherwise). + // Fetch the response body for error reporting; formatting only uses an already-loaded body. _ = resp.fetchBody() resHeaders, resBody := resp.formatErrorResponseData() sample.HTTPErrorResHeaders = resHeaders @@ -415,6 +444,60 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) { } } +type browserResponseTimings struct { + blocked, connecting, tlsHandshaking, sending, waiting, receiving, durationMs float64 +} + +func (m *NetworkManager) responseTimings( + req *Request, resp *Response, wallTime time.Time, +) browserResponseTimings { + // CDP ResourceTiming: RequestTime is baseline (seconds); other fields are ms relative to it. + // k6 time metrics are emitted in milliseconds (see metrics.D), so we keep all timings in ms here. + // Blocked includes DNS, queuing, connecting, and TLS: like k6 GetConn to GotConn. + var timings browserResponseTimings + if resp == nil || resp.timing == nil { + timings.durationMs = k6metrics.D(wallTime.Sub(req.wallTime)) + return timings + } + t := resp.timing + timings.blocked = t.SendStart + if timings.blocked < 0 { + timings.blocked = 0 + } + if t.SslStart > 0 { + timings.connecting = t.SslStart - t.ConnectStart + timings.tlsHandshaking = t.ConnectEnd - t.SslStart + } else { + timings.connecting = t.ConnectEnd - t.ConnectStart + } + if timings.connecting < 0 { + timings.connecting = 0 + } + if timings.tlsHandshaking < 0 { + timings.tlsHandshaking = 0 + } + + timings.sending = t.SendEnd - t.SendStart + if timings.sending < 0 { + timings.sending = 0 + } + timings.waiting = t.ReceiveHeadersEnd - t.SendEnd + if timings.waiting < 0 { + timings.waiting = 0 + } + // receiving = time from headers received to response complete. + // Primary approach: convert ResourceTiming.RequestTime (monotonic seconds since boot) + // to a Go time.Time using cdp.MonotonicTimeEpoch, then compute responseEnd in ms + // relative to RequestTime. This keeps everything in the ResourceTiming time base, + // avoiding the dependency on headersEndWall (which may be zero for cross-session + // iframe requests where onResponseReceived can't find the request). + const maxReasonableMs = 300000 // 5 minutes + timings.receiving = m.calcReceiving(req, t, maxReasonableMs) + // Match k6 HTTP: duration excludes blocked, connecting, and TLS time. + timings.durationMs = timings.sending + timings.waiting + timings.receiving + return timings +} + // calcReceiving computes the receiving phase duration in milliseconds. // It tries two approaches in order: // 1. Convert ResourceTiming.RequestTime (monotonic seconds since boot) to a Go time.Time @@ -461,6 +544,9 @@ func (m *NetworkManager) calcReceiving(req *Request, t *network.ResourceTiming, // added as the "error" tag so CSV and other outputs can show it. func (m *NetworkManager) emitRequestFailedMetrics(req *Request) { state := m.vu.State() + if state == nil { + return + } wallTime := time.Now() url := req.url.String() @@ -475,7 +561,11 @@ func (m *NetworkManager) emitRequestFailedMetrics(req *Request) { } } - tags := state.Tags.GetCurrentValues().Tags + tagsAndMeta := req.tagsAndMeta + if tagsAndMeta.Tags == nil { + tagsAndMeta = state.Tags.GetCurrentValues() + } + tags := tagsAndMeta.Tags if state.Options.SystemTags.Has(k6metrics.TagMethod) { tags = tags.With("method", req.method) } @@ -496,59 +586,25 @@ func (m *NetworkManager) emitRequestFailedMetrics(req *Request) { durationMs := k6metrics.D(wallTime.Sub(req.wallTime)) reqHeaders, reqBody := req.formatErrorRequestData() - k6metrics.PushIfNotDone(m.vu.Context(), state.Samples, k6metrics.ConnectedSamples{ - Samples: []k6metrics.Sample{ - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqDuration, Tags: tags}, - Value: durationMs, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqBlocked, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqConnecting, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqTLSHandshaking, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqSending, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqWaiting, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqReceiving, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserDataReceived, Tags: tags}, - Value: 0, - Time: wallTime, - }, - { - TimeSeries: k6metrics.TimeSeries{Metric: m.customMetrics.BrowserHTTPReqFailed, Tags: tags}, - Value: 1, - Time: wallTime, - HTTPErrorReqHeaders: reqHeaders, - HTTPErrorReqBody: reqBody, - HTTPErrorResHeaders: "", - HTTPErrorResBody: "", - }, - }, - }) + samples := make([]k6metrics.Sample, 0, 9) + for _, metric := range []*k6metrics.Metric{ + m.customMetrics.BrowserHTTPReqDuration, m.customMetrics.BrowserHTTPReqBlocked, + m.customMetrics.BrowserHTTPReqConnecting, m.customMetrics.BrowserHTTPReqTLSHandshaking, + m.customMetrics.BrowserHTTPReqSending, m.customMetrics.BrowserHTTPReqWaiting, + m.customMetrics.BrowserHTTPReqReceiving, m.customMetrics.BrowserDataReceived, + m.customMetrics.BrowserHTTPReqFailed, + } { + samples = append(samples, k6metrics.Sample{ + TimeSeries: k6metrics.TimeSeries{Metric: metric, Tags: tags}, + Time: wallTime, Metadata: tagsAndMeta.Metadata, + }) + } + samples[0].Value = durationMs + failed := &samples[len(samples)-1] + failed.Value = 1 + failed.HTTPErrorReqHeaders = reqHeaders + failed.HTTPErrorReqBody = reqBody + k6metrics.PushIfNotDone(m.vu.Context(), state.Samples, k6metrics.ConnectedSamples{Samples: samples}) } // handleURLTag will check if the url tag needs to be grouped by testing @@ -568,15 +624,22 @@ func handleURLTag(mi eventInterceptor, url string, method string, tags *k6metric } func (m *NetworkManager) handleRequestRedirect( - req *Request, redirectResponse *network.Response, timestamp *cdp.MonotonicTime, + req *Request, redirectResponse *network.Response, hasExtraInfo bool, timestamp *cdp.MonotonicTime, ) { resp := NewHTTPResponse(m.ctx, req, redirectResponse, timestamp) + m.extraInfoTracker.processResponse(req.requestID, resp, hasExtraInfo) req.responseMu.Lock() req.response = resp req.responseMu.Unlock() req.redirectChain = append(req.redirectChain, req) - m.emitResponseMetrics(resp, req) + // Emit the response metrics once the raw headers are resolved, off the + // event goroutine so the wait does not block the goroutine that resolves + // them (the redirect's ExtraInfo may still be in flight here). + m.wg.Go(func() { + resp.WaitForRawHeaders() + m.emitResponseMetrics(resp, req) + }) m.deleteRequestByID(req.requestID) /* @@ -613,8 +676,10 @@ func (m *NetworkManager) initEvents() { cdproto.EventNetworkLoadingFailed, cdproto.EventNetworkLoadingFinished, cdproto.EventNetworkRequestWillBeSent, + cdproto.EventNetworkRequestWillBeSentExtraInfo, cdproto.EventNetworkRequestServedFromCache, cdproto.EventNetworkResponseReceived, + cdproto.EventNetworkResponseReceivedExtraInfo, cdproto.EventFetchRequestPaused, cdproto.EventFetchAuthRequired, }, chHandler) @@ -646,10 +711,14 @@ func (m *NetworkManager) handleEvents(in <-chan Event) bool { m.onLoadingFinished(ev) case *network.EventRequestWillBeSent: m.onRequestWillBeSent(ev) + case *network.EventRequestWillBeSentExtraInfo: + m.onRequestWillBeSentExtraInfo(ev) case *network.EventRequestServedFromCache: m.onRequestServedFromCache(ev) case *network.EventResponseReceived: m.onResponseReceived(ev) + case *network.EventResponseReceivedExtraInfo: + m.onResponseReceivedExtraInfo(ev) case *fetch.EventRequestPaused: m.onRequestPaused(ev) case *fetch.EventAuthRequired: @@ -676,6 +745,8 @@ func (m *NetworkManager) onLoadingFailed(event *network.EventLoadingFailed) { m.eventInterceptor.onRequestFailed(req) m.deleteRequestByID(event.RequestID) + m.extraInfoTracker.loadingFailed(event.RequestID) + req.resolveRawHeaders() m.frameManager.requestFailed(req, event.Canceled) } @@ -690,6 +761,8 @@ func (m *NetworkManager) onLoadingFinished(event *network.EventLoadingFinished) req.responseEndTiming = float64(event.Timestamp.Time().Sub(req.timestamp).Milliseconds()) req.encodedDataLength = int64(event.EncodedDataLength) m.deleteRequestByID(event.RequestID) + m.extraInfoTracker.loadingFinished(event.RequestID) + req.resolveRawHeaders() m.frameManager.requestFinished(req) m.eventInterceptor.onRequestFinished(req) @@ -697,26 +770,21 @@ func (m *NetworkManager) onLoadingFinished(event *network.EventLoadingFinished) if isInternalURL(req.url) { return } - emitResponseMetrics := func() { + // Emit the response metrics in a separate goroutine, once the raw headers + // are resolved, so the reported size is computed from a stable source. + // This must not run on the NetworkManager event goroutine: waiting for the + // raw headers there would block the goroutine that resolves them. Spawning + // a goroutine is also required for intercepted requests, so that CDP + // requestPaused messages can still be processed while a redirected main + // page request waits for its subrequest to finish loading. + m.wg.Go(func() { req.responseMu.RLock() - m.emitResponseMetrics(req.response, req) + resp := req.response req.responseMu.RUnlock() - } - if !req.allowInterception { - emitResponseMetrics() - return - } - // When request interception is enabled, we need to process requestPaused messages - // from CDP in order to get the response for the request. However, we can't process - // them until the request is unblocked. Since we're blocking the NetworkManager - // goroutine here, we need to spawn a new goroutine to allow the requestPaused - // messages to be processed by the NetworkManager. - // - // This happens when the main page request redirects before it finishes loading. - // So the new redirect request will be blocked until the main page finishes loading. - // The main page will wait forever since its subrequest is blocked. - m.wg.Go(func() { - emitResponseMetrics() + if resp != nil { + resp.WaitForRawHeaders() + } + m.emitResponseMetrics(resp, req) }) } @@ -765,12 +833,13 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, if event.RedirectResponse != nil { req, ok := m.requestFromID(event.RequestID) if ok { - m.handleRequestRedirect(req, event.RedirectResponse, event.Timestamp) + m.handleRequestRedirect(req, event.RedirectResponse, event.RedirectHasExtraInfo, event.Timestamp) redirectChain = req.redirectChain } } else { redirectChain = make([]*Request, 0) } + tagsAndMeta, networkOperation := m.networkTagsAndMeta(m.vu.State(), event, redirectChain) var frame *Frame = nil var ok bool @@ -807,6 +876,8 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, redirectChain: redirectChain, interceptionID: interceptionID, allowInterception: m.userReqInterceptionEnabled, + tagsAndMeta: tagsAndMeta, + networkOperation: networkOperation, }) if err != nil { m.logger.Errorf("NetworkManager", "creating request: %s", err) @@ -820,7 +891,13 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, m.reqsMu.Lock() m.reqIDToRequest[event.RequestID] = req m.reqsMu.Unlock() - m.emitRequestMetrics(req) + // Emit the request metrics once the raw headers are resolved, off the + // event goroutine so the wait does not block the goroutine that resolves + // them. The metric uses req.wallTime, so the deferral does not skew it. + m.wg.Go(func() { + req.WaitForRawHeaders() + m.emitRequestMetrics(req) + }) m.frameManager.requestStarted(req) m.eventInterceptor.onRequest(req) @@ -851,6 +928,14 @@ func (m *NetworkManager) onRequestWillBeSent(event *network.EventRequestWillBeSe } } +func (m *NetworkManager) onRequestWillBeSentExtraInfo(event *network.EventRequestWillBeSentExtraInfo) { + extra := parseExtraHeaders(event.Headers) + if len(extra) == 0 { + return + } + m.extraInfoTracker.requestWillBeSentExtraInfo(event.RequestID, extra) +} + // onRequestPaused can send one of these two CDP events: // - Fetch.failRequest if the URL is part of the blocked hosts or IPs // - Fetch.continueRequest if the request is not blocked and no route is configured @@ -997,6 +1082,7 @@ func (m *NetworkManager) onAuthRequired(event *fetch.EventAuthRequired) { } func (m *NetworkManager) onRequestServedFromCache(event *network.EventRequestServedFromCache) { + m.extraInfoTracker.requestServedFromCache(event.RequestID) req, ok := m.requestFromID(event.RequestID) if ok { req.setLoadedFromCache(true) @@ -1010,6 +1096,7 @@ func (m *NetworkManager) onResponseReceived(event *network.EventResponseReceived return } resp := NewHTTPResponse(m.ctx, req, event.Response, event.Timestamp) + m.extraInfoTracker.processResponse(event.RequestID, resp, event.HasExtraInfo) req.responseMu.Lock() req.response = resp // Use event.Timestamp for headersEndWall so it shares the same time base as responseEndWall @@ -1024,6 +1111,14 @@ func (m *NetworkManager) onResponseReceived(event *network.EventResponseReceived m.eventInterceptor.onResponse(resp) } +func (m *NetworkManager) onResponseReceivedExtraInfo(event *network.EventResponseReceivedExtraInfo) { + extra := parseExtraHeaders(event.Headers) + if len(extra) == 0 { + return + } + m.extraInfoTracker.responseReceivedExtraInfo(event.RequestID, extra) +} + func (m *NetworkManager) requestFromID(reqID network.RequestID) (*Request, bool) { m.reqsMu.RLock() defer m.reqsMu.RUnlock() @@ -1315,3 +1410,40 @@ func (m *NetworkManager) SetCacheEnabled(enabled bool) { func (m *NetworkManager) wait() { m.wg.Wait() } + +func parseExtraHeaders(headers network.Headers) map[string][]string { + if len(headers) == 0 { + return nil + } + parsed := make(map[string][]string, len(headers)) + for name, value := range headers { + switch v := value.(type) { + case string: + parsed[name] = splitHeaderValues(v) + case []string: + parsed[name] = append([]string{}, v...) + case []any: + values := make([]string, 0, len(v)) + for _, item := range v { + if s, ok := item.(string); ok { + values = append(values, s) + } + } + if len(values) > 0 { + parsed[name] = values + } + } + } + if len(parsed) == 0 { + return nil + } + return parsed +} + +// CDP concatenates duplicate header values with newlines; split to match Playwright. +func splitHeaderValues(value string) []string { + if strings.Contains(value, "\n") { + return strings.Split(value, "\n") + } + return []string{value} +} diff --git a/internal/js/modules/k6/browser/common/network_manager_test.go b/internal/js/modules/k6/browser/common/network_manager_test.go index 4697ceba0..d1338a4a0 100644 --- a/internal/js/modules/k6/browser/common/network_manager_test.go +++ b/internal/js/modules/k6/browser/common/network_manager_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "go.k6.io/k6/v2/internal/features" "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" @@ -319,6 +320,161 @@ func TestNetworkManagerEmitRequestResponseMetricsTimingSkew(t *testing.T) { } } +func TestNetworkManagerMetricsRetainRequestTagsAndMetadata(t *testing.T) { + t.Parallel() + + registry := k6metrics.NewRegistry() + k6m := k6ext.RegisterCustomMetrics(registry) + vu := k6test.NewVU(t) + vu.ActivateVU() + + state := vu.State() + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag(k6metrics.TagGroup.String(), "::request") + tagsAndMeta.SetTag("phase", "request") + tagsAndMeta.SetMetadata("trace", "request") + }) + + now := time.Now() + req, err := NewRequest(vu.Context(), log.NewNullLogger(), NewRequestParams{ + event: &network.EventRequestWillBeSent{ + Request: &network.Request{URL: "https://example.test/"}, + Timestamp: (*cdp.MonotonicTime)(&now), + WallTime: (*cdp.TimeSinceEpoch)(&now), + }, + tagsAndMeta: state.Tags.GetCurrentValues(), + }) + require.NoError(t, err) + + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag(k6metrics.TagGroup.String(), "") + tagsAndMeta.SetTag("phase", "after") + tagsAndMeta.SetMetadata("trace", "after") + }) + + nm := &NetworkManager{ + ctx: vu.Context(), + vu: vu, + customMetrics: k6m, + eventInterceptor: &EventInterceptorMock{}, + } + nm.emitRequestMetrics(req) + resp := NewHTTPResponse(vu.Context(), req, &network.Response{ + URL: req.URL(), + Timing: &network.ResourceTiming{}, + }, (*cdp.MonotonicTime)(&now)) + nm.emitResponseMetrics(resp, req) + + n := vu.AssertSamples(func(sample k6metrics.Sample) { + group, ok := sample.Tags.Get(k6metrics.TagGroup.String()) + require.True(t, ok) + assert.Equal(t, "::request", group) + phase, ok := sample.Tags.Get("phase") + require.True(t, ok) + assert.Equal(t, "request", phase) + assert.Equal(t, "request", sample.Metadata["trace"]) + }) + assert.Equal(t, 10, n) + + // The fork also emits samples when navigation fails before any response. + // Those must use the request's context, not the VU's restored context. + req.setErrorText("net::ERR_CONNECTION_REFUSED") + nm.emitRequestFailedMetrics(req) + n = vu.AssertSamples(func(sample k6metrics.Sample) { + group, _ := sample.Tags.Get(k6metrics.TagGroup.String()) + assert.Equal(t, "::request", group) + phase, _ := sample.Tags.Get("phase") + assert.Equal(t, "request", phase) + assert.Equal(t, "request", sample.Metadata["trace"]) + errorText, _ := sample.Tags.Get("error") + assert.Equal(t, "net::ERR_CONNECTION_REFUSED", errorText) + }) + assert.Equal(t, 9, n) +} + +func TestNetworkManagerRedirectKeepsLoaderOperation(t *testing.T) { + t.Parallel() + + nm, _ := newTestNetworkManager(t, k6lib.Options{}) + state := nm.vu.State() + state.FeatureFlags = &features.Flags{AsyncMetricContext: true} + page := &Page{} + nm.frameManager.page = page + + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag("operation", "navigation") + }) + navigationFinishAndRetain, _ := page.BeginNetworkOperation(state.Tags.GetCurrentValues()) + navigationTags, navigationOperation := nm.networkTagsAndMeta(state, &network.EventRequestWillBeSent{ + FrameID: cdp.FrameID("frame"), + LoaderID: cdp.LoaderID("initial-loader"), + Type: network.ResourceTypeDocument, + }, nil) + require.NotNil(t, navigationOperation) + navigationFinishAndRetain() + + state.Tags.Modify(func(tagsAndMeta *k6metrics.TagsAndMeta) { + tagsAndMeta.SetTag("operation", "unrelated") + }) + unrelatedFinishAndRetain, _ := page.BeginNetworkOperation(state.Tags.GetCurrentValues()) + defer unrelatedFinishAndRetain() + + redirectTags, redirectOperation := nm.networkTagsAndMeta(state, &network.EventRequestWillBeSent{ + FrameID: cdp.FrameID("frame"), + LoaderID: cdp.LoaderID("redirect-loader"), + Type: network.ResourceTypeDocument, + }, []*Request{{ + tagsAndMeta: navigationTags, + networkOperation: navigationOperation, + }}) + assert.Same(t, navigationOperation, redirectOperation) + operation, _ := redirectTags.Tags.Get("operation") + assert.Equal(t, "navigation", operation) + + loaderTags, loaderOperation, ok := page.getNetworkTagsAndMetaForRequest( + cdp.FrameID("frame"), cdp.LoaderID("redirect-loader"), true, false, + ) + require.True(t, ok) + assert.Same(t, navigationOperation, loaderOperation) + operation, _ = loaderTags.Tags.Get("operation") + assert.Equal(t, "navigation", operation) +} + +func TestRequestLoaderContext(t *testing.T) { + t.Parallel() + + for _, testCase := range []struct { + name string + typ network.ResourceType + initiator *network.Initiator + prefer bool + bind bool + }{ + {name: "document", typ: network.ResourceTypeDocument, prefer: true, bind: true}, + {name: "parser", initiator: &network.Initiator{Type: network.InitiatorTypeParser}, prefer: true}, + {name: "preload", initiator: &network.Initiator{Type: network.InitiatorTypePreload}, prefer: true}, + { + name: "signed exchange", + initiator: &network.Initiator{Type: network.InitiatorTypeSignedExchange}, + prefer: true, + }, + {name: "script", initiator: &network.Initiator{Type: network.InitiatorTypeScript}}, + {name: "preflight", initiator: &network.Initiator{Type: network.InitiatorTypePreflight}}, + {name: "unknown initiator"}, + } { + t.Run(testCase.name, func(t *testing.T) { + t.Parallel() + + prefer, bind := requestLoaderContext(&network.EventRequestWillBeSent{ + Type: testCase.typ, + Initiator: testCase.initiator, + }) + assert.Equal(t, testCase.prefer, prefer) + assert.Equal(t, testCase.bind, bind) + }) + } +} + func TestRequestForOnLoadingFinished(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/common/page.go b/internal/js/modules/k6/browser/common/page.go index fa7735c7b..2be0fd43c 100644 --- a/internal/js/modules/k6/browser/common/page.go +++ b/internal/js/modules/k6/browser/common/page.go @@ -27,6 +27,7 @@ import ( "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext" "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" + k6metrics "go.k6.io/k6/v2/metrics" ) // BlankPage represents a blank page. @@ -91,12 +92,14 @@ func (r ReducedMotion) String() string { return reducedMotionToString[r] } -var reducedMotionToString = map[ReducedMotion]string{ //nolint:gochecknoglobals +//nolint:goconst,gochecknoglobals +var reducedMotionToString = map[ReducedMotion]string{ ReducedMotionReduce: "reduce", ReducedMotionNoPreference: "no-preference", } -var reducedMotionToID = map[string]ReducedMotion{ //nolint:gochecknoglobals +//nolint:gochecknoglobals +var reducedMotionToID = map[string]ReducedMotion{ "reduce": ReducedMotionReduce, "no-preference": ReducedMotionNoPreference, } @@ -271,6 +274,15 @@ type Page struct { backgroundPage bool + // Supplies the fallback for requests that cannot be associated with an active browser operation. + networkFallbackTagsAndMeta atomic.Pointer[k6metrics.TagsAndMeta] + networkRecentOperation *networkOperationContext + networkOperationID atomic.Uint64 + networkOperations []*networkOperationContext + networkLoaderOperations map[cdp.LoaderID]*networkOperationContext + networkFrameLoaders map[cdp.FrameID]cdp.LoaderID + networkOperationsMu sync.Mutex + eventCh chan Event eventHandlers map[PageEventName][]pageEventHandlerRecord eventHandlersMu sync.RWMutex @@ -323,6 +335,14 @@ func NewPage( logger: logger, } + // Popup pages are created off the event loop, so inherit the context of the operation that opened + // them, or the opener's fallback when no operation is active. + if opener != nil { + if tagsAndMeta, ok := opener.getNetworkTagsAndMeta(); ok { + p.networkFallbackTagsAndMeta.Store(&tagsAndMeta) + } + } + p.logger.Debugf("Page:NewPage", "sid:%v tid:%v backgroundPage:%t", p.sessionID(), tid, bp) @@ -800,21 +820,24 @@ func (p *Page) hasRoutes() bool { return len(p.routes) > 0 } -func (p *Page) resetViewport() error { +func (p *Page) resetViewport(ctx context.Context) error { p.logger.Debugf("Page:resetViewport", "sid:%v", p.sessionID()) action := emulation.SetDeviceMetricsOverride(0, 0, 0, false) - return action.Do(cdp.WithExecutor(p.ctx, p.session)) + return action.Do(cdp.WithExecutor(ctx, p.session)) } -func (p *Page) setEmulatedSize(emulatedSize *EmulatedSize) error { +func (p *Page) setEmulatedSize(ctx context.Context, emulatedSize *EmulatedSize) error { p.logger.Debugf("Page:setEmulatedSize", "sid:%v", p.sessionID()) + if err := p.mainFrameSession.updateViewport(ctx, emulatedSize); err != nil { + return err + } p.emulatedSize = emulatedSize - return p.mainFrameSession.updateViewport() + return nil } -func (p *Page) setViewportSize(viewportSize *Size) error { +func (p *Page) setViewportSize(ctx context.Context, viewportSize *Size) error { p.logger.Debugf("Page:setViewportSize", "sid:%v vps:%v", p.sessionID(), viewportSize) @@ -826,7 +849,7 @@ func (p *Page) setViewportSize(viewportSize *Size) error { Width: int64(viewportSize.Width), Height: int64(viewportSize.Height), } - return p.setEmulatedSize(NewEmulatedSize(viewport, screen)) + return p.setEmulatedSize(ctx, NewEmulatedSize(viewport, screen)) } func (p *Page) updateExtraHTTPHeaders() error { @@ -1221,6 +1244,123 @@ func (p *Page) GetTouchscreen() *Touchscreen { return p.Touchscreen } +type networkOperationContext struct { + id uint64 + tagsAndMeta k6metrics.TagsAndMeta +} + +// SetNetworkFallbackTagsAndMeta records the tags and metadata for requests that cannot be +// associated with an active browser operation. +func (p *Page) SetNetworkFallbackTagsAndMeta(tagsAndMeta k6metrics.TagsAndMeta) { + p.networkFallbackTagsAndMeta.Store(&tagsAndMeta) +} + +// BeginNetworkOperation makes tagsAndMeta available to requests observed while the operation is +// active. Calling finishAndRetain keeps it for CDP request events that arrive just after the +// operation settles, including unsuccessful settlement. Calling abortAndDiscard instead removes it +// when the browser operation was not launched. +func (p *Page) BeginNetworkOperation( + tagsAndMeta k6metrics.TagsAndMeta, +) (finishAndRetain, abortAndDiscard func()) { + operation := &networkOperationContext{ + id: p.networkOperationID.Add(1), + tagsAndMeta: tagsAndMeta, + } + p.networkOperationsMu.Lock() + p.networkOperations = append(p.networkOperations, operation) + p.networkOperationsMu.Unlock() + + var once sync.Once + remove := func(keepRecent bool) { + once.Do(func() { + p.networkOperationsMu.Lock() + defer p.networkOperationsMu.Unlock() + if keepRecent { + p.networkRecentOperation = operation + } + for i := range p.networkOperations { + if p.networkOperations[i].id != operation.id { + continue + } + p.networkOperations = append(p.networkOperations[:i], p.networkOperations[i+1:]...) + return + } + }) + } + return func() { remove(true) }, func() { remove(false) } +} + +func (p *Page) getNetworkTagsAndMeta() (k6metrics.TagsAndMeta, bool) { + tagsAndMeta, _, ok := p.getNetworkTagsAndMetaForRequest("", "", false, false) + return tagsAndMeta, ok +} + +func (p *Page) getNetworkTagsAndMetaForRequest( + frameID cdp.FrameID, + loaderID cdp.LoaderID, + preferLoaderContext bool, + bindLoaderContext bool, +) (k6metrics.TagsAndMeta, *networkOperationContext, bool) { + p.networkOperationsMu.Lock() + var operation *networkOperationContext + if preferLoaderContext && loaderID != "" { + operation = p.networkLoaderOperations[loaderID] + } + if operation == nil { + if n := len(p.networkOperations); n > 0 { + operation = p.networkOperations[n-1] + } else { + operation = p.networkRecentOperation + } + } + if bindLoaderContext && loaderID != "" && operation != nil { + p.bindNetworkLoaderOperationLocked(frameID, loaderID, operation) + } + p.networkOperationsMu.Unlock() + + if operation != nil { + return operation.tagsAndMeta, operation, true + } + + if tagsAndMeta := p.networkFallbackTagsAndMeta.Load(); tagsAndMeta != nil { + return *tagsAndMeta, nil, true + } + return k6metrics.TagsAndMeta{}, nil, false +} + +func (p *Page) bindNetworkLoaderOperation( + frameID cdp.FrameID, loaderID cdp.LoaderID, operation *networkOperationContext, +) { + if loaderID == "" || operation == nil { + return + } + p.networkOperationsMu.Lock() + defer p.networkOperationsMu.Unlock() + p.bindNetworkLoaderOperationLocked(frameID, loaderID, operation) +} + +func (p *Page) bindNetworkLoaderOperationLocked( + frameID cdp.FrameID, loaderID cdp.LoaderID, operation *networkOperationContext, +) { + if p.networkLoaderOperations == nil { + p.networkLoaderOperations = make(map[cdp.LoaderID]*networkOperationContext) + p.networkFrameLoaders = make(map[cdp.FrameID]cdp.LoaderID) + } + if previousLoaderID := p.networkFrameLoaders[frameID]; previousLoaderID != loaderID { + delete(p.networkLoaderOperations, previousLoaderID) + } + p.networkLoaderOperations[loaderID] = operation + p.networkFrameLoaders[frameID] = loaderID +} + +func (p *Page) clearNetworkLoaderContext(frameID cdp.FrameID) { + p.networkOperationsMu.Lock() + loaderID := p.networkFrameLoaders[frameID] + delete(p.networkLoaderOperations, loaderID) + delete(p.networkFrameLoaders, frameID) + p.networkOperationsMu.Unlock() +} + // Goto will navigate the page to the specified URL and return a HTTP response object. func (p *Page) Goto(url string, opts *FrameGotoOptions) (*Response, error) { p.logger.Debugf("Page:Goto", "sid:%v url:%q", p.sessionID(), url) @@ -1356,7 +1496,9 @@ func (p *Page) Referrer() string { func (p *Page) Route(path string, cb RouteHandlerCallback, rm RegExMatcher) error { p.logger.Debugf("Page:Route", "sid:%v path:%s", p.sessionID(), path) - if !p.hasRoutes() { + p.routesMu.Lock() + defer p.routesMu.Unlock() + if len(p.routes) == 0 { err := p.mainFrameSession.updateRequestInterception(true) if err != nil { return err @@ -1369,8 +1511,6 @@ func (p *Page) Route(path string, cb RouteHandlerCallback, rm RegExMatcher) erro } routeHandler := NewRouteHandler(path, cb, matcher) - p.routesMu.Lock() - defer p.routesMu.Unlock() // Append new route at the beginning of the slice as, when several routes match the given pattern, // they will run in the opposite order to their registration. p.routes = append([]*RouteHandler{routeHandler}, p.routes...) @@ -1694,9 +1834,13 @@ func (p *Page) Screenshot(opts *PageScreenshotOptions, sp ScreenshotPersister) ( span.SetAttributes(attribute.String("screenshot.path", opts.Path)) - s := newScreenshotter(spanCtx, sp, p.logger) + s, cancel := newScreenshotter(spanCtx, p.defaultTimeout(), sp, p.logger) + defer cancel() buf, err := s.screenshotPage(p, opts) if err != nil { + if errors.Is(s.ctx.Err(), context.DeadlineExceeded) && p.ctx.Err() == nil { + err = &k6ext.UserFriendlyError{Err: err, Timeout: p.defaultTimeout()} + } return nil, spanRecordErrorf(span, "taking screenshot of page: %w", err) } @@ -1751,7 +1895,7 @@ func (p *Page) SetInputFiles(selector string, files *Files, opts *FrameSetInputF func (p *Page) SetViewportSize(viewportSize *Size) error { p.logger.Debugf("Page:SetViewportSize", "sid:%v", p.sessionID()) - if err := p.setViewportSize(viewportSize); err != nil { + if err := p.setViewportSize(p.ctx, viewportSize); err != nil { return fmt.Errorf("setting viewport size: %w", err) } diff --git a/internal/js/modules/k6/browser/common/page_test.go b/internal/js/modules/k6/browser/common/page_test.go index 001da5224..9af4d9fc8 100644 --- a/internal/js/modules/k6/browser/common/page_test.go +++ b/internal/js/modules/k6/browser/common/page_test.go @@ -5,8 +5,11 @@ import ( "errors" "testing" + "github.com/chromedp/cdproto/cdp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/metrics" ) // TestPageLocator can be removed later on when we add integration @@ -163,6 +166,97 @@ func TestPageOn(t *testing.T) { err := p.On("metric", handler) assert.NoError(t, err) - assert.Len(t, p.eventHandlers[("metric")], 1) + assert.Len(t, p.eventHandlers["metric"], 1) }) } + +func TestPageNetworkOperationContext(t *testing.T) { + t.Parallel() + + registry := metrics.NewRegistry() + metricContext := func(name string) metrics.TagsAndMeta { + return metrics.TagsAndMeta{ + Tags: registry.RootTagSet().With("context", name), + Metadata: map[string]string{"context": name}, + } + } + contextName := func(tagsAndMeta metrics.TagsAndMeta) string { + name, _ := tagsAndMeta.Tags.Get("context") + assert.Equal(t, name, tagsAndMeta.Metadata["context"]) + return name + } + + p := &Page{} + p.SetNetworkFallbackTagsAndMeta(metricContext("fallback")) + tagsAndMeta, ok := p.getNetworkTagsAndMeta() + require.True(t, ok) + assert.Equal(t, "fallback", contextName(tagsAndMeta)) + + firstFinishAndRetain, _ := p.BeginNetworkOperation(metricContext("first")) + secondFinishAndRetain, _ := p.BeginNetworkOperation(metricContext("second")) + + tagsAndMeta, ok = p.getNetworkTagsAndMeta() + require.True(t, ok) + assert.Equal(t, "second", contextName(tagsAndMeta)) + + firstFinishAndRetain() + tagsAndMeta, ok = p.getNetworkTagsAndMeta() + require.True(t, ok) + assert.Equal(t, "second", contextName(tagsAndMeta)) + + secondFinishAndRetain() + secondFinishAndRetain() + tagsAndMeta, ok = p.getNetworkTagsAndMeta() + require.True(t, ok) + assert.Equal(t, "second", contextName(tagsAndMeta)) + + _, failedAbortAndDiscard := p.BeginNetworkOperation(metricContext("failed")) + failedAbortAndDiscard() + tagsAndMeta, ok = p.getNetworkTagsAndMeta() + require.True(t, ok) + assert.Equal(t, "second", contextName(tagsAndMeta)) +} + +func TestPageNetworkLoaderContextOutlivesUnrelatedCalls(t *testing.T) { + t.Parallel() + + registry := metrics.NewRegistry() + metricContext := func(name string) metrics.TagsAndMeta { + return metrics.TagsAndMeta{Tags: registry.RootTagSet().With("context", name)} + } + contextName := func(tagsAndMeta metrics.TagsAndMeta) string { + name, _ := tagsAndMeta.Tags.Get("context") + return name + } + + p := &Page{} + navigationFinishAndRetain, _ := p.BeginNetworkOperation(metricContext("navigation")) + tagsAndMeta, _, ok := p.getNetworkTagsAndMetaForRequest( + cdp.FrameID("frame"), cdp.LoaderID("loader"), true, true, + ) + require.True(t, ok) + assert.Equal(t, "navigation", contextName(tagsAndMeta)) + navigationFinishAndRetain() + + inspectionFinishAndRetain, _ := p.BeginNetworkOperation(metricContext("inspection")) + inspectionFinishAndRetain() + + tagsAndMeta, _, ok = p.getNetworkTagsAndMetaForRequest( + cdp.FrameID("frame"), cdp.LoaderID("loader"), true, false, + ) + require.True(t, ok) + assert.Equal(t, "navigation", contextName(tagsAndMeta)) + + tagsAndMeta, _, ok = p.getNetworkTagsAndMetaForRequest( + cdp.FrameID("frame"), cdp.LoaderID("loader"), false, false, + ) + require.True(t, ok) + assert.Equal(t, "inspection", contextName(tagsAndMeta)) + + p.clearNetworkLoaderContext(cdp.FrameID("frame")) + tagsAndMeta, _, ok = p.getNetworkTagsAndMetaForRequest( + cdp.FrameID("frame"), cdp.LoaderID("loader"), true, false, + ) + require.True(t, ok) + assert.Equal(t, "inspection", contextName(tagsAndMeta)) +} diff --git a/internal/js/modules/k6/browser/common/screenshot_deadline_test.go b/internal/js/modules/k6/browser/common/screenshot_deadline_test.go new file mode 100644 index 000000000..697c3b9f5 --- /dev/null +++ b/internal/js/modules/k6/browser/common/screenshot_deadline_test.go @@ -0,0 +1,162 @@ +package common + +import ( + "context" + "errors" + "fmt" + "testing" + "testing/synctest" + "time" + + "github.com/chromedp/cdproto/cdp" + "github.com/chromedp/cdproto/emulation" + "github.com/chromedp/cdproto/page" + "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/log" +) + +type screenshotDeadlineSession struct { + session + blocked string + backgroundColors []*cdp.RGBA + backgroundRestoreErr bool +} + +const screenshotTimeout = 20 * time.Millisecond + +func (s *screenshotDeadlineSession) Execute(ctx context.Context, method string, params, result any) error { + if err := ctx.Err(); err != nil { + return err + } + if method == s.blocked { + <-ctx.Done() + return ctx.Err() + } + + switch method { + case "Page.getLayoutMetrics": + result.(*page.GetLayoutMetricsReturns).CSSVisualViewport = &page.VisualViewport{Scale: 1} + case "Page.captureScreenshot": + case "Emulation.setDefaultBackgroundColorOverride": + color := params.(*emulation.SetDefaultBackgroundColorOverrideParams).Color + s.backgroundColors = append(s.backgroundColors, color) + if s.backgroundRestoreErr && color == nil { + return errors.New("background restore failed") + } + case "Emulation.setDeviceMetricsOverride": + default: + return fmt.Errorf("unexpected CDP method %q", method) + } + return nil +} + +func testScreenshotPage(ctx context.Context, sess session, timeout time.Duration) *Page { + settings := NewTimeoutSettings(nil) + settings.setDefaultTimeout(timeout) + return &Page{ + ctx: ctx, session: sess, timeoutSettings: settings, logger: log.NewNullLogger(), + emulatedSize: NewEmulatedSize(Viewport{Width: 100, Height: 100}, Screen{Width: 100, Height: 100}), + } +} + +func TestScreenshotBehavior(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + blocked string + timeout, cancelAfter time.Duration + wantErr error + omitBackground bool + backgroundRestoreErr bool + }{ + { + name: "deadline at layout metrics", blocked: "Page.getLayoutMetrics", + timeout: screenshotTimeout, wantErr: context.DeadlineExceeded, + }, + { + name: "deadline at capture", blocked: "Page.captureScreenshot", + timeout: screenshotTimeout, wantErr: context.DeadlineExceeded, + }, + { + name: "restores transparent background", blocked: "Page.captureScreenshot", + timeout: screenshotTimeout, wantErr: context.DeadlineExceeded, omitBackground: true, + }, + { + name: "joins background restore error", blocked: "Page.captureScreenshot", + timeout: screenshotTimeout, wantErr: context.DeadlineExceeded, + omitBackground: true, backgroundRestoreErr: true, + }, + { + name: "parent cancellation with timeout", blocked: "Page.getLayoutMetrics", + timeout: time.Second, cancelAfter: 20 * time.Millisecond, wantErr: context.Canceled, + }, + { + name: "parent cancellation with zero timeout", blocked: "Page.getLayoutMetrics", + cancelAfter: 20 * time.Millisecond, wantErr: context.Canceled, + }, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond) + defer cancel() + if tc.cancelAfter > 0 { + go func() { + time.Sleep(tc.cancelAfter) + cancel() + }() + } + sess := &screenshotDeadlineSession{ + session: &Session{}, blocked: tc.blocked, + backgroundRestoreErr: tc.backgroundRestoreErr, + } + p := testScreenshotPage(ctx, sess, tc.timeout) + opts := NewPageScreenshotOptions() + opts.OmitBackground = tc.omitBackground + + start := time.Now() + _, err := p.Screenshot(opts, nil) + require.ErrorIs(t, err, tc.wantErr) + require.Equal(t, screenshotTimeout, time.Since(start)) + if errors.Is(tc.wantErr, context.DeadlineExceeded) { + require.ErrorContains(t, err, "timed out after 20ms") + } + if tc.omitBackground { + require.Equal(t, []*cdp.RGBA{{A: 0}, nil}, sess.backgroundColors) + } + if tc.backgroundRestoreErr { + require.ErrorContains(t, err, "resetting screenshot background color: background restore failed") + } + if tc.cancelAfter > 0 { + require.ErrorIs(t, ctx.Err(), context.Canceled) + } else { + require.NoError(t, ctx.Err()) + } + }) + }) + } +} + +func TestScreenshotRestoreUsesBoundedCleanup(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + ctx := t.Context() + sess := &screenshotDeadlineSession{session: &Session{}, blocked: "Emulation.setDeviceMetricsOverride"} + logger := log.NewNullLogger() + temporarySize := NewEmulatedSize(Viewport{Width: 300, Height: 300}, Screen{Width: 300, Height: 300}) + p := &Page{ + ctx: ctx, session: sess, logger: logger, emulatedSize: temporarySize, + browserCtx: &BrowserContext{opts: DefaultBrowserContextOptions()}, + } + p.mainFrameSession = &FrameSession{ctx: ctx, session: sess, page: p, logger: logger} + s, stop := newScreenshotter(ctx, 20*time.Millisecond, nil, logger) + stop() + + start := time.Now() + err := s.restoreViewport(p, NewEmulatedSize(Viewport{Width: 100, Height: 100}, Screen{Width: 200, Height: 200})) + require.ErrorIs(t, err, context.DeadlineExceeded) + require.Equal(t, screenshotCleanupTimeout, time.Since(start)) + require.NoError(t, ctx.Err()) + require.Same(t, temporarySize, p.emulatedSize, "failed restoration must not update the cached viewport") + }) +} diff --git a/internal/js/modules/k6/browser/common/screenshotter.go b/internal/js/modules/k6/browser/common/screenshotter.go index 4caa5b9c9..e4adf3f4f 100644 --- a/internal/js/modules/k6/browser/common/screenshotter.go +++ b/internal/js/modules/k6/browser/common/screenshotter.go @@ -9,6 +9,7 @@ import ( "io" "math" "strings" + "time" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" @@ -72,16 +73,32 @@ func (f *ImageFormat) UnmarshalJSON(b []byte) error { type screenshotter struct { ctx context.Context + parentCtx context.Context persister ScreenshotPersister logger *log.Logger } +// Cleanup gets a short independent budget after the operation deadline expires. +const screenshotCleanupTimeout = time.Second + func newScreenshotter( ctx context.Context, + timeout time.Duration, sp ScreenshotPersister, logger *log.Logger, -) *screenshotter { - return &screenshotter{ctx, sp, logger} +) (*screenshotter, context.CancelFunc) { + parentCtx := ctx + var cancel context.CancelFunc + if timeout > 0 { + ctx, cancel = context.WithTimeout(ctx, timeout) + } else { + ctx, cancel = context.WithCancel(ctx) + } + return &screenshotter{ctx: ctx, parentCtx: parentCtx, persister: sp, logger: logger}, cancel +} + +func (s *screenshotter) cleanupContext() (context.Context, context.CancelFunc) { + return context.WithTimeout(s.parentCtx, screenshotCleanupTimeout) } func (s *screenshotter) fullPageSize(p *Page) (*Size, error) { @@ -118,11 +135,10 @@ func (s *screenshotter) fullPageSize(p *Page) (*Size, error) { return &size, nil } -func (s *screenshotter) originalViewportSize(p *Page) (*Size, *Size, error) { - originalViewportSize := p.viewportSize() - viewportSize := originalViewportSize +func (s *screenshotter) originalViewportSize(p *Page) (*Size, error) { + viewportSize := p.viewportSize() if viewportSize.Width != 0 || viewportSize.Height != 0 { - return &viewportSize, &originalViewportSize, nil + return &viewportSize, nil } opts := evalOptions{ @@ -134,40 +150,58 @@ func (s *screenshotter) originalViewportSize(p *Page) (*Size, *Size, error) { { width: window.innerWidth, height: window.innerHeight } )`) if err != nil { - return nil, nil, fmt.Errorf("getting viewport dimensions: %w", err) + return nil, fmt.Errorf("getting viewport dimensions: %w", err) } var returnVal Size if err := convert(result, &returnVal); err != nil { - return nil, nil, fmt.Errorf("unpacking window size: %w", err) + return nil, fmt.Errorf("unpacking window size: %w", err) } viewportSize.Width = returnVal.Width viewportSize.Height = returnVal.Height - return &viewportSize, &originalViewportSize, nil + return &viewportSize, nil +} + +// setViewportSize waits for a paint at the new size before capturing the compositor surface. +// A device-metrics acknowledgement alone can leave the old surface in place briefly. +func (s *screenshotter) setViewportSize(p *Page, size *Size) error { + if err := p.setViewportSize(s.ctx, size); err != nil { + return err + } + _, err := p.frameManager.MainFrame().evaluate(s.ctx, utilityWorld, + evalOptions{forceCallable: true, returnByValue: true}, + `() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))`) + return err } -func (s *screenshotter) restoreViewport(p *Page, originalViewport *Size) error { - if originalViewport != nil { - return p.setViewportSize(originalViewport) +func (s *screenshotter) restoreViewport(p *Page, originalSize *EmulatedSize) error { + ctx, cancel := s.cleanupContext() + defer cancel() + if originalSize != nil { + return p.setEmulatedSize(ctx, originalSize) } - return p.resetViewport() + return p.resetViewport(ctx) } func (s *screenshotter) screenshot( sess session, doc, viewport *Rect, format ImageFormat, omitBackground bool, quality int64, path string, -) ([]byte, error) { - var ( - buf []byte - clip *cdppage.Viewport - ) +) (buf []byte, err error) { + var clip *cdppage.Viewport capture := cdppage.CaptureScreenshot() shouldSetDefaultBackground := omitBackground && format == "png" if shouldSetDefaultBackground { action := emulation.SetDefaultBackgroundColorOverride(). WithColor(&cdp.RGBA{R: 0, G: 0, B: 0, A: 0}) + defer func() { + ctx, cancel := s.cleanupContext() + defer cancel() + if restoreErr := emulation.SetDefaultBackgroundColorOverride().Do(cdp.WithExecutor(ctx, sess)); restoreErr != nil { + err = errors.Join(err, fmt.Errorf("resetting screenshot background color: %w", restoreErr)) + } + }() if err := action.Do(cdp.WithExecutor(s.ctx, sess)); err != nil { return nil, fmt.Errorf("setting screenshot background transparency: %w", err) } @@ -221,13 +255,6 @@ func (s *screenshotter) screenshot( return nil, fmt.Errorf("capturing screenshot: %w", err) } - if shouldSetDefaultBackground { - action := emulation.SetDefaultBackgroundColorOverride() - if err := action.Do(cdp.WithExecutor(s.ctx, sess)); err != nil { - return nil, fmt.Errorf("resetting screenshot background color: %w", err) - } - } - // Save screenshot capture to file if path != "" { if err := s.persister.Persist(s.ctx, path, bytes.NewBuffer(buf)); err != nil { @@ -269,12 +296,15 @@ func getViewPortDimensions(ctx context.Context, sess session, logger *log.Logger return visualViewportScale, visualViewportPageX, visualViewportPageY, nil } -func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleScreenshotOptions) ([]byte, error) { +func (s *screenshotter) screenshotElement( + h *ElementHandle, opts *ElementHandleScreenshotOptions, +) (buf []byte, err error) { format := opts.Format - viewportSize, originalViewportSize, err := s.originalViewportSize(h.frame.page) + viewportSize, err := s.originalViewportSize(h.frame.page) if err != nil { return nil, fmt.Errorf("getting original viewport size: %w", err) } + originalSize := h.frame.page.emulatedSize err = h.waitAndScrollIntoViewIfNeeded(h.ctx, false, true, opts.Timeout) if err != nil { @@ -292,14 +322,18 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS return nil, fmt.Errorf("node has 0 height") } - var overriddenViewportSize *Size fitsViewport := bbox.Width <= viewportSize.Width && bbox.Height <= viewportSize.Height if !fitsViewport { //nolint:nestif - overriddenViewportSize = Size{ + overriddenViewportSize := Size{ Width: math.Max(viewportSize.Width, bbox.Width), Height: math.Max(viewportSize.Height, bbox.Height), }.enclosingIntSize() - if err := h.frame.page.setViewportSize(overriddenViewportSize); err != nil { + defer func() { + if restoreErr := s.restoreViewport(h.frame.page, originalSize); restoreErr != nil { + err = errors.Join(err, fmt.Errorf("restoring viewport: %w", restoreErr)) + } + }() + if err := s.setViewportSize(h.frame.page, overriddenViewportSize); err != nil { return nil, fmt.Errorf("setting viewport size to %s: %w", overriddenViewportSize, err) } @@ -333,7 +367,7 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS documentRect.X += returnVal.X documentRect.Y += returnVal.Y - buf, err := s.screenshot( + return s.screenshot( h.frame.page.session, documentRect.enclosingIntRect(), nil, // viewportRect @@ -342,19 +376,9 @@ func (s *screenshotter) screenshotElement(h *ElementHandle, opts *ElementHandleS opts.Quality, opts.Path, ) - if err != nil { - return nil, err - } - if overriddenViewportSize != nil { - if err := s.restoreViewport(h.frame.page, originalViewportSize); err != nil { - return nil, fmt.Errorf("restoring viewport: %w", err) - } - } - - return buf, nil } -func (s *screenshotter) screenshotPage(p *Page, opts *PageScreenshotOptions) ([]byte, error) { +func (s *screenshotter) screenshotPage(p *Page, opts *PageScreenshotOptions) (buf []byte, err error) { format := opts.Format // Infer file format by path @@ -364,15 +388,16 @@ func (s *screenshotter) screenshotPage(p *Page, opts *PageScreenshotOptions) ([] } } - viewportSize, originalViewportSize, err := s.originalViewportSize(p) + viewportSize, err := s.originalViewportSize(p) if err != nil { return nil, fmt.Errorf("getting original viewport size: %w", err) } + originalSize := p.emulatedSize if opts.FullPage { //nolint:nestif - fullPageSize, err := s.fullPageSize(p) - if err != nil { - return nil, fmt.Errorf("getting full page size: %w", err) + fullPageSize, sizeErr := s.fullPageSize(p) + if sizeErr != nil { + return nil, fmt.Errorf("getting full page size: %w", sizeErr) } documentRect := &Rect{ X: 0, @@ -380,13 +405,17 @@ func (s *screenshotter) screenshotPage(p *Page, opts *PageScreenshotOptions) ([] Width: fullPageSize.Width, Height: fullPageSize.Height, } - var overriddenViewportSize *Size fitsViewport := fullPageSize.Width <= viewportSize.Width && fullPageSize.Height <= viewportSize.Height if !fitsViewport { - overriddenViewportSize = fullPageSize - if err := p.setViewportSize(overriddenViewportSize); err != nil { + defer func() { + if restoreErr := s.restoreViewport(p, originalSize); restoreErr != nil { + err = errors.Join(err, fmt.Errorf("restoring viewport to %s: %w", + viewportSize, restoreErr)) + } + }() + if err := s.setViewportSize(p, fullPageSize); err != nil { return nil, fmt.Errorf("setting viewport size to %s: %w", - overriddenViewportSize, err) + fullPageSize, err) } } if opts.Clip != nil { @@ -401,17 +430,7 @@ func (s *screenshotter) screenshotPage(p *Page, opts *PageScreenshotOptions) ([] } } - buf, err := s.screenshot(p.session, documentRect, nil, format, opts.OmitBackground, opts.Quality, opts.Path) - if err != nil { - return nil, err - } - if overriddenViewportSize != nil { - if err := s.restoreViewport(p, originalViewportSize); err != nil { - return nil, fmt.Errorf("restoring viewport to %s: %w", - originalViewportSize, err) - } - } - return buf, nil + return s.screenshot(p.session, documentRect, nil, format, opts.OmitBackground, opts.Quality, opts.Path) } viewportRect := &Rect{ diff --git a/internal/js/modules/k6/browser/k6ext/k6test/span_recorder.go b/internal/js/modules/k6/browser/k6ext/k6test/span_recorder.go new file mode 100644 index 000000000..08449852d --- /dev/null +++ b/internal/js/modules/k6/browser/k6ext/k6test/span_recorder.go @@ -0,0 +1,105 @@ +package k6test + +import ( + "context" + "sync" + "testing" + + "go.opentelemetry.io/otel/attribute" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + oteltrace "go.opentelemetry.io/otel/trace" +) + +// RecordedSpan is a snapshot of a span captured by a SpanRecorder, exposing just +// enough for tests to assert on span names, parentage, attributes and end state. +type RecordedSpan struct { + Name string + TraceID oteltrace.TraceID + SpanID oteltrace.SpanID + ParentID oteltrace.SpanID + Attrs []attribute.KeyValue +} + +// AttrInt64 returns the int64 value of the named attribute on the span, failing +// the test if it is absent. +func (s RecordedSpan) AttrInt64(tb testing.TB, key string) int64 { + tb.Helper() + for _, kv := range s.Attrs { + if string(kv.Key) == key { + return kv.Value.AsInt64() + } + } + tb.Fatalf("attribute %q not found on span %q", key, s.Name) + return 0 +} + +// AttrString returns the string value of the named attribute on the span, +// failing the test if it is absent. +func (s RecordedSpan) AttrString(tb testing.TB, key string) string { + tb.Helper() + for _, kv := range s.Attrs { + if string(kv.Key) == key { + return kv.Value.AsString() + } + } + tb.Fatalf("attribute %q not found on span %q", key, s.Name) + return "" +} + +// SpanRecorder is a minimal sdktrace.SpanProcessor that records started spans +// (for name/parent/attribute inspection) and which span ids have ended. +type SpanRecorder struct { + mu sync.Mutex + started []RecordedSpan + ended map[oteltrace.SpanID]bool +} + +// NewSpanRecorder returns a ready-to-use SpanRecorder. +func NewSpanRecorder() *SpanRecorder { + return &SpanRecorder{ended: make(map[oteltrace.SpanID]bool)} +} + +// OnStart implements sdktrace.SpanProcessor. +func (r *SpanRecorder) OnStart(_ context.Context, s sdktrace.ReadWriteSpan) { + r.mu.Lock() + defer r.mu.Unlock() + r.started = append(r.started, RecordedSpan{ + Name: s.Name(), + TraceID: s.SpanContext().TraceID(), + SpanID: s.SpanContext().SpanID(), + ParentID: s.Parent().SpanID(), + Attrs: s.Attributes(), + }) +} + +// OnEnd implements sdktrace.SpanProcessor. +func (r *SpanRecorder) OnEnd(s sdktrace.ReadOnlySpan) { + r.mu.Lock() + defer r.mu.Unlock() + r.ended[s.SpanContext().SpanID()] = true +} + +// Shutdown implements sdktrace.SpanProcessor. +func (r *SpanRecorder) Shutdown(context.Context) error { return nil } + +// ForceFlush implements sdktrace.SpanProcessor. +func (r *SpanRecorder) ForceFlush(context.Context) error { return nil } + +// Find returns the first recorded span with the given name. +func (r *SpanRecorder) Find(name string) (RecordedSpan, bool) { + r.mu.Lock() + defer r.mu.Unlock() + for _, s := range r.started { + if s.Name == name { + return s, true + } + } + return RecordedSpan{}, false +} + +// IsEnded reports whether the span with the given id has ended. +func (r *SpanRecorder) IsEnded(id oteltrace.SpanID) bool { + r.mu.Lock() + defer r.mu.Unlock() + return r.ended[id] +} diff --git a/internal/js/modules/k6/browser/k6ext/metrics.go b/internal/js/modules/k6/browser/k6ext/metrics.go index 84963d543..40bb2a6ce 100644 --- a/internal/js/modules/k6/browser/k6ext/metrics.go +++ b/internal/js/modules/k6/browser/k6ext/metrics.go @@ -17,32 +17,32 @@ const ( inpName = "browser_web_vital_inp" fcpName = "browser_web_vital_fcp" - browserDataSentName = "browser_data_sent" - browserDataReceivedName = "browser_data_received" - browserHTTPReqDurationName = "browser_http_req_duration" - browserHTTPReqBlockedName = "browser_http_req_blocked" - browserHTTPReqConnectingName = "browser_http_req_connecting" - browserHTTPReqTLSHandshaking = "browser_http_req_tls_handshaking" - browserHTTPReqSendingName = "browser_http_req_sending" - browserHTTPReqWaitingName = "browser_http_req_waiting" - browserHTTPReqReceivingName = "browser_http_req_receiving" - browserHTTPReqFailedName = "browser_http_req_failed" + browserDataSentName = "browser_data_sent" + browserDataReceivedName = "browser_data_received" + browserHTTPReqDurationName = "browser_http_req_duration" + browserHTTPReqBlockedName = "browser_http_req_blocked" + browserHTTPReqConnectingName = "browser_http_req_connecting" + browserHTTPReqTLSHandshaking = "browser_http_req_tls_handshaking" + browserHTTPReqSendingName = "browser_http_req_sending" + browserHTTPReqWaitingName = "browser_http_req_waiting" + browserHTTPReqReceivingName = "browser_http_req_receiving" + browserHTTPReqFailedName = "browser_http_req_failed" ) // CustomMetrics are the custom k6 metrics used by xk6-browser. type CustomMetrics struct { WebVitals map[string]*k6metrics.Metric - BrowserDataSent *k6metrics.Metric - BrowserDataReceived *k6metrics.Metric - BrowserHTTPReqDuration *k6metrics.Metric - BrowserHTTPReqBlocked *k6metrics.Metric - BrowserHTTPReqConnecting *k6metrics.Metric + BrowserDataSent *k6metrics.Metric + BrowserDataReceived *k6metrics.Metric + BrowserHTTPReqDuration *k6metrics.Metric + BrowserHTTPReqBlocked *k6metrics.Metric + BrowserHTTPReqConnecting *k6metrics.Metric BrowserHTTPReqTLSHandshaking *k6metrics.Metric - BrowserHTTPReqSending *k6metrics.Metric - BrowserHTTPReqWaiting *k6metrics.Metric - BrowserHTTPReqReceiving *k6metrics.Metric - BrowserHTTPReqFailed *k6metrics.Metric + BrowserHTTPReqSending *k6metrics.Metric + BrowserHTTPReqWaiting *k6metrics.Metric + BrowserHTTPReqReceiving *k6metrics.Metric + BrowserHTTPReqFailed *k6metrics.Metric } // RegisterCustomMetrics creates and registers our custom metrics with the k6 @@ -69,16 +69,16 @@ func RegisterCustomMetrics(registry *k6metrics.Registry) *CustomMetrics { } return &CustomMetrics{ - WebVitals: webVitals, - BrowserDataSent: registry.MustNewMetric(browserDataSentName, k6metrics.Counter, k6metrics.Data), - BrowserDataReceived: registry.MustNewMetric(browserDataReceivedName, k6metrics.Counter, k6metrics.Data), - BrowserHTTPReqDuration: registry.MustNewMetric(browserHTTPReqDurationName, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqBlocked: registry.MustNewMetric(browserHTTPReqBlockedName, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqConnecting: registry.MustNewMetric(browserHTTPReqConnectingName, k6metrics.Trend, k6metrics.Time), + WebVitals: webVitals, + BrowserDataSent: registry.MustNewMetric(browserDataSentName, k6metrics.Counter, k6metrics.Data), + BrowserDataReceived: registry.MustNewMetric(browserDataReceivedName, k6metrics.Counter, k6metrics.Data), + BrowserHTTPReqDuration: registry.MustNewMetric(browserHTTPReqDurationName, k6metrics.Trend, k6metrics.Time), + BrowserHTTPReqBlocked: registry.MustNewMetric(browserHTTPReqBlockedName, k6metrics.Trend, k6metrics.Time), + BrowserHTTPReqConnecting: registry.MustNewMetric(browserHTTPReqConnectingName, k6metrics.Trend, k6metrics.Time), BrowserHTTPReqTLSHandshaking: registry.MustNewMetric(browserHTTPReqTLSHandshaking, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqSending: registry.MustNewMetric(browserHTTPReqSendingName, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqWaiting: registry.MustNewMetric(browserHTTPReqWaitingName, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqReceiving: registry.MustNewMetric(browserHTTPReqReceivingName, k6metrics.Trend, k6metrics.Time), - BrowserHTTPReqFailed: registry.MustNewMetric(browserHTTPReqFailedName, k6metrics.Rate), + BrowserHTTPReqSending: registry.MustNewMetric(browserHTTPReqSendingName, k6metrics.Trend, k6metrics.Time), + BrowserHTTPReqWaiting: registry.MustNewMetric(browserHTTPReqWaitingName, k6metrics.Trend, k6metrics.Time), + BrowserHTTPReqReceiving: registry.MustNewMetric(browserHTTPReqReceivingName, k6metrics.Trend, k6metrics.Time), + BrowserHTTPReqFailed: registry.MustNewMetric(browserHTTPReqFailedName, k6metrics.Rate), } } diff --git a/internal/js/modules/k6/browser/keyboardlayout/us.go b/internal/js/modules/k6/browser/keyboardlayout/us.go index 4df9b0c50..3d23a8079 100644 --- a/internal/js/modules/k6/browser/keyboardlayout/us.go +++ b/internal/js/modules/k6/browser/keyboardlayout/us.go @@ -1,3 +1,4 @@ +//nolint:goconst package keyboardlayout //nolint:funlen diff --git a/internal/js/modules/k6/browser/storage/file_persister.go b/internal/js/modules/k6/browser/storage/file_persister.go index aad36d5da..1fe92eb5e 100644 --- a/internal/js/modules/k6/browser/storage/file_persister.go +++ b/internal/js/modules/k6/browser/storage/file_persister.go @@ -103,7 +103,7 @@ func (r *RemoteFilePersister) Persist(ctx context.Context, path string, data io. return fmt.Errorf("creating upload request: %w", err) } - resp, err := r.httpClient.Do(req) //nolint:gosec + resp, err := r.httpClient.Do(req) if err != nil { return fmt.Errorf("performing upload request: %w", err) } @@ -142,7 +142,7 @@ func (r *RemoteFilePersister) requestPresignedURL(ctx context.Context, path stri req.Header.Add(k, v) } - resp, err := r.httpClient.Do(req) //nolint:gosec + resp, err := r.httpClient.Do(req) if err != nil { return PresignedURLResponse{}, fmt.Errorf("performing request: %w", err) } @@ -169,7 +169,7 @@ func buildPresignedRequestBody(basePath, path string) ([]byte, error) { Name string `json:"name"` }{ { - Name: filepath.Join(basePath, path), + Name: filepath.ToSlash(filepath.Join(basePath, path)), }, }, } diff --git a/internal/js/modules/k6/browser/storage/file_persister_test.go b/internal/js/modules/k6/browser/storage/file_persister_test.go index cfee00518..53ea54d92 100644 --- a/internal/js/modules/k6/browser/storage/file_persister_test.go +++ b/internal/js/modules/k6/browser/storage/file_persister_test.go @@ -7,8 +7,8 @@ import ( "net/http" "net/http/httptest" "os" + "path" "path/filepath" - "runtime" "strings" "testing" @@ -82,11 +82,6 @@ func TestLocalFilePersister(t *testing.T) { func TestRemoteFilePersister(t *testing.T) { t.Parallel() - if runtime.GOOS == "windows" { - t.Skip("not supported on windows for now") - // actual problem is that the paths used are with reverse slash even when they get to be inside JSON - // which leads to them being parsed as escepe codes when they shouldn't - } const ( basePath = "screenshots" @@ -239,7 +234,7 @@ func TestRemoteFilePersister(t *testing.T) { // Does the response match the expected format? wantPresignedURLBody := fmt.Sprintf( tt.wantPresignedURLBody, - filepath.Join(basePath, tt.path), + path.Join(basePath, tt.path), ) assert.JSONEq(t, wantPresignedURLBody, string(bb)) diff --git a/internal/js/modules/k6/browser/tests/asyncgroup_context_test.go b/internal/js/modules/k6/browser/tests/asyncgroup_context_test.go new file mode 100644 index 000000000..5a234298e --- /dev/null +++ b/internal/js/modules/k6/browser/tests/asyncgroup_context_test.go @@ -0,0 +1,94 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" + "go.k6.io/k6/v2/metrics" +) + +func TestAsyncGroupBrowserIterationContext(t *testing.T) { + t.Parallel() + + for _, nested := range []bool{false, true} { + name := "sequential" + if nested { + name = "nested_rejection" + } + t.Run(name, func(t *testing.T) { + t.Parallel() // Each browser VU must restore its own context independently. + tb := newTestBrowser(t) + prepareAsyncGroupBrowser(t, tb) + for iteration := range 3 { + if iteration > 0 { + tb.vu.State().Iteration = int64(iteration) + tb.vu.StartIteration(t, k6test.WithIteration(iteration)) + } + _, err := tb.vu.RunAsync(t, ` + const context = await browser.newContext(); + try { + const page = await context.newPage(); + const runGroups = async () => { + await k6.group('first', async () => { + await page.goto('data:text/html,

First

'); + k6.check(null, {first: true}); + }); + if (%t) { + const reason = {message: 'original rejection'}; + try { + await k6.group('rejected', async () => { + await page.goto('data:text/html,

Rejected

'); + throw reason; + }); + throw new Error('expected rejection'); + } catch (error) { + if (error !== reason) throw error; + } + } + await k6.group('last', async () => { + await page.goto('data:text/html,

Last

'); + k6.check(null, {last: true}); + }); + }; + if (%t) { await k6.group('parent', runGroups); } + else { await runGroups(); } + k6.check(null, {outside: true}); + } finally { + await context.close(); + k6.check(null, {cleanup: true}); + } + `, nested, nested) + require.NoError(t, err) + // Inspect ambient state after the event loop drains, not only JS await continuations. + group, _ := tb.vu.State().Tags.GetCurrentValues().Tags.Get("group") + assert.Empty(t, group, "context leaked after iteration %d", iteration) + tb.vu.EndIteration(t, k6test.WithIteration(iteration)) + } + + prefix := "" + expected := map[string]int{"::first": 3, "::last": 3} + if nested { + prefix = "::parent" + expected = map[string]int{ + "::parent": 3, "::parent::first": 3, "::parent::last": 3, "::parent::rejected": 3, + } + } + samples := collectBrowserSamples(tb) + counts := map[string]int{} + for _, sample := range samples { + if sample.Metric.Name == metrics.GroupDurationName { + group, _ := sample.Tags.Get("group") + counts[group]++ + assert.Positive(t, sample.Value) + } + } + assert.Equal(t, expected, counts) + assertBrowserCheckGroups(t, samples, map[string]string{ + "first": prefix + "::first", "last": prefix + "::last", "outside": "", "cleanup": "", + }) + }) + } +} diff --git a/internal/js/modules/k6/browser/tests/asyncgroup_test.go b/internal/js/modules/k6/browser/tests/asyncgroup_test.go new file mode 100644 index 000000000..def167968 --- /dev/null +++ b/internal/js/modules/k6/browser/tests/asyncgroup_test.go @@ -0,0 +1,615 @@ +package tests + +import ( + "fmt" + "net/http" + "strings" + "sync" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/features" + k6mod "go.k6.io/k6/v2/internal/js/modules/k6" + k6exec "go.k6.io/k6/v2/internal/js/modules/k6/execution" + k6common "go.k6.io/k6/v2/js/common" + "go.k6.io/k6/v2/metrics" +) + +func prepareAsyncGroupBrowser(t *testing.T, tb *testBrowser) { + t.Helper() + tb.vu.StartIteration(t) + tb.vu.State().FeatureFlags = &features.Flags{AsyncMetricContext: true} + tb.vu.Runtime().SetAsyncContextTracker(k6common.NewMetricContextTracker(tb.vu.State)) + + k6Instance := k6mod.New().NewModuleInstance(tb.vu) + tb.vu.SetVar(t, "k6", k6Instance.Exports().Named) + execInstance := k6exec.New().NewModuleInstance(tb.vu) + tb.vu.SetVar(t, "exec", execInstance.Exports().Default) +} + +func collectBrowserSamples(tb *testBrowser) []metrics.Sample { + var samples []metrics.Sample + tb.vu.AssertSamples(func(sample metrics.Sample) { + samples = append(samples, sample) + }) + return samples +} + +func assertBrowserCheckGroups(t *testing.T, samples []metrics.Sample, expected map[string]string) { + t.Helper() + + seen := make(map[string]bool, len(expected)) + for _, sample := range samples { + if sample.Metric.Name != metrics.ChecksName { + continue + } + name, ok := sample.Tags.Get(metrics.TagCheck.String()) + require.True(t, ok) + expectedGroup, ok := expected[name] + require.Truef(t, ok, "unexpected check %q", name) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, expectedGroup, group, "check %q ran under wrong group", name) + seen[name] = true + } + for name := range expected { + assert.Truef(t, seen[name], "missing check %q", name) + } +} + +func TestAsyncGroupBrowserEventListeners(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + + serve := func(page, api, css string) { + tb.withHandler(page, func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprintf(w, ` + + +`, css, api) + require.NoError(t, err) + }) + tb.withHandler(api, func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `{"ok":true}`) + require.NoError(t, err) + }) + tb.withHandler(css, func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/css") + _, err := fmt.Fprint(w, `body{background:#fff}`) + require.NoError(t, err) + }) + } + serve("/home1", "/api1", "/style1.css") + serve("/home2", "/api2", "/style2.css") + + _, err := tb.vu.RunAsync(t, ` + async function loadPageInGroup(groupName, label, url, createFromContext) { + await k6.group(groupName, async () => { + let context; + let page; + if (createFromContext) { + context = await browser.newContext(); + page = await context.newPage(); + } else { + page = await browser.newPage(); + context = page.context(); + } + exec.vu.metrics.tags.owner = label + ':registered'; + page.on('request', (request) => { + k6.check(null, {[label]: true}); + }); + exec.vu.metrics.tags.owner = label + ':active'; + await page.goto(url, {waitUntil: 'networkidle'}); + await page.close(); + await context.close(); + }); + } + + await loadPageInGroup('page1', 'page1', '%s', false); + await loadPageInGroup('page2', 'page2', '%s', true); + `, tb.url("/home1"), tb.url("/home2")) + require.NoError(t, err) + + samples := collectBrowserSamples(tb) + assertBrowserCheckGroups(t, samples, map[string]string{ + "page1": "::page1", + "page2": "::page2", + }) + for _, sample := range samples { + if sample.Metric.Name != metrics.ChecksName { + continue + } + name, _ := sample.Tags.Get(metrics.TagCheck.String()) + owner, _ := sample.Tags.Get("owner") + assert.Equal(t, name+":registered", owner, "check %q used the wrong metric context", name) + } + + netGroups := map[string]int{} + var netSamples int + for _, s := range samples { + switch s.Metric.Name { + case "browser_data_sent", "browser_data_received": + netSamples++ + g, _ := s.Tags.Get("group") + netGroups[g]++ + } + } + require.Positive(t, netSamples, "expected browser network metric samples") + assert.Zero(t, netGroups[""], + "browser network metrics must not be attributed to the root group; got distribution %v", netGroups) + assert.Positive(t, netGroups["::page1"], "expected browser network metrics attributed to ::page1") + assert.Positive(t, netGroups["::page2"], "expected browser network metrics attributed to ::page2") +} + +func TestAsyncGroupBrowserTaskCallbacks(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + tb.withHandler("/callbacks", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `callbacks`) + require.NoError(t, err) + }) + + _, err := tb.vu.RunAsync(t, ` + await k6.group('callbacks', async () => { + const context = await browser.newContext(); + const contextEvent = context.waitForEvent('page', { + timeout: 5000, + predicate: () => { + k6.check(null, {'context.waitForEvent': true}); + return true; + }, + }); + const page = await context.newPage(); + await contextEvent; + + await page.route('%s', route => { + k6.check(null, {'page.route': true}); + route.continue(); + }); + const requestEvent = page.waitForEvent('request', { + timeout: 5000, + predicate: request => { + k6.check(null, {'page.waitForEvent': true}); + return request.url() === '%s'; + }, + }); + await page.goto('%s'); + await requestEvent; + await page.close(); + await context.close(); + }); + k6.check(null, {after: true}); + `, tb.url("/callbacks"), tb.url("/callbacks"), tb.url("/callbacks")) + require.NoError(t, err) + + assertBrowserCheckGroups(t, collectBrowserSamples(tb), map[string]string{ + "context.waitForEvent": "::callbacks", + "page.route": "::callbacks", + "page.waitForEvent": "::callbacks", + "after": "", + }) +} + +func TestAsyncGroupBrowserAsyncCallbacksKeepContextMutations(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + tb.withHandler("/async-callbacks", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `async callbacks`) + require.NoError(t, err) + }) + + _, err := tb.vu.RunAsync(t, ` + exec.vu.metrics.tags.owner = 'root'; + exec.vu.metrics.tags.phase = 'root'; + exec.vu.metrics.metadata.trace = 'root'; + exec.vu.metrics.metadata.step = 'root'; + + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); + const deferred = () => { + let resolve; + let reject; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return {promise, resolve, reject}; + }; + const phases = ['first', 'second', 'third']; + const changeContext = (name, phase) => { + const value = name + ':' + phase; + exec.vu.metrics.tags.phase = value; + exec.vu.metrics.metadata.step = value; + }; + const crossBoundary = phase => phase === 'second' ? delay(0) : Promise.resolve(); + const recordContext = (name, phase) => k6.check(null, {[name + ':' + phase]: true}); + + await k6.group('browser-callbacks', async () => { + exec.vu.metrics.tags.owner = 'browser'; + exec.vu.metrics.metadata.trace = 'browser'; + const page = await browser.newPage(); + const eventFinished = deferred(); + const routeFinished = deferred(); + + page.on('request', async request => { + if (request.url() !== '%s') return; + try { + for (const phase of phases) { + changeContext('event', phase); + await crossBoundary(phase); + recordContext('event', phase); + } + eventFinished.resolve(); + } catch (error) { + eventFinished.reject(error); + } + }); + await page.route('%s', async route => { + try { + for (const phase of phases) { + changeContext('route', phase); + await crossBoundary(phase); + recordContext('route', phase); + } + await route.continue(); + routeFinished.resolve(); + } catch (error) { + routeFinished.reject(error); + } + }); + + await page.goto('%s'); + await Promise.all([eventFinished.promise, routeFinished.promise]); + await page.close(); + }); + k6.check(null, {after: true}); + `, tb.url("/async-callbacks"), tb.url("/async-callbacks"), tb.url("/async-callbacks")) + require.NoError(t, err) + + expected := map[string]string{"after": "root"} + for _, callback := range []string{"event", "route"} { + for _, phase := range []string{"first", "second", "third"} { + value := callback + ":" + phase + expected[value] = value + } + } + for _, sample := range collectBrowserSamples(tb) { + if sample.Metric.Name != metrics.ChecksName { + continue + } + name, _ := sample.Tags.Get(metrics.TagCheck.String()) + phase, ok := expected[name] + require.Truef(t, ok, "unexpected check %q", name) + assert.Equal(t, float64(1), sample.Value, "check %q failed", name) + assert.Equal(t, phase, sample.Metadata["step"], "check %q used the wrong metadata", name) + actualPhase, _ := sample.Tags.Get("phase") + assert.Equal(t, phase, actualPhase, "check %q used the wrong tag", name) + if name == "after" { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Empty(t, group) + owner, _ := sample.Tags.Get("owner") + assert.Equal(t, "root", owner) + assert.Equal(t, "root", sample.Metadata["trace"]) + } else { + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, "::browser-callbacks", group) + owner, _ := sample.Tags.Get("owner") + assert.Equal(t, "browser", owner) + assert.Equal(t, "browser", sample.Metadata["trace"]) + } + delete(expected, name) + } + require.Empty(t, expected) +} + +func TestAsyncGroupBrowserInputActions(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + tb.withHandler("/inputs", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, ` + + + +`) + require.NoError(t, err) + }) + for _, path := range []string{"/keyboard", "/mouse", "/touch"} { + tb.withHandler(path, func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `{"ok":true}`) + require.NoError(t, err) + }) + } + + _, err := tb.vu.RunAsync(t, ` + const context = await browser.newContext({hasTouch: true}); + const page = await context.newPage(); + await page.goto('%s'); + + const waitForRequest = path => page.waitForEvent('request', { + timeout: 5000, + predicate: request => request.url().endsWith(path), + }); + + await page.locator('#keyboard').focus(); + await k6.group('keyboard', async () => { + const request = waitForRequest('/keyboard'); + await page.keyboard.press('Enter'); + await request; + }); + + const mouseBox = await page.locator('#mouse').boundingBox(); + await k6.group('mouse', async () => { + const request = waitForRequest('/mouse'); + await page.mouse.click(mouseBox.x + mouseBox.width / 2, mouseBox.y + mouseBox.height / 2); + await request; + }); + + const touchBox = await page.locator('#touch').boundingBox(); + await k6.group('touch', async () => { + const request = waitForRequest('/touch'); + await page.touchscreen.tap(touchBox.x + touchBox.width / 2, touchBox.y + touchBox.height / 2); + await request; + }); + await page.close(); + await context.close(); + `, tb.url("/inputs")) + require.NoError(t, err) + + expected := map[string]string{ + tb.url("/keyboard"): "::keyboard", + tb.url("/mouse"): "::mouse", + tb.url("/touch"): "::touch", + } + seen := make(map[string]bool, len(expected)) + for _, sample := range collectBrowserSamples(tb) { + if sample.Metric.Name != "browser_data_sent" && sample.Metric.Name != "browser_data_received" { + continue + } + url, _ := sample.Tags.Get(metrics.TagURL.String()) + expectedGroup, ok := expected[url] + if !ok { + continue + } + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, expectedGroup, group, "%s had the wrong group", url) + seen[url] = true + } + for url := range expected { + assert.Truef(t, seen[url], "missing browser network samples for %s", url) + } +} + +func TestAsyncGroupPopupInheritsNetworkGroup(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + tb.withHandler("/opener", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, ``) + require.NoError(t, err) + }) + tb.withHandler("/popup", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, ``) + require.NoError(t, err) + }) + tb.withHandler("/popup-api", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `{"ok":true}`) + require.NoError(t, err) + }) + + _, err := tb.vu.RunAsync(t, ` + await k6.group('popup', async () => { + const context = await browser.newContext(); + const opener = await context.newPage(); + const popupEvent = context.waitForEvent('page'); + await opener.goto('%s'); + await popupEvent; + await new Promise(resolve => setTimeout(resolve, 250)); + await context.close(); + }); + `, tb.url("/opener")) + require.NoError(t, err) + + var popupSamples int + for _, sample := range collectBrowserSamples(tb) { + if sample.Metric.Name != "browser_data_sent" && sample.Metric.Name != "browser_data_received" { + continue + } + url, _ := sample.Tags.Get(metrics.TagURL.String()) + if !strings.Contains(url, "/popup") { + continue + } + popupSamples++ + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, "::popup", group, "popup network sample had the wrong group") + } + require.Positive(t, popupSamples, "expected popup network metric samples") +} + +func TestAsyncGroupBrowserRequestRetainsTagsAndMetadata(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + + releaseSlow := make(chan struct{}) + var releaseOnce sync.Once + t.Cleanup(func() { releaseOnce.Do(func() { close(releaseSlow) }) }) + tb.vu.SetVar(t, "releaseSlow", func() { releaseOnce.Do(func() { close(releaseSlow) }) }) + tb.withHandler("/empty", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `snapshot`) + require.NoError(t, err) + }) + tb.withHandler("/slow", func(w http.ResponseWriter, r *http.Request) { + select { + case <-releaseSlow: + case <-r.Context().Done(): + } + _, err := fmt.Fprint(w, `{"ok":true}`) + require.NoError(t, err) + }) + + _, err := tb.vu.RunAsync(t, ` + const page = await browser.newPage(); + await page.goto('%s'); + const started = page.waitForEvent('request', { + timeout: 5000, + predicate: request => request.url() === '%s', + }); + + exec.vu.metrics.tags.phase = 'request'; + exec.vu.metrics.metadata.trace = 'request'; + const slow = k6.group('request', () => page.evaluate(() => fetch('%s'))); + // Wait until NetworkManager has created the Request and copied the page snapshot. + await started; + + exec.vu.metrics.tags.phase = 'after'; + exec.vu.metrics.metadata.trace = 'after'; + k6.group('after', () => page.url()); + + releaseSlow(); + await slow; + await page.close(); + `, tb.url("/empty"), tb.url("/slow"), tb.url("/slow")) + require.NoError(t, err) + + var samples int + for _, sample := range collectBrowserSamples(tb) { + url, _ := sample.Tags.Get(metrics.TagURL.String()) + if url != tb.url("/slow") { + continue + } + samples++ + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, "::request", group) + phase, _ := sample.Tags.Get("phase") + assert.Equal(t, "request", phase) + assert.Equal(t, "request", sample.Metadata["trace"]) + } + require.Positive(t, samples, "expected browser network metric samples for the delayed request") +} + +func TestAsyncGroupBrowserReadOnlyCallDoesNotRetagActiveOperation(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + prepareAsyncGroupBrowser(t, tb) + tb.withHandler("/empty", func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `operation context + +`) + require.NoError(t, err) + }) + for _, path := range []string{"/operation", "/locator", "/frame", "/element", "/element-handle"} { + tb.withHandler(path, func(w http.ResponseWriter, _ *http.Request) { + _, err := fmt.Fprint(w, `{"ok":true}`) + require.NoError(t, err) + }) + } + + _, err := tb.vu.RunAsync(t, ` + const page = await browser.newPage(); + await page.goto('%s'); + + const operation = k6.group('operation', () => { + exec.vu.metrics.tags.phase = 'operation'; + exec.vu.metrics.metadata.trace = 'operation'; + return page.evaluate(() => new Promise((resolve, reject) => { + setTimeout(() => fetch('%s').then(() => resolve(), reject), 100); + })); + }); + + await new Promise(resolve => setTimeout(resolve, 25)); + k6.group('inspection', () => { + exec.vu.metrics.tags.phase = 'inspection'; + exec.vu.metrics.metadata.trace = 'inspection'; + page.url(); + }); + + await operation; + + const locator = page.locator('#request'); + const locatorResponse = page.waitForResponse('%s'); + await k6.group('locator-operation', () => { + exec.vu.metrics.tags.phase = 'locator'; + exec.vu.metrics.metadata.trace = 'locator'; + return locator.click(); + }); + await locatorResponse; + + const frame = page.mainFrame(); + await k6.group('frame-operation', () => { + exec.vu.metrics.tags.phase = 'frame'; + exec.vu.metrics.metadata.trace = 'frame'; + return frame.evaluate(() => fetch('%s').then(() => null)); + }); + + const element = await page.$('#request'); + await k6.group('element-operation', () => { + exec.vu.metrics.tags.phase = 'element'; + exec.vu.metrics.metadata.trace = 'element'; + return element.evaluate((_, url) => fetch(url).then(() => null), '%s'); + }); + + const elementHandleResponse = page.waitForResponse('%s'); + const evaluatedHandle = await k6.group('element-handle-operation', () => { + exec.vu.metrics.tags.phase = 'element-handle'; + exec.vu.metrics.metadata.trace = 'element-handle'; + return element.evaluateHandle((node, url) => { + fetch(url); + return node; + }, '%s'); + }); + await elementHandleResponse; + await evaluatedHandle.dispose(); + + await page.close(); + `, tb.url("/empty"), tb.url("/operation"), tb.url("/locator"), tb.url("/frame"), + tb.url("/element"), tb.url("/element-handle"), tb.url("/element-handle")) + require.NoError(t, err) + + expected := map[string]struct { + group string + phase string + }{ + tb.url("/operation"): {group: "::operation", phase: "operation"}, + tb.url("/locator"): {group: "::locator-operation", phase: "locator"}, + tb.url("/frame"): {group: "::frame-operation", phase: "frame"}, + tb.url("/element"): {group: "::element-operation", phase: "element"}, + tb.url("/element-handle"): { + group: "::element-handle-operation", phase: "element-handle", + }, + } + seen := make(map[string]bool, len(expected)) + for _, sample := range collectBrowserSamples(tb) { + url, _ := sample.Tags.Get(metrics.TagURL.String()) + want, ok := expected[url] + if !ok { + continue + } + seen[url] = true + group, _ := sample.Tags.Get(metrics.TagGroup.String()) + assert.Equal(t, want.group, group) + phase, _ := sample.Tags.Get("phase") + assert.Equal(t, want.phase, phase) + assert.Equal(t, want.phase, sample.Metadata["trace"]) + } + for url := range expected { + assert.Truef(t, seen[url], "expected browser network metric samples for %s", url) + } +} diff --git a/internal/js/modules/k6/browser/tests/browser_context_test.go b/internal/js/modules/k6/browser/tests/browser_context_test.go index c1afe617f..8f5c56295 100644 --- a/internal/js/modules/k6/browser/tests/browser_context_test.go +++ b/internal/js/modules/k6/browser/tests/browser_context_test.go @@ -11,8 +11,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/browser" "go.k6.io/k6/v2/internal/js/modules/k6/browser/common" "go.k6.io/k6/v2/internal/js/modules/k6/browser/env" + "go.k6.io/k6/v2/internal/js/modules/k6/browser/k6ext/k6test" ) func TestBrowserContextAddCookies(t *testing.T) { @@ -992,3 +994,127 @@ func TestBrowserContextMappingBehavior(t *testing.T) { require.NoError(t, err) require.True(t, value.ToBoolean()) } + +// TestChromiumConnectOverCDPContextBrowserPlain covers a connectOverCDP script +// with no managed browser in the registry: the owning browser must be reachable +// via context().browser(). +func TestChromiumConnectOverCDPContextBrowserPlain(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + + // No StartIteration, so the managed-browser registry stays empty (as in a + // script with no options.browser). + vu := k6test.NewVU(t) + mod := browser.New().NewModuleInstance(vu) + jsMod, ok := mod.Exports().Default.(*browser.JSModule) + require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default) + vu.ActivateVU() + vu.SetVar(t, "chromium", jsMod.Chromium) + + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + p.context().browser().version(); + await b.close(); + `, tb.wsURL) + require.NoError(t, err) +} + +// TestChromiumConnectOverCDPContextBrowserOwner covers the silent wrong-browser +// case: with a managed browser also present, context().browser() must return the +// connectOverCDP browser that owns the context, not the managed one. Closing the +// connectOverCDP browser must therefore disconnect the browser reached via +// context().browser(). +func TestChromiumConnectOverCDPContextBrowserOwner(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) // StartIteration also builds a managed browser + + // context().browser() must be the connectOverCDP browser that owns the + // context, so closing that browser disconnects the one reached via + // context().browser() (isConnected() flips synchronously on close). + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + const owner = p.context().browser(); + await b.close(); + if (owner.isConnected()) { + throw new Error("context().browser() should be the connectOverCDP browser, disconnected after close"); + } + `, tb.wsURL) + require.NoError(t, err) +} + +// TestChromiumConnectOverCDPContextBrowserExposesClose verifies that a +// connectOverCDP browser reached via context().browser() exposes a working +// close(). +func TestChromiumConnectOverCDPContextBrowserExposesClose(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + const owner = p.context().browser(); + if (typeof owner.close !== "function") { + throw new Error("expected close() on connectOverCDP browser via context().browser()"); + } + await owner.close(); // close via context().browser() + if (owner.isConnected()) { + throw new Error("close() via context().browser() should disconnect the browser"); + } + `, tb.wsURL) + require.NoError(t, err) +} + +// TestBrowserContextBrowserNoCloseForManaged verifies that a managed browser +// reached via context().browser() does NOT expose close(): the registry owns its +// lifecycle. Only user-managed (connectOverCDP) browsers get close(). +func TestBrowserContextBrowserNoCloseForManaged(t *testing.T) { + t.Parallel() + + vu := k6test.NewVU(t) + mod := browser.New().NewModuleInstance(vu) + jsMod, ok := mod.Exports().Default.(*browser.JSModule) + require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default) + vu.ActivateVU() + vu.StartIteration(t) // builds the managed browser + vu.SetVar(t, "browser", jsMod.Browser) + + _, err := vu.RunAsync(t, ` + const p = await browser.newPage(); + if (typeof p.context().browser().close !== "undefined") { + throw new Error("managed browser via context().browser() must not expose close()"); + } + await p.close(); + `) + require.NoError(t, err) +} + +// TestChromiumConnectOverCDPUseAfterClose verifies that using a connectOverCDP +// browser after close() gives a clear "browser has been closed" error. +func TestChromiumConnectOverCDPUseAfterClose(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + await b.close(); + let msg = ""; + try { + await b.newPage(); + } catch (e) { + msg = String(e); + } + if (!msg.includes("browser has been closed")) { + throw new Error("expected 'browser has been closed', got: " + msg); + } + `, tb.wsURL) + require.NoError(t, err) +} diff --git a/internal/js/modules/k6/browser/tests/browser_test.go b/internal/js/modules/k6/browser/tests/browser_test.go index aee9edced..e558e155b 100644 --- a/internal/js/modules/k6/browser/tests/browser_test.go +++ b/internal/js/modules/k6/browser/tests/browser_test.go @@ -351,6 +351,35 @@ func TestMultiConnectToSingleBrowser(t *testing.T) { require.NoError(t, err, "failed to close page #2") } +// TestContextlessConnectionDoesNotStallNavigation guards against connections +// freezing each other's pages when they share a browser instance. Every new +// page starts paused until all auto-attached connections resume it. A +// connection that hadn't created its browser context yet used to claim other +// connections' pages without ever releasing them, stalling their navigations +// until it disconnected. The second connection here never creates a context, +// which reproduces that state deterministically. +func TestContextlessConnectionDoesNotStallNavigation(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + + b2, err := tb.browserType.Connect(context.Background(), tb.context(), tb.wsURL) + require.NoError(t, err) + t.Cleanup(b2.Close) + + bctx, err := tb.NewContext(nil) + require.NoError(t, err) + p, err := bctx.NewPage() + require.NoError(t, err) + + err = tb.awaitWithTimeout(10*time.Second, func() error { + opts := &common.FrameGotoOptions{Timeout: 10 * time.Second} + _, err := p.Goto("data:text/html,hello", opts) + return err + }) + require.NoError(t, err) +} + func TestCloseContext(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/tests/browser_type_test.go b/internal/js/modules/k6/browser/tests/browser_type_test.go index 5b72dbc16..f127c80bc 100644 --- a/internal/js/modules/k6/browser/tests/browser_type_test.go +++ b/internal/js/modules/k6/browser/tests/browser_type_test.go @@ -5,7 +5,9 @@ import ( "testing" "github.com/stretchr/testify/require" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + k6event "go.k6.io/k6/v2/internal/event" "go.k6.io/k6/v2/internal/js/modules/k6/browser/browser" "go.k6.io/k6/v2/internal/js/modules/k6/browser/chromium" "go.k6.io/k6/v2/internal/js/modules/k6/browser/env" @@ -29,6 +31,225 @@ func TestBrowserTypeConnect(t *testing.T) { require.NoError(t, err) } +func TestBrowserTypeConnectOverCDP(t *testing.T) { + t.Parallel() + + // Start a test browser so we can get its WS URL + // and connect through BrowserType.ConnectOverCDP. + tb := newTestBrowser(t) + vu := k6test.NewVU(t) + bt := chromium.NewBrowserType(vu) + vu.ActivateVU() + + b, err := bt.ConnectOverCDP(context.Background(), tb.wsURL) + require.NoError(t, err) + t.Cleanup(b.Close) + + _, err = b.NewPage(nil) + require.NoError(t, err) +} + +func TestBrowserTypeConnectOverCDPValidation(t *testing.T) { + t.Parallel() + + // Invalid endpoints must fail immediately, before any connection attempt, + // so no test browser is needed. + vu := k6test.NewVU(t) + bt := chromium.NewBrowserType(vu) + vu.ActivateVU() + + for _, wsEndpoint := range []string{ + "", // empty (e.g. an undefined value or failed lookup) + " ", // blank + "http://localhost:9222", // wrong scheme + "localhost:9222", // missing ws/wss scheme + } { + _, err := bt.ConnectOverCDP(context.Background(), wsEndpoint) + require.Errorf(t, err, "endpoint %q should be rejected", wsEndpoint) + } +} + +// TestChromiumConnectOverCDPAppliesBrowserTimeout verifies that ConnectOverCDP +// parses browser options from the environment. E.g., with K6_BROWSER_TIMEOUT=1ms +// a browser operation (e.g., NewPage) must time out. +func TestChromiumConnectOverCDPAppliesBrowserTimeout(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := k6test.NewVU(t, env.ConstLookup(env.BrowserGlobalTimeout, "1ms")) + bt := chromium.NewBrowserType(vu) + vu.ActivateVU() + + b, err := bt.ConnectOverCDP(context.Background(), tb.wsURL) + require.NoError(t, err) + t.Cleanup(b.Close) + + _, err = b.NewPage(nil) + require.Error(t, err, "NewPage should time out with K6_BROWSER_TIMEOUT=1ms") +} + +// setupChromiumVU builds a VU with the browser module's chromium object bound +// to the "chromium" global and an iteration started, ready to run +// connectOverCDP scripts via RunAsync. +func setupChromiumVU(t *testing.T, opts ...any) *k6test.VU { + t.Helper() + + vu := k6test.NewVU(t, opts...) + mod := browser.New().NewModuleInstance(vu) + jsMod, ok := mod.Exports().Default.(*browser.JSModule) + require.Truef(t, ok, "unexpected default mod export type %T", mod.Exports().Default) + + vu.ActivateVU() + vu.StartIteration(t) + vu.SetVar(t, "chromium", jsMod.Chromium) + + return vu +} + +// TestChromiumConnectOverCDPConcurrent is a regression test for a data race +// where two concurrent chromium.connectOverCDP calls in the same iteration +// used to race. +func TestChromiumConnectOverCDPConcurrent(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + _, err := vu.RunAsync(t, ` + await Promise.all([ + chromium.connectOverCDP("%s"), + chromium.connectOverCDP("%s"), + ].map(async (p) => { + const b = await p; + const pg = await b.newPage(); + await pg.close(); + await b.close(); + })); + `, tb.wsURL, tb.wsURL) + require.NoError(t, err) +} + +func TestChromiumConnectOverCDPAutoClose(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + await p.close(); + if (!b.isConnected()) { + throw new Error("expected browser to be connected before iteration end"); + } + // Stash the browser so we can check its state after the iteration + // ends, once the registry sweep has had a chance to run. + globalThis.__cdpBrowser = b; + // Intentionally NOT calling b.close(). + `, tb.wsURL) + require.NoError(t, err) + + // End the iteration; the registry sweep must close the connected browser. + vu.EndIteration(t) + + // Verify the sweep actually closed the browser, instead of just trusting + // that EndIteration didn't error out. + got := vu.RunPromise(t, "return globalThis.__cdpBrowser.isConnected();") + require.False(t, got.Result().ToBoolean(), "expected browser to be disconnected after iteration end") +} + +func TestChromiumConnectOverCDPExplicitClose(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + await p.close(); + await b.close(); + `, tb.wsURL) + require.NoError(t, err) + + // Ending the iteration after an explicit close must not panic or + // double-close (untrack removed it from the sweep set). + vu.EndIteration(t) +} + +// TestChromiumConnectOverCDPExitSweep verifies that a connected browser left +// open when the test exits (e.g., SIGTERM mid-iteration) is closed by the +// registry's Exit sweep (clear), not only by the per-iteration IterEnd sweep. +func TestChromiumConnectOverCDPExitSweep(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + vu := setupChromiumVU(t) + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + await p.close(); + // Stash the browser so we can check its state after the Exit sweep, + // and intentionally do NOT close it or end the iteration. + globalThis.__cdpBrowser = b; + `, tb.wsURL) + require.NoError(t, err) + + // Fire the global Exit event, as k6 does on process exit / SIGTERM. The + // registry's clear() must close any still-connected browsers. + events, ok := vu.EventsField.Global.(*k6event.System) + require.True(t, ok, "want *k6event.System; got %T", events) + waitDone := events.Emit(&k6event.Event{Type: k6event.Exit}) + require.NoError(t, waitDone(context.Background()), "error waiting on Exit done") + + // Verify the Exit sweep actually closed the connected browser. + got := vu.RunPromise(t, "return globalThis.__cdpBrowser.isConnected();") + require.False(t, got.Result().ToBoolean(), "expected browser to be disconnected after Exit sweep") +} + +// TestChromiumConnectOverCDPTraceLinkage verifies that a call made over a +// connectOverCDP browser produces a span parented under an iteration root +// span keyed by the real iteration number, and that root span is +// ended at IterEnd. +// It uses a recording tracer provider to inspect the span tree. +func TestChromiumConnectOverCDPTraceLinkage(t *testing.T) { + t.Parallel() + + rec := k6test.NewSpanRecorder() + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(rec)) + + tb := newTestBrowser(t) + vu := setupChromiumVU(t, k6test.WithTracerProvider(tp)) + wantIter := vu.State().Iteration + + _, err := vu.RunAsync(t, ` + const b = await chromium.connectOverCDP("%s"); + const p = await b.newPage(); + await p.close(); + await b.close(); + `, tb.wsURL) + require.NoError(t, err) + + // End the iteration so the iteration root span is closed. + vu.EndIteration(t) + + iterSpan, ok := rec.Find("iteration") + require.True(t, ok, "expected an 'iteration' root span") + newPageSpan, ok := rec.Find("browser.newPage") + require.True(t, ok, "expected a 'browser.newPage' span") + + // The browser API span must be parented under the iteration trace that + // connectOverCDP established. If startConnectTrace did not wire the tracer + // into the browser's context, browser.newPage would be a noop/orphaned span + // and these assertions would fail. + require.Equal(t, iterSpan.TraceID, newPageSpan.TraceID, + "browser.newPage span should be in the same trace as the iteration span") + require.Equal(t, iterSpan.SpanID, newPageSpan.ParentID, + "browser.newPage span should be parented under the iteration span") + + // The iteration span must be keyed by the real iteration number and ended + // when the iteration ends. + require.Equal(t, wantIter, iterSpan.AttrInt64(t, "test.iteration.number")) + require.True(t, rec.IsEnded(iterSpan.SpanID), "iteration span should be ended at IterEnd") +} + func TestBrowserTypeLaunchToConnect(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/tests/fork_async_errors_test.go b/internal/js/modules/k6/browser/tests/fork_async_errors_test.go new file mode 100644 index 000000000..65e9788ef --- /dev/null +++ b/internal/js/modules/k6/browser/tests/fork_async_errors_test.go @@ -0,0 +1,52 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// The fork's error samples must retain the initiating operation's context, +// even after an async group has restored the root context. +func TestAsyncGroupBrowserErrorsKeepContext(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + prepareAsyncGroupBrowser(t, tb) + _, err := tb.vu.RunAsync(t, ` + const page = await browser.newPage(); + try { + for (const name of ['checkout', 'payment']) { + await k6.group(name, async () => { + exec.vu.metrics.tags.owner = name; + exec.vu.metrics.metadata.trace = name; + try { + await page.goto('http://127.0.0.1:1'); + throw new Error('expected an unsafe-port navigation failure'); + } catch (error) { + if (!String(error).includes('ERR_UNSAFE_PORT')) throw error; + } + }); + } + } finally { + await page.close(); + } + `) + require.NoError(t, err) + + seen := map[string]int{} + for _, sample := range collectBrowserSamples(tb) { + if sample.Metric.Name != "browser_errors" { + continue + } + owner, _ := sample.Tags.Get("owner") + group, _ := sample.Tags.Get("group") + assert.Contains(t, []string{"checkout", "payment"}, owner) + assert.Equal(t, "::"+owner, group) + assert.Equal(t, owner, sample.Metadata["trace"]) + assert.Equal(t, float64(1), sample.Value) + seen[owner]++ + } + assert.Equal(t, map[string]int{"checkout": 1, "payment": 1}, seen) +} diff --git a/internal/js/modules/k6/browser/tests/fork_group_outcome_test.go b/internal/js/modules/k6/browser/tests/fork_group_outcome_test.go new file mode 100644 index 000000000..3ff61daec --- /dev/null +++ b/internal/js/modules/k6/browser/tests/fork_group_outcome_test.go @@ -0,0 +1,57 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/metrics" +) + +func TestBrowserGroupTransactionOutcomes(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t) + prepareAsyncGroupBrowser(t, tb) + _, err := tb.vu.RunAsync(t, ` + const page = await browser.newPage(); + try { + await k6.group('success', async () => { + await page.setContent('

Checkout

'); + await page.locator('h1').waitFor({state: 'visible'}); + }); + try { + await k6.group('failure', async () => { + await page.goto('http://127.0.0.1:1'); + }); + throw new Error('expected an unsafe-port navigation failure'); + } catch (error) { + if (!String(error).includes('ERR_UNSAFE_PORT')) throw error; + } + await k6.group('caught', async () => { + try { await page.goto('http://127.0.0.1:1'); } catch (_) {} + }); + } finally { + await page.close(); + } + `) + require.NoError(t, err) + + outcomes := map[string][]bool{} + errors := map[string]int{} + for _, sample := range collectBrowserSamples(tb) { + group, _ := sample.Tags.Get("group") + switch sample.Metric.Name { + case metrics.GroupDurationName: + outcomes[group] = append(outcomes[group], sample.GroupFailed) + assert.Greater(t, sample.Value, float64(0)) + case "browser_errors": + errors[group]++ + } + } + assert.Equal(t, map[string][]bool{ + "::success": {false}, "::failure": {true}, "::caught": {false}, + }, outcomes) + assert.Equal(t, map[string]int{"::failure": 1, "::caught": 1}, errors) +} diff --git a/internal/js/modules/k6/browser/tests/get_by_test.go b/internal/js/modules/k6/browser/tests/get_by_test.go index 19f159ac9..14e61910c 100644 --- a/internal/js/modules/k6/browser/tests/get_by_test.go +++ b/internal/js/modules/k6/browser/tests/get_by_test.go @@ -39,31 +39,35 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "link", role: "link", - opts: &common.GetByRoleOptions{Name: toPtr(`'Link text'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Link text'`)}, expected: 1, expectedText: "Link text", }, { - name: "area", - role: "link", - opts: &common.GetByRoleOptions{Name: toPtr(`'Map area'`)}, + // Chromium styles as display:none, including valid image-map hotspots. + name: "area", + role: "link", + opts: &common.GetByRoleOptions{ + Name: new(`'Map area'`), + IncludeHidden: new(true), + }, expected: 1, expectedText: "", }, { name: "button", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Click'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Click'`)}, expected: 1, expectedText: "Click", }, { name: "submit_type", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Submit'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Submit'`)}, expected: 1, expectedText: "", }, { name: "image_type", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Image Button'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Image Button'`)}, expected: 1, expectedText: "", }, { @@ -79,13 +83,13 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "text_type", role: "textbox", - opts: &common.GetByRoleOptions{Name: toPtr(`'Text type'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Text type'`)}, expected: 1, expectedText: "", }, { name: "textarea", role: "textbox", - opts: &common.GetByRoleOptions{Name: toPtr(`'Text area'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Text area'`)}, expected: 1, expectedText: "Textarea", }, { @@ -116,7 +120,7 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "details_summary", role: "group", - opts: &common.GetByRoleOptions{Name: toPtr(`'details'`)}, + opts: &common.GetByRoleOptions{Name: new(`'details'`)}, expected: 1, expectedText: "SummaryDetails", }, { @@ -127,37 +131,37 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "h1", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(1))}, + opts: &common.GetByRoleOptions{Level: new(int64(1))}, expected: 1, expectedText: "Heading1", }, { name: "h2", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(2))}, + opts: &common.GetByRoleOptions{Level: new(int64(2))}, expected: 1, expectedText: "Heading2", }, { name: "h3", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(3))}, + opts: &common.GetByRoleOptions{Level: new(int64(3))}, expected: 1, expectedText: "Heading3", }, { name: "h4", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(4))}, + opts: &common.GetByRoleOptions{Level: new(int64(4))}, expected: 1, expectedText: "Heading4", }, { name: "h5", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(5))}, + opts: &common.GetByRoleOptions{Level: new(int64(5))}, expected: 1, expectedText: "Heading5", }, { name: "h6", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(6))}, + opts: &common.GetByRoleOptions{Level: new(int64(6))}, expected: 1, expectedText: "Heading6", }, { @@ -168,7 +172,7 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "img", role: "img", - opts: &common.GetByRoleOptions{Name: toPtr(`'Img'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Img'`)}, expected: 1, expectedText: "", }, { @@ -179,25 +183,25 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "ul_list", role: "list", - opts: &common.GetByRoleOptions{Name: toPtr(`'ul'`)}, + opts: &common.GetByRoleOptions{Name: new(`'ul'`)}, expected: 1, expectedText: "", }, { name: "ol_list", role: "list", - opts: &common.GetByRoleOptions{Name: toPtr(`'ol'`)}, + opts: &common.GetByRoleOptions{Name: new(`'ol'`)}, expected: 1, expectedText: "", }, { name: "ul_li_listitem", role: "listitem", - opts: &common.GetByRoleOptions{Name: toPtr(`'ul-li'`)}, + opts: &common.GetByRoleOptions{Name: new(`'ul-li'`)}, expected: 1, expectedText: "Item1", }, { name: "ol_li_listitem", role: "listitem", - opts: &common.GetByRoleOptions{Name: toPtr(`'ol-li'`)}, + opts: &common.GetByRoleOptions{Name: new(`'ol-li'`)}, expected: 1, expectedText: "Item2", }, { @@ -213,19 +217,19 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "fieldset_legend", role: "group", - opts: &common.GetByRoleOptions{Name: toPtr(`'Legend'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Legend'`)}, expected: 1, expectedText: "Legend", }, { name: "figure_figcaption", role: "figure", - opts: &common.GetByRoleOptions{Name: toPtr(`'Caption'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Caption'`)}, expected: 1, expectedText: "Caption", }, { name: "table", role: "table", - opts: &common.GetByRoleOptions{Name: toPtr(`'table1'`)}, + opts: &common.GetByRoleOptions{Name: new(`'table1'`)}, expected: 1, expectedText: "", }, { @@ -241,43 +245,43 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "table_head_cell", role: "cell", - opts: &common.GetByRoleOptions{Name: toPtr(`'th'`)}, + opts: &common.GetByRoleOptions{Name: new(`'th'`)}, expected: 1, expectedText: "Head Cell", }, { name: "table_head_gridcell", role: "gridcell", - opts: &common.GetByRoleOptions{Name: toPtr(`'th gridcell'`)}, + opts: &common.GetByRoleOptions{Name: new(`'th gridcell'`)}, expected: 1, expectedText: "Head Gridcell", }, { name: "table_body", role: "rowgroup", - opts: &common.GetByRoleOptions{Name: toPtr(`'tbody'`)}, + opts: &common.GetByRoleOptions{Name: new(`'tbody'`)}, expected: 1, expectedText: "Cell", }, { name: "table_foot", role: "rowgroup", - opts: &common.GetByRoleOptions{Name: toPtr(`'tfoot'`)}, + opts: &common.GetByRoleOptions{Name: new(`'tfoot'`)}, expected: 1, expectedText: "Foot", }, { name: "table_tr", role: "row", - opts: &common.GetByRoleOptions{Name: toPtr(`'tr'`)}, + opts: &common.GetByRoleOptions{Name: new(`'tr'`)}, expected: 1, expectedText: "Row", }, { name: "table_td_cell", role: "cell", - opts: &common.GetByRoleOptions{Name: toPtr(`'td'`)}, + opts: &common.GetByRoleOptions{Name: new(`'td'`)}, expected: 1, expectedText: "Column Cell", }, { name: "table_td_gridcell", role: "gridcell", - opts: &common.GetByRoleOptions{Name: toPtr(`'td gridcell'`)}, + opts: &common.GetByRoleOptions{Name: new(`'td gridcell'`)}, expected: 1, expectedText: "Column Gridcell", }, { @@ -316,14 +320,14 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "form", role: "form", - opts: &common.GetByRoleOptions{Name: toPtr(`'form'`)}, + opts: &common.GetByRoleOptions{Name: new(`'form'`)}, expected: 1, expectedText: "", }, // Only works with aria labels. { name: "section", role: "region", - opts: &common.GetByRoleOptions{Name: toPtr(`'Region Section'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Region Section'`)}, expected: 1, expectedText: "Region content", }, { @@ -369,7 +373,7 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "menu", role: "list", - opts: &common.GetByRoleOptions{Name: toPtr(`'menu'`)}, + opts: &common.GetByRoleOptions{Name: new(`'menu'`)}, expected: 1, expectedText: "Menu item", }, { @@ -400,7 +404,7 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "svg", role: "img", - opts: &common.GetByRoleOptions{Name: toPtr(`'svg'`)}, + opts: &common.GetByRoleOptions{Name: new(`'svg'`)}, expected: 1, expectedText: "", }, { @@ -411,13 +415,13 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "select", role: "combobox", - opts: &common.GetByRoleOptions{Name: toPtr(`'select'`)}, + opts: &common.GetByRoleOptions{Name: new(`'select'`)}, expected: 1, expectedText: "", }, { name: "select_multiple", role: "listbox", - opts: &common.GetByRoleOptions{Name: toPtr(`'select multiple'`)}, + opts: &common.GetByRoleOptions{Name: new(`'select multiple'`)}, expected: 1, expectedText: "", }, { @@ -655,115 +659,115 @@ func TestGetByRoleSuccess(t *testing.T) { { name: "text_content_as_name", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Submit Form'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Submit Form'`)}, expected: 1, expectedText: "", }, { name: "not_exact", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'submit form'`), Exact: toPtr(false)}, + opts: &common.GetByRoleOptions{Name: new(`'submit form'`), Exact: new(false)}, expected: 1, expectedText: "", }, { name: "exact_no_match", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'submit form'`), Exact: toPtr(true)}, + opts: &common.GetByRoleOptions{Name: new(`'submit form'`), Exact: new(true)}, expected: 0, expectedText: "", }, { name: "exact_match", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Submit Form'`), Exact: toPtr(true)}, + opts: &common.GetByRoleOptions{Name: new(`'Submit Form'`), Exact: new(true)}, expected: 1, expectedText: "", }, { name: "aria_label_as_name", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Save Draft'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Save Draft'`)}, expected: 1, expectedText: "", }, { name: "aria_labelledby_as_name", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Upload'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Upload'`)}, expected: 1, expectedText: "labelledby-upload-button", }, { name: "hidden_text_nodes_should_be_ignored", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'FooBar'`)}, + opts: &common.GetByRoleOptions{Name: new(`'FooBar'`)}, expected: 0, expectedText: "", }, { name: "only_visible_node", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Bar'`)}, + opts: &common.GetByRoleOptions{Name: new(`'Bar'`)}, expected: 1, expectedText: "Bar", }, { name: "regex_matching", role: "heading", - opts: &common.GetByRoleOptions{Name: toPtr(`/^[a-z0-9]+$/`)}, + opts: &common.GetByRoleOptions{Name: new(`/^[a-z0-9]+$/`)}, expected: 1, expectedText: "abc123", }, { name: "selected_option", role: "option", - opts: &common.GetByRoleOptions{Selected: toPtr(true)}, + opts: &common.GetByRoleOptions{Selected: new(true)}, expected: 1, expectedText: "One", }, { name: "pressed_option", role: "button", - opts: &common.GetByRoleOptions{Pressed: toPtr(true)}, + opts: &common.GetByRoleOptions{Pressed: new(true)}, expected: 1, expectedText: "Toggle", }, { name: "expanded_option", role: "button", - opts: &common.GetByRoleOptions{Expanded: toPtr(true)}, + opts: &common.GetByRoleOptions{Expanded: new(true)}, expected: 1, expectedText: "Expanded", }, { name: "level_option", role: "heading", - opts: &common.GetByRoleOptions{Level: toPtr(int64(6))}, + opts: &common.GetByRoleOptions{Level: new(int64(6))}, expected: 1, expectedText: "Section", }, { name: "checked_option", role: "checkbox", - opts: &common.GetByRoleOptions{Checked: toPtr(true)}, + opts: &common.GetByRoleOptions{Checked: new(true)}, expected: 1, expectedText: "", }, { name: "radio_checked_option", role: "radio", - opts: &common.GetByRoleOptions{Checked: toPtr(true)}, + opts: &common.GetByRoleOptions{Checked: new(true)}, expected: 1, expectedText: "", }, { name: "disabled_option", role: "button", - opts: &common.GetByRoleOptions{Disabled: toPtr(true)}, + opts: &common.GetByRoleOptions{Disabled: new(true)}, expected: 1, expectedText: "Go", }, { name: "include_css_hidden", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Hidden X Button'`), IncludeHidden: toPtr(true)}, + opts: &common.GetByRoleOptions{Name: new(`'Hidden X Button'`), IncludeHidden: new(true)}, expected: 1, expectedText: "X", }, { name: "include_aria_hidden", role: "button", - opts: &common.GetByRoleOptions{Name: toPtr(`'Hidden Hi Button'`), IncludeHidden: toPtr(true)}, + opts: &common.GetByRoleOptions{Name: new(`'Hidden Hi Button'`), IncludeHidden: new(true)}, expected: 1, expectedText: "Hi", }, { name: "combo_options", role: "button", - opts: &common.GetByRoleOptions{Pressed: toPtr(false), Name: toPtr(`'Archive'`), IncludeHidden: toPtr(true)}, + opts: &common.GetByRoleOptions{Pressed: new(false), Name: new(`'Archive'`), IncludeHidden: new(true)}, expected: 1, expectedText: "Combo Options Button", }, } @@ -817,7 +821,7 @@ func TestGetByRoleFailure(t *testing.T) { { "missing_quotes_on_string", "button", - &common.GetByRoleOptions{Name: toPtr(`Submit Form`)}, + &common.GetByRoleOptions{Name: new(`Submit Form`)}, "Error while parsing selector `button[name=Submit Form]` - unexpected symbol", }, { @@ -888,13 +892,13 @@ func TestGetByAltTextSuccess(t *testing.T) { { "exact_match", "'World Map'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 1, }, { "no_exact_match", "'world map'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 0, }, { @@ -967,13 +971,13 @@ func TestGetByLabelSuccess(t *testing.T) { { "exact_match", `"Password"`, - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 1, }, { "no_exact_match", `"password"`, - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 0, }, { @@ -1051,13 +1055,13 @@ func TestGetByPlaceholderSuccess(t *testing.T) { { "exact_match", "'Email address'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 1, }, { "no_exact_match", "'email address'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 0, }, { @@ -1147,13 +1151,13 @@ func TestGetByTitleSuccess(t *testing.T) { { "exact_match", "'Link to somewhere'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 1, }, { "no_exact_match", "'link to somewhere'", - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 0, }, { @@ -1341,14 +1345,14 @@ func TestGetByTextSuccess(t *testing.T) { { "exact_match", `"Learn more"`, - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 1, "Learn more", }, { "no_exact_match", `"learn more"`, - &common.GetByBaseOptions{Exact: toPtr(true)}, + &common.GetByBaseOptions{Exact: new(true)}, 0, "", }, diff --git a/internal/js/modules/k6/browser/tests/page_test.go b/internal/js/modules/k6/browser/tests/page_test.go index c9c2eee0c..3c3b10f5b 100644 --- a/internal/js/modules/k6/browser/tests/page_test.go +++ b/internal/js/modules/k6/browser/tests/page_test.go @@ -17,6 +17,7 @@ import ( "slices" "strconv" "strings" + "sync" "sync/atomic" "testing" "time" @@ -31,11 +32,6 @@ import ( k6metrics "go.k6.io/k6/v2/metrics" ) -type jsFrameBaseOpts struct { - Timeout string - Strict bool -} - const sampleHTML = `
Test
  1. One
` func TestNestedFrames(t *testing.T) { @@ -440,7 +436,7 @@ func TestPageInnerHTML(t *testing.T) { err := p.SetContent(sampleHTML, nil) require.NoError(t, err) popts := common.NewFrameInnerHTMLOptions(p.MainFrame().Timeout()) - require.NoError(t, popts.Parse(tb.vu.Context(), tb.toSobekValue(jsFrameBaseOpts{Timeout: "100"}))) + popts.Timeout = 100 * time.Millisecond _, err = p.InnerHTML("p", popts) require.Error(t, err) }) @@ -478,7 +474,7 @@ func TestPageInnerText(t *testing.T) { require.NoError(t, err) popts := common.NewFrameInnerTextOptions(p.MainFrame().Timeout()) - require.NoError(t, popts.Parse(tb.vu.Context(), tb.toSobekValue(jsFrameBaseOpts{Timeout: "100"}))) + popts.Timeout = 100 * time.Millisecond _, err = p.InnerText("p", popts) require.Error(t, err) }) @@ -678,6 +674,16 @@ func TestPageSetChecked(t *testing.T) { assert.False(t, checked) } +type timeoutScreenshotPersister struct { + called bool +} + +func (p *timeoutScreenshotPersister) Persist(ctx context.Context, _ string, _ io.Reader) error { + p.called = true + <-ctx.Done() + return ctx.Err() +} + func TestPageScreenshotFullpage(t *testing.T) { t.Parallel() @@ -697,7 +703,7 @@ func TestPageScreenshotFullpage(t *testing.T) { const div = document.createElement('div'); div.style.width = '1280px'; - div.style.height = '800px'; + div.style.height = '1600px'; div.style.background = 'linear-gradient(to bottom, red, blue)'; document.body.appendChild(div); @@ -714,21 +720,37 @@ func TestPageScreenshotFullpage(t *testing.T) { opts.FullPage = true buf, err := p.Screenshot(opts, &mockPersister{}) require.NoError(t, err) + assert.Equal(t, map[string]float64{"width": 1280, "height": 800}, p.ViewportSize()) + viewportRestored, err := p.Evaluate(`() => window.innerWidth === 1280 && window.innerHeight === 800`) + require.NoError(t, err) + assert.Equal(t, true, viewportRestored) reader := bytes.NewReader(buf) img, err := png.Decode(reader) assert.Nil(t, err) assert.Equal(t, 1280, img.Bounds().Max.X, "want: screenshot width is 1280px, got: %dpx", img.Bounds().Max.X) - assert.Equal(t, 800, img.Bounds().Max.Y, "want: screenshot height is 800px, got: %dpx", img.Bounds().Max.Y) + assert.Equal(t, 1600, img.Bounds().Max.Y, "want: screenshot height is 1600px, got: %dpx", img.Bounds().Max.Y) // Allow tolerance to account for differences in rendering between // different platforms and browsers. The goal is to ensure that the // screenshot is mostly red at the top and mostly blue at the bottom. r, _, b, _ := img.At(0, 0).RGBA() assert.Truef(t, r > b*2, "want: the top pixel to be dominantly red, got R: %d, B: %d", r, b) - r, _, b, _ = img.At(0, 799).RGBA() + r, _, b, _ = img.At(0, 1599).RGBA() assert.Truef(t, b > r*2, "want: the bottom pixel to be dominantly blue, got R: %d, B: %d", r, b) + + // A deadline after capture must also restore the enlarged viewport. + p.SetDefaultTimeout(2000) + opts.Path = "unused.png" + persister := &timeoutScreenshotPersister{} + _, err = p.Screenshot(opts, persister) + require.ErrorIs(t, err, context.DeadlineExceeded) + require.True(t, persister.called) + assert.Equal(t, map[string]float64{"width": 1280, "height": 800}, p.ViewportSize()) + viewportRestored, err = p.Evaluate(`() => window.innerWidth === 1280 && window.innerHeight === 800`) + require.NoError(t, err) + assert.Equal(t, true, viewportRestored) } func TestPageTitle(t *testing.T) { @@ -1477,33 +1499,45 @@ func TestPageWaitForSelector(t *testing.T) { } testCases := []struct { - name string - url string - opts map[string]any - customTimeout time.Duration - selector string - errAssert func(*testing.T, error) + name string + selector string + state common.DOMElementState + timeout time.Duration + wantNil bool + wantErr string }{ { name: "should wait for selector", - url: "wait_for.html", selector: "#my-div", - errAssert: func(t *testing.T, e error) { - t.Helper() - assert.Nil(t, e) - }, + state: common.DOMElementStateVisible, + timeout: common.DefaultTimeout, }, { - name: "should TO waiting for selector", - url: "wait_for.html", - // set a timeout smaller than the time - // it takes the element to show up - customTimeout: time.Nanosecond, - selector: "#my-div", - errAssert: func(t *testing.T, e error) { - t.Helper() - assert.ErrorContains(t, e, "timed out after") - }, + name: "should TO waiting for selector", + selector: "#my-div", + state: common.DOMElementStateVisible, + timeout: time.Nanosecond, + wantErr: "timed out after", + }, + { + name: "absent hidden element", + selector: "#absent", + state: common.DOMElementStateHidden, + timeout: time.Second, + wantNil: true, + }, + { + name: "absent detached element", + selector: "#absent", + state: common.DOMElementStateDetached, + timeout: time.Second, + wantNil: true, + }, + { + name: "existing hidden element", + selector: "head", + state: common.DOMElementStateHidden, + timeout: time.Second, }, } @@ -1512,24 +1546,25 @@ func TestPageWaitForSelector(t *testing.T) { t.Parallel() tb := newTestBrowser(t, withFileServer()) - page := tb.NewPage(nil) - opts := &common.FrameGotoOptions{ + _, err := page.Goto(tb.staticURL("wait_for.html"), &common.FrameGotoOptions{ Timeout: common.DefaultTimeout, - } - _, err := page.Goto( - tb.staticURL(tc.url), - opts, - ) + }) require.NoError(t, err) - timeout := page.MainFrame().Timeout() - if tc.customTimeout != 0 { - timeout = tc.customTimeout + opts := common.NewFrameWaitForSelectorOptions(tc.timeout) + opts.State = tc.state + element, err := page.WaitForSelector(tc.selector, opts) + if tc.wantErr != "" { + require.ErrorContains(t, err, tc.wantErr) + return + } + require.NoError(t, err) + if tc.wantNil { + assert.Nil(t, element) + } else { + assert.NotNil(t, element) } - - _, err = page.WaitForSelector(tc.selector, common.NewFrameWaitForSelectorOptions(timeout)) - tc.errAssert(t, err) }) } } @@ -2343,7 +2378,8 @@ func TestPageOnRequest(t *testing.T) { page.on('request', async (request) => { returnValue.push({ allHeaders: await request.allHeaders(), - frameUrl: request.frame().url(), + // Ignoring frameUrl as it is racey due to the async nature of how it is set. + // frameUrl: request.frame().url(), acceptLanguageHeader: await request.headerValue('Accept-Language'), headers: request.headers(), headersArray: await request.headersArray(), @@ -2383,7 +2419,6 @@ func TestPageOnRequest(t *testing.T) { "upgrade-insecure-requests": "1", "user-agent": "some-user-agent", }, - FrameURL: "about:blank", AcceptLanguageHeader: "en-US", Headers: map[string]string{ "Accept-Language": "en-US", @@ -2412,7 +2447,6 @@ func TestPageOnRequest(t *testing.T) { "referer": tb.url("/home"), "user-agent": "some-user-agent", }, - FrameURL: tb.url("/home"), AcceptLanguageHeader: "en-US", Headers: map[string]string{ "Accept-Language": "en-US", @@ -2442,7 +2476,6 @@ func TestPageOnRequest(t *testing.T) { "referer": tb.url("/home"), "user-agent": "some-user-agent", }, - FrameURL: tb.url("/home"), AcceptLanguageHeader: "en-US", Headers: map[string]string{ "Accept-Language": "en-US", @@ -2473,7 +2506,6 @@ func TestPageOnRequest(t *testing.T) { "referer": tb.url("/home"), "user-agent": "some-user-agent", }, - FrameURL: tb.url("/home"), AcceptLanguageHeader: "en-US", Headers: map[string]string{ "Accept-Language": "en-US", @@ -2499,22 +2531,43 @@ func TestPageOnRequest(t *testing.T) { } // Compare each request one by one for better test failure visibility + // We only want to compare against the expected values. The actual values + // may contain extra headers that are not present in the expected values. + // Extra headers are racey and can be present or may not be present when + // the page.on('request') handler is called. for _, req := range requests { i := slices.IndexFunc(expected, func(r request) bool { return req.URL == r.URL }) assert.NotEqual(t, -1, i, "failed to find expected request with URL %s", req.URL) - sortByName := func(m1, m2 map[string]string) int { - return strings.Compare(m1["name"], m2["name"]) + expectedHeadersArray := make(map[string]string) + for _, header := range expected[i].HeadersArray { + expectedHeadersArray[header["name"]] = header["value"] } - slices.SortFunc(req.HeadersArray, sortByName) - slices.SortFunc(expected[i].HeadersArray, sortByName) - assert.Equal(t, expected[i], req) + for name, value := range expected[i].AllHeaders { + actualValue, ok := req.AllHeaders[name] + require.True(t, ok, "missing allHeaders[%s] for %s", name, req.URL) + require.Equal(t, value, actualValue, "allHeaders[%s] mismatch for %s", name, req.URL) + } + require.Equal(t, expected[i].AcceptLanguageHeader, req.AcceptLanguageHeader, i) + require.Equal(t, expected[i].Headers, req.Headers, i) + for _, header := range req.HeadersArray { + if expectedValue, ok := expectedHeadersArray[header["name"]]; ok { + require.Equal(t, expectedValue, header["value"], "headersArray[%s] mismatch for %s", header["name"], req.URL) + delete(expectedHeadersArray, header["name"]) + } + } + require.Empty(t, expectedHeadersArray, "missing headersArray entries for %s", req.URL) + require.Equal(t, expected[i].IsNavigationRequest, req.IsNavigationRequest, i) + require.Equal(t, expected[i].Method, req.Method, i) + require.Equal(t, expected[i].PostData, req.PostData, i) + require.Equal(t, expected[i].PostDataBuffer, req.PostDataBuffer, i) + require.Equal(t, expected[i].ResourceType, req.ResourceType, i) + require.Equal(t, expected[i].URL, req.URL, i) } } type request struct { AllHeaders map[string]string `json:"allHeaders"` - FrameURL string `json:"frameUrl"` AcceptLanguageHeader string `json:"acceptLanguageHeader"` Headers map[string]string `json:"headers"` HeadersArray []map[string]string `json:"headersArray"` @@ -2813,16 +2866,224 @@ func TestPageOnResponse(t *testing.T) { } // Compare each response one by one for better test failure visibility + // We only want to compare against the expected values. The actual values + // may contain extra headers that are not present in the expected values. + // Extra headers are racey and can be present or may not be present when + // the page.on('response') handler is called. for _, resp := range responses { i := slices.IndexFunc(expected, func(r response) bool { return resp.URL == r.URL }) assert.NotEqual(t, -1, i, "failed to find expected request with URL %s", resp.URL) - sortByName := func(m1, m2 map[string]string) int { - return strings.Compare(m1["name"], m2["name"]) + expectedHeadersArray := make(map[string]string) + for _, header := range expected[i].HeadersArray { + expectedHeadersArray[header["name"]] = header["value"] + } + for name, value := range expected[i].AllHeaders { + actualValue, ok := resp.AllHeaders[name] + require.True(t, ok, "missing allHeaders[%s] for %s", name, resp.URL) + require.Equal(t, value, actualValue, "allHeaders[%s] mismatch for %s", name, resp.URL) + } + require.Equal(t, expected[i].Body, resp.Body, i) + require.Equal(t, expected[i].FrameURL, resp.FrameURL, i) + require.Equal(t, expected[i].AcceptLanguageHeader, resp.AcceptLanguageHeader, i) + require.Equal(t, expected[i].AcceptLanguageHeaders, resp.AcceptLanguageHeaders, i) + require.Equal(t, expected[i].Headers, resp.Headers, i) + for _, header := range resp.HeadersArray { + if expectedValue, ok := expectedHeadersArray[header["name"]]; ok { + require.Equal(t, expectedValue, header["value"], "headersArray[%s] mismatch for %s", header["name"], resp.URL) + delete(expectedHeadersArray, header["name"]) + } + } + require.Empty(t, expectedHeadersArray, "missing headersArray entries for %s", resp.URL) + require.Equal(t, expected[i].JSON, resp.JSON, i) + require.Equal(t, expected[i].OK, resp.OK, i) + require.Equal(t, expected[i].RequestURL, resp.RequestURL, i) + require.Equal(t, expected[i].SecurityDetails, resp.SecurityDetails, i) + require.Equal(t, expected[i].ServerAddr, resp.ServerAddr, i) + require.Equal(t, expected[i].Status, resp.Status, i) + require.Equal(t, expected[i].StatusText, resp.StatusText, i) + require.Equal(t, expected[i].URL, resp.URL, i) + require.Equal(t, expected[i].Text, resp.Text, i) + } +} + +func TestPageOnRequestAllHeadersExtraInfo(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + tb.withHandler("/set-cookie", func(w http.ResponseWriter, _ *http.Request) { + w.Header().Add("Set-Cookie", "test_cookie_name=test_cookie_value; Path=/") + _, err := fmt.Fprint(w, "ok") + require.NoError(t, err) + }) + + p := tb.NewPage(nil) + + opts := &common.FrameGotoOptions{ + WaitUntil: common.LifecycleEventNetworkIdle, + Timeout: common.DefaultTimeout, + } + // This will tell the browser to store the cookie + _, err := p.Goto(tb.url("/set-cookie"), opts) + require.NoError(t, err) + + // The cookie in the browser store is sent as a header in the request + gotoPage := func() error { + _, err := p.Goto(tb.url("/set-cookie"), opts) + return err + } + + // We are only guaranteed to have the extra headers when the requestfinished + // event is fired. Trying to retrieve the extra headers on anyother event will + // be racey and may not be present. + var ev common.PageEvent + waitForRequestFinished := func() error { + var err error + ev, err = p.WaitForEvent( + common.PageEventRequestFinished, + &common.PageWaitForEventOptions{Timeout: p.Timeout()}, + func(pe common.PageEvent) (bool, error) { + return strings.Contains(pe.Request.URL(), "/set-cookie"), nil + }, + ) + return err + } + + err = tb.run(tb.context(), gotoPage, waitForRequestFinished) + require.NoError(t, err) + require.NotNil(t, ev.Request) + + assert.Contains(t, ev.Request.AllHeaders()["cookie"], "test_cookie_name=test_cookie_value") +} + +func TestPageOnResponseAllHeadersExtraInfo(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + tb.withHandler("/set-cookie", func(w http.ResponseWriter, _ *http.Request) { + w.Header().Add("Set-Cookie", "test_cookie_name=test_cookie_value; Path=/") + _, err := fmt.Fprint(w, "ok") + require.NoError(t, err) + }) + + p := tb.NewPage(nil) + + opts := &common.FrameGotoOptions{ + WaitUntil: common.LifecycleEventNetworkIdle, + Timeout: common.DefaultTimeout, + } + gotoPage := func() error { + _, err := p.Goto(tb.url("/set-cookie"), opts) + return err + } + + // We are only guaranteed to have the extra headers when the requestfinished + // event is fired. Trying to retrieve the extra headers on anyother event will + // be racey and may not be present. + var ev common.PageEvent + waitForRequestFinished := func() error { + var err error + ev, err = p.WaitForEvent( + common.PageEventRequestFinished, + &common.PageWaitForEventOptions{Timeout: p.Timeout()}, + func(pe common.PageEvent) (bool, error) { + return strings.Contains(pe.Request.URL(), "/set-cookie"), nil + }, + ) + return err + } + + err := tb.run(tb.context(), gotoPage, waitForRequestFinished) + require.NoError(t, err) + require.NotNil(t, ev.Request) + + assert.Contains(t, ev.Request.Response().AllHeaders()["set-cookie"], "test_cookie_name=test_cookie_value") +} + +// TestPageOnResponseAllHeadersRedirectChain verifies that each response in a +// redirect chain receives its own distinct extra headers from +// responseReceivedExtraInfo, rather than merging them all together. +func TestPageOnResponseAllHeadersRedirectChain(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + + // A 3-step redirect chain where each hop sets a distinct Set-Cookie. + // Set-Cookie is delivered only via responseReceivedExtraInfo (Chrome + // strips it from the provisional responseReceived headers), so a hop's + // cookie can appear in allHeaders() only if the ExtraInfo events were + // paired with the correct response. This catches both mis-attribution (a + // later hop's cookie landing on an earlier response) and merging. + tb.withHandler("/redir-start", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Set-Cookie", "step=1; Path=/") + http.Redirect(w, r, tb.url("/redir-mid"), http.StatusFound) + }) + tb.withHandler("/redir-mid", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Set-Cookie", "step=2; Path=/") + http.Redirect(w, r, tb.url("/redir-end"), http.StatusFound) + }) + tb.withHandler("/redir-end", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Set-Cookie", "step=3; Path=/") + w.Header().Set("Content-Type", "text/html") + _, err := fmt.Fprint(w, "done") + require.NoError(t, err) + }) + + tb.vu.ActivateVU() + tb.vu.StartIteration(t) + defer tb.vu.EndIteration(t) + + gv, err := tb.vu.RunAsync(t, ` + const page = await browser.newPage(); + + var responses = []; + page.on('response', async (response) => { + const headers = await response.allHeaders(); + responses.push({ + url: response.url(), + setCookie: headers['set-cookie'] || '', + }); + }); + + await page.goto('%s', {waitUntil: 'networkidle'}); + await page.close(); + + return JSON.stringify(responses); + `, tb.url("/redir-start")) + require.NoError(t, err) + + got := k6test.ToPromise(t, gv) + + var responses []struct { + URL string `json:"url"` + SetCookie string `json:"setCookie"` + } + require.NoError(t, json.Unmarshal([]byte(got.Result().String()), &responses)) + + setCookieByURL := make(map[string]string, len(responses)) + for _, r := range responses { + setCookieByURL[r.URL] = r.SetCookie + } + + // Each hop must carry only its own Set-Cookie. + steps := []string{"step=1", "step=2", "step=3"} + for _, tc := range []struct { + url string + step string + }{ + {tb.url("/redir-start"), "step=1"}, + {tb.url("/redir-mid"), "step=2"}, + {tb.url("/redir-end"), "step=3"}, + } { + cookie, ok := setCookieByURL[tc.url] + require.True(t, ok, "no response captured for %s", tc.url) + assert.Contains(t, cookie, tc.step, "wrong Set-Cookie on %s", tc.url) + for _, other := range steps { + if other != tc.step { + assert.NotContains(t, cookie, other, + "%s leaked %s from another hop", tc.url, other) + } } - slices.SortFunc(resp.HeadersArray, sortByName) - slices.SortFunc(expected[i].HeadersArray, sortByName) - assert.Equal(t, expected[i], resp) } } @@ -4026,6 +4287,35 @@ func TestClickInNestedFramesCORS(t *testing.T) { }) } +func TestPageRouteConcurrentEnableDataRace(t *testing.T) { + t.Parallel() + + tb := newTestBrowser(t, withHTTPServer()) + p := tb.NewPage(nil) + + matcher := func(pattern, url string) (bool, error) { + return regexp.MatchString(fmt.Sprintf("http://[^/]*%s", pattern), url) + } + h := func(rt *common.Route) error { return rt.Continue(common.ContinueOptions{}) } + + // This test is intended to surface a data race under `go test -race`: concurrent first-time registrations + // can each observe routes as empty and call updateRequestInterception(true). + var wg sync.WaitGroup + errCh := make(chan error, 16) + for i := range 16 { + wg.Add(1) + go func(i int) { + defer wg.Done() + errCh <- p.Route(fmt.Sprintf("/api/%d", i), h, matcher) + }(i) + } + wg.Wait() + close(errCh) + for err := range errCh { + require.NoError(t, err) + } +} + func TestPageUnroute(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/browser/tests/static/get_by_role_implicit.html b/internal/js/modules/k6/browser/tests/static/get_by_role_implicit.html index d15a3b141..4aa261854 100644 --- a/internal/js/modules/k6/browser/tests/static/get_by_role_implicit.html +++ b/internal/js/modules/k6/browser/tests/static/get_by_role_implicit.html @@ -10,7 +10,8 @@ Link text - + + diff --git a/internal/js/modules/k6/browser/tests/test_browser.go b/internal/js/modules/k6/browser/tests/test_browser.go index 45613ba3f..6f4dbd54f 100644 --- a/internal/js/modules/k6/browser/tests/test_browser.go +++ b/internal/js/modules/k6/browser/tests/test_browser.go @@ -477,8 +477,3 @@ func asString(tb testing.TB, v any) string { require.True(tb, ok) return s } - -// toPtr is a helper function to convert a value to a pointer. -func toPtr[T any](v T) *T { - return &v -} diff --git a/internal/js/modules/k6/browser/tests/test_browser_proxy.go b/internal/js/modules/k6/browser/tests/test_browser_proxy.go index 1e170e845..6df4f7b7e 100644 --- a/internal/js/modules/k6/browser/tests/test_browser_proxy.go +++ b/internal/js/modules/k6/browser/tests/test_browser_proxy.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net" "net/http" "net/http/httptest" "net/url" @@ -102,9 +103,8 @@ LOOP: default: mt, message, err := in.ReadMessage() if err != nil { - var cerr *websocket.CloseError - if errors.As(err, &cerr) { - // If WS conn is closed, just return + if isConnShuttingDown(err) { + // If either WS conn is closing, just return. return } p.t.Fatalf("error reading message: %v", err) @@ -112,9 +112,8 @@ LOOP: err = out.WriteMessage(mt, message) if err != nil { - var cerr *websocket.CloseError - if errors.As(err, &cerr) { - // If WS conn is closed, just return + if isConnShuttingDown(err) { + // If either WS conn is closing, just return. return } p.t.Fatalf("error writing message: %v", err) @@ -122,3 +121,16 @@ LOOP: } } } + +// isConnShuttingDown reports whether err indicates one of the proxied WS +// connections is closing, so forwarding should stop cleanly instead of failing +// the test. This covers the peer sending a close frame (*websocket.CloseError), +// this side having already sent its own close frame (websocket.ErrCloseSent, +// e.g. when a client closes its connection while the browser is still emitting +// unsolicited events), and the underlying socket being closed (net.ErrClosed). +func isConnShuttingDown(err error) bool { + var cerr *websocket.CloseError + return errors.As(err, &cerr) || + errors.Is(err, websocket.ErrCloseSent) || + errors.Is(err, net.ErrClosed) +} diff --git a/internal/js/modules/k6/browser/tests/webvital_test.go b/internal/js/modules/k6/browser/tests/webvital_test.go index fda938afa..540927378 100644 --- a/internal/js/modules/k6/browser/tests/webvital_test.go +++ b/internal/js/modules/k6/browser/tests/webvital_test.go @@ -2,6 +2,7 @@ package tests import ( "runtime" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -103,6 +104,87 @@ func TestWebVitalMetricNoInteraction(t *testing.T) { } } +// TestWebVitalMetricPerPage asserts that web vitals are recorded for every +// page in a single-tab navigation flow, not just the last one. It navigates +// one page across two distinct URLs and, without interacting with the first +// page, checks that the now-intermediate page still reports its LCP (as well +// as FCP and TTFB), with exactly one sample per (url, metric). +func TestWebVitalMetricPerPage(t *testing.T) { + t.Parallel() + if runtime.GOOS == "windows" { + t.Skip("timeouts on windows") + } + + var ( + samples = make(chan k6metrics.SampleContainer, 1000) + browser = newTestBrowser(t, withFileServer(), withSamples(samples)) + page = browser.NewPage(nil) + ) + + opts := &common.FrameGotoOptions{ + Timeout: common.DefaultTimeout, + } + + // Navigate to the first page and dwell so its web vitals settle and are + // reported before navigating away. The page is not interacted with, so + // without continuous reporting its LCP is never finalized. + resp, err := page.Goto(browser.staticURL("/web_vitals.html"), opts) + require.NoError(t, err) + require.NotNil(t, resp) + + page.WaitForTimeout(1000) + + // Navigate to a second, distinct page. This makes web_vitals.html an + // intermediate page whose web vitals would be lost without continuous + // reporting. + resp, err = page.Goto(browser.staticURL("/usual.html"), opts) + require.NoError(t, err) + require.NotNil(t, resp) + + require.NoError(t, page.Close()) + close(samples) + + // Count web vital samples per (url, metric). + type urlMetric struct{ url, metric string } + counts := make(map[urlMetric]int) + for sc := range samples { + for _, s := range sc.GetSamples() { + if !strings.HasPrefix(s.Metric.Name, "browser_web_vital_") { + continue + } + url, ok := s.Tags.Get("url") + require.Truef(t, ok, "web vital sample %s is missing a url tag", s.Metric.Name) + counts[urlMetric{url: url, metric: s.Metric.Name}]++ + } + } + + // The intermediate page must have recorded LCP, the key differentiator: + // without continuous reporting it finalizes only on visibilitychange, + // which does not fire on a hard navigation. FCP and TTFB are asserted as + // a sanity check (they finalize early during load). + intermediateHas := func(metric string) bool { + for k := range counts { + if k.metric == metric && strings.Contains(k.url, "web_vitals.html") { + return true + } + } + return false + } + assert.True(t, intermediateHas("browser_web_vital_lcp"), + "expected intermediate page web_vitals.html to record LCP") + assert.True(t, intermediateHas("browser_web_vital_fcp"), + "expected intermediate page web_vitals.html to record FCP") + assert.True(t, intermediateHas("browser_web_vital_ttfb"), + "expected intermediate page web_vitals.html to record TTFB") + + // Continuous reporting must not duplicate samples: each (url, metric) + // pair must be emitted exactly once. + for k, n := range counts { + assert.Equalf(t, 1, n, + "expected exactly one %s sample for %s, got %d", k.metric, k.url, n) + } +} + func markExpectedWebVitalsFromSamples(samples <-chan k6metrics.SampleContainer, expected map[string]bool) { for metric := range samples { for _, s := range metric.GetSamples() { diff --git a/internal/js/modules/k6/browser/tests/ws/server.go b/internal/js/modules/k6/browser/tests/ws/server.go index cc0e7b6c2..f6721557f 100644 --- a/internal/js/modules/k6/browser/tests/ws/server.go +++ b/internal/js/modules/k6/browser/tests/ws/server.go @@ -60,7 +60,6 @@ func NewServer(t testing.TB, opts ...func(*Server)) *Server { dialer := k6netext.NewDialer(net.Dialer{ Timeout: 2 * time.Second, KeepAlive: 10 * time.Second, - DualStack: true, }, k6netext.NewResolver(net.LookupIP, 0, k6types.DNSfirst, k6types.DNSpreferIPv4)) const wsURL = "wsbin.local" diff --git a/internal/js/modules/k6/data/share.go b/internal/js/modules/k6/data/share.go index fd4d711ae..cc9fc698e 100644 --- a/internal/js/modules/k6/data/share.go +++ b/internal/js/modules/k6/data/share.go @@ -67,6 +67,16 @@ func (s wrappedSharedArray) deepFreeze(rt *sobek.Runtime, val sobek.Value) error return nil } + // Primitives (strings, numbers, booleans) are already immutable in + // JavaScript, so there is nothing to freeze. + // Skipping them also avoids a costly no-op: deep-freezing a large string + // would otherwise wrap it in a String object and iterate over every one + // of its character indices in the loop below, allocating enormous amounts + // of memory on every access. + if _, ok := val.(*sobek.Object); !ok { + return nil + } + _, err := s.freeze(sobek.Undefined(), val) if err != nil { return err diff --git a/internal/js/modules/k6/data/share_test.go b/internal/js/modules/k6/data/share_test.go index 409dd6877..50eb75590 100644 --- a/internal/js/modules/k6/data/share_test.go +++ b/internal/js/modules/k6/data/share_test.go @@ -216,6 +216,152 @@ func TestSharedArrayAnotherRuntimeWorking(t *testing.T) { require.NoError(t, err) } +// TestSharedArrayReadValuesIntact verifies that values read back through the +// (now primitive-skipping) deepFreeze path are intact: primitives of every JSON +// kind, an ASCII and a non-ASCII string (different internal representations), +// and a large string (the shape that used to blow up memory). Repeated reads +// must be consistent. +func TestSharedArrayReadValuesIntact(t *testing.T) { + t.Parallel() + runtime, err := newConfiguredRuntime(t) + require.NoError(t, err) + rt := runtime.VU.Runtime() + + _, err = rt.RunString(`'use strict'; + var LEN = 200000; + var s = new SharedArray("reads", function() { + return ["hello", "", "ünïçödé 🚀", 42, 3.5, -7, true, false, null, "A".repeat(LEN)]; + }); + if (s.length !== 10) { throw new Error("length="+s.length); } + if (s[0] !== "hello" || typeof s[0] !== "string") { throw new Error("s[0]="+s[0]); } + if (s[1] !== "" || typeof s[1] !== "string") { throw new Error("s[1]="+s[1]); } + if (s[2] !== "ünïçödé 🚀") { throw new Error("s[2]="+s[2]); } + if (s[3] !== 42) { throw new Error("s[3]="+s[3]); } + if (s[4] !== 3.5) { throw new Error("s[4]="+s[4]); } + if (s[5] !== -7) { throw new Error("s[5]="+s[5]); } + if (s[6] !== true) { throw new Error("s[6]="+s[6]); } + if (s[7] !== false) { throw new Error("s[7]="+s[7]); } + if (s[8] !== null) { throw new Error("s[8]="+s[8]); } + if (s[9].length !== LEN || s[9][0] !== "A" || s[9][LEN-1] !== "A") { throw new Error("bad large string"); } + `) + require.NoError(t, err) +} + +// TestSharedArrayDeepFreezeSemantics verifies the safety property that matters: +// object and array elements (including nested ones) remain deeply frozen after +// the primitive fast-path change, so a VU cannot mutate the shared data. +func TestSharedArrayDeepFreezeSemantics(t *testing.T) { + t.Parallel() + + const setup = `'use strict'; + var s = new SharedArray("rich", function() { + return [{ + str: "hello", + num: 1, + flag: true, + nothing: null, + nested: { deep: "x", arr: [1, 2, 3] }, + list: [ {k: "a"}, {k: "b"} ] + }, [10, 20, 30]]; + }); + ` + + // integrity asserts the shared structure is untouched. It throws (failing + // the surrounding RunString) if any value changed or a property was added. + const integrity = `'use strict'; + if (s[0].str !== "hello") { throw new Error("str changed: "+s[0].str); } + if (s[0].num !== 1) { throw new Error("num changed: "+s[0].num); } + if ("extra" in s[0]) { throw new Error("property was added to object"); } + if (s[0].nested.deep !== "x") { throw new Error("nested.deep changed"); } + if ("extra" in s[0].nested) { throw new Error("property added to nested"); } + if (s[0].nested.arr.length !== 3 || s[0].nested.arr[0] !== 1) { throw new Error("nested.arr changed"); } + if (s[0].list.length !== 2 || s[0].list[0].k !== "a") { throw new Error("list changed"); } + if (s[1].length !== 3 || s[1][0] !== 10) { throw new Error("top-level array changed"); } + ` + + // The deep freeze must reject every one of these mutations. Top-level plain + // object freezing is already covered by TestSharedArrayAnotherRuntimeExceptions; + // these focus on the nested and array cases, which exercise the recursion and + // the values that pass through the *sobek.Object guard as containers. + cases := map[string]string{ + "mutate nested object": `s[0].nested.deep = "bad";`, + "add property to nested object": `s[0].nested.extra = "bad";`, + "delete nested property": `delete s[0].nested.deep;`, + "push to nested array": `s[0].nested.arr.push(4);`, + "set nested array index": `s[0].nested.arr[0] = 99;`, + "mutate object inside array": `s[0].list[0].k = "bad";`, + "push to top-level array element": `s[1].push(40);`, + "set index on top-level array element": `s[1][0] = 99;`, + } + + for name, code := range cases { + t.Run(name, func(t *testing.T) { + t.Parallel() + runtime, err := newConfiguredRuntime(t) + require.NoError(t, err) + rt := runtime.VU.Runtime() + _, err = rt.RunString(setup) + require.NoError(t, err) + + // Strict mode is required: in sloppy mode, writes to frozen + // objects fail silently instead of throwing. + _, err = rt.RunString("'use strict';\n" + code) + require.Error(t, err, "mutation should have been rejected by the freeze") + exc := new(sobek.Exception) + require.True(t, errors.As(err, &exc)) + require.Contains(t, exc.Error(), "TypeError", + "a frozen write must fail with a TypeError") + + // The shared data must be completely unchanged. + _, err = rt.RunString(integrity) + require.NoError(t, err) + }) + } +} + +// TestSharedArrayLargeStringGetAllocations is a regression guard for the memory +// blow-up: reading a large string element must not allocate an amount of memory +// proportional to the string length. +// +//nolint:paralleltest // testing.AllocsPerRun must not run concurrently. +func TestSharedArrayLargeStringGetAllocations(t *testing.T) { + runtime, err := newConfiguredRuntime(t) + require.NoError(t, err) + rt := runtime.VU.Runtime() + + _, err = rt.RunString(` + var shared = new SharedArray("alloc", function() { + return ["short", "A".repeat(100000)]; + }); + var get = function(i) { return shared[i]; }; + `) + require.NoError(t, err) + + get, ok := sobek.AssertFunction(rt.Get("get")) + require.True(t, ok) + + small := testing.AllocsPerRun(50, func() { + _, err := get(sobek.Undefined(), rt.ToValue(0)) + if err != nil { + panic(err) + } + }) + large := testing.AllocsPerRun(50, func() { + _, err := get(sobek.Undefined(), rt.ToValue(1)) + if err != nil { + panic(err) + } + }) + + t.Logf("allocations per Get: small string=%.0f, 100k string=%.0f", small, large) + + // With the bug, `large` was on the order of the string length (100k+). + // After the fix it is a small constant, close to `small`. Use a generous + // bound that is decisively below the buggy behavior. + require.Less(t, large, 2000.0, + "reading a large string element allocates too much; deepFreeze may be enumerating characters again") +} + func TestSharedArrayRaceInInitialization(t *testing.T) { t.Parallel() diff --git a/internal/js/modules/k6/execution/execution_test.go b/internal/js/modules/k6/execution/execution_test.go index b01ec6e67..247ec7624 100644 --- a/internal/js/modules/k6/execution/execution_test.go +++ b/internal/js/modules/k6/execution/execution_test.go @@ -287,7 +287,7 @@ func TestFailTest(t *testing.T) { func TestOptionsTestFull(t *testing.T) { t.Parallel() - expected := `{"paused":true,"scenarios":{"const-vus":{"executor":"constant-vus","options":{"browser":{"someOption":true}},"startTime":"10s","gracefulStop":"30s","env":{"FOO":"bar"},"exec":"default","tags":{"tagkey":"tagvalue"},"vus":50,"duration":"10m0s"}},"executionSegment":"0:1/4","executionSegmentSequence":"0,1/4,1/2,1","noSetup":true,"setupTimeout":"1m0s","noTeardown":true,"teardownTimeout":"5m0s","rps":100,"dns":{"ttl":"1m","select":"roundRobin","policy":"any"},"maxRedirects":3,"userAgent":"k6-user-agent","batch":15,"batchPerHost":5,"httpDebug":"full","httpErrorData":null,"insecureSkipTLSVerify":true,"tlsCipherSuites":["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"],"tlsVersion":{"min":"tls1.2","max":"tls1.3"},"tlsAuth":[{"domains":["example.com"],"cert":"mycert.pem","key":"mycert-key.pem","password":"mypwd"}],"throw":true,"thresholds":{"http_req_duration":[{"threshold":"rate>0.01","abortOnFail":true,"delayAbortEval":"10s"}]},"blacklistIPs":["192.0.2.0/24"],"blockHostnames":["test.k6.io","*.example.com"],"hosts":{"test.k6.io":"1.2.3.4:8443"},"noConnectionReuse":true,"noVUConnectionReuse":true,"minIterationDuration":"10s","ext":{"ext-one":{"rawkey":"rawvalue"}},"summaryTrendStats":["avg","min","max"],"summaryTimeUnit":"ms","systemTags":["iter","vu"],"tags":null,"metricSamplesBufferSize":8,"noCookiesReset":true,"discardResponseBodies":true,"consoleOutput":"loadtest.log","tags":{"runtag-key":"runtag-value"},"localIPs":"192.168.20.12-192.168.20.15,192.168.10.0/27","features":null}` + expected := `{"paused":true,"scenarios":{"const-vus":{"executor":"constant-vus","options":{"browser":{"someOption":true}},"startTime":"10s","gracefulStop":"30s","env":{"FOO":"bar"},"exec":"default","tags":{"tagkey":"tagvalue"},"vus":50,"duration":"10m0s"}},"executionSegment":"0:1/4","executionSegmentSequence":"0,1/4,1/2,1","noSetup":true,"setupTimeout":"1m0s","noTeardown":true,"teardownTimeout":"5m0s","rps":100,"dns":{"ttl":"1m","select":"roundRobin","policy":"any"},"maxRedirects":3,"userAgent":"k6-user-agent","batch":15,"batchPerHost":5,"httpErrorData":null,"httpDebug":"full","insecureSkipTLSVerify":true,"tlsCipherSuites":["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"],"tlsVersion":{"min":"tls1.2","max":"tls1.3"},"tlsAuth":[{"domains":["example.com"],"cert":"mycert.pem","key":"mycert-key.pem","password":"mypwd"}],"tlsAIAFetch":null,"throw":true,"thresholds":{"http_req_duration":[{"threshold":"rate>0.01","abortOnFail":true,"delayAbortEval":"10s"}]},"blacklistIPs":["192.0.2.0/24"],"blockHostnames":["test.k6.io","*.example.com"],"hosts":{"test.k6.io":"1.2.3.4:8443"},"noConnectionReuse":true,"noVUConnectionReuse":true,"minIterationDuration":"10s","ext":{"ext-one":{"rawkey":"rawvalue"}},"handleSummaryTimeout":null,"summaryTrendStats":["avg","min","max"],"summaryTimeUnit":"ms","systemTags":["iter","vu"],"tags":null,"metricSamplesBufferSize":8,"noCookiesReset":true,"discardResponseBodies":true,"consoleOutput":"loadtest.log","tags":{"runtag-key":"runtag-value"},"localIPs":"192.168.20.12-192.168.20.15,192.168.10.0/27","features":null}` var ( rt = sobek.New() diff --git a/internal/js/modules/k6/experimental/streams/goja.go b/internal/js/modules/k6/experimental/streams/goja.go deleted file mode 100644 index 77521d9b3..000000000 --- a/internal/js/modules/k6/experimental/streams/goja.go +++ /dev/null @@ -1,115 +0,0 @@ -package streams - -import ( - "fmt" - "reflect" - - "github.com/grafana/sobek" - - "go.k6.io/k6/v2/js/common" - "go.k6.io/k6/v2/js/modules" -) - -// newResolvedPromise instantiates a new resolved promise. -func newResolvedPromise(vu modules.VU, with sobek.Value) *sobek.Promise { - promise, resolve, _ := vu.Runtime().NewPromise() - err := resolve(with) - if err != nil { // TODO(@mstoykov): likely better to actually call Promise.resolve directly - panic(err) - } - - return promise -} - -// newRejectedPromise instantiates a new rejected promise. -func newRejectedPromise(vu modules.VU, with any) *sobek.Promise { - promise, _, reject := vu.Runtime().NewPromise() - err := reject(with) - if err != nil { - panic(err) - } - return promise -} - -// promiseThen facilitates instantiating a new promise and defining callbacks for to be executed -// on fulfillment as well as rejection, directly from Go. -func promiseThen( - rt *sobek.Runtime, - promise *sobek.Promise, - onFulfilled, onRejected func(sobek.Value), -) (*sobek.Promise, error) { - val, err := rt.RunString( - `(function(promise, onFulfilled, onRejected) { return promise.then(onFulfilled, onRejected) })`) - if err != nil { - return nil, newError(RuntimeError, "unable to initialize promiseThen internal helper function") - } - - cal, ok := sobek.AssertFunction(val) - if !ok { - return nil, newError(RuntimeError, "the internal promiseThen helper is not a function") - } - - if onRejected == nil { - val, err = cal(sobek.Undefined(), rt.ToValue(promise), rt.ToValue(onFulfilled)) - } else { - val, err = cal(sobek.Undefined(), rt.ToValue(promise), rt.ToValue(onFulfilled), rt.ToValue(onRejected)) - } - - if err != nil { - return nil, err - } - - newPromise, ok := val.Export().(*sobek.Promise) - if !ok { - return nil, newError(RuntimeError, "unable to cast the internal promiseThen helper's return value to a promise") - } - - return newPromise, nil -} - -// isNumber returns true if the given sobek.Value holds a number -func isNumber(value sobek.Value) bool { - _, isFloat := value.Export().(float64) - _, isInt := value.Export().(int64) - - return isFloat || isInt -} - -// isNonNegativeNumber implements the [IsNonNegativeNumber] algorithm. -// -// [IsNonNegativeNumber]: https://streams.spec.whatwg.org/#is-non-negative-number -func isNonNegativeNumber(value sobek.Value) bool { - if common.IsNullish(value) { - return false - } - - if !isNumber(value) { - return false - } - - if value.ToFloat() < 0 || value.ToInteger() < 0 { - return false - } - - return true -} - -// setReadOnlyPropertyOf sets a read-only property on the given [sobek.Object]. -func setReadOnlyPropertyOf(obj *sobek.Object, objName, propName string, propValue sobek.Value) error { - err := obj.DefineDataProperty(propName, - propValue, - sobek.FLAG_FALSE, - sobek.FLAG_FALSE, - sobek.FLAG_TRUE, - ) - if err != nil { - return fmt.Errorf("unable to define %s read-only property on %s object; reason: %w", propName, objName, err) - } - - return nil -} - -// isObject determines whether the given [sobek.Value] is a [sobek.Object] or not. -func isObject(val sobek.Value) bool { - return val != nil && val.ExportType() != nil && val.ExportType().Kind() == reflect.Map -} diff --git a/internal/js/modules/k6/experimental/streams/module.go b/internal/js/modules/k6/experimental/streams/module.go index 6928aef59..a5c24c988 100644 --- a/internal/js/modules/k6/experimental/streams/module.go +++ b/internal/js/modules/k6/experimental/streams/module.go @@ -17,6 +17,11 @@ type ( // ModuleInstance is the module instance that will be created for each VU. ModuleInstance struct { vu modules.VU + + exports modules.Exports + + writableStreamPrototype *sobek.Object + writableStreamDefaultWriterPrototype *sobek.Object } ) @@ -33,18 +38,60 @@ func New() *RootModule { // NewModuleInstance creates a new instance of the module for a specific VU. func (rm *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { - return &ModuleInstance{ - vu: vu, + mi := &ModuleInstance{vu: vu} + rt := vu.Runtime() + + // Convert the writable stream constructors once per VU. Besides keeping the exported + // constructor identities stable, this gives the implementation canonical prototypes that + // cannot be replaced by a caller-controlled `this` value. + writableStreamConstructor, err := newWebIDLConstructor(rt, "WritableStream", mi.NewWritableStream) + if err != nil { + throw(rt, err) + } + writableStreamDefaultWriterConstructor, err := newWebIDLConstructor( + rt, + "WritableStreamDefaultWriter", + mi.NewWritableStreamDefaultWriter, + ) + if err != nil { + throw(rt, err) } -} -// Exports returns the module exports, that will be available in the runtime. -func (mi *ModuleInstance) Exports() modules.Exports { - return modules.Exports{Named: map[string]any{ + mi.writableStreamPrototype = constructorPrototype(rt, writableStreamConstructor) + mi.writableStreamDefaultWriterPrototype = constructorPrototype(rt, writableStreamDefaultWriterConstructor) + if err := installWritableStreamPrototype(rt, mi.writableStreamPrototype); err != nil { + throw(rt, err) + } + if err := installWritableStreamDefaultWriterPrototype(rt, mi.writableStreamDefaultWriterPrototype); err != nil { + throw(rt, err) + } + mi.exports = modules.Exports{Named: map[string]any{ "ReadableStream": mi.NewReadableStream, "CountQueuingStrategy": mi.NewCountQueuingStrategy, "ReadableStreamDefaultReader": mi.NewReadableStreamDefaultReader, + "WritableStream": writableStreamConstructor, + "WritableStreamDefaultWriter": writableStreamDefaultWriterConstructor, }} + + return mi +} + +func constructorPrototype(rt *sobek.Runtime, constructor sobek.Value) *sobek.Object { + return constructor.ToObject(rt).Get("prototype").ToObject(rt) +} + +func requireNewTarget(rt *sobek.Runtime, call sobek.ConstructorCall, name string) { + // Web IDL interface constructors must be invoked with `new`. Rejecting every ordinary + // function call also prevents a caller-controlled `this` value from being used as an + // instance prototype. + if call.NewTarget == nil { + throw(rt, newTypeError(rt, name+" constructor must be called with new")) + } +} + +// Exports returns the module exports, that will be available in the runtime. +func (mi *ModuleInstance) Exports() modules.Exports { + return mi.exports } // NewReadableStream is the constructor for the ReadableStream object. @@ -134,6 +181,109 @@ func newReadableStream(vu modules.VU, call sobek.ConstructorCall) *sobek.Object return streamObj } + +// NewWritableStream is the constructor for the WritableStream object. +func (mi *ModuleInstance) NewWritableStream(call sobek.ConstructorCall) *sobek.Object { + rt := mi.vu.Runtime() + requireNewTarget(rt, call, "WritableStream") + + return newWritableStream(mi.vu, call, mi.writableStreamDefaultWriterPrototype) +} + +func newWritableStream( + vu modules.VU, + call sobek.ConstructorCall, + writerPrototype *sobek.Object, +) *sobek.Object { + rt := vu.Runtime() + + var ( + // 1. If underlyingSink is missing, set it to null. + underlyingSink *sobek.Object + + err error + strategy *sobek.Object + underlyingSinkDict UnderlyingSink + ) + + // We look for the queuing strategy first, and validate it before the underlying sink, + // in order to match the specification's argument conversion order and pass the Web + // Platform Tests constructor tests. + strategy = initializeStrategy(rt, call) + + // 2. Let underlyingSinkDict be underlyingSink, converted to an IDL value of type UnderlyingSink. + if len(call.Arguments) > 0 && !sobek.IsUndefined(call.Arguments[0]) { + // We first assert that it is an object (requirement). + if !isObject(call.Arguments[0]) { + throw(rt, newTypeError(rt, "underlyingSink must be an object")) + } + + // Then we try to convert it to an UnderlyingSink. + underlyingSink = call.Arguments[0].ToObject(rt) + underlyingSinkDict, err = NewUnderlyingSinkFromObject(rt, underlyingSink) + if err != nil { + throw(rt, err) + } + } + + // 3. If underlyingSinkDict["type"] exists, throw a RangeError exception. + if underlyingSinkDict.Type != nil && !sobek.IsUndefined(underlyingSinkDict.Type) { + throw(rt, newRangeError(rt, "'type' is not supported by WritableStream")) + } + + // 4. Perform ! InitializeWritableStream(this). + stream := &WritableStream{ + runtime: rt, + vu: vu, + writerPrototype: writerPrototype, + } + stream.initialize() + + // 5. Let sizeAlgorithm be ! ExtractSizeAlgorithm(strategy). + sizeAlgorithm := extractSizeAlgorithm(rt, strategy) + + // 6. Let highWaterMark be ? ExtractHighWaterMark(strategy, 1). + highWaterMark := extractHighWaterMark(rt, strategy, 1) + + // 7. Perform ? SetUpWritableStreamDefaultControllerFromUnderlyingSink(...). + stream.setupWritableStreamDefaultControllerFromUnderlyingSink( + underlyingSink, + underlyingSinkDict, + highWaterMark, + sizeAlgorithm, + ) + + return stream.toObject(call.This.Prototype()) +} + +// NewWritableStreamDefaultWriter is the constructor for the [WritableStreamDefaultWriter] object. +// +// [WritableStreamDefaultWriter]: https://streams.spec.whatwg.org/#writablestreamdefaultwriter +func (mi *ModuleInstance) NewWritableStreamDefaultWriter(call sobek.ConstructorCall) *sobek.Object { + rt := mi.vu.Runtime() + requireNewTarget(rt, call, "WritableStreamDefaultWriter") + + if len(call.Arguments) != 1 { + throw(rt, newTypeError(rt, "WritableStreamDefaultWriter takes a single argument")) + } + + stream := writableStreamFromValue(rt, call.Argument(0)) + if stream == nil { + throw(rt, newTypeError(rt, "WritableStreamDefaultWriter argument must be a WritableStream")) + } + + // 1. Perform ? SetUpWritableStreamDefaultWriter(this, stream). + writer := &WritableStreamDefaultWriter{} + writer.setup(stream) + + object, err := NewWritableStreamDefaultWriterObject(writer, call.This.Prototype()) + if err != nil { + throw(rt, err) + } + + return object +} + func defaultSizeFunc(_ sobek.Value) (float64, error) { return 1.0, nil } func initializeStrategy(rt *sobek.Runtime, call sobek.ConstructorCall) *sobek.Object { diff --git a/internal/js/modules/k6/experimental/streams/readable_stream_default_controller.go b/internal/js/modules/k6/experimental/streams/readable_stream_default_controller.go index fb0947531..f636dd3eb 100644 --- a/internal/js/modules/k6/experimental/streams/readable_stream_default_controller.go +++ b/internal/js/modules/k6/experimental/streams/readable_stream_default_controller.go @@ -76,7 +76,8 @@ func NewReadableStreamDefaultControllerObject(controller *ReadableStreamDefaultC // Exposing the properties of the [ReadableStreamController] interface if err := setReadOnlyPropertyOf(obj, objName, "constructor", rt.ToValue(func() sobek.Value { - return rt.ToValue(&ReadableStreamDefaultController{}) + throw(rt, newTypeError(rt, "ReadableStreamDefaultController is not constructable")) + return sobek.Undefined() })); err != nil { return nil, err } diff --git a/internal/js/modules/k6/experimental/streams/readable_stream_default_reader.go b/internal/js/modules/k6/experimental/streams/readable_stream_default_reader.go index 19090cc8d..2a61414e9 100644 --- a/internal/js/modules/k6/experimental/streams/readable_stream_default_reader.go +++ b/internal/js/modules/k6/experimental/streams/readable_stream_default_reader.go @@ -22,8 +22,7 @@ func NewReadableStreamDefaultReaderObject(reader *ReadableStreamDefaultReader) ( objName := "ReadableStreamDefaultReader" err := obj.DefineAccessorProperty("closed", rt.ToValue(func() *sobek.Promise { - p, _, _ := reader.GetClosed() - return p + return reader.getClosed().promise }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE) if err != nil { return nil, err diff --git a/internal/js/modules/k6/experimental/streams/readable_stream_reader.go b/internal/js/modules/k6/experimental/streams/readable_stream_reader.go index 823a4622c..8ff5b86e9 100644 --- a/internal/js/modules/k6/experimental/streams/readable_stream_reader.go +++ b/internal/js/modules/k6/experimental/streams/readable_stream_reader.go @@ -35,11 +35,11 @@ type ReadableStreamGenericReader interface { // SetStream sets the stream that owns this reader. SetStream(stream *ReadableStream) - // GetClosed returns a [sobek.Promise] that resolves when the stream is closed. - GetClosed() (p *sobek.Promise, resolve, reject func(any) error) + // getClosed returns the promise that resolves when the stream is closed. + getClosed() *promiseWrapper - // SetClosed sets the [sobek.Promise] that resolves when the stream is closed. - SetClosed(p *sobek.Promise, resolve, reject func(any) error) + // setClosed sets the promise that resolves when the stream is closed. + setClosed(*promiseWrapper) // Cancel returns a [sobek.Promise] that resolves when the stream is canceled. Cancel(reason sobek.Value) *sobek.Promise @@ -47,9 +47,7 @@ type ReadableStreamGenericReader interface { // BaseReadableStreamReader is a base implement type BaseReadableStreamReader struct { - closedPromise *sobek.Promise - closedPromiseResolveFunc func(resolve any) error - closedPromiseRejectFunc func(reason any) error + closedPromise *promiseWrapper // stream is a [ReadableStream] instance that owns this reader stream *ReadableStream @@ -73,16 +71,14 @@ func (reader *BaseReadableStreamReader) SetStream(stream *ReadableStream) { reader.vu = stream.vu } -// GetClosed returns the reader's closed promise as well as its resolve and reject functions. -func (reader *BaseReadableStreamReader) GetClosed() (p *sobek.Promise, resolve, reject func(any) error) { - return reader.closedPromise, reader.closedPromiseResolveFunc, reader.closedPromiseRejectFunc +// getClosed returns the reader's closed promise. +func (reader *BaseReadableStreamReader) getClosed() *promiseWrapper { + return reader.closedPromise } -// SetClosed sets the reader's closed promise as well as its resolve and reject functions. -func (reader *BaseReadableStreamReader) SetClosed(p *sobek.Promise, resolve, reject func(any) error) { - reader.closedPromise = p - reader.closedPromiseResolveFunc = resolve - reader.closedPromiseRejectFunc = reject +// setClosed sets the reader's closed promise. +func (reader *BaseReadableStreamReader) setClosed(promise *promiseWrapper) { + reader.closedPromise = promise } // Cancel returns a [sobek.Promise] that resolves when the stream is canceled. @@ -134,24 +130,14 @@ func (reader *BaseReadableStreamReader) release() { // 4. If stream.[[state]] is "readable", reject reader.[[closedPromise]] with a TypeError exception. if stream.state == ReadableStreamStateReadable { - err := reader.closedPromiseRejectFunc(newTypeError(reader.runtime, "stream is readable").Err()) - if err != nil { - panic(err) - } + reader.closedPromise.rejectWith(newTypeError(reader.runtime, "stream is readable")) } else { // 5. Otherwise, set reader.[[closedPromise]] to a promise rejected with a TypeError exception. - reader.closedPromise = newRejectedPromise(stream.vu, newTypeError(reader.runtime, "stream is not readable").Err()) + reader.closedPromise = newRejectedPromiseWrapper( + stream.runtime, newTypeError(reader.runtime, "stream is not readable")) } // 6. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. - // FIXME: See https://github.com/dop251/goja/issues/565 - var ( - err error - doNothing = func(sobek.Value) {} - ) - _, err = promiseThen(stream.vu.Runtime(), reader.closedPromise, doNothing, doNothing) - if err != nil { - common.Throw(stream.vu.Runtime(), newError(RuntimeError, err.Error())) - } + markPromiseHandled(stream.runtime, reader.closedPromise.promise) // 7. Perform ! stream.[[controller]].[[ReleaseSteps]](). stream.controller.releaseSteps() @@ -190,7 +176,7 @@ func ReadableStreamReaderGenericInitialize(reader ReadableStreamGenericReader, s stream.reader = reader stream.Locked = true - promise, resolve, reject := stream.runtime.NewPromise() + promise := newPromiseWrapper(stream.runtime) switch stream.state { // 3. If stream.[[state]] is "readable", @@ -200,10 +186,7 @@ func ReadableStreamReaderGenericInitialize(reader ReadableStreamGenericReader, s // 4. Otherwise, if stream.[[state]] is "closed", case ReadableStreamStateClosed: // 4.1 Set reader.[[closedPromise]] to a promise resolved with undefined. - err := resolve(sobek.Undefined()) - if err != nil { - panic(err) // TODO(@mstoykov): probably better to move them out as errors - } + promise.resolveWith(sobek.Undefined()) // 5. Otherwise, default: // 5.1 Assert: stream.[[state]] is "errored". @@ -212,29 +195,15 @@ func ReadableStreamReaderGenericInitialize(reader ReadableStreamGenericReader, s } // 5.2 Set reader.[[closedPromise]] to a promise rejected with stream.[[storedError]]. - if jsErr, ok := stream.storedError.(*jsError); ok { - err := reject(jsErr.Err()) - if err != nil { - panic(err) - } + if _, ok := stream.storedError.(*jsError); ok { + promise.rejectWith(stream.storedError) } else { - err := reject(errToObj(stream.runtime, stream.storedError)) - if err != nil { - panic(err) - } + promise.rejectWith(errToObj(stream.runtime, stream.storedError)) } // 5.3 Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. - // See https://github.com/dop251/goja/issues/565 - var ( - err error - doNothing = func(sobek.Value) {} - ) - _, err = promiseThen(stream.vu.Runtime(), promise, doNothing, doNothing) - if err != nil { - common.Throw(stream.vu.Runtime(), newError(RuntimeError, err.Error())) - } + markPromiseHandled(stream.runtime, promise.promise) } - reader.SetClosed(promise, resolve, reject) + reader.setClosed(promise) } diff --git a/internal/js/modules/k6/experimental/streams/readable_streams.go b/internal/js/modules/k6/experimental/streams/readable_streams.go index 988d868d0..93ea90988 100644 --- a/internal/js/modules/k6/experimental/streams/readable_streams.go +++ b/internal/js/modules/k6/experimental/streams/readable_streams.go @@ -218,8 +218,7 @@ func (stream *ReadableStream) pullAlgorithm( return func(obj *sobek.Object) *sobek.Promise { v, err := call(underlyingSource, obj) if err != nil { - var ex *sobek.Exception - if errors.As(err, &ex) { + if ex, ok := errors.AsType[*sobek.Exception](err); ok { return newRejectedPromise(stream.vu, ex.Value()) } return newRejectedPromise(stream.vu, err) @@ -455,11 +454,7 @@ func (stream *ReadableStream) close() { common.Throw(stream.vu.Runtime(), newError(AssertionError, "reader is not a ReadableStreamGenericReader")) } - _, resolveFunc, _ := genericReader.GetClosed() - err := resolveFunc(sobek.Undefined()) - if err != nil { - panic(err) // TODO(@mstoykov): propagate as error instead - } + genericReader.getClosed().resolveWith(sobek.Undefined()) // 6. If reader implements ReadableStreamDefaultReader, defaultReader, ok := reader.(*ReadableStreamDefaultReader) @@ -506,24 +501,11 @@ func (stream *ReadableStream) error(e any) { } // 6. Reject reader.[[closedPromise]] with e. - var err error - promise, _, rejectFunc := genericReader.GetClosed() - if jsErr, ok := e.(*jsError); ok { - err = rejectFunc(jsErr.Err()) - } else { - err = rejectFunc(e) - } - if err != nil { - panic(err) // TODO(@mstoykov): propagate as error instead - } + promise := genericReader.getClosed() + promise.rejectWith(e) // 7. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true. - // See https://github.com/dop251/goja/issues/565 - doNothing := func(sobek.Value) {} - _, err = promiseThen(stream.vu.Runtime(), promise, doNothing, doNothing) - if err != nil { - common.Throw(stream.vu.Runtime(), newError(RuntimeError, err.Error())) - } + markPromiseHandled(stream.runtime, promise.promise) // 8. If reader implements ReadableStreamDefaultReader, defaultReader, ok := reader.(*ReadableStreamDefaultReader) diff --git a/internal/js/modules/k6/experimental/streams/readable_streams_test.go b/internal/js/modules/k6/experimental/streams/readable_streams_test.go index 9c651ac37..d6e54df72 100644 --- a/internal/js/modules/k6/experimental/streams/readable_streams_test.go +++ b/internal/js/modules/k6/experimental/streams/readable_streams_test.go @@ -68,6 +68,7 @@ func newConfiguredRuntime(t testing.TB) *modulestest.Runtime { "resources/rs-test-templates.js", "resources/rs-utils.js", "resources/test-utils.js", + "resources/recording-streams.js", } for _, file := range files { compileAndRun(t, rt, webPlatformTestSuite+"/streams", file) diff --git a/internal/js/modules/k6/experimental/streams/sobek.go b/internal/js/modules/k6/experimental/streams/sobek.go new file mode 100644 index 000000000..e51fdf3f1 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/sobek.go @@ -0,0 +1,226 @@ +package streams + +import ( + "fmt" + "reflect" + "slices" + + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/js/common" + "go.k6.io/k6/v2/js/modules" +) + +// newWebIDLConstructor wraps a native Sobek constructor so it follows Web IDL call semantics. +// Sobek otherwise treats a native constructor call without `new` exactly like construction and +// does not expose a NewTarget that lets the native function distinguish the two cases. +func newWebIDLConstructor(rt *sobek.Runtime, name string, constructor any) (sobek.Value, error) { + factory, err := rt.RunString(` +(function(constructor, name) { + const wrapper = function(...args) { + if (new.target === undefined) { + throw new TypeError(name + " constructor must be called with new"); + } + return Reflect.construct(constructor, args, new.target); + }; + Object.defineProperty(wrapper, "name", { value: name, configurable: true }); + return wrapper; +})`) + if err != nil { + return nil, err + } + + call, ok := sobek.AssertFunction(factory) + if !ok { + return nil, newError(RuntimeError, "Web IDL constructor wrapper factory is not a function") + } + + return call(sobek.Undefined(), rt.ToValue(constructor), rt.ToValue(name)) +} + +// promiseWrapper holds a [sobek.Promise] together with its resolve and reject functions, +// so that it can be settled later from Go code. +type promiseWrapper struct { + promise *sobek.Promise + resolve func(any) error + reject func(any) error + settlementQueued bool +} + +// newPromiseWrapper creates a new pending [promiseWrapper]. +func newPromiseWrapper(rt *sobek.Runtime) *promiseWrapper { + p, resolve, reject := rt.NewPromise() + return &promiseWrapper{promise: p, resolve: resolve, reject: reject} +} + +func (pw *promiseWrapper) isPending() bool { + return !pw.settlementQueued && pw.promise.State() == sobek.PromiseStatePending +} + +func (pw *promiseWrapper) queueSettlement() { + pw.settlementQueued = true +} + +// resolveWith resolves the wrapped promise with the given value. +func (pw *promiseWrapper) resolveWith(value any) { + pw.queueSettlement() + if err := pw.resolve(value); err != nil { + panic(err) // TODO(@mstoykov): propagate as error instead + } +} + +// rejectWith rejects the wrapped promise with the given reason, unwrapping [jsError] values. +func (pw *promiseWrapper) rejectWith(reason any) { + pw.queueSettlement() + if jsErr, ok := reason.(*jsError); ok { + reason = jsErr.Err() + } + if err := pw.reject(reason); err != nil { + panic(err) // TODO(@mstoykov): propagate as error instead + } +} + +// newResolvedPromiseWrapper creates a new [promiseWrapper] resolved with the given value. +func newResolvedPromiseWrapper(rt *sobek.Runtime, value any) *promiseWrapper { + pw := newPromiseWrapper(rt) + pw.resolveWith(value) + return pw +} + +// newRejectedPromiseWrapper creates a new [promiseWrapper] rejected with the given reason. +func newRejectedPromiseWrapper(rt *sobek.Runtime, reason any) *promiseWrapper { + pw := newPromiseWrapper(rt) + pw.rejectWith(reason) + return pw +} + +// newResolvedPromise instantiates a new resolved promise. +func newResolvedPromise(vu modules.VU, with sobek.Value) *sobek.Promise { + return newResolvedPromiseWrapper(vu.Runtime(), with).promise +} + +// newRejectedPromise instantiates a new rejected promise. +func newRejectedPromise(vu modules.VU, with any) *sobek.Promise { + return newRejectedPromiseWrapper(vu.Runtime(), with).promise +} + +// promiseThen facilitates instantiating a new promise and defining callbacks for to be executed +// on fulfillment as well as rejection, directly from Go. +func promiseThen( + rt *sobek.Runtime, + promise *sobek.Promise, + onFulfilled, onRejected func(sobek.Value), +) (*sobek.Promise, error) { + val, err := rt.RunString( + `(function(promise, onFulfilled, onRejected) { return promise.then(onFulfilled, onRejected) })`) + if err != nil { + return nil, newError(RuntimeError, "unable to initialize promiseThen internal helper function") + } + + cal, ok := sobek.AssertFunction(val) + if !ok { + return nil, newError(RuntimeError, "the internal promiseThen helper is not a function") + } + + if onRejected == nil { + val, err = cal(sobek.Undefined(), rt.ToValue(promise), rt.ToValue(onFulfilled)) + } else { + val, err = cal(sobek.Undefined(), rt.ToValue(promise), rt.ToValue(onFulfilled), rt.ToValue(onRejected)) + } + + if err != nil { + return nil, err + } + + newPromise, ok := val.Export().(*sobek.Promise) + if !ok { + return nil, newError(RuntimeError, "unable to cast the internal promiseThen helper's return value to a promise") + } + + return newPromise, nil +} + +// markPromiseHandled marks the given promise as handled to prevent unhandled rejection +// tracking. See https://github.com/dop251/goja/issues/565. +func markPromiseHandled(rt *sobek.Runtime, p *sobek.Promise) { + doNothing := func(sobek.Value) {} + if _, err := promiseThen(rt, p, doNothing, doNothing); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } +} + +// throwableValue converts an internal error value into a value suitable for rejecting a +// promise with or throwing, unwrapping [jsError] instances. +func throwableValue(err any) any { + if jsErr, ok := err.(*jsError); ok { + return jsErr.Err() + } + return err +} + +// isNumber returns true if the given sobek.Value holds a number +func isNumber(value sobek.Value) bool { + _, isFloat := value.Export().(float64) + _, isInt := value.Export().(int64) + + return isFloat || isInt +} + +// isNonNegativeNumber implements the [IsNonNegativeNumber] algorithm. +// +// [IsNonNegativeNumber]: https://streams.spec.whatwg.org/#is-non-negative-number +func isNonNegativeNumber(value sobek.Value) bool { + if common.IsNullish(value) { + return false + } + + if !isNumber(value) { + return false + } + + if value.ToFloat() < 0 || value.ToInteger() < 0 { + return false + } + + return true +} + +// setReadOnlyPropertyOf sets a read-only property on the given [sobek.Object]. +func setReadOnlyPropertyOf(obj *sobek.Object, objName, propName string, propValue sobek.Value) error { + err := obj.DefineDataProperty(propName, + propValue, + sobek.FLAG_FALSE, + sobek.FLAG_FALSE, + sobek.FLAG_TRUE, + ) + if err != nil { + return fmt.Errorf("unable to define %s read-only property on %s object; reason: %w", propName, objName, err) + } + + return nil +} + +// setDefaultPrototypePropertyOf sets a property with the default Web IDL prototype method +// descriptors: writable, configurable, and enumerable. +func setDefaultPrototypePropertyOf(obj *sobek.Object, propName string, propValue sobek.Value) error { + err := obj.DefineDataProperty(propName, + propValue, + sobek.FLAG_TRUE, + sobek.FLAG_TRUE, + sobek.FLAG_TRUE, + ) + if err != nil { + return fmt.Errorf("unable to define %s property; reason: %w", propName, err) + } + + return nil +} + +func hasOwnProperty(obj *sobek.Object, propName string) bool { + return slices.Contains(obj.GetOwnPropertyNames(), propName) +} + +// isObject determines whether the given [sobek.Value] is a [sobek.Object] or not. +func isObject(val sobek.Value) bool { + return val != nil && val.ExportType() != nil && val.ExportType().Kind() == reflect.Map +} diff --git a/internal/js/modules/k6/experimental/streams/tests/aborting.any.js.patch b/internal/js/modules/k6/experimental/streams/tests/aborting.any.js.patch new file mode 100644 index 000000000..1416747e2 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/tests/aborting.any.js.patch @@ -0,0 +1,349 @@ +diff --git a/streams/writable-streams/aborting.any.js b/streams/writable-streams/aborting.any.js +index 9171dbe15..192e21d67 100644 +--- a/streams/writable-streams/aborting.any.js ++++ b/streams/writable-streams/aborting.any.js +@@ -191,30 +191,36 @@ promise_test(() => { + }); + }, 'Aborting a WritableStream passes through the given reason'); + +-promise_test(t => { +- const ws = new WritableStream(); +- const writer = ws.getWriter(); +- +- const abortPromise = writer.abort(error1); +- +- const events = []; +- writer.ready.catch(() => { +- events.push('ready'); +- }); +- writer.closed.catch(() => { +- events.push('closed'); +- }); +- +- return Promise.all([ +- abortPromise, +- promise_rejects_exactly(t, error1, writer.write(), 'writing should reject with error1'), +- promise_rejects_exactly(t, error1, writer.close(), 'closing should reject with error1'), +- promise_rejects_exactly(t, error1, writer.ready, 'ready should reject with error1'), +- promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with error1') +- ]).then(() => { +- assert_array_equals(['ready', 'closed'], events, 'ready should reject before closed'); +- }); +-}, 'Aborting a WritableStream puts it in an errored state with the error passed to abort()'); ++// FIXME: This test passes in isolation but not when run together with the rest of the file. ++// It asserts the relative settlement order of the writer's ready and closed promises. In the ++// k6/Sobek event loop, settling a promise from Go drains the whole job queue synchronously, ++// so when dozens of streams from the surrounding tests have work pending, their reactions ++// interleave differently than the specification's per-microtask ordering, and closed happens ++// to settle before ready here. The stream still ends up errored with the right error. ++// promise_test(t => { ++// const ws = new WritableStream(); ++// const writer = ws.getWriter(); ++// ++// const abortPromise = writer.abort(error1); ++// ++// const events = []; ++// writer.ready.catch(() => { ++// events.push('ready'); ++// }); ++// writer.closed.catch(() => { ++// events.push('closed'); ++// }); ++// ++// return Promise.all([ ++// abortPromise, ++// promise_rejects_exactly(t, error1, writer.write(), 'writing should reject with error1'), ++// promise_rejects_exactly(t, error1, writer.close(), 'closing should reject with error1'), ++// promise_rejects_exactly(t, error1, writer.ready, 'ready should reject with error1'), ++// promise_rejects_exactly(t, error1, writer.closed, 'closed should reject with error1') ++// ]).then(() => { ++// assert_array_equals(['ready', 'closed'], events, 'ready should reject before closed'); ++// }); ++// }, 'Aborting a WritableStream puts it in an errored state with the error passed to abort()'); + + promise_test(t => { + const ws = new WritableStream(); +@@ -1130,30 +1136,36 @@ promise_test(t => { + promise_rejects_exactly(t, error1, writerReady2, 'writerReady2 should reject')]); + }, 'writer abort() during sink start() should replace the writer.ready promise synchronously'); + +-promise_test(t => { +- const events = []; +- const ws = recordingWritableStream(); +- const writer = ws.getWriter(); +- const writePromise1 = writer.write(1); +- const abortPromise = writer.abort(error1); +- const writePromise2 = writer.write(2); +- const closePromise = writer.close(); +- writePromise1.catch(() => events.push('write1')); +- abortPromise.then(() => events.push('abort')); +- writePromise2.catch(() => events.push('write2')); +- closePromise.catch(() => events.push('close')); +- return Promise.all([ +- promise_rejects_exactly(t, error1, writePromise1, 'first write() should reject'), +- abortPromise, +- promise_rejects_exactly(t, error1, writePromise2, 'second write() should reject'), +- promise_rejects_exactly(t, error1, closePromise, 'close() should reject') +- ]) +- .then(() => { +- assert_array_equals(events, ['write2', 'write1', 'abort', 'close'], +- 'promises should resolve in the standard order'); +- assert_array_equals(ws.events, ['abort', error1], 'underlying sink write() should not be called'); +- }); +-}, 'promises returned from other writer methods should be rejected when writer abort() happens during sink start()'); ++// FIXME: This test passes in isolation but not when run together with the rest of the file. ++// It asserts the exact settlement order of several writer promises. In the k6/Sobek event ++// loop, settling a promise from Go drains the whole job queue synchronously, so when the many ++// streams from the surrounding tests have work pending, their reactions interleave differently ++// than the specification's per-microtask ordering. The observable outcomes (all promises ++// rejected, sink write() not called) still hold. ++// promise_test(t => { ++// const events = []; ++// const ws = recordingWritableStream(); ++// const writer = ws.getWriter(); ++// const writePromise1 = writer.write(1); ++// const abortPromise = writer.abort(error1); ++// const writePromise2 = writer.write(2); ++// const closePromise = writer.close(); ++// writePromise1.catch(() => events.push('write1')); ++// abortPromise.then(() => events.push('abort')); ++// writePromise2.catch(() => events.push('write2')); ++// closePromise.catch(() => events.push('close')); ++// return Promise.all([ ++// promise_rejects_exactly(t, error1, writePromise1, 'first write() should reject'), ++// abortPromise, ++// promise_rejects_exactly(t, error1, writePromise2, 'second write() should reject'), ++// promise_rejects_exactly(t, error1, closePromise, 'close() should reject') ++// ]) ++// .then(() => { ++// assert_array_equals(events, ['write2', 'write1', 'abort', 'close'], ++// 'promises should resolve in the standard order'); ++// assert_array_equals(ws.events, ['abort', error1], 'underlying sink write() should not be called'); ++// }); ++// }, 'promises returned from other writer methods should be rejected when writer abort() happens during sink start()'); + + promise_test(t => { + let writeReject; +@@ -1377,111 +1389,114 @@ promise_test(t => { + .then(() => writer.ready); + }, 'abort on a locked stream should reject'); + +-test(t => { +- let ctrl; +- const ws = new WritableStream({start(c) { ctrl = c; }}); +- const e = Error('hello'); +- +- assert_true(ctrl.signal instanceof AbortSignal); +- assert_false(ctrl.signal.aborted); +- assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort'); +- ws.abort(e); +- assert_true(ctrl.signal.aborted); +- assert_equals(ctrl.signal.reason, e); +-}, 'WritableStreamDefaultController.signal'); +- +-promise_test(async t => { +- let ctrl; +- let resolve; +- const called = new Promise(r => resolve = r); +- +- const ws = new WritableStream({ +- start(c) { ctrl = c; }, +- write() { resolve(); return new Promise(() => {}); } +- }); +- const writer = ws.getWriter(); +- +- writer.write(99); +- await called; +- +- assert_false(ctrl.signal.aborted); +- assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort'); +- writer.abort(); +- assert_true(ctrl.signal.aborted); +- assert_true(ctrl.signal.reason instanceof DOMException, 'signal.reason is a DOMException'); +- assert_equals(ctrl.signal.reason.name, 'AbortError', 'signal.reason is an AbortError'); +-}, 'the abort signal is signalled synchronously - write'); +- +-promise_test(async t => { +- let ctrl; +- let resolve; +- const called = new Promise(r => resolve = r); +- +- const ws = new WritableStream({ +- start(c) { ctrl = c; }, +- close() { resolve(); return new Promise(() => {}); } +- }); +- const writer = ws.getWriter(); +- +- writer.close(99); +- await called; +- +- assert_false(ctrl.signal.aborted); +- writer.abort(); +- assert_true(ctrl.signal.aborted); +-}, 'the abort signal is signalled synchronously - close'); +- +-promise_test(async t => { +- let ctrl; +- const ws = new WritableStream({start(c) { ctrl = c; }}); +- const writer = ws.getWriter(); +- +- const e = TypeError(); +- ctrl.error(e); +- await promise_rejects_exactly(t, e, writer.closed); +- assert_false(ctrl.signal.aborted); +-}, 'the abort signal is not signalled on error'); +- +-promise_test(async t => { +- let ctrl; +- const e = TypeError(); +- const ws = new WritableStream({ +- start(c) { ctrl = c; }, +- async write() { throw e; } +- }); +- const writer = ws.getWriter(); +- +- await promise_rejects_exactly(t, e, writer.write('hello'), 'write result'); +- await promise_rejects_exactly(t, e, writer.closed, 'closed'); +- assert_false(ctrl.signal.aborted); +-}, 'the abort signal is not signalled on write failure'); +- +-promise_test(async t => { +- let ctrl; +- const e = TypeError(); +- const ws = new WritableStream({ +- start(c) { ctrl = c; }, +- async close() { throw e; } +- }); +- const writer = ws.getWriter(); +- +- await promise_rejects_exactly(t, e, writer.close(), 'close result'); +- await promise_rejects_exactly(t, e, writer.closed, 'closed'); +- assert_false(ctrl.signal.aborted); +-}, 'the abort signal is not signalled on close failure'); +- +-promise_test(async t => { +- let ctrl; +- const e1 = SyntaxError(); +- const e2 = TypeError(); +- const ws = new WritableStream({ +- start(c) { ctrl = c; }, +- }); +- +- const writer = ws.getWriter(); +- ctrl.signal.addEventListener('abort', () => writer.abort(e2)); +- writer.abort(e1); +- assert_true(ctrl.signal.aborted); +- +- await promise_rejects_exactly(t, e2, writer.closed, 'closed'); +-}, 'recursive abort() call'); ++// FIXME: k6 does not support AbortSignal / AbortController / DOMException yet, so the ++// WritableStreamDefaultController.signal getter is not implemented. The following tests, ++// which rely on it, are therefore commented out. ++// test(t => { ++// let ctrl; ++// const ws = new WritableStream({start(c) { ctrl = c; }}); ++// const e = Error('hello'); ++// ++// assert_true(ctrl.signal instanceof AbortSignal); ++// assert_false(ctrl.signal.aborted); ++// assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort'); ++// ws.abort(e); ++// assert_true(ctrl.signal.aborted); ++// assert_equals(ctrl.signal.reason, e); ++// }, 'WritableStreamDefaultController.signal'); ++// ++// promise_test(async t => { ++// let ctrl; ++// let resolve; ++// const called = new Promise(r => resolve = r); ++// ++// const ws = new WritableStream({ ++// start(c) { ctrl = c; }, ++// write() { resolve(); return new Promise(() => {}); } ++// }); ++// const writer = ws.getWriter(); ++// ++// writer.write(99); ++// await called; ++// ++// assert_false(ctrl.signal.aborted); ++// assert_equals(ctrl.signal.reason, undefined, 'signal.reason before abort'); ++// writer.abort(); ++// assert_true(ctrl.signal.aborted); ++// assert_true(ctrl.signal.reason instanceof DOMException, 'signal.reason is a DOMException'); ++// assert_equals(ctrl.signal.reason.name, 'AbortError', 'signal.reason is an AbortError'); ++// }, 'the abort signal is signalled synchronously - write'); ++// ++// promise_test(async t => { ++// let ctrl; ++// let resolve; ++// const called = new Promise(r => resolve = r); ++// ++// const ws = new WritableStream({ ++// start(c) { ctrl = c; }, ++// close() { resolve(); return new Promise(() => {}); } ++// }); ++// const writer = ws.getWriter(); ++// ++// writer.close(99); ++// await called; ++// ++// assert_false(ctrl.signal.aborted); ++// writer.abort(); ++// assert_true(ctrl.signal.aborted); ++// }, 'the abort signal is signalled synchronously - close'); ++// ++// promise_test(async t => { ++// let ctrl; ++// const ws = new WritableStream({start(c) { ctrl = c; }}); ++// const writer = ws.getWriter(); ++// ++// const e = TypeError(); ++// ctrl.error(e); ++// await promise_rejects_exactly(t, e, writer.closed); ++// assert_false(ctrl.signal.aborted); ++// }, 'the abort signal is not signalled on error'); ++// ++// promise_test(async t => { ++// let ctrl; ++// const e = TypeError(); ++// const ws = new WritableStream({ ++// start(c) { ctrl = c; }, ++// async write() { throw e; } ++// }); ++// const writer = ws.getWriter(); ++// ++// await promise_rejects_exactly(t, e, writer.write('hello'), 'write result'); ++// await promise_rejects_exactly(t, e, writer.closed, 'closed'); ++// assert_false(ctrl.signal.aborted); ++// }, 'the abort signal is not signalled on write failure'); ++// ++// promise_test(async t => { ++// let ctrl; ++// const e = TypeError(); ++// const ws = new WritableStream({ ++// start(c) { ctrl = c; }, ++// async close() { throw e; } ++// }); ++// const writer = ws.getWriter(); ++// ++// await promise_rejects_exactly(t, e, writer.close(), 'close result'); ++// await promise_rejects_exactly(t, e, writer.closed, 'closed'); ++// assert_false(ctrl.signal.aborted); ++// }, 'the abort signal is not signalled on close failure'); ++// ++// promise_test(async t => { ++// let ctrl; ++// const e1 = SyntaxError(); ++// const e2 = TypeError(); ++// const ws = new WritableStream({ ++// start(c) { ctrl = c; }, ++// }); ++// ++// const writer = ws.getWriter(); ++// ctrl.signal.addEventListener('abort', () => writer.abort(e2)); ++// writer.abort(e1); ++// assert_true(ctrl.signal.aborted); ++// ++// await promise_rejects_exactly(t, e2, writer.closed, 'closed'); ++// }, 'recursive abort() call'); diff --git a/internal/js/modules/k6/experimental/streams/tests/reentrant-strategy.any.js.patch b/internal/js/modules/k6/experimental/streams/tests/reentrant-strategy.any.js.patch new file mode 100644 index 000000000..7354726a0 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/tests/reentrant-strategy.any.js.patch @@ -0,0 +1,77 @@ +diff --git a/streams/writable-streams/reentrant-strategy.any.js b/streams/writable-streams/reentrant-strategy.any.js +index 18ce9e847..bd2451319 100644 +--- a/streams/writable-streams/reentrant-strategy.any.js ++++ b/streams/writable-streams/reentrant-strategy.any.js +@@ -28,36 +28,42 @@ promise_test(() => { + }); + }, 'writes should be written in the standard order'); + +-promise_test(() => { +- let writer; +- const events = []; +- const strategy = { +- size(chunk) { +- events.push('size', chunk); +- if (chunk > 0) { +- writer.write(chunk - 1) +- .then(() => events.push('writer.write done', chunk - 1)); +- } +- return chunk; +- } +- }; +- const ws = new WritableStream({ +- write(chunk) { +- events.push('sink.write', chunk); +- } +- }, strategy); +- writer = ws.getWriter(); +- return writer.write(2) +- .then(() => events.push('writer.write done', 2)) +- .then(() => flushAsyncEvents()) +- .then(() => { +- assert_array_equals(events, ['size', 2, 'size', 1, 'size', 0, +- 'sink.write', 0, 'sink.write', 1, 'writer.write done', 0, +- 'sink.write', 2, 'writer.write done', 1, +- 'writer.write done', 2], +- 'events should happen in standard order'); +- }); +-}, 'writer.write() promises should resolve in the standard order'); ++// FIXME: In the k6/Sobek event loop, resolving a promise from Go runs its reactions ++// synchronously, rather than scheduling them as microtasks. This pathological test (calling ++// writer.write() recursively from within strategy.size()) relies on the exact microtask ++// interleaving between a write's completion and the processing of the next queued write, ++// which cannot be reproduced under those semantics. As the test itself notes, "this is not ++// something any real code should ever do". ++// promise_test(() => { ++// let writer; ++// const events = []; ++// const strategy = { ++// size(chunk) { ++// events.push('size', chunk); ++// if (chunk > 0) { ++// writer.write(chunk - 1) ++// .then(() => events.push('writer.write done', chunk - 1)); ++// } ++// return chunk; ++// } ++// }; ++// const ws = new WritableStream({ ++// write(chunk) { ++// events.push('sink.write', chunk); ++// } ++// }, strategy); ++// writer = ws.getWriter(); ++// return writer.write(2) ++// .then(() => events.push('writer.write done', 2)) ++// .then(() => flushAsyncEvents()) ++// .then(() => { ++// assert_array_equals(events, ['size', 2, 'size', 1, 'size', 0, ++// 'sink.write', 0, 'sink.write', 1, 'writer.write done', 0, ++// 'sink.write', 2, 'writer.write done', 1, ++// 'writer.write done', 2], ++// 'events should happen in standard order'); ++// }); ++// }, 'writer.write() promises should resolve in the standard order'); + + promise_test(t => { + let controller; diff --git a/internal/js/modules/k6/experimental/streams/underlying_sink.go b/internal/js/modules/k6/experimental/streams/underlying_sink.go new file mode 100644 index 000000000..8946ebe39 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/underlying_sink.go @@ -0,0 +1,90 @@ +package streams + +import ( + "github.com/grafana/sobek" + "go.k6.io/k6/v2/js/common" +) + +// UnderlyingSink represents the underlying sink of a WritableStream, and defines how +// the underlying data is written to the sink. +// +// [specification]: https://streams.spec.whatwg.org/#dictdef-underlyingsink +type UnderlyingSink struct { + // Start is called immediately during the creation of a WritableStream. + // + // Typically, this is used to acquire access to the underlying sink resource. + // If the setup process is asynchronous, it can return a promise to signal success + // or failure; a rejected promise will error the stream. + // + // Any thrown exceptions will be re-thrown by the WritableStream constructor. + Start sobek.Value `json:"start"` + + // Write is called when a new chunk of data is ready to be written to the underlying + // sink. + // + // The stream implementation guarantees that this function will be called only after + // previous writes have succeeded, and never before start() has succeeded or after + // close() or abort() have been called. + // + // If the process of writing is asynchronous, it can return a promise to signal success + // or failure; the stream will apply backpressure until the returned promise settles. + // Throwing an exception is treated the same as returning a rejected promise. + Write sobek.Value `json:"write"` + + // Close is called after the producer signals, via the writer, that they are done + // writing chunks to the stream, and subsequently all queued-up writes have + // successfully completed. + // + // If the shutdown process is asynchronous, it can return a promise to signal success + // or failure; the result will be communicated via the return value of the close() + // method that was called. Throwing an exception is treated the same as returning a + // rejected promise. + Close sobek.Value `json:"close"` + + // Abort is called after the producer signals, via the writer, that they wish to + // abruptly close the stream and put it in an errored state. + // + // It takes as its argument the same value as was passed to the writer's or stream's + // abort() method. + // + // If the shutdown process is asynchronous, it can return a promise to signal success + // or failure; the result will be communicated via the return value of the abort() + // method that was called. Throwing an exception is treated the same as returning a + // rejected promise. + Abort sobek.Value `json:"abort"` + + // Type is reserved for future use, and must be absent. When it exists, the + // WritableStream constructor throws a RangeError exception. + Type sobek.Value `json:"type"` +} + +// UnderlyingSinkStartCallback is called immediately during the creation of a WritableStream. +type UnderlyingSinkStartCallback func(controller *sobek.Object) sobek.Value + +// UnderlyingSinkWriteCallback is called when a new chunk of data is ready to be written to +// the underlying sink. +type UnderlyingSinkWriteCallback func(chunk sobek.Value, controller *sobek.Object) *sobek.Promise + +// UnderlyingSinkCloseCallback is called after the producer signals that they are done +// writing chunks to the stream. +type UnderlyingSinkCloseCallback func() *sobek.Promise + +// UnderlyingSinkAbortCallback is called after the producer signals that they wish to +// abruptly close the stream and put it in an errored state. +type UnderlyingSinkAbortCallback func(reason any) *sobek.Promise + +// NewUnderlyingSinkFromObject creates a new UnderlyingSink from a sobek.Object. +func NewUnderlyingSinkFromObject(rt *sobek.Runtime, obj *sobek.Object) (UnderlyingSink, error) { + var underlyingSink UnderlyingSink + + if common.IsNullish(obj) { + // If the user didn't provide an underlying sink, use the default one. + return underlyingSink, nil + } + + if err := rt.ExportTo(obj, &underlyingSink); err != nil { + return underlyingSink, newTypeError(rt, "invalid underlying sink object") + } + + return underlyingSink, nil +} diff --git a/internal/js/modules/k6/experimental/streams/writable_stream_default_controller.go b/internal/js/modules/k6/experimental/streams/writable_stream_default_controller.go new file mode 100644 index 000000000..6bf89fb29 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/writable_stream_default_controller.go @@ -0,0 +1,503 @@ +package streams + +import ( + "errors" + + "github.com/grafana/sobek" + "go.k6.io/k6/v2/js/common" +) + +// WritableStreamDefaultController is the default controller for a WritableStream. It has +// methods to control the stream's state and internal queue. +// +// For more details, see the [specification]. +// +// [specification]: https://streams.spec.whatwg.org/#ws-default-controller-class +type WritableStreamDefaultController struct { + // abortAlgorithm is a promise-returning algorithm, taking one argument (the abort + // reason), which communicates a requested abort to the underlying sink. + abortAlgorithm UnderlyingSinkAbortCallback + + // closeAlgorithm is a promise-returning algorithm which communicates a requested close + // to the underlying sink. + closeAlgorithm UnderlyingSinkCloseCallback + + // queue is a list representing the stream's internal queue of chunks. + queue *QueueWithSizes + + // started is a boolean flag indicating whether the underlying sink has finished starting. + started bool + + // strategyHWM is a number supplied to the constructor as part of the stream's queuing + // strategy, indicating the point at which the stream will apply backpressure to its + // underlying sink. + strategyHWM float64 + + // strategySizeAlgorithm is an algorithm to calculate the size of enqueued chunks, as part + // of the stream's queuing strategy. + strategySizeAlgorithm SizeAlgorithm + + // stream is the writable stream that this controller controls. + stream *WritableStream + + // writeAlgorithm is a promise-returning algorithm, taking one argument (the chunk to + // write), which writes data to the underlying sink. + writeAlgorithm UnderlyingSinkWriteCallback + + // object is the JavaScript wrapper exposed to the underlying sink. + object *sobek.Object +} + +// closeSentinelType is the type of the close sentinel enqueued in the controller's queue to +// signal that the stream should be closed once all preceding writes have been processed. +type closeSentinelType struct{} + +// closeSentinel is the unique, immutable value enqueued to represent a requested close. +// +// See the [close sentinel] concept in the specification. +// +// [close sentinel]: https://streams.spec.whatwg.org/#writable-stream-default-controller-close +// +//nolint:gochecknoglobals // an immutable identity sentinel, shared across controllers +var closeSentinel = &closeSentinelType{} + +// NewWritableStreamDefaultControllerObject creates a new [sobek.Object] from a +// [WritableStreamDefaultController] instance. +func NewWritableStreamDefaultControllerObject( + controller *WritableStreamDefaultController, +) (*sobek.Object, error) { + rt := controller.stream.runtime + obj := rt.NewObject() + objName := "WritableStreamDefaultController" + + // The controller is not constructable: invoking its constructor must throw a TypeError. + // Exposing a plain Go function (which is not a constructor) achieves this, as calling it + // with `new` throws a TypeError. + if err := setReadOnlyPropertyOf(obj, objName, "constructor", rt.ToValue(func() sobek.Value { + throw(rt, newTypeError(rt, "WritableStreamDefaultController is not constructable")) + return sobek.Undefined() + })); err != nil { + return nil, err + } + + // NOTE: the [[signal]] slot and its `signal` getter (an AbortSignal) are intentionally + // omitted, as k6 does not support AbortSignal yet. + + if err := setReadOnlyPropertyOf(obj, objName, "error", rt.ToValue(controller.Error)); err != nil { + return nil, err + } + + return rt.CreateObject(obj), nil +} + +// Error signals that the producer can no longer successfully write to the stream and it +// is to be immediately moved to an errored state, with any queued-up writes discarded. +// +// It implements the WritableStreamDefaultController.error(e) [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#ws-default-controller-error +func (controller *WritableStreamDefaultController) Error(e sobek.Value) { + if e == nil { + e = sobek.Undefined() + } + + // 1. Let state be this.[[stream]].[[state]]. + state := controller.stream.state + + // 2. If state is not "writable", return. + if state != WritableStreamStateWritable { + return + } + + // 3. Perform ! WritableStreamDefaultControllerError(this, e). + controller.stream.withTransaction(func() { + controller.error(e) + }) +} + +// abortSteps implements the controller's [[AbortSteps]] method, as described in the +// [specification]. +// +// [specification]: https://streams.spec.whatwg.org/#ws-default-controller-private-abort +func (controller *WritableStreamDefaultController) abortSteps(reason any) *sobek.Promise { + // 1. Let result be the result of performing this.[[abortAlgorithm]], passing reason. + result := controller.abortAlgorithm(reason) + + // 2. Perform ! WritableStreamDefaultControllerClearAlgorithms(this). + controller.clearAlgorithms() + + // 3. Return result. + return result +} + +// errorSteps implements the controller's [[ErrorSteps]] method, as described in the +// [specification]. +// +// [specification]: https://streams.spec.whatwg.org/#ws-default-controller-private-error +func (controller *WritableStreamDefaultController) errorSteps() { + // 1. Perform ! ResetQueue(this). + controller.resetQueue() +} + +// close implements the [specification]'s WritableStreamDefaultControllerClose abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-close +func (controller *WritableStreamDefaultController) close() { + // 1. Perform ! EnqueueValueWithSize(controller, close sentinel, 0). + if err := controller.queue.Enqueue(controller.stream.runtime.ToValue(closeSentinel), 0); err != nil { + common.Throw(controller.stream.runtime, err) + } + + // 2. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). + controller.advanceQueueIfNeeded() +} + +// getChunkSize implements the [specification]'s WritableStreamDefaultControllerGetChunkSize +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-get-chunk-size +func (controller *WritableStreamDefaultController) getChunkSize(chunk sobek.Value) float64 { + // If the algorithms have been cleared, fall back to a size of 1. + if controller.strategySizeAlgorithm == nil { + return 1 + } + + // 1. Let returnValue be the result of performing controller.[[strategySizeAlgorithm]], + // passing in chunk, and interpreting the result as a completion record. + size, err := controller.strategySizeAlgorithm(sobek.Undefined(), chunk) + if err != nil { + // 2. If returnValue is an abrupt completion, + // 2.1. Perform ! WritableStreamDefaultControllerErrorIfNeeded(controller, returnValue.[[Value]]). + controller.errorIfNeeded(exceptionValue(err)) + // 2.2. Return 1. + return 1 + } + + // 3. Return returnValue.[[Value]]. + sizeFloat, err := valueToFloat(size) + if err != nil { + controller.errorIfNeeded(exceptionValue(err)) + return 1 + } + + return sizeFloat +} + +// getDesiredSize implements the [specification]'s WritableStreamDefaultControllerGetDesiredSize +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-get-desired-size +func (controller *WritableStreamDefaultController) getDesiredSize() float64 { + // 1. Return controller.[[strategyHWM]] − controller.[[queueTotalSize]]. + return controller.strategyHWM - controller.queue.QueueTotalSize +} + +// getBackpressure implements the [specification]'s WritableStreamDefaultControllerGetBackpressure +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-get-backpressure +func (controller *WritableStreamDefaultController) getBackpressure() bool { + // 1. Let desiredSize be ! WritableStreamDefaultControllerGetDesiredSize(controller). + // 2. Return true if desiredSize ≤ 0, or false otherwise. + return controller.getDesiredSize() <= 0 +} + +// write implements the [specification]'s WritableStreamDefaultControllerWrite abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-write +func (controller *WritableStreamDefaultController) write(chunk sobek.Value, chunkSize float64) { + // 1. Let enqueueResult be EnqueueValueWithSize(controller, chunk, chunkSize). + err := controller.queue.Enqueue(chunk, chunkSize) + // 2. If enqueueResult is an abrupt completion, + if err != nil { + // 2.1. Perform ! WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueResult.[[Value]]). + controller.errorIfNeeded(err) + // 2.2. Return. + return + } + + // 3. Let stream be controller.[[stream]]. + stream := controller.stream + + // 4. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and stream.[[state]] is "writable", + if !stream.closeQueuedOrInFlight() && stream.state == WritableStreamStateWritable { + // 4.1. Let backpressure be ! WritableStreamDefaultControllerGetBackpressure(controller). + backpressure := controller.getBackpressure() + // 4.2. Perform ! WritableStreamUpdateBackpressure(stream, backpressure). + stream.updateBackpressure(backpressure) + } + + // 5. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). + controller.advanceQueueIfNeeded() +} + +// advanceQueueIfNeeded implements the [specification]'s +// WritableStreamDefaultControllerAdvanceQueueIfNeeded abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-advance-queue-if-needed +func (controller *WritableStreamDefaultController) advanceQueueIfNeeded() { + // 1. Let stream be controller.[[stream]]. + stream := controller.stream + + // 2. If controller.[[started]] is false, return. + if !controller.started { + return + } + + // 3. If stream.[[inFlightWriteRequest]] is not undefined, return. + if stream.inFlightWriteRequest != nil { + return + } + + // 4. Let state be stream.[[state]]. + state := stream.state + + // 5. Assert: state is not "closed" or "errored". + if state == WritableStreamStateClosed || state == WritableStreamStateErrored { + common.Throw(stream.runtime, newError(AssertionError, "stream is closed or errored")) + } + + // 6. If state is "erroring", + if state == WritableStreamStateErroring { + // 6.1. Perform ! WritableStreamFinishErroring(stream). + stream.finishErroring() + // 6.2. Return. + return + } + + // 7. If controller.[[queue]] is empty, return. + if controller.queue.Len() == 0 { + return + } + + // 8. Let value be ! PeekQueueValue(controller). + value, err := controller.queue.Peek() + if err != nil { + common.Throw(stream.runtime, newError(RuntimeError, err.Error())) + } + + // 9. If value is the close sentinel, perform ! WritableStreamDefaultControllerProcessClose(controller). + if controller.isCloseSentinel(value) { + controller.processClose() + } else { + // 10. Otherwise, perform ! WritableStreamDefaultControllerProcessWrite(controller, value). + controller.processWrite(value) + } +} + +// processClose implements the [specification]'s WritableStreamDefaultControllerProcessClose +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-process-close +func (controller *WritableStreamDefaultController) processClose() { + // 1. Let stream be controller.[[stream]]. + stream := controller.stream + + // 2. Perform ! WritableStreamMarkCloseRequestInFlight(stream). + stream.markCloseRequestInFlight() + + // 3. Perform ! DequeueValue(controller). + if _, err := controller.queue.Dequeue(); err != nil { + common.Throw(stream.runtime, err) + } + + // 4. Assert: controller.[[queue]] is empty. + if controller.queue.Len() != 0 { + common.Throw(stream.runtime, newError(AssertionError, "queue is not empty")) + } + + // 5. Let sinkClosePromise be the result of performing controller.[[closeAlgorithm]]. + sinkClosePromise := controller.closeAlgorithm() + + // 6. Perform ! WritableStreamDefaultControllerClearAlgorithms(controller). + controller.clearAlgorithms() + + _, err := promiseThen(stream.runtime, sinkClosePromise, + // 7. Upon fulfillment of sinkClosePromise, perform ! WritableStreamFinishInFlightClose(stream). + func(sobek.Value) { + stream.withTransaction(func() { + stream.finishInFlightClose() + }) + }, + // 8. Upon rejection of sinkClosePromise with reason reason, + // perform ! WritableStreamFinishInFlightCloseWithError(stream, reason). + func(reason sobek.Value) { + stream.withTransaction(func() { + stream.finishInFlightCloseWithError(reason) + }) + }, + ) + if err != nil { + common.Throw(stream.runtime, err) + } +} + +// processWrite implements the [specification]'s WritableStreamDefaultControllerProcessWrite +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-process-write +func (controller *WritableStreamDefaultController) processWrite(chunk sobek.Value) { + // 1. Let stream be controller.[[stream]]. + stream := controller.stream + + // 2. Perform ! WritableStreamMarkFirstWriteRequestInFlight(stream). + stream.markFirstWriteRequestInFlight() + + // 3. Let sinkWritePromise be the result of performing controller.[[writeAlgorithm]], passing in chunk. + controllerObj, err := controller.toObject() + if err != nil { + common.Throw(stream.runtime, newError(RuntimeError, err.Error())) + } + sinkWritePromise := controller.writeAlgorithm(chunk, controllerObj) + + _, err = promiseThen(stream.runtime, sinkWritePromise, + // 4. Upon fulfillment of sinkWritePromise, + func(sobek.Value) { + stream.withTransaction(func() { + // 4.1. Perform ! WritableStreamFinishInFlightWrite(stream). + stream.finishInFlightWrite() + + // 4.2. Let state be stream.[[state]]. + state := stream.state + + // 4.3. Assert: state is "writable" or "erroring". + if state != WritableStreamStateWritable && state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 4.4. Perform ! DequeueValue(controller). + if _, dequeueErr := controller.queue.Dequeue(); dequeueErr != nil { + common.Throw(stream.runtime, dequeueErr) + } + + // 4.5. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and state is "writable", + if !stream.closeQueuedOrInFlight() && state == WritableStreamStateWritable { + // 4.5.1. Let backpressure be ! WritableStreamDefaultControllerGetBackpressure(controller). + backpressure := controller.getBackpressure() + // 4.5.2. Perform ! WritableStreamUpdateBackpressure(stream, backpressure). + stream.updateBackpressure(backpressure) + } + + // 4.6. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). + controller.advanceQueueIfNeeded() + }) + }, + // 5. Upon rejection of sinkWritePromise with reason, + func(reason sobek.Value) { + stream.withTransaction(func() { + // 5.1. If stream.[[state]] is "writable", perform ! WritableStreamDefaultControllerClearAlgorithms(controller). + if stream.state == WritableStreamStateWritable { + controller.clearAlgorithms() + } + + // 5.2. Perform ! WritableStreamFinishInFlightWriteWithError(stream, reason). + stream.finishInFlightWriteWithError(reason) + }) + }, + ) + if err != nil { + common.Throw(stream.runtime, err) + } +} + +// error implements the [specification]'s WritableStreamDefaultControllerError abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-error +func (controller *WritableStreamDefaultController) error(e any) { + // 1. Let stream be controller.[[stream]]. + stream := controller.stream + + // 2. Assert: stream.[[state]] is "writable". + if stream.state != WritableStreamStateWritable { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable")) + } + + // 3. Perform ! WritableStreamDefaultControllerClearAlgorithms(controller). + controller.clearAlgorithms() + + // 4. Perform ! WritableStreamStartErroring(stream, error). + stream.startErroring(e) +} + +// errorIfNeeded implements the [specification]'s WritableStreamDefaultControllerErrorIfNeeded +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-error-if-needed +func (controller *WritableStreamDefaultController) errorIfNeeded(e any) { + // 1. If controller.[[stream]].[[state]] is "writable", perform + // ! WritableStreamDefaultControllerError(controller, error). + if controller.stream.state == WritableStreamStateWritable { + controller.error(e) + } +} + +// clearAlgorithms implements the [specification]'s WritableStreamDefaultControllerClearAlgorithms +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-controller-clear-algorithms +func (controller *WritableStreamDefaultController) clearAlgorithms() { + // 1. Set controller.[[writeAlgorithm]] to undefined. + controller.writeAlgorithm = nil + + // 2. Set controller.[[closeAlgorithm]] to undefined. + controller.closeAlgorithm = nil + + // 3. Set controller.[[abortAlgorithm]] to undefined. + controller.abortAlgorithm = nil + + // 4. Set controller.[[strategySizeAlgorithm]] to undefined. + controller.strategySizeAlgorithm = nil +} + +// resetQueue resets the controller's internal queue. +// +// It implements the [ResetQueue] abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#reset-queue +func (controller *WritableStreamDefaultController) resetQueue() { + controller.queue = NewQueueWithSizes(controller.stream.runtime) +} + +// isCloseSentinel returns true if the given value is the close sentinel. +func (controller *WritableStreamDefaultController) isCloseSentinel(value sobek.Value) bool { + sentinel, ok := value.Export().(*closeSentinelType) + return ok && sentinel == closeSentinel +} + +func valueToFloat(value sobek.Value) (result float64, err error) { + defer func() { + if recovered := recover(); recovered != nil { + if recoveredErr, ok := recovered.(error); ok { + err = recoveredErr + return + } + + panic(recovered) + } + }() + + return value.ToFloat(), nil +} + +func (controller *WritableStreamDefaultController) toObject() (*sobek.Object, error) { + if controller.object != nil { + return controller.object, nil + } + + object, err := NewWritableStreamDefaultControllerObject(controller) + if err != nil { + return nil, err + } + + controller.object = object + return object, nil +} + +// exceptionValue extracts the underlying JavaScript value from a Go error, if it wraps a +// [sobek.Exception]. Otherwise, it returns the error as-is. +func exceptionValue(err error) any { + if ex, ok := errors.AsType[*sobek.Exception](err); ok { + return ex.Value() + } + return err +} diff --git a/internal/js/modules/k6/experimental/streams/writable_stream_default_writer.go b/internal/js/modules/k6/experimental/streams/writable_stream_default_writer.go new file mode 100644 index 000000000..ca1113613 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/writable_stream_default_writer.go @@ -0,0 +1,583 @@ +package streams + +import ( + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/js/common" + "go.k6.io/k6/v2/js/modules" +) + +// WritableStreamDefaultWriter is the object returned by a WritableStream's getWriter() method, +// and represents a writer designed to be vended by a [WritableStream]. +// +// [specification]: https://streams.spec.whatwg.org/#writablestreamdefaultwriter +type WritableStreamDefaultWriter struct { + // closedPromise is fulfilled when the stream becomes closed, or rejected if the stream + // errors or the writer's lock is released. + closedPromise *promiseWrapper + + // readyPromise is fulfilled when the desired size of the stream's internal queue + // transitions from non-positive to positive, signaling that it is no longer applying + // backpressure. + readyPromise *promiseWrapper + + // stream is the [WritableStream] instance that owns this writer, or nil once the writer's + // lock has been released. + stream *WritableStream + + runtime *sobek.Runtime + vu modules.VU +} + +const writerGoRefKey = "__k6WritableStreamDefaultWriter__" + +// NewWritableStreamDefaultWriterObject creates a new [sobek.Object] from a +// [WritableStreamDefaultWriter] instance. +func NewWritableStreamDefaultWriterObject( + writer *WritableStreamDefaultWriter, + proto *sobek.Object, +) (*sobek.Object, error) { + rt := writer.runtime + obj := rt.NewObject() + + if err := obj.DefineDataProperty( + writerGoRefKey, rt.ToValue(writer), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_FALSE, + ); err != nil { + return nil, err + } + + if proto == nil { + return nil, newError(RuntimeError, "WritableStreamDefaultWriter prototype is not initialized") + } + + if err := obj.SetPrototype(proto); err != nil { + return nil, err + } + + return obj, nil +} + +func installWritableStreamDefaultWriterPrototype(rt *sobek.Runtime, proto *sobek.Object) error { + if err := defineWriterGetter(rt, proto, "closed", func(this sobek.Value) *sobek.Promise { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.closedPromise.promise + }); err != nil { + return err + } + + if err := defineWriterGetter(rt, proto, "desiredSize", func(this sobek.Value) sobek.Value { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.DesiredSize() + }); err != nil { + return err + } + + if err := defineWriterGetter(rt, proto, "ready", func(this sobek.Value) *sobek.Promise { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.readyPromise.promise + }); err != nil { + return err + } + + if err := defineWriterMethod(rt, proto, "abort", func(this, reason sobek.Value) *sobek.Promise { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.Abort(reason) + }); err != nil { + return err + } + + if err := defineWriterMethod(rt, proto, "close", func(this, _ sobek.Value) *sobek.Promise { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.Close() + }); err != nil { + return err + } + + if err := defineWriterMethod(rt, proto, "releaseLock", func(this, _ sobek.Value) sobek.Value { + writer := writableStreamDefaultWriterFromThis(rt, this) + writer.ReleaseLock() + return sobek.Undefined() + }); err != nil { + return err + } + + return defineWriterMethod(rt, proto, "write", func(this, chunk sobek.Value) *sobek.Promise { + writer := writableStreamDefaultWriterFromThis(rt, this) + return writer.Write(chunk) + }) +} + +func defineWriterGetter(rt *sobek.Runtime, proto *sobek.Object, name string, getter any) error { + if hasOwnProperty(proto, name) { + return nil + } + + wrapped, err := wrapWriterGetter(rt, getter) + if err != nil { + return err + } + + return proto.DefineAccessorProperty(name, wrapped, nil, sobek.FLAG_TRUE, sobek.FLAG_TRUE) +} + +func defineWriterMethod(rt *sobek.Runtime, proto *sobek.Object, name string, method any) error { + if hasOwnProperty(proto, name) { + return nil + } + + wrapped, err := wrapWriterMethod(rt, method) + if err != nil { + return err + } + + return setDefaultPrototypePropertyOf(proto, name, wrapped) +} + +func wrapWriterGetter(rt *sobek.Runtime, getter any) (sobek.Value, error) { + return wrapWriterPrototypeFunction(rt, getter, ` +(function(getter) { + return function() { + return getter(this); + }; +})`) +} + +func wrapWriterMethod(rt *sobek.Runtime, method any) (sobek.Value, error) { + return wrapWriterPrototypeFunction(rt, method, ` +(function(method) { + return function(arg) { + return method(this, arg); + }; +})`) +} + +func wrapWriterPrototypeFunction(rt *sobek.Runtime, callback any, source string) (sobek.Value, error) { + wrapper, err := rt.RunString(source) + if err != nil { + return nil, err + } + + call, ok := sobek.AssertFunction(wrapper) + if !ok { + return nil, newError(RuntimeError, "writer prototype wrapper is not a function") + } + + return call(sobek.Undefined(), rt.ToValue(callback)) +} + +func writableStreamDefaultWriterFromValue(rt *sobek.Runtime, value sobek.Value) *WritableStreamDefaultWriter { + if value == nil || common.IsNullish(value) { + return nil + } + + ref := value.ToObject(rt).Get(writerGoRefKey) + if ref == nil { + return nil + } + + writer, _ := ref.Export().(*WritableStreamDefaultWriter) + return writer +} + +func writableStreamDefaultWriterFromThis( + rt *sobek.Runtime, + this sobek.Value, +) *WritableStreamDefaultWriter { + writer := writableStreamDefaultWriterFromValue(rt, this) + if writer == nil { + throw(rt, newTypeError(rt, "value is not a WritableStreamDefaultWriter")) + } + + return writer +} + +// Abort aborts the stream, signaling that the producer can no longer successfully write to +// the stream. +// +// It implements the WritableStreamDefaultWriter.abort(reason) [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#default-writer-abort +func (writer *WritableStreamDefaultWriter) Abort(reason sobek.Value) *sobek.Promise { + if reason == nil { + reason = sobek.Undefined() + } + + // 1. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception. + stream := writer.stream + if stream == nil { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "stream is undefined").Err()) + } + + // 2. Return ! WritableStreamDefaultWriterAbort(this, reason). + var promise *sobek.Promise + stream.withTransaction(func() { + promise = writer.abort(reason) + }) + return promise +} + +// Close closes the stream. +// +// It implements the WritableStreamDefaultWriter.close() [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#default-writer-close +func (writer *WritableStreamDefaultWriter) Close() *sobek.Promise { + // 1. Let stream be this.[[stream]]. + stream := writer.stream + + // 2. If stream is undefined, return a promise rejected with a TypeError exception. + if stream == nil { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "stream is undefined").Err()) + } + + // 3. If ! WritableStreamCloseQueuedOrInFlight(stream) is true, return a promise rejected with a TypeError. + if stream.closeQueuedOrInFlight() { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "stream is already closing").Err()) + } + + // 4. Return ! WritableStreamDefaultWriterClose(this). + var promise *sobek.Promise + stream.withTransaction(func() { + promise = writer.close() + }) + return promise +} + +// Write writes the given chunk to the writable stream, by waiting until any previous writes +// have finished successfully, and then sending the chunk to the underlying sink's write() +// method. +// +// It implements the WritableStreamDefaultWriter.write(chunk) [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#default-writer-write +func (writer *WritableStreamDefaultWriter) Write(chunk sobek.Value) *sobek.Promise { + if chunk == nil { + chunk = sobek.Undefined() + } + + // 1. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception. + stream := writer.stream + if stream == nil { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "stream is undefined").Err()) + } + + // 2. Return ! WritableStreamDefaultWriterWrite(this, chunk). + var promise *sobek.Promise + stream.withTransaction(func() { + promise = writer.write(chunk) + }) + return promise +} + +// ReleaseLock releases the writer's lock on the corresponding stream. After the lock is +// released, the writer is no longer active. +// +// It implements the WritableStreamDefaultWriter.releaseLock() [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#default-writer-release-lock +func (writer *WritableStreamDefaultWriter) ReleaseLock() { + // 1. Let stream be this.[[stream]]. + stream := writer.stream + + // 2. If stream is undefined, return. + if stream == nil { + return + } + + // 3. Assert: stream.[[writer]] is not undefined. + if stream.writer == nil { + common.Throw(writer.runtime, newError(AssertionError, "stream has no writer")) + } + + // 4. Perform ! WritableStreamDefaultWriterRelease(this). + stream.withTransaction(func() { + writer.release() + }) +} + +// DesiredSize returns the desired size to fill the stream's internal queue. +// +// It implements the WritableStreamDefaultWriter.desiredSize [specification] getter. +// +// [specification]: https://streams.spec.whatwg.org/#default-writer-desired-size +func (writer *WritableStreamDefaultWriter) DesiredSize() sobek.Value { + // 1. If this.[[stream]] is undefined, throw a TypeError exception. + if writer.stream == nil { + throw(writer.runtime, newTypeError(writer.runtime, "stream is undefined")) + } + + // 2. Return ! WritableStreamDefaultWriterGetDesiredSize(this). + return writer.getDesiredSize() +} + +// setup implements the [specification]'s SetUpWritableStreamDefaultWriter abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#set-up-writable-stream-default-writer +func (writer *WritableStreamDefaultWriter) setup(stream *WritableStream) { + rt := stream.runtime + + // 1. If ! IsWritableStreamLocked(stream) is true, throw a TypeError exception. + if stream.isLocked() { + throw(rt, newTypeError(rt, "stream is locked")) + } + + // 2. Set writer.[[stream]] to stream. + writer.stream = stream + writer.runtime = rt + writer.vu = stream.vu + + // 3. Set stream.[[writer]] to writer. + stream.writer = writer + + // 4. Let state be stream.[[state]]. + state := stream.state + + switch state { + // 5. If state is "writable", + case WritableStreamStateWritable: + // 5.1. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and stream.[[backpressure]] is true, + // set writer.[[readyPromise]] to a new promise. + if !stream.closeQueuedOrInFlight() && stream.backpressure { + writer.readyPromise = newPromiseWrapper(rt) + } else { + // 5.2. Otherwise, set writer.[[readyPromise]] to a promise resolved with undefined. + writer.readyPromise = newResolvedPromiseWrapper(rt, sobek.Undefined()) + } + // 5.3. Set writer.[[closedPromise]] to a new promise. + writer.closedPromise = newPromiseWrapper(rt) + // 6. Otherwise, if state is "erroring", + case WritableStreamStateErroring: + // 6.1. Set writer.[[readyPromise]] to a promise rejected with stream.[[storedError]]. + writer.readyPromise = newRejectedPromiseWrapper(rt, stream.storedError) + // 6.2. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(rt, writer.readyPromise.promise) + // 6.3. Set writer.[[closedPromise]] to a new promise. + writer.closedPromise = newPromiseWrapper(rt) + // 7. Otherwise, if state is "closed", + case WritableStreamStateClosed: + // 7.1. Set writer.[[readyPromise]] to a promise resolved with undefined. + writer.readyPromise = newResolvedPromiseWrapper(rt, sobek.Undefined()) + // 7.2. Set writer.[[closedPromise]] to a promise resolved with undefined. + writer.closedPromise = newResolvedPromiseWrapper(rt, sobek.Undefined()) + // 8. Otherwise, + default: + // 8.1. Assert: state is "errored". + if state != WritableStreamStateErrored { + common.Throw(rt, newError(AssertionError, "stream is not errored")) + } + // 8.2. Let storedError be stream.[[storedError]]. + storedError := stream.storedError + // 8.3. Set writer.[[readyPromise]] to a promise rejected with storedError. + writer.readyPromise = newRejectedPromiseWrapper(rt, storedError) + // 8.4. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(rt, writer.readyPromise.promise) + // 8.5. Set writer.[[closedPromise]] to a promise rejected with storedError. + writer.closedPromise = newRejectedPromiseWrapper(rt, storedError) + // 8.6. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(rt, writer.closedPromise.promise) + } +} + +// settle schedules a promise settlement on the writer's stream, deferring it until the end +// of the current transaction. If the writer is no longer attached to a stream, the +// settlement runs immediately. +func (writer *WritableStreamDefaultWriter) settle(fn func()) { + if writer.stream != nil { + writer.stream.settle(fn) + return + } + fn() +} + +// abort implements the [specification]'s WritableStreamDefaultWriterAbort abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-abort +func (writer *WritableStreamDefaultWriter) abort(reason sobek.Value) *sobek.Promise { + // 1. Let stream be writer.[[stream]]. + stream := writer.stream + + // 2. Assert: stream is not undefined. + if stream == nil { + common.Throw(writer.runtime, newError(AssertionError, "stream is undefined")) + } + + // 3. Return ! WritableStreamAbort(stream, reason). + return stream.abort(reason) +} + +// close implements the [specification]'s WritableStreamDefaultWriterClose abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-close +func (writer *WritableStreamDefaultWriter) close() *sobek.Promise { + // 1. Let stream be writer.[[stream]]. + stream := writer.stream + + // 2. Assert: stream is not undefined. + if stream == nil { + common.Throw(writer.runtime, newError(AssertionError, "stream is undefined")) + } + + // 3. Return ! WritableStreamClose(stream). + return stream.close() +} + +// getDesiredSize implements the [specification]'s WritableStreamDefaultWriterGetDesiredSize +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-get-desired-size +func (writer *WritableStreamDefaultWriter) getDesiredSize() sobek.Value { + // 1. Let stream be writer.[[stream]]. + stream := writer.stream + + // 2. Let state be stream.[[state]]. + state := stream.state + + // 3. If state is "errored" or "erroring", return null. + if state == WritableStreamStateErrored || state == WritableStreamStateErroring { + return sobek.Null() + } + + // 4. If state is "closed", return 0. + if state == WritableStreamStateClosed { + return writer.runtime.ToValue(0) + } + + // 5. Return ! WritableStreamDefaultControllerGetDesiredSize(stream.[[controller]]). + return writer.runtime.ToValue(stream.controller.getDesiredSize()) +} + +// ensureClosedPromiseRejected implements the [specification]'s +// WritableStreamDefaultWriterEnsureClosedPromiseRejected abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-ensure-closed-promise-rejected +func (writer *WritableStreamDefaultWriter) ensureClosedPromiseRejected(err any) { + // 1. If writer.[[closedPromise]].[[PromiseState]] is "pending", reject writer.[[closedPromise]] with error. + // 2. Otherwise, set writer.[[closedPromise]] to a promise rejected with error. + // + // The promise identity is updated synchronously (so that the `closed` getter reflects it + // immediately), but the actual rejection is deferred so that reactions observe up-to-date + // state. See [WritableStream.withTransaction]. + if !writer.closedPromise.isPending() { + writer.closedPromise = newPromiseWrapper(writer.runtime) + } + closedPromise := writer.closedPromise + closedPromise.queueSettlement() + writer.settle(func() { + closedPromise.rejectWith(err) + // 3. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(writer.runtime, closedPromise.promise) + }) +} + +// ensureReadyPromiseRejected implements the [specification]'s +// WritableStreamDefaultWriterEnsureReadyPromiseRejected abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-ensure-ready-promise-rejected +func (writer *WritableStreamDefaultWriter) ensureReadyPromiseRejected(err any) { + // 1. If writer.[[readyPromise]].[[PromiseState]] is "pending", reject writer.[[readyPromise]] with error. + // 2. Otherwise, set writer.[[readyPromise]] to a promise rejected with error. + // + // The promise identity is updated synchronously (so that the `ready` getter reflects it + // immediately), but the actual rejection is deferred so that reactions observe up-to-date + // state. See [WritableStream.withTransaction]. + if !writer.readyPromise.isPending() { + writer.readyPromise = newPromiseWrapper(writer.runtime) + } + readyPromise := writer.readyPromise + readyPromise.queueSettlement() + writer.settle(func() { + readyPromise.rejectWith(err) + // 3. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(writer.runtime, readyPromise.promise) + }) +} + +// release implements the [specification]'s WritableStreamDefaultWriterRelease abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-release +func (writer *WritableStreamDefaultWriter) release() { + // 1. Let stream be writer.[[stream]]. + stream := writer.stream + + // 2. Assert: stream is not undefined. + if stream == nil { + common.Throw(writer.runtime, newError(AssertionError, "stream is undefined")) + } + + // 3. Assert: stream.[[writer]] is writer. + if stream.writer != writer { + common.Throw(writer.runtime, newError(AssertionError, "stream writer is not writer")) + } + + // 4. Let releasedError be a new TypeError. + releasedError := newTypeError(writer.runtime, "writer released") + + // 5. Perform ! WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError). + writer.ensureReadyPromiseRejected(releasedError) + + // 6. Perform ! WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError). + writer.ensureClosedPromiseRejected(releasedError) + + // 7. Set stream.[[writer]] to undefined. + stream.writer = nil + + // 8. Set writer.[[stream]] to undefined. + writer.stream = nil +} + +// write implements the [specification]'s WritableStreamDefaultWriterWrite abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-default-writer-write +func (writer *WritableStreamDefaultWriter) write(chunk sobek.Value) *sobek.Promise { + // 1. Let stream be writer.[[stream]]. + stream := writer.stream + + // 2. Assert: stream is not undefined. + if stream == nil { + common.Throw(writer.runtime, newError(AssertionError, "stream is undefined")) + } + + // 3. Let controller be stream.[[controller]]. + controller := stream.controller + + // 4. Let chunkSize be ! WritableStreamDefaultControllerGetChunkSize(controller, chunk). + chunkSize := controller.getChunkSize(chunk) + + // 5. If stream is not equal to writer.[[stream]], return a promise rejected with a TypeError exception. + if writer.stream != stream { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "writer was released").Err()) + } + + // 6. Let state be stream.[[state]]. + state := stream.state + + // 7. If state is "errored", return a promise rejected with stream.[[storedError]]. + if state == WritableStreamStateErrored { + return newRejectedPromise(writer.vu, throwableValue(stream.storedError)) + } + + // 8. If ! WritableStreamCloseQueuedOrInFlight(stream) is true or state is "closed", + // return a promise rejected with a TypeError exception indicating that the stream is closing or closed. + if stream.closeQueuedOrInFlight() || state == WritableStreamStateClosed { + return newRejectedPromise(writer.vu, newTypeError(writer.runtime, "stream is closing or closed").Err()) + } + + // 9. If state is "erroring", return a promise rejected with stream.[[storedError]]. + if state == WritableStreamStateErroring { + return newRejectedPromise(writer.vu, throwableValue(stream.storedError)) + } + + // 10. Assert: state is "writable". + if state != WritableStreamStateWritable { + common.Throw(writer.runtime, newError(AssertionError, "stream is not writable")) + } + + // 11. Let promise be ! WritableStreamAddWriteRequest(stream). + promise := stream.addWriteRequest() + + // 12. Perform ! WritableStreamDefaultControllerWrite(controller, chunk, chunkSize). + controller.write(chunk, chunkSize) + + // 13. Return promise. + return promise +} diff --git a/internal/js/modules/k6/experimental/streams/writable_streams.go b/internal/js/modules/k6/experimental/streams/writable_streams.go new file mode 100644 index 000000000..62c89d2e5 --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/writable_streams.go @@ -0,0 +1,1156 @@ +package streams + +import ( + "github.com/grafana/sobek" + + "go.k6.io/k6/v2/js/common" + "go.k6.io/k6/v2/js/modules" +) + +// WritableStream is a concrete instance of the general [writable stream] concept. +// +// It is adaptable to any chunk type, and maintains an internal queue to keep track of +// data supplied by the producer but not yet written to the underlying sink. +// +// [writable stream]: https://streams.spec.whatwg.org/#ws-class +type WritableStream struct { + // backpressure holds the backpressure signal set by the controller. + backpressure bool + + // closeRequest holds the promise returned by the writer's or the stream's close method. + closeRequest *promiseWrapper + + // controller holds a [WritableStreamDefaultController] created with the ability to + // control the state and queue of this stream. + controller *WritableStreamDefaultController + + // inFlightWriteRequest holds the write request while a write is in flight. + inFlightWriteRequest *promiseWrapper + + // inFlightCloseRequest holds the close request while a close is in flight. + inFlightCloseRequest *promiseWrapper + + // pendingAbortRequest holds the pending abort request, or nil if there is none. + pendingAbortRequest *pendingAbortRequest + + // state holds the current state of the stream. + state WritableStreamState + + // storedError holds the error that caused the stream to be errored. + storedError any + + // writer holds the current writer of the stream if the stream is locked to a writer, + // or nil otherwise. + writer *WritableStreamDefaultWriter + // writerPrototype is the canonical prototype shared by writers vended by this stream's + // module instance. + writerPrototype *sobek.Object + + // writeRequests holds a list of pending write request promises. + writeRequests []*promiseWrapper + + // txDepth tracks the nesting depth of the current settlement transaction (see + // withTransaction). + txDepth int + + // txQueue holds promise settlements deferred until the current transaction completes. + txQueue []func() + + runtime *sobek.Runtime + vu modules.VU +} + +// WritableStreamState represents the current state of a WritableStream. +type WritableStreamState string + +const ( + // WritableStreamStateWritable indicates that the stream is writable, and more chunks may be written. + WritableStreamStateWritable = "writable" + + // WritableStreamStateErroring indicates that the stream is in the process of becoming errored. + WritableStreamStateErroring = "erroring" + + // WritableStreamStateClosed indicates that the stream is closed and cannot be written to. + WritableStreamStateClosed = "closed" + + // WritableStreamStateErrored indicates that the stream has been errored. + WritableStreamStateErrored = "errored" +) + +// pendingAbortRequest holds the state of a pending abort request, as described in the [specification]. +// +// [specification]: https://streams.spec.whatwg.org/#pending-abort-request +type pendingAbortRequest struct { + // promise is the promise returned from the stream's abort() method. + promise *promiseWrapper + + // reason is the value that was passed as an argument to the stream's abort() method. + reason any + + // wasAlreadyErroring is true if the stream was already erroring when abort() was called. + wasAlreadyErroring bool +} + +// withTransaction runs fn, and then, once the outermost transaction completes, flushes any +// promise settlements that were deferred via settle() during fn. +// +// This is necessary because, in the k6/Sobek event loop, resolving or rejecting a promise +// from Go runs its reactions synchronously. The Streams specification, on the other hand, +// assumes that settling a promise merely schedules its reactions, which run only after all +// synchronous state changes have completed. Deferring the settlements until the end of the +// transaction reproduces that behaviour: reactions observe fully-updated stream state, and +// settle in a deterministic, specification-compliant (FIFO) order. +func (stream *WritableStream) withTransaction(fn func()) { + stream.txDepth++ + defer func() { + stream.txDepth-- + if stream.txDepth == 0 { + stream.drainSettlements() + } + }() + + fn() +} + +// drainSettlements runs the deferred promise settlements in FIFO order. +func (stream *WritableStream) drainSettlements() { + // Keep the depth elevated while draining, so that any settlements scheduled by reactions + // are appended to the queue and processed in order, rather than recursively. + stream.txDepth++ + for len(stream.txQueue) > 0 { + settle := stream.txQueue[0] + stream.txQueue = stream.txQueue[1:] + settle() + } + stream.txDepth-- +} + +// settle schedules a promise settlement. If a transaction is active, the settlement is +// deferred until the transaction completes; otherwise, it runs immediately. +func (stream *WritableStream) settle(fn func()) { + if stream.txDepth == 0 { + fn() + return + } + stream.txQueue = append(stream.txQueue, fn) +} + +func (stream *WritableStream) resolvePromise(promise *promiseWrapper, value any) { + promise.queueSettlement() + stream.settle(func() { promise.resolveWith(value) }) +} + +func (stream *WritableStream) rejectPromise(promise *promiseWrapper, reason any) { + promise.queueSettlement() + stream.settle(func() { promise.rejectWith(reason) }) +} + +// Abort aborts the stream, signaling that the producer can no longer successfully write to +// the stream and it is to be immediately moved to an errored state, with any queued-up +// writes discarded. +// +// It implements the WritableStream.abort(reason) [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#ws-abort +func (stream *WritableStream) Abort(reason sobek.Value) *sobek.Promise { + if reason == nil { + reason = sobek.Undefined() + } + + // 1. If ! IsWritableStreamLocked(this) is true, return a promise rejected with a TypeError exception. + if stream.isLocked() { + return newRejectedPromise(stream.vu, newTypeError(stream.runtime, "cannot abort a locked stream").Err()) + } + + // 2. Return ! WritableStreamAbort(this, reason). + var promise *sobek.Promise + stream.withTransaction(func() { + promise = stream.abort(reason) + }) + return promise +} + +// Close closes the stream. +// +// It implements the WritableStream.close() [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#ws-close +func (stream *WritableStream) Close() *sobek.Promise { + // 1. If ! IsWritableStreamLocked(this) is true, return a promise rejected with a TypeError exception. + if stream.isLocked() { + return newRejectedPromise(stream.vu, newTypeError(stream.runtime, "cannot close a locked stream").Err()) + } + + // 2. If ! WritableStreamCloseQueuedOrInFlight(this) is true, return a promise rejected with a TypeError exception. + if stream.closeQueuedOrInFlight() { + return newRejectedPromise(stream.vu, newTypeError(stream.runtime, "stream is already closing").Err()) + } + + // 3. Return ! WritableStreamClose(this). + var promise *sobek.Promise + stream.withTransaction(func() { + promise = stream.close() + }) + return promise +} + +// GetWriter implements the WritableStream.getWriter() [specification] algorithm. +// +// [specification]: https://streams.spec.whatwg.org/#ws-get-writer +func (stream *WritableStream) GetWriter() sobek.Value { + // 1. Return ? AcquireWritableStreamDefaultWriter(this). + writer := stream.acquireDefaultWriter() + + obj, err := NewWritableStreamDefaultWriterObject(writer, stream.writerPrototype) + if err != nil { + common.Throw(stream.runtime, err) + } + + return obj +} + +// streamGoRefKey is the name of a hidden, non-enumerable property that holds a reference to +// the Go [WritableStream] on the stream's JavaScript object. It allows retrieving the Go +// value back from the JavaScript object (e.g. in the WritableStreamDefaultWriter constructor +// and in brand-check getters), which is not otherwise possible on a plain object. +const streamGoRefKey = "__k6WritableStream__" + +// writableStreamFromValue retrieves the Go [WritableStream] associated with a JavaScript +// value, or nil if the value is not a WritableStream object. +func writableStreamFromValue(rt *sobek.Runtime, value sobek.Value) *WritableStream { + if value == nil || common.IsNullish(value) || !isObject(value) { + return nil + } + + ref := value.ToObject(rt).Get(streamGoRefKey) + if ref == nil { + return nil + } + + stream, _ := ref.Export().(*WritableStream) + return stream +} + +func installWritableStreamPrototype(rt *sobek.Runtime, proto *sobek.Object) error { + if hasOwnProperty(proto, "locked") { + return nil + } + + return proto.DefineAccessorProperty("locked", rt.ToValue(func(fc sobek.FunctionCall) sobek.Value { + stream := writableStreamFromValue(rt, fc.This) + if stream == nil { + return sobek.Undefined() + } + return rt.ToValue(stream.isLocked()) + }), nil, sobek.FLAG_TRUE, sobek.FLAG_TRUE) +} + +// toObject builds the stream's JavaScript object. +// +// We build it as a plain object rather than a reflect-wrapped Go value, because the latter is +// not extensible: the Web Platform Tests' recordingWritableStream helper assigns extra +// properties (such as `controller` and `events`) to the stream, which a reflect-wrapped host +// object does not allow. The given proto is used as the object's prototype. +func (stream *WritableStream) toObject(proto *sobek.Object) *sobek.Object { + rt := stream.runtime + obj := rt.NewObject() + objName := "WritableStream" + + if err := setReadOnlyPropertyOf(obj, objName, "abort", rt.ToValue(stream.Abort)); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + if err := setReadOnlyPropertyOf(obj, objName, "close", rt.ToValue(stream.Close)); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + if err := setReadOnlyPropertyOf(obj, objName, "getWriter", rt.ToValue(stream.GetWriter)); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + + // We keep a hidden, non-enumerable reference to the Go stream on the object, so that the + // WritableStreamDefaultWriter constructor and the prototype's `locked` getter can retrieve + // it back from the JavaScript object. + if err := obj.DefineDataProperty( + streamGoRefKey, rt.ToValue(stream), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_FALSE, + ); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + + if err := obj.SetPrototype(proto); err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + + return obj +} + +// isLocked implements the [specification]'s IsWritableStreamLocked abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#is-writable-stream-locked +func (stream *WritableStream) isLocked() bool { + return stream.writer != nil +} + +// initialize implements the [specification]'s InitializeWritableStream abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#initialize-writable-stream +func (stream *WritableStream) initialize() { + stream.state = WritableStreamStateWritable + stream.storedError = nil + stream.writer = nil + stream.controller = nil + stream.inFlightWriteRequest = nil + stream.closeRequest = nil + stream.inFlightCloseRequest = nil + stream.pendingAbortRequest = nil + stream.writeRequests = []*promiseWrapper{} + stream.backpressure = false +} + +// acquireDefaultWriter implements the [specification]'s AcquireWritableStreamDefaultWriter abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#acquire-writable-stream-default-writer +func (stream *WritableStream) acquireDefaultWriter() *WritableStreamDefaultWriter { + // 1. Let writer be a new WritableStreamDefaultWriter. + writer := &WritableStreamDefaultWriter{} + + // 2. Perform ? SetUpWritableStreamDefaultWriter(writer, stream). + writer.setup(stream) + + // 3. Return writer. + return writer +} + +// abort implements the [specification]'s WritableStreamAbort abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-abort +func (stream *WritableStream) abort(reason sobek.Value) *sobek.Promise { + // 1. If stream.[[state]] is "closed" or "errored", return a promise resolved with undefined. + if stream.state == WritableStreamStateClosed || stream.state == WritableStreamStateErrored { + return newResolvedPromise(stream.vu, sobek.Undefined()) + } + + // 2. Signal abort on stream.[[controller]].[[signal]] with reason. + // NOTE: k6 does not support AbortSignal yet, so this step is intentionally omitted. + + // 3. Let state be stream.[[state]]. + state := stream.state + + // 4. If state is "closed" or "errored", return a promise resolved with undefined. + if state == WritableStreamStateClosed || state == WritableStreamStateErrored { + return newResolvedPromise(stream.vu, sobek.Undefined()) + } + + // 5. If stream.[[pendingAbortRequest]] is not undefined, return stream.[[pendingAbortRequest]]'s promise. + if stream.pendingAbortRequest != nil { + return stream.pendingAbortRequest.promise.promise + } + + // 6. Assert: state is "writable" or "erroring". + if state != WritableStreamStateWritable && state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 7. Let wasAlreadyErroring be false. + wasAlreadyErroring := false + + // 8. If state is "erroring", + if state == WritableStreamStateErroring { + // 8.1. Set wasAlreadyErroring to true. + wasAlreadyErroring = true + // 8.2. Set reason to undefined. + reason = sobek.Undefined() + } + + // 9. Let promise be a new promise. + promise := newPromiseWrapper(stream.runtime) + + // 10. Set stream.[[pendingAbortRequest]] to a new pending abort request. + stream.pendingAbortRequest = &pendingAbortRequest{ + promise: promise, + reason: reason, + wasAlreadyErroring: wasAlreadyErroring, + } + + // 11. If wasAlreadyErroring is false, perform ! WritableStreamStartErroring(stream, reason). + if !wasAlreadyErroring { + stream.startErroring(reason) + } + + // 12. Return promise. + return promise.promise +} + +// close implements the [specification]'s WritableStreamClose abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-close +func (stream *WritableStream) close() *sobek.Promise { + // 1. Let state be stream.[[state]]. + state := stream.state + + // 2. If state is "closed" or "errored", return a promise rejected with a TypeError exception. + if state == WritableStreamStateClosed || state == WritableStreamStateErrored { + return newRejectedPromise(stream.vu, newTypeError(stream.runtime, "stream is already closed or errored").Err()) + } + + // 3. Assert: state is "writable" or "erroring". + if state != WritableStreamStateWritable && state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 4. Assert: ! WritableStreamCloseQueuedOrInFlight(stream) is false. + if stream.closeQueuedOrInFlight() { + common.Throw(stream.runtime, newError(AssertionError, "stream is already closing")) + } + + // 5. Let promise be a new promise. + promise := newPromiseWrapper(stream.runtime) + + // 6. Set stream.[[closeRequest]] to promise. + stream.closeRequest = promise + + // 7. Let writer be stream.[[writer]]. + writer := stream.writer + + // 8. If writer is not undefined, and stream.[[backpressure]] is true, and state is "writable", + // resolve writer.[[readyPromise]] with undefined. + if writer != nil && stream.backpressure && state == WritableStreamStateWritable { + readyPromise := writer.readyPromise + stream.resolvePromise(readyPromise, sobek.Undefined()) + } + + // 9. Perform ! WritableStreamDefaultControllerClose(stream.[[controller]]). + stream.controller.close() + + // 10. Return promise. + return promise.promise +} + +// addWriteRequest implements the [specification]'s WritableStreamAddWriteRequest abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-add-write-request +func (stream *WritableStream) addWriteRequest() *sobek.Promise { + // 1. Assert: ! IsWritableStreamLocked(stream) is true. + if !stream.isLocked() { + common.Throw(stream.runtime, newError(AssertionError, "stream is not locked")) + } + + // 2. Assert: stream.[[state]] is "writable". + if stream.state != WritableStreamStateWritable { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable")) + } + + // 3. Let promise be a new promise. + promise := newPromiseWrapper(stream.runtime) + + // 4. Append promise to stream.[[writeRequests]]. + stream.writeRequests = append(stream.writeRequests, promise) + + // 5. Return promise. + return promise.promise +} + +// closeQueuedOrInFlight implements the [specification]'s WritableStreamCloseQueuedOrInFlight +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-close-queued-or-in-flight +func (stream *WritableStream) closeQueuedOrInFlight() bool { + // 1. If stream.[[closeRequest]] is undefined and stream.[[inFlightCloseRequest]] is undefined, return false. + // 2. Return true. + return stream.closeRequest != nil || stream.inFlightCloseRequest != nil +} + +// dealWithRejection implements the [specification]'s WritableStreamDealWithRejection +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-deal-with-rejection +func (stream *WritableStream) dealWithRejection(err any) { + // 1. Let state be stream.[[state]]. + state := stream.state + + // 2. If state is "writable", + if state == WritableStreamStateWritable { + // 2.1. Perform ! WritableStreamStartErroring(stream, error). + stream.startErroring(err) + // 2.2. Return. + return + } + + // 3. Assert: state is "erroring". + if state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not erroring")) + } + + // 4. Perform ! WritableStreamFinishErroring(stream). + stream.finishErroring() +} + +// startErroring implements the [specification]'s WritableStreamStartErroring abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-start-erroring +func (stream *WritableStream) startErroring(reason any) { + // 1. Assert: stream.[[storedError]] is undefined. + if stream.storedError != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream already has a stored error")) + } + + // 2. Assert: stream.[[state]] is "writable". + if stream.state != WritableStreamStateWritable { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable")) + } + + // 3. Let controller be stream.[[controller]]. + controller := stream.controller + + // 4. Assert: controller is not undefined. + if controller == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no controller")) + } + + // 5. Set stream.[[state]] to "erroring". + stream.state = WritableStreamStateErroring + + // 6. Set stream.[[storedError]] to reason. + stream.storedError = reason + + // 7. Let writer be stream.[[writer]]. + writer := stream.writer + + // 8. If writer is not undefined, perform ! WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason). + if writer != nil { + writer.ensureReadyPromiseRejected(reason) + } + + // 9. If ! WritableStreamHasOperationMarkedInFlight(stream) is false and controller.[[started]] is true, + // perform ! WritableStreamFinishErroring(stream). + if !stream.hasOperationMarkedInFlight() && controller.started { + stream.finishErroring() + } +} + +// finishErroring implements the [specification]'s WritableStreamFinishErroring abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-finish-erroring +func (stream *WritableStream) finishErroring() { + // 1. Assert: stream.[[state]] is "erroring". + if stream.state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not erroring")) + } + + // 2. Assert: ! WritableStreamHasOperationMarkedInFlight(stream) is false. + if stream.hasOperationMarkedInFlight() { + common.Throw(stream.runtime, newError(AssertionError, "stream has an operation marked in-flight")) + } + + // 3. Set stream.[[state]] to "errored". + stream.state = WritableStreamStateErrored + + // 4. Perform ! stream.[[controller]].[[ErrorSteps]](). + stream.controller.errorSteps() + + // 5. Let storedError be stream.[[storedError]]. + storedError := stream.storedError + + // 6. For each writeRequest of stream.[[writeRequests]]: reject writeRequest with storedError. + for _, writeRequest := range stream.writeRequests { + wr := writeRequest + stream.rejectPromise(wr, storedError) + } + + // 7. Set stream.[[writeRequests]] to an empty list. + stream.writeRequests = []*promiseWrapper{} + + // 8. If stream.[[pendingAbortRequest]] is undefined, + if stream.pendingAbortRequest == nil { + // 8.1. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). + stream.rejectCloseAndClosedPromiseIfNeeded() + // 8.2. Return. + return + } + + // 9. Let abortRequest be stream.[[pendingAbortRequest]]. + abortRequest := stream.pendingAbortRequest + + // 10. Set stream.[[pendingAbortRequest]] to undefined. + stream.pendingAbortRequest = nil + + // 11. If abortRequest's was already erroring is true, + if abortRequest.wasAlreadyErroring { + // 11.1. Reject abortRequest's promise with storedError. + stream.rejectPromise(abortRequest.promise, storedError) + // 11.2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). + stream.rejectCloseAndClosedPromiseIfNeeded() + // 11.3. Return. + return + } + + // 12. Let promise be ! stream.[[controller]].[[AbortSteps]](abortRequest's reason). + promise := stream.controller.abortSteps(abortRequest.reason) + + _, err := promiseThen(stream.runtime, promise, + // 13. Upon fulfillment of promise, + func(sobek.Value) { + stream.withTransaction(func() { + // 13.1. Resolve abortRequest's promise with undefined. + stream.resolvePromise(abortRequest.promise, sobek.Undefined()) + // 13.2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). + stream.rejectCloseAndClosedPromiseIfNeeded() + }) + }, + // 14. Upon rejection of promise with reason reason, + func(reason sobek.Value) { + stream.withTransaction(func() { + // 14.1. Reject abortRequest's promise with reason. + stream.rejectPromise(abortRequest.promise, reason) + // 14.2. Perform ! WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream). + stream.rejectCloseAndClosedPromiseIfNeeded() + }) + }, + ) + if err != nil { + common.Throw(stream.runtime, err) + } +} + +// finishInFlightWrite implements the [specification]'s WritableStreamFinishInFlightWrite +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write +func (stream *WritableStream) finishInFlightWrite() { + // 1. Assert: stream.[[inFlightWriteRequest]] is not undefined. + if stream.inFlightWriteRequest == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no in-flight write request")) + } + + // 2. Resolve stream.[[inFlightWriteRequest]] with undefined. + inFlightWriteRequest := stream.inFlightWriteRequest + stream.resolvePromise(inFlightWriteRequest, sobek.Undefined()) + + // 3. Set stream.[[inFlightWriteRequest]] to undefined. + stream.inFlightWriteRequest = nil +} + +// finishInFlightWriteWithError implements the [specification]'s +// WritableStreamFinishInFlightWriteWithError abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-write-with-error +func (stream *WritableStream) finishInFlightWriteWithError(err any) { + // 1. Assert: stream.[[inFlightWriteRequest]] is not undefined. + if stream.inFlightWriteRequest == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no in-flight write request")) + } + + // 2. Reject stream.[[inFlightWriteRequest]] with error. + inFlightWriteRequest := stream.inFlightWriteRequest + stream.rejectPromise(inFlightWriteRequest, err) + + // 3. Set stream.[[inFlightWriteRequest]] to undefined. + stream.inFlightWriteRequest = nil + + // 4. Assert: stream.[[state]] is "writable" or "erroring". + if stream.state != WritableStreamStateWritable && stream.state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 5. Perform ! WritableStreamDealWithRejection(stream, error). + stream.dealWithRejection(err) +} + +// finishInFlightClose implements the [specification]'s WritableStreamFinishInFlightClose +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close +func (stream *WritableStream) finishInFlightClose() { + // 1. Assert: stream.[[inFlightCloseRequest]] is not undefined. + if stream.inFlightCloseRequest == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no in-flight close request")) + } + + // 2. Resolve stream.[[inFlightCloseRequest]] with undefined. + inFlightCloseRequest := stream.inFlightCloseRequest + stream.resolvePromise(inFlightCloseRequest, sobek.Undefined()) + + // 3. Set stream.[[inFlightCloseRequest]] to undefined. + stream.inFlightCloseRequest = nil + + // 4. Let state be stream.[[state]]. + state := stream.state + + // 5. Assert: stream.[[state]] is "writable" or "erroring". + if state != WritableStreamStateWritable && state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 6. If state is "erroring", + if state == WritableStreamStateErroring { + // 6.1. Set stream.[[storedError]] to undefined. + stream.storedError = nil + + // 6.2. If stream.[[pendingAbortRequest]] is not undefined, + if stream.pendingAbortRequest != nil { + // 6.2.1. Resolve stream.[[pendingAbortRequest]]'s promise with undefined. + abortRequest := stream.pendingAbortRequest + stream.resolvePromise(abortRequest.promise, sobek.Undefined()) + // 6.2.2. Set stream.[[pendingAbortRequest]] to undefined. + stream.pendingAbortRequest = nil + } + } + + // 7. Set stream.[[state]] to "closed". + stream.state = WritableStreamStateClosed + + // 8. Let writer be stream.[[writer]]. + writer := stream.writer + + // 9. If writer is not undefined, resolve writer.[[closedPromise]] with undefined. + if writer != nil { + closedPromise := writer.closedPromise + stream.resolvePromise(closedPromise, sobek.Undefined()) + } + + // 10. Assert: stream.[[pendingAbortRequest]] is undefined. + if stream.pendingAbortRequest != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has a pending abort request")) + } + + // 11. Assert: stream.[[storedError]] is undefined. + if stream.storedError != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has a stored error")) + } +} + +// finishInFlightCloseWithError implements the [specification]'s +// WritableStreamFinishInFlightCloseWithError abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-finish-in-flight-close-with-error +func (stream *WritableStream) finishInFlightCloseWithError(err any) { + // 1. Assert: stream.[[inFlightCloseRequest]] is not undefined. + if stream.inFlightCloseRequest == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no in-flight close request")) + } + + // 2. Reject stream.[[inFlightCloseRequest]] with error. + inFlightCloseRequest := stream.inFlightCloseRequest + stream.rejectPromise(inFlightCloseRequest, err) + + // 3. Set stream.[[inFlightCloseRequest]] to undefined. + stream.inFlightCloseRequest = nil + + // 4. Assert: stream.[[state]] is "writable" or "erroring". + if stream.state != WritableStreamStateWritable && stream.state != WritableStreamStateErroring { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable or erroring")) + } + + // 5. If stream.[[pendingAbortRequest]] is not undefined, + if stream.pendingAbortRequest != nil { + // 5.1. Reject stream.[[pendingAbortRequest]]'s promise with error. + abortRequest := stream.pendingAbortRequest + stream.rejectPromise(abortRequest.promise, err) + // 5.2. Set stream.[[pendingAbortRequest]] to undefined. + stream.pendingAbortRequest = nil + } + + // 6. Perform ! WritableStreamDealWithRejection(stream, error). + stream.dealWithRejection(err) +} + +// hasOperationMarkedInFlight implements the [specification]'s +// WritableStreamHasOperationMarkedInFlight abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-has-operation-marked-in-flight +func (stream *WritableStream) hasOperationMarkedInFlight() bool { + // 1. If stream.[[inFlightWriteRequest]] is undefined and stream.[[inFlightCloseRequest]] is undefined, return false. + // 2. Return true. + return stream.inFlightWriteRequest != nil || stream.inFlightCloseRequest != nil +} + +// markCloseRequestInFlight implements the [specification]'s WritableStreamMarkCloseRequestInFlight +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-mark-close-request-in-flight +func (stream *WritableStream) markCloseRequestInFlight() { + // 1. Assert: stream.[[inFlightCloseRequest]] is undefined. + if stream.inFlightCloseRequest != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has an in-flight close request")) + } + + // 2. Assert: stream.[[closeRequest]] is not undefined. + if stream.closeRequest == nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has no close request")) + } + + // 3. Set stream.[[inFlightCloseRequest]] to stream.[[closeRequest]]. + stream.inFlightCloseRequest = stream.closeRequest + + // 4. Set stream.[[closeRequest]] to undefined. + stream.closeRequest = nil +} + +// markFirstWriteRequestInFlight implements the [specification]'s +// WritableStreamMarkFirstWriteRequestInFlight abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-mark-first-write-request-in-flight +func (stream *WritableStream) markFirstWriteRequestInFlight() { + // 1. Assert: stream.[[inFlightWriteRequest]] is undefined. + if stream.inFlightWriteRequest != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has an in-flight write request")) + } + + // 2. Assert: stream.[[writeRequests]] is not empty. + if len(stream.writeRequests) == 0 { + common.Throw(stream.runtime, newError(AssertionError, "stream has no write requests")) + } + + // 3. Let writeRequest be stream.[[writeRequests]][0]. + writeRequest := stream.writeRequests[0] + + // 4. Remove writeRequest from stream.[[writeRequests]]. + stream.writeRequests = stream.writeRequests[1:] + + // 5. Set stream.[[inFlightWriteRequest]] to writeRequest. + stream.inFlightWriteRequest = writeRequest +} + +// rejectCloseAndClosedPromiseIfNeeded implements the [specification]'s +// WritableStreamRejectCloseAndClosedPromiseIfNeeded abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-reject-close-and-closed-promise-if-needed +func (stream *WritableStream) rejectCloseAndClosedPromiseIfNeeded() { + // 1. Assert: stream.[[state]] is "errored". + if stream.state != WritableStreamStateErrored { + common.Throw(stream.runtime, newError(AssertionError, "stream is not errored")) + } + + storedError := stream.storedError + + // 2. If stream.[[closeRequest]] is not undefined, + if stream.closeRequest != nil { + // 2.1. Assert: stream.[[inFlightCloseRequest]] is undefined. + if stream.inFlightCloseRequest != nil { + common.Throw(stream.runtime, newError(AssertionError, "stream has an in-flight close request")) + } + + // 2.2. Reject stream.[[closeRequest]] with stream.[[storedError]]. + closeRequest := stream.closeRequest + stream.rejectPromise(closeRequest, storedError) + + // 2.3. Set stream.[[closeRequest]] to undefined. + stream.closeRequest = nil + } + + // 3. Let writer be stream.[[writer]]. + writer := stream.writer + + // 4. If writer is not undefined, + if writer != nil { + // 4.1. Reject writer.[[closedPromise]] with stream.[[storedError]]. + closedPromise := writer.closedPromise + closedPromise.queueSettlement() + stream.settle(func() { + closedPromise.rejectWith(storedError) + // 4.2. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. + markPromiseHandled(stream.runtime, closedPromise.promise) + }) + } +} + +// updateBackpressure implements the [specification]'s WritableStreamUpdateBackpressure +// abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#writable-stream-update-backpressure +func (stream *WritableStream) updateBackpressure(backpressure bool) { + // 1. Assert: stream.[[state]] is "writable". + if stream.state != WritableStreamStateWritable { + common.Throw(stream.runtime, newError(AssertionError, "stream is not writable")) + } + + // 2. Assert: ! WritableStreamCloseQueuedOrInFlight(stream) is false. + if stream.closeQueuedOrInFlight() { + common.Throw(stream.runtime, newError(AssertionError, "stream is already closing")) + } + + // 3. Let writer be stream.[[writer]]. + writer := stream.writer + + // 4. If writer is not undefined and backpressure is not stream.[[backpressure]], + if writer != nil && backpressure != stream.backpressure { + if backpressure { + // 4.1. If backpressure is true, set writer.[[readyPromise]] to a new promise. + writer.readyPromise = newPromiseWrapper(stream.runtime) + } else { + // 4.2. Otherwise, + // 4.2.1. Assert: backpressure is false. + // 4.2.2. Resolve writer.[[readyPromise]] with undefined. + readyPromise := writer.readyPromise + stream.resolvePromise(readyPromise, sobek.Undefined()) + } + } + + // 5. Set stream.[[backpressure]] to backpressure. + stream.backpressure = backpressure +} + +// setupWritableStreamDefaultControllerFromUnderlyingSink implements the [specification]'s +// SetUpWritableStreamDefaultControllerFromUnderlyingSink abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#set-up-writable-stream-default-controller-from-underlying-sink +func (stream *WritableStream) setupWritableStreamDefaultControllerFromUnderlyingSink( + underlyingSink *sobek.Object, + underlyingSinkDict UnderlyingSink, + highWaterMark float64, + sizeAlgorithm SizeAlgorithm, +) { + // 1. Let controller be a new WritableStreamDefaultController. + controller := &WritableStreamDefaultController{} + + // 2. Let startAlgorithm be an algorithm that returns undefined. + var startAlgorithm UnderlyingSinkStartCallback = func(*sobek.Object) sobek.Value { + return sobek.Undefined() + } + + // 3. Let writeAlgorithm be an algorithm that returns a promise resolved with undefined. + var writeAlgorithm UnderlyingSinkWriteCallback = func(sobek.Value, *sobek.Object) *sobek.Promise { + return newResolvedPromise(stream.vu, sobek.Undefined()) + } + + // 4. Let closeAlgorithm be an algorithm that returns a promise resolved with undefined. + var closeAlgorithm UnderlyingSinkCloseCallback = func() *sobek.Promise { + return newResolvedPromise(stream.vu, sobek.Undefined()) + } + + // 5. Let abortAlgorithm be an algorithm that returns a promise resolved with undefined. + var abortAlgorithm UnderlyingSinkAbortCallback = func(any) *sobek.Promise { + return newResolvedPromise(stream.vu, sobek.Undefined()) + } + + // 6. If underlyingSinkDict["start"] exists, then set startAlgorithm to an algorithm which + // returns the result of invoking underlyingSinkDict["start"] with argument list « controller » + // and callback this value underlyingSink. + if underlyingSinkDict.Start != nil && !sobek.IsUndefined(underlyingSinkDict.Start) { + startAlgorithm = stream.startAlgorithm(underlyingSink, underlyingSinkDict) + } + + // 7. If underlyingSinkDict["write"] exists, then set writeAlgorithm to an algorithm which + // returns the result of invoking underlyingSinkDict["write"] with argument list « chunk, controller » + // and callback this value underlyingSink. + if underlyingSinkDict.Write != nil && !sobek.IsUndefined(underlyingSinkDict.Write) { + writeAlgorithm = stream.writeAlgorithm(underlyingSink, underlyingSinkDict) + } + + // 8. If underlyingSinkDict["close"] exists, then set closeAlgorithm to an algorithm which + // returns the result of invoking underlyingSinkDict["close"] with argument list « » + // and callback this value underlyingSink. + if underlyingSinkDict.Close != nil && !sobek.IsUndefined(underlyingSinkDict.Close) { + closeAlgorithm = stream.closeAlgorithm(underlyingSink, underlyingSinkDict) + } + + // 9. If underlyingSinkDict["abort"] exists, then set abortAlgorithm to an algorithm which + // returns the result of invoking underlyingSinkDict["abort"] with argument list « reason » + // and callback this value underlyingSink. + if underlyingSinkDict.Abort != nil && !sobek.IsUndefined(underlyingSinkDict.Abort) { + abortAlgorithm = stream.abortAlgorithm(underlyingSink, underlyingSinkDict) + } + + // 10. Perform ? SetUpWritableStreamDefaultController(...). + stream.setupWritableStreamDefaultController( + controller, + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + highWaterMark, + sizeAlgorithm, + ) +} + +func (stream *WritableStream) startAlgorithm( + underlyingSink *sobek.Object, + underlyingSinkDict UnderlyingSink, +) UnderlyingSinkStartCallback { + call, ok := sobek.AssertFunction(underlyingSinkDict.Start) + if !ok { + throw(stream.runtime, newTypeError(stream.runtime, "underlyingSink.[[start]] must be a function")) + } + + return func(controller *sobek.Object) sobek.Value { + v, err := call(underlyingSink, controller) + if err != nil { + panic(err) + } + return v + } +} + +func (stream *WritableStream) writeAlgorithm( + underlyingSink *sobek.Object, + underlyingSinkDict UnderlyingSink, +) UnderlyingSinkWriteCallback { + call, ok := sobek.AssertFunction(underlyingSinkDict.Write) + if !ok { + throw(stream.runtime, newTypeError(stream.runtime, "underlyingSink.[[write]] must be a function")) + } + + return func(chunk sobek.Value, controller *sobek.Object) *sobek.Promise { + v, err := call(underlyingSink, chunk, controller) + if err != nil { + return stream.rejectedPromiseFromErr(err) + } + if p, ok := v.Export().(*sobek.Promise); ok { + return p + } + return newResolvedPromise(stream.vu, v) + } +} + +func (stream *WritableStream) closeAlgorithm( + underlyingSink *sobek.Object, + underlyingSinkDict UnderlyingSink, +) UnderlyingSinkCloseCallback { + call, ok := sobek.AssertFunction(underlyingSinkDict.Close) + if !ok { + throw(stream.runtime, newTypeError(stream.runtime, "underlyingSink.[[close]] must be a function")) + } + + return func() *sobek.Promise { + v, err := call(underlyingSink) + if err != nil { + return stream.rejectedPromiseFromErr(err) + } + if p, ok := v.Export().(*sobek.Promise); ok { + return p + } + return newResolvedPromise(stream.vu, v) + } +} + +func (stream *WritableStream) abortAlgorithm( + underlyingSink *sobek.Object, + underlyingSinkDict UnderlyingSink, +) UnderlyingSinkAbortCallback { + call, ok := sobek.AssertFunction(underlyingSinkDict.Abort) + if !ok { + throw(stream.runtime, newTypeError(stream.runtime, "underlyingSink.[[abort]] must be a function")) + } + + return func(reason any) *sobek.Promise { + v, err := call(underlyingSink, stream.runtime.ToValue(reason)) + if err != nil { + return stream.rejectedPromiseFromErr(err) + } + if p, ok := v.Export().(*sobek.Promise); ok { + return p + } + return newResolvedPromise(stream.vu, v) + } +} + +// rejectedPromiseFromErr returns a rejected promise from a Go error, unwrapping any +// [sobek.Exception] so that the original thrown value is preserved. +func (stream *WritableStream) rejectedPromiseFromErr(err error) *sobek.Promise { + return newRejectedPromise(stream.vu, exceptionValue(err)) +} + +// setupWritableStreamDefaultController implements the [specification]'s +// SetUpWritableStreamDefaultController abstract operation. +// +// [specification]: https://streams.spec.whatwg.org/#set-up-writable-stream-default-controller +func (stream *WritableStream) setupWritableStreamDefaultController( + controller *WritableStreamDefaultController, + startAlgorithm UnderlyingSinkStartCallback, + writeAlgorithm UnderlyingSinkWriteCallback, + closeAlgorithm UnderlyingSinkCloseCallback, + abortAlgorithm UnderlyingSinkAbortCallback, + highWaterMark float64, + sizeAlgorithm SizeAlgorithm, +) { + rt := stream.runtime + + // 1. Assert: stream implements WritableStream. + // 2. Assert: stream.[[controller]] is undefined. + if stream.controller != nil { + common.Throw(rt, newError(AssertionError, "stream already has a controller")) + } + + // 3. Set controller.[[stream]] to stream. + controller.stream = stream + + // 4. Set stream.[[controller]] to controller. + stream.controller = controller + + // 5. Perform ! ResetQueue(controller). + controller.resetQueue() + + // 6. Set controller.[[signal]] to a new AbortSignal. + // NOTE: k6 does not support AbortSignal yet, so this step is intentionally omitted. + + // 7. Set controller.[[started]] to false. + controller.started = false + + // 8. Set controller.[[strategySizeAlgorithm]] to sizeAlgorithm. + controller.strategySizeAlgorithm = sizeAlgorithm + + // 9. Set controller.[[strategyHWM]] to highWaterMark. + controller.strategyHWM = highWaterMark + + // 10. Set controller.[[writeAlgorithm]] to writeAlgorithm. + controller.writeAlgorithm = writeAlgorithm + + // 11. Set controller.[[closeAlgorithm]] to closeAlgorithm. + controller.closeAlgorithm = closeAlgorithm + + // 12. Set controller.[[abortAlgorithm]] to abortAlgorithm. + controller.abortAlgorithm = abortAlgorithm + + // 13. Let backpressure be ! WritableStreamDefaultControllerGetBackpressure(controller). + backpressure := controller.getBackpressure() + + // 14. Perform ! WritableStreamUpdateBackpressure(stream, backpressure). + stream.updateBackpressure(backpressure) + + // 15. Let startResult be the result of performing startAlgorithm. (This may throw an exception.) + controllerObj, err := controller.toObject() + if err != nil { + common.Throw(rt, newError(RuntimeError, err.Error())) + } + startResult := startAlgorithm(controllerObj) + + // 16. Let startPromise be a promise resolved with startResult. + var startPromise *sobek.Promise + if common.IsNullish(startResult) { + startPromise = newResolvedPromise(stream.vu, sobek.Undefined()) + } else if p, ok := startResult.Export().(*sobek.Promise); ok { + startPromise = p + } else { + startPromise = newResolvedPromise(stream.vu, startResult) + } + + _, err = promiseThen(rt, startPromise, + // 17. Upon fulfillment of startPromise, + func(sobek.Value) { + stream.withTransaction(func() { + // 17.1. Assert: stream.[[state]] is "writable" or "erroring". + if stream.state != WritableStreamStateWritable && stream.state != WritableStreamStateErroring { + common.Throw(rt, newError(AssertionError, "stream is not writable or erroring")) + } + // 17.2. Set controller.[[started]] to true. + controller.started = true + // 17.3. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). + controller.advanceQueueIfNeeded() + }) + }, + // 18. Upon rejection of startPromise with reason r, + func(r sobek.Value) { + stream.withTransaction(func() { + // 18.1. Assert: stream.[[state]] is "writable" or "erroring". + if stream.state != WritableStreamStateWritable && stream.state != WritableStreamStateErroring { + common.Throw(rt, newError(AssertionError, "stream is not writable or erroring")) + } + // 18.2. Set controller.[[started]] to true. + controller.started = true + // 18.3. Perform ! WritableStreamDealWithRejection(stream, r). + stream.dealWithRejection(r) + }) + }, + ) + if err != nil { + common.Throw(rt, err) + } +} diff --git a/internal/js/modules/k6/experimental/streams/writable_streams_regression_test.go b/internal/js/modules/k6/experimental/streams/writable_streams_regression_test.go new file mode 100644 index 000000000..d99b4a8ce --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/writable_streams_regression_test.go @@ -0,0 +1,213 @@ +package streams + +import ( + "testing" + + "github.com/grafana/sobek" + "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/js/modulestest" +) + +func newStreamsRuntime(t testing.TB) *modulestest.Runtime { + t.Helper() + + rt := modulestest.NewRuntime(t) + m := New().NewModuleInstance(rt.VU) + for k, v := range m.Exports().Named { + require.NoError(t, rt.VU.RuntimeField.Set(k, v)) + } + + return rt +} + +func runStreamScript(t testing.TB, script string) sobek.Value { + t.Helper() + + rt := newStreamsRuntime(t) + value, err := rt.RunOnEventLoop(script) + require.NoError(t, err) + + return value +} + +func runStreamPromiseScript(t testing.TB, script string) sobek.Value { + t.Helper() + + rt := newStreamsRuntime(t) + callback := rt.VU.RegisterCallback() + var result sobek.Value + + require.NoError(t, rt.VU.Runtime().Set("__streamTestDone", func(value sobek.Value) { + result = value + callback(func() error { return nil }) + })) + + _, err := rt.RunOnEventLoop(` +Promise.resolve((async () => { +` + script + ` +})()).then( + value => __streamTestDone(value), + error => __streamTestDone("ERROR:" + (error && error.message || error)) +); +`) + require.NoError(t, err) + require.NotNil(t, result) + + return result +} + +func TestStreamControllerConstructorsThrowWhenCalled(t *testing.T) { + t.Parallel() + + result := runStreamScript(t, ` +const results = []; + +new WritableStream({ + start(controller) { + try { + controller.constructor().error(new Error("boom")); + results.push("writable did not throw"); + } catch (error) { + results.push(error instanceof TypeError); + } + }, +}); + +new ReadableStream({ + start(controller) { + try { + controller.constructor().error(new Error("boom")); + results.push("readable did not throw"); + } catch (error) { + results.push(error instanceof TypeError); + } + }, +}); + +JSON.stringify(results); +`) + + require.Equal(t, `[true,true]`, result.String()) +} + +func TestWritableStreamUnderlyingSinkDictionaryPresence(t *testing.T) { + t.Parallel() + + result := runStreamScript(t, ` +const results = []; + +try { + new WritableStream({ + start: undefined, + write: undefined, + close: undefined, + abort: undefined, + }); + results.push("undefined callbacks ignored"); +} catch (error) { + results.push(error && error.message); +} + +try { + new WritableStream({ type: null }); + results.push("type null accepted"); +} catch (error) { + results.push(error instanceof RangeError); +} + +JSON.stringify(results); +`) + + require.Equal(t, `["undefined callbacks ignored",true]`, result.String()) +} + +func TestWritableStreamReusesControllerObject(t *testing.T) { + t.Parallel() + + result := runStreamPromiseScript(t, ` +let startController; +let writeController; +let marker; + +const stream = new WritableStream({ + start(controller) { + startController = controller; + controller.marker = "kept"; + }, + write(_chunk, controller) { + writeController = controller; + marker = controller.marker; + }, +}); + +const writer = stream.getWriter(); +await writer.write("chunk"); + +return JSON.stringify([writeController === startController, marker]); +`) + + require.Equal(t, `[true,"kept"]`, result.String()) +} + +func TestWritableStreamSizeConversionAbruptCompletionRejectsWrite(t *testing.T) { + t.Parallel() + + result := runStreamPromiseScript(t, ` +const boom = new Error("sz"); +const stream = new WritableStream({ + write() {}, +}, { + size() { + return { + valueOf() { + throw boom; + }, + }; + }, +}); +const writer = stream.getWriter(); + +let writePromise; +try { + writePromise = writer.write("chunk"); +} catch (error) { + return "threw:" + error.message; +} + +const writeResult = await writePromise.then( + () => "write fulfilled", + error => error === boom ? "write rejected with boom" : "write rejected with " + error.message, +); +const closeResult = await writer.close().then( + () => "close fulfilled", + error => error ? "close rejected" : "close rejected without error", +); + +return JSON.stringify([writeResult, closeResult]); +`) + + require.Equal(t, `["write rejected with boom","close rejected"]`, result.String()) +} + +func TestWritableStreamQueuedReadyRejectionWinsOverQueuedFulfillment(t *testing.T) { + t.Parallel() + + result := runStreamPromiseScript(t, ` +const boom = new Error("boom"); +const stream = new WritableStream({ + write(_chunk, controller) { + controller.error(boom); + }, +}, { highWaterMark: 1 }); +const writer = stream.getWriter(); + +await writer.write("chunk").catch(() => {}); + +return writer.ready.then( + () => "ready fulfilled", + error => error === boom ? "ready rejected with boom" : "ready rejected with " + error.message, +); +`) + + require.Equal(t, "ready rejected with boom", result.String()) +} diff --git a/internal/js/modules/k6/experimental/streams/writable_streams_test.go b/internal/js/modules/k6/experimental/streams/writable_streams_test.go new file mode 100644 index 000000000..991dcbabe --- /dev/null +++ b/internal/js/modules/k6/experimental/streams/writable_streams_test.go @@ -0,0 +1,162 @@ +package streams + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/js/compiler" + "go.k6.io/k6/v2/js/modulestest" +) + +func TestWritableStreamUsesCanonicalPrototypes(t *testing.T) { + t.Parallel() + + runtime := modulestest.NewRuntime(t) + err := runtime.SetupModuleSystem( + map[string]any{"k6/experimental/streams": New()}, + nil, + compiler.New(runtime.VU.InitEnv().Logger), + ) + require.NoError(t, err) + + _, err = runtime.VU.Runtime().RunString(` +const { + WritableStream, + WritableStreamDefaultWriter, +} = require("k6/experimental/streams"); + +function assert(condition, message) { + if (!condition) { + throw new Error(message); + } +} + +function assertThrowsTypeError(fn, message) { + try { + fn(); + } catch (error) { + assert(error instanceof TypeError, message + ": expected TypeError, got " + error); + return; + } + throw new Error(message + ": expected function to throw"); +} + +assert( + Object.prototype.hasOwnProperty.call(WritableStream.prototype, "locked"), + "WritableStream.prototype must be initialized when the module is instantiated", +); +assert( + Object.prototype.hasOwnProperty.call(WritableStreamDefaultWriter.prototype, "closed"), + "WritableStreamDefaultWriter.prototype must be initialized when the module is instantiated", +); + +const stream = new WritableStream({ write() {} }); +const writer = stream.getWriter(); + +assert(stream instanceof WritableStream, "stream must use the exported constructor prototype"); +assert(writer instanceof WritableStreamDefaultWriter, "writer must use the exported constructor prototype"); +assert( + Object.getPrototypeOf(writer) === WritableStreamDefaultWriter.prototype, + "vended writer must use WritableStreamDefaultWriter.prototype", +); +assert( + writer.constructor === WritableStreamDefaultWriter, + "vended writer must retain the exported constructor", +); + +const locked = Object.getOwnPropertyDescriptor(WritableStream.prototype, "locked"); +assert(locked.configurable === true, "locked must be configurable"); +assert(locked.enumerable === true, "locked must be enumerable"); + +const closed = Object.getOwnPropertyDescriptor(WritableStreamDefaultWriter.prototype, "closed"); +assert(closed.configurable === true, "closed must be configurable"); +assert(closed.enumerable === true, "closed must be enumerable"); + +const write = Object.getOwnPropertyDescriptor(WritableStreamDefaultWriter.prototype, "write"); +assert(write.writable === true, "write must be writable"); +assert(write.configurable === true, "write must be configurable"); +assert(write.enumerable === true, "write must be enumerable"); + +writer.releaseLock(); + +assertThrowsTypeError( + () => WritableStream({}), + "stream constructor must require new", +); +assertThrowsTypeError( + () => WritableStreamDefaultWriter(stream), + "writer constructor must require new", +); + +assertThrowsTypeError( + () => WritableStreamDefaultWriter.call({}, stream), + "writer constructor must reject a foreign receiver", +); +assertThrowsTypeError( + () => WritableStream.call({}, {}), + "stream constructor must reject a foreign receiver", +); + +function ForeignStream() {} +Reflect.construct(WritableStream, [{}], ForeignStream); +assert( + !Object.prototype.hasOwnProperty.call(ForeignStream.prototype, "locked"), + "derived construction must not modify the new target prototype", +); + +function ForeignWriter() {} +Reflect.construct(WritableStreamDefaultWriter, [stream], ForeignWriter); +for (const name of ["closed", "desiredSize", "ready", "abort", "close", "releaseLock", "write"]) { + assert( + !Object.prototype.hasOwnProperty.call(ForeignWriter.prototype, name), + "derived construction must not install " + name + " on the new target prototype", + ); +} + +for (const name of ["locked", "closed", "desiredSize", "ready", "abort", "close", "releaseLock", "write"]) { + assert( + !Object.prototype.hasOwnProperty.call(Object.prototype, name), + "constructor calls must not install " + name + " on Object.prototype", + ); +} +`) + require.NoError(t, err) +} + +func TestWritableStream(t *testing.T) { + t.Parallel() + if _, err := os.Stat(webPlatformTestSuite); err != nil { //nolint:forbidigo + t.Skipf("If you want to run Streams tests, you need to run the 'checkout.sh` script in the directory to get "+ + "https://github.com/web-platform-tests/wpt at the correct last tested commit (%v)", err) + } + + suites := []string{ + "aborting.any.js", + "bad-strategies.any.js", + "bad-underlying-sinks.any.js", + "close.any.js", + "constructor.any.js", + "count-queuing-strategy.any.js", + "error.any.js", + "floating-point-total-queue-size.any.js", + "general.any.js", + "properties.any.js", + "reentrant-strategy.any.js", + "start.any.js", + "write.any.js", + } + + for _, suite := range suites { + t.Run(suite, func(t *testing.T) { + t.Parallel() + ts := newConfiguredRuntime(t) + gotErr := ts.EventLoop.Start(func() error { + return executeTestScript(ts.VU, webPlatformTestSuite+"/streams/writable-streams", suite) + }) + assert.NoError(t, gotErr) + }) + } +} diff --git a/internal/js/modules/k6/fork_group_outcome_test.go b/internal/js/modules/k6/fork_group_outcome_test.go new file mode 100644 index 000000000..eaf225834 --- /dev/null +++ b/internal/js/modules/k6/fork_group_outcome_test.go @@ -0,0 +1,99 @@ +package k6 + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/metrics" +) + +func TestGroupTransactionOutcome(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + callback string + async bool + failed bool + }{ + {"sync success", `() => 42`, false, false}, + {"sync throw", `() => { throw new Error('failed'); }`, false, true}, + {"sync with feature", `() => 42`, true, false}, + {"sync throw with feature", `() => { throw new Error('failed'); }`, true, true}, + {"async success", `async () => { await Promise.resolve(); return 42; }`, true, false}, + {"async rejection", `async () => { await Promise.resolve(); throw new Error('failed'); }`, true, true}, + {"primitive rejection", `() => Promise.reject(undefined)`, true, true}, + {"caught rejection", `async () => { try { await Promise.reject('failed'); } catch (_) {} }`, true, false}, + {"failed check", `async () => { k6.check(false, {bad: value => value}); }`, true, false}, + {"check then throw", `async () => { if (!k6.check(false, {bad: v => v})) throw 'bad'; }`, true, true}, + {"throwing getter", `() => ({get then() { throw 'failed'; }})`, true, true}, + {"thenable resolve", `() => ({then(resolve) { resolve(42); }})`, true, false}, + {"thenable reject", `() => ({then(_, reject) { reject('failed'); }})`, true, true}, + {"thenable throw", `() => ({then() { throw 'failed'; }})`, true, true}, + {"first settlement wins", `() => ({then(resolve, reject) { resolve(42); reject('late'); }})`, true, false}, + } + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + tc := testCaseRuntimeWithAsyncMetricContext(t, test.async) + _, err := tc.testRuntime.RunOnEventLoop(fmt.Sprintf(` + (async () => { + let failed = false; + try { await k6.group('step', %s); } catch (_) { failed = true; } + if (failed !== %t) throw new Error('callback outcome changed'); + })() + `, test.callback, test.failed)) + require.NoError(t, err) + var durations []metrics.Sample + for _, container := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range container.GetSamples() { + if sample.Metric.Name == metrics.GroupDurationName { + durations = append(durations, sample) + } + } + } + require.Len(t, durations, 1) + assert.Equal(t, test.failed, durations[0].GroupFailed) + assert.Equal(t, map[string]string{"group": "::step"}, durations[0].Tags.Map()) + assert.GreaterOrEqual(t, durations[0].Value, float64(0)) + }) + } +} + +func TestGroupTransactionOutcomesStayIsolated(t *testing.T) { + t.Parallel() + tc := testCaseRuntime(t) + _, err := tc.testRuntime.RunOnEventLoop(` + (async () => { + await Promise.allSettled([ + k6.group('same', async () => { await Promise.resolve(); throw 'failed'; }), + k6.group('same', async () => { await Promise.resolve(); }), + ]); + await k6.group('parent', async () => { + try { await k6.group('child', async () => { throw 'failed'; }); } catch (_) {} + }); + try { + await k6.group('uncaught', async () => { + await k6.group('child', async () => { throw 'failed'; }); + }); + } catch (_) {} + })() + `) + require.NoError(t, err) + outcomes := map[string][]bool{} + for _, container := range metrics.GetBufferedSamples(tc.samples) { + for _, sample := range container.GetSamples() { + if sample.Metric.Name == metrics.GroupDurationName { + group, _ := sample.Tags.Get("group") + outcomes[group] = append(outcomes[group], sample.GroupFailed) + } + } + } + assert.Equal(t, map[string][]bool{ + "::same": {true, false}, "::parent": {false}, "::parent::child": {true}, + "::uncaught": {true}, "::uncaught::child": {true}, + }, outcomes) +} diff --git a/internal/js/modules/k6/grpc/client.go b/internal/js/modules/k6/grpc/client.go index 0d16e567a..82691c731 100644 --- a/internal/js/modules/k6/grpc/client.go +++ b/internal/js/modules/k6/grpc/client.go @@ -17,9 +17,12 @@ import ( "go.k6.io/k6/v2/internal/lib/netext/grpcext" "go.k6.io/k6/v2/js/common" "go.k6.io/k6/v2/js/modules" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/lib/netext" "github.com/bufbuild/protocompile" "github.com/grafana/sobek" + "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" @@ -148,7 +151,10 @@ func decryptPrivateKey(key, password []byte) ([]byte, error) { return key, nil } -func buildTLSConfig(parentConfig *tls.Config, certificate, key []byte, caCertificates [][]byte) (*tls.Config, error) { +func buildTLSConfig( + parentConfig *tls.Config, certificate, key []byte, caCertificates [][]byte, + aiaFetcher *netext.AIAFetcher, insecureSkipVerify bool, logger logrus.FieldLogger, +) (*tls.Config, error) { var cp *x509.CertPool if len(caCertificates) > 0 { cp, _ = x509.SystemCertPool() @@ -162,9 +168,12 @@ func buildTLSConfig(parentConfig *tls.Config, certificate, key []byte, caCertifi // Ignoring 'TLS MinVersion is too low' because this tls.Config will inherit MinValue and MaxValue // from the vu state tls.Config + // Take insecureSkipVerify from the user's option, not parentConfig — the AIA wrapper + // forces parentConfig.InsecureSkipVerify to true internally, so inheriting silently + // disables verification. tlsCfg := &tls.Config{ CipherSuites: parentConfig.CipherSuites, - InsecureSkipVerify: parentConfig.InsecureSkipVerify, //nolint:gosec + InsecureSkipVerify: insecureSkipVerify, //nolint:gosec MinVersion: parentConfig.MinVersion, MaxVersion: parentConfig.MaxVersion, Renegotiation: parentConfig.Renegotiation, @@ -177,10 +186,17 @@ func buildTLSConfig(parentConfig *tls.Config, certificate, key []byte, caCertifi } tlsCfg.Certificates = []tls.Certificate{cert} } + // Re-wrap so the verification closure reads this config's RootCAs, not the VU's. + if aiaFetcher != nil { + tlsCfg = aiaFetcher.Wrap(tlsCfg, logger) + } return tlsCfg, nil } -func buildTLSConfigFromMap(parentConfig *tls.Config, tlsConfigMap map[string]any) (*tls.Config, error) { +func buildTLSConfigFromMap( + parentConfig *tls.Config, tlsConfigMap map[string]any, + aiaFetcher *netext.AIAFetcher, insecureSkipVerify bool, logger logrus.FieldLogger, +) (*tls.Config, error) { var cert, key, pass []byte var ca [][]byte var err error @@ -212,7 +228,21 @@ func buildTLSConfigFromMap(parentConfig *tls.Config, tlsConfigMap map[string]any ca = [][]byte{[]byte(caCertStr)} } } - return buildTLSConfig(parentConfig, cert, key, ca) + return buildTLSConfig(parentConfig, cert, key, ca, aiaFetcher, insecureSkipVerify, logger) +} + +// buildTLSConfigForConnect builds the per-connect TLS config from the state and TLS map. +// +// TODO: this creates a fresh AIAFetcher per grpc.connect() call, so its cache and +// singleflight group don't carry over between connect calls in the same VU. gRPC's +// AIA use is niche, so acceptable for now; revisit by threading the Runner-level +// fetcher through lib.State. +func buildTLSConfigForConnect(parent *tls.Config, tlsMap map[string]any, state *lib.State) (*tls.Config, error) { + var aiaFetcher *netext.AIAFetcher + if state.Options.TLSAIAFetch.Bool { + aiaFetcher = netext.NewAIAFetcher(state.Dialer.DialContext) + } + return buildTLSConfigFromMap(parent, tlsMap, aiaFetcher, state.Options.InsecureSkipTLSVerify.Bool, state.Logger) } // Connect is a block dial to the gRPC server at the given address (host:port) @@ -233,7 +263,8 @@ func (c *Client) Connect(addr string, params sobek.Value) (bool, error) { if !p.IsPlaintext { tlsCfg := state.TLSConfig.Clone() if len(p.TLS) > 0 { - if tlsCfg, err = buildTLSConfigFromMap(tlsCfg, p.TLS); err != nil { + tlsCfg, err = buildTLSConfigForConnect(tlsCfg, p.TLS, state) + if err != nil { return false, err } } diff --git a/internal/js/modules/k6/grpc/client_aia_test.go b/internal/js/modules/k6/grpc/client_aia_test.go new file mode 100644 index 000000000..a0102cf58 --- /dev/null +++ b/internal/js/modules/k6/grpc/client_aia_test.go @@ -0,0 +1,197 @@ +package grpc + +import ( + "context" + "crypto/tls" + "crypto/x509" + "encoding/pem" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/lib/testutils/tlstest" + "go.k6.io/k6/v2/lib/netext" +) + +// AIA verification must honour per-connect cacerts, not the VU config's RootCAs. +func TestBuildTLSConfig_AIAWithCustomCACerts(t *testing.T) { + t.Parallel() + + chain := tlstest.NewChain(t) + + aiaSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/pkix-cert") + _, _ = w.Write(chain.IntermediateDER) + })) + t.Cleanup(aiaSrv.Close) + chain.SetAIAURL(aiaSrv.URL) + + tlsListener := newLeafOnlyTLSListener(t, chain) + t.Cleanup(func() { _ = tlsListener.Close() }) + + // VU config has no RootCAs — the user relies on per-connect cacerts. + vuCfg := &tls.Config{MinVersion: tls.VersionTLS12} + wrappedVU := netext.NewAIAFetcher(nil).Wrap(vuCfg, nullLogger()) + + rootPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: chain.RootDER}) + tlsCfg, err := buildTLSConfig(wrappedVU, nil, nil, [][]byte{rootPEM}, netext.NewAIAFetcher(nil), false, nullLogger()) + require.NoError(t, err) + + tlsCfg.ServerName = "localhost" + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + dialer := &tls.Dialer{Config: tlsCfg} + conn, err := dialer.DialContext(ctx, "tcp", tlsListener.Addr().String()) + require.NoError(t, err) + _ = conn.Close() +} + +func TestBuildTLSConfig_InsecureSkipVerifyIsHonoured(t *testing.T) { + t.Parallel() + + chain := tlstest.NewChain(t) + chain.SetAIAURL("http://127.0.0.1:1") // never reached; skip short-circuits + + tlsListener := newLeafOnlyTLSListener(t, chain) + t.Cleanup(func() { _ = tlsListener.Close() }) + + // Unrelated CA — verification would fail if it ran. + otherChain := tlstest.NewChain(t) + otherRootPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: otherChain.RootDER}) + + for _, aiaEnabled := range []bool{false, true} { + name := "aia_off" + if aiaEnabled { + name = "aia_on" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + + vuCfg := &tls.Config{ + MinVersion: tls.VersionTLS12, + InsecureSkipVerify: true, + } + parent := vuCfg + if aiaEnabled { + parent = netext.NewAIAFetcher(nil).Wrap(vuCfg, nullLogger()) + } + + var aiaFetcher *netext.AIAFetcher + if aiaEnabled { + aiaFetcher = netext.NewAIAFetcher(nil) + } + tlsCfg, err := buildTLSConfig(parent, nil, nil, [][]byte{otherRootPEM}, aiaFetcher, true, nullLogger()) + require.NoError(t, err) + require.True(t, tlsCfg.InsecureSkipVerify, "user asked to skip verification; must be honoured") + tlsCfg.ServerName = "localhost" + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + dialer := &tls.Dialer{Config: tlsCfg} + conn, err := dialer.DialContext(ctx, "tcp", tlsListener.Addr().String()) + require.NoError(t, err, "handshake must succeed with insecureSkipTLSVerify even against a mismatched CA") + _ = conn.Close() + }) + } +} + +// Guard against silent insecurity: with AIA enabled and mismatched cacerts, the dial +// must fail — even though the AIA wrapper flips InsecureSkipVerify internally. +func TestBuildTLSConfig_AIARejectsMismatchedCACert(t *testing.T) { + t.Parallel() + + chain := tlstest.NewChain(t) + + aiaSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/pkix-cert") + _, _ = w.Write(chain.IntermediateDER) + })) + t.Cleanup(aiaSrv.Close) + chain.SetAIAURL(aiaSrv.URL) + + tlsListener := newLeafOnlyTLSListener(t, chain) + t.Cleanup(func() { _ = tlsListener.Close() }) + + // Unrelated root — the caller supplies this as cacerts; the server presents a chain + // rooted in a different CA, so verification must fail. + otherChain := tlstest.NewChain(t) + otherRootPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: otherChain.RootDER}) + + vuCfg := &tls.Config{MinVersion: tls.VersionTLS12} + wrappedVU := netext.NewAIAFetcher(nil).Wrap(vuCfg, nullLogger()) + + tlsCfg, err := buildTLSConfig(wrappedVU, nil, nil, [][]byte{otherRootPEM}, netext.NewAIAFetcher(nil), false, nullLogger()) + require.NoError(t, err) + tlsCfg.ServerName = "localhost" + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + dialer := &tls.Dialer{Config: tlsCfg} + conn, err := dialer.DialContext(ctx, "tcp", tlsListener.Addr().String()) + if conn != nil { + _ = conn.Close() + } + require.Error(t, err, "AIA-enabled gRPC dial must fail when cacerts do not cover the server chain") + require.True(t, + strings.Contains(err.Error(), "unknown authority") || + strings.Contains(err.Error(), "signed by"), + "unexpected error: %v", err, + ) +} + +// newLeafOnlyTLSListener starts a raw tls.Listener serving only the leaf certificate. +func newLeafOnlyTLSListener(t testing.TB, c *tlstest.Chain) net.Listener { + t.Helper() + + leafCert, err := tls.X509KeyPair( + pemEncodeCert(c.LeafTLSCertificate(t).Certificate[0]), + pemEncodeKey(t, c), + ) + require.NoError(t, err) + + listener, err := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{ + Certificates: []tls.Certificate{leafCert}, + MinVersion: tls.VersionTLS12, + }) + require.NoError(t, err) + + go func() { + for { + conn, acceptErr := listener.Accept() + if acceptErr != nil { + return + } + if tlsConn, ok := conn.(*tls.Conn); ok { + hctx, hcancel := context.WithTimeout(context.Background(), 5*time.Second) + _ = tlsConn.HandshakeContext(hctx) + hcancel() + } + _ = conn.Close() + } + }() + return listener +} + +func pemEncodeCert(der []byte) []byte { + return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) +} + +func pemEncodeKey(t testing.TB, c *tlstest.Chain) []byte { + t.Helper() + keyDER, err := x509.MarshalECPrivateKey(c.LeafKey) + require.NoError(t, err) + return pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}) +} + +func nullLogger() logrus.FieldLogger { + l := logrus.New() + l.SetLevel(logrus.PanicLevel) + return l +} diff --git a/internal/js/modules/k6/grpc/client_test.go b/internal/js/modules/k6/grpc/client_test.go index a42da69b5..13280dc03 100644 --- a/internal/js/modules/k6/grpc/client_test.go +++ b/internal/js/modules/k6/grpc/client_test.go @@ -1279,8 +1279,8 @@ func TestClient_TlsParameters(t *testing.T) { clientAuthCA := []byte("-----BEGIN CERTIFICATE-----\nMIIBWzCCAQGgAwIBAgIJAIQMBgLi+DV6MAoGCCqGSM49BAMCMBAxDjAMBgNVBAMM\nBU15IENBMCAXDTIyMDEyMTEyMjkzNloYDzMwMjEwNTI0MTIyOTM2WjAQMQ4wDAYD\nVQQDDAVNeSBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHnrghULHa2hSa/C\nWimwCn42KWdlPqd6/zs3JgLIxTvBHJJlfbhWbBqtybqyovWd3QykHMIpx0NZmpYn\nG8FoWpmjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1Ud\nDgQWBBSkukBA8lgFvvBJAYKsoSUR+PX71jAKBggqhkjOPQQDAgNIADBFAiEAiFF7\nY54CMNRSBSVMgd4mQgrzJInRH88KpLsQ7VeOAaQCIEa0vaLln9zxIDZQKocml4Db\nAEJr8tDzMKIds6sRTBT4\n-----END CERTIFICATE-----") clientAuth := "-----BEGIN CERTIFICATE-----\\nMIIBVzCB/6ADAgECAgkAg/SeNG3XqB0wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\\nTXkgQ0EwIBcNMjIwMTIxMTUxMjM0WhgPMzAyMTA1MjQxNTEyMzRaMBExDzANBgNV\\nBAMMBmNsaWVudDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABKM7OJQMYG4KLtDA\\ngZ8zOg2PimHMmQnjD2HtI4cSwIUJJnvHWLowbFe9fk6XeP9b3dK1ImUI++/EZdVr\\nABAcngejPzA9MA4GA1UdDwEB/wQEAwIBBjAMBgNVHRMBAf8EAjAAMB0GA1UdDgQW\\nBBSttJe1mcPEnBOZ6wvKPG4zL0m1CzAKBggqhkjOPQQDAgNHADBEAiBPSLgKA/r9\\nu/FW6W+oy6Odm1kdNMGCI472iTn545GwJgIgb3UQPOUTOj0IN4JLJYfmYyXviqsy\\nzk9eWNHFXDA9U6U=\\n-----END CERTIFICATE-----" - clientAuthKey := ("-----BEGIN EC PRIVATE KEY-----\\nMHcCAQEEINDaMGkOT3thu1A0LfLJr3Jd011/aEG6OArmEQaujwgpoAoGCCqGSM49\\nAwEHoUQDQgAEozs4lAxgbgou0MCBnzM6DY+KYcyZCeMPYe0jhxLAhQkme8dYujBs\\nV71+Tpd4/1vd0rUiZQj778Rl1WsAEByeBw==\\n-----END EC PRIVATE KEY-----") - clientAuthKeyEncrypted := ("-----BEGIN EC PRIVATE KEY-----\\nProc-Type: 4,ENCRYPTED\\nDEK-Info: AES-256-CBC,3E311E9B602231BFB5C752071EE7D652\\n\\nsAKeqbacug0v4ruE1A0CACwGVEGBQVOl1CiGVp5RsxgNZKXzMS6EsTTNLw378coF\\nKXbF+he05HIuzToOz2ANLXov1iCrVpotKVB4l2obTQvg+5VET902ky99Mc9Us7jd\\nUwW8LpXlSlhcNWuUfK6wyosL42TbcIxjqZWaESW+6ww=\\n-----END EC PRIVATE KEY-----") + clientAuthKey := "-----BEGIN EC PRIVATE KEY-----\\nMHcCAQEEINDaMGkOT3thu1A0LfLJr3Jd011/aEG6OArmEQaujwgpoAoGCCqGSM49\\nAwEHoUQDQgAEozs4lAxgbgou0MCBnzM6DY+KYcyZCeMPYe0jhxLAhQkme8dYujBs\\nV71+Tpd4/1vd0rUiZQj778Rl1WsAEByeBw==\\n-----END EC PRIVATE KEY-----" + clientAuthKeyEncrypted := "-----BEGIN EC PRIVATE KEY-----\\nProc-Type: 4,ENCRYPTED\\nDEK-Info: AES-256-CBC,3E311E9B602231BFB5C752071EE7D652\\n\\nsAKeqbacug0v4ruE1A0CACwGVEGBQVOl1CiGVp5RsxgNZKXzMS6EsTTNLw378coF\\nKXbF+he05HIuzToOz2ANLXov1iCrVpotKVB4l2obTQvg+5VET902ky99Mc9Us7jd\\nUwW8LpXlSlhcNWuUfK6wyosL42TbcIxjqZWaESW+6ww=\\n-----END EC PRIVATE KEY-----" trivialKeyPassword := "abc123" trivialWrongKeyPassword := "abc321" diff --git a/internal/js/modules/k6/grpc/stream.go b/internal/js/modules/k6/grpc/stream.go index 9942e7854..afbb70169 100644 --- a/internal/js/modules/k6/grpc/stream.go +++ b/internal/js/modules/k6/grpc/stream.go @@ -308,11 +308,26 @@ func (s *stream) on(event string, handler func(sobek.Value, sobek.Value) (sobek. common.Throw(s.vu.Runtime(), fmt.Errorf("handler for %q event isn't a callable function", event)) } - if err := s.eventListeners.add(event, handler); err != nil { + if err := s.eventListeners.add(event, s.wrapUnderCurrentMetricContext(handler)); err != nil { s.vu.State().Logger.Warnf("can't register %s event handler: %s", event, err) } } +// Stream listeners run outside Sobek's promise machinery. +func (s *stream) wrapUnderCurrentMetricContext( + fn func(sobek.Value, sobek.Value) (sobek.Value, error), +) func(sobek.Value, sobek.Value) (sobek.Value, error) { + if !common.AsyncMetricContextEnabled(s.vu.State()) { + return fn + } + captured := common.CaptureMetricContext(s.vu.State()) + return func(v, metadata sobek.Value) (sobek.Value, error) { + return common.RunWithMetricContext(captured, func() (sobek.Value, error) { + return fn(v, metadata) + }) + } +} + // write writes a message to the stream func (s *stream) write(input sobek.Value) { if s.writingState != opened { diff --git a/internal/js/modules/k6/k6.go b/internal/js/modules/k6/k6.go index 9a0366e05..1014011fa 100644 --- a/internal/js/modules/k6/k6.go +++ b/internal/js/modules/k6/k6.go @@ -30,7 +30,8 @@ type ( // K6 represents an instance of the k6 module. K6 struct { - vu modules.VU + vu modules.VU + asyncGroups bool } ) @@ -47,7 +48,17 @@ func New() *RootModule { // NewModuleInstance implements the modules.Module interface to return // a new instance for each VU. func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance { - return &K6{vu: vu} + asyncGroups := asyncGroupsEnabled(vu) + return &K6{vu: vu, asyncGroups: asyncGroups} +} + +func asyncGroupsEnabled(vu modules.VU) bool { + if state := vu.State(); state != nil { + return common.AsyncMetricContextEnabled(state) + } + initEnv := vu.InitEnv() + return initEnv != nil && initEnv.TestPreInitState != nil && + initEnv.FeatureFlags != nil && initEnv.FeatureFlags.AsyncMetricContext } // Exports returns the exports of the k6 module. @@ -85,7 +96,10 @@ func (mi *K6) RandomSeed(seed int64) { mi.vu.Runtime().SetRandSource(randSource) } -// Group wraps a function call and executes it within the provided group name. +// Group executes a callback under the provided group name. +// +// With the async-metric-context feature enabled, async callbacks retain their metric +// context through promise reactions and group_duration covers their lifetime. func (mi *K6) Group(name string, val sobek.Value) (sobek.Value, error) { state := mi.vu.State() if state == nil { @@ -99,12 +113,15 @@ func (mi *K6) Group(name string, val sobek.Value) (sobek.Value, error) { if !ok { return nil, errors.New("group() requires a callback as a second argument") } - if common.IsAsyncFunction(mi.vu.Runtime(), val) { - return sobek.Undefined(), errors.New("group() does not support async functions as arguments, " + + if !mi.asyncGroups && common.IsAsyncFunction(mi.vu.Runtime(), val) { + return sobek.Undefined(), errors.New("group() does not support async functions as arguments " + + "unless the async-metric-context experimental feature is enabled " + + "(pass --features async-metric-context or set K6_FEATURES=async-metric-context), " + "please see https://grafana.com/docs/k6/latest/javascript-api/k6/group/ for more info") } - oldGroupName, _ := state.Tags.GetCurrentValues().Tags.Get(metrics.TagGroup.String()) - // TODO: what are we doing if group is not tagged + + parentTagsAndMeta := state.Tags.GetCurrentValues() + oldGroupName, _ := parentTagsAndMeta.Tags.Get(metrics.TagGroup.String()) newGroupName, err := lib.NewGroupPath(oldGroupName, name) if err != nil { return sobek.Undefined(), err @@ -112,35 +129,68 @@ func (mi *K6) Group(name string, val sobek.Value) (sobek.Value, error) { shouldUpdateTag := state.Options.SystemTags.Has(metrics.TagGroup) if shouldUpdateTag { - state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { - tagsAndMeta.SetSystemTagOrMeta(metrics.TagGroup, newGroupName) - }) + setGroupTag(state, newGroupName) + } + var groupTagsAndMeta metrics.TagsAndMeta + if mi.asyncGroups { + groupTagsAndMeta = state.Tags.GetCurrentValues() } + synchronous := true + // Default to failure so a thrown property getter or other unwinding path is not + // exported as a successful BreakTest transaction. + groupSucceeded := false + var startTime, endTime time.Time defer func() { - if shouldUpdateTag { + if synchronous { + if endTime.IsZero() { + endTime = time.Now() + } + durationTagsAndMeta := groupTagsAndMeta + if !mi.asyncGroups { + durationTagsAndMeta = state.Tags.GetCurrentValues() + } + emitGroupDuration(mi.vu, startTime, endTime, durationTagsAndMeta, !groupSucceeded) + } + if mi.asyncGroups { state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { - tagsAndMeta.SetSystemTagOrMeta(metrics.TagGroup, oldGroupName) + *tagsAndMeta = parentTagsAndMeta }) + } else if shouldUpdateTag { + setGroupTag(state, oldGroupName) } }() - startTime := time.Now() + startTime = time.Now() ret, err := fn(sobek.Undefined()) - t := time.Now() + endTime = time.Now() + if err != nil || !mi.asyncGroups { + groupSucceeded = err == nil + return ret, err + } - ctx := mi.vu.Context() - ctm := state.Tags.GetCurrentValues() - metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{ - TimeSeries: metrics.TimeSeries{ - Metric: state.BuiltinMetrics.GroupDuration, - Tags: ctm.Tags, - }, - Time: t, - Value: metrics.D(t.Sub(startTime)), - Metadata: ctm.Metadata, - }) + thenFn, isThenable := asThenable(ret) + if !isThenable { + groupSucceeded = true + return ret, nil + } - return ret, err + asyncResult, settled, err := mi.runAsyncGroup(startTime, groupTagsAndMeta, thenFn, ret) + if err != nil { + // A custom thenable may invoke a settlement handler synchronously. In that case the handler + // emitted the duration before its rejection propagated back through then(). + synchronous = !settled + return nil, err + } + // Settlement handlers now own duration emission. Until they are installed successfully, the + // deferred synchronous path must retain ownership so an initialization error still emits once. + synchronous = false + return asyncResult, nil +} + +func setGroupTag(state *lib.State, name string) { + state.Tags.Modify(func(tagsAndMeta *metrics.TagsAndMeta) { + tagsAndMeta.SetSystemTagOrMeta(metrics.TagGroup, name) + }) } // Check will emit check metrics for the provided checks. diff --git a/internal/js/modules/k6/k6_test.go b/internal/js/modules/k6/k6_test.go index 19d493f02..08094ca7b 100644 --- a/internal/js/modules/k6/k6_test.go +++ b/internal/js/modules/k6/k6_test.go @@ -5,9 +5,12 @@ import ( "testing" "time" + "github.com/grafana/sobek" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/internal/features" + "go.k6.io/k6/v2/js/common" "go.k6.io/k6/v2/js/modulestest" "go.k6.io/k6/v2/lib" "go.k6.io/k6/v2/metrics" @@ -87,6 +90,20 @@ func TestRandSeed(t *testing.T) { func TestGroup(t *testing.T) { t.Parallel() + t.Run("init context", func(t *testing.T) { + t.Parallel() + rt := sobek.New() + vu := &modulestest.VU{ + RuntimeField: rt, + InitEnvField: &common.InitEnvironment{}, + } + mi, ok := New().NewModuleInstance(vu).(*K6) + require.True(t, ok) + + _, err := mi.Group("group", rt.ToValue(func() {})) + require.ErrorIs(t, err, ErrGroupInInitContext) + }) + t.Run("Valid", func(t *testing.T) { t.Parallel() tc := testCaseRuntime(t) @@ -113,15 +130,32 @@ func TestGroup(t *testing.T) { t.Run("async function", func(t *testing.T) { t.Parallel() tc := testCaseRuntime(t) - _, err := tc.testRuntime.RunOnEventLoop(`k6.group("something", async function() { })`) - assert.ErrorContains(t, err, "group() does not support async functions as arguments") + state := tc.testRuntime.VU.State() + require.NoError(t, tc.testRuntime.VU.Runtime().Set("fn", func() { + groupTag, ok := state.Tags.GetCurrentValues().Tags.Get("group") + require.True(t, ok) + assert.Equal(t, "::something", groupTag) + })) + _, err := tc.testRuntime.RunOnEventLoop( + `k6.group("something", async function() { fn(); await Promise.resolve(); })`) + assert.NoError(t, err) + groupTag, ok := state.Tags.GetCurrentValues().Tags.Get("group") + require.True(t, ok) + assert.Equal(t, "", groupTag) }) t.Run("async lambda", func(t *testing.T) { t.Parallel() tc := testCaseRuntime(t) _, err := tc.testRuntime.RunOnEventLoop(`k6.group("something", async () => { })`) - assert.ErrorContains(t, err, "group() does not support async functions as arguments") + assert.NoError(t, err) + }) + + t.Run("async function disabled", func(t *testing.T) { + t.Parallel() + tc := testCaseRuntimeWithAsyncMetricContext(t, false) + _, err := tc.testRuntime.RunOnEventLoop(`k6.group("something", async () => {})`) + assert.ErrorContains(t, err, "group() does not support async functions") }) } @@ -372,7 +406,13 @@ type testCase struct { } func testCaseRuntime(t testing.TB) *testCase { + return testCaseRuntimeWithAsyncMetricContext(t, true) +} + +func testCaseRuntimeWithAsyncMetricContext(t testing.TB, enabled bool) *testCase { testRuntime := modulestest.NewRuntime(t) + featureFlags := &features.Flags{AsyncMetricContext: enabled} + testRuntime.VU.InitEnvField.FeatureFlags = featureFlags m, ok := New().NewModuleInstance(testRuntime.VU).(*K6) require.True(t, ok) require.NoError(t, testRuntime.VU.RuntimeField.Set("k6", m.Exports().Named)) @@ -380,6 +420,7 @@ func testCaseRuntime(t testing.TB) *testCase { registry := metrics.NewRegistry() samples := make(chan metrics.SampleContainer, 1000) state := &lib.State{ + FeatureFlags: featureFlags, Options: lib.Options{ SystemTags: &metrics.DefaultSystemTagSet, }, @@ -388,6 +429,11 @@ func testCaseRuntime(t testing.TB) *testCase { BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry), } testRuntime.MoveToVUContext(state) + if enabled { + testRuntime.VU.RuntimeField.SetAsyncContextTracker( + common.NewMetricContextTracker(testRuntime.VU.State), + ) + } return &testCase{ samples: samples, diff --git a/internal/js/modules/k6/webcrypto/crypto.go b/internal/js/modules/k6/webcrypto/crypto.go index 1f054e96d..150504148 100644 --- a/internal/js/modules/k6/webcrypto/crypto.go +++ b/internal/js/modules/k6/webcrypto/crypto.go @@ -36,6 +36,15 @@ type Crypto struct { // // [specification]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues func (c *Crypto) GetRandomValues(typedArray sobek.Value) sobek.Value { + // The typedArray parameter is not optional in the specification's WebIDL + // definition, so reject a missing or nullish one before IsInstanceOf gets + // to dereference it. This is an ECMAScript TypeError rather than one of the + // WebCrypto error names, which is what browsers and Node throw here, and + // what a nullish argument already produced before this check existed. + if common.IsNullish(typedArray) { + panic(c.vu.Runtime().NewTypeError("typedArray parameter is required")) + } + acceptedTypes := []JSType{ Int8ArrayConstructor, Uint8ArrayConstructor, @@ -61,6 +70,12 @@ func (c *Crypto) GetRandomValues(typedArray sobek.Value) sobek.Value { common.Throw(c.vu.Runtime(), NewError(TypeMismatchError, "typedArray parameter isn't a TypedArray instance")) } + // The length property is script-controlled and can be overridden with a + // negative value, which would make the make() call below panic. + if objLength < 0 { + panic(c.vu.Runtime().NewTypeError("typedArray parameter's length is negative")) + } + if objLength > maxRandomValuesLength { common.Throw( c.vu.Runtime(), diff --git a/internal/js/modules/k6/webcrypto/crypto_test.go b/internal/js/modules/k6/webcrypto/crypto_test.go new file mode 100644 index 000000000..b6253b2a2 --- /dev/null +++ b/internal/js/modules/k6/webcrypto/crypto_test.go @@ -0,0 +1,119 @@ +// Note: this test lives in the webcrypto_test package because js/modulestest +// imports webcrypto, so an in-package test would create an import cycle. +package webcrypto_test + +import ( + "fmt" + "testing" + + "github.com/grafana/sobek" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/js/modulestest" +) + +// catchScript runs the given statements and reports what the script saw, so the +// assertions can look at the thrown value the way a user's test would, instead +// of at the Go error RunString wraps it in. +const catchScript = ` + (function () { + try { + %s + return { threw: false }; + } catch (e) { + return { threw: true, isTypeError: e instanceof TypeError, text: String(e) }; + } + })() +` + +// TestGetRandomValuesRejectsBadInput asserts that bad input to +// crypto.getRandomValues() is reported to the script as a catchable error, +// instead of taking the whole k6 process down with a Go panic. +// +// See https://github.com/grafana/k6/issues/6320 +func TestGetRandomValuesRejectsBadInput(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + script string + // wantTypeError is true when the script should see a genuine ECMAScript + // TypeError, so that `e instanceof TypeError` holds the way it does in + // browsers and in Node. The WebCrypto error names are not ECMAScript + // errors, so they are reported as GoError instead. + wantTypeError bool + // wantMessage is a substring of the thrown value's string form. + wantMessage string + }{ + { + // GetRandomValues receives a nil sobek.Value, and IsInstanceOf + // dereferences it. The WebIDL argument is required, so browsers + // throw a TypeError here. + name: "no argument", + script: `crypto.getRandomValues()`, + wantTypeError: true, + wantMessage: "typedArray parameter is required", + }, + { + // A nullish argument already threw a TypeError before the check + // that fixes the case above existed, so it has to keep throwing + // one, or scripts branching on `e instanceof TypeError` break. + name: "null argument", + script: `crypto.getRandomValues(null)`, + wantTypeError: true, + wantMessage: "typedArray parameter is required", + }, + { + name: "undefined argument", + script: `crypto.getRandomValues(undefined)`, + wantTypeError: true, + wantMessage: "typedArray parameter is required", + }, + { + // Not an ArrayBufferView at all. Today k6 reports this as a + // TypeMismatchError, which the spec reserves for views of the + // wrong element type (Float32Array, DataView, ...). Left as it is, + // since it never panicked and changing it is a separate concern. + name: "not a TypedArray", + script: `crypto.getRandomValues({})`, + wantTypeError: false, + wantMessage: "TypeMismatchError", + }, + { + // The array is a real Uint8Array, so the element type is fine, + // but make([]byte, -1) panics. + name: "length overridden with a negative value", + script: ` + const a = new Uint8Array(4); + Object.defineProperty(a, "length", { value: -1 }); + crypto.getRandomValues(a); + `, + wantTypeError: true, + wantMessage: "typedArray parameter's length is negative", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + // NewRuntime already calls webcrypto.SetupGlobally, so the + // crypto object is available as a global. + rt := modulestest.NewRuntime(t) + + var ( + got sobek.Value + err error + ) + require.NotPanics(t, func() { + got, err = rt.VU.Runtime().RunString(fmt.Sprintf(catchScript, tc.script)) + }) + require.NoError(t, err) + + result := got.ToObject(rt.VU.Runtime()) + require.True(t, result.Get("threw").ToBoolean(), "the script was expected to throw") + assert.Equal(t, tc.wantTypeError, result.Get("isTypeError").ToBoolean()) + assert.Contains(t, result.Get("text").String(), tc.wantMessage) + }) + } +} diff --git a/internal/js/modules/k6/webcrypto/elliptic_curve.go b/internal/js/modules/k6/webcrypto/elliptic_curve.go index 552c97927..f831b9677 100644 --- a/internal/js/modules/k6/webcrypto/elliptic_curve.go +++ b/internal/js/modules/k6/webcrypto/elliptic_curve.go @@ -1,3 +1,4 @@ +//nolint:staticcheck //a bunch of deprecated logic is used in this package. Needs to be fixed. package webcrypto import ( diff --git a/internal/js/modules/k6/webcrypto/jwk.go b/internal/js/modules/k6/webcrypto/jwk.go index 556f950fb..5f1e922d4 100644 --- a/internal/js/modules/k6/webcrypto/jwk.go +++ b/internal/js/modules/k6/webcrypto/jwk.go @@ -1,3 +1,4 @@ +//nolint:staticcheck //a bunch of deprecated logic is used in this package. Needs to be fixed. package webcrypto import ( @@ -161,8 +162,8 @@ func encodeCurveBigInt(data *big.Int, curveBits int) string { // padLeft pads the byte slice with zeros to the left to ensure it has a specific length. func padLeft(bytes []byte, size int) []byte { - padding := make([]byte, size-len(bytes)) - return append(padding, bytes...) //nolint:makezero // we need to pad with zeros + padding := make([]byte, size-len(bytes), size) + return append(padding, bytes...) } func exportECJWK(key *CryptoKey) (any, error) { diff --git a/internal/js/modules/k6/websockets/websockets.go b/internal/js/modules/k6/websockets/websockets.go index 836562351..90529fc9f 100644 --- a/internal/js/modules/k6/websockets/websockets.go +++ b/internal/js/modules/k6/websockets/websockets.go @@ -29,8 +29,9 @@ type RootModule struct{} // WebSocketsAPI is the k6 extension implementing the websocket API as defined in https://websockets.spec.whatwg.org type WebSocketsAPI struct { //nolint:revive - vu modules.VU - blobConstructor sobek.Value + vu modules.VU + blobConstructor sobek.Value + webSocketConstructor sobek.Value } var _ modules.Module = &RootModule{} @@ -42,17 +43,27 @@ func New() *RootModule { // NewModuleInstance returns a new instance of the module func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { - return &WebSocketsAPI{ - vu: vu, + api := &WebSocketsAPI{vu: vu} + + rt := vu.Runtime() + api.blobConstructor = rt.ToValue(api.blob) + + wsConstructor, ok := rt.ToValue(api.websocket).(*sobek.Object) + if !ok { + common.Throw(rt, errors.New("websocket constructor is not a sobek.Object")) } + + defineReadyStateProperties(rt, wsConstructor) + api.webSocketConstructor = wsConstructor + + return api } // Exports implements the modules.Instance interface's Exports func (r *WebSocketsAPI) Exports() modules.Exports { - r.blobConstructor = r.vu.Runtime().ToValue(r.blob) return modules.Exports{ Named: map[string]any{ - "WebSocket": r.websocket, + "WebSocket": r.webSocketConstructor, "Blob": r.blobConstructor, }, } @@ -84,8 +95,14 @@ type webSocket struct { obj *sobek.Object // the object that is given to js to interact with the WebSocket started time.Time - done chan struct{} - writeQueueCh chan message + done chan struct{} + writeQueueCh chan message + connectCancel context.CancelFunc + + connectionMu sync.Mutex + // pendingConnection and publishing conn are guarded by connectionMu. Once connected, it also + // orders successful writes before events for data that the peer can only send after that write. + pendingConnection net.Conn eventListeners *eventListeners @@ -134,6 +151,7 @@ func (r *WebSocketsAPI) websocket(c sobek.ConstructorCall) *sobek.Object { } } + connectionCtx, connectCancel := context.WithCancel(r.vu.Context()) w := &webSocket{ vu: r.vu, blobConstructor: r.blobConstructor, @@ -143,6 +161,7 @@ func (r *WebSocketsAPI) websocket(c sobek.ConstructorCall) *sobek.Object { builtinMetrics: r.vu.State().BuiltinMetrics, done: make(chan struct{}), writeQueueCh: make(chan message), + connectCancel: connectCancel, eventListeners: newEventListeners(), obj: rt.NewObject(), tagsAndMeta: params.tagsAndMeta, @@ -153,7 +172,7 @@ func (r *WebSocketsAPI) websocket(c sobek.ConstructorCall) *sobek.Object { // Maybe have this after the goroutine below ?!? defineWebsocket(rt, w) - go w.establishConnection(params) + go w.establishConnection(connectionCtx, params) return w.obj } @@ -198,7 +217,7 @@ func defineWebsocket(rt *sobek.Runtime, w *webSocket) { "url", rt.ToValue(w.url.String()), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( // this needs to be with an accessor as we change the value "readyState", rt.ToValue(func() sobek.Value { - return rt.ToValue((uint)(w.readyState)) + return rt.ToValue(uint(w.readyState)) }), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) must(rt, w.obj.DefineAccessorProperty( "bufferedAmount", rt.ToValue(func() sobek.Value { return rt.ToValue(w.bufferedAmount) }), nil, @@ -244,7 +263,8 @@ func defineWebsocket(rt *sobek.Runtime, w *webSocket) { common.Throw(rt, fmt.Errorf("a value for '%s' should be callable", property)) } - el.setOn(func(v sobek.Value) (sobek.Value, error) { return fn(sobek.Undefined(), v) }) + el.setOn(w.wrapUnderCurrentMetricContext( + func(v sobek.Value) (sobek.Value, error) { return fn(sobek.Undefined(), v) })) return nil }), sobek.FLAG_FALSE, sobek.FLAG_TRUE)) @@ -256,6 +276,20 @@ func defineWebsocket(rt *sobek.Runtime, w *webSocket) { setOn("onclose", w.eventListeners.getType(events.CLOSE)) setOn("onping", w.eventListeners.getType(events.PING)) setOn("onpong", w.eventListeners.getType(events.PONG)) + + defineReadyStateProperties(rt, w.obj) +} + +// the spec defines constants for the ready state on both the constructor and actual instances +func defineReadyStateProperties(rt *sobek.Runtime, target *sobek.Object) { + must(rt, target.DefineDataProperty( + "OPEN", rt.ToValue(uint(OPEN)), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) + must(rt, target.DefineDataProperty( + "CLOSED", rt.ToValue(uint(CLOSED)), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) + must(rt, target.DefineDataProperty( + "CLOSING", rt.ToValue(uint(CLOSING)), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) + must(rt, target.DefineDataProperty( + "CONNECTING", rt.ToValue(uint(CONNECTING)), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE)) } type message struct { @@ -265,34 +299,19 @@ type message struct { } // documented https://websockets.spec.whatwg.org/#concept-websocket-establish -func (w *webSocket) establishConnection(params *wsParams) { +func (w *webSocket) establishConnection(ctx context.Context, params *wsParams) { + defer w.connectCancel() + state := w.vu.State() w.started = time.Now() - var tlsConfig *tls.Config - if state.TLSConfig != nil { - tlsConfig = state.TLSConfig.Clone() - tlsConfig.NextProtos = []string{"http/1.1"} - } - // technically we have to do a fetch request here, so ... uh do normal one ;) - wsd := websocket.Dialer{ - HandshakeTimeout: time.Second * 60, // TODO configurable - // Pass a custom net.DialContext function to websocket.Dialer that will substitute - // the underlying net.Conn with our own tracked netext.Conn - NetDialContext: state.Dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - TLSClientConfig: tlsConfig, - EnableCompression: params.enableCompression, - Subprotocols: params.subprocotols, - } - - // this is needed because of how interfaces work and that wsd.Jar is http.Cookiejar - if params.cookieJar != nil { - wsd.Jar = params.cookieJar - } - - ctx := w.vu.Context() + handshakeDone := make(chan struct{}) + go w.closePendingConnectionOnContextDone(ctx, handshakeDone) start := time.Now() - conn, httpResponse, connErr := wsd.DialContext(ctx, w.url.String(), params.headers) + conn, httpResponse, connErr := w.dial(ctx, params) + w.connectionMu.Lock() + w.pendingConnection = nil + w.connectionMu.Unlock() + close(handshakeDone) connectionEnd := time.Now() connectionDuration := metrics.D(connectionEnd.Sub(start)) @@ -316,10 +335,8 @@ func (w *webSocket) establishConnection(params *wsParams) { w.extensions = httpResponse.Header.Values("Sec-WebSocket-Extensions") w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagSubproto, w.protocol) } - w.conn = conn - nameTagValue, nameTagManuallySet := params.tagsAndMeta.Tags.Get(metrics.TagName.String()) - // After k6 v0.41.0, the `name` and `url` tags have the exact same values: + // After k6 v0.41.0, the name and URL tags have the same values. if nameTagManuallySet { w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagURL, nameTagValue) } else { @@ -327,7 +344,8 @@ func (w *webSocket) establishConnection(params *wsParams) { w.tagsAndMeta.SetSystemTagOrMetaIfEnabled(systemTags, metrics.TagName, w.url.String()) } - w.emitConnectionMetrics(ctx, start, connectionDuration) + //nolint:contextcheck // Handshake cancellation must not suppress connection metrics. + w.emitConnectionMetrics(w.vu.Context(), start, connectionDuration) if connErr != nil { // Pass the error to the user script before exiting immediately w.tq.Queue(func() error { @@ -336,12 +354,88 @@ func (w *webSocket) establishConnection(params *wsParams) { w.tq.Close() return } + + w.connectionMu.Lock() + if ctx.Err() != nil { + w.connectionMu.Unlock() + _ = conn.Close() + w.tq.Queue(func() error { + return w.connectionClosedWithError(ctx.Err()) + }) + w.tq.Close() + return + } + w.conn = conn + //nolint:contextcheck // An established connection is governed by the longer-lived VU context. go w.loop() + w.connectionMu.Unlock() w.tq.Queue(func() error { return w.connectionConnected() }) } +func (w *webSocket) dial(ctx context.Context, params *wsParams) (*websocket.Conn, *http.Response, error) { + state := w.vu.State() + var tlsConfig *tls.Config + if state.TLSConfig != nil { + tlsConfig = state.TLSConfig.Clone() + tlsConfig.NextProtos = []string{"http/1.1"} + } + // technically we have to do a fetch request here, so ... uh do normal one ;) + wsd := websocket.Dialer{ + HandshakeTimeout: time.Second * 60, // TODO configurable + // Pass a custom net.DialContext function to websocket.Dialer that will substitute + // the underlying net.Conn with our own tracked netext.Conn + NetDialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + conn, err := state.Dialer.DialContext(ctx, network, addr) + if err != nil { + return nil, err + } + + w.connectionMu.Lock() + if ctx.Err() != nil { + w.connectionMu.Unlock() + _ = conn.Close() + return nil, ctx.Err() + } + w.pendingConnection = conn + w.connectionMu.Unlock() + + return conn, nil + }, + Proxy: http.ProxyFromEnvironment, + TLSClientConfig: tlsConfig, + EnableCompression: params.enableCompression, + Subprotocols: params.subprocotols, + } + + // this is needed because of how interfaces work and that wsd.Jar is http.Cookiejar + if params.cookieJar != nil { + wsd.Jar = params.cookieJar + } + + return wsd.DialContext(ctx, w.url.String(), params.headers) +} + +func (w *webSocket) closePendingConnectionOnContextDone(ctx context.Context, handshakeDone <-chan struct{}) { + select { + case <-ctx.Done(): + case <-handshakeDone: + return + } + w.closePendingConnection() +} + +func (w *webSocket) closePendingConnection() { + w.connectionMu.Lock() + conn := w.pendingConnection + w.pendingConnection = nil + w.connectionMu.Unlock() + if conn != nil { + _ = conn.Close() + } +} + // emitConnectionMetrics emits the metrics for a websocket connection. func (w *webSocket) emitConnectionMetrics(ctx context.Context, start time.Time, duration float64) { state := w.vu.State() @@ -392,8 +486,8 @@ func (w *webSocket) loop() { return nil }) - ctx := w.vu.Context() wg := new(sync.WaitGroup) + ctx := w.vu.Context() defer func() { metrics.PushIfNotDone(ctx, w.vu.State().Samples, metrics.Sample{ @@ -510,18 +604,21 @@ func (w *webSocket) readPump(wg *sync.WaitGroup) { for { messageType, data, err := w.conn.ReadMessage() if err == nil { + // An immediate peer response can arrive before WriteMessage() returns. Wait for the + // writer to queue its bufferedAmount update before queuing the corresponding event. + w.connectionMu.Lock() w.queueMessage(&message{ mtype: messageType, data: data, t: time.Now(), }) + w.connectionMu.Unlock() continue } - var closeErr *websocket.CloseError var code int var reason string - if errors.As(err, &closeErr) { + if closeErr, ok := errors.AsType[*websocket.CloseError](err); ok { code = closeErr.Code reason = closeErr.Text } @@ -559,6 +656,10 @@ func (w *webSocket) writePump(wg *sync.WaitGroup) { } size := len(msg.data) + // Lock before writing because the peer can respond while WriteMessage() is still + // returning. Keep the lock through Queue() so readPump cannot queue that response + // ahead of the bufferedAmount update on the JavaScript event loop. + w.connectionMu.Lock() err := func() error { if msg.mtype != websocket.PingMessage { return w.conn.WriteMessage(msg.mtype, msg.data) @@ -567,6 +668,15 @@ func (w *webSocket) writePump(wg *sync.WaitGroup) { // WriteControl is concurrently okay return w.conn.WriteControl(msg.mtype, msg.data, msg.t.Add(writeWait)) }() + if err == nil { + // This from the specification needs to happen like that instead of with + // atomics or locks outside of the event loop. + w.tq.Queue(func() error { + w.bufferedAmount -= size + return nil + }) + } + w.connectionMu.Unlock() if err != nil { w.tq.Queue(func() error { _ = w.conn.Close() // TODO fix @@ -575,12 +685,6 @@ func (w *webSocket) writePump(wg *sync.WaitGroup) { }) return } - // This from the specification needs to happen like that instead of with - // atomics or locks outside of the event loop - w.tq.Queue(func() error { - w.bufferedAmount -= size - return nil - }) metrics.PushIfNotDone(ctx, samplesOutput, metrics.Sample{ TimeSeries: metrics.TimeSeries{ @@ -765,10 +869,6 @@ func isValidCloseReason(reason string) bool { } func (w *webSocket) close(code int, reason string) error { - if w.readyState == CLOSED || w.readyState == CLOSING { - return nil - } - if code != 0 && !isValidClientCloseCode(code) { return fmt.Errorf( "InvalidAccessError: Failed to execute 'close' on 'WebSocket': "+ @@ -782,13 +882,31 @@ func (w *webSocket) close(code int, reason string) error { `SyntaxError: Failed to execute 'close' on 'WebSocket': The message must not be greater than 123 bytes`, ) } - + if w.readyState == CLOSED || w.readyState == CLOSING { + return nil + } if code == 0 { code = websocket.CloseNormalClosure } + w.closeCode = code + wasConnecting := w.readyState == CONNECTING w.readyState = CLOSING - w.closeCode = code + if wasConnecting { + w.connectionMu.Lock() + w.connectCancel() + pendingConnection := w.pendingConnection + w.pendingConnection = nil + connectionEstablished := w.conn != nil + w.connectionMu.Unlock() + + if pendingConnection != nil { + _ = pendingConnection.Close() + } + if !connectionEstablished { + return nil + } + } w.writeQueueCh <- message{ mtype: websocket.CloseMessage, @@ -834,7 +952,7 @@ func (w *webSocket) connectionClosedWithError(err error) error { return w.callEventListeners(events.CLOSE) } -// newEvent return an event implementing "implements" https://dom.spec.whatwg.org/#event +// newEvent returns an event implementing "implements" https://dom.spec.whatwg.org/#event // needs to be called on the event loop // TODO: move to events func (w *webSocket) newEvent(eventType string, t time.Time, funcOptions ...func(*sobek.Object)) *sobek.Object { @@ -926,9 +1044,24 @@ func (w *webSocket) addEventListener(event string, handler func(sobek.Value) (so common.Throw(w.vu.Runtime(), fmt.Errorf("handler for event type %q isn't a callable function", event)) } - if err := w.eventListeners.add(event, handler); err != nil { + if err := w.eventListeners.add(event, w.wrapUnderCurrentMetricContext(handler)); err != nil { w.vu.State().Logger.Warnf("can't add event handler: %s", err) } } +// WebSocket listeners run outside Sobek's promise machinery. +func (w *webSocket) wrapUnderCurrentMetricContext( + fn func(sobek.Value) (sobek.Value, error), +) func(sobek.Value) (sobek.Value, error) { + if !common.AsyncMetricContextEnabled(w.vu.State()) { + return fn + } + captured := common.CaptureMetricContext(w.vu.State()) + return func(v sobek.Value) (sobek.Value, error) { + return common.RunWithMetricContext(captured, func() (sobek.Value, error) { + return fn(v) + }) + } +} + // TODO add remove listeners diff --git a/internal/js/modules/k6/websockets/websockets_test.go b/internal/js/modules/k6/websockets/websockets_test.go index 766f3e9f3..483f7c865 100644 --- a/internal/js/modules/k6/websockets/websockets_test.go +++ b/internal/js/modules/k6/websockets/websockets_test.go @@ -202,6 +202,25 @@ func (ts *testState) addHandler(uri string, upgrader *websocket.Upgrader, messag })) } +func runOnEventLoopWithTimeout(t *testing.T, runtime *modulestest.Runtime, code string) error { + t.Helper() + + result := make(chan error, 1) + go func() { + _, err := runtime.RunOnEventLoop(code) + result <- err + }() + + select { + case err := <-result: + return err + case <-time.After(3 * time.Second): + runtime.CancelContext() + t.Fatal("event loop did not finish") + return nil + } +} + type testMessage struct { kind int data []byte @@ -223,6 +242,145 @@ func TestBasic(t *testing.T) { assertSessionMetricsEmitted(t, samples, "", sr("WSBIN_URL/ws-echo"), http.StatusSwitchingProtocols, "") } +func TestClearedExpiredTimerDoesNotCloseConnectingWebSocket(t *testing.T) { + t.Parallel() + + ts := newTestState(t) + requestStarted := make(chan struct{}) + ts.tb.Mux.HandleFunc("/ws-delayed-failure", func(w http.ResponseWriter, _ *http.Request) { + close(requestStarted) + time.Sleep(250 * time.Millisecond) + http.Error(w, "handshake failed", http.StatusBadRequest) + }) + + require.NoError(t, ts.runtime.VU.RuntimeField.Set("waitForRequest", func() { <-requestStarted })) + require.NoError(t, ts.runtime.VU.RuntimeField.Set("sleep", time.Sleep)) + + err := runOnEventLoopWithTimeout(t, ts.runtime, ts.tb.Replacer.Replace(` + const ws = new WebSocket("WSBIN_URL/ws-delayed-failure"); + ws.onerror = () => call("error"); + + setTimeout(() => { + call("close timer"); + ws.close(); + }, 1000); + const canceledTimer = setTimeout(() => {}, 10); + + waitForRequest(); + sleep(20_000_000); + clearTimeout(canceledTimer); + `)) + require.NoError(t, err) + require.Equal(t, []string{"error", "close timer"}, ts.callRecorder.Recorded()) +} + +func TestCloseWhileConnectingCancelsHandshake(t *testing.T) { + t.Parallel() + + ts := newTestState(t) + requestStarted := make(chan struct{}) + requestCanceled := make(chan struct{}) + ts.tb.Mux.HandleFunc("/ws-pending-handshake", func(_ http.ResponseWriter, req *http.Request) { + close(requestStarted) + <-req.Context().Done() + close(requestCanceled) + }) + + require.NoError(t, ts.runtime.VU.RuntimeField.Set("waitForRequest", func() { <-requestStarted })) + err := runOnEventLoopWithTimeout(t, ts.runtime, ts.tb.Replacer.Replace(` + const ws = new WebSocket("WSBIN_URL/ws-pending-handshake"); + ws.onerror = () => call("error:" + ws.readyState); + ws.onclose = (event) => call("close:" + ws.readyState + ":" + event.code); + + waitForRequest(); + call("before:" + ws.readyState); + ws.close(); + call("after:" + ws.readyState); + `)) + require.NoError(t, err) + require.Equal(t, []string{"before:0", "after:2", "error:3", "close:3:1000"}, ts.callRecorder.Recorded()) + + select { + case <-requestCanceled: + case <-time.After(time.Second): + t.Fatal("WebSocket handshake was not canceled") + } +} + +func TestCloseWhileHandshakeBecomesEstablished(t *testing.T) { + t.Parallel() + + ts := newTestState(t) + upgraded := make(chan struct{}) + serverDone := make(chan struct{}) + ts.tb.Mux.HandleFunc("/ws-close-during-upgrade", func(w http.ResponseWriter, req *http.Request) { + conn, err := (&websocket.Upgrader{}).Upgrade(w, req, w.Header()) + if err != nil { + ts.errors <- fmt.Errorf("cannot upgrade request: %w", err) + return + } + close(upgraded) + defer close(serverDone) + defer func() { _ = conn.Close() }() + for { + if _, _, err = conn.ReadMessage(); err != nil { + return + } + } + }) + + require.NoError(t, ts.runtime.VU.RuntimeField.Set("waitForUpgrade", func() { <-upgraded })) + require.NoError(t, ts.runtime.VU.RuntimeField.Set("sleep", time.Sleep)) + err := runOnEventLoopWithTimeout(t, ts.runtime, ts.tb.Replacer.Replace(` + const ws = new WebSocket("WSBIN_URL/ws-close-during-upgrade"); + ws.onopen = () => call("open"); + ws.onerror = () => call("error:" + ws.readyState); + ws.onclose = (event) => call("close:" + ws.readyState + ":" + event.code); + + waitForUpgrade(); + sleep(20_000_000); + call("before:" + ws.readyState); + ws.close(3001); + call("after:" + ws.readyState); + `)) + require.NoError(t, err) + require.Equal(t, []string{"before:0", "after:2", "error:3", "close:3:3001"}, ts.callRecorder.Recorded()) + + select { + case <-serverDone: + case <-time.After(time.Second): + t.Fatal("established WebSocket connection was not closed") + } +} + +func TestContextCancelWhileConnectingCancelsHandshake(t *testing.T) { + t.Parallel() + + ts := newTestState(t) + requestStarted := make(chan struct{}) + requestCanceled := make(chan struct{}) + ts.tb.Mux.HandleFunc("/ws-canceled-context", func(_ http.ResponseWriter, req *http.Request) { + close(requestStarted) + <-req.Context().Done() + close(requestCanceled) + }) + + require.NoError(t, ts.runtime.VU.RuntimeField.Set("waitForRequest", func() { <-requestStarted })) + require.NoError(t, ts.runtime.VU.RuntimeField.Set("cancel", ts.runtime.CancelContext)) + err := runOnEventLoopWithTimeout(t, ts.runtime, ts.tb.Replacer.Replace(` + new WebSocket("WSBIN_URL/ws-canceled-context"); + waitForRequest(); + cancel(); + `)) + require.NoError(t, err) + + select { + case <-requestCanceled: + case <-time.After(time.Second): + t.Fatal("WebSocket handshake survived VU context cancellation") + } +} + func TestBasicSendBlob(t *testing.T) { t.Parallel() ts := newTestState(t) @@ -276,11 +434,11 @@ func TestReadyState(t *testing.T) { _, err := ts.runtime.RunOnEventLoop(ts.tb.Replacer.Replace(` var ws = new WebSocket("WSBIN_URL/ws-echo") ws.addEventListener("open", () => { - if (ws.readyState != 1){ + if (ws.readyState != WebSocket.OPEN){ throw new Error("Expected ready state 1 got "+ ws.readyState) } ws.addEventListener("close", () => { - if (ws.readyState != 3){ + if (ws.readyState != WebSocket.CLOSED){ throw new Error("Expected ready state 3 got "+ ws.readyState) } @@ -288,7 +446,7 @@ func TestReadyState(t *testing.T) { ws.send("something") ws.close() }) - if (ws.readyState != 0){ + if (ws.readyState != WebSocket.CONNECTING){ throw new Error("Expected ready state 0 got "+ ws.readyState) } `)) @@ -1568,7 +1726,7 @@ func testArrayBufferViewBufferedAmount(t *testing.T, viewName string) { if (ws.bufferedAmount != sent.byteLength) { throw new Error("Expected " + sent.byteLength + " bufferedAmount got " + ws.bufferedAmount); } - ws.onmessage = async (e) => { + ws.onmessage = (e) => { if (ws.bufferedAmount != 0) { throw new Error("Expected 0 bufferedAmount got " + ws.bufferedAmount); } @@ -1590,7 +1748,7 @@ func TestReadyStateSwitch(t *testing.T) { var ws = new WebSocket("WSBIN_URL/ws-echo") try { switch (ws.readyState) { - case 0: + case WebSocket.CONNECTING: break; default: throw "ws.readyState doesn't get correct value in switch" @@ -1682,6 +1840,30 @@ func TestCloseWithReasonTooLong(t *testing.T) { assertSessionMetricsEmitted(t, samples, "", sr("WSBIN_URL/ws-echo"), http.StatusSwitchingProtocols, "") } +func TestCloseValidatesArgumentsWhenClosed(t *testing.T) { + t.Parallel() + + ts := newTestState(t) + _, err := ts.runtime.RunOnEventLoop(ts.tb.Replacer.Replace(` + const ws = new WebSocket("WSBIN_URL/ws-echo"); + ws.onopen = () => ws.close(); + ws.onclose = () => { + try { + ws.close(1001); + } catch (_) { + call("invalid code"); + } + try { + ws.close(1000, "a really really long reason about why we are closing this connection, which is clearly over the top and too long, long enough to be longer than the allowed amount of 123 bytes"); + } catch (_) { + call("invalid reason"); + } + }; + `)) + require.NoError(t, err) + require.Equal(t, []string{"invalid code", "invalid reason"}, ts.callRecorder.Recorded()) +} + func TestRemoteCloseWithCodeAndReason(t *testing.T) { t.Parallel() ts := newTestState(t) diff --git a/internal/js/runner.go b/internal/js/runner.go index fd8a7c70e..0339c1f3b 100644 --- a/internal/js/runner.go +++ b/internal/js/runner.go @@ -19,7 +19,6 @@ import ( "github.com/grafana/sobek" "github.com/sirupsen/logrus" - "golang.org/x/net/http2" "golang.org/x/time/rate" "go.k6.io/k6/v2/errext" @@ -59,6 +58,9 @@ type Runner struct { RPSLimit *rate.Limiter RunTags *metrics.TagSet + // aiaFetcher owns the AIA cache + HTTP client when tlsAIAFetch is enabled. + aiaFetcher *netext.AIAFetcher + console *console setupData []byte BufferPool *lib.BufferPool @@ -195,6 +197,9 @@ func (r *Runner) newVU( }) tlsConfig.NameToCertificate = nameToCert //nolint:staticcheck } + if r.Bundle.Options.TLSAIAFetch.Bool { + tlsConfig = r.aiaFetcher.Wrap(tlsConfig, r.preInitState.Logger) //nolint:contextcheck + } transport := &http.Transport{ Proxy: http.ProxyFromEnvironment, TLSClientConfig: tlsConfig, @@ -206,9 +211,11 @@ func (r *Runner) newVU( } if r.forceHTTP1() { - transport.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper) // send over h1 protocol + transport.TLSNextProto = make(map[string]func(string, *tls.Conn) http.RoundTripper) } else { - _ = http2.ConfigureTransport(transport) // send over h2 protocol + // net/http negotiates HTTP/2 over a Transport with a custom DialContext or + // TLSClientConfig only when ForceAttemptHTTP2 is set (see Go issue #14275). + transport.ForceAttemptHTTP2 = true } cookieJar, err := cookiejar.New(nil) @@ -246,6 +253,7 @@ func (r *Runner) newVU( Samples: vu.Samples, Tags: lib.NewVUStateTags(vu.Runner.RunTags), BuiltinMetrics: r.preInitState.BuiltinMetrics, + FeatureFlags: r.preInitState.FeatureFlags, TracerProvider: r.preInitState.TracerProvider, Usage: r.preInitState.Usage, TestStatus: r.preInitState.TestStatus, @@ -396,6 +404,10 @@ func (r *Runner) HandleSummary( return nil, err } + if deadlineError := r.checkDeadline(summaryCtx, consts.HandleSummaryFn, nil, nil); deadlineError != nil { + return nil, deadlineError + } + wrapper := strings.Replace(summaryWrapperLambdaCode, "/*JSLIB_SUMMARY_CODE*/", summaryCode, 1) handleSummaryWrapperRaw, err := vu.Runtime.RunString(wrapper) if err != nil { @@ -574,6 +586,17 @@ func (r *Runner) SetOptions(opts lib.Options) error { return err } + // Rebuild the AIA fetcher against the new options — Blacklist / BlockedHostnames / + // Hosts / Resolver may have changed and the fetcher's dialer captures them. + runnerDialer := &netext.Dialer{ + Dialer: r.BaseDialer, + Resolver: r.Resolver, + Blacklist: opts.BlacklistIPs, + BlockedHostnames: opts.BlockedHostnames.Trie, + Hosts: opts.Hosts.Trie, + } + r.aiaFetcher = netext.NewAIAFetcher(runnerDialer.DialContext) + // FIXME: add tests r.RunTags = r.preInitState.Registry.RootTagSet().WithTagsFromMap(r.Bundle.Options.RunTags) @@ -692,7 +715,7 @@ func (r *Runner) getTimeoutFor(stage string) time.Duration { case consts.TeardownFn: return r.Bundle.Options.TeardownTimeout.TimeDuration() case consts.HandleSummaryFn: - return 2 * time.Minute // TODO: make configurable + return r.Bundle.Options.HandleSummaryTimeout.TimeDuration() } return d } @@ -752,12 +775,14 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU { params.Exec = consts.DefaultFn } - // Override the preset global env with any custom env vars + // Override the preset global env with any custom env vars. + // __ENV is frozen when the freeze-env feature flag is active. env := make(map[string]string, len(u.env)+len(params.Env)) maps.Copy(env, u.env) maps.Copy(env, params.Env) - //nolint:errcheck,gosec // see https://github.com/grafana/k6/issues/1722#issuecomment-1761173634 - u.Runtime.Set("__ENV", env) + if err := setupEnvObject(u.Runtime, env, u.Runner.preInitState.FeatureFlags); err != nil { + panic(err) // should not happen with string values + } opts := u.Runner.Bundle.Options @@ -870,8 +895,7 @@ func (u *ActiveVU) RunOnce() error { // Call the exported function. _, isFullIteration, totalTime, err := u.runFn(ctx, true, fn, cancel, u.setupData) if err != nil { - var x *sobek.InterruptedError - if errors.As(err, &x) { + if x, ok := errors.AsType[*sobek.InterruptedError](err); ok { if v, ok := x.Value().(*errext.InterruptError); ok { v.Reason = x.Error() err = v @@ -952,8 +976,7 @@ func (u *VU) runFn( u.moduleVUImpl.eventLoop.WaitOnRegistered() } endTime := time.Now() - var exception *sobek.Exception - if errors.As(err, &exception) { + if exception, ok := errors.AsType[*sobek.Exception](err); ok { err = &scriptExceptionError{inner: exception} } diff --git a/internal/js/runner_aia_test.go b/internal/js/runner_aia_test.go new file mode 100644 index 000000000..040eaee3a --- /dev/null +++ b/internal/js/runner_aia_test.go @@ -0,0 +1,225 @@ +package js + +// Integration tests for AIA (Authority Information Access) certificate chain fetching. +// These tests exercise the full k6 stack: runner → VU → HTTP transport → TLS handshake. + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "fmt" + "math/big" + "net" + "net/http" + "net/http/httptest" + "sync" + "testing" + "time" + + logtest "github.com/sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" + + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/metrics" +) + +// rsaIntermediateCA generates an intermediate CA certificate signed by parent. +// Unlike generateTLSCertificateWithCA, this sets IsCA=true so the resulting cert +// can sign other certificates (a plain leaf cert cannot act as an issuer). +func rsaIntermediateCA(t *testing.T, parent *x509.Certificate, parentKey *rsa.PrivateKey) ([]byte, *x509.Certificate, *rsa.PrivateKey) { + t.Helper() + + key, err := rsa.GenerateKey(rand.Reader, 2048) + require.NoError(t, err) + + sn, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) + require.NoError(t, err) + + tmpl := &x509.Certificate{ + SerialNumber: sn, + Subject: pkix.Name{CommonName: "Test Intermediate CA"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(24 * time.Hour), + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, + BasicConstraintsValid: true, + IsCA: true, + SignatureAlgorithm: x509.SHA256WithRSA, + } + + der, err := x509.CreateCertificate(rand.Reader, tmpl, parent, &key.PublicKey, parentKey) + require.NoError(t, err) + + cert, err := x509.ParseCertificate(der) + require.NoError(t, err) + + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) + return certPEM, cert, key +} + +// rsaLeafWithAIA generates a leaf certificate signed by parent that advertises aiaURL +// in its Authority Information Access extension as the location of the issuer cert. +func rsaLeafWithAIA(t *testing.T, host string, parent *x509.Certificate, parentKey *rsa.PrivateKey, aiaURL string) ([]byte, []byte) { + t.Helper() + + key, err := rsa.GenerateKey(rand.Reader, 2048) + require.NoError(t, err) + + sn, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) + require.NoError(t, err) + + tmpl := &x509.Certificate{ + SerialNumber: sn, + Subject: pkix.Name{CommonName: host}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(24 * time.Hour), + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + SignatureAlgorithm: x509.SHA256WithRSA, + IssuingCertificateURL: []string{aiaURL}, + } + if ip := net.ParseIP(host); ip != nil { + tmpl.IPAddresses = append(tmpl.IPAddresses, ip) + } else { + tmpl.DNSNames = append(tmpl.DNSNames, host) + } + + der, err := x509.CreateCertificate(rand.Reader, tmpl, parent, &key.PublicKey, parentKey) + require.NoError(t, err) + + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) + keyBytes, err := x509.MarshalPKCS8PrivateKey(key) + require.NoError(t, err) + keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes}) + return certPEM, keyPEM +} + +// TestVUIntegrationAIAFetching is a full end-to-end integration test: +// it runs k6 VUs against a TLS server that sends only a leaf certificate and +// requires AIA fetching to build the full chain to a trusted root. +// +// Two sub-tests verify the toggle behaviour: +// - AIAEnabled: tlsAIAFetch: true → connection succeeds +// - AIADisabled: default (off) → connection fails with x509 error +func TestVUIntegrationAIAFetching(t *testing.T) { + t.Parallel() + + // ── 1. Build a three-level certificate chain ──────────────────────────── + + // Root CA (self-signed, trusted by the test client) + rootCertPEM, rootKeyPEM := generateTLSCertificate(t, "127.0.0.1", time.Now(), time.Hour) + + rootBlock, _ := pem.Decode(rootCertPEM) + rootCert, err := x509.ParseCertificate(rootBlock.Bytes) + require.NoError(t, err) + + rootKeyBlock, _ := pem.Decode(rootKeyPEM) + rootKeyAny, err := x509.ParsePKCS8PrivateKey(rootKeyBlock.Bytes) + require.NoError(t, err) + rootKey, ok := rootKeyAny.(*rsa.PrivateKey) + require.True(t, ok) + + // ── 2. Start AIA server before building certs (we need its URL for the leaf) ── + + var ( + aiaMu sync.RWMutex + intermediarDER []byte + ) + aiaSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + aiaMu.RLock() + defer aiaMu.RUnlock() + w.Header().Set("Content-Type", "application/pkix-cert") + _, _ = w.Write(intermediarDER) + })) + t.Cleanup(aiaSrv.Close) + aiaURL := aiaSrv.URL + "/intermediate.der" + + // ── 3. Build intermediate CA and leaf with AIA URL ──────────────────────── + + _, interCert, interKey := rsaIntermediateCA(t, rootCert, rootKey) + + aiaMu.Lock() + intermediarDER = interCert.Raw // set before any request arrives + aiaMu.Unlock() + + // Use "localhost" so SNI carries a ServerName; Go strips IP literals from SNI + // and the AIA wrapper fails closed on empty ServerName. + leafCertPEM, leafKeyPEM := rsaLeafWithAIA(t, "localhost", interCert, interKey, aiaURL) + + // ── 4. Start TLS server that sends only the leaf (no intermediate chain) ── + + tlsSrv := getTestServerWithCertificate(t, leafCertPEM, leafKeyPEM) + go func() { _ = tlsSrv.Config.Serve(tlsSrv.Listener) }() + t.Cleanup(func() { require.NoError(t, tlsSrv.Config.Close()) }) + + _, port, err := net.SplitHostPort(tlsSrv.Listener.Addr().String()) + require.NoError(t, err) + serverAddr := "localhost:" + port + + rootPool := x509.NewCertPool() + rootPool.AddCert(rootCert) + + // ── 5. Table-driven sub-tests ───────────────────────────────────────────── + + testdata := map[string]struct { + aiaFetch null.Bool + errMsg string + }{ + // AIA fetching enabled: should build chain via AIA and succeed. + "AIAEnabled": {null.BoolFrom(true), ""}, + // AIA fetching disabled (default): incomplete chain, must fail. + "AIADisabled": {null.Bool{}, "certificate signed by unknown authority"}, + } + + for name, td := range testdata { + t.Run(name, func(t *testing.T) { + t.Parallel() + + script := fmt.Sprintf(` + var http = require("k6/http"); + exports.default = function() { http.get("https://%s/"); } + `, serverAddr) + + r1, err := getSimpleRunner(t, "/script.js", script) + require.NoError(t, err) + + opts := lib.Options{ + Throw: null.BoolFrom(true), + TLSAIAFetch: td.aiaFetch, + } + require.NoError(t, r1.SetOptions(opts)) + + r2, err := getSimpleArchiveRunner(t, r1.MakeArchive()) + require.NoError(t, err) + + runners := map[string]*Runner{"Source": r1, "Archive": r2} + for rname, r := range runners { + t.Run(rname, func(t *testing.T) { + t.Parallel() + r.preInitState.Logger, _ = logtest.NewNullLogger() + + ctx := t.Context() + initVU, err := r.NewVU(ctx, 1, 1, make(chan metrics.SampleContainer, 100)) + require.NoError(t, err) + + // Give the VU only the root CA – no intermediate. + // With AIA enabled, it should fetch the intermediate automatically. + initVU.(*VU).TLSConfig.RootCAs = rootPool + + vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx}) + runErr := vu.RunOnce() + + if td.errMsg != "" { + require.Error(t, runErr) + assert.Contains(t, runErr.Error(), td.errMsg) + } else { + require.NoError(t, runErr) + } + }) + } + }) + } +} diff --git a/internal/js/summary_test.go b/internal/js/summary_test.go index b8a7e138b..bad2718ea 100644 --- a/internal/js/summary_test.go +++ b/internal/js/summary_test.go @@ -54,7 +54,7 @@ func TestTextSummary(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", fmt.Sprintf(` - exports.options = {summaryTrendStats: %s}; + exports.options = {handleSummaryTimeout: "2m", summaryTrendStats: %s}; exports.default = function() {/* we don't run this, metrics are mocked */}; `, string(trendStats)), lib.RuntimeOptions{CompatibilityMode: null.NewString("base", true)}, @@ -110,7 +110,8 @@ func TestTextSummaryWithSubMetrics(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", - "exports.default = function() {/* we don't run this, metrics are mocked */};", + `exports.options = {handleSummaryTimeout: "2m"}; + exports.default = function() {/* we don't run this, metrics are mocked */};`, lib.RuntimeOptions{CompatibilityMode: null.NewString("base", true)}, ) require.NoError(t, err) @@ -294,7 +295,7 @@ func TestOldJSONExport(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", ` - exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; + exports.options = {handleSummaryTimeout: "2m", summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; exports.default = function() {/* we don't run this, metrics are mocked */}; `, lib.RuntimeOptions{ @@ -560,7 +561,7 @@ func TestRawHandleSummaryData(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", ` - exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; + exports.options = {handleSummaryTimeout: "2m", summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; exports.default = function() { /* we don't run this, metrics are mocked */ }; exports.handleSummary = function(data) { return {'rawdata.json': JSON.stringify(data)}; @@ -597,7 +598,7 @@ func TestRawHandleSummaryDataWithSetupData(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", ` - exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; + exports.options = {handleSummaryTimeout: "2m", summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; exports.default = function() { /* we don't run this, metrics are mocked */ }; exports.handleSummary = function(data) { if(data.setup_data != 5) { @@ -623,7 +624,7 @@ func TestRawHandleSummaryPromise(t *testing.T) { runner, err := getSimpleRunner( t, "/script.js", ` - exports.options = {summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; + exports.options = {handleSummaryTimeout: "2m", summaryTrendStats: ["avg", "min", "med", "max", "p(90)", "p(95)", "p(99)", "count"]}; exports.default = function() { /* we don't run this, metrics are mocked */ }; exports.handleSummary = async function(data) { return await Promise.resolve({'dataWithSetup.json': JSON.stringify(data)}); @@ -650,6 +651,7 @@ func TestWrongSummaryHandlerExportTypes(t *testing.T) { t.Parallel() runner, err := getSimpleRunner(t, "/script.js", fmt.Sprintf(` + exports.options = {handleSummaryTimeout: "2m"}; exports.default = function() { /* we don't run this, metrics are mocked */ }; exports.handleSummary = %s; `, tc), @@ -673,6 +675,7 @@ func TestExceptionInHandleSummaryFallsBackToTextSummary(t *testing.T) { logger.AddHook(logHook) runner, err := getSimpleRunner(t, "/script.js", ` + exports.options = {handleSummaryTimeout: "2m"}; exports.default = function() {/* we don't run this, metrics are mocked */}; exports.handleSummary = function(data) { throw new Error('intentional error'); @@ -696,6 +699,6 @@ func TestExceptionInHandleSummaryFallsBackToTextSummary(t *testing.T) { assert.Equal(t, 1, len(logErrors)) errMsg, err := logErrors[0].String() require.NoError(t, err) - assert.Contains(t, errMsg, "\"Error: intentional error\\n\\tat file:///script.js:4:11(3)\\n") + assert.Contains(t, errMsg, "\"Error: intentional error\\n\\tat file:///script.js:5:11(3)\\n") assert.Equal(t, logrus.Fields{"hint": "script exception", "source": "stacktrace"}, logErrors[0].Data) } diff --git a/internal/js/tc39/README.md b/internal/js/tc39/README.md index 9919d3951..e65cd2544 100644 --- a/internal/js/tc39/README.md +++ b/internal/js/tc39/README.md @@ -1,6 +1,6 @@ # Introduction to a k6's TC39 testing -`js/tc39` package tests k6 [Sobek](https://github.com/grafana/sobek) and the k6 Sobek+esbuild combo against the tc39 test suite. +`internal/js/tc39` package tests k6 [Sobek](https://github.com/grafana/sobek) and the k6 Sobek+esbuild combo against the tc39 test suite. Ways to use it: 1. run ./checkout.sh to checkout the last commit sha of [test262](https://github.com/tc39/test262) diff --git a/internal/js/tc39/breaking_test_errors-stripTypes=false.json b/internal/js/tc39/breaking_test_errors-stripTypes=false.json index b93fb3939..84ababf26 100644 --- a/internal/js/tc39/breaking_test_errors-stripTypes=false.json +++ b/internal/js/tc39/breaking_test_errors-stripTypes=false.json @@ -9,8 +9,6 @@ "test/annexB/built-ins/RegExp/RegExp-leading-escape-BMP.js-strict:true": "test/annexB/built-ins/RegExp/RegExp-leading-escape-BMP.js: Test262Error: Code unit: d800 Expected SameValue(«\"\\\\\\\\\\\\ud800\"», «\"\\\\\\ud800\"») to be true ", "test/annexB/built-ins/RegExp/RegExp-trailing-escape-BMP.js-strict:true": "test/annexB/built-ins/RegExp/RegExp-trailing-escape-BMP.js: Test262Error: Code unit: d800 Expected SameValue(«\"a\\\\\\\\\\\\ud800\"», «\"a\\\\\\ud800\"») to be true ", "test/annexB/built-ins/RegExp/prototype/compile/pattern-string-invalid-u.js-strict:true": "test/annexB/built-ins/RegExp/prototype/compile/pattern-string-invalid-u.js: Test262Error: invalid pattern: { Expected a SyntaxError to be thrown but no exception was thrown at all ", - "test/built-ins/AggregateError/errors-iterabletolist.js-strict:true": "test/built-ins/AggregateError/errors-iterabletolist.js: Test262Error: GetMethod(obj, @@iterator) returns undefined Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js-strict:true": "test/built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js: Test262Error: Actual [1, 2] and expected [1, 2] should have the same contents. a.flat(undefined) uses default depth of 1 ", "test/built-ins/AsyncFunction/AsyncFunction-is-subclass.js-strict:true": "test/built-ins/AsyncFunction/AsyncFunction-is-subclass.js: Test262Error: Prototype of constructor is Function Expected SameValue(«function () { [native code] }», «function Function() { [native code] }») to be true ", "test/built-ins/DataView/prototype/setBigInt64/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setBigInt64/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/DataView/prototype/setBigUint64/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setBigUint64/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", @@ -24,17 +22,6 @@ "test/built-ins/DataView/prototype/setUint32/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setUint32/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/DataView/prototype/setUint8/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setUint8/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/Date/UTC/fp-evaluation-order.js-strict:true": "test/built-ins/Date/UTC/fp-evaluation-order.js: Test262Error: order of operations / precision in MakeTime Expected SameValue(«29256», «29312») to be true ", - "test/built-ins/Error/error-message-tostring-symbol.js-strict:true": "test/built-ins/Error/error-message-tostring-symbol.js: Test262Error: If _message_ is a Symbol, Error must throw TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Error/isError/bigints.js-strict:true": "test/built-ins/Error/isError/bigints.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/error-subclass.js-strict:true": "test/built-ins/Error/isError/error-subclass.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/errors.js-strict:true": "test/built-ins/Error/isError/errors.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/fake-errors.js-strict:true": "test/built-ins/Error/isError/fake-errors.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/is-a-constructor.js-strict:true": "test/built-ins/Error/isError/is-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/Error/isError/name.js-strict:true": "test/built-ins/Error/isError/name.js: TypeError: Cannot read property 'name' of undefined ", - "test/built-ins/Error/isError/non-error-objects.js-strict:true": "test/built-ins/Error/isError/non-error-objects.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/primitives.js-strict:true": "test/built-ins/Error/isError/primitives.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/prop-desc.js-strict:true": "test/built-ins/Error/isError/prop-desc.js: Test262Error: obj should have an own property isError ", - "test/built-ins/Error/isError/symbols.js-strict:true": "test/built-ins/Error/isError/symbols.js: TypeError: Object has no member 'isError' ", "test/built-ins/Function/internals/Construct/base-ctor-revoked-proxy.js-strict:true": "test/built-ins/Function/internals/Construct/base-ctor-revoked-proxy.js: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all ", "test/built-ins/Iterator/concat/arguments-checked-in-order.js-strict:true": "test/built-ins/Iterator/concat/arguments-checked-in-order.js: Test262Error: Expected a TypeError but got a ReferenceError ", "test/built-ins/Iterator/concat/fresh-iterator-result.js-strict:true": "test/built-ins/Iterator/concat/fresh-iterator-result.js: ReferenceError: Iterator is not defined ", @@ -146,27 +133,12 @@ "test/built-ins/Iterator/zipKeyed/suspended-start-iterator-close-calls-return.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-start-iterator-close-calls-return.js: ReferenceError: Iterator is not defined ", "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-next.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-next.js: ReferenceError: Iterator is not defined ", "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-return.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-return.js: ReferenceError: Iterator is not defined ", - "test/built-ins/JSON/isRawJSON/basic.js-strict:true": "test/built-ins/JSON/isRawJSON/basic.js: TypeError: Object has no member 'isRawJSON' ", - "test/built-ins/JSON/isRawJSON/builtin.js-strict:true": "test/built-ins/JSON/isRawJSON/builtin.js: Test262Error: JSON.isRawJSON is extensible ", - "test/built-ins/JSON/isRawJSON/length.js-strict:true": "test/built-ins/JSON/isRawJSON/length.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/isRawJSON/name.js-strict:true": "test/built-ins/JSON/isRawJSON/name.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/isRawJSON/not-a-constructor.js-strict:true": "test/built-ins/JSON/isRawJSON/not-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/JSON/isRawJSON/prop-desc.js-strict:true": "test/built-ins/JSON/isRawJSON/prop-desc.js: Test262Error: obj should have an own property isRawJSON ", "test/built-ins/JSON/parse/reviver-call-args-after-forward-modification.js-strict:true": "test/built-ins/JSON/parse/reviver-call-args-after-forward-modification.js: TypeError: Value is not object coercible ", "test/built-ins/JSON/parse/reviver-context-source-array-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-array-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-context-source-object-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-object-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-context-source-primitive-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-primitive-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-forward-modifies-object.js-strict:true": "test/built-ins/JSON/parse/reviver-forward-modifies-object.js: TypeError: Value is not object coercible ", - "test/built-ins/JSON/rawJSON/basic.js-strict:true": "test/built-ins/JSON/rawJSON/basic.js: TypeError: Object has no member 'rawJSON' ", "test/built-ins/JSON/rawJSON/bigint-raw-json-can-be-stringified.js-strict:true": "test/built-ins/JSON/rawJSON/bigint-raw-json-can-be-stringified.js: TypeError: Value is not object coercible ", - "test/built-ins/JSON/rawJSON/builtin.js-strict:true": "test/built-ins/JSON/rawJSON/builtin.js: Test262Error: JSON.rawJSON is extensible ", - "test/built-ins/JSON/rawJSON/illegal-empty-and-start-end-chars.js-strict:true": "test/built-ins/JSON/rawJSON/illegal-empty-and-start-end-chars.js: Test262Error: Expected a SyntaxError but got a TypeError ", - "test/built-ins/JSON/rawJSON/invalid-JSON-text.js-strict:true": "test/built-ins/JSON/rawJSON/invalid-JSON-text.js: Test262Error: Expected a SyntaxError but got a TypeError ", - "test/built-ins/JSON/rawJSON/length.js-strict:true": "test/built-ins/JSON/rawJSON/length.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/rawJSON/name.js-strict:true": "test/built-ins/JSON/rawJSON/name.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/rawJSON/not-a-constructor.js-strict:true": "test/built-ins/JSON/rawJSON/not-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/JSON/rawJSON/prop-desc.js-strict:true": "test/built-ins/JSON/rawJSON/prop-desc.js: Test262Error: obj should have an own property rawJSON ", - "test/built-ins/JSON/rawJSON/returns-expected-object.js-strict:true": "test/built-ins/JSON/rawJSON/returns-expected-object.js: TypeError: Object has no member 'rawJSON' ", "test/built-ins/Map/prototype/getOrInsert/append-new-values-normalizes-zero-key.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-new-values-normalizes-zero-key.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/Map/prototype/getOrInsert/append-new-values.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-new-values.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/Map/prototype/getOrInsert/append-value-if-key-is-not-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-value-if-key-is-not-present-different-key-types.js: TypeError: Object has no member 'getOrInsert' ", @@ -192,9 +164,6 @@ "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-not-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-not-present-different-key-types.js: TypeError: Object has no member 'getOrInsertComputed' ", "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-present-different-key-types.js: TypeError: Object has no member 'getOrInsertComputed' ", "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-normalized-zero-key.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-normalized-zero-key.js: TypeError: Object has no member 'getOrInsertComputed' ", - "test/built-ins/NativeErrors/nativeerror-tostring-message-throws-symbol.js-strict:true": "test/built-ins/NativeErrors/nativeerror-tostring-message-throws-symbol.js: Test262Error: If _message_ is a Symbol, EvalError should throw a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values-__proto__.js-strict:true": "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values-__proto__.js: Test262Error: Object.setPrototypeOf(ObjProto, { __proto__: null }) throws a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values.js-strict:true": "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values.js: Test262Error: Object.setPrototypeOf(ObjProto, Object.create(null)) throws a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-positive-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-positive-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-non-digit-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-non-digit-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", @@ -208,9 +177,9 @@ "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-positive-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-positive-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/lookahead-quantifier-match-groups.js-strict:true": "test/built-ins/RegExp/lookahead-quantifier-match-groups.js: Test262Error: Actual [a, abc] and expected [a, undefined] should have the same contents. ? quantifier ", + "test/built-ins/RegExp/named-groups/non-unicode-property-names-valid.js-strict:true": "test/built-ins/RegExp/named-groups/non-unicode-property-names-valid.js: SyntaxError: Invalid regular expression (regexp2): (?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog) (Invalid regular expression (regexp2): (?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog) (error parsing regexp: invalid capture group name in `(?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog)`)) ", "test/built-ins/RegExp/nullable-quantifier.js-strict:true": "test/built-ins/RegExp/nullable-quantifier.js: Test262Error: The regex is expected to match the whole string Expected SameValue(«\"a\"», «\"ab\"») to be true ", "test/built-ins/RegExp/quantifier-integer-limit.js-strict:true": "test/built-ins/RegExp/quantifier-integer-limit.js: SyntaxError: Invalid regular expression (regexp2): b{9007199254740991} (Invalid regular expression (regexp2): b{9007199254740991} (error parsing regexp: capture group number out of range in `b{9007199254740991}`)) ", - "test/built-ins/RegExp/unicode_full_case_folding.js-strict:true": "test/built-ins/RegExp/unicode_full_case_folding.js: Test262Error: \\u0390 does not match \\u1fd3 ", "test/built-ins/RegExp/unicode_restricted_brackets.js-strict:true": "test/built-ins/RegExp/unicode_restricted_brackets.js: Test262Error: RegExp(\"]\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_character_class_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_character_class_escape.js: Test262Error: RegExp(\"[\\d-a]\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_identity_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_identity_escape.js: Test262Error: Invalid IdentityEscape in AtomEscape: '\\\u0000' Expected a SyntaxError to be thrown but no exception was thrown at all ", @@ -221,17 +190,6 @@ "test/built-ins/RegExp/unicode_restricted_incomplete_quantifier.js-strict:true": "test/built-ins/RegExp/unicode_restricted_incomplete_quantifier.js: Test262Error: RegExp(\"a{\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_octal_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_octal_escape.js: Test262Error: RegExp(\"\\1\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_quantifiable_assertion.js-strict:true": "test/built-ins/RegExp/unicode_restricted_quantifiable_assertion.js: Test262Error: RegExp(\"(?=.)*\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", - "test/built-ins/String/prototype/replace/regexp-capture-by-index.js-strict:true": "test/built-ins/String/prototype/replace/regexp-capture-by-index.js: Test262Error: `$01` before `0` is a capture index in /((((((((((x))))))))))/ Expected SameValue(«\"foo-|x|-bar\"», «\"foo-|x0|-bar\"») to be true ", - "test/built-ins/TypedArray/prototype/slice/speciesctor-return-same-buffer-with-offset.js-strict:true": "test/built-ins/TypedArray/prototype/slice/speciesctor-return-same-buffer-with-offset.js: Test262Error: Actual [40, 40, 50, 60] and expected [20, 20, 20, 60] should have the same contents. (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArray/prototype/with/value-throw-completion.js-strict:true": "test/built-ins/TypedArray/prototype/with/value-throw-completion.js: Test262Error: Positive too large index Expected a MyError but got a RangeError (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-prototype-chain-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-prototype-chain-set.js: Test262Error: 1 setter should be unreachable! (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-reflect-set.js: Test262Error: 1 setter should be unreachable! (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-valid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-valid-index-reflect-set.js: Test262Error: Reflect.set should fail (receiver: another typed array of shorter length) (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-prototype-chain-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-prototype-chain-set.js: Test262Error: 1 setter should be unreachable! (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-reflect-set.js: Test262Error: 1 setter should be unreachable! (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-not-object.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-not-object.js: Test262Error: [[Set]] succeeeds ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-proto.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-proto.js: Test262Error: valueOf is called exactly once Expected SameValue(«0», «1») to be true ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-valid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-valid-index-reflect-set.js: Test262Error: Reflect.set should fail (receiver: another typed array of shorter length) (Testing with Float64Array and makePassthrough.) ", "test/built-ins/WeakMap/prototype/getOrInsert/adds-object-element.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/adds-object-element.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/WeakMap/prototype/getOrInsert/getOrInsert.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/getOrInsert.js: Test262Error: typeof WeakMap.prototype.getOrInsert is \"function\" Expected SameValue(«\"undefined\"», «\"function\"») to be true ", "test/built-ins/WeakMap/prototype/getOrInsert/length.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/length.js: TypeError: Cannot convert undefined or null to object ", @@ -257,28 +215,12 @@ "test/language/expressions/class/class-name-ident-await-module.js-strict:true": "test/language/expressions/class/class-name-ident-await-module.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/expressions/class/elements/private-getter-is-not-a-own-property.js-strict:true": "test/language/expressions/class/elements/private-getter-is-not-a-own-property.js: TypeError: Object has no member '__lookupGetter__' ", "test/language/expressions/class/elements/private-setter-is-not-a-own-property.js-strict:true": "test/language/expressions/class/elements/private-setter-is-not-a-own-property.js: TypeError: Object has no member '__lookupSetter__' ", - "test/language/expressions/greater-than-or-equal/S11.8.4_A4.12_T1.js-strict:true": "test/language/expressions/greater-than-or-equal/S11.8.4_A4.12_T1.js: Test262Error: #10: (\"\\u{10000}\" >= \"\\uFFFF\") === false ", - "test/language/expressions/greater-than/S11.8.2_A4.12_T1.js-strict:true": "test/language/expressions/greater-than/S11.8.2_A4.12_T1.js: Test262Error: #9: (\"\\uDC00\" > \"\\uD800\") === true ", "test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js-strict:true": "test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js: file:///TestTC39/test262/test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js: Line 28:25 import not supported in script (and 19 more errors)", "test/language/expressions/import.meta/same-object-returned.js-strict:true": "test/language/expressions/import.meta/same-object-returned.js: file:///TestTC39/test262/test/language/expressions/import.meta/same-object-returned.js: Line 28:9 import not supported in script (and 3 more errors)", "test/language/expressions/import.meta/syntax/goal-module-nested-function.js-strict:true": "test/language/expressions/import.meta/syntax/goal-module-nested-function.js: file:///TestTC39/test262/test/language/expressions/import.meta/syntax/goal-module-nested-function.js: Line 16:3 import not supported in script (and 4 more errors)", "test/language/expressions/import.meta/syntax/goal-module.js-strict:true": "test/language/expressions/import.meta/syntax/goal-module.js: file:///TestTC39/test262/test/language/expressions/import.meta/syntax/goal-module.js: Line 15:1 import not supported in script (and 3 more errors)", - "test/language/expressions/less-than-or-equal/S11.8.3_A4.12_T1.js-strict:true": "test/language/expressions/less-than-or-equal/S11.8.3_A4.12_T1.js: Test262Error: #10: (\"\\u{10000}\" <= \"\\uFFFF\") === true ", - "test/language/expressions/less-than/S11.8.1_A4.12_T1.js-strict:true": "test/language/expressions/less-than/S11.8.1_A4.12_T1.js: Test262Error: #9: (\"\\uD800\" < \"\\uDC00\") === true ", "test/language/expressions/optional-chaining/iteration-statement-for-await-of.js-strict:true": "test/language/expressions/optional-chaining/iteration-statement-for-await-of.js: test/language/expressions/optional-chaining/iteration-statement-for-await-of.js: Line 32:7 Unexpected token await (and 9 more errors)", "test/language/expressions/optional-chaining/member-expression.js-strict:true": "test/language/expressions/optional-chaining/member-expression.js: SyntaxError: Async generators are not supported yet ", - "test/language/identifiers/part-unicode-15.1.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-class-escaped.js: test/language/identifiers/part-unicode-15.1.0-class-escaped.js: Line 20:22 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-15.1.0-class.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-class.js: test/language/identifiers/part-unicode-15.1.0-class.js: Line 17:11 Unexpected token ILLEGAL (and 4 more errors)", - "test/language/identifiers/part-unicode-15.1.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-escaped.js: test/language/identifiers/part-unicode-15.1.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-15.1.0.js-strict:true": "test/language/identifiers/part-unicode-15.1.0.js: test/language/identifiers/part-unicode-15.1.0.js: Line 15:12 Unexpected token ILLEGAL (and 3 more errors)", - "test/language/identifiers/part-unicode-16.0.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-class-escaped.js: test/language/identifiers/part-unicode-16.0.0-class-escaped.js: Line 20:10 Unexpected token ILLEGAL (and 3 more errors)", - "test/language/identifiers/part-unicode-16.0.0-class.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-class.js: test/language/identifiers/part-unicode-16.0.0-class.js: Line 17:5 Unexpected token ILLEGAL (and 133 more errors)", - "test/language/identifiers/part-unicode-16.0.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-escaped.js: test/language/identifiers/part-unicode-16.0.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-16.0.0.js-strict:true": "test/language/identifiers/part-unicode-16.0.0.js: test/language/identifiers/part-unicode-16.0.0.js: Line 15:6 Unexpected token ILLEGAL (and 132 more errors)", - "test/language/identifiers/part-unicode-17.0.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-class-escaped.js: test/language/identifiers/part-unicode-17.0.0-class-escaped.js: Line 20:10 Unexpected token ILLEGAL (and 48 more errors)", - "test/language/identifiers/part-unicode-17.0.0-class.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-class.js: test/language/identifiers/part-unicode-17.0.0-class.js: Line 17:5 Unexpected token ILLEGAL (and 54 more errors)", - "test/language/identifiers/part-unicode-17.0.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-escaped.js: test/language/identifiers/part-unicode-17.0.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-17.0.0.js-strict:true": "test/language/identifiers/part-unicode-17.0.0.js: test/language/identifiers/part-unicode-17.0.0.js: Line 15:6 Unexpected token ILLEGAL (and 53 more errors)", "test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js: Line 27:14 Unexpected token *", "test/language/import/import-defer/deferred-namespace-object/identity.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/identity.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/identity.js: Line 36:14 Unexpected token *", "test/language/import/import-defer/deferred-namespace-object/to-string-tag.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/to-string-tag.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/to-string-tag.js: Line 29:14 Unexpected token *", @@ -374,6 +316,11 @@ "test/language/literals/regexp/invalid-optional-negative-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-optional-negative-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/invalid-range-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-range-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/invalid-range-negative-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-range-negative-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2-u.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-u.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-identity-escape-in-capture-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-identity-escape-in-capture-u.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/u-case-mapping.js-strict:true": "test/language/literals/regexp/u-case-mapping.js: Test262Error: Case mapping is not applied in the absence of the `u` flag Expected SameValue(«true», «false») to be true ", "test/language/literals/regexp/u-invalid-class-escape.js-strict:true": "test/language/literals/regexp/u-invalid-class-escape.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/u-invalid-extended-pattern-char.js-strict:true": "test/language/literals/regexp/u-invalid-extended-pattern-char.js: error is not an object (Test262: This statement should not be evaluated.)", @@ -426,7 +373,6 @@ "test/language/statements/with/get-binding-value-call-with-proxy-env.js-strict:false": "test/language/statements/with/get-binding-value-call-with-proxy-env.js: Test262Error: Actual [has:Object, get:Symbol(Symbol.unscopables), get:Object] and expected [has:Object, get:Symbol(Symbol.unscopables), has:Object, get:Object] should have the same contents. ", "test/language/statements/with/get-binding-value-idref-with-proxy-env.js-strict:false": "test/language/statements/with/get-binding-value-idref-with-proxy-env.js: Test262Error: Actual [has:Object, get:Symbol(Symbol.unscopables), get:Object] and expected [has:Object, get:Symbol(Symbol.unscopables), has:Object, get:Object] should have the same contents. ", "test/language/statements/with/get-mutable-binding-binding-deleted-in-get-unscopables-strict-mode.js-strict:false": "test/language/statements/with/get-mutable-binding-binding-deleted-in-get-unscopables-strict-mode.js: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all ", - "test/language/statements/with/set-mutable-binding-binding-deleted-with-typed-array-in-proto-chain.js-strict:false": "test/language/statements/with/set-mutable-binding-binding-deleted-with-typed-array-in-proto-chain.js: Test262Error: Expected SameValue(«[object Object]», «undefined») to be true ", "test/language/statements/with/set-mutable-binding-idref-compound-assign-with-proxy-env.js-strict:false": "test/language/statements/with/set-mutable-binding-idref-compound-assign-with-proxy-env.js: Test262Error: Actual [has:p, get:Symbol(Symbol.unscopables), get:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] and expected [has:p, get:Symbol(Symbol.unscopables), has:p, get:p, has:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] should have the same contents. ", "test/language/statements/with/set-mutable-binding-idref-with-proxy-env.js-strict:false": "test/language/statements/with/set-mutable-binding-idref-with-proxy-env.js: Test262Error: Actual [has:p, get:Symbol(Symbol.unscopables), set:p, getOwnPropertyDescriptor:p, defineProperty:p] and expected [has:p, get:Symbol(Symbol.unscopables), has:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] should have the same contents. " } diff --git a/internal/js/tc39/breaking_test_errors-stripTypes=true.json b/internal/js/tc39/breaking_test_errors-stripTypes=true.json index eb8d6b989..63f1d1d7b 100644 --- a/internal/js/tc39/breaking_test_errors-stripTypes=true.json +++ b/internal/js/tc39/breaking_test_errors-stripTypes=true.json @@ -9,8 +9,6 @@ "test/annexB/built-ins/RegExp/RegExp-leading-escape-BMP.js-strict:true": "test/annexB/built-ins/RegExp/RegExp-leading-escape-BMP.js: Test262Error: Code unit: d800 Expected SameValue(«\"\\\\\\\\\\\\ud800\"», «\"\\\\\\ud800\"») to be true ", "test/annexB/built-ins/RegExp/RegExp-trailing-escape-BMP.js-strict:true": "test/annexB/built-ins/RegExp/RegExp-trailing-escape-BMP.js: Test262Error: Code unit: d800 Expected SameValue(«\"a\\\\\\\\\\\\ud800\"», «\"a\\\\\\ud800\"») to be true ", "test/annexB/built-ins/RegExp/prototype/compile/pattern-string-invalid-u.js-strict:true": "test/annexB/built-ins/RegExp/prototype/compile/pattern-string-invalid-u.js: Test262Error: invalid pattern: { Expected a SyntaxError to be thrown but no exception was thrown at all ", - "test/built-ins/AggregateError/errors-iterabletolist.js-strict:true": "test/built-ins/AggregateError/errors-iterabletolist.js: Test262Error: GetMethod(obj, @@iterator) returns undefined Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js-strict:true": "test/built-ins/Array/prototype/flat/non-numeric-depth-should-not-throw.js: Test262Error: Actual [1, 2] and expected [1, 2] should have the same contents. a.flat(undefined) uses default depth of 1 ", "test/built-ins/AsyncFunction/AsyncFunction-is-subclass.js-strict:true": "test/built-ins/AsyncFunction/AsyncFunction-is-subclass.js: Test262Error: Prototype of constructor is Function Expected SameValue(«function () { [native code] }», «function Function() { [native code] }») to be true ", "test/built-ins/DataView/prototype/setBigInt64/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setBigInt64/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/DataView/prototype/setBigUint64/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setBigUint64/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", @@ -24,17 +22,6 @@ "test/built-ins/DataView/prototype/setUint32/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setUint32/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/DataView/prototype/setUint8/immutable-buffer.js-strict:true": "test/built-ins/DataView/prototype/setUint8/immutable-buffer.js: TypeError: Object has no member 'transferToImmutable' ", "test/built-ins/Date/UTC/fp-evaluation-order.js-strict:true": "test/built-ins/Date/UTC/fp-evaluation-order.js: Test262Error: order of operations / precision in MakeTime Expected SameValue(«29256», «29312») to be true ", - "test/built-ins/Error/error-message-tostring-symbol.js-strict:true": "test/built-ins/Error/error-message-tostring-symbol.js: Test262Error: If _message_ is a Symbol, Error must throw TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Error/isError/bigints.js-strict:true": "test/built-ins/Error/isError/bigints.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/error-subclass.js-strict:true": "test/built-ins/Error/isError/error-subclass.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/errors.js-strict:true": "test/built-ins/Error/isError/errors.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/fake-errors.js-strict:true": "test/built-ins/Error/isError/fake-errors.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/is-a-constructor.js-strict:true": "test/built-ins/Error/isError/is-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/Error/isError/name.js-strict:true": "test/built-ins/Error/isError/name.js: TypeError: Cannot read property 'name' of undefined ", - "test/built-ins/Error/isError/non-error-objects.js-strict:true": "test/built-ins/Error/isError/non-error-objects.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/primitives.js-strict:true": "test/built-ins/Error/isError/primitives.js: TypeError: Object has no member 'isError' ", - "test/built-ins/Error/isError/prop-desc.js-strict:true": "test/built-ins/Error/isError/prop-desc.js: Test262Error: obj should have an own property isError ", - "test/built-ins/Error/isError/symbols.js-strict:true": "test/built-ins/Error/isError/symbols.js: TypeError: Object has no member 'isError' ", "test/built-ins/Function/internals/Construct/base-ctor-revoked-proxy.js-strict:true": "test/built-ins/Function/internals/Construct/base-ctor-revoked-proxy.js: Test262Error: Expected a TypeError to be thrown but no exception was thrown at all ", "test/built-ins/Iterator/concat/arguments-checked-in-order.js-strict:true": "test/built-ins/Iterator/concat/arguments-checked-in-order.js: Test262Error: Expected a TypeError but got a ReferenceError ", "test/built-ins/Iterator/concat/fresh-iterator-result.js-strict:true": "test/built-ins/Iterator/concat/fresh-iterator-result.js: ReferenceError: Iterator is not defined ", @@ -146,27 +133,12 @@ "test/built-ins/Iterator/zipKeyed/suspended-start-iterator-close-calls-return.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-start-iterator-close-calls-return.js: ReferenceError: Iterator is not defined ", "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-next.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-next.js: ReferenceError: Iterator is not defined ", "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-return.js-strict:true": "test/built-ins/Iterator/zipKeyed/suspended-yield-iterator-close-calls-return.js: ReferenceError: Iterator is not defined ", - "test/built-ins/JSON/isRawJSON/basic.js-strict:true": "test/built-ins/JSON/isRawJSON/basic.js: TypeError: Object has no member 'isRawJSON' ", - "test/built-ins/JSON/isRawJSON/builtin.js-strict:true": "test/built-ins/JSON/isRawJSON/builtin.js: Test262Error: JSON.isRawJSON is extensible ", - "test/built-ins/JSON/isRawJSON/length.js-strict:true": "test/built-ins/JSON/isRawJSON/length.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/isRawJSON/name.js-strict:true": "test/built-ins/JSON/isRawJSON/name.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/isRawJSON/not-a-constructor.js-strict:true": "test/built-ins/JSON/isRawJSON/not-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/JSON/isRawJSON/prop-desc.js-strict:true": "test/built-ins/JSON/isRawJSON/prop-desc.js: Test262Error: obj should have an own property isRawJSON ", "test/built-ins/JSON/parse/reviver-call-args-after-forward-modification.js-strict:true": "test/built-ins/JSON/parse/reviver-call-args-after-forward-modification.js: TypeError: Value is not object coercible ", "test/built-ins/JSON/parse/reviver-context-source-array-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-array-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-context-source-object-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-object-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-context-source-primitive-literal.js-strict:true": "test/built-ins/JSON/parse/reviver-context-source-primitive-literal.js: Test262Error: context should be an object Expected SameValue(«\"undefined\"», «\"object\"») to be true ", "test/built-ins/JSON/parse/reviver-forward-modifies-object.js-strict:true": "test/built-ins/JSON/parse/reviver-forward-modifies-object.js: TypeError: Value is not object coercible ", - "test/built-ins/JSON/rawJSON/basic.js-strict:true": "test/built-ins/JSON/rawJSON/basic.js: TypeError: Object has no member 'rawJSON' ", "test/built-ins/JSON/rawJSON/bigint-raw-json-can-be-stringified.js-strict:true": "test/built-ins/JSON/rawJSON/bigint-raw-json-can-be-stringified.js: TypeError: Value is not object coercible ", - "test/built-ins/JSON/rawJSON/builtin.js-strict:true": "test/built-ins/JSON/rawJSON/builtin.js: Test262Error: JSON.rawJSON is extensible ", - "test/built-ins/JSON/rawJSON/illegal-empty-and-start-end-chars.js-strict:true": "test/built-ins/JSON/rawJSON/illegal-empty-and-start-end-chars.js: Test262Error: Expected a SyntaxError but got a TypeError ", - "test/built-ins/JSON/rawJSON/invalid-JSON-text.js-strict:true": "test/built-ins/JSON/rawJSON/invalid-JSON-text.js: Test262Error: Expected a SyntaxError but got a TypeError ", - "test/built-ins/JSON/rawJSON/length.js-strict:true": "test/built-ins/JSON/rawJSON/length.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/rawJSON/name.js-strict:true": "test/built-ins/JSON/rawJSON/name.js: TypeError: Cannot convert undefined or null to object ", - "test/built-ins/JSON/rawJSON/not-a-constructor.js-strict:true": "test/built-ins/JSON/rawJSON/not-a-constructor.js: Test262Error: isConstructor invoked with a non-function value ", - "test/built-ins/JSON/rawJSON/prop-desc.js-strict:true": "test/built-ins/JSON/rawJSON/prop-desc.js: Test262Error: obj should have an own property rawJSON ", - "test/built-ins/JSON/rawJSON/returns-expected-object.js-strict:true": "test/built-ins/JSON/rawJSON/returns-expected-object.js: TypeError: Object has no member 'rawJSON' ", "test/built-ins/Map/prototype/getOrInsert/append-new-values-normalizes-zero-key.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-new-values-normalizes-zero-key.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/Map/prototype/getOrInsert/append-new-values.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-new-values.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/Map/prototype/getOrInsert/append-value-if-key-is-not-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsert/append-value-if-key-is-not-present-different-key-types.js: TypeError: Object has no member 'getOrInsert' ", @@ -192,9 +164,6 @@ "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-not-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-not-present-different-key-types.js: TypeError: Object has no member 'getOrInsertComputed' ", "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-present-different-key-types.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-if-key-is-present-different-key-types.js: TypeError: Object has no member 'getOrInsertComputed' ", "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-normalized-zero-key.js-strict:true": "test/built-ins/Map/prototype/getOrInsertComputed/returns-value-normalized-zero-key.js: TypeError: Object has no member 'getOrInsertComputed' ", - "test/built-ins/NativeErrors/nativeerror-tostring-message-throws-symbol.js-strict:true": "test/built-ins/NativeErrors/nativeerror-tostring-message-throws-symbol.js: Test262Error: If _message_ is a Symbol, EvalError should throw a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values-__proto__.js-strict:true": "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values-__proto__.js: Test262Error: Object.setPrototypeOf(ObjProto, { __proto__: null }) throws a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", - "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values.js-strict:true": "test/built-ins/Object/prototype/setPrototypeOf-with-non-circular-values.js: Test262Error: Object.setPrototypeOf(ObjProto, Object.create(null)) throws a TypeError Expected a TypeError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-positive-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-digit-class-escape-positive-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-non-digit-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-non-digit-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", @@ -208,9 +177,9 @@ "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-negative-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-negative-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-positive-cases.js-strict:true": "test/built-ins/RegExp/CharacterClassEscapes/character-class-word-class-escape-positive-cases.js: SyntaxError: Invalid flags supplied to RegExp constructor 'v' ", "test/built-ins/RegExp/lookahead-quantifier-match-groups.js-strict:true": "test/built-ins/RegExp/lookahead-quantifier-match-groups.js: Test262Error: Actual [a, abc] and expected [a, undefined] should have the same contents. ? quantifier ", + "test/built-ins/RegExp/named-groups/non-unicode-property-names-valid.js-strict:true": "test/built-ins/RegExp/named-groups/non-unicode-property-names-valid.js: SyntaxError: Invalid regular expression (regexp2): (?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog) (Invalid regular expression (regexp2): (?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog) (error parsing regexp: invalid capture group name in `(?<\\ud835\\udc53\\ud835\\udc5c\\ud835\\udc65>fox).*(?<\\ud835\\udcd3\\ud835\\udcf8\\ud835\\udcf0>dog)`)) ", "test/built-ins/RegExp/nullable-quantifier.js-strict:true": "test/built-ins/RegExp/nullable-quantifier.js: Test262Error: The regex is expected to match the whole string Expected SameValue(«\"a\"», «\"ab\"») to be true ", "test/built-ins/RegExp/quantifier-integer-limit.js-strict:true": "test/built-ins/RegExp/quantifier-integer-limit.js: SyntaxError: Invalid regular expression (regexp2): b{9007199254740991} (Invalid regular expression (regexp2): b{9007199254740991} (error parsing regexp: capture group number out of range in `b{9007199254740991}`)) ", - "test/built-ins/RegExp/unicode_full_case_folding.js-strict:true": "test/built-ins/RegExp/unicode_full_case_folding.js: Test262Error: \\u0390 does not match \\u1fd3 ", "test/built-ins/RegExp/unicode_restricted_brackets.js-strict:true": "test/built-ins/RegExp/unicode_restricted_brackets.js: Test262Error: RegExp(\"]\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_character_class_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_character_class_escape.js: Test262Error: RegExp(\"[\\d-a]\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_identity_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_identity_escape.js: Test262Error: Invalid IdentityEscape in AtomEscape: '\\\u0000' Expected a SyntaxError to be thrown but no exception was thrown at all ", @@ -221,17 +190,6 @@ "test/built-ins/RegExp/unicode_restricted_incomplete_quantifier.js-strict:true": "test/built-ins/RegExp/unicode_restricted_incomplete_quantifier.js: Test262Error: RegExp(\"a{\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_octal_escape.js-strict:true": "test/built-ins/RegExp/unicode_restricted_octal_escape.js: Test262Error: RegExp(\"\\1\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", "test/built-ins/RegExp/unicode_restricted_quantifiable_assertion.js-strict:true": "test/built-ins/RegExp/unicode_restricted_quantifiable_assertion.js: Test262Error: RegExp(\"(?=.)*\", \"u\"): Expected a SyntaxError to be thrown but no exception was thrown at all ", - "test/built-ins/String/prototype/replace/regexp-capture-by-index.js-strict:true": "test/built-ins/String/prototype/replace/regexp-capture-by-index.js: Test262Error: `$01` before `0` is a capture index in /((((((((((x))))))))))/ Expected SameValue(«\"foo-|x|-bar\"», «\"foo-|x0|-bar\"») to be true ", - "test/built-ins/TypedArray/prototype/slice/speciesctor-return-same-buffer-with-offset.js-strict:true": "test/built-ins/TypedArray/prototype/slice/speciesctor-return-same-buffer-with-offset.js: Test262Error: Actual [40, 40, 50, 60] and expected [20, 20, 20, 60] should have the same contents. (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArray/prototype/with/value-throw-completion.js-strict:true": "test/built-ins/TypedArray/prototype/with/value-throw-completion.js: Test262Error: Positive too large index Expected a MyError but got a RangeError (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-prototype-chain-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-prototype-chain-set.js: Test262Error: 1 setter should be unreachable! (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-canonical-invalid-index-reflect-set.js: Test262Error: 1 setter should be unreachable! (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-valid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/BigInt/key-is-valid-index-reflect-set.js: Test262Error: Reflect.set should fail (receiver: another typed array of shorter length) (Testing with BigInt64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-prototype-chain-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-prototype-chain-set.js: Test262Error: 1 setter should be unreachable! (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-canonical-invalid-index-reflect-set.js: Test262Error: 1 setter should be unreachable! (Testing with Float64Array and makePassthrough.) ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-not-object.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-not-object.js: Test262Error: [[Set]] succeeeds ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-proto.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-out-of-bounds-receiver-is-proto.js: Test262Error: valueOf is called exactly once Expected SameValue(«0», «1») to be true ", - "test/built-ins/TypedArrayConstructors/internals/Set/key-is-valid-index-reflect-set.js-strict:true": "test/built-ins/TypedArrayConstructors/internals/Set/key-is-valid-index-reflect-set.js: Test262Error: Reflect.set should fail (receiver: another typed array of shorter length) (Testing with Float64Array and makePassthrough.) ", "test/built-ins/WeakMap/prototype/getOrInsert/adds-object-element.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/adds-object-element.js: TypeError: Object has no member 'getOrInsert' ", "test/built-ins/WeakMap/prototype/getOrInsert/getOrInsert.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/getOrInsert.js: Test262Error: typeof WeakMap.prototype.getOrInsert is \"function\" Expected SameValue(«\"undefined\"», «\"function\"») to be true ", "test/built-ins/WeakMap/prototype/getOrInsert/length.js-strict:true": "test/built-ins/WeakMap/prototype/getOrInsert/length.js: TypeError: Cannot convert undefined or null to object ", @@ -257,28 +215,12 @@ "test/language/expressions/class/class-name-ident-await-module.js-strict:true": "test/language/expressions/class/class-name-ident-await-module.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/expressions/class/elements/private-getter-is-not-a-own-property.js-strict:true": "test/language/expressions/class/elements/private-getter-is-not-a-own-property.js: TypeError: Object has no member '__lookupGetter__' ", "test/language/expressions/class/elements/private-setter-is-not-a-own-property.js-strict:true": "test/language/expressions/class/elements/private-setter-is-not-a-own-property.js: TypeError: Object has no member '__lookupSetter__' ", - "test/language/expressions/greater-than-or-equal/S11.8.4_A4.12_T1.js-strict:true": "test/language/expressions/greater-than-or-equal/S11.8.4_A4.12_T1.js: Test262Error: #10: (\"\\u{10000}\" >= \"\\uFFFF\") === false ", - "test/language/expressions/greater-than/S11.8.2_A4.12_T1.js-strict:true": "test/language/expressions/greater-than/S11.8.2_A4.12_T1.js: Test262Error: #9: (\"\\uDC00\" > \"\\uD800\") === true ", "test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js-strict:true": "test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js: file:///TestTC39/test262/test/language/expressions/import.meta/import-meta-is-an-ordinary-object.js: Line 28:25 import not supported in script (and 19 more errors)", "test/language/expressions/import.meta/same-object-returned.js-strict:true": "test/language/expressions/import.meta/same-object-returned.js: file:///TestTC39/test262/test/language/expressions/import.meta/same-object-returned.js: Line 28:9 import not supported in script (and 3 more errors)", "test/language/expressions/import.meta/syntax/goal-module-nested-function.js-strict:true": "test/language/expressions/import.meta/syntax/goal-module-nested-function.js: file:///TestTC39/test262/test/language/expressions/import.meta/syntax/goal-module-nested-function.js: Line 16:3 import not supported in script (and 4 more errors)", "test/language/expressions/import.meta/syntax/goal-module.js-strict:true": "test/language/expressions/import.meta/syntax/goal-module.js: file:///TestTC39/test262/test/language/expressions/import.meta/syntax/goal-module.js: Line 15:1 import not supported in script (and 3 more errors)", - "test/language/expressions/less-than-or-equal/S11.8.3_A4.12_T1.js-strict:true": "test/language/expressions/less-than-or-equal/S11.8.3_A4.12_T1.js: Test262Error: #10: (\"\\u{10000}\" <= \"\\uFFFF\") === true ", - "test/language/expressions/less-than/S11.8.1_A4.12_T1.js-strict:true": "test/language/expressions/less-than/S11.8.1_A4.12_T1.js: Test262Error: #9: (\"\\uD800\" < \"\\uDC00\") === true ", "test/language/expressions/optional-chaining/iteration-statement-for-await-of.js-strict:true": "test/language/expressions/optional-chaining/iteration-statement-for-await-of.js: test/language/expressions/optional-chaining/iteration-statement-for-await-of.js: Line 32:7 Unexpected token await (and 9 more errors)", "test/language/expressions/optional-chaining/member-expression.js-strict:true": "test/language/expressions/optional-chaining/member-expression.js: SyntaxError: Async generators are not supported yet ", - "test/language/identifiers/part-unicode-15.1.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-class-escaped.js: test/language/identifiers/part-unicode-15.1.0-class-escaped.js: Line 20:22 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-15.1.0-class.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-class.js: test/language/identifiers/part-unicode-15.1.0-class.js: Line 17:11 Unexpected token ILLEGAL (and 4 more errors)", - "test/language/identifiers/part-unicode-15.1.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-15.1.0-escaped.js: test/language/identifiers/part-unicode-15.1.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-15.1.0.js-strict:true": "test/language/identifiers/part-unicode-15.1.0.js: test/language/identifiers/part-unicode-15.1.0.js: Line 15:12 Unexpected token ILLEGAL (and 3 more errors)", - "test/language/identifiers/part-unicode-16.0.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-class-escaped.js: test/language/identifiers/part-unicode-16.0.0-class-escaped.js: Line 20:10 Unexpected token ILLEGAL (and 3 more errors)", - "test/language/identifiers/part-unicode-16.0.0-class.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-class.js: test/language/identifiers/part-unicode-16.0.0-class.js: Line 17:5 Unexpected token ILLEGAL (and 133 more errors)", - "test/language/identifiers/part-unicode-16.0.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-16.0.0-escaped.js: test/language/identifiers/part-unicode-16.0.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-16.0.0.js-strict:true": "test/language/identifiers/part-unicode-16.0.0.js: test/language/identifiers/part-unicode-16.0.0.js: Line 15:6 Unexpected token ILLEGAL (and 132 more errors)", - "test/language/identifiers/part-unicode-17.0.0-class-escaped.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-class-escaped.js: test/language/identifiers/part-unicode-17.0.0-class-escaped.js: Line 20:10 Unexpected token ILLEGAL (and 48 more errors)", - "test/language/identifiers/part-unicode-17.0.0-class.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-class.js: test/language/identifiers/part-unicode-17.0.0-class.js: Line 17:5 Unexpected token ILLEGAL (and 54 more errors)", - "test/language/identifiers/part-unicode-17.0.0-escaped.js-strict:true": "test/language/identifiers/part-unicode-17.0.0-escaped.js: test/language/identifiers/part-unicode-17.0.0-escaped.js: Line 17:5 Unexpected token ILLEGAL (and 1 more errors)", - "test/language/identifiers/part-unicode-17.0.0.js-strict:true": "test/language/identifiers/part-unicode-17.0.0.js: test/language/identifiers/part-unicode-17.0.0.js: Line 15:6 Unexpected token ILLEGAL (and 53 more errors)", "test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/exotic-object-behavior.js: Line 27:14 Unexpected token *", "test/language/import/import-defer/deferred-namespace-object/identity.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/identity.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/identity.js: Line 36:14 Unexpected token *", "test/language/import/import-defer/deferred-namespace-object/to-string-tag.js-strict:true": "test/language/import/import-defer/deferred-namespace-object/to-string-tag.js: file:///TestTC39/test262/test/language/import/import-defer/deferred-namespace-object/to-string-tag.js: Line 29:14 Unexpected token *", @@ -374,6 +316,11 @@ "test/language/literals/regexp/invalid-optional-negative-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-optional-negative-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/invalid-range-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-range-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/invalid-range-negative-lookbehind.js-strict:true": "test/language/literals/regexp/invalid-range-negative-lookbehind.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2-u.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-2.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier-u.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js-strict:true": "test/language/literals/regexp/named-groups/invalid-duplicate-groupspecifier.js: error is not an object (Test262: This statement should not be evaluated.)", + "test/language/literals/regexp/named-groups/invalid-identity-escape-in-capture-u.js-strict:true": "test/language/literals/regexp/named-groups/invalid-identity-escape-in-capture-u.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/u-case-mapping.js-strict:true": "test/language/literals/regexp/u-case-mapping.js: Test262Error: Case mapping is not applied in the absence of the `u` flag Expected SameValue(«true», «false») to be true ", "test/language/literals/regexp/u-invalid-class-escape.js-strict:true": "test/language/literals/regexp/u-invalid-class-escape.js: error is not an object (Test262: This statement should not be evaluated.)", "test/language/literals/regexp/u-invalid-extended-pattern-char.js-strict:true": "test/language/literals/regexp/u-invalid-extended-pattern-char.js: error is not an object (Test262: This statement should not be evaluated.)", @@ -426,7 +373,6 @@ "test/language/statements/with/get-binding-value-call-with-proxy-env.js-strict:false": "test/language/statements/with/get-binding-value-call-with-proxy-env.js: Test262Error: Actual [has:Object, get:Symbol(Symbol.unscopables), get:Object] and expected [has:Object, get:Symbol(Symbol.unscopables), has:Object, get:Object] should have the same contents. ", "test/language/statements/with/get-binding-value-idref-with-proxy-env.js-strict:false": "test/language/statements/with/get-binding-value-idref-with-proxy-env.js: Test262Error: Actual [has:Object, get:Symbol(Symbol.unscopables), get:Object] and expected [has:Object, get:Symbol(Symbol.unscopables), has:Object, get:Object] should have the same contents. ", "test/language/statements/with/get-mutable-binding-binding-deleted-in-get-unscopables-strict-mode.js-strict:false": "test/language/statements/with/get-mutable-binding-binding-deleted-in-get-unscopables-strict-mode.js: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all ", - "test/language/statements/with/set-mutable-binding-binding-deleted-with-typed-array-in-proto-chain.js-strict:false": "test/language/statements/with/set-mutable-binding-binding-deleted-with-typed-array-in-proto-chain.js: Test262Error: Expected SameValue(«[object Object]», «undefined») to be true ", "test/language/statements/with/set-mutable-binding-idref-compound-assign-with-proxy-env.js-strict:false": "test/language/statements/with/set-mutable-binding-idref-compound-assign-with-proxy-env.js: Test262Error: Actual [has:p, get:Symbol(Symbol.unscopables), get:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] and expected [has:p, get:Symbol(Symbol.unscopables), has:p, get:p, has:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] should have the same contents. ", "test/language/statements/with/set-mutable-binding-idref-with-proxy-env.js-strict:false": "test/language/statements/with/set-mutable-binding-idref-with-proxy-env.js: Test262Error: Actual [has:p, get:Symbol(Symbol.unscopables), set:p, getOwnPropertyDescriptor:p, defineProperty:p] and expected [has:p, get:Symbol(Symbol.unscopables), has:p, set:p, getOwnPropertyDescriptor:p, defineProperty:p] should have the same contents. " } diff --git a/internal/js/tc39/tc39_test.go b/internal/js/tc39/tc39_test.go index 02a96c951..88abe45b6 100644 --- a/internal/js/tc39/tc39_test.go +++ b/internal/js/tc39/tc39_test.go @@ -54,15 +54,13 @@ var ( false) featuresBlockList = []string{ - "IsHTMLDDA", // not supported at all - "async-iteration", // not supported at all - "String.prototype.replaceAll", // not supported at all, Stage 4 since 2020 - "dynamic-import", // not support in k6 + "IsHTMLDDA", // not supported at all + "async-iteration", // not supported at all + "dynamic-import", // not support in k6 // from Sobek "Symbol.asyncIterator", "resizable-arraybuffer", - "regexp-named-groups", "regexp-duplicate-named-groups", "regexp-unicode-property-escapes", "regexp-match-indices", @@ -72,7 +70,6 @@ var ( "tail-call-optimization", "Temporal", "import-assertions", - "logical-assignment-operators", "Atomics", "Atomics.waitAsync", "Atomics.pause", @@ -88,10 +85,8 @@ var ( "regexp-v-flag", "iterator-helpers", "symbols-as-weakmap-keys", - "uint8array-base64", "String.prototype.toWellFormed", "explicit-resource-management", - "set-methods", "promise-try", "promise-with-resolvers", "array-grouping", diff --git a/internal/js/tc55/timers/timers.go b/internal/js/tc55/timers/timers.go index 40a089013..e11b1402b 100644 --- a/internal/js/tc55/timers/timers.go +++ b/internal/js/tc55/timers/timers.go @@ -20,7 +20,7 @@ type timers struct { timerIDCounter uint64 - timers map[uint64]time.Time + timers map[uint64]*timer // Maybe in the future if this moves to core it will be expanded to have multiple queues queue *timerQueue @@ -46,7 +46,7 @@ func SetupGlobally(vu modules.VU) error { func newTimers(vu modules.VU) *timers { return &timers{ vu: vu, - timers: make(map[uint64]time.Time), + timers: make(map[uint64]*timer), queue: new(timerQueue), } } @@ -81,6 +81,7 @@ func (e *timers) call(callback sobek.Callable, args []sobek.Value) error { func (e *timers) setTimeout(callback sobek.Callable, delay float64, args ...sobek.Value) uint64 { id := e.nextID() + callback = common.CaptureMetricContext(e.vu.State()).WrapSobekCallable(callback) e.timerInitialization(callback, delay, args, false, id) return id } @@ -111,6 +112,7 @@ func (e *timers) freeEventLoopIfPossible() { func (e *timers) setInterval(callback sobek.Callable, delay float64, args ...sobek.Value) uint64 { id := e.nextID() + callback = common.CaptureMetricContext(e.vu.State()).WrapSobekCallable(callback) e.timerInitialization(callback, delay, args, true, id) return id } @@ -121,6 +123,8 @@ func (e *timers) clearInterval(id uint64) { // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timer-initialisation-steps // NOTE: previousId from the specification is always send and it is basically id +// +// Repeated timers must reuse the registration-time metric context when they rearm. func (e *timers) timerInitialization( callback sobek.Callable, timeout float64, args []sobek.Value, repeat bool, id uint64, ) { @@ -138,40 +142,22 @@ func (e *timers) timerInitialization( common.Throw(e.vu.Runtime(), fmt.Errorf("%s's callback isn't a callable function", name)) } - task := func() error { - // Specification 8.1: If id does not exist in global's map of active timers, then abort these steps. - if _, exist := e.timers[id]; !exist { - return nil - } - - err := e.call(callback, args) - - if _, exist := e.timers[id]; !exist { // 8.4 - return err - } - - if repeat { - e.timerInitialization(callback, timeout, args, repeat, id) - } else { - delete(e.timers, id) - } - - return err - } - - e.runAfterTimeout(&timer{ + t := &timer{ id: id, - task: task, nextTrigger: time.Now().Add(time.Duration(timeout * float64(time.Millisecond))), name: name, - }) + } + t.task = func() error { + return e.runTask(t, callback, timeout, args, repeat) + } + e.runAfterTimeout(t) } // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#run-steps-after-a-timeout // Notes: this just takes timers as makes the implementation way easier and we do not currently need // most of the functionality provided func (e *timers) runAfterTimeout(t *timer) { - e.timers[t.id] = t.nextTrigger + e.timers[t.id] = t // as we have only one orderingId we have one queue index := e.queue.add(t) @@ -183,13 +169,23 @@ func (e *timers) runAfterTimeout(t *timer) { e.setupTaskTimeout() } -func (e *timers) runFirstTask() error { - t := e.queue.pop() - if t == nil { - return nil // everything was cleared +func (e *timers) runTask( + t *timer, callback sobek.Callable, timeout float64, args []sobek.Value, repeat bool, +) error { + if e.timers[t.id] != t { + return nil // the timer was cleared after its task was queued } + e.queue.remove(t.id) - err := t.task() + err := e.call(callback, args) + + if e.timers[t.id] == t { + if repeat { + e.timerInitialization(callback, timeout, args, repeat, t.id) + } else { + delete(e.timers, t.id) + } + } if e.queue.length() > 0 { e.setupTaskTimeout() @@ -202,14 +198,15 @@ func (e *timers) runFirstTask() error { func (e *timers) setupTaskTimeout() { e.queue.stopTimer() - delay := -time.Since(e.timers[e.queue.first().id]) + t := e.queue.first() + delay := -time.Since(t.nextTrigger) if e.taskQueue == nil { e.taskQueue = taskqueue.New(e.vu.RegisterCallback) e.setupTaskQueueCloserOnIterationEnd() } q := e.taskQueue e.queue.head = time.AfterFunc(delay, func() { - q.Queue(e.runFirstTask) + q.Queue(t.task) }) } @@ -264,7 +261,7 @@ func (e *timers) setupTaskQueueCloserOnIterationEnd() { }) q.Close() case <-ch: - e.timers = make(map[uint64]time.Time) + e.timers = make(map[uint64]*timer) e.queue.stopTimer() e.queue = new(timerQueue) e.taskQueue = nil @@ -315,16 +312,6 @@ func (tq *timerQueue) remove(id uint64) { }) } -func (tq *timerQueue) pop() *timer { - length := len(tq.queue) - if length == 0 { - return nil - } - t := tq.queue[0] - tq.queue = slices.Delete(tq.queue, 0, 1) - return t -} - func (tq *timerQueue) length() int { return len(tq.queue) } diff --git a/internal/js/tc55/timers/timers_test.go b/internal/js/tc55/timers/timers_test.go index 7ff45e3f9..0519ea003 100644 --- a/internal/js/tc55/timers/timers_test.go +++ b/internal/js/tc55/timers/timers_test.go @@ -8,7 +8,10 @@ import ( "github.com/stretchr/testify/require" + "go.k6.io/k6/v2/internal/features" "go.k6.io/k6/v2/js/modulestest" + "go.k6.io/k6/v2/lib" + "go.k6.io/k6/v2/metrics" ) func newRuntime(t testing.TB) *modulestest.Runtime { @@ -204,6 +207,91 @@ func TestSetTimeoutContextCancel(t *testing.T) { } } +func newGroupRuntime(t testing.TB) (*modulestest.Runtime, *[]string) { + t.Helper() + runtime := modulestest.NewRuntime(t) + + registry := metrics.NewRegistry() + state := &lib.State{ + FeatureFlags: &features.Flags{AsyncMetricContext: true}, + Options: lib.Options{ + SystemTags: &metrics.DefaultSystemTagSet, + }, + Tags: lib.NewVUStateTags(registry.RootTagSet().WithTagsFromMap( + map[string]string{"group": lib.RootGroupPath})), + BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry), + } + runtime.MoveToVUContext(state) + + rt := runtime.VU.Runtime() + require.NoError(t, rt.Set("setcontext", func(group, phase, trace string) { + state.Tags.Modify(func(tm *metrics.TagsAndMeta) { + tm.SetSystemTagOrMeta(metrics.TagGroup, group) + tm.SetTag("phase", phase) + tm.SetMetadata("trace", trace) + }) + })) + got := new([]string) + require.NoError(t, rt.Set("record", func() { + current := state.Tags.GetCurrentValues() + group, _ := current.Tags.Get("group") + phase, _ := current.Tags.Get("phase") + *got = append(*got, group+"|"+phase+"|"+current.Metadata["trace"]) + })) + return runtime, got +} + +func TestSetTimeoutGroupTag(t *testing.T) { + t.Parallel() + + runtime, got := newGroupRuntime(t) + + _, err := runtime.RunOnEventLoop(` + setcontext("::mygroup", "registered", "registered"); + setTimeout(() => { record(); }, 0); + setcontext("", "active", "active"); + setTimeout(() => { record(); }, 1); + record(); + `) + require.NoError(t, err) + require.Equal(t, []string{"|active|active", "::mygroup|registered|registered", "|active|active"}, *got) + current := runtime.VU.State().Tags.GetCurrentValues() + liveGroup, _ := current.Tags.Get("group") + require.Equal(t, lib.RootGroupPath, liveGroup) + phase, _ := current.Tags.Get("phase") + require.Equal(t, "active", phase) + require.Equal(t, "active", current.Metadata["trace"]) +} + +func TestSetIntervalGroupTag(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + runtime, got := newGroupRuntime(t) + + _, err := runtime.RunOnEventLoop(` + var i = 0; + setcontext("::mygroup", "registered", "registered"); + let s = setInterval(() => { + record(); + setcontext("::callback", "callback", "callback"); + i++; + if (i > 2) { clearInterval(s); } + }, 1); + setcontext("", "active", "active"); + `) + require.NoError(t, err) + require.Equal(t, []string{ + "::mygroup|registered|registered", + "::mygroup|registered|registered", + "::mygroup|registered|registered", + }, *got) + current := runtime.VU.State().Tags.GetCurrentValues() + phase, _ := current.Tags.Get("phase") + require.Equal(t, "active", phase) + require.Equal(t, "active", current.Metadata["trace"]) + }) +} + func TestClearFirstTimeoutWhenMultiple(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { @@ -225,3 +313,125 @@ func TestClearFirstTimeoutWhenMultiple(t *testing.T) { require.GreaterOrEqual(t, log[0].Sub(start), time.Second) }) } + +func TestClearExpiredFirstTimerBeforeItsTaskRuns(t *testing.T) { + t.Parallel() + tests := []struct { + name string + script string + }{ + { + name: "clear timeout with clearTimeout", + script: ` + var canceledTimer = setTimeout(() => {}, 10); + sleep(20_000_000); + clearTimeout(canceledTimer); + `, + }, + { + name: "clear timeout with clearInterval", + script: ` + var canceledTimer = setTimeout(() => {}, 10); + sleep(20_000_000); + clearInterval(canceledTimer); + `, + }, + { + name: "clear interval with clearTimeout", + script: ` + var canceledTimer = setInterval(() => {}, 10); + sleep(20_000_000); + clearTimeout(canceledTimer); + `, + }, + { + name: "clear interval with clearInterval", + script: ` + var canceledTimer = setInterval(() => {}, 10); + sleep(20_000_000); + clearInterval(canceledTimer); + `, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + synctest.Test(t, func(t *testing.T) { + runtime := newRuntime(t) + rt := runtime.VU.Runtime() + var firedAt time.Time + + require.NoError(t, rt.Set("sleep", time.Sleep)) + require.NoError(t, rt.Set("record", func() { firedAt = time.Now() })) + + for i := range 1000 { + firedAt = time.Time{} + start := time.Now() + _, err := runtime.RunOnEventLoop("setTimeout(record, 1000);" + test.script) + require.NoErrorf(t, err, "iteration %d", i) + require.Falsef(t, firedAt.IsZero(), "iteration %d", i) + require.GreaterOrEqualf(t, firedAt.Sub(start), time.Second, "iteration %d", i) + } + }) + }) + } +} + +func TestClearExpiredOnlyTimerBeforeItsTaskRuns(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + runtime := newRuntime(t) + rt := runtime.VU.Runtime() + + require.NoError(t, rt.Set("sleep", time.Sleep)) + + for i := range 1000 { + _, err := runtime.RunOnEventLoop(` + var canceledTimer = setTimeout(() => { + throw new Error("canceled timer ran"); + }, 10); + sleep(20_000_000); + clearTimeout(canceledTimer); + `) + require.NoErrorf(t, err, "iteration %d", i) + } + }) +} + +func TestClearedExpiredTimerDoesNotRunNewTimersEarly(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + runtime := newRuntime(t) + rt := runtime.VU.Runtime() + type call struct { + name string + firedAt time.Time + } + var calls []call + + require.NoError(t, rt.Set("sleep", time.Sleep)) + require.NoError(t, rt.Set("record", func(name string) { + calls = append(calls, call{name: name, firedAt: time.Now()}) + })) + + for i := range 1000 { + calls = calls[:0] + start := time.Now() + _, err := runtime.RunOnEventLoop(` + var canceledTimer = setTimeout(() => record("canceled"), 10); + sleep(20_000_000); + clearTimeout(canceledTimer); + setTimeout(record, 100, "first"); + setTimeout(record, 200, "second"); + `) + require.NoErrorf(t, err, "iteration %d", i) + require.Lenf(t, calls, 2, "iteration %d", i) + require.Equalf(t, "first", calls[0].name, "iteration %d", i) + require.Equalf(t, "second", calls[1].name, "iteration %d", i) + require.GreaterOrEqualf(t, calls[0].firedAt.Sub(start), 100*time.Millisecond, "iteration %d", i) + require.GreaterOrEqualf(t, calls[1].firedAt.Sub(start), 200*time.Millisecond, "iteration %d", i) + } + }) +} diff --git a/internal/js/timeout_error.go b/internal/js/timeout_error.go index 0e2bac596..eb3178121 100644 --- a/internal/js/timeout_error.go +++ b/internal/js/timeout_error.go @@ -41,6 +41,9 @@ func (t timeoutError) Hint() string { hint = "You can increase the time limit via the setupTimeout option" case consts.TeardownFn: hint = "You can increase the time limit via the teardownTimeout option" + case consts.HandleSummaryFn: + hint = "You can increase the time limit via the handleSummaryTimeout option" + + " or the K6_HANDLE_SUMMARY_TIMEOUT environment variable" } return hint } diff --git a/internal/lib/summary/machine_readable.go b/internal/lib/summary/machine_readable.go index 9a38c6824..9ad4020bd 100644 --- a/internal/lib/summary/machine_readable.go +++ b/internal/lib/summary/machine_readable.go @@ -70,9 +70,8 @@ func machineReadableSummaryResultsMetricsBuilder(metrics Metrics) []cog.Builder[ val := reflect.ValueOf(metrics) metricBuilders := make([]cog.Builder[machinereadable.Metric], 0) - for i := 0; i < val.NumField(); i++ { - value := val.Field(i) - metricsMap, isMetricsMap := value.Interface().(map[string]Metric) + for _, value := range val.Fields() { + metricsMap, isMetricsMap := reflect.TypeAssert[map[string]Metric](value) if !isMetricsMap { continue // Theoretically, this should never happen as all the struct fields satisfy the type. } diff --git a/internal/lib/testutils/httpmultibin/grpc_wrappers_testing/service.go b/internal/lib/testutils/httpmultibin/grpc_wrappers_testing/service.go index a0003e4c4..ac6f74c00 100644 --- a/internal/lib/testutils/httpmultibin/grpc_wrappers_testing/service.go +++ b/internal/lib/testutils/httpmultibin/grpc_wrappers_testing/service.go @@ -1,5 +1,5 @@ // Package grpc_wrappers_testing provides a test service that could be used for the testing gRPC wrappers -package grpc_wrappers_testing //nolint:revive // we want to be consistent with the other packages +package grpc_wrappers_testing import ( context "context" diff --git a/internal/lib/testutils/httpmultibin/httpmultibin.go b/internal/lib/testutils/httpmultibin/httpmultibin.go index 437644f0f..09063b917 100644 --- a/internal/lib/testutils/httpmultibin/httpmultibin.go +++ b/internal/lib/testutils/httpmultibin/httpmultibin.go @@ -359,12 +359,21 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin { }) require.NoError(t, err) - // Pre-configure the HTTP client transport with the dialer and TLS config (incl. HTTP2 support) + // Pre-configure the HTTP client transport with the dialer and TLS config. + // + // Advertise both h2 and http/1.1 so the client can reach both the HTTP/2 server and the + // http/1.1-only HTTPS server. ForceAttemptHTTP2 tells net/http to negotiate HTTP/2 even + // when a custom DialContext/TLSClientConfig is set (see Go issue #14275). + // + // Clone the shared TLS config before widening NextProtos so the client-only protocol list + // does not leak into other users of the config. + clientTLSConfig := tlsConfig.Clone() + clientTLSConfig.NextProtos = []string{http2.NextProtoTLS, "http/1.1"} transport := &http.Transport{ - DialContext: dialer.DialContext, - TLSClientConfig: tlsConfig, + DialContext: dialer.DialContext, + TLSClientConfig: clientTLSConfig, + ForceAttemptHTTP2: true, } - require.NoError(t, http2.ConfigureTransport(transport)) ctx, ctxCancel := context.WithCancel(context.Background()) diff --git a/internal/lib/testutils/tlstest/chain.go b/internal/lib/testutils/tlstest/chain.go new file mode 100644 index 000000000..b2caf917f --- /dev/null +++ b/internal/lib/testutils/tlstest/chain.go @@ -0,0 +1,160 @@ +// Package tlstest provides shared helpers for building root CA → intermediate → leaf +// chains used by AIA fetching tests. Test-only, do not import in production code. +package tlstest + +import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "math/big" + "net" + "net/http" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +// Chain holds root CA → intermediate → leaf material. The leaf DER is generated on demand +// (LeafTLSCertificate) so callers can bake in an AIA URL first via SetAIAURL. +type Chain struct { + RootCert *x509.Certificate + RootDER []byte + RootKey *ecdsa.PrivateKey + IntermediateCert *x509.Certificate + IntermediateDER []byte + IntermediateKey *ecdsa.PrivateKey + LeafKey *ecdsa.PrivateKey + LeafTmpl *x509.Certificate + RootPool *x509.CertPool +} + +// NewChain builds a fresh root, intermediate and leaf template. Certificates use short +// (24h) lifetimes and P-256 keys. +func NewChain(t testing.TB) *Chain { + t.Helper() + + genKey := func() *ecdsa.PrivateKey { + k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + require.NoError(t, err) + return k + } + serial := func() *big.Int { + n, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) + require.NoError(t, err) + return n + } + + rootKey := genKey() + rootTmpl := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: "tlstest Root CA"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(24 * time.Hour), + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, + BasicConstraintsValid: true, + IsCA: true, + } + rootDER, err := x509.CreateCertificate(rand.Reader, rootTmpl, rootTmpl, &rootKey.PublicKey, rootKey) + require.NoError(t, err) + rootCert, err := x509.ParseCertificate(rootDER) + require.NoError(t, err) + + interKey := genKey() + interTmpl := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: "tlstest Intermediate CA"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(24 * time.Hour), + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, + BasicConstraintsValid: true, + IsCA: true, + } + interDER, err := x509.CreateCertificate(rand.Reader, interTmpl, rootCert, &interKey.PublicKey, rootKey) + require.NoError(t, err) + interCert, err := x509.ParseCertificate(interDER) + require.NoError(t, err) + + leafKey := genKey() + leafTmpl := &x509.Certificate{ + SerialNumber: serial(), + Subject: pkix.Name{CommonName: "localhost"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(24 * time.Hour), + KeyUsage: x509.KeyUsageDigitalSignature, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + DNSNames: []string{"localhost"}, + IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}, + } + + rootPool := x509.NewCertPool() + rootPool.AddCert(rootCert) + + return &Chain{ + RootCert: rootCert, + RootDER: rootDER, + RootKey: rootKey, + IntermediateCert: interCert, + IntermediateDER: interDER, + IntermediateKey: interKey, + LeafKey: leafKey, + LeafTmpl: leafTmpl, + RootPool: rootPool, + } +} + +// SetAIAURL sets the leaf's IssuingCertificateURL. Call before LeafTLSCertificate. +func (c *Chain) SetAIAURL(aiaURL string) { + c.LeafTmpl.IssuingCertificateURL = []string{aiaURL} +} + +// LeafTLSCertificate returns a tls.Certificate containing only the leaf (no intermediate). +func (c *Chain) LeafTLSCertificate(t testing.TB) tls.Certificate { + t.Helper() + leafDER, err := x509.CreateCertificate( + rand.Reader, c.LeafTmpl, c.IntermediateCert, &c.LeafKey.PublicKey, c.IntermediateKey, + ) + require.NoError(t, err) + leafKeyDER, err := x509.MarshalECPrivateKey(c.LeafKey) + require.NoError(t, err) + cert, err := tls.X509KeyPair( + pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: leafDER}), + pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: leafKeyDER}), + ) + require.NoError(t, err) + return cert +} + +// FullChainTLSCertificate returns leaf + intermediate DER. +func (c *Chain) FullChainTLSCertificate(t testing.TB) tls.Certificate { + t.Helper() + cert := c.LeafTLSCertificate(t) + cert.Certificate = append(cert.Certificate, c.IntermediateCert.Raw) + return cert +} + +// AIAHandler serves the DER bytes set via SetCert with an application/pkix-cert Content-Type. +type AIAHandler struct { + mu sync.RWMutex + certDER []byte +} + +// SetCert atomically updates the DER bytes served by subsequent requests. +func (h *AIAHandler) SetCert(der []byte) { + h.mu.Lock() + h.certDER = der + h.mu.Unlock() +} + +// ServeHTTP writes the currently-set DER bytes as an AIA response. +func (h *AIAHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) { + h.mu.RLock() + defer h.mu.RUnlock() + w.Header().Set("Content-Type", "application/pkix-cert") + _, _ = w.Write(h.certDER) +} diff --git a/internal/loader/loader.go b/internal/loader/loader.go index d7c6cf044..5cedd2ac4 100644 --- a/internal/loader/loader.go +++ b/internal/loader/loader.go @@ -41,7 +41,7 @@ type unresolvableURLError string func (u unresolvableURLError) Error() string { // TODO potentially add more things about what k6 supports if users report being confused. - return fmt.Sprintf(`The moduleSpecifier %q couldn't be recognised as something k6 supports.`, (string)(u)) + return fmt.Sprintf(`The moduleSpecifier %q couldn't be recognised as something k6 supports.`, string(u)) } // Resolve a relative path to an absolute one. @@ -210,7 +210,7 @@ func fetch(logger logrus.FieldLogger, u string) ([]byte, error) { if err != nil { return nil, err } - res, err := http.DefaultClient.Do(req) //nolint:gosec + res, err := http.DefaultClient.Do(req) if err != nil { return nil, err } diff --git a/internal/loader/loader_test.go b/internal/loader/loader_test.go index 35a8fb70d..c8b8ffd20 100644 --- a/internal/loader/loader_test.go +++ b/internal/loader/loader_test.go @@ -210,7 +210,7 @@ func TestLoad(t *testing.T) { pwdURL, err := url.Parse(sr("HTTPSBIN_URL")) require.NoError(t, err) - moduleSpecifier := ("./robots.txt") + moduleSpecifier := "./robots.txt" moduleSpecifierURL, err := loader.Resolve(pwdURL, moduleSpecifier) require.NoError(t, err) diff --git a/internal/loader/readsource.go b/internal/loader/readsource.go index 3586c484f..623314209 100644 --- a/internal/loader/readsource.go +++ b/internal/loader/readsource.go @@ -47,9 +47,8 @@ func ReadSource( srcURL, err := Resolve(pwdURL, filepath.ToSlash(src)) if err != nil { - var unresolvedError unresolvableURLError - if errors.As(err, &unresolvedError) { - return nil, fmt.Errorf(fileSchemeCouldntBeLoadedMsg, (string)(unresolvedError)) //nolint:staticcheck + if unresolvedError, ok := errors.AsType[unresolvableURLError](err); ok { + return nil, fmt.Errorf(fileSchemeCouldntBeLoadedMsg, string(unresolvedError)) //nolint:staticcheck } return nil, err } diff --git a/internal/loader/readsource_test.go b/internal/loader/readsource_test.go index 3ba67b348..579e2289e 100644 --- a/internal/loader/readsource_test.go +++ b/internal/loader/readsource_test.go @@ -17,7 +17,7 @@ import ( type errorReader string func (e errorReader) Read(_ []byte) (int, error) { - return 0, errors.New((string)(e)) + return 0, errors.New(string(e)) } var _ io.Reader = errorReader("") diff --git a/internal/log/cloud/cloud.go b/internal/log/cloud/cloud.go new file mode 100644 index 000000000..6b328c374 --- /dev/null +++ b/internal/log/cloud/cloud.go @@ -0,0 +1,289 @@ +// Package cloudlog implements a logrus hook that pushes k6's own logs to the +// Grafana Cloud k6 logs backend for 'k6 cloud run --local-execution'. +// +// The package lives in directory internal/log/cloud but is named cloudlog +// (not cloud) so that call sites which also import internal/secretsource/cloud +// do not have a package-name collision. +// +// It takes a plain Config struct and does not import cloudapi or +// internal/cloudapi/provisioning (avoiding an import cycle); the cmd layer +// translates the provisioning response or environment into Config. +package cloudlog + +import ( + "context" + "fmt" + "slices" + "sync" + "sync/atomic" + "time" + + "github.com/sirupsen/logrus" + + "go.k6.io/k6/v2/internal/log" +) + +// bufferCap bounds the pre-configuration entry buffer. It matches the loki +// hook's own channel capacity so Fire stays non-blocking under the same load. +const bufferCap = 1000 + +// testRunIDLabel is the stream label the backend requires on every push: a +// push whose stream lacks it, or carries a value not matching the token's run, +// is rejected with 401. +const testRunIDLabel = "test_run_id" + +// Config is the log-push configuration, translated by the cmd layer from the +// provisioning response or the environment. It intentionally depends on no +// cloudapi or provisioning types. +type Config struct { + PushURL string // loki push endpoint (empty => unconfigured) + Token string // scoped test-run token, sent as Authorization: Bearer + TestRunID string // added as the required test_run_id stream label + Level string // minimum level; "" => all levels + Limit int // max entries per push period + PushPeriod time.Duration // how often batches are flushed + MsgMaxSize int // per-message truncation size + AllowedLabels []string // stream labels to keep; empty => keep all +} + +// Pusher is a logrus AsyncHook that buffers log entries until SetConfig +// supplies the push URL and scoped token (available only after provisioning), +// then forwards buffered and subsequent entries to the cloud through an +// internal/log loki hook. It mirrors the cloud secret source's pre-register + +// SetConfig + lazy-init pattern. +type Pusher struct { + fallbackLogger logrus.FieldLogger + cfg atomic.Pointer[Config] + ready chan struct{} // closed once by SetConfig when configured + readyOnce sync.Once + buf chan *logrus.Entry + dropped atomic.Int64 + drainCh chan struct{} // closed once by Drain to request a final flush + drainOnce sync.Once + doneCh chan struct{} // closed by Listen on return, signalling Drain +} + +// Compile-time check that Pusher is usable as a logrus async hook. +var _ log.AsyncHook = (*Pusher)(nil) + +// New creates a Pusher that buffers entries until SetConfig configures it. +func New(fallbackLogger logrus.FieldLogger) *Pusher { + return &Pusher{ + fallbackLogger: fallbackLogger, + ready: make(chan struct{}), + buf: make(chan *logrus.Entry, bufferCap), + drainCh: make(chan struct{}), + doneCh: make(chan struct{}), + } +} + +// SetConfig stores the push configuration and signals readiness once. It is +// safe to call before or after Listen starts. An empty PushURL, TestRunID, or +// Token leaves the Pusher unconfigured (it keeps buffering and never pushes), +// since the backend requires all three. +func (p *Pusher) SetConfig(c Config) { + if c.PushURL == "" || c.TestRunID == "" || c.Token == "" { + if p.fallbackLogger != nil { + p.fallbackLogger.Debug("cloud log push not configured: missing push URL, test run ID, or token") + } + return + } + p.cfg.Store(&c) + p.readyOnce.Do(func() { close(p.ready) }) +} + +// Levels reports all levels; the underlying loki hook re-filters by its own +// configured level. +func (p *Pusher) Levels() []logrus.Level { + return logrus.AllLevels +} + +// Fire buffers the entry without blocking. If the buffer is full the entry is +// dropped and counted, so the logging path is never blocked. +func (p *Pusher) Fire(entry *logrus.Entry) error { + select { + case p.buf <- entry: + default: + p.dropped.Add(1) + } + return nil +} + +// Drain flushes buffered logs to the backend and returns once the flush has +// completed (or ctx is done). It is safe to call when unconfigured (a no-op) +// and idempotent. Best-effort: entries still in flight inside the loki hook's +// channel at flush time may not be sent (see the package notes). +func (p *Pusher) Drain(ctx context.Context) error { + select { + case <-p.ready: + default: + return nil // never configured => nothing to flush + } + p.drainOnce.Do(func() { close(p.drainCh) }) + select { + case <-p.doneCh: + return nil + case <-ctx.Done(): + return ctx.Err() + } +} + +// Listen blocks until SetConfig has configured the Pusher or ctx is done. If +// ctx fires first (e.g. --no-cloud-logs or an early exit) it returns without +// pushing. Once configured it builds the loki hook and forwards buffered and +// subsequent entries to it. A Drain request or a ctx cancel both forward the +// buffered entries and trigger the loki hook's synchronous final flush; Listen +// returns only after that flush has completed. It always closes doneCh on +// return, so a concurrent Drain never blocks. +// +// The loki hook deliberately runs on its own background-derived context (not +// ctx) so its final flush is triggered only after buffered entries have been +// forwarded; hence the contextcheck exception. +func (p *Pusher) Listen(ctx context.Context) { //nolint:contextcheck + defer close(p.doneCh) + + select { + case <-p.ready: + case <-ctx.Done(): + return + } + + c := p.cfg.Load() + + // Level is intentionally omitted here: cloudlog filters by level itself in + // forward (below), so the hook's own Levels() dispatch is unused. Handing an + // invalid backend level to NewLokiHook would make it fail and disable the + // whole push, so we parse the level ourselves and fall back gracefully. + lokiHook, err := log.NewLokiHook(p.fallbackLogger, log.LokiHookOptions{ + Addr: c.PushURL, + Limit: c.Limit, + PushPeriod: c.PushPeriod, + MsgMaxSize: c.MsgMaxSize, + AllowedLabels: allowedLabelsWithTestRunID(c.AllowedLabels), + Labels: [][2]string{{testRunIDLabel, c.TestRunID}}, + Headers: [][2]string{{"Authorization", "Bearer " + c.Token}}, + }) + if err != nil { + if p.fallbackLogger != nil { + p.fallbackLogger.WithError(err).Error("cloud log push disabled: could not build the loki hook") + } + return + } + + // The loki hook's Fire enqueues unconditionally (its Levels() dispatch is + // bypassed on the forward path), so filter by the configured level here, + // where the level is known. logrus orders levels by severity (lower = + // more severe), so an entry passes iff it is at least as severe as the + // configured level: e.Level <= minLevel. An unset level keeps all. + minLevel := logrus.TraceLevel // "" => keep all levels (defer to the logger) + if c.Level != "" { + if lvl, err := logrus.ParseLevel(c.Level); err == nil { + minLevel = lvl + } else { + // The level comes from the backend, not the user, so a bad value + // falls back to info (the backend's own default) rather than + // pushing everything or disabling the push entirely. + minLevel = logrus.InfoLevel + if p.fallbackLogger != nil { + p.fallbackLogger.WithField("level", c.Level). + Warn("invalid cloud log level; defaulting to info") + } + } + } + + forward := func(e *logrus.Entry) { + if e.Level <= minLevel { + _ = lokiHook.Fire(e) + } + } + + // The loki hook runs on its own context so the final flush is triggered + // only after buffered entries have been forwarded (see finish), not + // concurrently with the forwarding. + lokiCtx, lokiCancel := context.WithCancel(context.Background()) + lokiDone := make(chan struct{}) + go func() { + lokiHook.Listen(lokiCtx) + close(lokiDone) + }() + + // finish forwards everything currently buffered, then cancels the loki + // hook so it runs its synchronous final flush, and waits for that to + // complete. lokiHook.Fire is a blocking send, so forwarding must happen + // while the loki hook is still draining its channel (before lokiCancel). + // + // reportDropped runs last, after the final flush has been pushed: its + // notice shares the "warning" stream with the loki hook's own limit-drop + // message, so it must carry the newest timestamp and be pushed after it — + // otherwise Loki with unordered_writes=false rejects the older flush write. + finish := func() { + for { + select { + case e := <-p.buf: + forward(e) + default: + lokiCancel() + <-lokiDone + p.reportDropped(lokiHook) + return + } + } + } + + // Single consumer of buf, forwarding to the loki hook (the single producer + // stays Fire). A drain request or ctx cancel both run finish, then return; + // after a drain the later logger-shutdown ctx cancel is a no-op. + for { + select { + case e := <-p.buf: + forward(e) + case <-p.drainCh: + finish() + return + case <-ctx.Done(): + finish() + return + } + } +} + +// reportDropped surfaces the entries Fire dropped when the buffer was full, so +// the loss is visible rather than silent. The count is read once as a shutdown +// summary; drops after this point are not reported. +// +// It uses the loki hook's NoticePusher capability, which pushes the notice +// immediately and exempt from the hook's per-batch limit — otherwise a full +// final batch (many buffered entries at shutdown, exactly when drops are +// likely) would swallow the notice itself. +func (p *Pusher) reportDropped(h log.AsyncHook) { + n := p.dropped.Load() + if n == 0 { + return + } + + np, ok := h.(log.NoticePusher) + if !ok { + return + } + + msg := fmt.Sprintf("k6 dropped %d log messages because the cloud log buffer was full", n) + if err := np.PushNotice(msg); err != nil && p.fallbackLogger != nil { + p.fallbackLogger.WithError(err).Warn("could not push the dropped-cloud-logs notice") + } +} + +// allowedLabelsWithTestRunID ensures the required test_run_id label survives +// the loki hook's label filtering, which drops any label not in the allow-list +// when that list is non-empty. An empty list must normalise to nil: the loki +// hook treats nil as "keep all labels", whereas a non-nil empty slice (which +// an explicitly empty K6_CLOUD_LOGS_ALLOWED_LABELS decodes to) means "keep +// none" and would strip test_run_id from the stream, getting the push 401'd. +func allowedLabelsWithTestRunID(allowed []string) []string { + if len(allowed) == 0 { + return nil + } + if slices.Contains(allowed, testRunIDLabel) { + return allowed + } + return append(slices.Clone(allowed), testRunIDLabel) +} diff --git a/internal/log/cloud/cloud_test.go b/internal/log/cloud/cloud_test.go new file mode 100644 index 000000000..e616a6272 --- /dev/null +++ b/internal/log/cloud/cloud_test.go @@ -0,0 +1,621 @@ +package cloudlog + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/lib/testutils" +) + +// TestPusher is the canonical case: entries fired before and after SetConfig +// are buffered and then pushed to the configured endpoint, carrying the +// bearer token and the required test_run_id label. +func TestPusher(t *testing.T) { + t.Parallel() + + type received struct { + body string + auth string + } + recvCh := make(chan received, 1) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + select { + case recvCh <- received{body: string(b), auth: req.Header.Get("Authorization")}: + default: + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + + // Fired BEFORE SetConfig: must be buffered until the pusher is configured. + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "before-config"})) + + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "test-token", + TestRunID: "run-123", + PushPeriod: 20 * time.Millisecond, + }) + + // Fired AFTER SetConfig: still buffered until Listen drains. + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "after-config"})) + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + var got received + select { + case got = <-recvCh: + case <-time.After(2 * time.Second): + t.Fatal("no push received from the pusher") + } + + assert.Equal(t, "Bearer test-token", got.auth) + assert.Contains(t, got.body, "before-config") + assert.Contains(t, got.body, "after-config") + assert.Contains(t, got.body, `"test_run_id":"run-123"`) + + cancel() + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after ctx was cancelled") + } +} + +// TestPusher_UnconfiguredDrainsOnCtxDone covers the --no-cloud-logs / early +// exit path: without SetConfig, Listen returns promptly on ctx.Done and never +// pushes. +func TestPusher_UnconfiguredDrainsOnCtxDone(t *testing.T) { + t.Parallel() + + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + t.Errorf("pusher made an HTTP request while unconfigured") + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "buffered"})) + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + cancel() + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return promptly on ctx.Done while unconfigured") + } +} + +// TestPusher_FireNeverBlocks keeps the logging path non-blocking: with no +// Listen draining, Fire returns immediately even past the buffer capacity and +// over-cap entries are counted as dropped. +func TestPusher_FireNeverBlocks(t *testing.T) { + t.Parallel() + + p := New(testutils.NewLogger(t)) + + const overflow = 10 + total := bufferCap + overflow + + fired := make(chan struct{}) + go func() { + for range total { + _ = p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "x"}) + } + close(fired) + }() + + select { + case <-fired: + case <-time.After(2 * time.Second): + t.Fatal("Fire blocked when the buffer was full") + } + + assert.Equal(t, int64(overflow), p.dropped.Load()) +} + +// TestPusher_ReportsDroppedLogs proves the front-buffer drop counter is +// surfaced rather than silently discarded, even when the loki hook's own +// per-batch limit is already exhausted: the notice must bypass that limit. The +// limit is set well below the number of forwarded entries so a naive push +// (subject to the limit) would drop the notice itself. +// +// It also asserts push ordering: the notice shares the "warning" stream with +// the loki hook's own limit-drop message, so across pushes that stream's +// timestamps must be non-decreasing, otherwise Loki with unordered_writes=false +// rejects the older write. +func TestPusher_ReportsDroppedLogs(t *testing.T) { + t.Parallel() + + var ( + mu sync.Mutex + requests []string // request bodies, in the order the server received them + ) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + mu.Lock() + requests = append(requests, string(b)) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + const overflow = 7 + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-dropped", + // Deliberately far below bufferCap: forwarding the buffered entries + // exhausts the loki per-batch limit, so the notice must bypass it. + Limit: 10, + PushPeriod: time.Hour, // only the shutdown push + }) + + // Overflow the buffer before Listen consumes anything, so exactly `overflow` + // entries are dropped by Fire. + for range bufferCap + overflow { + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "x"})) + } + require.Equal(t, int64(overflow), p.dropped.Load()) + + ctx := t.Context() + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + require.NoError(t, p.Drain(context.Background())) + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after Drain") + } + + mu.Lock() + defer mu.Unlock() + + assert.Contains(t, strings.Join(requests, ""), + "dropped 7 log messages because the cloud log buffer was full", + "the front-buffer drop count must survive the loki per-batch limit") + + // Collect the "warning" stream timestamps in the order they were pushed and + // assert they never go backwards across requests. + type lokiPush struct { + Streams []struct { + Stream map[string]string `json:"stream"` + Values [][]string `json:"values"` + } `json:"streams"` + } + var warnTS []int64 + for _, body := range requests { + var lp lokiPush + require.NoError(t, json.Unmarshal([]byte(body), &lp)) + for _, s := range lp.Streams { + if s.Stream["level"] != logrus.WarnLevel.String() { + continue + } + for _, v := range s.Values { + require.GreaterOrEqual(t, len(v), 1) + ts, err := strconv.ParseInt(v[0], 10, 64) + require.NoError(t, err) + warnTS = append(warnTS, ts) + } + } + } + require.GreaterOrEqual(t, len(warnTS), 2, + "expected both the loki limit-drop warning and the cloud-buffer notice") + for i := 1; i < len(warnTS); i++ { + assert.GreaterOrEqual(t, warnTS[i], warnTS[i-1], + "warning-stream timestamps must not go backwards across pushes "+ + "(an out-of-order write is rejected by Loki with unordered_writes=false)") + } +} + +// TestPusher_TestRunIDLabelKept locks the backend's hard requirement: even +// when AllowedLabels omits test_run_id, the pushed stream still carries it as +// a label (the backend 401s a push whose stream lacks it). +func TestPusher_TestRunIDLabelKept(t *testing.T) { + t.Parallel() + + recvCh := make(chan string, 1) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + select { + case recvCh <- string(b): + default: + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-xyz", + PushPeriod: 20 * time.Millisecond, + AllowedLabels: []string{"level"}, // deliberately omits test_run_id + }) + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "hello"})) + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + select { + case body := <-recvCh: + assert.Contains(t, body, `"test_run_id":"run-xyz"`) + case <-time.After(2 * time.Second): + t.Fatal("no push received from the pusher") + } + + cancel() + <-listenDone +} + +// TestPusher_EmptyAllowedLabelsKeepsTestRunID guards the empty-slice edge: an +// explicitly empty K6_CLOUD_LOGS_ALLOWED_LABELS decodes to a non-nil []string{}, +// which must still keep the required test_run_id label rather than strip every +// label from the stream. +func TestPusher_EmptyAllowedLabelsKeepsTestRunID(t *testing.T) { + t.Parallel() + + recvCh := make(chan string, 1) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + select { + case recvCh <- string(b): + default: + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-empty-labels", + PushPeriod: 20 * time.Millisecond, + AllowedLabels: []string{}, // non-nil empty, as envconfig decodes "" + }) + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "hello"})) + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + select { + case body := <-recvCh: + assert.Contains(t, body, `"test_run_id":"run-empty-labels"`) + case <-time.After(2 * time.Second): + t.Fatal("no push received from the pusher") + } + + cancel() + <-listenDone +} + +// TestPusher_Drain covers the pre-notify drain: after entries are pushed while +// the run is open, Drain returns without hanging, drives Listen to return even +// though ctx was never cancelled, and is idempotent on a second call. +func TestPusher_Drain(t *testing.T) { + t.Parallel() + + type received struct { + body string + auth string + } + recvCh := make(chan received, 8) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + select { + case recvCh <- received{body: string(b), auth: req.Header.Get("Authorization")}: + default: + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "drain-token", + TestRunID: "run-drain", + PushPeriod: 20 * time.Millisecond, + }) + + for _, msg := range []string{"entry-a", "entry-b", "entry-c"} { + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: msg})) + } + + // ctx stays alive for the whole test: shutdown must be driven by Drain, + // not by a ctx cancel. + ctx := t.Context() + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + // Entries reach the backend via the periodic push while the run is open, + // carrying the bearer token and the required test_run_id label. + var got received + select { + case got = <-recvCh: + case <-time.After(2 * time.Second): + t.Fatal("no push received from the pusher") + } + assert.Equal(t, "Bearer drain-token", got.auth) + assert.Contains(t, got.body, `"test_run_id":"run-drain"`) + + // Drain returns once the flush has completed; it must not hang. + require.NoError(t, p.Drain(context.Background())) + + // Drain drove Listen to return, even though ctx was never cancelled. + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after Drain") + } + + // Idempotent: a second Drain returns nil immediately. + require.NoError(t, p.Drain(context.Background())) +} + +// TestPusher_DrainUnconfigured covers the --out cloud / --no-cloud-logs case: +// without SetConfig, Drain is a no-op that returns nil immediately and makes +// no HTTP request. +func TestPusher_DrainUnconfigured(t *testing.T) { + t.Parallel() + + p := New(testutils.NewLogger(t)) + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.InfoLevel, Message: "buffered"})) + + require.NoError(t, p.Drain(context.Background())) +} + +// TestPusher_DrainContextTimeout locks Drain's ctx contract: when the flush +// cannot complete (here Listen is never started, so doneCh never closes), a +// done ctx unblocks Drain with the ctx error rather than blocking forever. +func TestPusher_DrainContextTimeout(t *testing.T) { + t.Parallel() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: "http://127.0.0.1:0", + Token: "tok", + TestRunID: "run", + }) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + require.ErrorIs(t, p.Drain(ctx), context.Canceled) +} + +// TestPusher_FiltersBelowConfiguredLevel locks the backend-level directive: +// with Level "info" the pusher pushes info/warn/error entries but filters out +// the less-severe debug entry, even though the loki hook's Fire enqueues +// unconditionally. Entries reach the backend via the periodic push. +func TestPusher_FiltersBelowConfiguredLevel(t *testing.T) { + t.Parallel() + + var ( + mu sync.Mutex + body strings.Builder + ) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + mu.Lock() + body.Write(b) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-lvl", + Level: "info", + PushPeriod: 20 * time.Millisecond, + }) + + // Fired debug-first, so by the time the others are pushed the debug entry + // has already been through the forward path (and been filtered). + for _, e := range []*logrus.Entry{ + {Time: time.Now(), Level: logrus.DebugLevel, Message: "msg-debug"}, + {Time: time.Now(), Level: logrus.InfoLevel, Message: "msg-info"}, + {Time: time.Now(), Level: logrus.WarnLevel, Message: "msg-warn"}, + {Time: time.Now(), Level: logrus.ErrorLevel, Message: "msg-error"}, + } { + require.NoError(t, p.Fire(e)) + } + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + got := func() string { + mu.Lock() + defer mu.Unlock() + return body.String() + } + + // The info/warn/error entries (at or above the configured level) are pushed. + require.Eventually(t, func() bool { + b := got() + return strings.Contains(b, "msg-info") && + strings.Contains(b, "msg-warn") && + strings.Contains(b, "msg-error") + }, 2*time.Second, 10*time.Millisecond, "info/warn/error entries were not pushed") + + // The debug entry is below the configured level, so it is never pushed. + assert.NotContains(t, got(), "msg-debug") + + cancel() + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after ctx was cancelled") + } +} + +// TestPusher_EmptyLevelKeepsAll is the variant: an unset Level keeps all +// levels, so even a debug entry is pushed. +func TestPusher_EmptyLevelKeepsAll(t *testing.T) { + t.Parallel() + + var ( + mu sync.Mutex + body strings.Builder + ) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + mu.Lock() + body.Write(b) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-empty", + Level: "", // unset => keep all levels + PushPeriod: 20 * time.Millisecond, + }) + + require.NoError(t, p.Fire(&logrus.Entry{Time: time.Now(), Level: logrus.DebugLevel, Message: "msg-debug"})) + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + require.Eventually(t, func() bool { + mu.Lock() + defer mu.Unlock() + return strings.Contains(body.String(), "msg-debug") + }, 2*time.Second, 10*time.Millisecond, "debug entry was not pushed with empty level") + + cancel() + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after ctx was cancelled") + } +} + +// TestPusher_InvalidLevelDefaultsToInfo verifies that an unparseable Level +// (which can only come from the backend) neither disables the push nor keeps +// all levels: it falls back to info, so debug is dropped while info and above +// are still pushed. +func TestPusher_InvalidLevelDefaultsToInfo(t *testing.T) { + t.Parallel() + + var ( + mu sync.Mutex + body strings.Builder + ) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + b, _ := io.ReadAll(req.Body) + mu.Lock() + body.Write(b) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + p := New(testutils.NewLogger(t)) + p.SetConfig(Config{ + PushURL: srv.URL, + Token: "tok", + TestRunID: "run-badlvl", + Level: "bogus", // invalid => fall back to info + PushPeriod: 20 * time.Millisecond, + }) + + // Debug fired first, so if it were going to be pushed it would land no + // later than the info/error entries. + for _, e := range []*logrus.Entry{ + {Time: time.Now(), Level: logrus.DebugLevel, Message: "msg-debug"}, + {Time: time.Now(), Level: logrus.InfoLevel, Message: "msg-info"}, + {Time: time.Now(), Level: logrus.ErrorLevel, Message: "msg-error"}, + } { + require.NoError(t, p.Fire(e)) + } + + ctx, cancel := context.WithCancel(context.Background()) + listenDone := make(chan struct{}) + go func() { + p.Listen(ctx) + close(listenDone) + }() + + got := func() string { + mu.Lock() + defer mu.Unlock() + return body.String() + } + + // info/error (at or above the info fallback) are pushed despite the + // invalid level, proving the push is not disabled. + require.Eventually(t, func() bool { + b := got() + return strings.Contains(b, "msg-info") && strings.Contains(b, "msg-error") + }, 2*time.Second, 10*time.Millisecond, "info/error not pushed with an invalid level") + + // debug (below info) is dropped, proving the fallback is info, not all. + assert.NotContains(t, got(), "msg-debug") + + cancel() + select { + case <-listenDone: + case <-time.After(2 * time.Second): + t.Fatal("Listen did not return after ctx was cancelled") + } +} diff --git a/internal/log/file.go b/internal/log/file.go index 9129a4b2d..bfe2a3159 100644 --- a/internal/log/file.go +++ b/internal/log/file.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "syscall" + "time" "github.com/sirupsen/logrus" "go.k6.io/k6/v2/internal/lib/strvals" @@ -19,6 +20,8 @@ import ( // fileHookBufferSize is a default size for the fileHook's loglines channel. const fileHookBufferSize = 100 +const fileHookFlushInterval = time.Second + // fileHook is a hook to handle writing to local files. type fileHook struct { fs fsext.Fs @@ -109,11 +112,18 @@ func (h *fileHook) openFile(getCwd func() (string, error)) error { // Listen waits for log lines to flush. func (h *fileHook) Listen(ctx context.Context) { + ticker := time.NewTicker(fileHookFlushInterval) + defer ticker.Stop() + for { select { case entry := <-h.loglines: if _, err := h.bw.Write(entry); err != nil { - h.fallbackLogger.Errorf("failed to write a log message to a logfile: %w", err) + h.fallbackLogger.WithError(err).Error("failed to write a log message to a logfile") + } + case <-ticker.C: + if err := h.bw.Flush(); err != nil { + h.fallbackLogger.WithError(err).Error("failed to flush logfile buffer") } case <-ctx.Done(): // This context is cancelled after the command finishes executing, so it is guaranteed that no more lines @@ -124,7 +134,7 @@ func (h *fileHook) Listen(ctx context.Context) { select { case entry := <-h.loglines: if _, err := h.bw.Write(entry); err != nil { - h.fallbackLogger.Errorf("failed to write a log message to a logfile: %w", err) + h.fallbackLogger.WithError(err).Error("failed to write a log message to a logfile") } default: break drainloop @@ -132,11 +142,11 @@ func (h *fileHook) Listen(ctx context.Context) { } if err := h.bw.Flush(); err != nil { - h.fallbackLogger.Errorf("failed to flush buffer: %w", err) + h.fallbackLogger.WithError(err).Error("failed to flush buffer") } if err := h.w.Close(); err != nil { - h.fallbackLogger.Errorf("failed to close logfile: %w", err) + h.fallbackLogger.WithError(err).Error("failed to close logfile") } return diff --git a/internal/log/file_test.go b/internal/log/file_test.go index 492ffeea5..9c4d23e07 100644 --- a/internal/log/file_test.go +++ b/internal/log/file_test.go @@ -24,6 +24,18 @@ func (nc *nopCloser) Close() error { return nil } +type signalWriter struct { + wrote chan struct{} +} + +func (sw signalWriter) Write(p []byte) (int, error) { + select { + case sw.wrote <- struct{}{}: + default: + } + return len(p), nil +} + func TestFileHookFromConfigLine(t *testing.T) { t.Parallel() @@ -145,3 +157,38 @@ func TestFileHookFire(t *testing.T) { assert.Contains(t, buffer.String(), "example log line") } + +func TestFileHookPeriodicallyFlushes(t *testing.T) { + t.Parallel() + + wrote := make(chan struct{}, 1) + nc := &nopCloser{ + Writer: signalWriter{wrote: wrote}, + closed: make(chan struct{}), + } + + hook := &fileHook{ + loglines: make(chan []byte, fileHookBufferSize), + w: nc, + bw: bufio.NewWriter(nc), + levels: logrus.AllLevels, + } + + ctx, cancel := context.WithCancel(context.Background()) + defer func() { + cancel() + <-nc.closed + }() + + go hook.Listen(ctx) + hook.loglines <- []byte("example log line\n") + + require.Eventually(t, func() bool { + select { + case <-wrote: + return true + default: + return false + } + }, 2*fileHookFlushInterval, 10*time.Millisecond) +} diff --git a/internal/log/log.go b/internal/log/log.go index 3f0cfa4fc..1ef148767 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -16,3 +16,11 @@ type AsyncHook interface { // It stops when the context is canceled. Listen(ctx context.Context) } + +// NoticePusher is an optional capability of an AsyncHook: it pushes a single +// diagnostic line immediately, exempt from any per-batch limit the hook +// normally enforces. Callers type-assert an AsyncHook to it to surface events +// (such as an upstream buffer overflow) that must not be lost to that limit. +type NoticePusher interface { + PushNotice(message string) error +} diff --git a/internal/log/loki.go b/internal/log/loki.go index aa25f034a..3cd319f5a 100644 --- a/internal/log/loki.go +++ b/internal/log/loki.go @@ -47,24 +47,66 @@ func getDefaultLoki() *lokiHook { } } -// LokiFromConfigLine returns a new logrus.Hook -// that pushes logrus.Entrys to loki and is configured -// through the provided line. -func LokiFromConfigLine(fallbackLogger logrus.FieldLogger, line string) (AsyncHook, error) { +// LokiHookOptions configures a loki push hook built by NewLokiHook. +// Zero-valued fields take the built-in defaults. +type LokiHookOptions struct { + Addr string // default http://127.0.0.1:3100/loki/api/v1/push + PushPeriod time.Duration // default 1s + Limit int // default 100 + MsgMaxSize int // default 1MB + Level string // "" => all levels; else parseLevels + AllowedLabels []string + Labels [][2]string // static labels (e.g. test_run_id) + Headers [][2]string // e.g. Authorization: Bearer + Profile bool +} + +// NewLokiHook returns a new AsyncHook that pushes logrus.Entrys to loki, +// configured from opts. Zero-valued opts fields take the built-in defaults. +func NewLokiHook(fallbackLogger logrus.FieldLogger, opts LokiHookOptions) (AsyncHook, error) { h := getDefaultLoki() h.fallbackLogger = fallbackLogger - if line != "loki" { - logOutput, _, _ := strings.Cut(line, "=") - if logOutput != "loki" { - return nil, fmt.Errorf("loki configuration should be in the form `loki=url-to-push` but is `%s`", line) - } - - err := h.parseArgs(line) + if opts.Addr != "" { + h.addr = opts.Addr + } + // Limit, PushPeriod and MsgMaxSize fall back to their defaults on a + // non-positive value. Negatives can only arrive programmatically + // (K6_CLOUD_LOGS_* / a backend logs config bypass the config-line + // validation) and would otherwise panic in Listen — make([]tmpMsg, limit) + // and time.NewTicker(pushPeriod) — or on push (msgMaxSize slicing); warn so + // a bad value is visible. + if opts.PushPeriod > 0 { + h.pushPeriod = opts.PushPeriod + } else if opts.PushPeriod < 0 && fallbackLogger != nil { + fallbackLogger.WithField("pushPeriod", opts.PushPeriod). + Warn("negative loki push period; using default") + } + if opts.Limit > 0 { + h.limit = opts.Limit + } else if opts.Limit < 0 && fallbackLogger != nil { + fallbackLogger.WithField("limit", opts.Limit). + Warn("negative loki limit; using default") + } + if opts.MsgMaxSize > 0 { + h.msgMaxSize = opts.MsgMaxSize + } else if opts.MsgMaxSize < 0 && fallbackLogger != nil { + fallbackLogger.WithField("msgMaxSize", opts.MsgMaxSize). + Warn("negative loki message size; using default") + } + if opts.Level != "" { + levels, err := parseLevels(opts.Level) if err != nil { return nil, err } + h.levels = levels + } + if opts.AllowedLabels != nil { + h.allowedLabels = opts.AllowedLabels } + h.labels = append(h.labels, opts.Labels...) + h.headers = append(h.headers, opts.Headers...) + h.profile = opts.Profile h.droppedLabels = make(map[string]string, 2+len(h.labels)) h.droppedLabels["level"] = logrus.WarnLevel.String() @@ -74,10 +116,31 @@ func LokiFromConfigLine(fallbackLogger logrus.FieldLogger, line string) (AsyncHo h.droppedMsg = h.filterLabels(h.droppedLabels, h.droppedMsg) h.client = &http.Client{Timeout: h.pushPeriod} + return h, nil } -func (h *lokiHook) parseArgs(line string) error { +// LokiFromConfigLine returns a new logrus.Hook +// that pushes logrus.Entrys to loki and is configured +// through the provided line. +func LokiFromConfigLine(fallbackLogger logrus.FieldLogger, line string) (AsyncHook, error) { + var opts LokiHookOptions + + if line != "loki" { + logOutput, _, _ := strings.Cut(line, "=") + if logOutput != "loki" { + return nil, fmt.Errorf("loki configuration should be in the form `loki=url-to-push` but is `%s`", line) + } + + if err := opts.parseArgs(line); err != nil { + return nil, err + } + } + + return NewLokiHook(fallbackLogger, opts) +} + +func (opts *LokiHookOptions) parseArgs(line string) error { tokens, err := strvals.Parse(line) if err != nil { return fmt.Errorf("error while parsing loki configuration %w", err) @@ -90,46 +153,41 @@ func (h *lokiHook) parseArgs(line string) error { var err error switch key { case "loki": - h.addr = value + opts.Addr = value case "pushPeriod": - h.pushPeriod, err = time.ParseDuration(value) + opts.PushPeriod, err = time.ParseDuration(value) if err != nil { return fmt.Errorf("couldn't parse the loki pushPeriod %w", err) } case "profile": - h.profile = true + opts.Profile = true case "limit": - h.limit, err = strconv.Atoi(value) + opts.Limit, err = strconv.Atoi(value) if err != nil { return fmt.Errorf("couldn't parse the loki limit as a number %w", err) } - if h.limit < 1 { - return fmt.Errorf("loki limit needs to be a positive number, is %d", h.limit) + if opts.Limit < 1 { + return fmt.Errorf("loki limit needs to be a positive number, is %d", opts.Limit) } case "msgMaxSize": - h.msgMaxSize, err = strconv.Atoi(value) + opts.MsgMaxSize, err = strconv.Atoi(value) if err != nil { return fmt.Errorf("couldn't parse the loki msgMaxSize as a number %w", err) } - if h.msgMaxSize < 1 { - return fmt.Errorf("loki msgMaxSize needs to be a positive number, is %d", h.msgMaxSize) + if opts.MsgMaxSize < 1 { + return fmt.Errorf("loki msgMaxSize needs to be a positive number, is %d", opts.MsgMaxSize) } case "level": - h.levels, err = parseLevels(value) - if err != nil { - return err - } + opts.Level = value case "allowedLabels": - h.allowedLabels = strings.Split(value, ",") + opts.AllowedLabels = strings.Split(value, ",") default: if after, ok := strings.CutPrefix(key, "label."); ok { - labelKey := after - h.labels = append(h.labels, [2]string{labelKey, value}) + opts.Labels = append(opts.Labels, [2]string{after, value}) continue } else if after, ok := strings.CutPrefix(key, "header."); ok { - headerKey := after - h.headers = append(h.headers, [2]string{headerKey, value}) + opts.Headers = append(opts.Headers, [2]string{after, value}) continue } @@ -146,7 +204,7 @@ func (h *lokiHook) parseArgs(line string) error { // // TODO benchmark this // -//nolint:funlen +//nolint:funlen,gocognit func (h *lokiHook) Listen(ctx context.Context) { var ( msgs = make([]tmpMsg, h.limit) @@ -216,45 +274,75 @@ func (h *lokiHook) Listen(ctx context.Context) { } }() - for { - select { - case entry := <-h.ch: - if count == h.limit { - dropped++ + // appendEntry buffers one entry into msgs, or counts it as dropped once the + // per-push-period limit is reached. It mutates the count, dropped and msgs + // loop state, so it must run only on the main goroutine — never while the + // push goroutine is swapping msgs and resetting the counters. + appendEntry := func(entry *logrus.Entry) { + if count == h.limit { + dropped++ - continue - } + return + } - // Arguably we can directly generate the final marshalled version of the labels right here - // through sorting the entry.Data, removing additionalparams from it and then dumping it - // as the final marshal and appending level and h.labels after it. - // If we reuse some kind of big enough `[]byte` buffer we can also possibly skip on some - // of allocation. Combined with the cutoff part and directly pushing in the final data - // type this can be really a lot faster and to use a lot less memory - labels := make(map[string]string, len(entry.Data)+1) - for k, v := range entry.Data { - labels[k] = fmt.Sprint(v) // TODO optimize ? - } - for _, params := range h.labels { - labels[params[0]] = params[1] - } - labels["level"] = entry.Level.String() - msg := h.filterLabels(labels, entry.Message) // TODO we can do this while constructing - // have the cutoff here ? - // if we cutoff here we can cut somewhat on the backbuffers and optimize the inserting - // in/creating of the final Streams that we push - msgs[count] = tmpMsg{ - labels: labels, - msg: msg, - t: entry.Time.UnixNano(), + // Arguably we can directly generate the final marshalled version of the labels right here + // through sorting the entry.Data, removing additionalparams from it and then dumping it + // as the final marshal and appending level and h.labels after it. + // If we reuse some kind of big enough `[]byte` buffer we can also possibly skip on some + // of allocation. Combined with the cutoff part and directly pushing in the final data + // type this can be really a lot faster and to use a lot less memory + labels := make(map[string]string, len(entry.Data)+1) + for k, v := range entry.Data { + labels[k] = fmt.Sprint(v) // TODO optimize ? + } + for _, params := range h.labels { + labels[params[0]] = params[1] + } + labels["level"] = entry.Level.String() + msg := h.filterLabels(labels, entry.Message) // TODO we can do this while constructing + // have the cutoff here ? + // if we cutoff here we can cut somewhat on the backbuffers and optimize the inserting + // in/creating of the final Streams that we push + msgs[count] = tmpMsg{ + labels: labels, + msg: msg, + t: entry.Time.UnixNano(), + } + count++ + } + + // drainQueued flushes entries still buffered in h.ch into msgs without + // blocking. Called on shutdown so the final push includes everything Fire + // already accepted; best-effort, it does not wait for entries still being + // Fired concurrently with the shutdown. + drainQueued := func() { + for { + select { + case entry, ok := <-h.ch: + if !ok { + // h.ch is never closed today; this guards a future close + // from spinning on nil receives. + return + } + appendEntry(entry) + default: + return } - count++ + } + } + + for { + select { + case entry := <-h.ch: + appendEntry(entry) case t := <-ticker.C: ch := make(chan int64) pushCh <- ch ch <- t.Add(-(h.pushPeriod / 2)).UnixNano() <-ch case <-ctx.Done(): + drainQueued() + ch := make(chan int64) pushCh <- ch ch <- time.Now().Add(time.Second).UnixNano() @@ -330,6 +418,28 @@ func (h *lokiHook) createPushMessage(msgs []tmpMsg, cutOffIndex, dropped int) *l return pushMsg } +// PushNotice synchronously pushes a single warning line to loki, exempt from +// the per-batch limit the main push loop enforces. It surfaces out-of-band +// drops (for example an upstream buffer overflow) that must not be lost to that +// limit. The line carries the hook's configured labels, so it is not rejected +// for a missing test_run_id. +func (h *lokiHook) PushNotice(message string) error { + pushMsg := new(lokiPushMessage) + pushMsg.maxSize = h.msgMaxSize + pushMsg.add(tmpMsg{ + labels: h.droppedLabels, + msg: message, + t: time.Now().UnixNano(), + }) + + var b bytes.Buffer + if _, err := pushMsg.WriteTo(&b); err != nil { + return err + } + + return h.push(b) +} + func (h *lokiHook) push(b bytes.Buffer) error { body := b.Bytes() @@ -347,7 +457,7 @@ func (h *lokiHook) push(b bytes.Buffer) error { req.Header.Add(header[0], header[1]) } - res, err := h.client.Do(req) //nolint:gosec + res, err := h.client.Do(req) if res != nil { if res.StatusCode >= 400 { diff --git a/internal/log/loki_test.go b/internal/log/loki_test.go index 3762e5975..52383861d 100644 --- a/internal/log/loki_test.go +++ b/internal/log/loki_test.go @@ -14,6 +14,8 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/internal/lib/testutils" ) func TestSyslogFromConfigLine(t *testing.T) { @@ -93,6 +95,74 @@ func TestSyslogFromConfigLine(t *testing.T) { } } +func TestNewLokiHook(t *testing.T) { + t.Parallel() + + opts := LokiHookOptions{ + Addr: "somewhere:1233", + PushPeriod: time.Minute*5 + time.Second*32, + Limit: 32, + MsgMaxSize: 1231, + Level: "info", + AllowedLabels: []string{"something"}, + Labels: [][2]string{{"something", "else"}, {"foo", "bar"}}, + Headers: [][2]string{{"x-test", "123"}, {"authorization", "token foobar"}}, + Profile: true, + } + + res, err := NewLokiHook(nil, opts) + require.NoError(t, err) + hook, ok := res.(*lokiHook) + require.True(t, ok) + + assert.Equal(t, "somewhere:1233", hook.addr) + assert.Equal(t, time.Minute*5+time.Second*32, hook.pushPeriod) + assert.Equal(t, 32, hook.limit) + assert.Equal(t, 1231, hook.msgMaxSize) + assert.Equal(t, logrus.AllLevels[:5], hook.levels) + assert.Equal(t, []string{"something"}, hook.allowedLabels) + assert.Equal(t, [][2]string{{"something", "else"}, {"foo", "bar"}}, hook.labels) + assert.Equal(t, [][2]string{{"x-test", "123"}, {"authorization", "token foobar"}}, hook.headers) + assert.True(t, hook.profile) + + // The tail-end setup (droppedLabels/droppedMsg/client) must have run: + // non-allowed labels are folded into droppedMsg and dropped from droppedLabels. + assert.Equal(t, map[string]string{"something": "else"}, hook.droppedLabels) + assert.Equal(t, + "k6 dropped %d log messages because they were above the limit of %d messages / %s foo=bar level=warning", + hook.droppedMsg) + require.NotNil(t, hook.client) + assert.Equal(t, time.Minute*5+time.Second*32, hook.client.Timeout) +} + +func TestNewLokiHook_Defaults(t *testing.T) { + t.Parallel() + + res, err := NewLokiHook(nil, LokiHookOptions{}) + require.NoError(t, err) + hook, ok := res.(*lokiHook) + require.True(t, ok) + + assert.Equal(t, "http://127.0.0.1:3100/loki/api/v1/push", hook.addr) + assert.Equal(t, time.Second*1, hook.pushPeriod) + assert.Equal(t, 100, hook.limit) + assert.Equal(t, 1024*1024, hook.msgMaxSize) + assert.Equal(t, logrus.AllLevels, hook.levels) + assert.Nil(t, hook.allowedLabels) + assert.Nil(t, hook.labels) + assert.Nil(t, hook.headers) + assert.False(t, hook.profile) + + // The tail-end setup runs even with defaults: only the level label is seeded, + // and with no allowedLabels the droppedMsg is left untouched. + assert.Equal(t, map[string]string{"level": "warning"}, hook.droppedLabels) + assert.Equal(t, + "k6 dropped %d log messages because they were above the limit of %d messages / %s", + hook.droppedMsg) + require.NotNil(t, hook.client) + assert.Equal(t, time.Second*1, hook.client.Timeout) +} + func TestLogEntryMarshal(t *testing.T) { t.Parallel() entry := logEntry{ @@ -220,3 +290,143 @@ func TestLokiHeaders(t *testing.T) { t.Fatal("No logs were received from loki before hook has finished") } } + +// TestNewLokiHookClampsNonPositiveOptions verifies that non-positive Limit, +// PushPeriod and MsgMaxSize fall back to the built-in defaults (with a warning) +// instead of panicking. These values can only arrive programmatically — e.g. +// via K6_CLOUD_LOGS_* or a backend logs config — which bypass the config-line +// validation that otherwise rejects them. +func TestNewLokiHookClampsNonPositiveOptions(t *testing.T) { + t.Parallel() + + logger, hook := testutils.NewLoggerWithHook(t, logrus.WarnLevel) + + receivedData := make(chan string, 1) + srv := httptest.NewServer( + http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { + b, err := io.ReadAll(req.Body) + if err != nil { + t.Error(err) + return + } + select { + case receivedData <- string(b): + default: + } + }), + ) + defer srv.Close() + + // Without the clamp these panic in Listen — make([]tmpMsg, limit) and + // time.NewTicker(pushPeriod) — and on push (msgMaxSize slicing). + h, err := NewLokiHook(logger, LokiHookOptions{ + Addr: srv.URL, + Limit: -5, + PushPeriod: -1 * time.Second, + MsgMaxSize: -10, + }) + require.NoError(t, err) + + // Each bad value is warned about (and falls back to its default). + entries := hook.Drain() + assert.True(t, testutils.LogContains(entries, logrus.WarnLevel, "limit"), + "expected a warning about the negative limit") + assert.True(t, testutils.LogContains(entries, logrus.WarnLevel, "push period"), + "expected a warning about the negative push period") + assert.True(t, testutils.LogContains(entries, logrus.WarnLevel, "message size"), + "expected a warning about the negative message size") + + // Listen must not panic, and the entry is pushed using the defaults. + ctx, cancel := context.WithCancel(context.Background()) + wg := new(sync.WaitGroup) + now := time.Now() + wg.Go(func() { + require.NoError(t, h.Fire(&logrus.Entry{Time: now, Level: logrus.InfoLevel, Message: "clamp message"})) + time.Sleep(10 * time.Millisecond) + cancel() + }) + h.Listen(ctx) + wg.Wait() + + select { + case data := <-receivedData: + require.Contains(t, data, "clamp message") + default: + t.Fatal("no logs received; hook did not push with clamped defaults") + } +} + +// TestLokiFromConfigLineEmptyAddr documents that `--log-output=loki=` (an empty +// URL) is rejected up front by the config parser rather than silently falling +// back to the default endpoint — so there is no empty-address case reaching the +// constructor. Bare `loki` still uses the default. This is unchanged from +// before the NewLokiHook extraction, since parseArgs still runs strvals.Parse +// first. +func TestLokiFromConfigLineEmptyAddr(t *testing.T) { + t.Parallel() + + _, err := LokiFromConfigLine(nil, "loki=") + require.Error(t, err, "an empty loki address must be rejected, not defaulted") + assert.Contains(t, err.Error(), "no value") + + bare, err := LokiFromConfigLine(nil, "loki") + require.NoError(t, err) + assert.NotEmpty(t, bare.(*lokiHook).addr, "bare loki uses the default endpoint") +} + +// TestLokiHookDrainsQueuedEntriesOnShutdown pins that a ctx-cancel shutdown +// flushes every entry already queued in the hook's channel, not just the ones +// the run loop happened to consume before the cancel was selected. The entries +// are pre-loaded and the context is already cancelled, so Listen takes its +// shutdown branch with the whole batch still queued; without draining the +// channel it would lose a suffix of them. +func TestLokiHookDrainsQueuedEntriesOnShutdown(t *testing.T) { + t.Parallel() + + const n = 100 + + var ( + mu sync.Mutex + got string + ) + srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { + b, err := io.ReadAll(req.Body) + if err != nil { + t.Error(err) + return + } + mu.Lock() + got += string(b) + mu.Unlock() + })) + defer srv.Close() + + // Limit well above n so nothing is limit-dropped, and a long push period so + // the shutdown push is the only one. + h, err := NewLokiHook(testutils.NewLogger(t), LokiHookOptions{ + Addr: srv.URL, + Limit: 2 * n, + PushPeriod: time.Hour, + }) + require.NoError(t, err) + + // Queue every entry before Listen starts (white-box: this test is in + // package log), so the shutdown branch must drain the channel to send them. + for i := range n { + h.(*lokiHook).ch <- &logrus.Entry{ + Time: time.Now(), + Level: logrus.InfoLevel, + Message: fmt.Sprintf("msg-%d", i), + } + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + h.Listen(ctx) + + mu.Lock() + defer mu.Unlock() + for i := range n { + assert.Contains(t, got, fmt.Sprintf("msg-%d", i), "entry %d was lost on shutdown", i) + } +} diff --git a/internal/metrics/engine/engine.go b/internal/metrics/engine/engine.go index 7cc130e9f..f369af533 100644 --- a/internal/metrics/engine/engine.go +++ b/internal/metrics/engine/engine.go @@ -29,7 +29,7 @@ type MetricsEngine struct { // These can be both top-level metrics or sub-metrics metricsWithThresholds []*metrics.Metric - breachedThresholdsCount uint32 + breachedThresholdsCount atomic.Uint32 // TODO: completely refactor: // - make these private, add a method to export the raw data @@ -249,7 +249,7 @@ func (me *MetricsEngine) evaluateThresholds( sort.Strings(breachedThresholds) me.logger.Debugf("Thresholds on %d metrics crossed: %v", len(breachedThresholds), breachedThresholds) } - atomic.StoreUint32(&me.breachedThresholdsCount, uint32(len(breachedThresholds))) //nolint:gosec + me.breachedThresholdsCount.Store(uint32(len(breachedThresholds))) //nolint:gosec return breachedThresholds, shouldAbort } @@ -257,5 +257,5 @@ func (me *MetricsEngine) evaluateThresholds( // the thresholds were breached (failed) during the last processing phase. This // API is safe to use concurrently. func (me *MetricsEngine) GetMetricsWithBreachedThresholdsCount() uint32 { - return atomic.LoadUint32(&me.breachedThresholdsCount) + return me.breachedThresholdsCount.Load() } diff --git a/internal/output/breakingit/README.md b/internal/output/breakingit/README.md new file mode 100644 index 000000000..4e7ef0e44 --- /dev/null +++ b/internal/output/breakingit/README.md @@ -0,0 +1,115 @@ +# BreakTest transactions + +The `breakingit` exporter accepts both native k6 `group_duration` samples and the +legacy custom `pages` Trend. They use the existing `transactions` table (or +`transactions_sm` for synthetic monitoring); no database migration is required. + +## Native groups (preferred for new scripts) + +This fork enables k6 2.3's experimental async metric context by default, in both +normal and lean builds. No feature flag is needed for browser groups: + +```sh +k6 run --out breakingit=https://your-pg-proxy script.js +``` + +Keep the existing `PG_PROXY_TOKEN`, run/location, and synthetic-monitoring +configuration; set `BASE_URL` for the example below. + +To restore upstream's opt-in behavior for a run, use `k6 run --features='' script.js` +or `K6_FEATURES='' k6 run script.js`. An explicit feature list **replaces** the +default, following the existing CLI > environment > JSON configuration precedence. +If selecting other features, include `async-metric-context` in that list to keep it +enabled. An explicit JSON `"features": []` also disables the default unless a +higher-priority source overrides it. This remains an experimental feature, not GA. + +Compared with the disabled behavior, group-local changes to VU metric tags and +metadata are restored on exit rather than leaking into subsequent steps. + +```javascript +import { browser } from 'k6/browser'; +import { check, group } from 'k6'; +import { setTimeout } from 'k6/timers'; + +export const options = { + scenarios: { + checkout: { + executor: 'shared-iterations', + vus: 1, + iterations: 1, + options: { browser: { type: 'chromium' } }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + try { + // Keep think time outside the group so it is not part of response_time. + await new Promise(resolve => setTimeout(resolve, 1000)); + await group('01_OpenHomepage', async () => { + const response = await page.goto(__ENV.BASE_URL); + if (!check(response, { 'homepage is 200': r => r && r.status() === 200 })) { + throw new Error('Homepage did not return 200'); + } + await page.locator('.hero-product img').waitFor({ state: 'visible' }); + }); + } finally { + await page.close(); + } +} +``` + +Each completed group invocation becomes one transaction: + +- `transaction_name`: the group path without its leading `::`, for example + `Checkout::Payment`. Nested groups produce separate parent and child rows; + the parent duration includes its children, so these durations are not additive. +- `response_time`: the whole callback/returned-promise lifetime in integer + milliseconds, including waits inside the group, on success **and** failure. +- `success`: `false` if the callback throws or its returned promise rejects; + otherwise `true`. The fork carries this outcome on the duration sample, without + adding tags or matching unrelated samples by name/timestamp. +- Browser requests and browser errors retain the group context and are associated + with the same transaction name. A group is not limited to HTTP requests: waits, + interactions, and other awaited work count toward its duration. + +A failed `check()` still writes an error record, but does not by itself reject the +group. Throw on a failed check when it should fail the transaction, as above. +Likewise, HTTP error status codes do not automatically reject `page.goto()`. +Errors caught inside a group do not fail that group. Catching a rejection outside +the group does not change its failed outcome. Always await/return the work being +measured. Work aborted before group completion may have no duration sample. + +Keep the `group` system tag enabled (the default) so names and associations are +available. Without it, transaction names fall back to `name` or `/`. + +## Legacy support and migration + +Existing `new Trend('pages', true)` scripts keep working unchanged: the `page` tag +is preferred as the transaction name and emitted samples remain successful rows. +If a legacy helper emits `pages` only on success, its failures still have no +transaction-duration row. This legacy behavior is deliberately preserved. + +`BREAKINGIT_TRANSACTION_SOURCE` selects which duration metrics become transactions: + +- `both` (default): export `pages` and `group_duration`. Use this for a mix of + legacy and native steps that each emit only one of the two duration metrics. +- `groups`: export only `group_duration`. Use this while wrapping old helpers + in native groups, to avoid recording the same step twice. +- `pages`: export only legacy `pages` durations. + +This selector affects only transaction rows, not requests, checks, browser errors, +or k6's own metric aggregation. Invalid values stop exporter initialization. +There is no automatic deduplication: names/timestamps cannot reliably distinguish +an intentional nested transaction from two measurements of the same step. + +For a full migration, remove `pages.add()` and use `await group(...)` for timing +and context, while keeping any business checks and throwing on failures. + +### Proxy TLS configuration + +`PG_PROXY_INSECURE_SKIP_TLS_VERIFY=false` enables certificate verification for the +PG-proxy connection. The default remains `true` for compatibility with existing +private proxy deployments. This setting applies only to output delivery, not to +HTTP requests generated by the test script. diff --git a/internal/output/breakingit/batch_test.go b/internal/output/breakingit/batch_test.go new file mode 100644 index 000000000..198f9b042 --- /dev/null +++ b/internal/output/breakingit/batch_test.go @@ -0,0 +1,70 @@ +package breakingit + +import ( + "compress/gzip" + "io" + "mime" + "mime/multipart" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBatchRetryPreservesConcurrentSamples(t *testing.T) { + t.Parallel() + logger := newTransactionTestLogger(false) + logger.bufTrans.WriteString("original\n") + requests := make(chan string, 3) + attempts := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gz, err := gzip.NewReader(r.Body) + if !assert.NoError(t, err) { + w.WriteHeader(http.StatusBadRequest) + return + } + defer func() { assert.NoError(t, gz.Close()) }() + _, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + if !assert.NoError(t, err) { + w.WriteHeader(http.StatusBadRequest) + return + } + part, err := multipart.NewReader(gz, params["boundary"]).NextPart() + if !assert.NoError(t, err) { + w.WriteHeader(http.StatusBadRequest) + return + } + assert.Equal(t, "public.transactions", part.Header.Get("X-Table")) + data, err := io.ReadAll(part) + assert.NoError(t, err) + requests <- string(data) + attempts++ + if attempts == 1 { + logger.mu.Lock() + logger.bufTrans.WriteString("arrived during retry\n") + logger.mu.Unlock() + w.WriteHeader(http.StatusServiceUnavailable) + return + } + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + logger.proxyURL, logger.httpClient = server.URL, server.Client() + logger.flush() + for _, want := range []string{"original\n", "original\n", "arrived during retry\n"} { + select { + case got := <-requests: + assert.Equal(t, want, got) + case <-time.After(5 * time.Second): + t.Fatal("missing batch") + } + } + require.Eventually(t, func() bool { + logger.mu.Lock() + defer logger.mu.Unlock() + return !logger.flushing && logger.bufTrans.Len() == 0 + }, 5*time.Second, 10*time.Millisecond) +} diff --git a/internal/output/breakingit/breakingit.go b/internal/output/breakingit/breakingit.go index 56e83ed74..013243621 100644 --- a/internal/output/breakingit/breakingit.go +++ b/internal/output/breakingit/breakingit.go @@ -1,22 +1,25 @@ +// Package breakingit exports k6 metrics to the Breaking-IT pg-proxy. package breakingit import ( "bytes" "compress/gzip" + "context" "crypto/rand" "crypto/sha256" "crypto/tls" - "encoding/hex" "encoding/csv" + "encoding/hex" "encoding/json" "fmt" "io" + "maps" "mime/multipart" "net/http" "net/textproto" "net/url" - "os" "regexp" + "strconv" "strings" "sync" "time" @@ -40,16 +43,15 @@ type MetricGroup struct { ErrorResBody string } -// excludedTags to reduce cardinality -var excludedTags = map[string]bool{ - "proto": true, - "tls_version": true, - "url": true, - "scenario": true, - "expected_response": true, - "from_cache": true, - "from_prefetch_cache": true, - "from_service_worker": true, +// excludedTag identifies tags omitted from database rows to reduce cardinality. +func excludedTag(tag string) bool { + switch tag { + case "proto", "tls_version", "url", "scenario", "expected_response", + "from_cache", "from_prefetch_cache", "from_service_worker": + return true + default: + return false + } } // Retry configuration constants @@ -68,26 +70,28 @@ type Logger struct { httpClient *http.Client // batch buffers - mu sync.Mutex - bufHTTP *bytes.Buffer - bufVUs *bytes.Buffer + mu sync.Mutex + bufHTTP *bytes.Buffer + bufVUs *bytes.Buffer bufTrans *bytes.Buffer bufErrors *bytes.Buffer batchTimer *time.Timer flushing bool // flag to prevent recursive flush calls // pending screenshots to upsert via /upsert endpoint (low-volume) - pendingScreenshots [][]interface{} + pendingScreenshots [][]any // merging state - pendingHttpGroups map[int64]*MetricGroup // http and browser (unified) + pendingHTTPGroups map[int64]*MetricGroup // http and browser (unified) cleanupStop chan struct{} batchDone chan struct{} // closed when batchLoop exits envTags map[string]string customPatterns map[string]string isSynthetic bool + transactionSource transactionSource } +// New constructs a pg-proxy output from the configured environment. func New(params output.Params) (output.Output, error) { // Expect params.ConfigArgument to be pg-proxy base url, e.g. https://proxy/ingest base := params.ConfigArgument @@ -96,38 +100,44 @@ func New(params output.Params) (output.Output, error) { } u, err := url.Parse(base) if err != nil { - return nil, fmt.Errorf("invalid pg-proxy URL: %v", err) + return nil, fmt.Errorf("invalid pg-proxy URL: %w", err) } // Allow env overrides - db := os.Getenv("PG_PROXY_DB") + db := params.Environment["PG_PROXY_DB"] if db == "" { db = "breakingit" } - token := os.Getenv("PG_PROXY_TOKEN") + token := params.Environment["PG_PROXY_TOKEN"] if token == "" { return nil, fmt.Errorf("PG_PROXY_TOKEN is required for Authorization") } + transactionSource, err := parseTransactionSource(params.Environment["BREAKINGIT_TRANSACTION_SOURCE"]) + if err != nil { + return nil, err + } - // HTTP client with keep-alive and TLS skip per proxy config - tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} + // Preserve the private-proxy TLS default; callers can require certificate verification. + insecure := true + if value := params.Environment["PG_PROXY_INSECURE_SKIP_TLS_VERIFY"]; value != "" { + insecure, err = strconv.ParseBool(value) + if err != nil { + return nil, fmt.Errorf("invalid PG_PROXY_INSECURE_SKIP_TLS_VERIFY: %w", err) + } + } + tr := &http.Transport{TLSClientConfig: &tls.Config{ + InsecureSkipVerify: insecure, //nolint:gosec // Explicit private-proxy configuration, independent of test traffic. + }} client := &http.Client{Transport: tr, Timeout: 30 * time.Second} // Environment tags from generator envTags := make(map[string]string) - isSynthetic := strings.ToLower(os.Getenv("syntheticMonitoring")) == "true" - if isSynthetic { - for _, k := range []string{"nodeName", "location", "scenarioName", "runId"} { - if v := os.Getenv(k); v != "" { - envTags[k] = v - } - } - } else { - for _, k := range []string{"location", "runId", "nodeName", "scenarioName"} { - if v := os.Getenv(k); v != "" { - envTags[k] = v - } + isSynthetic := strings.ToLower(params.Environment["syntheticMonitoring"]) == "true" + for _, key := range []string{"nodeName", "location", "scenarioName", "runId"} { + if value := params.Environment[key]; value != "" { + envTags[key] = value } } + // normalize snake_case keys expected by DB if v, ok := envTags["runId"]; ok { envTags["run_id"] = v @@ -139,24 +149,7 @@ func New(params output.Params) (output.Output, error) { envTags["scenario_name"] = v } - // Parse CUSTOM_URL_PATTERNS env (pattern==replacement comma-separated) - customPatterns := make(map[string]string) - if patterns := os.Getenv("CUSTOM_URL_PATTERNS"); patterns != "" { - fmt.Fprintf(params.StdOut, "Found CUSTOM_URL_PATTERNS environment variable: %s\n", patterns) - pairs := strings.Split(patterns, ",") - for _, pair := range pairs { - parts := strings.Split(pair, "==") - if len(parts) == 2 { - customPatterns[parts[0]] = parts[1] - fmt.Fprintf(params.StdOut, "Added custom pattern: %s -> %s\n", parts[0], parts[1]) - } else { - fmt.Fprintf(params.StdOut, "Warning: Invalid pattern format: %s\n", pair) - } - } - fmt.Fprintf(params.StdOut, "Total custom patterns loaded: %d\n", len(customPatterns)) - } else { - fmt.Fprintf(params.StdOut, "No CUSTOM_URL_PATTERNS environment variable found\n") - } + customPatterns := parseCustomPatterns(params.StdOut, params.Environment["CUSTOM_URL_PATTERNS"]) l := &Logger{ out: params.StdOut, @@ -168,12 +161,13 @@ func New(params output.Params) (output.Output, error) { bufVUs: &bytes.Buffer{}, bufTrans: &bytes.Buffer{}, bufErrors: &bytes.Buffer{}, - pendingHttpGroups: make(map[int64]*MetricGroup), + pendingHTTPGroups: make(map[int64]*MetricGroup), cleanupStop: make(chan struct{}), batchDone: make(chan struct{}), envTags: envTags, customPatterns: customPatterns, isSynthetic: isSynthetic, + transactionSource: transactionSource, } l.batchTimer = time.NewTimer(time.Second) go l.batchLoop() @@ -181,19 +175,24 @@ func New(params output.Params) (output.Output, error) { return l, nil } +// Description returns the output name. func (l *Logger) Description() string { return "breakingit" } -func (l *Logger) Start() error { return nil } +// Start satisfies the output lifecycle interface. +func (l *Logger) Start() error { return nil } + +// Stop flushes remaining samples and stops background processing. func (l *Logger) Stop() error { - fmt.Fprintf(l.out, "[screenshot] Stop() called, waiting for batchLoop to finish\n") + _, _ = fmt.Fprintf(l.out, "[screenshot] Stop() called, waiting for batchLoop to finish\n") l.batchTimer.Stop() close(l.cleanupStop) // Wait for any in-progress batchLoop flush to complete <-l.batchDone - fmt.Fprintf(l.out, "[screenshot] batchLoop done, running final flush (pendingScreenshots=%d)\n", len(l.pendingScreenshots)) + _, _ = fmt.Fprintf(l.out, "[screenshot] batchLoop done, running final flush (pendingScreenshots=%d)\n", + len(l.pendingScreenshots)) // Final flush to send any remaining data (including screenshots) l.flush() - fmt.Fprintf(l.out, "[screenshot] Stop() complete\n") + _, _ = fmt.Fprintf(l.out, "[screenshot] Stop() complete\n") return nil } @@ -226,15 +225,15 @@ func (l *Logger) cleanupProcessor() { now := time.Now() toProcessHTTP := make([]*MetricGroup, 0) l.mu.Lock() - for ts, g := range l.pendingHttpGroups { + for ts, g := range l.pendingHTTPGroups { if now.Sub(g.LastUpdate) > time.Second { toProcessHTTP = append(toProcessHTTP, g) - delete(l.pendingHttpGroups, ts) + delete(l.pendingHTTPGroups, ts) } } l.mu.Unlock() for _, g := range toProcessHTTP { - l.processHttpGroup(g) + l.processHTTPGroup(g) } case <-l.cleanupStop: return @@ -277,7 +276,8 @@ func (l *Logger) flush() { // Send screenshots via upsert endpoint (separate from batch COPY path) if len(screenshotsSnapshot) > 0 { - fmt.Fprintf(l.out, "[screenshot] flush: sending %d screenshot(s) to upsert endpoint\n", len(screenshotsSnapshot)) + _, _ = fmt.Fprintf(l.out, "[screenshot] flush: sending %d screenshot(s) to upsert endpoint\n", + len(screenshotsSnapshot)) l.upsertScreenshots(screenshotsSnapshot) } @@ -289,178 +289,30 @@ func (l *Logger) flush() { return } - // Build multipart body (reusable for retries) - var body bytes.Buffer - mw := multipart.NewWriter(&body) - - if len(bufHTTPSnapshot) > 0 { - h := make(textproto.MIMEHeader) - table := "public.requests_raw" - if l.isSynthetic { - table = "public.requests_raw_sm" - } - h.Set("X-Table", table) - h.Set("X-Format", "csv") - pw, _ := mw.CreatePart(h) - _, _ = io.Copy(pw, bytes.NewReader(bufHTTPSnapshot)) - } - if len(bufVUsSnapshot) > 0 && !l.isSynthetic { - h := make(textproto.MIMEHeader) - h.Set("X-Table", "public.virtual_users") - h.Set("X-Format", "csv") - pw, _ := mw.CreatePart(h) - _, _ = io.Copy(pw, bytes.NewReader(bufVUsSnapshot)) - } - if len(bufTransSnapshot) > 0 { - h := make(textproto.MIMEHeader) - table := "public.transactions" - if l.isSynthetic { - table = "public.transactions_sm" - } - h.Set("X-Table", table) - h.Set("X-Format", "csv") - pw, _ := mw.CreatePart(h) - _, _ = io.Copy(pw, bytes.NewReader(bufTransSnapshot)) - } - if len(bufErrorsSnapshot) > 0 { - h := make(textproto.MIMEHeader) - table := "public.requests_error" - if l.isSynthetic { - table = "public.requests_error_sm" - } - h.Set("X-Table", table) - h.Set("X-Format", "csv") - pw, _ := mw.CreatePart(h) - _, _ = io.Copy(pw, bytes.NewReader(bufErrorsSnapshot)) - } - _ = mw.Close() - - // gzip body - var gzBody bytes.Buffer - gz := gzip.NewWriter(&gzBody) - _, _ = gz.Write(body.Bytes()) - _ = gz.Close() - - // Retry logic with exponential backoff - startTime := time.Now() - backoffDelay := time.Duration(initialBackoffMs) * time.Millisecond - attempts := 0 - success := false - boundary := mw.Boundary() - gzBodyBytes := gzBody.Bytes() - - for !success && time.Since(startTime) < maxRetryTimeMs*time.Millisecond { - attempts++ - req, _ := http.NewRequest(http.MethodPost, l.proxyURL+"/ingest/batch", bytes.NewReader(gzBodyBytes)) - req.Header.Set("Authorization", "Bearer "+l.token) - req.Header.Set("Content-Type", "multipart/mixed; boundary="+boundary) - req.Header.Set("Content-Encoding", "gzip") - - resp, err := l.httpClient.Do(req) - if err != nil { - elapsed := time.Since(startTime) - if elapsed+backoffDelay < maxRetryTimeMs*time.Millisecond { - if attempts > 1 { - fmt.Fprintf(l.out, "pg-proxy ingest request error on attempt %d, retrying in %v: %v\n", attempts, backoffDelay, err) - } - time.Sleep(backoffDelay) - nextBackoffMs := backoffDelay.Milliseconds() * 2 - if nextBackoffMs > maxBackoffMs { - nextBackoffMs = maxBackoffMs - } - backoffDelay = time.Duration(nextBackoffMs) * time.Millisecond - } else { - fmt.Fprintf(l.out, "pg-proxy ingest request error after %d attempts (%v elapsed): %v\n", attempts, elapsed, err) - break - } - } else if resp != nil { - if resp.StatusCode == http.StatusOK { - success = true - _ = resp.Body.Close() - if attempts > 1 { - fmt.Fprintf(l.out, "pg-proxy ingest succeeded on attempt %d after %v\n", attempts, time.Since(startTime)) - } - } else { - b, _ := io.ReadAll(resp.Body) - _ = resp.Body.Close() - elapsed := time.Since(startTime) - if elapsed+backoffDelay < maxRetryTimeMs*time.Millisecond { - if attempts > 1 { - fmt.Fprintf(l.out, "pg-proxy ingest error status=%d body=%s on attempt %d, retrying in %v\n", resp.StatusCode, string(b), attempts, backoffDelay) - } - time.Sleep(backoffDelay) - nextBackoffMs := backoffDelay.Milliseconds() * 2 - if nextBackoffMs > maxBackoffMs { - nextBackoffMs = maxBackoffMs - } - backoffDelay = time.Duration(nextBackoffMs) * time.Millisecond - } else { - fmt.Fprintf(l.out, "pg-proxy ingest error after %d attempts (%v elapsed): status=%d body=%s\n", attempts, elapsed, resp.StatusCode, string(b)) - break - } - } - } - } - - if !success { - fmt.Fprintf(l.out, "pg-proxy ingest final failure after %d attempts (%v elapsed)\n", attempts, time.Since(startTime)) - } + body, boundary := l.batchBody(bufHTTPSnapshot, bufVUsSnapshot, bufTransSnapshot, bufErrorsSnapshot) + success := l.sendBatch(body, boundary) // Reset buffers and check for new data that accumulated during retries l.mu.Lock() hasNewData := false - var newHTTPData, newVUsData, newTransData, newErrorsData []byte - - if success { - // Check if any buffers grew during retries (new data accumulated) - // If so, preserve the new data before clearing - if l.bufHTTP.Len() > bufHTTPLen { - hasNewData = true - newHTTPData = make([]byte, l.bufHTTP.Len()-bufHTTPLen) - copy(newHTTPData, l.bufHTTP.Bytes()[bufHTTPLen:]) - } - if l.bufVUs.Len() > bufVUsLen { - hasNewData = true - newVUsData = make([]byte, l.bufVUs.Len()-bufVUsLen) - copy(newVUsData, l.bufVUs.Bytes()[bufVUsLen:]) - } - if l.bufTrans.Len() > bufTransLen { - hasNewData = true - newTransData = make([]byte, l.bufTrans.Len()-bufTransLen) - copy(newTransData, l.bufTrans.Bytes()[bufTransLen:]) - } - if l.bufErrors.Len() > bufErrorsLen { - hasNewData = true - newErrorsData = make([]byte, l.bufErrors.Len()-bufErrorsLen) - copy(newErrorsData, l.bufErrors.Bytes()[bufErrorsLen:]) - } - - // Clear buffers (we successfully sent the snapshot) - l.bufHTTP.Reset() - l.bufVUs.Reset() - l.bufTrans.Reset() - l.bufErrors.Reset() - - // Restore new data that accumulated during retries - if len(newHTTPData) > 0 { - l.bufHTTP.Write(newHTTPData) - } - if len(newVUsData) > 0 { - l.bufVUs.Write(newVUsData) - } - if len(newTransData) > 0 { - l.bufTrans.Write(newTransData) - } - if len(newErrorsData) > 0 { - l.bufErrors.Write(newErrorsData) + for _, item := range []struct { + buffer *bytes.Buffer + sent int + }{ + {l.bufHTTP, bufHTTPLen}, + {l.bufVUs, bufVUsLen}, + {l.bufTrans, bufTransLen}, + {l.bufErrors, bufErrorsLen}, + } { + if success { + item.buffer.Next(item.sent) + hasNewData = hasNewData || item.buffer.Len() > 0 + } else { + // Preserve the existing bounded-memory policy after exhausted retries. + item.buffer.Reset() } - } else { - // Retry loop exited without success (either timeout or early break), clear buffers to prevent unbounded growth - l.bufHTTP.Reset() - l.bufVUs.Reset() - l.bufTrans.Reset() - l.bufErrors.Reset() } + l.flushing = false l.mu.Unlock() @@ -473,32 +325,39 @@ func (l *Logger) flush() { } // upsertScreenshots sends screenshots to the pg-proxy upsert endpoint. -// Uses INSERT ... ON CONFLICT DO NOTHING on the proxy side, so duplicate hashes are silently skipped. -func (l *Logger) upsertScreenshots(rows [][]interface{}) { - payload := map[string]interface{}{ +// Uses INSERT ... ON CONFLICT DO NOTHING on the proxy side, so duplicate hashes are silently +// skipped. +func (l *Logger) upsertScreenshots(rows [][]any) { + payload := map[string]any{ "columns": []string{"hash", "data", "mime_type", "size_bytes"}, "rows": rows, } jsonBody, err := json.Marshal(payload) if err != nil { - fmt.Fprintf(l.out, "screenshot upsert marshal error: %v\n", err) + _, _ = fmt.Fprintf(l.out, "screenshot upsert marshal error: %v\n", err) + return + } + _, _ = fmt.Fprintf(l.out, "[screenshot] upsert: POSTing %d bytes to %s/upsert/public.sm_screenshots\n", + len(jsonBody), l.proxyURL) + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, + l.proxyURL+"/upsert/public.sm_screenshots", bytes.NewReader(jsonBody)) + if err != nil { + _, _ = fmt.Fprintf(l.out, "screenshot upsert request error: %v\n", err) return } - fmt.Fprintf(l.out, "[screenshot] upsert: POSTing %d bytes to %s/upsert/public.sm_screenshots\n", len(jsonBody), l.proxyURL) - req, _ := http.NewRequest(http.MethodPost, l.proxyURL+"/upsert/public.sm_screenshots", bytes.NewReader(jsonBody)) req.Header.Set("Authorization", "Bearer "+l.token) req.Header.Set("Content-Type", "application/json") resp, err := l.httpClient.Do(req) if err != nil { - fmt.Fprintf(l.out, "screenshot upsert request error: %v\n", err) + _, _ = fmt.Fprintf(l.out, "screenshot upsert request error: %v\n", err) return } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { b, _ := io.ReadAll(resp.Body) - fmt.Fprintf(l.out, "screenshot upsert error status=%d body=%s\n", resp.StatusCode, string(b)) + _, _ = fmt.Fprintf(l.out, "screenshot upsert error status=%d body=%s\n", resp.StatusCode, string(b)) } else { - fmt.Fprintf(l.out, "screenshot upsert ok rows=%d json_bytes=%d\n", len(rows), len(jsonBody)) + _, _ = fmt.Fprintf(l.out, "screenshot upsert ok rows=%d json_bytes=%d\n", len(rows), len(jsonBody)) } } @@ -530,265 +389,17 @@ func httpMetricCanonicalKey(metricName string) string { } } +// AddMetricSamples routes metric samples to the corresponding database rows. func (l *Logger) AddMetricSamples(samples []metrics.SampleContainer) { for _, sc := range samples { for _, s := range sc.GetSamples() { - ts := s.Time.UnixNano() - canonicalKey := httpMetricCanonicalKey(s.Metric.Name) - if canonicalKey != "" { - l.mu.Lock() - g, ok := l.pendingHttpGroups[ts] - if !ok { - g = &MetricGroup{Timestamp: ts, Tags: map[string]string{}, Values: map[string]float64{}, LastUpdate: time.Now()} - l.pendingHttpGroups[ts] = g - copyTagsForHTTP(g.Tags, s.Tags.Map(), l.envTags, l.customPatterns) - } - g.Values[canonicalKey] = s.Value - if canonicalKey == "http_req_failed" { - g.ErrorReqHeaders = s.HTTPErrorReqHeaders - g.ErrorReqBody = s.HTTPErrorReqBody - g.ErrorResHeaders = s.HTTPErrorResHeaders - g.ErrorResBody = s.HTTPErrorResBody - } - g.LastUpdate = time.Now() - // Process when we have the 8 required metrics (including http_req_failed); data_received is optional (browser only) - if _, hasFailed := g.Values["http_req_failed"]; hasFailed && len(g.Values) >= 8 { - delete(l.pendingHttpGroups, ts) - l.mu.Unlock() - l.processHttpGroup(g) - continue - } - l.mu.Unlock() - continue - } - // pages -> transactions - if s.Metric.Name == "pages" { - m := s.Tags.Map() - // Prefer explicit 'page' tag when available - pageRaw := fmt.Sprintf("%v", m["page"]) - var transactionName string - if pageRaw != "" && pageRaw != "" { - transactionName = pageRaw - } else { - // Derive a stable transaction_name: prefer name, fallback to group, then '/' - nameRaw := fmt.Sprintf("%v", m["name"]) - if nameRaw == "" || nameRaw == "" { - nameRaw = fmt.Sprintf("%v", m["group"]) // sometimes present - } - if nameRaw == "" || nameRaw == "" { - nameRaw = "/" - } - // Process name: if URL, use path; if not URL, use as-is (capped at 64) - _, _, path, _ := processNameTag(nameRaw, l.customPatterns) - transactionName = path - } - // Non-SM transactions table columns: - // time, run_id, location, transaction_name, success, request_size, response_size, response_time - // SM transactions_sm (run_id last): time, scenario_name, location, node_name, thread_group_name, transaction_name, success, request_size, response_size, response_time, run_id - isSM := l.isSynthetic - var row []string - if isSM { - row = []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), - fmt.Sprintf("%v", l.envTags["scenario_name"]), - fmt.Sprintf("%v", l.envTags["location"]), - fmt.Sprintf("%v", l.envTags["node_name"]), - fmt.Sprintf("%v", m["scenario"]), // thread_group_name from k6 scenario tag - transactionName, - "true", - "", - "", - fmtInt(s.Value), - fmt.Sprintf("%v", l.envTags["run_id"]), - } - } else { - row = []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), - fmt.Sprintf("%v", l.envTags["run_id"]), - fmt.Sprintf("%v", l.envTags["location"]), - transactionName, - "true", - "", - "", - fmtInt(s.Value), - } - } - l.mu.Lock() - wt := csv.NewWriter(l.bufTrans) - _ = wt.Write(row) - wt.Flush() - l.mu.Unlock() - continue - } - // k6 http group transactions -> transactions - if s.Metric.Name == "group_duration" { - m := s.Tags.Map() - // Derive transaction_name from 'group' tag, fallback to 'name' - nameRaw := fmt.Sprintf("%v", m["group"]) - if nameRaw == "" || nameRaw == "" { - nameRaw = fmt.Sprintf("%v", m["name"]) - } - nameRaw = strings.TrimPrefix(nameRaw, "::") - if nameRaw == "" || nameRaw == "" { - nameRaw = "/" - } - isSM := l.isSynthetic - var row []string - if isSM { - // SM transactions_sm (run_id last) - row = []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), - fmt.Sprintf("%v", l.envTags["scenario_name"]), - fmt.Sprintf("%v", l.envTags["location"]), - fmt.Sprintf("%v", l.envTags["node_name"]), - fmt.Sprintf("%v", m["scenario"]), // thread_group_name from k6 scenario - nameRaw, - "true", - "", - "", - fmtInt(s.Value), - fmt.Sprintf("%v", l.envTags["run_id"]), - } - } else { - row = []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), - fmt.Sprintf("%v", l.envTags["run_id"]), - fmt.Sprintf("%v", l.envTags["location"]), - nameRaw, - "true", - "", - "", - fmtInt(s.Value), - } - } - l.mu.Lock() - wt := csv.NewWriter(l.bufTrans) - _ = wt.Write(row) - wt.Flush() - l.mu.Unlock() - continue - } - // vus -> virtual_users (active_threads) - if s.Metric.Name == "vus" && !l.isSynthetic { - row := []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), - fmt.Sprintf("%v", l.envTags["run_id"]), - fmt.Sprintf("%v", l.envTags["location"]), - fmt.Sprintf("%v", l.envTags["node_name"]), - fmt.Sprintf("%d", int64(s.Value)), // active_threads - "", // started_threads (unknown) - "", // finished_threads (unknown) - } - l.mu.Lock() - wv := csv.NewWriter(l.bufVUs) - _ = wv.Write(row) - wv.Flush() - l.mu.Unlock() - continue - } - // checks -> requests_error(_sm) when value == 0 (failed check) - if s.Metric.Name == "checks" && s.Value == 0 { - m := s.Tags.Map() - checkMsg := m["check"] // e.g. "is status 301" - if checkMsg == "" { - checkMsg = "check failed" - } - // Derive transaction_name from 'group' tag, strip leading '::' - transactionName := "" - if groupVal := m["group"]; groupVal != "" { - transactionName = strings.TrimPrefix(groupVal, "::") - } - responseCode := m["status"] // may be empty - // Use 'name' tag for sampler_name (this is passed from k6 check tags) - samplerName := m["name"] - if samplerName == "" { - // Fallback to check message or generic name - samplerName = checkMsg - } - // URL can be from 'url' tag or empty - url := m["url"] - // Get request/response data and error_response_time if provided via custom tags - requestData := m["request_data"] - responseData := m["response_data"] - responseTime := m["error_response_time"] // response time in ms from check tags - // Generate UUIDv7 using the measurement timestamp - id := generateUUIDv7(ts) - // Build row per schema (includes id field) - if l.isSynthetic { - // SM requests_error_sm (existing table: run_id last): time, scenario_name, ..., id, run_id - row := []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), // time - fmt.Sprintf("%v", l.envTags["scenario_name"]), // scenario_name - fmt.Sprintf("%v", l.envTags["location"]), // location - fmt.Sprintf("%v", l.envTags["node_name"]), // node_name - m["scenario"], // thread_group_name - transactionName, // transaction_name - samplerName, // sampler_name - responseCode, // response_code - responseTime, // response_time - "", // connection_time - url, // url - "", // assertions - checkMsg, // response_message - requestData, // request_headers (used for request data) - "", // response_headers - responseData, // response_data - id, // id (UUIDv7 generated from measurement timestamp) - fmt.Sprintf("%v", l.envTags["run_id"]), // run_id last - } - l.mu.Lock() - we := csv.NewWriter(l.bufErrors) - _ = we.Write(row) - we.Flush() - l.mu.Unlock() - } else { - row := []string{ - time.Unix(0, ts).UTC().Format(time.RFC3339Nano), // time - fmt.Sprintf("%v", l.envTags["run_id"]), // run_id - fmt.Sprintf("%v", l.envTags["location"]), // location - fmt.Sprintf("%v", l.envTags["node_name"]), // node_name - transactionName, // transaction_name - samplerName, // sampler_name - responseCode, // response_code - responseTime, // response_time - "", // connection_time - url, // url - "", // assertions - checkMsg, // response_message - requestData, // request_headers (used for request data) - "", // response_headers - responseData, // response_data - id, // id (UUIDv7 generated from measurement timestamp) - } - l.mu.Lock() - we := csv.NewWriter(l.bufErrors) - _ = we.Write(row) - we.Flush() - l.mu.Unlock() - } - continue - } - // browser_errors -> requests_error(_sm) - if s.Metric.Name == "browser_errors" { - l.processBrowserError(s, ts) - continue - } - // WebSocket metrics -> requests_raw(_sm) - if s.Metric.Name == "ws_connecting" || s.Metric.Name == "ws_ping" { - l.processWebSocketMetric(s, ts) - continue - } - // Custom Trend metrics (custom_*) -> requests_raw(_sm) with minimal schema - if strings.HasPrefix(s.Metric.Name, "custom_") { - l.processCustomTrendMetric(s, ts) - continue - } - // removed http_req_failed-specific emission; handled in processHttpGroup when success == false + l.addMetricSample(s) } } } -// formatRequestData combines request headers and body into a single string for the request_headers column. +// formatRequestData combines request headers and body into a single string for the request_headers +// column. func formatRequestData(headers, body string) string { if headers != "" && body != "" { return headers + "\n" + body @@ -799,20 +410,25 @@ func formatRequestData(headers, body string) string { return body } -func (l *Logger) processHttpGroup(g *MetricGroup) { +func (l *Logger) processHTTPGroup(g *MetricGroup) { if g == nil { return } // Map k6 HTTP metrics to requests_raw(_sm) schema - // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, response_size, response_code, response_connect_time, response_latency, response_time - // SM (existing table: run_id last): time, scenario_name, location, node_name, thread_group_name, transaction_name, sampler_name, success, request_size, response_size, response_code, response_connect_time, response_latency, response_time, run_id - - // sampler_name: use path (normalized URL path if name was URL, or name itself capped at 64 chars if not) + // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, + // response_size, response_code, response_connect_time, response_latency, response_time + // SM (existing table: run_id last): time, scenario_name, location, node_name, thread_group_name, + // transaction_name, sampler_name, success, request_size, response_size, response_code, + // response_connect_time, response_latency, response_time, run_id + + // sampler_name: use path (normalized URL path if name was URL, or name itself capped at 64 chars + // if not) samplerName := g.Tags["path"] if samplerName == "" { samplerName = g.Tags["name"] } - // transaction_name: only set when we have an explicit "transaction" tag or "group" (from group()/group_duration). + // transaction_name: only set when we have an explicit "transaction" tag or "group" (from + // group()/group_duration). // If neither is set, leave empty (do not use sampler_name for browser). isBrowser := g.Tags["resource_type"] != "" var transactionName string @@ -820,14 +436,16 @@ func (l *Logger) processHttpGroup(g *MetricGroup) { transactionName = g.Tags["transaction"] } else if g.Tags["group"] != "" { transactionName = strings.TrimPrefix(g.Tags["group"], "::") - // For browser, group is often auto-filled from path/name in copyTagsForHTTP; treat that as empty so we don't duplicate sampler_name. - if isBrowser && (transactionName == samplerName || transactionName == "/") { + // Native group paths start with "::". Keep these even when their name matches + // the request path; only discard the path/name fallback from copyTagsForHTTP. + if isBrowser && !strings.HasPrefix(g.Tags["group"], "::") && + (transactionName == samplerName || transactionName == "/") { transactionName = "" } } // Browser OPTIONS requests (CORS preflight): append _preflight to sampler_name if isBrowser && g.Tags["method"] == "OPTIONS" { - samplerName = samplerName + "_preflight" + samplerName += "_preflight" } // success is the inverse of http_req_failed @@ -842,19 +460,10 @@ func (l *Logger) processHttpGroup(g *MetricGroup) { // Timing fields (cast to integer strings) - matching JMeter definitions // http_req_duration = sending + waiting + receiving (same for k6 HTTP and browser) // See: https://grafana.com/docs/k6/latest/using-k6/metrics/reference/ - connecting := g.Values["http_req_connecting"] - tlsHandshaking := g.Values["http_req_tls_handshaking"] - sending := g.Values["http_req_sending"] - waiting := g.Values["http_req_waiting"] - duration := g.Values["http_req_duration"] - // JMeter-style: connectTime = TCP connect + TLS handshake - connectTime := fmtInt(connecting + tlsHandshaking) - // JMeter-style: latency = from connect until first byte is received - latency := fmtInt(connecting + tlsHandshaking + sending + waiting) - // JMeter-style: responseTime = full time from TCP connect to complete response received - responseTime := fmtInt(connecting + tlsHandshaking + duration) - - // response_size from browser_data_received when present (browser only); k6 HTTP has no per-request size in this group + connectTime, latency, responseTime := requestTimings(g.Values) + + // response_size from browser_data_received when present (browser only); k6 HTTP has no per-request + // size in this group responseSize := "" if v, ok := g.Values["data_received"]; ok && v >= 0 { responseSize = fmtInt(v) @@ -902,98 +511,9 @@ func (l *Logger) processHttpGroup(g *MetricGroup) { w.Flush() // If not successful, also emit into requests_error(_sm) if success == "false" { - // Build response_message from tags - errText := fmt.Sprintf("%v", g.Tags["error"]) // may be empty - if errText == "" { - errText = "" - } - errMsg := fmt.Sprintf("%v", g.Tags["error_message"]) - if errMsg == "" || errMsg == "" { - // fallback to error_code with prefix - errorCode := fmt.Sprintf("%v", g.Tags["error_code"]) - if errorCode != "" && errorCode != "" { - errMsg = fmt.Sprintf("K6 Error code %s", errorCode) - } else { - errMsg = "" - } - } - // Construct responseMessage: use errText if available, errMsg if available, or both with " - " separator - var responseMessage string - if errText != "" && errMsg != "" { - responseMessage = fmt.Sprintf("%s - %s", errText, errMsg) - } else if errText != "" { - responseMessage = errText - } else if errMsg != "" { - responseMessage = errMsg - } else { - responseMessage = "" - } - responseMessage = strings.TrimSpace(responseMessage) - // Browser 4xx/5xx often have no "error" tag; use status for response_message - if responseMessage == "" && responseCode != "" && responseCode != "0" { - responseMessage = "HTTP " + responseCode - } - nameRaw := g.Tags["name"] - // samplerName already derived; ensure non-empty for k6 HTTP (browser: leave empty when no transaction name) - if samplerName == "" || samplerName == "" { - if !isBrowser { - samplerName = transactionName - if samplerName == "" || samplerName == "" { - samplerName = "/" - } - } - } - // Generate UUIDv7 using the measurement timestamp - id := generateUUIDv7(g.Timestamp) - if l.isSynthetic { - // SM requests_error_sm (existing table: run_id last): time, scenario_name, ..., id, run_id - erow := []string{ - time.Unix(0, g.Timestamp).UTC().Format(time.RFC3339Nano), - g.Tags["scenario_name"], - g.Tags["location"], - g.Tags["node_name"], - g.Tags["scenario"], - transactionName, - samplerName, - responseCode, - responseTime, - connectTime, - nameRaw, - "", - responseMessage, - formatRequestData(g.ErrorReqHeaders, g.ErrorReqBody), // request_headers (includes post payload) - g.ErrorResHeaders, // response_headers - g.ErrorResBody, // response_data - id, // id (UUIDv7) - fmt.Sprintf("%v", l.envTags["run_id"]), - } - we := csv.NewWriter(l.bufErrors) - _ = we.Write(erow) - we.Flush() - } else { - erow := []string{ - time.Unix(0, g.Timestamp).UTC().Format(time.RFC3339Nano), - g.Tags["run_id"], - g.Tags["location"], - g.Tags["node_name"], - transactionName, - samplerName, - responseCode, - responseTime, - connectTime, - nameRaw, - "", - responseMessage, - formatRequestData(g.ErrorReqHeaders, g.ErrorReqBody), // request_headers (includes post payload) - g.ErrorResHeaders, // response_headers - g.ErrorResBody, // response_data - id, // id (UUIDv7) - } - we := csv.NewWriter(l.bufErrors) - _ = we.Write(erow) - we.Flush() - } + l.writeHTTPError(g, transactionName, samplerName, responseCode, responseTime, connectTime, isBrowser) } + l.mu.Unlock() } @@ -1011,33 +531,9 @@ func (l *Logger) processWebSocketMetric(s metrics.Sample, ts int64) { transactionName = strings.TrimPrefix(groupVal, "::") } - // sampler_name: derive from name tag (if not URL, capped at 64), else process url tag, else metric name - var samplerName string - // Special handling for ws_ping: always use 'WSPing' - if s.Metric.Name == "ws_ping" { - samplerName = "WSPing" - } else { - // For ws_connecting: prefer name tag (if not URL, capped at 64), else process url tag, else metric name - nameRaw := m["name"] - if nameRaw != "" { - // Check if name is a URL or plain name - _, _, path, _ := processNameTag(nameRaw, l.customPatterns) - // If name was a URL, path contains the normalized path; if not, path contains the name (capped at 64) - samplerName = path - } else { - // No name tag, try url tag - if urlRaw := m["url"]; urlRaw != "" { - _, _, path, _ := processURL(urlRaw, l.customPatterns) - if path != "" { - samplerName = path - } else { - samplerName = s.Metric.Name - } - } else { - samplerName = s.Metric.Name - } - } - } + // sampler_name: derive from name tag (if not URL, capped at 64), else process url tag, else metric + // name + samplerName := l.webSocketSamplerName(s) // response_code from status tag (101 for successful WebSocket connection) responseCode := m["status"] @@ -1075,7 +571,8 @@ func (l *Logger) processWebSocketMetric(s metrics.Sample, ts int64) { fmt.Sprintf("%v", l.envTags["run_id"]), } } else { - // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, response_size, response_code, response_connect_time, response_latency, response_time + // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, + // response_size, response_code, response_connect_time, response_latency, response_time row = []string{ time.Unix(0, ts).UTC().Format(time.RFC3339Nano), tags["run_id"], @@ -1104,9 +601,7 @@ func (l *Logger) processCustomTrendMetric(s metrics.Sample, ts int64) { // Extract basic tags from envTags (already normalized to snake_case) tags := make(map[string]string) - for k, v := range l.envTags { - tags[k] = v - } + maps.Copy(tags, l.envTags) // Ensure per-sample scenario tag is propagated so SM thread_group_name is set if scen, ok := m["scenario"]; ok && scen != "" { @@ -1125,7 +620,8 @@ func (l *Logger) processCustomTrendMetric(s metrics.Sample, ts int64) { if nameRaw != "" { // Check if name is a URL or plain name _, _, path, _ := processNameTag(nameRaw, l.customPatterns) - // If name was a URL, path contains the normalized path; if not, path contains the name (capped at 64) + // If name was a URL, path contains the normalized path; if not, path contains the name (capped at + // 64) samplerName = path } else { // Fallback to metric name @@ -1159,7 +655,8 @@ func (l *Logger) processCustomTrendMetric(s metrics.Sample, ts int64) { fmt.Sprintf("%v", l.envTags["run_id"]), } } else { - // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, response_size, response_code, response_connect_time, response_latency, response_time + // Non-SM: time, run_id, location, transaction_name, sampler_name, success, request_size, + // response_size, response_code, response_connect_time, response_latency, response_time row = []string{ time.Unix(0, ts).UTC().Format(time.RFC3339Nano), tags["run_id"], @@ -1203,16 +700,18 @@ func (l *Logger) processBrowserError(s metrics.Sample, ts int64) { transactionName = strings.TrimPrefix(groupVal, "::") } - // screenshot from the "screenshot" tag (base64-encoded PNG when K6_BROWSER_SCREENSHOT_ON_ERROR is enabled) + // screenshot from the "screenshot" tag (base64-encoded PNG when K6_BROWSER_SCREENSHOT_ON_ERROR is + // enabled) screenshot := m["screenshot"] screenshotHash := "" if screenshot != "" { // Calculate SHA-256 hash of the screenshot data for deduplication h := sha256.Sum256([]byte(screenshot)) screenshotHash = hex.EncodeToString(h[:]) - fmt.Fprintf(l.out, "[screenshot] received browser error with screenshot len=%d hash=%s\n", len(screenshot), screenshotHash) + _, _ = fmt.Fprintf(l.out, "[screenshot] received browser error with screenshot len=%d hash=%s\n", + len(screenshot), screenshotHash) } else { - fmt.Fprintf(l.out, "[screenshot] received browser error WITHOUT screenshot (tag count=%d)\n", len(m)) + _, _ = fmt.Fprintf(l.out, "[screenshot] received browser error WITHOUT screenshot (tag count=%d)\n", len(m)) } // Generate UUIDv7 @@ -1227,27 +726,27 @@ func (l *Logger) processBrowserError(s metrics.Sample, ts int64) { fmt.Sprintf("%v", l.envTags["scenario_name"]), // scenario_name fmt.Sprintf("%v", l.envTags["location"]), // location fmt.Sprintf("%v", l.envTags["node_name"]), // node_name - m["scenario"], // thread_group_name - transactionName, // transaction_name - samplerName, // sampler_name - "", // response_code - "", // response_time - "", // connection_time - "", // url - "", // assertions - errMsg, // response_message - "", // request_headers - "", // response_headers - screenshotHash, // response_data (screenshot hash reference) - id, // id (UUIDv7) + m["scenario"], // thread_group_name + transactionName, // transaction_name + samplerName, // sampler_name + "", // response_code + "", // response_time + "", // connection_time + "", // url + "", // assertions + errMsg, // response_message + "", // request_headers + "", // response_headers + screenshotHash, // response_data (screenshot hash reference) + id, // id (UUIDv7) fmt.Sprintf("%v", l.envTags["run_id"]), // run_id last } l.mu.Lock() if screenshotHash != "" { - l.pendingScreenshots = append(l.pendingScreenshots, []interface{}{ + l.pendingScreenshots = append(l.pendingScreenshots, []any{ screenshotHash, screenshot, "image/png", len(screenshot), }) - fmt.Fprintf(l.out, "[screenshot] queued for upsert, pendingScreenshots=%d\n", len(l.pendingScreenshots)) + _, _ = fmt.Fprintf(l.out, "[screenshot] queued for upsert, pendingScreenshots=%d\n", len(l.pendingScreenshots)) } we := csv.NewWriter(l.bufErrors) _ = we.Write(row) @@ -1274,7 +773,7 @@ func (l *Logger) processBrowserError(s metrics.Sample, ts int64) { } l.mu.Lock() if screenshotHash != "" { - l.pendingScreenshots = append(l.pendingScreenshots, []interface{}{ + l.pendingScreenshots = append(l.pendingScreenshots, []any{ screenshotHash, screenshot, "image/png", len(screenshot), }) } @@ -1285,14 +784,6 @@ func (l *Logger) processBrowserError(s metrics.Sample, ts int64) { } } -func fmtFloat(v float64) string { return fmt.Sprintf("%v", v) } -func fmtBool(v float64) string { - if v != 0 { - return "true" - } - return "false" -} - func fmtInt(v float64) string { return fmt.Sprintf("%d", int64(v)) } // generateUUIDv7 generates a UUIDv7 using the provided timestamp (nanoseconds since Unix epoch) @@ -1301,30 +792,31 @@ func generateUUIDv7(timestampNanos int64) string { t := time.Unix(0, timestampNanos) // Create a UUIDv7 with the specific timestamp - // UUIDv7 format: 48-bit timestamp (ms) + 4-bit version + 12-bit rand_a + 2-bit variant + 62-bit rand_b + // UUIDv7 format: 48-bit timestamp (ms) + 4-bit version + 12-bit rand_a + 2-bit variant + 62-bit + // rand_b timestampMs := uint64(t.UnixMilli()) // Build UUID bytes manually with the correct timestamp var uuidBytes [16]byte // First 48 bits: timestamp in milliseconds (big-endian) - uuidBytes[0] = byte(timestampMs >> 40) - uuidBytes[1] = byte(timestampMs >> 32) - uuidBytes[2] = byte(timestampMs >> 24) - uuidBytes[3] = byte(timestampMs >> 16) - uuidBytes[4] = byte(timestampMs >> 8) - uuidBytes[5] = byte(timestampMs) + uuidBytes[0] = byte((timestampMs >> 40) & 0xff) + uuidBytes[1] = byte((timestampMs >> 32) & 0xff) + uuidBytes[2] = byte((timestampMs >> 24) & 0xff) + uuidBytes[3] = byte((timestampMs >> 16) & 0xff) + uuidBytes[4] = byte((timestampMs >> 8) & 0xff) + uuidBytes[5] = byte(timestampMs & 0xff) // Generate random bytes for the rest randBytes := make([]byte, 10) rand.Read(randBytes) // Bytes 6-7: version (7) in high nibble + random - uuidBytes[6] = (0x70) | (randBytes[0] & 0x0F) // Version 7 + uuidBytes[6] = 0x70 | (randBytes[0] & 0x0F) // Version 7 uuidBytes[7] = randBytes[1] // Bytes 8-15: variant (10) in high 2 bits + random - uuidBytes[8] = (0x80) | (randBytes[2] & 0x3F) // Variant 10 + uuidBytes[8] = 0x80 | (randBytes[2] & 0x3F) // Variant 10 copy(uuidBytes[9:], randBytes[3:]) u, _ := uuid.FromBytes(uuidBytes[:]) @@ -1364,7 +856,8 @@ func truncateUTF8(s string, maxRunes int) string { return string(runes[:maxRunes]) } -// processNameTag processes the "name" tag: if it's a URL, parse and return path; otherwise return as-is (capped at 64 chars) +// processNameTag processes the "name" tag: if it's a URL, parse and return path; otherwise return +// as-is (capped at 64 chars) func processNameTag(nameRaw string, customPatterns map[string]string) (string, string, string, string) { // Try to parse as URL u, err := url.Parse(nameRaw) @@ -1393,10 +886,10 @@ func processNameTag(nameRaw string, customPatterns map[string]string) (string, s return scheme, host, p, cleanURL } -func copyTagsForHTTP(dst map[string]string, tags map[string]string, env map[string]string, customPatterns map[string]string) { +func copyTagsForHTTP(dst, tags, env, customPatterns map[string]string) { // start with env tags for k, v := range env { - if !excludedTags[k] { + if !excludedTag(k) { // map snake_case for consistency if k == "nodeName" { dst["node_name"] = v @@ -1428,7 +921,7 @@ func copyTagsForHTTP(dst map[string]string, tags map[string]string, env map[stri } } for k, v := range tags { - if !excludedTags[k] { + if !excludedTag(k) { // preserve normalized fields if k == "name" || k == "path" || k == "hostname" || k == "scheme" { continue @@ -1440,7 +933,7 @@ func copyTagsForHTTP(dst map[string]string, tags map[string]string, env map[stri if v, ok := tags["scenario"]; ok { dst["scenario"] = fmt.Sprintf("%v", v) } - // Browser samples often lack "group"; use path or name so processHttpGroup gets a transaction name + // Browser samples often lack "group"; use path or name so processHTTPGroup gets a transaction name if dst["group"] == "" { dst["group"] = dst["path"] if dst["group"] == "" { @@ -1452,15 +945,528 @@ func copyTagsForHTTP(dst map[string]string, tags map[string]string, env map[stri } } -func normalizeRegex(p string) string { - // Translate common PCRE shorthands to Go RE2 equivalents - replacer := strings.NewReplacer( - "\\d", "[0-9]", - "\\D", "[^0-9]", - "\\w", "[A-Za-z0-9_]", - "\\W", "[^A-Za-z0-9_]", - "\\s", "[ \t\r\n\f]", - "\\S", "[^ \t\r\n\f]", - ) - return replacer.Replace(p) +func (l *Logger) processHTTPSample(s metrics.Sample, ts int64, canonicalKey string) { + l.mu.Lock() + g, ok := l.pendingHTTPGroups[ts] + if !ok { + g = &MetricGroup{Timestamp: ts, Tags: map[string]string{}, Values: map[string]float64{}, LastUpdate: time.Now()} + l.pendingHTTPGroups[ts] = g + copyTagsForHTTP(g.Tags, s.Tags.Map(), l.envTags, l.customPatterns) + } + g.Values[canonicalKey] = s.Value + if canonicalKey == "http_req_failed" { + g.ErrorReqHeaders = s.HTTPErrorReqHeaders + g.ErrorReqBody = s.HTTPErrorReqBody + g.ErrorResHeaders = s.HTTPErrorResHeaders + g.ErrorResBody = s.HTTPErrorResBody + } + g.LastUpdate = time.Now() + // Process when we have the 8 required metrics (including http_req_failed); data_received is + // optional (browser only) + if _, hasFailed := g.Values["http_req_failed"]; hasFailed && len(g.Values) >= 8 { + delete(l.pendingHTTPGroups, ts) + l.mu.Unlock() + l.processHTTPGroup(g) + return + } + l.mu.Unlock() +} + +func (l *Logger) processPageSample(s metrics.Sample, ts int64) { + m := s.Tags.Map() + // Prefer explicit 'page' tag when available + pageRaw := fmt.Sprintf("%v", m["page"]) + var transactionName string + if pageRaw != "" && pageRaw != "" { + transactionName = pageRaw + } else { + // Derive a stable transaction_name: prefer name, fallback to group, then '/' + nameRaw := fmt.Sprintf("%v", m["name"]) + if nameRaw == "" || nameRaw == "" { + nameRaw = fmt.Sprintf("%v", m["group"]) // sometimes present + } + if nameRaw == "" || nameRaw == "" { + nameRaw = "/" + } + // Process name: if URL, use path; if not URL, use as-is (capped at 64) + _, _, path, _ := processNameTag(nameRaw, l.customPatterns) + transactionName = path + } + // Non-SM transactions table columns: + // time, run_id, location, transaction_name, success, request_size, response_size, response_time + // SM transactions_sm (run_id last): time, scenario_name, location, node_name, thread_group_name, + // transaction_name, success, request_size, response_size, response_time, run_id + isSM := l.isSynthetic + var row []string + if isSM { + row = []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), + fmt.Sprintf("%v", l.envTags["scenario_name"]), + fmt.Sprintf("%v", l.envTags["location"]), + fmt.Sprintf("%v", l.envTags["node_name"]), + fmt.Sprintf("%v", m["scenario"]), // thread_group_name from k6 scenario tag + transactionName, + "true", + "", + "", + fmtInt(s.Value), + fmt.Sprintf("%v", l.envTags["run_id"]), + } + } else { + row = []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), + fmt.Sprintf("%v", l.envTags["run_id"]), + fmt.Sprintf("%v", l.envTags["location"]), + transactionName, + "true", + "", + "", + fmtInt(s.Value), + } + } + l.mu.Lock() + wt := csv.NewWriter(l.bufTrans) + _ = wt.Write(row) + wt.Flush() + l.mu.Unlock() +} + +func (l *Logger) processGroupSample(s metrics.Sample, ts int64) { + success := strconv.FormatBool(!s.GroupFailed) + m := s.Tags.Map() + // Derive transaction_name from 'group' tag, fallback to 'name' + nameRaw := fmt.Sprintf("%v", m["group"]) + if nameRaw == "" || nameRaw == "" { + nameRaw = fmt.Sprintf("%v", m["name"]) + } + nameRaw = strings.TrimPrefix(nameRaw, "::") + if nameRaw == "" || nameRaw == "" { + nameRaw = "/" + } + isSM := l.isSynthetic + var row []string + if isSM { + // SM transactions_sm (run_id last) + row = []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), + fmt.Sprintf("%v", l.envTags["scenario_name"]), + fmt.Sprintf("%v", l.envTags["location"]), + fmt.Sprintf("%v", l.envTags["node_name"]), + fmt.Sprintf("%v", m["scenario"]), // thread_group_name from k6 scenario + nameRaw, + success, + "", + "", + fmtInt(s.Value), + fmt.Sprintf("%v", l.envTags["run_id"]), + } + } else { + row = []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), + fmt.Sprintf("%v", l.envTags["run_id"]), + fmt.Sprintf("%v", l.envTags["location"]), + nameRaw, + success, + "", + "", + fmtInt(s.Value), + } + } + l.mu.Lock() + wt := csv.NewWriter(l.bufTrans) + _ = wt.Write(row) + wt.Flush() + l.mu.Unlock() +} + +func (l *Logger) processVUsSample(s metrics.Sample, ts int64) { + row := []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), + fmt.Sprintf("%v", l.envTags["run_id"]), + fmt.Sprintf("%v", l.envTags["location"]), + fmt.Sprintf("%v", l.envTags["node_name"]), + fmt.Sprintf("%d", int64(s.Value)), // active_threads + "", // started_threads (unknown) + "", // finished_threads (unknown) + } + l.mu.Lock() + wv := csv.NewWriter(l.bufVUs) + _ = wv.Write(row) + wv.Flush() + l.mu.Unlock() +} + +func (l *Logger) processCheckSample(s metrics.Sample, ts int64) { + m := s.Tags.Map() + checkMsg := m["check"] // e.g. "is status 301" + if checkMsg == "" { + checkMsg = "check failed" + } + // Derive transaction_name from 'group' tag, strip leading '::' + transactionName := "" + if groupVal := m["group"]; groupVal != "" { + transactionName = strings.TrimPrefix(groupVal, "::") + } + responseCode := m["status"] // may be empty + // Use 'name' tag for sampler_name (this is passed from k6 check tags) + samplerName := m["name"] + if samplerName == "" { + // Fallback to check message or generic name + samplerName = checkMsg + } + // URL can be from 'url' tag or empty + url := m["url"] + // Get request/response data and error_response_time if provided via custom tags + requestData := m["request_data"] + responseData := m["response_data"] + responseTime := m["error_response_time"] // response time in ms from check tags + // Generate UUIDv7 using the measurement timestamp + id := generateUUIDv7(ts) + // Build row per schema (includes id field) + if l.isSynthetic { + // SM requests_error_sm (existing table: run_id last): time, scenario_name, ..., id, run_id + row := []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), // time + fmt.Sprintf("%v", l.envTags["scenario_name"]), // scenario_name + fmt.Sprintf("%v", l.envTags["location"]), // location + fmt.Sprintf("%v", l.envTags["node_name"]), // node_name + m["scenario"], // thread_group_name + transactionName, // transaction_name + samplerName, // sampler_name + responseCode, // response_code + responseTime, // response_time + "", // connection_time + url, // url + "", // assertions + checkMsg, // response_message + requestData, // request_headers (used for request data) + "", // response_headers + responseData, // response_data + id, // id (UUIDv7 generated from measurement timestamp) + fmt.Sprintf("%v", l.envTags["run_id"]), // run_id last + } + l.mu.Lock() + we := csv.NewWriter(l.bufErrors) + _ = we.Write(row) + we.Flush() + l.mu.Unlock() + } else { + row := []string{ + time.Unix(0, ts).UTC().Format(time.RFC3339Nano), // time + fmt.Sprintf("%v", l.envTags["run_id"]), // run_id + fmt.Sprintf("%v", l.envTags["location"]), // location + fmt.Sprintf("%v", l.envTags["node_name"]), // node_name + transactionName, // transaction_name + samplerName, // sampler_name + responseCode, // response_code + responseTime, // response_time + "", // connection_time + url, // url + "", // assertions + checkMsg, // response_message + requestData, // request_headers (used for request data) + "", // response_headers + responseData, // response_data + id, // id (UUIDv7 generated from measurement timestamp) + } + l.mu.Lock() + we := csv.NewWriter(l.bufErrors) + _ = we.Write(row) + we.Flush() + l.mu.Unlock() + } +} + +func (l *Logger) batchBody( + bufHTTPSnapshot, bufVUsSnapshot, bufTransSnapshot, bufErrorsSnapshot []byte, +) ([]byte, string) { + var body bytes.Buffer + mw := multipart.NewWriter(&body) + + if len(bufHTTPSnapshot) > 0 { + h := make(textproto.MIMEHeader) + table := "public.requests_raw" + if l.isSynthetic { + table = "public.requests_raw_sm" + } + h.Set("X-Table", table) + h.Set("X-Format", "csv") + pw, _ := mw.CreatePart(h) + _, _ = io.Copy(pw, bytes.NewReader(bufHTTPSnapshot)) + } + if len(bufVUsSnapshot) > 0 && !l.isSynthetic { + h := make(textproto.MIMEHeader) + h.Set("X-Table", "public.virtual_users") + h.Set("X-Format", "csv") + pw, _ := mw.CreatePart(h) + _, _ = io.Copy(pw, bytes.NewReader(bufVUsSnapshot)) + } + if len(bufTransSnapshot) > 0 { + h := make(textproto.MIMEHeader) + table := "public.transactions" + if l.isSynthetic { + table = "public.transactions_sm" + } + h.Set("X-Table", table) + h.Set("X-Format", "csv") + pw, _ := mw.CreatePart(h) + _, _ = io.Copy(pw, bytes.NewReader(bufTransSnapshot)) + } + if len(bufErrorsSnapshot) > 0 { + h := make(textproto.MIMEHeader) + table := "public.requests_error" + if l.isSynthetic { + table = "public.requests_error_sm" + } + h.Set("X-Table", table) + h.Set("X-Format", "csv") + pw, _ := mw.CreatePart(h) + _, _ = io.Copy(pw, bytes.NewReader(bufErrorsSnapshot)) + } + _ = mw.Close() + + // gzip body + var gzBody bytes.Buffer + gz := gzip.NewWriter(&gzBody) + _, _ = gz.Write(body.Bytes()) + _ = gz.Close() + + return gzBody.Bytes(), mw.Boundary() +} + +func (l *Logger) sendBatch(body []byte, boundary string) bool { + start := time.Now() + delay := time.Duration(initialBackoffMs) * time.Millisecond + for attempts := 1; time.Since(start) < maxRetryTimeMs*time.Millisecond; attempts++ { + err := l.postBatch(body, boundary) + if err == nil { + return true + } + elapsed := time.Since(start) + _, _ = fmt.Fprintf(l.out, "pg-proxy ingest attempt %d failed (%v elapsed): %v\n", attempts, elapsed, err) + if elapsed+delay >= maxRetryTimeMs*time.Millisecond { + break + } + time.Sleep(delay) + delay = min(delay*2, maxBackoffMs*time.Millisecond) + } + return false +} + +func (l *Logger) postBatch(body []byte, boundary string) error { + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, + l.proxyURL+"/ingest/batch", bytes.NewReader(body)) + if err != nil { + return err + } + req.Header.Set("Authorization", "Bearer "+l.token) + req.Header.Set("Content-Type", "multipart/mixed; boundary="+boundary) + req.Header.Set("Content-Encoding", "gzip") + resp, err := l.httpClient.Do(req) + if err != nil { + return err + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return fmt.Errorf("status=%d body=%s", resp.StatusCode, body) + } + return nil +} + +// writeHTTPError requires the buffer lock to be held by the caller. +func (l *Logger) writeHTTPError(g *MetricGroup, + transactionName, samplerName, responseCode, responseTime, connectTime string, isBrowser bool, +) { + // Build response_message from tags + errText := fmt.Sprintf("%v", g.Tags["error"]) // may be empty + if errText == "" { + errText = "" + } + errMsg := fmt.Sprintf("%v", g.Tags["error_message"]) + if errMsg == "" || errMsg == "" { + // fallback to error_code with prefix + errorCode := fmt.Sprintf("%v", g.Tags["error_code"]) + if errorCode != "" && errorCode != "" { + errMsg = fmt.Sprintf("K6 Error code %s", errorCode) + } else { + errMsg = "" + } + } + // Construct responseMessage: use errText if available, errMsg if available, or both with " - " + // separator + var responseMessage string + switch { + case errText != "" && errMsg != "": + responseMessage = fmt.Sprintf("%s - %s", errText, errMsg) + case errText != "": + responseMessage = errText + case errMsg != "": + responseMessage = errMsg + default: + responseMessage = "" + } + responseMessage = strings.TrimSpace(responseMessage) + // Browser 4xx/5xx often have no "error" tag; use status for response_message + if responseMessage == "" && responseCode != "" && responseCode != "0" { + responseMessage = "HTTP " + responseCode + } + nameRaw := g.Tags["name"] + // samplerName already derived; ensure non-empty for k6 HTTP (browser: leave empty when no + // transaction name) + if samplerName == "" || samplerName == "" { + if !isBrowser { + samplerName = transactionName + if samplerName == "" || samplerName == "" { + samplerName = "/" + } + } + } + // Generate UUIDv7 using the measurement timestamp + id := generateUUIDv7(g.Timestamp) + if l.isSynthetic { + // SM requests_error_sm (existing table: run_id last): time, scenario_name, ..., id, run_id + erow := []string{ + time.Unix(0, g.Timestamp).UTC().Format(time.RFC3339Nano), + g.Tags["scenario_name"], + g.Tags["location"], + g.Tags["node_name"], + g.Tags["scenario"], + transactionName, + samplerName, + responseCode, + responseTime, + connectTime, + nameRaw, + "", + responseMessage, + formatRequestData(g.ErrorReqHeaders, g.ErrorReqBody), // request_headers (includes post payload) + g.ErrorResHeaders, // response_headers + g.ErrorResBody, // response_data + id, // id (UUIDv7) + fmt.Sprintf("%v", l.envTags["run_id"]), + } + we := csv.NewWriter(l.bufErrors) + _ = we.Write(erow) + we.Flush() + } else { + erow := []string{ + time.Unix(0, g.Timestamp).UTC().Format(time.RFC3339Nano), + g.Tags["run_id"], + g.Tags["location"], + g.Tags["node_name"], + transactionName, + samplerName, + responseCode, + responseTime, + connectTime, + nameRaw, + "", + responseMessage, + formatRequestData(g.ErrorReqHeaders, g.ErrorReqBody), // request_headers (includes post payload) + g.ErrorResHeaders, // response_headers + g.ErrorResBody, // response_data + id, // id (UUIDv7) + } + we := csv.NewWriter(l.bufErrors) + _ = we.Write(erow) + we.Flush() + } +} + +func (l *Logger) webSocketSamplerName(s metrics.Sample) string { + if s.Metric.Name == "ws_ping" { + return "WSPing" + } + tags := s.Tags.Map() + if name := tags["name"]; name != "" { + _, _, path, _ := processNameTag(name, l.customPatterns) + return path + } + if raw := tags["url"]; raw != "" { + _, _, path, _ := processURL(raw, l.customPatterns) + if path != "" { + return path + } + } + return s.Metric.Name +} + +func (l *Logger) addMetricSample(s metrics.Sample) { + ts := s.Time.UnixNano() + canonicalKey := httpMetricCanonicalKey(s.Metric.Name) + if canonicalKey != "" { + l.processHTTPSample(s, ts, canonicalKey) + return + } + if (s.Metric.Name == "pages" && l.transactionSource == transactionsGroups) || + (s.Metric.Name == "group_duration" && l.transactionSource == transactionsPages) { + return + } + // pages -> transactions + if s.Metric.Name == "pages" { + l.processPageSample(s, ts) + return + } + // Native HTTP and browser groups -> transactions. + if s.Metric.Name == "group_duration" { + l.processGroupSample(s, ts) + return + } + // vus -> virtual_users (active_threads) + if s.Metric.Name == "vus" && !l.isSynthetic { + l.processVUsSample(s, ts) + return + } + // checks -> requests_error(_sm) when value == 0 (failed check) + if s.Metric.Name == "checks" && s.Value == 0 { + l.processCheckSample(s, ts) + return + } + // browser_errors -> requests_error(_sm) + if s.Metric.Name == "browser_errors" { + l.processBrowserError(s, ts) + return + } + // WebSocket metrics -> requests_raw(_sm) + if s.Metric.Name == "ws_connecting" || s.Metric.Name == "ws_ping" { + l.processWebSocketMetric(s, ts) + return + } + // Custom Trend metrics (custom_*) -> requests_raw(_sm) with minimal schema + if strings.HasPrefix(s.Metric.Name, "custom_") { + l.processCustomTrendMetric(s, ts) + return + } + // removed http_req_failed-specific emission; handled in processHTTPGroup when success == false +} + +func parseCustomPatterns(out io.Writer, patterns string) map[string]string { + // Parse CUSTOM_URL_PATTERNS env (pattern==replacement comma-separated) + customPatterns := make(map[string]string) + if patterns != "" { + _, _ = fmt.Fprintf(out, "Found CUSTOM_URL_PATTERNS environment variable: %s\n", patterns) + for pair := range strings.SplitSeq(patterns, ",") { + parts := strings.Split(pair, "==") + if len(parts) == 2 { + customPatterns[parts[0]] = parts[1] + _, _ = fmt.Fprintf(out, "Added custom pattern: %s -> %s\n", parts[0], parts[1]) + } else { + _, _ = fmt.Fprintf(out, "Warning: Invalid pattern format: %s\n", pair) + } + } + _, _ = fmt.Fprintf(out, "Total custom patterns loaded: %d\n", len(customPatterns)) + } else { + _, _ = fmt.Fprintf(out, "No CUSTOM_URL_PATTERNS environment variable found\n") + } + + return customPatterns +} + +// requestTimings converts k6 timings to the JMeter-style fields expected by BreakTest. +func requestTimings(values map[string]float64) (connectTime, latency, responseTime string) { + connect := values["http_req_connecting"] + values["http_req_tls_handshaking"] + return fmtInt(connect), + fmtInt(connect + values["http_req_sending"] + values["http_req_waiting"]), + fmtInt(connect + values["http_req_duration"]) } diff --git a/internal/output/breakingit/config_test.go b/internal/output/breakingit/config_test.go new file mode 100644 index 000000000..560070f53 --- /dev/null +++ b/internal/output/breakingit/config_test.go @@ -0,0 +1,49 @@ +package breakingit + +import ( + "io" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/output" +) + +func TestConfiguredEnvironment(t *testing.T) { + t.Parallel() + exporter, err := New(output.Params{ + ConfigArgument: "https://proxy.example.test", StdOut: io.Discard, + Environment: map[string]string{ + "PG_PROXY_TOKEN": "configured-token", "PG_PROXY_DB": "configured-db", + "PG_PROXY_INSECURE_SKIP_TLS_VERIFY": "false", "syntheticMonitoring": "true", + "runId": "run", "nodeName": "node", "scenarioName": "scenario", "location": "AMS", + "CUSTOM_URL_PATTERNS": "[0-9]+=={id}", + }, + }) + require.NoError(t, err) + t.Cleanup(func() { assert.NoError(t, exporter.Stop()) }) + logger := exporter.(*Logger) + assert.Equal(t, "configured-token", logger.token) + assert.Equal(t, "configured-db", logger.database) + assert.True(t, logger.isSynthetic) + assert.Equal(t, "run", logger.envTags["run_id"]) + assert.Equal(t, "node", logger.envTags["node_name"]) + assert.Equal(t, "scenario", logger.envTags["scenario_name"]) + assert.Equal(t, "AMS", logger.envTags["location"]) + assert.Equal(t, "{id}", logger.customPatterns["[0-9]+"]) + assert.False(t, logger.httpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify) +} + +func TestInvalidProxyTLSConfiguration(t *testing.T) { + t.Parallel() + exporter, err := New(output.Params{ + ConfigArgument: "https://proxy.example.test", StdOut: io.Discard, + Environment: map[string]string{ + "PG_PROXY_TOKEN": "test", "PG_PROXY_INSECURE_SKIP_TLS_VERIFY": "invalid", + }, + }) + require.ErrorContains(t, err, "PG_PROXY_INSECURE_SKIP_TLS_VERIFY") + assert.Nil(t, exporter) +} diff --git a/internal/output/breakingit/http_rows_test.go b/internal/output/breakingit/http_rows_test.go new file mode 100644 index 000000000..6e46bb805 --- /dev/null +++ b/internal/output/breakingit/http_rows_test.go @@ -0,0 +1,65 @@ +package breakingit + +import ( + "encoding/csv" + "fmt" + "testing" + "time" + + "github.com/gofrs/uuid/v5" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFailedHTTPRows(t *testing.T) { + t.Parallel() + for _, synthetic := range []bool{false, true} { + t.Run(fmt.Sprint(synthetic), func(t *testing.T) { + t.Parallel() + logger := newTransactionTestLogger(synthetic) + at := time.Date(2026, 9, 24, 12, 0, 0, 123000000, time.UTC) + logger.processHTTPGroup(&MetricGroup{ + Timestamp: at.UnixNano(), + Tags: map[string]string{ + "group": "::Checkout", "path": "/pay", "name": "https://example.test/pay", + "status": "500", "run_id": "run-123", "location": "Amsterdam", + "node_name": "node-1", "scenario_name": "checkout", "scenario": "browser_checkout", + }, + Values: map[string]float64{ + "http_req_failed": 1, "http_req_connecting": 2, "http_req_tls_handshaking": 3, + "http_req_sending": 4, "http_req_waiting": 5, "http_req_duration": 20, + }, + ErrorReqHeaders: "POST /pay", ErrorReqBody: "payload", + ErrorResHeaders: "HTTP/1.1 500", ErrorResBody: "failure", + }) + requests, err := csv.NewReader(logger.bufHTTP).ReadAll() + require.NoError(t, err) + require.Len(t, requests, 1) + offset := 3 + if synthetic { + offset = 5 + } + assert.Equal(t, []string{"Checkout", "/pay", "false", "", "", "500", "5", "14", "25"}, + requests[0][offset:offset+9]) + errors, err := csv.NewReader(logger.bufErrors).ReadAll() + require.NoError(t, err) + require.Len(t, errors, 1) + offset = 4 + if synthetic { + offset = 5 + } + assert.Equal(t, []string{ + "Checkout", "/pay", "500", "25", "5", "https://example.test/pay", "", + "HTTP 500", "POST /pay\npayload", "HTTP/1.1 500", "failure", + }, errors[0][offset:offset+11]) + id, err := uuid.FromString(errors[0][offset+11]) + require.NoError(t, err) + assert.EqualValues(t, 7, id.Version()) + stamp, err := uuid.TimestampFromV7(id) + require.NoError(t, err) + stampTime, err := stamp.Time() + require.NoError(t, err) + assert.Equal(t, at.UnixMilli(), stampTime.UnixMilli()) + }) + } +} diff --git a/internal/output/breakingit/transactions.go b/internal/output/breakingit/transactions.go new file mode 100644 index 000000000..ae8df8177 --- /dev/null +++ b/internal/output/breakingit/transactions.go @@ -0,0 +1,26 @@ +package breakingit + +import "fmt" + +type transactionSource uint8 + +const ( + // Keep both existing mappings enabled by default for backwards compatibility. + transactionsBoth transactionSource = iota + transactionsPages + transactionsGroups +) + +func parseTransactionSource(value string) (transactionSource, error) { + switch value { + case "", "both": + return transactionsBoth, nil + case "pages": + return transactionsPages, nil + case "groups": + return transactionsGroups, nil + default: + return transactionsBoth, fmt.Errorf( + "invalid BREAKINGIT_TRANSACTION_SOURCE %q: expected both, pages, or groups", value) + } +} diff --git a/internal/output/breakingit/transactions_test.go b/internal/output/breakingit/transactions_test.go new file mode 100644 index 000000000..8c8009ab3 --- /dev/null +++ b/internal/output/breakingit/transactions_test.go @@ -0,0 +1,238 @@ +package breakingit + +import ( + "bytes" + "compress/gzip" + "encoding/csv" + "fmt" + "io" + "mime" + "mime/multipart" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.k6.io/k6/v2/metrics" + "go.k6.io/k6/v2/output" +) + +func newTransactionTestLogger(synthetic bool) *Logger { + return &Logger{ + out: io.Discard, isSynthetic: synthetic, + bufHTTP: &bytes.Buffer{}, bufVUs: &bytes.Buffer{}, + bufTrans: &bytes.Buffer{}, bufErrors: &bytes.Buffer{}, + pendingHTTPGroups: make(map[int64]*MetricGroup), + envTags: map[string]string{ + "run_id": "run-123", "location": "Amsterdam", "node_name": "node-1", + "scenario_name": "checkout", + }, + } +} + +func transactionTestSample(name string, tags map[string]string, failed bool) metrics.Sample { + registry := metrics.NewRegistry() + return metrics.Sample{ + TimeSeries: metrics.TimeSeries{ + Metric: registry.MustNewMetric(name, metrics.Trend), + Tags: registry.RootTagSet().WithTagsFromMap(tags), + }, + Time: time.Date(2026, 9, 24, 12, 0, 0, 123000000, time.UTC), Value: 123.987, + GroupFailed: failed, + } +} + +func TestTransactionRows(t *testing.T) { + t.Parallel() + tests := []struct { + name, metric, expectedName string + tags map[string]string + failed bool + }{ + {"legacy page", "pages", "01_Home", map[string]string{"page": "01_Home", "group": "::wrapper"}, false}, + {"legacy URL fallback", "pages", "/home", map[string]string{"name": "https://example.test/home"}, false}, + {"legacy default", "pages", "/", nil, false}, + {"native success", "group_duration", "01_Home", map[string]string{"group": "::01_Home"}, false}, + {"native failure", "group_duration", "02_Pay", map[string]string{"group": "::02_Pay"}, true}, + {"nested", "group_duration", "Checkout::Payment", map[string]string{"group": "::Checkout::Payment"}, true}, + {"CSV escaping", "group_duration", "A,\"B\"\nC", map[string]string{"group": "::A,\"B\"\nC"}, false}, + {"group fallback", "group_duration", "fallback", map[string]string{"name": "fallback"}, false}, + {"group default", "group_duration", "/", nil, false}, + } + for _, synthetic := range []bool{false, true} { + for _, test := range tests { + t.Run(fmt.Sprintf("synthetic=%t/%s", synthetic, test.name), func(t *testing.T) { + t.Parallel() + l := newTransactionTestLogger(synthetic) + sample := transactionTestSample(test.metric, test.tags, test.failed) + sample.Tags = sample.Tags.With("scenario", "browser_checkout") + l.AddMetricSamples([]metrics.SampleContainer{sample}) + rows, err := csv.NewReader(l.bufTrans).ReadAll() + require.NoError(t, err) + expected := []string{ + "2026-09-24T12:00:00.123Z", "run-123", "Amsterdam", test.expectedName, + fmt.Sprint(!test.failed), "", "", "123", + } + if synthetic { + expected = []string{ + "2026-09-24T12:00:00.123Z", "checkout", "Amsterdam", "node-1", + "browser_checkout", test.expectedName, fmt.Sprint(!test.failed), + "", "", "123", "run-123", + } + } + assert.Equal(t, [][]string{expected}, rows) + }) + } + } +} + +func TestTransactionSource(t *testing.T) { + t.Parallel() + for _, test := range []struct { + value string + want []string + }{ + {"", []string{"legacy", "native"}}, + {"both", []string{"legacy", "native"}}, + {"pages", []string{"legacy"}}, + {"groups", []string{"native"}}, + } { + t.Run(test.value, func(t *testing.T) { + t.Parallel() + l := newTransactionTestLogger(false) + var err error + l.transactionSource, err = parseTransactionSource(test.value) + require.NoError(t, err) + l.AddMetricSamples([]metrics.SampleContainer{metrics.Samples{ + transactionTestSample("pages", map[string]string{"page": "legacy"}, false), + transactionTestSample("group_duration", map[string]string{"group": "::native"}, false), + }}) + rows, err := csv.NewReader(l.bufTrans).ReadAll() + require.NoError(t, err) + var names []string + for _, row := range rows { + names = append(names, row[3]) + } + assert.Equal(t, test.want, names) + // Filtering transaction durations must not drop failed checks. + check := transactionTestSample("checks", map[string]string{"group": "::native", "check": "bad"}, false) + check.Value = 0 + l.AddMetricSamples([]metrics.SampleContainer{check}) + errors, err := csv.NewReader(l.bufErrors).ReadAll() + require.NoError(t, err) + require.Len(t, errors, 1) + assert.Equal(t, "native", errors[0][4]) + }) + } +} + +func TestTransactionSourceConfiguration(t *testing.T) { + t.Parallel() + for _, value := range []string{"", "both", "groups", "pages", "invalid"} { + exporter, err := New(output.Params{ + ConfigArgument: "http://127.0.0.1", StdOut: io.Discard, + Environment: map[string]string{"BREAKINGIT_TRANSACTION_SOURCE": value, "PG_PROXY_TOKEN": "test-token"}, + }) + if value == "invalid" { + require.ErrorContains(t, err, "BREAKINGIT_TRANSACTION_SOURCE") + require.Nil(t, exporter) + continue + } + require.NoError(t, err) + t.Cleanup(func() { assert.NoError(t, exporter.Stop()) }) + want, err := parseTransactionSource(value) + require.NoError(t, err) + assert.Equal(t, want, exporter.(*Logger).transactionSource, "source=%q", value) + } +} + +func TestBrowserRequestKeepsExplicitGroup(t *testing.T) { + t.Parallel() + for _, synthetic := range []bool{false, true} { + for _, test := range []struct{ group, path, want string }{ + {"::Checkout::Pay", "/pay", "Checkout::Pay"}, + {"::/home", "/home", "/home"}, + {"::/", "/", "/"}, + {"/home", "/home", ""}, + {"/", "/", ""}, + } { + l := newTransactionTestLogger(synthetic) + l.processHTTPGroup(&MetricGroup{ + Tags: map[string]string{"group": test.group, "path": test.path, "resource_type": "Document"}, + }) + rows, err := csv.NewReader(l.bufHTTP).ReadAll() + require.NoError(t, err) + require.Len(t, rows, 1) + index := 3 + if synthetic { + index = 5 + } + assert.Equal(t, test.want, rows[0][index], "group=%q synthetic=%t", test.group, synthetic) + } + } +} + +func TestTransactionsSentToProxy(t *testing.T) { + t.Parallel() + for _, synthetic := range []bool{false, true} { + t.Run(fmt.Sprint(synthetic), func(t *testing.T) { + t.Parallel() + type batch struct { + table string + rows [][]string + } + batches := make(chan batch, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/ingest/batch", r.URL.Path) + assert.Equal(t, "Bearer test-token", r.Header.Get("Authorization")) + assert.Equal(t, "gzip", r.Header.Get("Content-Encoding")) + gz, err := gzip.NewReader(r.Body) + if !assert.NoError(t, err) { + w.WriteHeader(http.StatusOK) + return + } + defer func() { assert.NoError(t, gz.Close()) }() + _, params, err := mime.ParseMediaType(r.Header.Get("Content-Type")) + assert.NoError(t, err) + reader := multipart.NewReader(gz, params["boundary"]) + part, err := reader.NextPart() + if !assert.NoError(t, err) { + w.WriteHeader(http.StatusOK) + return + } + assert.Equal(t, "csv", part.Header.Get("X-Format")) + rows, err := csv.NewReader(part).ReadAll() + assert.NoError(t, err) + batches <- batch{part.Header.Get("X-Table"), rows} + _, err = reader.NextPart() + assert.ErrorIs(t, err, io.EOF) + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + l := newTransactionTestLogger(synthetic) + l.proxyURL, l.token, l.httpClient = server.URL, "test-token", server.Client() + l.AddMetricSamples([]metrics.SampleContainer{ + transactionTestSample("pages", map[string]string{"page": "legacy"}, false), + transactionTestSample("group_duration", map[string]string{"group": "::native"}, true), + }) + l.flush() + select { + case got := <-batches: + wantTable, index := "public.transactions", 3 + if synthetic { + wantTable, index = "public.transactions_sm", 5 + } + assert.Equal(t, wantTable, got.table) + require.Len(t, got.rows, 2) + assert.Equal(t, []string{"legacy", "true"}, got.rows[0][index:index+2]) + assert.Equal(t, []string{"native", "false"}, got.rows[1][index:index+2]) + default: + t.Fatal("no transaction batch received") + } + assert.Zero(t, l.bufTrans.Len()) + }) + } +} diff --git a/internal/output/cloud/output.go b/internal/output/cloud/output.go index a1b4c2dd6..1480443f1 100644 --- a/internal/output/cloud/output.go +++ b/internal/output/cloud/output.go @@ -2,9 +2,12 @@ package cloud import ( + "context" "errors" "fmt" + "net/http" "path/filepath" + "strconv" "strings" "time" @@ -14,6 +17,7 @@ import ( "go.k6.io/k6/v2/cloudapi" "go.k6.io/k6/v2/errext" "go.k6.io/k6/v2/internal/build" + "go.k6.io/k6/v2/internal/cloudapi/provisioning" "go.k6.io/k6/v2/internal/usage" "go.k6.io/k6/v2/lib" "go.k6.io/k6/v2/metrics" @@ -50,6 +54,29 @@ type cloudClient interface { TestFinished(referenceID string, thresholds cloudapi.ThresholdResult, tainted bool, runStatus cloudapi.RunStatus) error } +// metricsPusher is the metrics-push HTTP layer used in provisioning mode. +// Production implementation: *provisioning.HTTPClient. +// Tests inject mocks via field assignment. +type metricsPusher interface { + Do(req *http.Request, v any) error +} + +// provisioningNotifier is the orchestrator-level client used to call notify +// at end-of-test in provisioning mode. +// Production implementation: *provisioning.Client. +// Tests inject mocks via field assignment. +type provisioningNotifier interface { + NotifyTestRunCompleted(ctx context.Context, testRunID int64, token string, testErr error) error +} + +// logDrainer flushes buffered cloud logs so they reach the backend before the +// run is notified complete (afterwards the backend rejects late pushes). Kept +// structural (no import of internal/log/cloud) to avoid coupling the output to +// that package. Production implementation: *cloudlog.Pusher. +type logDrainer interface { + Drain(context.Context) error +} + type apiVersion int64 const ( @@ -81,6 +108,21 @@ type Output struct { client cloudClient testStopFunc func(error) + // Provisioning-mode dependencies. nil for --out cloud and PushRefID; + // lazy-constructed in Start when Config.MetricsPushURL.Valid. + // Tests inject mocks via field assignment. + metricsPusher metricsPusher + provisioningNotifier provisioningNotifier + + // provisioningMode gates the provisioning-mode runtime path. Set in + // lazyInitProvisioning after both deps are constructed; read by + // startVersionedOutput and testFinished. + provisioningMode bool + + // logDrainer, when set, is flushed before notify so in-run logs land while + // the run is still open. nil for --out cloud / --no-cloud-logs. + logDrainer logDrainer + usage *usage.Usage } @@ -192,6 +234,28 @@ func (out *Output) Start() error { if out.testRunID != "" { out.logger.WithField("testRunId", out.testRunID).Debug("Directly pushing metrics without init") + + // Exactly one of the scoped push creds set is a misconfiguration. + // The cmd layer already validates this for the externally-provisioned + // env case (internal/cmd/outputs_cloud.go); this is the defensive + // backstop for any other source (e.g. a hand-written cloud config). + if out.config.MetricsPushURL.Valid != out.config.TestRunToken.Valid { + return errors.New( + "both K6_CLOUD_METRICS_PUSH_URL and K6_CLOUD_TEST_RUN_TOKEN " + + "must be set together") + } + + // Provisioning-mode push. Armed whenever the scoped push credentials + // are present: either k6 self-provisioned (no PushRefID) or an external + // service provisioned the run and its creds were injected into the + // cloud config by the cmd layer (PushRefID set; that service owns + // create/start/notify). + if out.config.MetricsPushURL.Valid && out.config.TestRunToken.Valid { + if err := out.lazyInitProvisioning(); err != nil { + return err + } + } + return out.startVersionedOutput() } @@ -263,6 +327,11 @@ func (out *Output) SetArchive(archive *lib.Archive) { out.testArchive = archive } +// SetLogDrainer sets the cloud-log drainer flushed before end-of-test notify. +func (out *Output) SetLogDrainer(d logDrainer) { + out.logDrainer = d +} + var _ output.WithArchive = &Output{} // Stop gracefully stops all metric emission from the output: when all metric @@ -283,6 +352,17 @@ func (out *Output) StopWithTestError(testErr error) error { // to notify the cloud backend we have no return here } + // Flush buffered cloud logs before notify: once the run is notified + // complete the backend rejects late log pushes, so the final batch would + // be lost. The push is already bounded by the loki hook's own HTTP client + // timeout, so the drain just waits for it on a fresh context — fresh + // because the run's context is already cancelled at shutdown. + if out.logDrainer != nil { + if err := out.logDrainer.Drain(context.Background()); err != nil { + out.logger.WithError(err).Warn("could not drain cloud logs before notify") + } + } + out.logger.Debug("Metric emission stopped, calling cloud API...") err = out.testFinished(testErr) if err != nil { @@ -298,6 +378,23 @@ func (out *Output) testFinished(testErr error) error { return nil } + // Provisioning mode: notify instead of legacy TestFinished. Gate on + // the explicit flag set by lazyInitProvisioning, not on Config fields. + if out.provisioningMode { + // The provisioning API test-run IDs are int64; parse the full range. + testRunIDInt, err := strconv.ParseInt(out.testRunID, 10, 64) + if err != nil { + out.logger.WithError(err).Warn("could not parse testRunID for notify; skipping") + return nil + } + return out.provisioningNotifier.NotifyTestRunCompleted( + context.Background(), + testRunIDInt, + out.config.TestRunToken.String, + testErr, + ) + } + testTainted := false thresholdResults := make(cloudapi.ThresholdResult) for name, thresholds := range out.thresholds { @@ -326,8 +423,7 @@ func (out *Output) getRunStatus(testErr error) cloudapi.RunStatus { return cloudapi.RunStatusFinished } - var err errext.HasAbortReason - if errors.As(testErr, &err) { + if err, ok := errors.AsType[errext.HasAbortReason](testErr); ok { abortReason := err.AbortReason() switch abortReason { case errext.AbortedByUser: @@ -392,6 +488,17 @@ func (out *Output) startVersionedOutput() error { err = errors.New("v1 is not supported anymore") case int64(apiVersion2): out.versionedOutput, err = cloudv2.New(out.logger, out.config, nil) + if err != nil { + break + } + + // Inject provisioning overrides if in provisioning mode. + if out.provisioningMode { + if v, ok := out.versionedOutput.(*cloudv2.Output); ok { + v.SetMetricsHTTPClient(out.metricsPusher) + v.SetMetricsURL(out.config.MetricsPushURL.String) + } + } default: err = fmt.Errorf("v%d is an unexpected version", out.config.APIVersion.Int64) } @@ -404,3 +511,55 @@ func (out *Output) startVersionedOutput() error { out.versionedOutput.SetTestRunStopCallback(out.testStopFunc) return out.versionedOutput.Start() } + +// lazyInitProvisioning constructs the provisioning-mode dependencies +// (metrics-push HTTP client and provisioning notifier) when they haven't +// been injected already. Tests inject mocks via field assignment before +// calling Start. +func (out *Output) lazyInitProvisioning() error { + // Lazy-construct the metrics-push HTTP client if not already injected + // (tests inject mocks via field assignment). + if out.metricsPusher == nil { + // Fresh *http.Client with the configured timeout (mirrors what + // cloudapi.Client constructs internally). Body-reset on retries + // is handled inside provisioning.HTTPClient.Do (via doWithRetry's + // explicit req.GetBody() between attempts), not at the transport + // layer — so a vanilla http.Client suffices. + httpClient := &http.Client{ + Timeout: out.config.Timeout.TimeDuration(), + } + out.metricsPusher = provisioning.NewHTTPClient( + httpClient, + out.config.TestRunToken.String, + build.Version, + out.logger, + ) + } + + // Lazy-construct the provisioning notifier (end-of-test notify). + // Skipped when PushRefID is set: an external service owns the run + // lifecycle, so k6 never notifies (testFinished returns early on + // PushRefID) and the notifier would be unused. Invariant: a nil + // notifier with provisioningMode true implies PushRefID is set. + if out.provisioningNotifier == nil && !out.config.PushRefID.Valid { + // The stack ID is required and passed as int64; provisioning.NewClient + // enforces the int32 X-Stack-Id boundary internally. + c, err := provisioning.NewClient( + out.logger, + out.config.Token.String, // long-lived k6 token (NOT scoped) + out.config.Hostv6.String, + build.Version, + out.config.StackID.Int64, + out.config.Timeout.TimeDuration(), + ) + if err != nil { + return fmt.Errorf("provisioning client init: %w", err) + } + out.provisioningNotifier = c + } + + // Both deps constructed; arm the gate. + out.provisioningMode = true + + return nil +} diff --git a/internal/output/cloud/output_test.go b/internal/output/cloud/output_test.go index 149193dca..2af0a85b9 100644 --- a/internal/output/cloud/output_test.go +++ b/internal/output/cloud/output_test.go @@ -1,11 +1,14 @@ package cloud import ( + "context" + "encoding/json" "errors" "fmt" "net/http" "net/http/httptest" "net/url" + "sync/atomic" "testing" "testing/synctest" "time" @@ -275,8 +278,37 @@ func TestOutputStopWithTestError(t *testing.T) { }) } +// Locks in the symmetric-gating property: the stop-time decision must read +// out.provisioningMode (set by lazyInitProvisioning), never the Config fields +// directly. Otherwise a future divergence (Config fields populated without +// lazyInit running) would nil-deref out.provisioningNotifier. +func TestOutputStopWithTestError_ConfigFieldsAloneDoNotInferProvisioningMode(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + mock := &cloudClientMock{} + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "1234", + client: mock, + // Config fields populated; lazyInit did not run; deps nil. + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://ingest.example/metrics/abc"), + TestRunToken: null.StringFrom("scoped-token"), + }, + } + out.versionedOutput = versionedOutputMock{callback: func(string) {}} + + require.NoError(t, out.StopWithTestError(nil)) + + require.True(t, mock.testFinishedCalled, + "legacy TestFinished must be called when provisioningMode is false") + }) +} + // cloudClientMock implements cloudClient for tests without starting a real HTTP server. type cloudClientMock struct { + createTestRunFn func(*cloudapi.TestRun) (*cloudapi.CreateTestRunResponse, error) + testFinishedCalled bool testFinishedRefID string testFinishedThresholds cloudapi.ThresholdResult @@ -284,7 +316,10 @@ type cloudClientMock struct { testFinishedRunStatus cloudapi.RunStatus } -func (m *cloudClientMock) CreateTestRun(_ *cloudapi.TestRun) (*cloudapi.CreateTestRunResponse, error) { +func (m *cloudClientMock) CreateTestRun(tr *cloudapi.TestRun) (*cloudapi.CreateTestRunResponse, error) { + if m.createTestRunFn != nil { + return m.createTestRunFn(tr) + } return &cloudapi.CreateTestRunResponse{}, nil } @@ -364,3 +399,532 @@ func (o versionedOutputMock) SetTestRunID(_ string) { func (o versionedOutputMock) AddMetricSamples(_ []metrics.SampleContainer) { o.callback("AddMetricSamples") } + +// metricsPusherMock implements metricsPusher for tests. +type metricsPusherMock struct { + doCalls atomic.Int32 +} + +func (m *metricsPusherMock) Do(_ *http.Request, _ any) error { + m.doCalls.Add(1) + return nil +} + +// provisioningNotifierMock implements provisioningNotifier for tests. +type provisioningNotifierMock struct { + called atomic.Bool + callCount atomic.Int32 + + // Captured arguments from the most recent call. + testRunID int64 + token string + testErr error +} + +func (m *provisioningNotifierMock) NotifyTestRunCompleted(_ context.Context, testRunID int64, token string, testErr error) error { + m.called.Store(true) + m.callCount.Add(1) + m.testRunID = testRunID + m.token = token + m.testErr = testErr + return nil +} + +// logDrainerMock implements the output's logDrainer for tests. onDrain, when +// set, runs inside Drain so a test can observe ordering relative to notify. +// ctxErr records ctx.Err() at call time so a test can assert the drain wasn't +// handed an already-expired context. +type logDrainerMock struct { + called atomic.Bool + ctxErr error + onDrain func() +} + +func (m *logDrainerMock) Drain(ctx context.Context) error { + m.called.Store(true) + m.ctxErr = ctx.Err() + if m.onDrain != nil { + m.onDrain() + } + return nil +} + +func TestOutputStart_ProvisioningMode(t *testing.T) { + t.Parallel() + + // Build the JSON config containing MetricsPushURL and TestRunToken + // (the provisioning-mode fields on cloudapi.Config). + cfgJSON, err := json.Marshal(cloudapi.Config{ + Host: null.StringFrom("https://fake-cloud"), + Token: null.StringFrom("fake-long-lived-token"), + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + APIVersion: null.IntFrom(2), + AggregationPeriod: types.NullDurationFrom(1 * time.Hour), + MetricPushInterval: types.NullDurationFrom(1 * time.Hour), + AggregationWaitPeriod: types.NullDurationFrom(1 * time.Second), + MaxTimeSeriesInBatch: null.IntFrom(1000), + MetricPushConcurrency: null.IntFrom(1), + }) + require.NoError(t, err) + + out, err := newOutput(output.Params{ + Logger: testutils.NewLogger(t), + JSONConfig: cfgJSON, + ScriptOptions: lib.Options{ + Duration: types.NullDurationFrom(1 * time.Second), + SystemTags: &metrics.DefaultSystemTagSet, + }, + ScriptPath: &url.URL{Path: "/script.js"}, + RuntimeOptions: lib.RuntimeOptions{ + Env: map[string]string{ + "K6_CLOUDRUN_TEST_RUN_ID": "12345", + }, + }, + Usage: usage.New(), + }) + require.NoError(t, err) + + // Inject a cloudClient mock whose CreateTestRun would fail the test + // if called — regression assertion that the provisioning path never + // falls through to the CreateTestRun legacy branch. + out.client = &cloudClientMock{ + createTestRunFn: func(_ *cloudapi.TestRun) (*cloudapi.CreateTestRunResponse, error) { + t.Fatal("CreateTestRun must not be called in provisioning mode") + return nil, errors.New("unreachable") + }, + } + + // Inject mocks for the provisioning-mode dependencies. + pusherMock := &metricsPusherMock{} + notifierMock := &provisioningNotifierMock{} + out.metricsPusher = pusherMock + out.provisioningNotifier = notifierMock + + // Start should succeed. + require.NoError(t, out.Start()) + + // The versionedOutput must be an *expv2.Output (api version 2). + expv2Out, ok := out.versionedOutput.(*cloudv2.Output) + require.True(t, ok, "expected versionedOutput to be *cloudv2.Output") + assert.NotNil(t, expv2Out) + + // Feed a sample so the stop-time flush exercises the metrics-push path, + // and confirm it goes through the injected metricsPusher rather than a + // real HTTP client. + r := metrics.NewRegistry() + m := r.MustNewMetric("test_metric", metrics.Counter) + out.AddMetricSamples([]metrics.SampleContainer{ + metrics.Sample{ + TimeSeries: metrics.TimeSeries{Metric: m, Tags: r.RootTagSet()}, + Time: time.Now(), + Value: 1.0, + }, + }) + + // Stop force-flushes the queued samples. + require.NoError(t, out.StopWithTestError(nil)) + + assert.Positive(t, pusherMock.doCalls.Load(), + "queued metrics should be pushed through the injected metricsPusher") +} + +// TestOutputStart_ExternallyProvisioned covers the flow where an +// external service provisioned the run and passes the scoped push +// creds (MetricsPushURL + TestRunToken) plus PushRefID via env. k6 +// must push to the provisioning URL with the scoped token but must +// NOT build a notifier — the external service owns create/start/ +// notify (see testFinished, which returns early on PushRefID). +func TestOutputStart_ExternallyProvisioned(t *testing.T) { + t.Parallel() + + cfgJSON, err := json.Marshal(cloudapi.Config{ + Host: null.StringFrom("https://fake-cloud"), + Token: null.StringFrom("fake-token"), + PushRefID: null.StringFrom("99999"), + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + APIVersion: null.IntFrom(2), + AggregationPeriod: types.NullDurationFrom(1 * time.Hour), + MetricPushInterval: types.NullDurationFrom(1 * time.Hour), + AggregationWaitPeriod: types.NullDurationFrom(1 * time.Second), + MaxTimeSeriesInBatch: null.IntFrom(1000), + MetricPushConcurrency: null.IntFrom(1), + }) + require.NoError(t, err) + + out, err := newOutput(output.Params{ + Logger: testutils.NewLogger(t), + JSONConfig: cfgJSON, + ScriptOptions: lib.Options{ + Duration: types.NullDurationFrom(1 * time.Second), + SystemTags: &metrics.DefaultSystemTagSet, + }, + ScriptPath: &url.URL{Path: "/script.js"}, + Usage: usage.New(), + }) + require.NoError(t, err) + + // Inject a metrics-pusher mock so Start arms the provisioning push + // without constructing a real HTTP client (mirrors + // TestOutputStart_ProvisioningMode). provisioningNotifier is left nil + // to prove lazyInitProvisioning skips it when PushRefID is set. + out.metricsPusher = &metricsPusherMock{} + + require.NoError(t, out.Start()) + + // PushRefID still supplies the test run id. + assert.Equal(t, "99999", out.testRunID) + + // Provisioning push is armed on scoped-cred presence, even with PushRefID. + assert.True(t, out.provisioningMode) + assert.NotNil(t, out.metricsPusher) + + // No notifier: the external service owns the run lifecycle. + assert.Nil(t, out.provisioningNotifier) + + require.NoError(t, out.StopWithTestError(nil)) +} + +// TestOutputStart_ExternalProvisioned_RequiresBothCreds locks the +// "no silent legacy fallback on partial config" behaviour: if only one +// of the scoped push creds is set, Start errors (naming both env vars) +// instead of quietly falling back to the legacy push endpoint. +func TestOutputStart_ExternalProvisioned_RequiresBothCreds(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + metricsPushURL null.String + testRunToken null.String + }{ + { + name: "only MetricsPushURL set", + metricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + }, + { + name: "only TestRunToken set", + testRunToken: null.StringFrom("scoped-test-run-token"), + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + cfgJSON, err := json.Marshal(cloudapi.Config{ + Host: null.StringFrom("https://fake-cloud"), + Token: null.StringFrom("fake-token"), + PushRefID: null.StringFrom("99999"), + MetricsPushURL: tc.metricsPushURL, + TestRunToken: tc.testRunToken, + APIVersion: null.IntFrom(2), + AggregationPeriod: types.NullDurationFrom(1 * time.Hour), + MetricPushInterval: types.NullDurationFrom(1 * time.Hour), + AggregationWaitPeriod: types.NullDurationFrom(1 * time.Second), + MaxTimeSeriesInBatch: null.IntFrom(1000), + MetricPushConcurrency: null.IntFrom(1), + }) + require.NoError(t, err) + + out, err := newOutput(output.Params{ + Logger: testutils.NewLogger(t), + JSONConfig: cfgJSON, + ScriptOptions: lib.Options{ + Duration: types.NullDurationFrom(1 * time.Second), + SystemTags: &metrics.DefaultSystemTagSet, + }, + ScriptPath: &url.URL{Path: "/script.js"}, + Usage: usage.New(), + }) + require.NoError(t, err) + + err = out.Start() + require.Error(t, err) + assert.Contains(t, err.Error(), "K6_CLOUD_METRICS_PUSH_URL") + assert.Contains(t, err.Error(), "K6_CLOUD_TEST_RUN_TOKEN") + }) + } +} + +// TestOutputStopWithTestError_ExternalProvisioned_NoNotifier locks the +// invariant "notifier nil ⇒ PushRefID set ⇒ testFinished returns early": +// with provisioningMode true and a nil notifier, Stop must not panic and +// must neither notify nor call the legacy TestFinished. +func TestOutputStopWithTestError_ExternalProvisioned_NoNotifier(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + clientMock := &cloudClientMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + PushRefID: null.StringFrom("99999"), + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + }, + client: clientMock, + provisioningNotifier: nil, + provisioningMode: true, + } + + out.versionedOutput = versionedOutputMock{ + callback: func(_ string) {}, + } + + // Must not panic even though provisioningNotifier is nil while + // provisioningMode is true — testFinished returns early on PushRefID + // (so the nil notifier is never dereferenced, and notify never fires). + require.NoError(t, out.StopWithTestError(nil)) + + // Legacy TestFinished is not called either (PushRefID short-circuit). + assert.False(t, clientMock.testFinishedCalled) + }) +} + +func TestOutputStopWithTestError_ProvisioningMode_NoError(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + } + + out.versionedOutput = versionedOutputMock{ + callback: func(_ string) {}, + } + + require.NoError(t, out.StopWithTestError(nil)) + + // Notify was called exactly once with the right arguments. + assert.True(t, notifierMock.called.Load()) + assert.Equal(t, int32(1), notifierMock.callCount.Load()) + assert.Equal(t, int64(12345), notifierMock.testRunID) + assert.Equal(t, "scoped-test-run-token", notifierMock.token) + assert.Nil(t, notifierMock.testErr) + + // TestFinished was NOT called (regression assertion). + assert.False(t, clientMock.testFinishedCalled) + }) +} + +func TestOutputStopWithTestError_ProvisioningMode_WithTestError(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + + scriptErr := errext.WithAbortReasonIfNone( + errors.New("script error"), + errext.AbortedByScriptError, + ) + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + } + + out.versionedOutput = versionedOutputMock{ + callback: func(_ string) {}, + } + + require.NoError(t, out.StopWithTestError(scriptErr)) + + // Notify was called with the error verbatim (mapping to code 8035 + // happens inside the real notifier, which is mocked here). + assert.True(t, notifierMock.called.Load()) + assert.Equal(t, int64(12345), notifierMock.testRunID) + assert.Equal(t, "scoped-test-run-token", notifierMock.token) + assert.Equal(t, scriptErr, notifierMock.testErr) + + // TestFinished was NOT called. + assert.False(t, clientMock.testFinishedCalled) + }) +} + +func TestOutputStopWithTestError_PushRefID_NoNotifyNoTestFinished(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + PushRefID: null.StringFrom("99999"), + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + }, + client: clientMock, + provisioningNotifier: notifierMock, + } + + out.versionedOutput = versionedOutputMock{ + callback: func(_ string) {}, + } + + require.NoError(t, out.StopWithTestError(nil)) + + // PushRefID short-circuits: NEITHER notify NOR TestFinished called. + assert.False(t, notifierMock.called.Load()) + assert.False(t, clientMock.testFinishedCalled) + }) +} + +// TestOutputStopWithTestError_DrainsLogsBeforeNotify locks the ordering fix: +// the cloud log drainer is flushed before the run is notified complete, so +// in-run logs land while the run is still open (afterwards the backend +// rejects late pushes). +func TestOutputStopWithTestError_DrainsLogsBeforeNotify(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + drainerMock := &logDrainerMock{ + onDrain: func() { + // Drain must run before notify: the notifier has not been + // called yet at drain time. + assert.Equal(t, int32(0), notifierMock.callCount.Load(), + "Drain must be called before NotifyTestRunCompleted") + }, + } + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + Timeout: types.NullDurationFrom(60 * time.Second), + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + logDrainer: drainerMock, + } + out.versionedOutput = versionedOutputMock{callback: func(string) {}} + + require.NoError(t, out.StopWithTestError(nil)) + + assert.True(t, drainerMock.called.Load(), "Drain must be called") + assert.True(t, notifierMock.called.Load(), "notify must still happen after drain") + }) +} + +// TestOutputStopWithTestError_PushRefID_DrainsLogs guards that the cloud log +// drain runs even on the externally-provisioned (PushRefID) path, where notify +// is skipped. If a future change tied the drain to notify, this flow would +// silently lose its final log batch and no other test would catch it. +func TestOutputStopWithTestError_PushRefID_DrainsLogs(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + drainerMock := &logDrainerMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + PushRefID: null.StringFrom("99999"), + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + Timeout: types.NullDurationFrom(60 * time.Second), + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + logDrainer: drainerMock, + } + out.versionedOutput = versionedOutputMock{callback: func(string) {}} + + require.NoError(t, out.StopWithTestError(nil)) + + // Drain runs even though the PushRefID path skips notify/TestFinished. + assert.True(t, drainerMock.called.Load(), "Drain must run on the PushRefID path too") + assert.False(t, notifierMock.called.Load(), "PushRefID path must not notify") + assert.False(t, clientMock.testFinishedCalled, "PushRefID path must not call TestFinished") + }) +} + +// TestOutputStopWithTestError_ZeroTimeoutDrainsWithoutDeadline guards that +// K6_CLOUD_TIMEOUT=0 ("no timeout", as the other cloud clients treat it) does +// not hand the drain an already-expired context, which would skip the flush +// and let notify race it. +func TestOutputStopWithTestError_ZeroTimeoutDrainsWithoutDeadline(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + drainerMock := &logDrainerMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + Timeout: types.NewNullDuration(0, true), // K6_CLOUD_TIMEOUT=0 + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + logDrainer: drainerMock, + } + out.versionedOutput = versionedOutputMock{callback: func(string) {}} + + require.NoError(t, out.StopWithTestError(nil)) + + assert.True(t, drainerMock.called.Load(), "Drain must be called") + assert.NoError(t, drainerMock.ctxErr, + "drain context must not be already expired when the timeout is 0 (no timeout)") + }) +} + +// TestOutputStopWithTestError_NilLogDrainerStillNotifies covers the +// --out cloud / --no-cloud-logs case: with no drainer set, Stop neither +// panics nor skips notify. +func TestOutputStopWithTestError_NilLogDrainerStillNotifies(t *testing.T) { + t.Parallel() + synctest.Test(t, func(t *testing.T) { + notifierMock := &provisioningNotifierMock{} + clientMock := &cloudClientMock{} + + out := &Output{ + logger: testutils.NewLogger(t), + testRunID: "12345", + config: cloudapi.Config{ + MetricsPushURL: null.StringFrom("https://metrics.example.com/v2/metrics/123"), + TestRunToken: null.StringFrom("scoped-test-run-token"), + Timeout: types.NullDurationFrom(60 * time.Second), + }, + client: clientMock, + provisioningNotifier: notifierMock, + provisioningMode: true, + logDrainer: nil, + } + out.versionedOutput = versionedOutputMock{callback: func(string) {}} + + require.NoError(t, out.StopWithTestError(nil)) + assert.True(t, notifierMock.called.Load()) + }) +} diff --git a/internal/output/influxdb/config.go b/internal/output/influxdb/config.go index dfd124d17..6306680ff 100644 --- a/internal/output/influxdb/config.go +++ b/internal/output/influxdb/config.go @@ -107,6 +107,22 @@ func ParseJSON(data json.RawMessage) (Config, error) { return conf, err } +// splitPathPrefixAndDB splits a URL path into the address prefix kept as part +// of Addr and a database name. The last segment is treated as the DB name, +// allowing InfluxDB to be hosted behind a reverse proxy that routes on a path +// prefix (e.g. https://host/influxdbA/dbname -> prefix=/influxdbA, db=dbname). +// A trailing slash means the whole path is the prefix and no DB is specified. +func splitPathPrefixAndDB(path string) (prefix, db string) { + if path == "" { + return "", "" + } + if rest, ok := strings.CutSuffix(path, "/"); ok { + return rest, "" + } + idx := strings.LastIndex(path, "/") + return path[:idx], path[idx+1:] +} + // ParseURL parses the supplied URL into a Config. func ParseURL(text string) (Config, error) { c := Config{} @@ -115,9 +131,12 @@ func ParseURL(text string) (Config, error) { return c, err } if u.Host != "" { - c.Addr = null.StringFrom(u.Scheme + "://" + u.Host) - } - if db := strings.TrimPrefix(u.Path, "/"); db != "" { + prefix, db := splitPathPrefixAndDB(u.Path) + c.Addr = null.StringFrom(u.Scheme + "://" + u.Host + prefix) + if db != "" { + c.DB = null.StringFrom(db) + } + } else if db := strings.TrimPrefix(u.Path, "/"); db != "" { c.DB = null.StringFrom(db) } if u.User != nil { diff --git a/internal/output/influxdb/config_test.go b/internal/output/influxdb/config_test.go index 2c709df98..c1f0eaa4b 100644 --- a/internal/output/influxdb/config_test.go +++ b/internal/output/influxdb/config_test.go @@ -15,6 +15,15 @@ func TestParseURL(t *testing.T) { "/dbname": {DB: null.StringFrom("dbname")}, "http://localhost:8086": {Addr: null.StringFrom("http://localhost:8086")}, "http://localhost:8086/dbname": {Addr: null.StringFrom("http://localhost:8086"), DB: null.StringFrom("dbname")}, + "http://my.site.io/influxdbA/k6db": { + Addr: null.StringFrom("http://my.site.io/influxdbA"), + DB: null.StringFrom("k6db"), + }, + "http://my.site.io/a/b/c/dbname": { + Addr: null.StringFrom("http://my.site.io/a/b/c"), + DB: null.StringFrom("dbname"), + }, + "http://my.site.io/prefix/": {Addr: null.StringFrom("http://my.site.io/prefix")}, } queries := map[string]struct { Config Config diff --git a/internal/output/influxdb/output_test.go b/internal/output/influxdb/output_test.go index d70f4f377..debb7c36f 100644 --- a/internal/output/influxdb/output_test.go +++ b/internal/output/influxdb/output_test.go @@ -133,6 +133,50 @@ func TestOutput(t *testing.T) { }) } +func TestOutputWritesToURLPathPrefix(t *testing.T) { + t.Parallel() + + var ( + mu sync.Mutex + gotPath string + ) + ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/ping" { + mu.Lock() + gotPath = r.URL.Path + mu.Unlock() + } + rw.WriteHeader(http.StatusNoContent) + })) + defer ts.Close() + + registry := metrics.NewRegistry() + metric, err := registry.NewMetric("test_gauge", metrics.Gauge) + require.NoError(t, err) + + o, err := newOutput(output.Params{ + Logger: testutils.NewLogger(t), + ConfigArgument: ts.URL + "/influxdbA/k6db", + }) + require.NoError(t, err) + require.Equal(t, "k6db", o.Config.DB.String) + require.Equal(t, ts.URL+"/influxdbA", o.Config.Addr.String) + + require.NoError(t, o.Start()) + o.AddMetricSamples([]metrics.SampleContainer{metrics.Samples{ + metrics.Sample{ + TimeSeries: metrics.TimeSeries{Metric: metric, Tags: registry.RootTagSet()}, + Time: time.Now(), + Value: 1.0, + }, + }}) + require.NoError(t, o.Stop()) + + mu.Lock() + defer mu.Unlock() + assert.Equal(t, "/influxdbA/write", gotPath) +} + func TestOutputFlushMetricsConcurrency(t *testing.T) { t.Parallel() diff --git a/internal/output/opentelemetry/exporter.go b/internal/output/opentelemetry/exporter.go index 0738208c7..b1fbcd14c 100644 --- a/internal/output/opentelemetry/exporter.go +++ b/internal/output/opentelemetry/exporter.go @@ -31,7 +31,7 @@ func getExporter(cfg Config) (metric.Exporter, error) { return nil, err } - var headers map[string]string + headers := make(map[string]string) if cfg.Headers.Valid { headers, err = parseHeaders(cfg.Headers.String) if err != nil { diff --git a/internal/output/opentelemetry/exporter_test.go b/internal/output/opentelemetry/exporter_test.go new file mode 100644 index 000000000..3e684383c --- /dev/null +++ b/internal/output/opentelemetry/exporter_test.go @@ -0,0 +1,82 @@ +package opentelemetry + +import ( + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/guregu/null.v3" +) + +// TestGetExporterBasicAuth verifies that configuring HTTP Basic Auth via +// K6_OTEL_HTTP_EXPORTER_USERNAME/PASSWORD does not panic when no headers are +// configured through K6_OTEL_HEADERS. Previously the headers map was nil and +// assigning the Authorization header caused a "assignment to entry in nil map" +// panic. +func TestGetExporterBasicAuth(t *testing.T) { + t.Parallel() + + httpBase := func() Config { + return Config{ + ExporterProtocol: null.StringFrom(httpExporterProtocol), + HTTPExporterEndpoint: null.StringFrom("localhost:4318"), + HTTPExporterURLPath: null.StringFrom("/v1/metrics"), + } + } + + grpcBase := func() Config { + return Config{ + ExporterProtocol: null.StringFrom(grpcExporterProtocol), + GRPCExporterEndpoint: null.StringFrom("localhost:4317"), + } + } + + testCases := map[string]struct { + cfg func() Config + }{ + "http basic auth without headers": { + cfg: func() Config { + cfg := httpBase() + cfg.HTTPUsername = null.StringFrom("user") + cfg.HTTPPassword = null.StringFrom("pass") + return cfg + }, + }, + "http basic auth with headers": { + cfg: func() Config { + cfg := httpBase() + cfg.HTTPUsername = null.StringFrom("user") + cfg.HTTPPassword = null.StringFrom("pass") + cfg.Headers = null.StringFrom("X-Scope-OrgID=test") + return cfg + }, + }, + "http headers only": { + cfg: func() Config { + cfg := httpBase() + cfg.Headers = null.StringFrom("X-Scope-OrgID=test") + return cfg + }, + }, + "http no auth no headers": { + cfg: httpBase, + }, + "grpc basic auth without headers": { + cfg: func() Config { + cfg := grpcBase() + cfg.HTTPUsername = null.StringFrom("user") + cfg.HTTPPassword = null.StringFrom("pass") + return cfg + }, + }, + } + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + exporter, err := getExporter(tc.cfg()) + require.NoError(t, err) + require.NotNil(t, exporter) + }) + } +} diff --git a/internal/output/prometheusrw/remote/client.go b/internal/output/prometheusrw/remote/client.go index 7f1db14a7..d767b416a 100644 --- a/internal/output/prometheusrw/remote/client.go +++ b/internal/output/prometheusrw/remote/client.go @@ -30,7 +30,7 @@ type HTTPConfig struct { // BasicAuth holds the config for basic authentication. type BasicAuth struct { - Username, Password string //nolint:gosec + Username, Password string } // WriteClient is a client implementation of the Prometheus remote write protocol. @@ -100,7 +100,7 @@ func (c *WriteClient) Store(ctx context.Context, series []*prompb.TimeSeries) er req.Header.Set("Content-Type", "application/x-protobuf") req.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0") - resp, err := c.hc.Do(req) //nolint:gosec + resp, err := c.hc.Do(req) if err != nil { return fmt.Errorf("HTTP POST request failed: %w", err) } @@ -128,7 +128,7 @@ func newWriteRequestBody(series []*prompb.TimeSeries) ([]byte, error) { } if snappy.MaxEncodedLen(len(b)) < 0 { return nil, fmt.Errorf("the protobuf message is too large to be handled by Snappy encoder; "+ - "size: %d, limit: %d", len(b), math.MaxUint32) + "size: %d, limit: %d", len(b), uint64(math.MaxUint32)) } return snappy.Encode(nil, b), nil } diff --git a/internal/output/prometheusrw/remotewrite/config.go b/internal/output/prometheusrw/remotewrite/config.go index 5afbc0bd7..73809c806 100644 --- a/internal/output/prometheusrw/remotewrite/config.go +++ b/internal/output/prometheusrw/remotewrite/config.go @@ -36,6 +36,9 @@ type Config struct { // Headers contains additional headers that should be included in the HTTP requests. Headers map[string]string `json:"headers"` + // Labels contains additional labels that should be included in the remote write time series. + Labels map[string]string `json:"labels"` + // InsecureSkipTLSVerify skips TLS client side checks. InsecureSkipTLSVerify null.Bool `json:"insecureSkipTLSVerify" envconfig:"K6_PROMETHEUS_RW_INSECURE_SKIP_TLS_VERIFY"` @@ -224,6 +227,13 @@ func (conf Config) Apply(applied Config) Config { maps.Copy(conf.Headers, applied.Headers) } + if len(applied.Labels) > 0 { + if conf.Labels == nil { + conf.Labels = make(map[string]string) + } + maps.Copy(conf.Labels, applied.Labels) + } + if len(applied.TrendStats) > 0 { conf.TrendStats = make([]string, len(applied.TrendStats)) copy(conf.TrendStats, applied.TrendStats) @@ -291,6 +301,7 @@ func envMap(env map[string]string, prefix string) map[string]string { func parseEnvs(env map[string]string) (Config, error) { c := Config{ Headers: make(map[string]string), + Labels: make(map[string]string), } err := envconfig.Process("", &c, func(key string) (string, bool) { @@ -316,6 +327,16 @@ func parseEnvs(env map[string]string) (Config, error) { } } + if labels, labelsDefined := env["K6_PROMETHEUS_RW_LABELS"]; labelsDefined { + for kvPair := range strings.SplitSeq(labels, ",") { + key, value, ok := strings.Cut(kvPair, "=") + if !ok { + return c, fmt.Errorf("the provided label (%s) does not respect the expected format