name: "mdns: build/target-tests"

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened, labeled]

jobs:
  build_mdns:
    if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push'
    name: Build
    strategy:
      matrix:
        idf_ver: ["latest", "release-v5.0", "release-v5.2", "release-v5.3"]
        test: [ { app: example, path: "examples/query_advertise" }, { app: unit_test, path: "tests/unit_test" }, { app: test_app, path: "tests/test_apps" } ]
    runs-on: ubuntu-22.04
    container: espressif/idf:${{ matrix.idf_ver }}
    steps:
      - name: Checkout esp-protocols
        uses: actions/checkout@v4
      - name: Build ${{ matrix.test.app }} with IDF-${{ matrix.idf_ver }}
        shell: bash
        run: |
          . ${IDF_PATH}/export.sh
          python -m pip install idf-build-apps
          # Build default configs for all targets
          python ./ci/build_apps.py components/mdns/${{ matrix.test.path }} -r default -d
          # Build specific configs for test targets
          python ./ci/build_apps.py components/mdns/${{ matrix.test.path }}
          cd components/mdns/${{ matrix.test.path }}
          for dir in `ls -d build_esp32_*`; do
          $GITHUB_WORKSPACE/ci/clean_build_artifacts.sh `pwd`/$dir
          zip -qur artifacts.zip $dir
          done
      - uses: actions/upload-artifact@v4
        with:
          name: mdns_bin_esp32_${{ matrix.idf_ver }}_${{ matrix.test.app }}
          path: components/mdns/${{ matrix.test.path }}/artifacts.zip
          if-no-files-found: error

  target_tests_mdns:
    # Skip running on forks since it won't have access to secrets
    if: |
      github.repository == 'espressif/esp-protocols' &&
      ( contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push' )
    name: Target Example and Unit tests
    strategy:
      matrix:
        idf_ver: ["latest"]
        idf_target: ["esp32"]
        test: [ { app: example, path: "examples/query_advertise" }, { app: unit_test, path: "tests/unit_test" }, { app: test_app, path: "tests/test_apps" } ]
    needs: build_mdns
    runs-on:
      - self-hosted
      - ESP32-ETHERNET-KIT
    steps:
      - name: Clear repository
        run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          name: mdns_bin_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${{ matrix.test.app }}
          path: components/mdns/${{ matrix.test.path }}/ci/
      - name: Install Python packages
        env:
          PIP_EXTRA_INDEX_URL: "https://www.piwheels.org/simple"
        run: |
            sudo apt-get install -y dnsutils
      - name: Run ${{ matrix.test.app }} application on ${{ matrix.idf_target }}
        working-directory: components/mdns/${{ matrix.test.path }}
        run: |
            unzip ci/artifacts.zip -d ci
            for dir in `ls -d ci/build_*`; do
            rm -rf build sdkconfig.defaults
            mv $dir build
            python -m pytest --log-cli-level DEBUG --junit-xml=./results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}_${dir#"ci/build_"}.xml --target=${{ matrix.idf_target }}
            done
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: results_${{ matrix.test.app }}_${{ matrix.idf_target }}_${{ matrix.idf_ver }}.xml
          path: components/mdns/${{ matrix.test.path }}/*.xml
