mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-30 13:08:04 +00:00
Compare commits
2 Commits
support-of
...
wip-suppor
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
230f359bf7 | ||
|
|
f996e60784 |
66
.github/workflows/builder.yml
vendored
66
.github/workflows/builder.yml
vendored
@@ -34,9 +34,6 @@ on:
|
|||||||
|
|
||||||
env:
|
env:
|
||||||
DEFAULT_PYTHON: "3.13"
|
DEFAULT_PYTHON: "3.13"
|
||||||
COSIGN_VERSION: "v2.5.3"
|
|
||||||
CRANE_VERSION: "v0.20.7"
|
|
||||||
CRANE_SHA256: "8ef3564d264e6b5ca93f7b7f5652704c4dd29d33935aff6947dd5adefd05953e"
|
|
||||||
BUILD_NAME: supervisor
|
BUILD_NAME: supervisor
|
||||||
BUILD_TYPE: supervisor
|
BUILD_TYPE: supervisor
|
||||||
|
|
||||||
@@ -56,7 +53,7 @@ jobs:
|
|||||||
requirements: ${{ steps.requirements.outputs.changed }}
|
requirements: ${{ steps.requirements.outputs.changed }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the repository
|
- name: Checkout the repository
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
@@ -95,7 +92,7 @@ jobs:
|
|||||||
arch: ${{ fromJson(needs.init.outputs.architectures) }}
|
arch: ${{ fromJson(needs.init.outputs.architectures) }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the repository
|
- name: Checkout the repository
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
@@ -110,7 +107,7 @@ jobs:
|
|||||||
# home-assistant/wheels doesn't support sha pinning
|
# home-assistant/wheels doesn't support sha pinning
|
||||||
- name: Build wheels
|
- name: Build wheels
|
||||||
if: needs.init.outputs.requirements == 'true'
|
if: needs.init.outputs.requirements == 'true'
|
||||||
uses: home-assistant/wheels@2025.11.0
|
uses: home-assistant/wheels@2025.10.0
|
||||||
with:
|
with:
|
||||||
abi: cp313
|
abi: cp313
|
||||||
tag: musllinux_1_2
|
tag: musllinux_1_2
|
||||||
@@ -129,7 +126,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
|
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
|
||||||
if: needs.init.outputs.publish == 'true'
|
if: needs.init.outputs.publish == 'true'
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.DEFAULT_PYTHON }}
|
python-version: ${{ env.DEFAULT_PYTHON }}
|
||||||
|
|
||||||
@@ -137,7 +134,7 @@ jobs:
|
|||||||
if: needs.init.outputs.publish == 'true'
|
if: needs.init.outputs.publish == 'true'
|
||||||
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
||||||
with:
|
with:
|
||||||
cosign-release: ${{ env.COSIGN_VERSION }}
|
cosign-release: "v2.5.3"
|
||||||
|
|
||||||
- name: Install dirhash and calc hash
|
- name: Install dirhash and calc hash
|
||||||
if: needs.init.outputs.publish == 'true'
|
if: needs.init.outputs.publish == 'true'
|
||||||
@@ -176,12 +173,12 @@ jobs:
|
|||||||
|
|
||||||
version:
|
version:
|
||||||
name: Update version
|
name: Update version
|
||||||
needs: ["init", "run_supervisor", "retag_deprecated"]
|
needs: ["init", "run_supervisor"]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the repository
|
- name: Checkout the repository
|
||||||
if: needs.init.outputs.publish == 'true'
|
if: needs.init.outputs.publish == 'true'
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
|
|
||||||
- name: Initialize git
|
- name: Initialize git
|
||||||
if: needs.init.outputs.publish == 'true'
|
if: needs.init.outputs.publish == 'true'
|
||||||
@@ -206,7 +203,7 @@ jobs:
|
|||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the repository
|
- name: Checkout the repository
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
|
|
||||||
# home-assistant/builder doesn't support sha pinning
|
# home-assistant/builder doesn't support sha pinning
|
||||||
- name: Build the Supervisor
|
- name: Build the Supervisor
|
||||||
@@ -355,50 +352,3 @@ jobs:
|
|||||||
- name: Get supervisor logs on failiure
|
- name: Get supervisor logs on failiure
|
||||||
if: ${{ cancelled() || failure() }}
|
if: ${{ cancelled() || failure() }}
|
||||||
run: docker logs hassio_supervisor
|
run: docker logs hassio_supervisor
|
||||||
|
|
||||||
retag_deprecated:
|
|
||||||
needs: ["build", "init"]
|
|
||||||
name: Re-tag deprecated ${{ matrix.arch }} images
|
|
||||||
if: needs.init.outputs.publish == 'true'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
id-token: write
|
|
||||||
packages: write
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
arch: ["armhf", "armv7", "i386"]
|
|
||||||
env:
|
|
||||||
# Last available release for deprecated architectures
|
|
||||||
FROZEN_VERSION: "2025.11.5"
|
|
||||||
steps:
|
|
||||||
- name: Login to GitHub Container Registry
|
|
||||||
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.repository_owner }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Install Cosign
|
|
||||||
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
|
||||||
with:
|
|
||||||
cosign-release: ${{ env.COSIGN_VERSION }}
|
|
||||||
|
|
||||||
- name: Install crane
|
|
||||||
run: |
|
|
||||||
curl -sLO https://github.com/google/go-containerregistry/releases/download/${{ env.CRANE_VERSION }}/go-containerregistry_Linux_x86_64.tar.gz
|
|
||||||
echo "${{ env.CRANE_SHA256 }} go-containerregistry_Linux_x86_64.tar.gz" | sha256sum -c -
|
|
||||||
tar xzf go-containerregistry_Linux_x86_64.tar.gz crane
|
|
||||||
sudo mv crane /usr/local/bin/
|
|
||||||
|
|
||||||
- name: Re-tag deprecated image with updated version label
|
|
||||||
run: |
|
|
||||||
crane auth login ghcr.io -u ${{ github.repository_owner }} -p ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
crane mutate \
|
|
||||||
--label io.hass.version=${{ needs.init.outputs.version }} \
|
|
||||||
--tag ghcr.io/home-assistant/${{ matrix.arch }}-hassio-supervisor:${{ needs.init.outputs.version }} \
|
|
||||||
ghcr.io/home-assistant/${{ matrix.arch }}-hassio-supervisor:${{ env.FROZEN_VERSION }}
|
|
||||||
|
|
||||||
- name: Sign image with Cosign
|
|
||||||
run: |
|
|
||||||
cosign sign --yes ghcr.io/home-assistant/${{ matrix.arch }}-hassio-supervisor:${{ needs.init.outputs.version }}
|
|
||||||
|
|||||||
38
.github/workflows/ci.yaml
vendored
38
.github/workflows/ci.yaml
vendored
@@ -26,10 +26,10 @@ jobs:
|
|||||||
name: Prepare Python dependencies
|
name: Prepare Python dependencies
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
id: python
|
id: python
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
with:
|
with:
|
||||||
python-version: ${{ env.DEFAULT_PYTHON }}
|
python-version: ${{ env.DEFAULT_PYTHON }}
|
||||||
- name: Restore Python virtual environment
|
- name: Restore Python virtual environment
|
||||||
@@ -68,9 +68,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -111,9 +111,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -154,7 +154,7 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Register hadolint problem matcher
|
- name: Register hadolint problem matcher
|
||||||
run: |
|
run: |
|
||||||
echo "::add-matcher::.github/workflows/matchers/hadolint.json"
|
echo "::add-matcher::.github/workflows/matchers/hadolint.json"
|
||||||
@@ -169,9 +169,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -213,9 +213,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -257,9 +257,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -293,9 +293,9 @@ jobs:
|
|||||||
needs: prepare
|
needs: prepare
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -339,9 +339,9 @@ jobs:
|
|||||||
name: Run tests Python ${{ needs.prepare.outputs.python-version }}
|
name: Run tests Python ${{ needs.prepare.outputs.python-version }}
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
@@ -398,9 +398,9 @@ jobs:
|
|||||||
needs: ["pytest", "prepare"]
|
needs: ["pytest", "prepare"]
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||||
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
|
||||||
id: python
|
id: python
|
||||||
with:
|
with:
|
||||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||||
|
|||||||
2
.github/workflows/release-drafter.yml
vendored
2
.github/workflows/release-drafter.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
|||||||
name: Release Drafter
|
name: Release Drafter
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the repository
|
- name: Checkout the repository
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
|
|||||||
2
.github/workflows/sentry.yaml
vendored
2
.github/workflows/sentry.yaml
vendored
@@ -10,7 +10,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code from GitHub
|
- name: Check out code from GitHub
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Sentry Release
|
- name: Sentry Release
|
||||||
uses: getsentry/action-release@128c5058bbbe93c8e02147fe0a9c713f166259a6 # v3.4.0
|
uses: getsentry/action-release@128c5058bbbe93c8e02147fe0a9c713f166259a6 # v3.4.0
|
||||||
env:
|
env:
|
||||||
|
|||||||
6
.github/workflows/update_frontend.yml
vendored
6
.github/workflows/update_frontend.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
latest_version: ${{ steps.latest_frontend_version.outputs.latest_tag }}
|
latest_version: ${{ steps.latest_frontend_version.outputs.latest_tag }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Get latest frontend release
|
- name: Get latest frontend release
|
||||||
id: latest_frontend_version
|
id: latest_frontend_version
|
||||||
uses: abatilo/release-info-action@32cb932219f1cee3fc4f4a298fd65ead5d35b661 # v1.3.3
|
uses: abatilo/release-info-action@32cb932219f1cee3fc4f4a298fd65ead5d35b661 # v1.3.3
|
||||||
@@ -49,7 +49,7 @@ jobs:
|
|||||||
if: needs.check-version.outputs.skip != 'true'
|
if: needs.check-version.outputs.skip != 'true'
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||||
- name: Clear www folder
|
- name: Clear www folder
|
||||||
run: |
|
run: |
|
||||||
rm -rf supervisor/api/panel/*
|
rm -rf supervisor/api/panel/*
|
||||||
@@ -68,7 +68,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
rm -f supervisor/api/panel/home_assistant_frontend_supervisor-*.tar.gz
|
rm -f supervisor/api/panel/home_assistant_frontend_supervisor-*.tar.gz
|
||||||
- name: Create PR
|
- name: Create PR
|
||||||
uses: peter-evans/create-pull-request@84ae59a2cdc2258d6fa0732dd66352dddae2a412 # v7.0.9
|
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
|
||||||
with:
|
with:
|
||||||
commit-message: "Update frontend to version ${{ needs.check-version.outputs.latest_version }}"
|
commit-message: "Update frontend to version ${{ needs.check-version.outputs.latest_version }}"
|
||||||
branch: autoupdate-frontend
|
branch: autoupdate-frontend
|
||||||
|
|||||||
10
build.yaml
10
build.yaml
@@ -1,7 +1,13 @@
|
|||||||
image: ghcr.io/home-assistant/{arch}-hassio-supervisor
|
image: ghcr.io/home-assistant/{arch}-hassio-supervisor
|
||||||
build_from:
|
build_from:
|
||||||
aarch64: ghcr.io/home-assistant/aarch64-base-python:3.13-alpine3.22-2025.11.1
|
aarch64: ghcr.io/home-assistant/aarch64-base-python:3.13-alpine3.22
|
||||||
amd64: ghcr.io/home-assistant/amd64-base-python:3.13-alpine3.22-2025.11.1
|
armhf: ghcr.io/home-assistant/armhf-base-python:3.13-alpine3.22
|
||||||
|
armv7: ghcr.io/home-assistant/armv7-base-python:3.13-alpine3.22
|
||||||
|
amd64: ghcr.io/home-assistant/amd64-base-python:3.13-alpine3.22
|
||||||
|
i386: ghcr.io/home-assistant/i386-base-python:3.13-alpine3.22
|
||||||
|
codenotary:
|
||||||
|
signer: notary@home-assistant.io
|
||||||
|
base_image: notary@home-assistant.io
|
||||||
cosign:
|
cosign:
|
||||||
base_identity: https://github.com/home-assistant/docker-base/.*
|
base_identity: https://github.com/home-assistant/docker-base/.*
|
||||||
identity: https://github.com/home-assistant/supervisor/.*
|
identity: https://github.com/home-assistant/supervisor/.*
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
aiodns==3.5.0
|
aiodns==3.5.0
|
||||||
aiodocker==0.24.0
|
|
||||||
aiohttp==3.13.2
|
aiohttp==3.13.2
|
||||||
atomicwrites-homeassistant==1.4.1
|
atomicwrites-homeassistant==1.4.1
|
||||||
attrs==25.4.0
|
attrs==25.4.0
|
||||||
awesomeversion==25.8.0
|
awesomeversion==25.8.0
|
||||||
backports.zstd==1.1.0
|
|
||||||
blockbuster==1.5.25
|
blockbuster==1.5.25
|
||||||
brotli==1.2.0
|
brotli==1.1.0
|
||||||
ciso8601==2.3.3
|
ciso8601==2.3.3
|
||||||
colorlog==6.10.1
|
colorlog==6.10.1
|
||||||
cpe==1.3.1
|
cpe==1.3.1
|
||||||
@@ -25,8 +23,8 @@ pyudev==0.24.4
|
|||||||
PyYAML==6.0.3
|
PyYAML==6.0.3
|
||||||
requests==2.32.5
|
requests==2.32.5
|
||||||
securetar==2025.2.1
|
securetar==2025.2.1
|
||||||
sentry-sdk==2.46.0
|
sentry-sdk==2.43.0
|
||||||
setuptools==80.9.0
|
setuptools==80.9.0
|
||||||
voluptuous==0.15.2
|
voluptuous==0.15.2
|
||||||
dbus-fast==3.1.2
|
dbus-fast==2.44.5
|
||||||
zlib-fast==0.2.1
|
zlib-fast==0.2.1
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
astroid==4.0.2
|
astroid==4.0.1
|
||||||
coverage==7.12.0
|
coverage==7.11.0
|
||||||
mypy==1.18.2
|
mypy==1.18.2
|
||||||
pre-commit==4.5.0
|
pre-commit==4.3.0
|
||||||
pylint==4.0.3
|
pylint==4.0.2
|
||||||
pytest-aiohttp==1.1.0
|
pytest-aiohttp==1.1.0
|
||||||
pytest-asyncio==1.3.0
|
pytest-asyncio==0.25.2
|
||||||
pytest-cov==7.0.0
|
pytest-cov==7.0.0
|
||||||
pytest-timeout==2.4.0
|
pytest-timeout==2.4.0
|
||||||
pytest==9.0.1
|
pytest==8.4.2
|
||||||
ruff==0.14.6
|
ruff==0.14.3
|
||||||
time-machine==3.1.0
|
time-machine==2.19.0
|
||||||
types-docker==7.1.0.20251127
|
types-docker==7.1.0.20251009
|
||||||
types-pyyaml==6.0.12.20250915
|
types-pyyaml==6.0.12.20250915
|
||||||
types-requests==2.32.4.20250913
|
types-requests==2.32.4.20250913
|
||||||
urllib3==2.5.0
|
urllib3==2.5.0
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import base64
|
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
import json
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
@@ -14,15 +12,12 @@ from ..const import (
|
|||||||
ATTR_ARGS,
|
ATTR_ARGS,
|
||||||
ATTR_BUILD_FROM,
|
ATTR_BUILD_FROM,
|
||||||
ATTR_LABELS,
|
ATTR_LABELS,
|
||||||
ATTR_PASSWORD,
|
|
||||||
ATTR_SQUASH,
|
ATTR_SQUASH,
|
||||||
ATTR_USERNAME,
|
|
||||||
FILE_SUFFIX_CONFIGURATION,
|
FILE_SUFFIX_CONFIGURATION,
|
||||||
META_ADDON,
|
META_ADDON,
|
||||||
SOCKET_DOCKER,
|
SOCKET_DOCKER,
|
||||||
)
|
)
|
||||||
from ..coresys import CoreSys, CoreSysAttributes
|
from ..coresys import CoreSys, CoreSysAttributes
|
||||||
from ..docker.const import DOCKER_HUB, DOCKER_HUB_LEGACY
|
|
||||||
from ..docker.interface import MAP_ARCH
|
from ..docker.interface import MAP_ARCH
|
||||||
from ..exceptions import ConfigurationFileError, HassioArchNotFound
|
from ..exceptions import ConfigurationFileError, HassioArchNotFound
|
||||||
from ..utils.common import FileConfiguration, find_one_filetype
|
from ..utils.common import FileConfiguration, find_one_filetype
|
||||||
@@ -127,46 +122,8 @@ class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|||||||
except HassioArchNotFound:
|
except HassioArchNotFound:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_docker_config_json(self) -> str | None:
|
|
||||||
"""Generate Docker config.json content with registry credentials for base image.
|
|
||||||
|
|
||||||
Returns a JSON string with registry credentials for the base image's registry,
|
|
||||||
or None if no matching registry is configured.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
HassioArchNotFound: If the add-on is not supported on the current architecture.
|
|
||||||
|
|
||||||
"""
|
|
||||||
# Early return before accessing base_image to avoid unnecessary arch lookup
|
|
||||||
if not self.sys_docker.config.registries:
|
|
||||||
return None
|
|
||||||
|
|
||||||
registry = self.sys_docker.config.get_registry_for_image(self.base_image)
|
|
||||||
if not registry:
|
|
||||||
return None
|
|
||||||
|
|
||||||
stored = self.sys_docker.config.registries[registry]
|
|
||||||
username = stored[ATTR_USERNAME]
|
|
||||||
password = stored[ATTR_PASSWORD]
|
|
||||||
|
|
||||||
# Docker config.json uses base64-encoded "username:password" for auth
|
|
||||||
auth_string = base64.b64encode(f"{username}:{password}".encode()).decode()
|
|
||||||
|
|
||||||
# Use the actual registry URL for the key
|
|
||||||
# Docker Hub uses "https://index.docker.io/v1/" as the key
|
|
||||||
# Support both docker.io (official) and hub.docker.com (legacy)
|
|
||||||
registry_key = (
|
|
||||||
"https://index.docker.io/v1/"
|
|
||||||
if registry in (DOCKER_HUB, DOCKER_HUB_LEGACY)
|
|
||||||
else registry
|
|
||||||
)
|
|
||||||
|
|
||||||
config = {"auths": {registry_key: {"auth": auth_string}}}
|
|
||||||
|
|
||||||
return json.dumps(config)
|
|
||||||
|
|
||||||
def get_docker_args(
|
def get_docker_args(
|
||||||
self, version: AwesomeVersion, image_tag: str, docker_config_path: Path | None
|
self, version: AwesomeVersion, image_tag: str
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Create a dict with Docker run args."""
|
"""Create a dict with Docker run args."""
|
||||||
dockerfile_path = self.get_dockerfile().relative_to(self.addon.path_location)
|
dockerfile_path = self.get_dockerfile().relative_to(self.addon.path_location)
|
||||||
@@ -215,24 +172,12 @@ class AddonBuild(FileConfiguration, CoreSysAttributes):
|
|||||||
self.addon.path_location
|
self.addon.path_location
|
||||||
)
|
)
|
||||||
|
|
||||||
volumes = {
|
|
||||||
SOCKET_DOCKER: {"bind": "/var/run/docker.sock", "mode": "rw"},
|
|
||||||
addon_extern_path: {"bind": "/addon", "mode": "ro"},
|
|
||||||
}
|
|
||||||
|
|
||||||
# Mount Docker config with registry credentials if available
|
|
||||||
if docker_config_path:
|
|
||||||
docker_config_extern_path = self.sys_config.local_to_extern_path(
|
|
||||||
docker_config_path
|
|
||||||
)
|
|
||||||
volumes[docker_config_extern_path] = {
|
|
||||||
"bind": "/root/.docker/config.json",
|
|
||||||
"mode": "ro",
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"command": build_cmd,
|
"command": build_cmd,
|
||||||
"volumes": volumes,
|
"volumes": {
|
||||||
|
SOCKET_DOCKER: {"bind": "/var/run/docker.sock", "mode": "rw"},
|
||||||
|
addon_extern_path: {"bind": "/addon", "mode": "ro"},
|
||||||
|
},
|
||||||
"working_dir": "/addon",
|
"working_dir": "/addon",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -152,7 +152,6 @@ class RestAPI(CoreSysAttributes):
|
|||||||
self._api_host.advanced_logs,
|
self._api_host.advanced_logs,
|
||||||
identifier=syslog_identifier,
|
identifier=syslog_identifier,
|
||||||
latest=True,
|
latest=True,
|
||||||
no_colors=True,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
web.get(
|
web.get(
|
||||||
@@ -450,7 +449,6 @@ class RestAPI(CoreSysAttributes):
|
|||||||
await async_capture_exception(err)
|
await async_capture_exception(err)
|
||||||
kwargs.pop("follow", None) # Follow is not supported for Docker logs
|
kwargs.pop("follow", None) # Follow is not supported for Docker logs
|
||||||
kwargs.pop("latest", None) # Latest is not supported for Docker logs
|
kwargs.pop("latest", None) # Latest is not supported for Docker logs
|
||||||
kwargs.pop("no_colors", None) # no_colors not supported for Docker logs
|
|
||||||
return await api_supervisor.logs(*args, **kwargs)
|
return await api_supervisor.logs(*args, **kwargs)
|
||||||
|
|
||||||
self.webapp.add_routes(
|
self.webapp.add_routes(
|
||||||
@@ -462,7 +460,7 @@ class RestAPI(CoreSysAttributes):
|
|||||||
),
|
),
|
||||||
web.get(
|
web.get(
|
||||||
"/supervisor/logs/latest",
|
"/supervisor/logs/latest",
|
||||||
partial(get_supervisor_logs, latest=True, no_colors=True),
|
partial(get_supervisor_logs, latest=True),
|
||||||
),
|
),
|
||||||
web.get("/supervisor/logs/boots/{bootid}", get_supervisor_logs),
|
web.get("/supervisor/logs/boots/{bootid}", get_supervisor_logs),
|
||||||
web.get(
|
web.get(
|
||||||
@@ -578,7 +576,7 @@ class RestAPI(CoreSysAttributes):
|
|||||||
),
|
),
|
||||||
web.get(
|
web.get(
|
||||||
"/addons/{addon}/logs/latest",
|
"/addons/{addon}/logs/latest",
|
||||||
partial(get_addon_logs, latest=True, no_colors=True),
|
partial(get_addon_logs, latest=True),
|
||||||
),
|
),
|
||||||
web.get("/addons/{addon}/logs/boots/{bootid}", get_addon_logs),
|
web.get("/addons/{addon}/logs/boots/{bootid}", get_addon_logs),
|
||||||
web.get(
|
web.get(
|
||||||
|
|||||||
@@ -206,7 +206,6 @@ class APIHost(CoreSysAttributes):
|
|||||||
identifier: str | None = None,
|
identifier: str | None = None,
|
||||||
follow: bool = False,
|
follow: bool = False,
|
||||||
latest: bool = False,
|
latest: bool = False,
|
||||||
no_colors: bool = False,
|
|
||||||
) -> web.StreamResponse:
|
) -> web.StreamResponse:
|
||||||
"""Return systemd-journald logs."""
|
"""Return systemd-journald logs."""
|
||||||
log_formatter = LogFormatter.PLAIN
|
log_formatter = LogFormatter.PLAIN
|
||||||
@@ -252,9 +251,6 @@ class APIHost(CoreSysAttributes):
|
|||||||
if "verbose" in request.query or request.headers[ACCEPT] == CONTENT_TYPE_X_LOG:
|
if "verbose" in request.query or request.headers[ACCEPT] == CONTENT_TYPE_X_LOG:
|
||||||
log_formatter = LogFormatter.VERBOSE
|
log_formatter = LogFormatter.VERBOSE
|
||||||
|
|
||||||
if "no_colors" in request.query:
|
|
||||||
no_colors = True
|
|
||||||
|
|
||||||
if "lines" in request.query:
|
if "lines" in request.query:
|
||||||
lines = request.query.get("lines", DEFAULT_LINES)
|
lines = request.query.get("lines", DEFAULT_LINES)
|
||||||
try:
|
try:
|
||||||
@@ -284,9 +280,7 @@ class APIHost(CoreSysAttributes):
|
|||||||
response = web.StreamResponse()
|
response = web.StreamResponse()
|
||||||
response.content_type = CONTENT_TYPE_TEXT
|
response.content_type = CONTENT_TYPE_TEXT
|
||||||
headers_returned = False
|
headers_returned = False
|
||||||
async for cursor, line in journal_logs_reader(
|
async for cursor, line in journal_logs_reader(resp, log_formatter):
|
||||||
resp, log_formatter, no_colors
|
|
||||||
):
|
|
||||||
try:
|
try:
|
||||||
if not headers_returned:
|
if not headers_returned:
|
||||||
if cursor:
|
if cursor:
|
||||||
@@ -324,12 +318,9 @@ class APIHost(CoreSysAttributes):
|
|||||||
identifier: str | None = None,
|
identifier: str | None = None,
|
||||||
follow: bool = False,
|
follow: bool = False,
|
||||||
latest: bool = False,
|
latest: bool = False,
|
||||||
no_colors: bool = False,
|
|
||||||
) -> web.StreamResponse:
|
) -> web.StreamResponse:
|
||||||
"""Return systemd-journald logs. Wrapped as standard API handler."""
|
"""Return systemd-journald logs. Wrapped as standard API handler."""
|
||||||
return await self.advanced_logs_handler(
|
return await self.advanced_logs_handler(request, identifier, follow, latest)
|
||||||
request, identifier, follow, latest, no_colors
|
|
||||||
)
|
|
||||||
|
|
||||||
@api_process
|
@api_process
|
||||||
async def disk_usage(self, request: web.Request) -> dict:
|
async def disk_usage(self, request: web.Request) -> dict:
|
||||||
@@ -343,14 +334,10 @@ class APIHost(CoreSysAttributes):
|
|||||||
|
|
||||||
disk = self.sys_hardware.disk
|
disk = self.sys_hardware.disk
|
||||||
|
|
||||||
total, _, free = await self.sys_run_in_executor(
|
total, used, _ = await self.sys_run_in_executor(
|
||||||
disk.disk_usage, self.sys_config.path_supervisor
|
disk.disk_usage, self.sys_config.path_supervisor
|
||||||
)
|
)
|
||||||
|
|
||||||
# Calculate used by subtracting free makes sure we include reserved space
|
|
||||||
# in used space reporting.
|
|
||||||
used = total - free
|
|
||||||
|
|
||||||
known_paths = await self.sys_run_in_executor(
|
known_paths = await self.sys_run_in_executor(
|
||||||
disk.get_dir_sizes,
|
disk.get_dir_sizes,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -253,28 +253,18 @@ class APIIngress(CoreSysAttributes):
|
|||||||
skip_auto_headers={hdrs.CONTENT_TYPE},
|
skip_auto_headers={hdrs.CONTENT_TYPE},
|
||||||
) as result:
|
) as result:
|
||||||
headers = _response_header(result)
|
headers = _response_header(result)
|
||||||
|
|
||||||
# Avoid parsing content_type in simple cases for better performance
|
# Avoid parsing content_type in simple cases for better performance
|
||||||
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
|
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
|
||||||
content_type = (maybe_content_type.partition(";"))[0].strip()
|
content_type = (maybe_content_type.partition(";"))[0].strip()
|
||||||
else:
|
else:
|
||||||
content_type = result.content_type
|
content_type = result.content_type
|
||||||
|
|
||||||
# Empty body responses (304, 204, HEAD, etc.) should not be streamed,
|
|
||||||
# otherwise aiohttp < 3.9.0 may generate an invalid "0\r\n\r\n" chunk
|
|
||||||
# This also avoids setting content_type for empty responses.
|
|
||||||
if must_be_empty_body(request.method, result.status):
|
|
||||||
# If upstream contains content-type, preserve it (e.g. for HEAD requests)
|
|
||||||
if maybe_content_type:
|
|
||||||
headers[hdrs.CONTENT_TYPE] = content_type
|
|
||||||
return web.Response(
|
|
||||||
headers=headers,
|
|
||||||
status=result.status,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Simple request
|
# Simple request
|
||||||
if (
|
if (
|
||||||
hdrs.CONTENT_LENGTH in result.headers
|
# empty body responses should not be streamed,
|
||||||
|
# otherwise aiohttp < 3.9.0 may generate
|
||||||
|
# an invalid "0\r\n\r\n" chunk instead of an empty response.
|
||||||
|
must_be_empty_body(request.method, result.status)
|
||||||
|
or hdrs.CONTENT_LENGTH in result.headers
|
||||||
and int(result.headers.get(hdrs.CONTENT_LENGTH, 0)) < 4_194_000
|
and int(result.headers.get(hdrs.CONTENT_LENGTH, 0)) < 4_194_000
|
||||||
):
|
):
|
||||||
# Return Response
|
# Return Response
|
||||||
|
|||||||
@@ -63,10 +63,12 @@ def json_loads(data: Any) -> dict[str, Any]:
|
|||||||
def api_process(method):
|
def api_process(method):
|
||||||
"""Wrap function with true/false calls to rest api."""
|
"""Wrap function with true/false calls to rest api."""
|
||||||
|
|
||||||
async def wrap_api(*args, **kwargs) -> web.Response | web.StreamResponse:
|
async def wrap_api(
|
||||||
|
api: CoreSysAttributes, *args, **kwargs
|
||||||
|
) -> web.Response | web.StreamResponse:
|
||||||
"""Return API information."""
|
"""Return API information."""
|
||||||
try:
|
try:
|
||||||
answer = await method(*args, **kwargs)
|
answer = await method(api, *args, **kwargs)
|
||||||
except BackupFileNotFoundError as err:
|
except BackupFileNotFoundError as err:
|
||||||
return api_return_error(err, status=404)
|
return api_return_error(err, status=404)
|
||||||
except APIError as err:
|
except APIError as err:
|
||||||
@@ -107,10 +109,12 @@ def api_process_raw(content, *, error_type=None):
|
|||||||
def wrap_method(method):
|
def wrap_method(method):
|
||||||
"""Wrap function with raw output to rest api."""
|
"""Wrap function with raw output to rest api."""
|
||||||
|
|
||||||
async def wrap_api(*args, **kwargs) -> web.Response | web.StreamResponse:
|
async def wrap_api(
|
||||||
|
api: CoreSysAttributes, *args, **kwargs
|
||||||
|
) -> web.Response | web.StreamResponse:
|
||||||
"""Return api information."""
|
"""Return api information."""
|
||||||
try:
|
try:
|
||||||
msg_data = await method(*args, **kwargs)
|
msg_data = await method(api, *args, **kwargs)
|
||||||
except APIError as err:
|
except APIError as err:
|
||||||
return api_return_error(
|
return api_return_error(
|
||||||
err,
|
err,
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from asyncio import Task
|
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@@ -39,13 +38,11 @@ class Bus(CoreSysAttributes):
|
|||||||
self._listeners.setdefault(event, []).append(listener)
|
self._listeners.setdefault(event, []).append(listener)
|
||||||
return listener
|
return listener
|
||||||
|
|
||||||
def fire_event(self, event: BusEvent, reference: Any) -> list[Task]:
|
def fire_event(self, event: BusEvent, reference: Any) -> None:
|
||||||
"""Fire an event to the bus."""
|
"""Fire an event to the bus."""
|
||||||
_LOGGER.debug("Fire event '%s' with '%s'", event, reference)
|
_LOGGER.debug("Fire event '%s' with '%s'", event, reference)
|
||||||
tasks: list[Task] = []
|
|
||||||
for listener in self._listeners.get(event, []):
|
for listener in self._listeners.get(event, []):
|
||||||
tasks.append(self.sys_create_task(listener.callback(reference)))
|
self.sys_create_task(listener.callback(reference))
|
||||||
return tasks
|
|
||||||
|
|
||||||
def remove_listener(self, listener: EventListener) -> None:
|
def remove_listener(self, listener: EventListener) -> None:
|
||||||
"""Unregister an listener."""
|
"""Unregister an listener."""
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ from datetime import UTC, datetime, tzinfo
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import TYPE_CHECKING, Any, Self, TypeVar
|
from typing import TYPE_CHECKING, Any, Self, TypeVar
|
||||||
|
|
||||||
@@ -656,14 +655,8 @@ class CoreSys:
|
|||||||
if kwargs:
|
if kwargs:
|
||||||
funct = partial(funct, **kwargs)
|
funct = partial(funct, **kwargs)
|
||||||
|
|
||||||
# Convert datetime to event loop time base
|
|
||||||
# If datetime is in the past, delay will be negative and call_at will
|
|
||||||
# schedule the call as soon as possible.
|
|
||||||
delay = when.timestamp() - time.time()
|
|
||||||
loop_time = self.loop.time() + delay
|
|
||||||
|
|
||||||
return self.loop.call_at(
|
return self.loop.call_at(
|
||||||
loop_time, funct, *args, context=self._create_context()
|
when.timestamp(), funct, *args, context=self._create_context()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -306,8 +306,6 @@ class DeviceType(IntEnum):
|
|||||||
VLAN = 11
|
VLAN = 11
|
||||||
TUN = 16
|
TUN = 16
|
||||||
VETH = 20
|
VETH = 20
|
||||||
WIREGUARD = 29
|
|
||||||
LOOPBACK = 32
|
|
||||||
|
|
||||||
|
|
||||||
class WirelessMethodType(IntEnum):
|
class WirelessMethodType(IntEnum):
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class DBusManager(CoreSysAttributes):
|
|||||||
|
|
||||||
async def load(self) -> None:
|
async def load(self) -> None:
|
||||||
"""Connect interfaces to D-Bus."""
|
"""Connect interfaces to D-Bus."""
|
||||||
if not await self.sys_run_in_executor(SOCKET_DBUS.exists):
|
if not SOCKET_DBUS.exists():
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"No D-Bus support on Host. Disabled any kind of host control!"
|
"No D-Bus support on Host. Disabled any kind of host control!"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -134,10 +134,9 @@ class NetworkManager(DBusInterfaceProxy):
|
|||||||
async def check_connectivity(self, *, force: bool = False) -> ConnectivityState:
|
async def check_connectivity(self, *, force: bool = False) -> ConnectivityState:
|
||||||
"""Check the connectivity of the host."""
|
"""Check the connectivity of the host."""
|
||||||
if force:
|
if force:
|
||||||
return ConnectivityState(
|
return await self.connected_dbus.call("check_connectivity")
|
||||||
await self.connected_dbus.call("check_connectivity")
|
else:
|
||||||
)
|
return await self.connected_dbus.get("connectivity")
|
||||||
return ConnectivityState(await self.connected_dbus.get("connectivity"))
|
|
||||||
|
|
||||||
async def connect(self, bus: MessageBus) -> None:
|
async def connect(self, bus: MessageBus) -> None:
|
||||||
"""Connect to system's D-Bus."""
|
"""Connect to system's D-Bus."""
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class NetworkConnection(DBusInterfaceProxy):
|
|||||||
@dbus_property
|
@dbus_property
|
||||||
def state(self) -> ConnectionStateType:
|
def state(self) -> ConnectionStateType:
|
||||||
"""Return the state of the connection."""
|
"""Return the state of the connection."""
|
||||||
return ConnectionStateType(self.properties[DBUS_ATTR_STATE])
|
return self.properties[DBUS_ATTR_STATE]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_flags(self) -> set[ConnectionStateFlags]:
|
def state_flags(self) -> set[ConnectionStateFlags]:
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""NetworkInterface object for Network Manager."""
|
"""NetworkInterface object for Network Manager."""
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from dbus_fast.aio.message_bus import MessageBus
|
from dbus_fast.aio.message_bus import MessageBus
|
||||||
@@ -24,8 +23,6 @@ from .connection import NetworkConnection
|
|||||||
from .setting import NetworkSetting
|
from .setting import NetworkSetting
|
||||||
from .wireless import NetworkWireless
|
from .wireless import NetworkWireless
|
||||||
|
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class NetworkInterface(DBusInterfaceProxy):
|
class NetworkInterface(DBusInterfaceProxy):
|
||||||
"""NetworkInterface object represents Network Manager Device objects.
|
"""NetworkInterface object represents Network Manager Device objects.
|
||||||
@@ -60,15 +57,7 @@ class NetworkInterface(DBusInterfaceProxy):
|
|||||||
@dbus_property
|
@dbus_property
|
||||||
def type(self) -> DeviceType:
|
def type(self) -> DeviceType:
|
||||||
"""Return interface type."""
|
"""Return interface type."""
|
||||||
try:
|
return self.properties[DBUS_ATTR_DEVICE_TYPE]
|
||||||
return DeviceType(self.properties[DBUS_ATTR_DEVICE_TYPE])
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.debug(
|
|
||||||
"Unknown device type %s for %s, treating as UNKNOWN",
|
|
||||||
self.properties[DBUS_ATTR_DEVICE_TYPE],
|
|
||||||
self.object_path,
|
|
||||||
)
|
|
||||||
return DeviceType.UNKNOWN
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@dbus_property
|
@dbus_property
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ class Resolved(DBusInterfaceProxy):
|
|||||||
@dbus_property
|
@dbus_property
|
||||||
def current_dns_server(
|
def current_dns_server(
|
||||||
self,
|
self,
|
||||||
) -> tuple[int, DNSAddressFamily, bytes] | None:
|
) -> list[tuple[int, DNSAddressFamily, bytes]] | None:
|
||||||
"""Return current DNS server."""
|
"""Return current DNS server."""
|
||||||
return self.properties[DBUS_ATTR_CURRENT_DNS_SERVER]
|
return self.properties[DBUS_ATTR_CURRENT_DNS_SERVER]
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ class Resolved(DBusInterfaceProxy):
|
|||||||
@dbus_property
|
@dbus_property
|
||||||
def current_dns_server_ex(
|
def current_dns_server_ex(
|
||||||
self,
|
self,
|
||||||
) -> tuple[int, DNSAddressFamily, bytes, int, str] | None:
|
) -> list[tuple[int, DNSAddressFamily, bytes, int, str]] | None:
|
||||||
"""Return current DNS server including port and server name."""
|
"""Return current DNS server including port and server name."""
|
||||||
return self.properties[DBUS_ATTR_CURRENT_DNS_SERVER_EX]
|
return self.properties[DBUS_ATTR_CURRENT_DNS_SERVER_EX]
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ class SystemdUnit(DBusInterface):
|
|||||||
@dbus_connected
|
@dbus_connected
|
||||||
async def get_active_state(self) -> UnitActiveState:
|
async def get_active_state(self) -> UnitActiveState:
|
||||||
"""Get active state of the unit."""
|
"""Get active state of the unit."""
|
||||||
return UnitActiveState(await self.connected_dbus.Unit.get("active_state"))
|
return await self.connected_dbus.Unit.get("active_state")
|
||||||
|
|
||||||
@dbus_connected
|
@dbus_connected
|
||||||
def properties_changed(self) -> DBusSignalWrapper:
|
def properties_changed(self) -> DBusSignalWrapper:
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from dbus_fast import Variant
|
|||||||
from .const import EncryptType, EraseMode
|
from .const import EncryptType, EraseMode
|
||||||
|
|
||||||
|
|
||||||
def udisks2_bytes_to_path(path_bytes: bytes) -> Path:
|
def udisks2_bytes_to_path(path_bytes: bytearray) -> Path:
|
||||||
"""Convert bytes to path object without null character on end."""
|
"""Convert bytes to path object without null character on end."""
|
||||||
if path_bytes and path_bytes[-1] == 0:
|
if path_bytes and path_bytes[-1] == 0:
|
||||||
return Path(path_bytes[:-1].decode())
|
return Path(path_bytes[:-1].decode())
|
||||||
@@ -73,7 +73,7 @@ FormatOptionsDataType = TypedDict(
|
|||||||
{
|
{
|
||||||
"label": NotRequired[str],
|
"label": NotRequired[str],
|
||||||
"take-ownership": NotRequired[bool],
|
"take-ownership": NotRequired[bool],
|
||||||
"encrypt.passphrase": NotRequired[bytes],
|
"encrypt.passphrase": NotRequired[bytearray],
|
||||||
"encrypt.type": NotRequired[str],
|
"encrypt.type": NotRequired[str],
|
||||||
"erase": NotRequired[str],
|
"erase": NotRequired[str],
|
||||||
"update-partition-type": NotRequired[bool],
|
"update-partition-type": NotRequired[bool],
|
||||||
|
|||||||
@@ -7,10 +7,8 @@ from ipaddress import IPv4Address
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import tempfile
|
|
||||||
from typing import TYPE_CHECKING, cast
|
from typing import TYPE_CHECKING, cast
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from attr import evolve
|
from attr import evolve
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
import docker
|
import docker
|
||||||
@@ -706,38 +704,12 @@ class DockerAddon(DockerInterface):
|
|||||||
with suppress(docker.errors.NotFound):
|
with suppress(docker.errors.NotFound):
|
||||||
self.sys_docker.containers.get(builder_name).remove(force=True, v=True)
|
self.sys_docker.containers.get(builder_name).remove(force=True, v=True)
|
||||||
|
|
||||||
# Generate Docker config with registry credentials for base image if needed
|
result = self.sys_docker.run_command(
|
||||||
docker_config_path: Path | None = None
|
ADDON_BUILDER_IMAGE,
|
||||||
docker_config_content = build_env.get_docker_config_json()
|
version=builder_version_tag,
|
||||||
temp_dir: tempfile.TemporaryDirectory | None = None
|
name=builder_name,
|
||||||
|
**build_env.get_docker_args(version, addon_image_tag),
|
||||||
try:
|
)
|
||||||
if docker_config_content:
|
|
||||||
# Create temporary directory for docker config
|
|
||||||
temp_dir = tempfile.TemporaryDirectory(
|
|
||||||
prefix="hassio_build_", dir=self.sys_config.path_tmp
|
|
||||||
)
|
|
||||||
docker_config_path = Path(temp_dir.name) / "config.json"
|
|
||||||
docker_config_path.write_text(
|
|
||||||
docker_config_content, encoding="utf-8"
|
|
||||||
)
|
|
||||||
_LOGGER.debug(
|
|
||||||
"Created temporary Docker config for build at %s",
|
|
||||||
docker_config_path,
|
|
||||||
)
|
|
||||||
|
|
||||||
result = self.sys_docker.run_command(
|
|
||||||
ADDON_BUILDER_IMAGE,
|
|
||||||
version=builder_version_tag,
|
|
||||||
name=builder_name,
|
|
||||||
**build_env.get_docker_args(
|
|
||||||
version, addon_image_tag, docker_config_path
|
|
||||||
),
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
# Clean up temporary directory
|
|
||||||
if temp_dir:
|
|
||||||
temp_dir.cleanup()
|
|
||||||
|
|
||||||
logs = result.output.decode("utf-8")
|
logs = result.output.decode("utf-8")
|
||||||
|
|
||||||
@@ -745,21 +717,19 @@ class DockerAddon(DockerInterface):
|
|||||||
error_message = f"Docker build failed for {addon_image_tag} (exit code {result.exit_code}). Build output:\n{logs}"
|
error_message = f"Docker build failed for {addon_image_tag} (exit code {result.exit_code}). Build output:\n{logs}"
|
||||||
raise docker.errors.DockerException(error_message)
|
raise docker.errors.DockerException(error_message)
|
||||||
|
|
||||||
return addon_image_tag, logs
|
addon_image = self.sys_docker.images.get(addon_image_tag)
|
||||||
|
|
||||||
|
return addon_image, logs
|
||||||
|
|
||||||
try:
|
try:
|
||||||
addon_image_tag, log = await self.sys_run_in_executor(build_image)
|
docker_image, log = await self.sys_run_in_executor(build_image)
|
||||||
|
|
||||||
_LOGGER.debug("Build %s:%s done: %s", self.image, version, log)
|
_LOGGER.debug("Build %s:%s done: %s", self.image, version, log)
|
||||||
|
|
||||||
# Update meta data
|
# Update meta data
|
||||||
self._meta = await self.sys_docker.images.inspect(addon_image_tag)
|
self._meta = docker_image.attrs
|
||||||
|
|
||||||
except (
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
docker.errors.DockerException,
|
|
||||||
requests.RequestException,
|
|
||||||
aiodocker.DockerError,
|
|
||||||
) as err:
|
|
||||||
_LOGGER.error("Can't build %s:%s: %s", self.image, version, err)
|
_LOGGER.error("Can't build %s:%s: %s", self.image, version, err)
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
@@ -781,8 +751,11 @@ class DockerAddon(DockerInterface):
|
|||||||
)
|
)
|
||||||
async def import_image(self, tar_file: Path) -> None:
|
async def import_image(self, tar_file: Path) -> None:
|
||||||
"""Import a tar file as image."""
|
"""Import a tar file as image."""
|
||||||
if docker_image := await self.sys_docker.import_image(tar_file):
|
docker_image = await self.sys_run_in_executor(
|
||||||
self._meta = docker_image
|
self.sys_docker.import_image, tar_file
|
||||||
|
)
|
||||||
|
if docker_image:
|
||||||
|
self._meta = docker_image.attrs
|
||||||
_LOGGER.info("Importing image %s and version %s", tar_file, self.version)
|
_LOGGER.info("Importing image %s and version %s", tar_file, self.version)
|
||||||
|
|
||||||
with suppress(DockerError):
|
with suppress(DockerError):
|
||||||
@@ -796,21 +769,17 @@ class DockerAddon(DockerInterface):
|
|||||||
version: AwesomeVersion | None = None,
|
version: AwesomeVersion | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Check if old version exists and cleanup other versions of image not in use."""
|
"""Check if old version exists and cleanup other versions of image not in use."""
|
||||||
if not (use_image := image or self.image):
|
await self.sys_run_in_executor(
|
||||||
raise DockerError("Cannot determine image from metadata!", _LOGGER.error)
|
self.sys_docker.cleanup_old_images,
|
||||||
if not (use_version := version or self.version):
|
(image := image or self.image),
|
||||||
raise DockerError("Cannot determine version from metadata!", _LOGGER.error)
|
version or self.version,
|
||||||
|
|
||||||
await self.sys_docker.cleanup_old_images(
|
|
||||||
use_image,
|
|
||||||
use_version,
|
|
||||||
{old_image} if old_image else None,
|
{old_image} if old_image else None,
|
||||||
keep_images={
|
keep_images={
|
||||||
f"{addon.image}:{addon.version}"
|
f"{addon.image}:{addon.version}"
|
||||||
for addon in self.sys_addons.installed
|
for addon in self.sys_addons.installed
|
||||||
if addon.slug != self.addon.slug
|
if addon.slug != self.addon.slug
|
||||||
and addon.image
|
and addon.image
|
||||||
and addon.image in {old_image, use_image}
|
and addon.image in {old_image, image}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -15,65 +15,6 @@ from ..const import MACHINE_ID
|
|||||||
|
|
||||||
RE_RETRYING_DOWNLOAD_STATUS = re.compile(r"Retrying in \d+ seconds?")
|
RE_RETRYING_DOWNLOAD_STATUS = re.compile(r"Retrying in \d+ seconds?")
|
||||||
|
|
||||||
# Docker Hub registry identifier (official default)
|
|
||||||
# Docker's default registry is docker.io
|
|
||||||
DOCKER_HUB = "docker.io"
|
|
||||||
|
|
||||||
# Legacy Docker Hub identifier for backward compatibility
|
|
||||||
DOCKER_HUB_LEGACY = "hub.docker.com"
|
|
||||||
|
|
||||||
# Docker image reference domain regex
|
|
||||||
# Based on Docker's reference implementation:
|
|
||||||
# vendor/github.com/distribution/reference/normalize.go
|
|
||||||
#
|
|
||||||
# A domain is detected if the part before the first / contains:
|
|
||||||
# - "localhost" (with optional port)
|
|
||||||
# - Contains "." (like registry.example.com or 127.0.0.1)
|
|
||||||
# - Contains ":" (like myregistry:5000)
|
|
||||||
# - IPv6 addresses in brackets (like [::1]:5000)
|
|
||||||
#
|
|
||||||
# Note: Docker also treats uppercase letters as domain indicators since
|
|
||||||
# namespaces must be lowercase, but this regex handles lowercase matching
|
|
||||||
# and the get_domain() function validates the domain rules.
|
|
||||||
IMAGE_DOMAIN_REGEX = re.compile(
|
|
||||||
r"^("
|
|
||||||
r"localhost(?::[0-9]+)?|" # localhost with optional port
|
|
||||||
r"(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])" # domain component
|
|
||||||
r"(?:\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))*" # more components
|
|
||||||
r"(?::[0-9]+)?|" # optional port
|
|
||||||
r"\[[a-fA-F0-9:]+\](?::[0-9]+)?" # IPv6 with optional port
|
|
||||||
r")/" # must be followed by /
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_domain(image_ref: str) -> str | None:
|
|
||||||
"""Extract domain from Docker image reference.
|
|
||||||
|
|
||||||
Returns the registry domain if the image reference contains one,
|
|
||||||
or None if the image uses Docker Hub (docker.io).
|
|
||||||
|
|
||||||
Based on Docker's reference implementation:
|
|
||||||
vendor/github.com/distribution/reference/normalize.go
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
get_domain("nginx") -> None (docker.io)
|
|
||||||
get_domain("library/nginx") -> None (docker.io)
|
|
||||||
get_domain("myregistry.com/nginx") -> "myregistry.com"
|
|
||||||
get_domain("localhost/myimage") -> "localhost"
|
|
||||||
get_domain("localhost:5000/myimage") -> "localhost:5000"
|
|
||||||
get_domain("registry.io:5000/org/app:v1") -> "registry.io:5000"
|
|
||||||
get_domain("[::1]:5000/myimage") -> "[::1]:5000"
|
|
||||||
|
|
||||||
"""
|
|
||||||
match = IMAGE_DOMAIN_REGEX.match(image_ref)
|
|
||||||
if match:
|
|
||||||
domain = match.group(1)
|
|
||||||
# Must contain '.' or ':' or be 'localhost' to be a real domain
|
|
||||||
# This prevents treating "myuser/myimage" as having domain "myuser"
|
|
||||||
if "." in domain or ":" in domain or domain == "localhost":
|
|
||||||
return domain
|
|
||||||
return None # No domain = Docker Hub (docker.io)
|
|
||||||
|
|
||||||
|
|
||||||
class Capabilities(StrEnum):
|
class Capabilities(StrEnum):
|
||||||
"""Linux Capabilities."""
|
"""Linux Capabilities."""
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
"""Init file for Supervisor Docker object."""
|
"""Init file for Supervisor Docker object."""
|
||||||
|
|
||||||
|
from collections.abc import Awaitable
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
@@ -235,10 +236,11 @@ class DockerHomeAssistant(DockerInterface):
|
|||||||
environment={ENV_TIME: self.sys_timezone},
|
environment={ENV_TIME: self.sys_timezone},
|
||||||
)
|
)
|
||||||
|
|
||||||
async def is_initialize(self) -> bool:
|
def is_initialize(self) -> Awaitable[bool]:
|
||||||
"""Return True if Docker container exists."""
|
"""Return True if Docker container exists."""
|
||||||
if not self.sys_homeassistant.version:
|
return self.sys_run_in_executor(
|
||||||
return False
|
self.sys_docker.container_is_initialized,
|
||||||
return await self.sys_docker.container_is_initialized(
|
self.name,
|
||||||
self.name, self.image, self.sys_homeassistant.version
|
self.image,
|
||||||
|
self.sys_homeassistant.version,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ from abc import ABC, abstractmethod
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from collections.abc import Awaitable
|
from collections.abc import Awaitable
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from http import HTTPStatus
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from time import time
|
from time import time
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
from awesomeversion.strategy import AwesomeVersionStrategy
|
from awesomeversion.strategy import AwesomeVersionStrategy
|
||||||
import docker
|
import docker
|
||||||
from docker.models.containers import Container
|
from docker.models.containers import Container
|
||||||
|
from docker.models.images import Image
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from ..bus import EventListener
|
from ..bus import EventListener
|
||||||
@@ -33,7 +33,6 @@ from ..coresys import CoreSys
|
|||||||
from ..exceptions import (
|
from ..exceptions import (
|
||||||
DockerAPIError,
|
DockerAPIError,
|
||||||
DockerError,
|
DockerError,
|
||||||
DockerHubRateLimitExceeded,
|
|
||||||
DockerJobError,
|
DockerJobError,
|
||||||
DockerLogOutOfOrder,
|
DockerLogOutOfOrder,
|
||||||
DockerNotFound,
|
DockerNotFound,
|
||||||
@@ -45,19 +44,16 @@ from ..jobs.decorator import Job
|
|||||||
from ..jobs.job_group import JobGroup
|
from ..jobs.job_group import JobGroup
|
||||||
from ..resolution.const import ContextType, IssueType, SuggestionType
|
from ..resolution.const import ContextType, IssueType, SuggestionType
|
||||||
from ..utils.sentry import async_capture_exception
|
from ..utils.sentry import async_capture_exception
|
||||||
from .const import (
|
from .const import ContainerState, PullImageLayerStage, RestartPolicy
|
||||||
DOCKER_HUB,
|
|
||||||
DOCKER_HUB_LEGACY,
|
|
||||||
ContainerState,
|
|
||||||
PullImageLayerStage,
|
|
||||||
RestartPolicy,
|
|
||||||
)
|
|
||||||
from .manager import CommandReturn, PullLogEntry
|
from .manager import CommandReturn, PullLogEntry
|
||||||
from .monitor import DockerContainerStateEvent
|
from .monitor import DockerContainerStateEvent
|
||||||
from .stats import DockerStats
|
from .stats import DockerStats
|
||||||
|
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
IMAGE_WITH_HOST = re.compile(r"^((?:[a-z0-9]+(?:-[a-z0-9]+)*\.)+[a-z]{2,})\/.+")
|
||||||
|
DOCKER_HUB = "hub.docker.com"
|
||||||
|
|
||||||
MAP_ARCH: dict[CpuArch | str, str] = {
|
MAP_ARCH: dict[CpuArch | str, str] = {
|
||||||
CpuArch.ARMV7: "linux/arm/v7",
|
CpuArch.ARMV7: "linux/arm/v7",
|
||||||
CpuArch.ARMHF: "linux/arm/v6",
|
CpuArch.ARMHF: "linux/arm/v6",
|
||||||
@@ -182,17 +178,25 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
return self.meta_config.get("Healthcheck")
|
return self.meta_config.get("Healthcheck")
|
||||||
|
|
||||||
def _get_credentials(self, image: str) -> dict:
|
def _get_credentials(self, image: str) -> dict:
|
||||||
"""Return a dictionary with credentials for docker login."""
|
"""Return a dictionay with credentials for docker login."""
|
||||||
|
registry = None
|
||||||
credentials = {}
|
credentials = {}
|
||||||
registry = self.sys_docker.config.get_registry_for_image(image)
|
matcher = IMAGE_WITH_HOST.match(image)
|
||||||
|
|
||||||
|
# Custom registry
|
||||||
|
if matcher:
|
||||||
|
if matcher.group(1) in self.sys_docker.config.registries:
|
||||||
|
registry = matcher.group(1)
|
||||||
|
credentials[ATTR_REGISTRY] = registry
|
||||||
|
|
||||||
|
# If no match assume "dockerhub" as registry
|
||||||
|
elif DOCKER_HUB in self.sys_docker.config.registries:
|
||||||
|
registry = DOCKER_HUB
|
||||||
|
|
||||||
if registry:
|
if registry:
|
||||||
stored = self.sys_docker.config.registries[registry]
|
stored = self.sys_docker.config.registries[registry]
|
||||||
credentials[ATTR_USERNAME] = stored[ATTR_USERNAME]
|
credentials[ATTR_USERNAME] = stored[ATTR_USERNAME]
|
||||||
credentials[ATTR_PASSWORD] = stored[ATTR_PASSWORD]
|
credentials[ATTR_PASSWORD] = stored[ATTR_PASSWORD]
|
||||||
# Don't include registry for Docker Hub (both official and legacy)
|
|
||||||
if registry not in (DOCKER_HUB, DOCKER_HUB_LEGACY):
|
|
||||||
credentials[ATTR_REGISTRY] = registry
|
|
||||||
|
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Logging in to %s as %s",
|
"Logging in to %s as %s",
|
||||||
@@ -202,6 +206,17 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
|
|
||||||
return credentials
|
return credentials
|
||||||
|
|
||||||
|
async def _docker_login(self, image: str) -> None:
|
||||||
|
"""Try to log in to the registry if there are credentials available."""
|
||||||
|
if not self.sys_docker.config.registries:
|
||||||
|
return
|
||||||
|
|
||||||
|
credentials = self._get_credentials(image)
|
||||||
|
if not credentials:
|
||||||
|
return
|
||||||
|
|
||||||
|
await self.sys_run_in_executor(self.sys_docker.docker.login, **credentials)
|
||||||
|
|
||||||
def _process_pull_image_log( # noqa: C901
|
def _process_pull_image_log( # noqa: C901
|
||||||
self, install_job_id: str, reference: PullLogEntry
|
self, install_job_id: str, reference: PullLogEntry
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -293,8 +308,6 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
if (
|
if (
|
||||||
stage in {PullImageLayerStage.DOWNLOADING, PullImageLayerStage.EXTRACTING}
|
stage in {PullImageLayerStage.DOWNLOADING, PullImageLayerStage.EXTRACTING}
|
||||||
and reference.progress_detail
|
and reference.progress_detail
|
||||||
and reference.progress_detail.current is not None
|
|
||||||
and reference.progress_detail.total is not None
|
|
||||||
):
|
):
|
||||||
job.update(
|
job.update(
|
||||||
progress=progress,
|
progress=progress,
|
||||||
@@ -328,23 +341,40 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
]
|
]
|
||||||
|
|
||||||
# First set the total bytes to be downloaded/extracted on the main job
|
# First set the total bytes to be downloaded/extracted on the main job
|
||||||
|
# Note: With containerd snapshotter, total may be None (time-based progress instead of byte-based)
|
||||||
if not install_job.extra:
|
if not install_job.extra:
|
||||||
total = 0
|
total = 0
|
||||||
|
has_byte_progress = True
|
||||||
for job in layer_jobs:
|
for job in layer_jobs:
|
||||||
if not job.extra:
|
if not job.extra:
|
||||||
return
|
return
|
||||||
|
# If any layer has None for total, we can't do byte-weighted aggregation
|
||||||
|
if job.extra["total"] is None:
|
||||||
|
has_byte_progress = False
|
||||||
|
break
|
||||||
total += job.extra["total"]
|
total += job.extra["total"]
|
||||||
install_job.extra = {"total": total}
|
|
||||||
|
# Store whether we have byte-based progress for later use
|
||||||
|
install_job.extra = {"total": total if has_byte_progress else None}
|
||||||
else:
|
else:
|
||||||
total = install_job.extra["total"]
|
total = install_job.extra["total"]
|
||||||
|
has_byte_progress = total is not None
|
||||||
|
|
||||||
# Then determine total progress based on progress of each sub-job, factoring in size of each compared to total
|
# Then determine total progress based on progress of each sub-job
|
||||||
|
# If we have byte counts, weight by size. Otherwise, simple average.
|
||||||
progress = 0.0
|
progress = 0.0
|
||||||
stage = PullImageLayerStage.PULL_COMPLETE
|
stage = PullImageLayerStage.PULL_COMPLETE
|
||||||
for job in layer_jobs:
|
for job in layer_jobs:
|
||||||
if not job.extra:
|
if not job.extra:
|
||||||
return
|
return
|
||||||
progress += job.progress * (job.extra["total"] / total)
|
|
||||||
|
if has_byte_progress:
|
||||||
|
# Byte-weighted progress (classic Docker behavior)
|
||||||
|
progress += job.progress * (job.extra["total"] / total)
|
||||||
|
else:
|
||||||
|
# Simple average progress (containerd snapshotter with time-based progress)
|
||||||
|
progress += job.progress / len(layer_jobs)
|
||||||
|
|
||||||
job_stage = PullImageLayerStage.from_status(cast(str, job.stage))
|
job_stage = PullImageLayerStage.from_status(cast(str, job.stage))
|
||||||
|
|
||||||
if job_stage < PullImageLayerStage.EXTRACTING:
|
if job_stage < PullImageLayerStage.EXTRACTING:
|
||||||
@@ -386,8 +416,9 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
|
|
||||||
_LOGGER.info("Downloading docker image %s with tag %s.", image, version)
|
_LOGGER.info("Downloading docker image %s with tag %s.", image, version)
|
||||||
try:
|
try:
|
||||||
# Get credentials for private registries to pass to aiodocker
|
if self.sys_docker.config.registries:
|
||||||
credentials = self._get_credentials(image) or None
|
# Try login if we have defined credentials
|
||||||
|
await self._docker_login(image)
|
||||||
|
|
||||||
curr_job_id = self.sys_jobs.current.uuid
|
curr_job_id = self.sys_jobs.current.uuid
|
||||||
|
|
||||||
@@ -403,13 +434,13 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
BusEvent.DOCKER_IMAGE_PULL_UPDATE, process_pull_image_log
|
BusEvent.DOCKER_IMAGE_PULL_UPDATE, process_pull_image_log
|
||||||
)
|
)
|
||||||
|
|
||||||
# Pull new image, passing credentials to aiodocker
|
# Pull new image
|
||||||
docker_image = await self.sys_docker.pull_image(
|
docker_image = await self.sys_run_in_executor(
|
||||||
|
self.sys_docker.pull_image,
|
||||||
self.sys_jobs.current.uuid,
|
self.sys_jobs.current.uuid,
|
||||||
image,
|
image,
|
||||||
str(version),
|
str(version),
|
||||||
platform=MAP_ARCH[image_arch],
|
platform=MAP_ARCH[image_arch],
|
||||||
auth=credentials,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Tag latest
|
# Tag latest
|
||||||
@@ -417,37 +448,22 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
"Tagging image %s with version %s as latest", image, version
|
"Tagging image %s with version %s as latest", image, version
|
||||||
)
|
)
|
||||||
await self.sys_docker.images.tag(
|
await self.sys_run_in_executor(docker_image.tag, image, tag="latest")
|
||||||
docker_image["Id"], image, tag="latest"
|
|
||||||
)
|
|
||||||
except docker.errors.APIError as err:
|
except docker.errors.APIError as err:
|
||||||
if err.status_code == HTTPStatus.TOO_MANY_REQUESTS:
|
if err.status_code == 429:
|
||||||
self.sys_resolution.create_issue(
|
self.sys_resolution.create_issue(
|
||||||
IssueType.DOCKER_RATELIMIT,
|
IssueType.DOCKER_RATELIMIT,
|
||||||
ContextType.SYSTEM,
|
ContextType.SYSTEM,
|
||||||
suggestions=[SuggestionType.REGISTRY_LOGIN],
|
suggestions=[SuggestionType.REGISTRY_LOGIN],
|
||||||
)
|
)
|
||||||
raise DockerHubRateLimitExceeded(_LOGGER.error) from err
|
_LOGGER.info(
|
||||||
await async_capture_exception(err)
|
"Your IP address has made too many requests to Docker Hub which activated a rate limit. "
|
||||||
raise DockerError(
|
"For more details see https://www.home-assistant.io/more-info/dockerhub-rate-limit"
|
||||||
f"Can't install {image}:{version!s}: {err}", _LOGGER.error
|
|
||||||
) from err
|
|
||||||
except aiodocker.DockerError as err:
|
|
||||||
if err.status == HTTPStatus.TOO_MANY_REQUESTS:
|
|
||||||
self.sys_resolution.create_issue(
|
|
||||||
IssueType.DOCKER_RATELIMIT,
|
|
||||||
ContextType.SYSTEM,
|
|
||||||
suggestions=[SuggestionType.REGISTRY_LOGIN],
|
|
||||||
)
|
)
|
||||||
raise DockerHubRateLimitExceeded(_LOGGER.error) from err
|
|
||||||
await async_capture_exception(err)
|
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't install {image}:{version!s}: {err}", _LOGGER.error
|
f"Can't install {image}:{version!s}: {err}", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
except (
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
docker.errors.DockerException,
|
|
||||||
requests.RequestException,
|
|
||||||
) as err:
|
|
||||||
await async_capture_exception(err)
|
await async_capture_exception(err)
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Unknown error with {image}:{version!s} -> {err!s}", _LOGGER.error
|
f"Unknown error with {image}:{version!s} -> {err!s}", _LOGGER.error
|
||||||
@@ -456,12 +472,14 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
if listener:
|
if listener:
|
||||||
self.sys_bus.remove_listener(listener)
|
self.sys_bus.remove_listener(listener)
|
||||||
|
|
||||||
self._meta = docker_image
|
self._meta = docker_image.attrs
|
||||||
|
|
||||||
async def exists(self) -> bool:
|
async def exists(self) -> bool:
|
||||||
"""Return True if Docker image exists in local repository."""
|
"""Return True if Docker image exists in local repository."""
|
||||||
with suppress(aiodocker.DockerError, requests.RequestException):
|
with suppress(docker.errors.DockerException, requests.RequestException):
|
||||||
await self.sys_docker.images.inspect(f"{self.image}:{self.version!s}")
|
await self.sys_run_in_executor(
|
||||||
|
self.sys_docker.images.get, f"{self.image}:{self.version!s}"
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -520,11 +538,11 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
with suppress(aiodocker.DockerError, requests.RequestException):
|
with suppress(docker.errors.DockerException, requests.RequestException):
|
||||||
if not self._meta and self.image:
|
if not self._meta and self.image:
|
||||||
self._meta = await self.sys_docker.images.inspect(
|
self._meta = self.sys_docker.images.get(
|
||||||
f"{self.image}:{version!s}"
|
f"{self.image}:{version!s}"
|
||||||
)
|
).attrs
|
||||||
|
|
||||||
# Successful?
|
# Successful?
|
||||||
if not self._meta:
|
if not self._meta:
|
||||||
@@ -592,17 +610,14 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
)
|
)
|
||||||
async def remove(self, *, remove_image: bool = True) -> None:
|
async def remove(self, *, remove_image: bool = True) -> None:
|
||||||
"""Remove Docker images."""
|
"""Remove Docker images."""
|
||||||
if not self.image or not self.version:
|
|
||||||
raise DockerError(
|
|
||||||
"Cannot determine image and/or version from metadata!", _LOGGER.error
|
|
||||||
)
|
|
||||||
|
|
||||||
# Cleanup container
|
# Cleanup container
|
||||||
with suppress(DockerError):
|
with suppress(DockerError):
|
||||||
await self.stop()
|
await self.stop()
|
||||||
|
|
||||||
if remove_image:
|
if remove_image:
|
||||||
await self.sys_docker.remove_image(self.image, self.version)
|
await self.sys_run_in_executor(
|
||||||
|
self.sys_docker.remove_image, self.image, self.version
|
||||||
|
)
|
||||||
|
|
||||||
self._meta = None
|
self._meta = None
|
||||||
|
|
||||||
@@ -624,16 +639,18 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
image_name = f"{expected_image}:{version!s}"
|
image_name = f"{expected_image}:{version!s}"
|
||||||
if self.image == expected_image:
|
if self.image == expected_image:
|
||||||
try:
|
try:
|
||||||
image = await self.sys_docker.images.inspect(image_name)
|
image: Image = await self.sys_run_in_executor(
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
self.sys_docker.images.get, image_name
|
||||||
|
)
|
||||||
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Could not get {image_name} for check due to: {err!s}",
|
f"Could not get {image_name} for check due to: {err!s}",
|
||||||
_LOGGER.error,
|
_LOGGER.error,
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
image_arch = f"{image['Os']}/{image['Architecture']}"
|
image_arch = f"{image.attrs['Os']}/{image.attrs['Architecture']}"
|
||||||
if "Variant" in image:
|
if "Variant" in image.attrs:
|
||||||
image_arch = f"{image_arch}/{image['Variant']}"
|
image_arch = f"{image_arch}/{image.attrs['Variant']}"
|
||||||
|
|
||||||
# If we have an image and its the right arch, all set
|
# If we have an image and its the right arch, all set
|
||||||
# It seems that newer Docker version return a variant for arm64 images.
|
# It seems that newer Docker version return a variant for arm64 images.
|
||||||
@@ -695,13 +712,11 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
version: AwesomeVersion | None = None,
|
version: AwesomeVersion | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Check if old version exists and cleanup."""
|
"""Check if old version exists and cleanup."""
|
||||||
if not (use_image := image or self.image):
|
await self.sys_run_in_executor(
|
||||||
raise DockerError("Cannot determine image from metadata!", _LOGGER.error)
|
self.sys_docker.cleanup_old_images,
|
||||||
if not (use_version := version or self.version):
|
image or self.image,
|
||||||
raise DockerError("Cannot determine version from metadata!", _LOGGER.error)
|
version or self.version,
|
||||||
|
{old_image} if old_image else None,
|
||||||
await self.sys_docker.cleanup_old_images(
|
|
||||||
use_image, use_version, {old_image} if old_image else None
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Job(
|
@Job(
|
||||||
@@ -753,10 +768,10 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
"""Return latest version of local image."""
|
"""Return latest version of local image."""
|
||||||
available_version: list[AwesomeVersion] = []
|
available_version: list[AwesomeVersion] = []
|
||||||
try:
|
try:
|
||||||
for image in await self.sys_docker.images.list(
|
for image in await self.sys_run_in_executor(
|
||||||
filters=f'{{"reference": ["{self.image}"]}}'
|
self.sys_docker.images.list, self.image
|
||||||
):
|
):
|
||||||
for tag in image["RepoTags"]:
|
for tag in image.tags:
|
||||||
version = AwesomeVersion(tag.partition(":")[2])
|
version = AwesomeVersion(tag.partition(":")[2])
|
||||||
if version.strategy == AwesomeVersionStrategy.UNKNOWN:
|
if version.strategy == AwesomeVersionStrategy.UNKNOWN:
|
||||||
continue
|
continue
|
||||||
@@ -765,7 +780,7 @@ class DockerInterface(JobGroup, ABC):
|
|||||||
if not available_version:
|
if not available_version:
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
|
|
||||||
except (aiodocker.DockerError, ValueError) as err:
|
except (docker.errors.DockerException, ValueError) as err:
|
||||||
raise DockerNotFound(
|
raise DockerNotFound(
|
||||||
f"No version found for {self.image}", _LOGGER.info
|
f"No version found for {self.image}", _LOGGER.info
|
||||||
) from err
|
) from err
|
||||||
|
|||||||
@@ -6,24 +6,20 @@ import asyncio
|
|||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from http import HTTPStatus
|
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
|
||||||
from typing import Any, Final, Self, cast
|
from typing import Any, Final, Self, cast
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from aiodocker.images import DockerImages
|
|
||||||
from aiohttp import ClientSession, ClientTimeout, UnixConnector
|
|
||||||
import attr
|
import attr
|
||||||
from awesomeversion import AwesomeVersion, AwesomeVersionCompareException
|
from awesomeversion import AwesomeVersion, AwesomeVersionCompareException
|
||||||
from docker import errors as docker_errors
|
from docker import errors as docker_errors
|
||||||
from docker.api.client import APIClient
|
from docker.api.client import APIClient
|
||||||
from docker.client import DockerClient
|
from docker.client import DockerClient
|
||||||
|
from docker.errors import DockerException, ImageNotFound, NotFound
|
||||||
from docker.models.containers import Container, ContainerCollection
|
from docker.models.containers import Container, ContainerCollection
|
||||||
|
from docker.models.images import Image, ImageCollection
|
||||||
from docker.models.networks import Network
|
from docker.models.networks import Network
|
||||||
from docker.types.daemon import CancellableStream
|
from docker.types.daemon import CancellableStream
|
||||||
import requests
|
import requests
|
||||||
@@ -49,7 +45,7 @@ from ..exceptions import (
|
|||||||
)
|
)
|
||||||
from ..utils.common import FileConfiguration
|
from ..utils.common import FileConfiguration
|
||||||
from ..validate import SCHEMA_DOCKER_CONFIG
|
from ..validate import SCHEMA_DOCKER_CONFIG
|
||||||
from .const import DOCKER_HUB, DOCKER_HUB_LEGACY, LABEL_MANAGED, get_domain
|
from .const import LABEL_MANAGED
|
||||||
from .monitor import DockerMonitor
|
from .monitor import DockerMonitor
|
||||||
from .network import DockerNetwork
|
from .network import DockerNetwork
|
||||||
|
|
||||||
@@ -57,7 +53,6 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
MIN_SUPPORTED_DOCKER: Final = AwesomeVersion("24.0.0")
|
MIN_SUPPORTED_DOCKER: Final = AwesomeVersion("24.0.0")
|
||||||
DOCKER_NETWORK_HOST: Final = "host"
|
DOCKER_NETWORK_HOST: Final = "host"
|
||||||
RE_IMPORT_IMAGE_STREAM = re.compile(r"(^Loaded image ID: |^Loaded image: )(.+)$")
|
|
||||||
|
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(frozen=True)
|
||||||
@@ -76,25 +71,15 @@ class DockerInfo:
|
|||||||
storage: str = attr.ib()
|
storage: str = attr.ib()
|
||||||
logging: str = attr.ib()
|
logging: str = attr.ib()
|
||||||
cgroup: str = attr.ib()
|
cgroup: str = attr.ib()
|
||||||
support_cpu_realtime: bool = attr.ib()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def new(data: dict[str, Any]) -> DockerInfo:
|
def new(data: dict[str, Any]):
|
||||||
"""Create a object from docker info."""
|
"""Create a object from docker info."""
|
||||||
# Check if CONFIG_RT_GROUP_SCHED is loaded (blocking I/O in executor)
|
|
||||||
cpu_rt_file_exists = await asyncio.get_running_loop().run_in_executor(
|
|
||||||
None, Path("/sys/fs/cgroup/cpu/cpu.rt_runtime_us").exists
|
|
||||||
)
|
|
||||||
cpu_rt_supported = (
|
|
||||||
cpu_rt_file_exists and os.environ.get(ENV_SUPERVISOR_CPU_RT) == "1"
|
|
||||||
)
|
|
||||||
|
|
||||||
return DockerInfo(
|
return DockerInfo(
|
||||||
AwesomeVersion(data.get("ServerVersion", "0.0.0")),
|
AwesomeVersion(data.get("ServerVersion", "0.0.0")),
|
||||||
data.get("Driver", "unknown"),
|
data.get("Driver", "unknown"),
|
||||||
data.get("LoggingDriver", "unknown"),
|
data.get("LoggingDriver", "unknown"),
|
||||||
data.get("CgroupVersion", "1"),
|
data.get("CgroupVersion", "1"),
|
||||||
cpu_rt_supported,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -105,6 +90,13 @@ class DockerInfo:
|
|||||||
except AwesomeVersionCompareException:
|
except AwesomeVersionCompareException:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def support_cpu_realtime(self) -> bool:
|
||||||
|
"""Return true, if CONFIG_RT_GROUP_SCHED is loaded."""
|
||||||
|
if not Path("/sys/fs/cgroup/cpu/cpu.rt_runtime_us").exists():
|
||||||
|
return False
|
||||||
|
return bool(os.environ.get(ENV_SUPERVISOR_CPU_RT) == "1")
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, slots=True)
|
@dataclass(frozen=True, slots=True)
|
||||||
class PullProgressDetail:
|
class PullProgressDetail:
|
||||||
@@ -202,33 +194,6 @@ class DockerConfig(FileConfiguration):
|
|||||||
"""Return credentials for docker registries."""
|
"""Return credentials for docker registries."""
|
||||||
return self._data.get(ATTR_REGISTRIES, {})
|
return self._data.get(ATTR_REGISTRIES, {})
|
||||||
|
|
||||||
def get_registry_for_image(self, image: str) -> str | None:
|
|
||||||
"""Return the registry name if credentials are available for the image.
|
|
||||||
|
|
||||||
Matches the image against configured registries and returns the registry
|
|
||||||
name if found, or None if no matching credentials are configured.
|
|
||||||
|
|
||||||
Uses Docker's domain detection logic from:
|
|
||||||
vendor/github.com/distribution/reference/normalize.go
|
|
||||||
"""
|
|
||||||
if not self.registries:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Check if image uses a custom registry (e.g., ghcr.io/org/image)
|
|
||||||
domain = get_domain(image)
|
|
||||||
if domain:
|
|
||||||
if domain in self.registries:
|
|
||||||
return domain
|
|
||||||
else:
|
|
||||||
# No domain prefix means Docker Hub
|
|
||||||
# Support both docker.io (official) and hub.docker.com (legacy)
|
|
||||||
if DOCKER_HUB in self.registries:
|
|
||||||
return DOCKER_HUB
|
|
||||||
if DOCKER_HUB_LEGACY in self.registries:
|
|
||||||
return DOCKER_HUB_LEGACY
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class DockerAPI(CoreSysAttributes):
|
class DockerAPI(CoreSysAttributes):
|
||||||
"""Docker Supervisor wrapper.
|
"""Docker Supervisor wrapper.
|
||||||
@@ -239,15 +204,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
def __init__(self, coresys: CoreSys):
|
def __init__(self, coresys: CoreSys):
|
||||||
"""Initialize Docker base wrapper."""
|
"""Initialize Docker base wrapper."""
|
||||||
self.coresys = coresys
|
self.coresys = coresys
|
||||||
# We keep both until we can fully refactor to aiodocker
|
self._docker: DockerClient | None = None
|
||||||
self._dockerpy: DockerClient | None = None
|
|
||||||
self.docker: aiodocker.Docker = aiodocker.Docker(
|
|
||||||
url="unix://localhost", # dummy hostname for URL composition
|
|
||||||
connector=(connector := UnixConnector(SOCKET_DOCKER.as_posix())),
|
|
||||||
session=ClientSession(connector=connector, timeout=ClientTimeout(900)),
|
|
||||||
api_version="auto",
|
|
||||||
)
|
|
||||||
|
|
||||||
self._network: DockerNetwork | None = None
|
self._network: DockerNetwork | None = None
|
||||||
self._info: DockerInfo | None = None
|
self._info: DockerInfo | None = None
|
||||||
self.config: DockerConfig = DockerConfig()
|
self.config: DockerConfig = DockerConfig()
|
||||||
@@ -255,28 +212,28 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
|
|
||||||
async def post_init(self) -> Self:
|
async def post_init(self) -> Self:
|
||||||
"""Post init actions that must be done in event loop."""
|
"""Post init actions that must be done in event loop."""
|
||||||
self._dockerpy = await asyncio.get_running_loop().run_in_executor(
|
self._docker = await asyncio.get_running_loop().run_in_executor(
|
||||||
None,
|
None,
|
||||||
partial(
|
partial(
|
||||||
DockerClient,
|
DockerClient,
|
||||||
base_url=f"unix:/{SOCKET_DOCKER.as_posix()}",
|
base_url=f"unix:/{str(SOCKET_DOCKER)}",
|
||||||
version="auto",
|
version="auto",
|
||||||
timeout=900,
|
timeout=900,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
self._info = await DockerInfo.new(self.dockerpy.info())
|
self._info = DockerInfo.new(self.docker.info())
|
||||||
await self.config.read_data()
|
await self.config.read_data()
|
||||||
self._network = await DockerNetwork(self.dockerpy).post_init(
|
self._network = await DockerNetwork(self.docker).post_init(
|
||||||
self.config.enable_ipv6, self.config.mtu
|
self.config.enable_ipv6, self.config.mtu
|
||||||
)
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def dockerpy(self) -> DockerClient:
|
def docker(self) -> DockerClient:
|
||||||
"""Get docker API client."""
|
"""Get docker API client."""
|
||||||
if not self._dockerpy:
|
if not self._docker:
|
||||||
raise RuntimeError("Docker API Client not initialized!")
|
raise RuntimeError("Docker API Client not initialized!")
|
||||||
return self._dockerpy
|
return self._docker
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def network(self) -> DockerNetwork:
|
def network(self) -> DockerNetwork:
|
||||||
@@ -286,19 +243,19 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
return self._network
|
return self._network
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def images(self) -> DockerImages:
|
def images(self) -> ImageCollection:
|
||||||
"""Return API images."""
|
"""Return API images."""
|
||||||
return self.docker.images
|
return self.docker.images
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def containers(self) -> ContainerCollection:
|
def containers(self) -> ContainerCollection:
|
||||||
"""Return API containers."""
|
"""Return API containers."""
|
||||||
return self.dockerpy.containers
|
return self.docker.containers
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def api(self) -> APIClient:
|
def api(self) -> APIClient:
|
||||||
"""Return API containers."""
|
"""Return API containers."""
|
||||||
return self.dockerpy.api
|
return self.docker.api
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def info(self) -> DockerInfo:
|
def info(self) -> DockerInfo:
|
||||||
@@ -310,7 +267,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
@property
|
@property
|
||||||
def events(self) -> CancellableStream:
|
def events(self) -> CancellableStream:
|
||||||
"""Return docker event stream."""
|
"""Return docker event stream."""
|
||||||
return self.dockerpy.events(decode=True)
|
return self.docker.events(decode=True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def monitor(self) -> DockerMonitor:
|
def monitor(self) -> DockerMonitor:
|
||||||
@@ -426,7 +383,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
with suppress(DockerError):
|
with suppress(DockerError):
|
||||||
self.network.detach_default_bridge(container)
|
self.network.detach_default_bridge(container)
|
||||||
else:
|
else:
|
||||||
host_network: Network = self.dockerpy.networks.get(DOCKER_NETWORK_HOST)
|
host_network: Network = self.docker.networks.get(DOCKER_NETWORK_HOST)
|
||||||
|
|
||||||
# Check if container is register on host
|
# Check if container is register on host
|
||||||
# https://github.com/moby/moby/issues/23302
|
# https://github.com/moby/moby/issues/23302
|
||||||
@@ -453,33 +410,35 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
|
|
||||||
return container
|
return container
|
||||||
|
|
||||||
async def pull_image(
|
def pull_image(
|
||||||
self,
|
self,
|
||||||
job_id: str,
|
job_id: str,
|
||||||
repository: str,
|
repository: str,
|
||||||
tag: str = "latest",
|
tag: str = "latest",
|
||||||
platform: str | None = None,
|
platform: str | None = None,
|
||||||
auth: dict[str, str] | None = None,
|
) -> Image:
|
||||||
) -> dict[str, Any]:
|
|
||||||
"""Pull the specified image and return it.
|
"""Pull the specified image and return it.
|
||||||
|
|
||||||
This mimics the high level API of images.pull but provides better error handling by raising
|
This mimics the high level API of images.pull but provides better error handling by raising
|
||||||
based on a docker error on pull. Whereas the high level API ignores all errors on pull and
|
based on a docker error on pull. Whereas the high level API ignores all errors on pull and
|
||||||
raises only if the get fails afterwards. Additionally it fires progress reports for the pull
|
raises only if the get fails afterwards. Additionally it fires progress reports for the pull
|
||||||
on the bus so listeners can use that to update status for users.
|
on the bus so listeners can use that to update status for users.
|
||||||
|
|
||||||
|
Must be run in executor.
|
||||||
"""
|
"""
|
||||||
async for e in self.images.pull(
|
pull_log = self.docker.api.pull(
|
||||||
repository, tag=tag, platform=platform, auth=auth, stream=True
|
repository, tag=tag, platform=platform, stream=True, decode=True
|
||||||
):
|
)
|
||||||
|
for e in pull_log:
|
||||||
entry = PullLogEntry.from_pull_log_dict(job_id, e)
|
entry = PullLogEntry.from_pull_log_dict(job_id, e)
|
||||||
if entry.error:
|
if entry.error:
|
||||||
raise entry.exception
|
raise entry.exception
|
||||||
await asyncio.gather(
|
self.sys_loop.call_soon_threadsafe(
|
||||||
*self.sys_bus.fire_event(BusEvent.DOCKER_IMAGE_PULL_UPDATE, entry)
|
self.sys_bus.fire_event, BusEvent.DOCKER_IMAGE_PULL_UPDATE, entry
|
||||||
)
|
)
|
||||||
|
|
||||||
sep = "@" if tag.startswith("sha256:") else ":"
|
sep = "@" if tag.startswith("sha256:") else ":"
|
||||||
return await self.images.inspect(f"{repository}{sep}{tag}")
|
return self.images.get(f"{repository}{sep}{tag}")
|
||||||
|
|
||||||
def run_command(
|
def run_command(
|
||||||
self,
|
self,
|
||||||
@@ -500,7 +459,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
_LOGGER.info("Runing command '%s' on %s", command, image_with_tag)
|
_LOGGER.info("Runing command '%s' on %s", command, image_with_tag)
|
||||||
container = None
|
container = None
|
||||||
try:
|
try:
|
||||||
container = self.dockerpy.containers.run(
|
container = self.docker.containers.run(
|
||||||
image_with_tag,
|
image_with_tag,
|
||||||
command=command,
|
command=command,
|
||||||
detach=True,
|
detach=True,
|
||||||
@@ -528,35 +487,35 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Repair local docker overlayfs2 issues."""
|
"""Repair local docker overlayfs2 issues."""
|
||||||
_LOGGER.info("Prune stale containers")
|
_LOGGER.info("Prune stale containers")
|
||||||
try:
|
try:
|
||||||
output = self.dockerpy.api.prune_containers()
|
output = self.docker.api.prune_containers()
|
||||||
_LOGGER.debug("Containers prune: %s", output)
|
_LOGGER.debug("Containers prune: %s", output)
|
||||||
except docker_errors.APIError as err:
|
except docker_errors.APIError as err:
|
||||||
_LOGGER.warning("Error for containers prune: %s", err)
|
_LOGGER.warning("Error for containers prune: %s", err)
|
||||||
|
|
||||||
_LOGGER.info("Prune stale images")
|
_LOGGER.info("Prune stale images")
|
||||||
try:
|
try:
|
||||||
output = self.dockerpy.api.prune_images(filters={"dangling": False})
|
output = self.docker.api.prune_images(filters={"dangling": False})
|
||||||
_LOGGER.debug("Images prune: %s", output)
|
_LOGGER.debug("Images prune: %s", output)
|
||||||
except docker_errors.APIError as err:
|
except docker_errors.APIError as err:
|
||||||
_LOGGER.warning("Error for images prune: %s", err)
|
_LOGGER.warning("Error for images prune: %s", err)
|
||||||
|
|
||||||
_LOGGER.info("Prune stale builds")
|
_LOGGER.info("Prune stale builds")
|
||||||
try:
|
try:
|
||||||
output = self.dockerpy.api.prune_builds()
|
output = self.docker.api.prune_builds()
|
||||||
_LOGGER.debug("Builds prune: %s", output)
|
_LOGGER.debug("Builds prune: %s", output)
|
||||||
except docker_errors.APIError as err:
|
except docker_errors.APIError as err:
|
||||||
_LOGGER.warning("Error for builds prune: %s", err)
|
_LOGGER.warning("Error for builds prune: %s", err)
|
||||||
|
|
||||||
_LOGGER.info("Prune stale volumes")
|
_LOGGER.info("Prune stale volumes")
|
||||||
try:
|
try:
|
||||||
output = self.dockerpy.api.prune_volumes()
|
output = self.docker.api.prune_builds()
|
||||||
_LOGGER.debug("Volumes prune: %s", output)
|
_LOGGER.debug("Volumes prune: %s", output)
|
||||||
except docker_errors.APIError as err:
|
except docker_errors.APIError as err:
|
||||||
_LOGGER.warning("Error for volumes prune: %s", err)
|
_LOGGER.warning("Error for volumes prune: %s", err)
|
||||||
|
|
||||||
_LOGGER.info("Prune stale networks")
|
_LOGGER.info("Prune stale networks")
|
||||||
try:
|
try:
|
||||||
output = self.dockerpy.api.prune_networks()
|
output = self.docker.api.prune_networks()
|
||||||
_LOGGER.debug("Networks prune: %s", output)
|
_LOGGER.debug("Networks prune: %s", output)
|
||||||
except docker_errors.APIError as err:
|
except docker_errors.APIError as err:
|
||||||
_LOGGER.warning("Error for networks prune: %s", err)
|
_LOGGER.warning("Error for networks prune: %s", err)
|
||||||
@@ -578,11 +537,11 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
|
|
||||||
Fix: https://github.com/moby/moby/issues/23302
|
Fix: https://github.com/moby/moby/issues/23302
|
||||||
"""
|
"""
|
||||||
network: Network = self.dockerpy.networks.get(network_name)
|
network: Network = self.docker.networks.get(network_name)
|
||||||
|
|
||||||
for cid, data in network.attrs.get("Containers", {}).items():
|
for cid, data in network.attrs.get("Containers", {}).items():
|
||||||
try:
|
try:
|
||||||
self.dockerpy.containers.get(cid)
|
self.docker.containers.get(cid)
|
||||||
continue
|
continue
|
||||||
except docker_errors.NotFound:
|
except docker_errors.NotFound:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
@@ -597,26 +556,22 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
with suppress(docker_errors.DockerException, requests.RequestException):
|
with suppress(docker_errors.DockerException, requests.RequestException):
|
||||||
network.disconnect(data.get("Name", cid), force=True)
|
network.disconnect(data.get("Name", cid), force=True)
|
||||||
|
|
||||||
async def container_is_initialized(
|
def container_is_initialized(
|
||||||
self, name: str, image: str, version: AwesomeVersion
|
self, name: str, image: str, version: AwesomeVersion
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Return True if docker container exists in good state and is built from expected image."""
|
"""Return True if docker container exists in good state and is built from expected image."""
|
||||||
try:
|
try:
|
||||||
docker_container = await self.sys_run_in_executor(self.containers.get, name)
|
docker_container = self.containers.get(name)
|
||||||
docker_image = await self.images.inspect(f"{image}:{version}")
|
docker_image = self.images.get(f"{image}:{version}")
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
return False
|
return False
|
||||||
except aiodocker.DockerError as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
if err.status == HTTPStatus.NOT_FOUND:
|
|
||||||
return False
|
|
||||||
raise DockerError() from err
|
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
# Check the image is correct and state is good
|
# Check the image is correct and state is good
|
||||||
return (
|
return (
|
||||||
docker_container.image is not None
|
docker_container.image is not None
|
||||||
and docker_container.image.id == docker_image["Id"]
|
and docker_container.image.id == docker_image.id
|
||||||
and docker_container.status in ("exited", "running", "created")
|
and docker_container.status in ("exited", "running", "created")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -626,18 +581,18 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Stop/remove Docker container."""
|
"""Stop/remove Docker container."""
|
||||||
try:
|
try:
|
||||||
docker_container: Container = self.containers.get(name)
|
docker_container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound() from None
|
raise DockerNotFound() from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
if docker_container.status == "running":
|
if docker_container.status == "running":
|
||||||
_LOGGER.info("Stopping %s application", name)
|
_LOGGER.info("Stopping %s application", name)
|
||||||
with suppress(docker_errors.DockerException, requests.RequestException):
|
with suppress(DockerException, requests.RequestException):
|
||||||
docker_container.stop(timeout=timeout)
|
docker_container.stop(timeout=timeout)
|
||||||
|
|
||||||
if remove_container:
|
if remove_container:
|
||||||
with suppress(docker_errors.DockerException, requests.RequestException):
|
with suppress(DockerException, requests.RequestException):
|
||||||
_LOGGER.info("Cleaning %s application", name)
|
_LOGGER.info("Cleaning %s application", name)
|
||||||
docker_container.remove(force=True, v=True)
|
docker_container.remove(force=True, v=True)
|
||||||
|
|
||||||
@@ -649,11 +604,11 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Start Docker container."""
|
"""Start Docker container."""
|
||||||
try:
|
try:
|
||||||
docker_container: Container = self.containers.get(name)
|
docker_container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound(
|
raise DockerNotFound(
|
||||||
f"{name} not found for starting up", _LOGGER.error
|
f"{name} not found for starting up", _LOGGER.error
|
||||||
) from None
|
) from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Could not get {name} for starting up", _LOGGER.error
|
f"Could not get {name} for starting up", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
@@ -661,36 +616,36 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
_LOGGER.info("Starting %s", name)
|
_LOGGER.info("Starting %s", name)
|
||||||
try:
|
try:
|
||||||
docker_container.start()
|
docker_container.start()
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(f"Can't start {name}: {err}", _LOGGER.error) from err
|
raise DockerError(f"Can't start {name}: {err}", _LOGGER.error) from err
|
||||||
|
|
||||||
def restart_container(self, name: str, timeout: int) -> None:
|
def restart_container(self, name: str, timeout: int) -> None:
|
||||||
"""Restart docker container."""
|
"""Restart docker container."""
|
||||||
try:
|
try:
|
||||||
container: Container = self.containers.get(name)
|
container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound() from None
|
raise DockerNotFound() from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
_LOGGER.info("Restarting %s", name)
|
_LOGGER.info("Restarting %s", name)
|
||||||
try:
|
try:
|
||||||
container.restart(timeout=timeout)
|
container.restart(timeout=timeout)
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(f"Can't restart {name}: {err}", _LOGGER.warning) from err
|
raise DockerError(f"Can't restart {name}: {err}", _LOGGER.warning) from err
|
||||||
|
|
||||||
def container_logs(self, name: str, tail: int = 100) -> bytes:
|
def container_logs(self, name: str, tail: int = 100) -> bytes:
|
||||||
"""Return Docker logs of container."""
|
"""Return Docker logs of container."""
|
||||||
try:
|
try:
|
||||||
docker_container: Container = self.containers.get(name)
|
docker_container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound() from None
|
raise DockerNotFound() from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return docker_container.logs(tail=tail, stdout=True, stderr=True)
|
return docker_container.logs(tail=tail, stdout=True, stderr=True)
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't grep logs from {name}: {err}", _LOGGER.warning
|
f"Can't grep logs from {name}: {err}", _LOGGER.warning
|
||||||
) from err
|
) from err
|
||||||
@@ -699,9 +654,9 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Read and return stats from container."""
|
"""Read and return stats from container."""
|
||||||
try:
|
try:
|
||||||
docker_container: Container = self.containers.get(name)
|
docker_container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound() from None
|
raise DockerNotFound() from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
# container is not running
|
# container is not running
|
||||||
@@ -710,7 +665,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return docker_container.stats(stream=False)
|
return docker_container.stats(stream=False)
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't read stats from {name}: {err}", _LOGGER.error
|
f"Can't read stats from {name}: {err}", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
@@ -719,84 +674,61 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Execute a command inside Docker container."""
|
"""Execute a command inside Docker container."""
|
||||||
try:
|
try:
|
||||||
docker_container: Container = self.containers.get(name)
|
docker_container: Container = self.containers.get(name)
|
||||||
except docker_errors.NotFound:
|
except NotFound:
|
||||||
raise DockerNotFound() from None
|
raise DockerNotFound() from None
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
# Execute
|
# Execute
|
||||||
try:
|
try:
|
||||||
code, output = docker_container.exec_run(command)
|
code, output = docker_container.exec_run(command)
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError() from err
|
raise DockerError() from err
|
||||||
|
|
||||||
return CommandReturn(code, output)
|
return CommandReturn(code, output)
|
||||||
|
|
||||||
async def remove_image(
|
def remove_image(
|
||||||
self, image: str, version: AwesomeVersion, latest: bool = True
|
self, image: str, version: AwesomeVersion, latest: bool = True
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Remove a Docker image by version and latest."""
|
"""Remove a Docker image by version and latest."""
|
||||||
try:
|
try:
|
||||||
if latest:
|
if latest:
|
||||||
_LOGGER.info("Removing image %s with latest", image)
|
_LOGGER.info("Removing image %s with latest", image)
|
||||||
try:
|
with suppress(ImageNotFound):
|
||||||
await self.images.delete(f"{image}:latest", force=True)
|
self.images.remove(image=f"{image}:latest", force=True)
|
||||||
except aiodocker.DockerError as err:
|
|
||||||
if err.status != HTTPStatus.NOT_FOUND:
|
|
||||||
raise
|
|
||||||
|
|
||||||
_LOGGER.info("Removing image %s with %s", image, version)
|
_LOGGER.info("Removing image %s with %s", image, version)
|
||||||
try:
|
with suppress(ImageNotFound):
|
||||||
await self.images.delete(f"{image}:{version!s}", force=True)
|
self.images.remove(image=f"{image}:{version!s}", force=True)
|
||||||
except aiodocker.DockerError as err:
|
|
||||||
if err.status != HTTPStatus.NOT_FOUND:
|
|
||||||
raise
|
|
||||||
|
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't remove image {image}: {err}", _LOGGER.warning
|
f"Can't remove image {image}: {err}", _LOGGER.warning
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
async def import_image(self, tar_file: Path) -> dict[str, Any] | None:
|
def import_image(self, tar_file: Path) -> Image | None:
|
||||||
"""Import a tar file as image."""
|
"""Import a tar file as image."""
|
||||||
try:
|
try:
|
||||||
with tar_file.open("rb") as read_tar:
|
with tar_file.open("rb") as read_tar:
|
||||||
resp: list[dict[str, Any]] = self.images.import_image(read_tar)
|
docker_image_list: list[Image] = self.images.load(read_tar) # type: ignore
|
||||||
except (aiodocker.DockerError, OSError) as err:
|
|
||||||
|
if len(docker_image_list) != 1:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Unexpected image count %d while importing image from tar",
|
||||||
|
len(docker_image_list),
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
return docker_image_list[0]
|
||||||
|
except (DockerException, OSError) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't import image from tar: {err}", _LOGGER.error
|
f"Can't import image from tar: {err}", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
docker_image_list: list[str] = []
|
|
||||||
for chunk in resp:
|
|
||||||
if "errorDetail" in chunk:
|
|
||||||
raise DockerError(
|
|
||||||
f"Can't import image from tar: {chunk['errorDetail']['message']}",
|
|
||||||
_LOGGER.error,
|
|
||||||
)
|
|
||||||
if "stream" in chunk:
|
|
||||||
if match := RE_IMPORT_IMAGE_STREAM.search(chunk["stream"]):
|
|
||||||
docker_image_list.append(match.group(2))
|
|
||||||
|
|
||||||
if len(docker_image_list) != 1:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"Unexpected image count %d while importing image from tar",
|
|
||||||
len(docker_image_list),
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
return await self.images.inspect(docker_image_list[0])
|
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
|
||||||
raise DockerError(
|
|
||||||
f"Could not inspect imported image due to: {err!s}", _LOGGER.error
|
|
||||||
) from err
|
|
||||||
|
|
||||||
def export_image(self, image: str, version: AwesomeVersion, tar_file: Path) -> None:
|
def export_image(self, image: str, version: AwesomeVersion, tar_file: Path) -> None:
|
||||||
"""Export current images into a tar file."""
|
"""Export current images into a tar file."""
|
||||||
try:
|
try:
|
||||||
docker_image = self.api.get_image(f"{image}:{version}")
|
docker_image = self.api.get_image(f"{image}:{version}")
|
||||||
except (docker_errors.DockerException, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't fetch image {image}: {err}", _LOGGER.error
|
f"Can't fetch image {image}: {err}", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
@@ -813,7 +745,7 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
|
|
||||||
_LOGGER.info("Export image %s done", image)
|
_LOGGER.info("Export image %s done", image)
|
||||||
|
|
||||||
async def cleanup_old_images(
|
def cleanup_old_images(
|
||||||
self,
|
self,
|
||||||
current_image: str,
|
current_image: str,
|
||||||
current_version: AwesomeVersion,
|
current_version: AwesomeVersion,
|
||||||
@@ -824,57 +756,46 @@ class DockerAPI(CoreSysAttributes):
|
|||||||
"""Clean up old versions of an image."""
|
"""Clean up old versions of an image."""
|
||||||
image = f"{current_image}:{current_version!s}"
|
image = f"{current_image}:{current_version!s}"
|
||||||
try:
|
try:
|
||||||
try:
|
keep = {cast(str, self.images.get(image).id)}
|
||||||
image_attr = await self.images.inspect(image)
|
except ImageNotFound:
|
||||||
except aiodocker.DockerError as err:
|
raise DockerNotFound(
|
||||||
if err.status == HTTPStatus.NOT_FOUND:
|
f"{current_image} not found for cleanup", _LOGGER.warning
|
||||||
raise DockerNotFound(
|
) from None
|
||||||
f"{current_image} not found for cleanup", _LOGGER.warning
|
except (DockerException, requests.RequestException) as err:
|
||||||
) from None
|
|
||||||
raise
|
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't get {current_image} for cleanup", _LOGGER.warning
|
f"Can't get {current_image} for cleanup", _LOGGER.warning
|
||||||
) from err
|
) from err
|
||||||
keep = {cast(str, image_attr["Id"])}
|
|
||||||
|
|
||||||
if keep_images:
|
if keep_images:
|
||||||
keep_images -= {image}
|
keep_images -= {image}
|
||||||
results = await asyncio.gather(
|
try:
|
||||||
*[self.images.inspect(image) for image in keep_images],
|
for image in keep_images:
|
||||||
return_exceptions=True,
|
# If its not found, no need to preserve it from getting removed
|
||||||
)
|
with suppress(ImageNotFound):
|
||||||
for result in results:
|
keep.add(cast(str, self.images.get(image).id))
|
||||||
# If its not found, no need to preserve it from getting removed
|
except (DockerException, requests.RequestException) as err:
|
||||||
if (
|
raise DockerError(
|
||||||
isinstance(result, aiodocker.DockerError)
|
f"Failed to get one or more images from {keep} during cleanup",
|
||||||
and result.status == HTTPStatus.NOT_FOUND
|
_LOGGER.warning,
|
||||||
):
|
) from err
|
||||||
continue
|
|
||||||
if isinstance(result, BaseException):
|
|
||||||
raise DockerError(
|
|
||||||
f"Failed to get one or more images from {keep} during cleanup",
|
|
||||||
_LOGGER.warning,
|
|
||||||
) from result
|
|
||||||
keep.add(cast(str, result["Id"]))
|
|
||||||
|
|
||||||
# Cleanup old and current
|
# Cleanup old and current
|
||||||
image_names = list(
|
image_names = list(
|
||||||
old_images | {current_image} if old_images else {current_image}
|
old_images | {current_image} if old_images else {current_image}
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
images_list = await self.images.list(
|
# This API accepts a list of image names. Tested and confirmed working on docker==7.1.0
|
||||||
filters=json.dumps({"reference": image_names})
|
# Its typing does say only `str` though. Bit concerning, could an update break this?
|
||||||
)
|
images_list = self.images.list(name=image_names) # type: ignore
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
except (DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Corrupt docker overlayfs found: {err}", _LOGGER.warning
|
f"Corrupt docker overlayfs found: {err}", _LOGGER.warning
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
for docker_image in images_list:
|
for docker_image in images_list:
|
||||||
if docker_image["Id"] in keep:
|
if docker_image.id in keep:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
with suppress(aiodocker.DockerError, requests.RequestException):
|
with suppress(DockerException, requests.RequestException):
|
||||||
_LOGGER.info("Cleanup images: %s", docker_image["RepoTags"])
|
_LOGGER.info("Cleanup images: %s", docker_image.tags)
|
||||||
await self.images.delete(docker_image["Id"], force=True)
|
self.images.remove(docker_image.id, force=True)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ class DockerMonitor(CoreSysAttributes, Thread):
|
|||||||
DockerContainerStateEvent(
|
DockerContainerStateEvent(
|
||||||
name=attributes["name"],
|
name=attributes["name"],
|
||||||
state=container_state,
|
state=container_state,
|
||||||
id=event["Actor"]["ID"],
|
id=event["id"],
|
||||||
time=event["time"],
|
time=event["time"],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
"""Init file for Supervisor Docker object."""
|
"""Init file for Supervisor Docker object."""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from collections.abc import Awaitable
|
from collections.abc import Awaitable
|
||||||
from ipaddress import IPv4Address
|
from ipaddress import IPv4Address
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from awesomeversion.awesomeversion import AwesomeVersion
|
from awesomeversion.awesomeversion import AwesomeVersion
|
||||||
import docker
|
import docker
|
||||||
import requests
|
import requests
|
||||||
@@ -114,18 +112,19 @@ class DockerSupervisor(DockerInterface):
|
|||||||
name="docker_supervisor_update_start_tag",
|
name="docker_supervisor_update_start_tag",
|
||||||
concurrency=JobConcurrency.GROUP_QUEUE,
|
concurrency=JobConcurrency.GROUP_QUEUE,
|
||||||
)
|
)
|
||||||
async def update_start_tag(self, image: str, version: AwesomeVersion) -> None:
|
def update_start_tag(self, image: str, version: AwesomeVersion) -> Awaitable[None]:
|
||||||
"""Update start tag to new version."""
|
"""Update start tag to new version."""
|
||||||
|
return self.sys_run_in_executor(self._update_start_tag, image, version)
|
||||||
|
|
||||||
|
def _update_start_tag(self, image: str, version: AwesomeVersion) -> None:
|
||||||
|
"""Update start tag to new version.
|
||||||
|
|
||||||
|
Need run inside executor.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
docker_container = await self.sys_run_in_executor(
|
docker_container = self.sys_docker.containers.get(self.name)
|
||||||
self.sys_docker.containers.get, self.name
|
docker_image = self.sys_docker.images.get(f"{image}:{version!s}")
|
||||||
)
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
docker_image = await self.sys_docker.images.inspect(f"{image}:{version!s}")
|
|
||||||
except (
|
|
||||||
aiodocker.DockerError,
|
|
||||||
docker.errors.DockerException,
|
|
||||||
requests.RequestException,
|
|
||||||
) as err:
|
|
||||||
raise DockerError(
|
raise DockerError(
|
||||||
f"Can't get image or container to fix start tag: {err}", _LOGGER.error
|
f"Can't get image or container to fix start tag: {err}", _LOGGER.error
|
||||||
) from err
|
) from err
|
||||||
@@ -145,14 +144,8 @@ class DockerSupervisor(DockerInterface):
|
|||||||
# If version tag
|
# If version tag
|
||||||
if start_tag != "latest":
|
if start_tag != "latest":
|
||||||
continue
|
continue
|
||||||
await asyncio.gather(
|
docker_image.tag(start_image, start_tag)
|
||||||
self.sys_docker.images.tag(
|
docker_image.tag(start_image, version.string)
|
||||||
docker_image["Id"], start_image, tag=start_tag
|
|
||||||
),
|
|
||||||
self.sys_docker.images.tag(
|
|
||||||
docker_image["Id"], start_image, tag=version.string
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
except (aiodocker.DockerError, requests.RequestException) as err:
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
raise DockerError(f"Can't fix start tag: {err}", _LOGGER.error) from err
|
raise DockerError(f"Can't fix start tag: {err}", _LOGGER.error) from err
|
||||||
|
|||||||
@@ -639,32 +639,9 @@ class DockerLogOutOfOrder(DockerError):
|
|||||||
class DockerNoSpaceOnDevice(DockerError):
|
class DockerNoSpaceOnDevice(DockerError):
|
||||||
"""Raise if a docker pull fails due to available space."""
|
"""Raise if a docker pull fails due to available space."""
|
||||||
|
|
||||||
error_key = "docker_no_space_on_device"
|
|
||||||
message_template = "No space left on disk"
|
|
||||||
|
|
||||||
def __init__(self, logger: Callable[..., None] | None = None) -> None:
|
def __init__(self, logger: Callable[..., None] | None = None) -> None:
|
||||||
"""Raise & log."""
|
"""Raise & log."""
|
||||||
super().__init__(None, logger=logger)
|
super().__init__("No space left on disk", logger=logger)
|
||||||
|
|
||||||
|
|
||||||
class DockerHubRateLimitExceeded(DockerError):
|
|
||||||
"""Raise for docker hub rate limit exceeded error."""
|
|
||||||
|
|
||||||
error_key = "dockerhub_rate_limit_exceeded"
|
|
||||||
message_template = (
|
|
||||||
"Your IP address has made too many requests to Docker Hub which activated a rate limit. "
|
|
||||||
"For more details see {dockerhub_rate_limit_url}"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, logger: Callable[..., None] | None = None) -> None:
|
|
||||||
"""Raise & log."""
|
|
||||||
super().__init__(
|
|
||||||
None,
|
|
||||||
logger=logger,
|
|
||||||
extra_fields={
|
|
||||||
"dockerhub_rate_limit_url": "https://www.home-assistant.io/more-info/dockerhub-rate-limit"
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DockerJobError(DockerError, JobException):
|
class DockerJobError(DockerError, JobException):
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from contextvars import Context, ContextVar, Token
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, Self, cast
|
from typing import Any, Self
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from attr.validators import gt, lt
|
from attr.validators import gt, lt
|
||||||
@@ -196,7 +196,7 @@ class SupervisorJob:
|
|||||||
self,
|
self,
|
||||||
progress: float | None = None,
|
progress: float | None = None,
|
||||||
stage: str | None = None,
|
stage: str | None = None,
|
||||||
extra: dict[str, Any] | None | type[DEFAULT] = DEFAULT,
|
extra: dict[str, Any] | None = DEFAULT, # type: ignore
|
||||||
done: bool | None = None,
|
done: bool | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update multiple fields with one on change event."""
|
"""Update multiple fields with one on change event."""
|
||||||
@@ -207,8 +207,8 @@ class SupervisorJob:
|
|||||||
self.progress = progress
|
self.progress = progress
|
||||||
if stage is not None:
|
if stage is not None:
|
||||||
self.stage = stage
|
self.stage = stage
|
||||||
if extra is not DEFAULT:
|
if extra != DEFAULT:
|
||||||
self.extra = cast(dict[str, Any] | None, extra)
|
self.extra = extra
|
||||||
|
|
||||||
# Done has special event. use that to trigger on change if included
|
# Done has special event. use that to trigger on change if included
|
||||||
# If not then just use any other field to trigger
|
# If not then just use any other field to trigger
|
||||||
@@ -306,21 +306,19 @@ class JobManager(FileConfiguration, CoreSysAttributes):
|
|||||||
reference: str | None = None,
|
reference: str | None = None,
|
||||||
initial_stage: str | None = None,
|
initial_stage: str | None = None,
|
||||||
internal: bool = False,
|
internal: bool = False,
|
||||||
parent_id: str | None | type[DEFAULT] = DEFAULT,
|
parent_id: str | None = DEFAULT, # type: ignore
|
||||||
child_job_syncs: list[ChildJobSyncFilter] | None = None,
|
child_job_syncs: list[ChildJobSyncFilter] | None = None,
|
||||||
) -> SupervisorJob:
|
) -> SupervisorJob:
|
||||||
"""Create a new job."""
|
"""Create a new job."""
|
||||||
kwargs: dict[str, Any] = {
|
job = SupervisorJob(
|
||||||
"reference": reference,
|
name,
|
||||||
"stage": initial_stage,
|
reference=reference,
|
||||||
"on_change": self._on_job_change,
|
stage=initial_stage,
|
||||||
"internal": internal,
|
on_change=self._on_job_change,
|
||||||
"child_job_syncs": child_job_syncs,
|
internal=internal,
|
||||||
}
|
child_job_syncs=child_job_syncs,
|
||||||
if parent_id is not DEFAULT:
|
**({} if parent_id == DEFAULT else {"parent_id": parent_id}), # type: ignore
|
||||||
kwargs["parent_id"] = parent_id
|
)
|
||||||
|
|
||||||
job = SupervisorJob(name, **kwargs)
|
|
||||||
|
|
||||||
# Shouldn't happen but inability to find a parent for progress reporting
|
# Shouldn't happen but inability to find a parent for progress reporting
|
||||||
# shouldn't raise and break the active job
|
# shouldn't raise and break the active job
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ class JobCondition(StrEnum):
|
|||||||
PLUGINS_UPDATED = "plugins_updated"
|
PLUGINS_UPDATED = "plugins_updated"
|
||||||
RUNNING = "running"
|
RUNNING = "running"
|
||||||
SUPERVISOR_UPDATED = "supervisor_updated"
|
SUPERVISOR_UPDATED = "supervisor_updated"
|
||||||
ARCHITECTURE_SUPPORTED = "architecture_supported"
|
|
||||||
|
|
||||||
|
|
||||||
class JobConcurrency(StrEnum):
|
class JobConcurrency(StrEnum):
|
||||||
|
|||||||
@@ -441,14 +441,6 @@ class Job(CoreSysAttributes):
|
|||||||
raise JobConditionException(
|
raise JobConditionException(
|
||||||
f"'{method_name}' blocked from execution, supervisor needs to be updated first"
|
f"'{method_name}' blocked from execution, supervisor needs to be updated first"
|
||||||
)
|
)
|
||||||
if (
|
|
||||||
JobCondition.ARCHITECTURE_SUPPORTED in used_conditions
|
|
||||||
and UnsupportedReason.SYSTEM_ARCHITECTURE
|
|
||||||
in coresys.sys_resolution.unsupported
|
|
||||||
):
|
|
||||||
raise JobConditionException(
|
|
||||||
f"'{method_name}' blocked from execution, unsupported system architecture"
|
|
||||||
)
|
|
||||||
|
|
||||||
if JobCondition.PLUGINS_UPDATED in used_conditions and (
|
if JobCondition.PLUGINS_UPDATED in used_conditions and (
|
||||||
out_of_date := [
|
out_of_date := [
|
||||||
|
|||||||
@@ -64,19 +64,6 @@ def filter_data(coresys: CoreSys, event: Event, hint: Hint) -> Event | None:
|
|||||||
|
|
||||||
# Not full startup - missing information
|
# Not full startup - missing information
|
||||||
if coresys.core.state in (CoreState.INITIALIZE, CoreState.SETUP):
|
if coresys.core.state in (CoreState.INITIALIZE, CoreState.SETUP):
|
||||||
# During SETUP, we have basic system info available for better debugging
|
|
||||||
if coresys.core.state == CoreState.SETUP:
|
|
||||||
event.setdefault("contexts", {}).update(
|
|
||||||
{
|
|
||||||
"versions": {
|
|
||||||
"docker": coresys.docker.info.version,
|
|
||||||
"supervisor": coresys.supervisor.version,
|
|
||||||
},
|
|
||||||
"host": {
|
|
||||||
"machine": coresys.machine,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
return event
|
return event
|
||||||
|
|
||||||
# List installed addons
|
# List installed addons
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""A collection of tasks."""
|
"""A collection of tasks."""
|
||||||
|
|
||||||
from contextlib import suppress
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import cast
|
from typing import cast
|
||||||
@@ -14,7 +13,6 @@ from ..exceptions import (
|
|||||||
BackupFileNotFoundError,
|
BackupFileNotFoundError,
|
||||||
HomeAssistantError,
|
HomeAssistantError,
|
||||||
ObserverError,
|
ObserverError,
|
||||||
SupervisorUpdateError,
|
|
||||||
)
|
)
|
||||||
from ..homeassistant.const import LANDINGPAGE, WSType
|
from ..homeassistant.const import LANDINGPAGE, WSType
|
||||||
from ..jobs.const import JobConcurrency
|
from ..jobs.const import JobConcurrency
|
||||||
@@ -163,7 +161,6 @@ class Tasks(CoreSysAttributes):
|
|||||||
JobCondition.INTERNET_HOST,
|
JobCondition.INTERNET_HOST,
|
||||||
JobCondition.OS_SUPPORTED,
|
JobCondition.OS_SUPPORTED,
|
||||||
JobCondition.RUNNING,
|
JobCondition.RUNNING,
|
||||||
JobCondition.ARCHITECTURE_SUPPORTED,
|
|
||||||
],
|
],
|
||||||
concurrency=JobConcurrency.REJECT,
|
concurrency=JobConcurrency.REJECT,
|
||||||
)
|
)
|
||||||
@@ -176,11 +173,7 @@ class Tasks(CoreSysAttributes):
|
|||||||
"Found new Supervisor version %s, updating",
|
"Found new Supervisor version %s, updating",
|
||||||
self.sys_supervisor.latest_version,
|
self.sys_supervisor.latest_version,
|
||||||
)
|
)
|
||||||
|
await self.sys_supervisor.update()
|
||||||
# Errors are logged by the exceptions, we can't really do something
|
|
||||||
# if an update fails here.
|
|
||||||
with suppress(SupervisorUpdateError):
|
|
||||||
await self.sys_supervisor.update()
|
|
||||||
|
|
||||||
async def _watchdog_homeassistant_api(self):
|
async def _watchdog_homeassistant_api(self):
|
||||||
"""Create scheduler task for monitoring running state of API.
|
"""Create scheduler task for monitoring running state of API.
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ class Mount(CoreSysAttributes, ABC):
|
|||||||
@property
|
@property
|
||||||
def state(self) -> UnitActiveState | None:
|
def state(self) -> UnitActiveState | None:
|
||||||
"""Get state of mount."""
|
"""Get state of mount."""
|
||||||
return UnitActiveState(self._state) if self._state is not None else None
|
return self._state
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def local_where(self) -> Path:
|
def local_where(self) -> Path:
|
||||||
|
|||||||
@@ -23,5 +23,4 @@ PLUGIN_UPDATE_CONDITIONS = [
|
|||||||
JobCondition.HEALTHY,
|
JobCondition.HEALTHY,
|
||||||
JobCondition.INTERNET_HOST,
|
JobCondition.INTERNET_HOST,
|
||||||
JobCondition.SUPERVISOR_UPDATED,
|
JobCondition.SUPERVISOR_UPDATED,
|
||||||
JobCondition.ARCHITECTURE_SUPPORTED,
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ class UnsupportedReason(StrEnum):
|
|||||||
SYSTEMD_JOURNAL = "systemd_journal"
|
SYSTEMD_JOURNAL = "systemd_journal"
|
||||||
SYSTEMD_RESOLVED = "systemd_resolved"
|
SYSTEMD_RESOLVED = "systemd_resolved"
|
||||||
VIRTUALIZATION_IMAGE = "virtualization_image"
|
VIRTUALIZATION_IMAGE = "virtualization_image"
|
||||||
SYSTEM_ARCHITECTURE = "system_architecture"
|
|
||||||
|
|
||||||
|
|
||||||
class UnhealthyReason(StrEnum):
|
class UnhealthyReason(StrEnum):
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ from .validate import get_valid_modules
|
|||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
UNHEALTHY = [
|
UNHEALTHY = [
|
||||||
|
UnsupportedReason.DOCKER_VERSION,
|
||||||
UnsupportedReason.LXC,
|
UnsupportedReason.LXC,
|
||||||
UnsupportedReason.PRIVILEGED,
|
UnsupportedReason.PRIVILEGED,
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ from ...coresys import CoreSys
|
|||||||
from ..const import UnsupportedReason
|
from ..const import UnsupportedReason
|
||||||
from .base import EvaluateBase
|
from .base import EvaluateBase
|
||||||
|
|
||||||
|
SUPPORTED_OS = ["Debian GNU/Linux 12 (bookworm)"]
|
||||||
|
|
||||||
|
|
||||||
def setup(coresys: CoreSys) -> EvaluateBase:
|
def setup(coresys: CoreSys) -> EvaluateBase:
|
||||||
"""Initialize evaluation-setup function."""
|
"""Initialize evaluation-setup function."""
|
||||||
@@ -31,4 +33,6 @@ class EvaluateOperatingSystem(EvaluateBase):
|
|||||||
|
|
||||||
async def evaluate(self) -> bool:
|
async def evaluate(self) -> bool:
|
||||||
"""Run evaluation."""
|
"""Run evaluation."""
|
||||||
return not self.sys_os.available
|
if self.sys_os.available:
|
||||||
|
return False
|
||||||
|
return self.sys_host.info.operating_system not in SUPPORTED_OS
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
"""Evaluation class for system architecture support."""
|
|
||||||
|
|
||||||
from ...const import CoreState
|
|
||||||
from ...coresys import CoreSys
|
|
||||||
from ..const import UnsupportedReason
|
|
||||||
from .base import EvaluateBase
|
|
||||||
|
|
||||||
|
|
||||||
def setup(coresys: CoreSys) -> EvaluateBase:
|
|
||||||
"""Initialize evaluation-setup function."""
|
|
||||||
return EvaluateSystemArchitecture(coresys)
|
|
||||||
|
|
||||||
|
|
||||||
class EvaluateSystemArchitecture(EvaluateBase):
|
|
||||||
"""Evaluate if the current Supervisor architecture is supported."""
|
|
||||||
|
|
||||||
@property
|
|
||||||
def reason(self) -> UnsupportedReason:
|
|
||||||
"""Return a UnsupportedReason enum."""
|
|
||||||
return UnsupportedReason.SYSTEM_ARCHITECTURE
|
|
||||||
|
|
||||||
@property
|
|
||||||
def on_failure(self) -> str:
|
|
||||||
"""Return a string that is printed when self.evaluate is True."""
|
|
||||||
return "System architecture is no longer supported. Move to a supported system architecture."
|
|
||||||
|
|
||||||
@property
|
|
||||||
def states(self) -> list[CoreState]:
|
|
||||||
"""Return a list of valid states when this evaluation can run."""
|
|
||||||
return [CoreState.INITIALIZE]
|
|
||||||
|
|
||||||
async def evaluate(self):
|
|
||||||
"""Run evaluation."""
|
|
||||||
return self.sys_host.info.sys_arch.supervisor in {
|
|
||||||
"i386",
|
|
||||||
"armhf",
|
|
||||||
"armv7",
|
|
||||||
}
|
|
||||||
@@ -242,10 +242,9 @@ class Updater(FileConfiguration, CoreSysAttributes):
|
|||||||
@Job(
|
@Job(
|
||||||
name="updater_fetch_data",
|
name="updater_fetch_data",
|
||||||
conditions=[
|
conditions=[
|
||||||
JobCondition.ARCHITECTURE_SUPPORTED,
|
|
||||||
JobCondition.INTERNET_SYSTEM,
|
JobCondition.INTERNET_SYSTEM,
|
||||||
JobCondition.HOME_ASSISTANT_CORE_SUPPORTED,
|
|
||||||
JobCondition.OS_SUPPORTED,
|
JobCondition.OS_SUPPORTED,
|
||||||
|
JobCondition.HOME_ASSISTANT_CORE_SUPPORTED,
|
||||||
],
|
],
|
||||||
on_condition=UpdaterJobError,
|
on_condition=UpdaterJobError,
|
||||||
throttle_period=timedelta(seconds=30),
|
throttle_period=timedelta(seconds=30),
|
||||||
|
|||||||
@@ -7,7 +7,13 @@ from collections.abc import Awaitable, Callable
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any, Protocol, cast
|
from typing import Any, Protocol, cast
|
||||||
|
|
||||||
from dbus_fast import ErrorType, InvalidIntrospectionError, Message, MessageType
|
from dbus_fast import (
|
||||||
|
ErrorType,
|
||||||
|
InvalidIntrospectionError,
|
||||||
|
Message,
|
||||||
|
MessageType,
|
||||||
|
Variant,
|
||||||
|
)
|
||||||
from dbus_fast.aio.message_bus import MessageBus
|
from dbus_fast.aio.message_bus import MessageBus
|
||||||
from dbus_fast.aio.proxy_object import ProxyInterface, ProxyObject
|
from dbus_fast.aio.proxy_object import ProxyInterface, ProxyObject
|
||||||
from dbus_fast.errors import DBusError as DBusFastDBusError
|
from dbus_fast.errors import DBusError as DBusFastDBusError
|
||||||
@@ -259,7 +265,7 @@ class DBus:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
async def sync_property_change(
|
async def sync_property_change(
|
||||||
prop_interface: str, changed: dict[str, Any], invalidated: list[str]
|
prop_interface: str, changed: dict[str, Variant], invalidated: list[str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Sync property changes to cache."""
|
"""Sync property changes to cache."""
|
||||||
if interface != prop_interface:
|
if interface != prop_interface:
|
||||||
|
|||||||
@@ -5,20 +5,12 @@ from collections.abc import AsyncGenerator
|
|||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
import json
|
import json
|
||||||
import re
|
|
||||||
|
|
||||||
from aiohttp import ClientResponse
|
from aiohttp import ClientResponse
|
||||||
|
|
||||||
from supervisor.exceptions import MalformedBinaryEntryError
|
from supervisor.exceptions import MalformedBinaryEntryError
|
||||||
from supervisor.host.const import LogFormatter
|
from supervisor.host.const import LogFormatter
|
||||||
|
|
||||||
_RE_ANSI_CSI_COLORS_PATTERN = re.compile(r"\x1B\[[0-9;]*m")
|
|
||||||
|
|
||||||
|
|
||||||
def _strip_ansi_colors(message: str) -> str:
|
|
||||||
"""Remove ANSI color codes from a message string."""
|
|
||||||
return _RE_ANSI_CSI_COLORS_PATTERN.sub("", message)
|
|
||||||
|
|
||||||
|
|
||||||
def formatter(required_fields: list[str]):
|
def formatter(required_fields: list[str]):
|
||||||
"""Decorate journal entry formatters with list of required fields.
|
"""Decorate journal entry formatters with list of required fields.
|
||||||
@@ -39,9 +31,9 @@ def formatter(required_fields: list[str]):
|
|||||||
|
|
||||||
|
|
||||||
@formatter(["MESSAGE"])
|
@formatter(["MESSAGE"])
|
||||||
def journal_plain_formatter(entries: dict[str, str], no_colors: bool = False) -> str:
|
def journal_plain_formatter(entries: dict[str, str]) -> str:
|
||||||
"""Format parsed journal entries as a plain message."""
|
"""Format parsed journal entries as a plain message."""
|
||||||
return _strip_ansi_colors(entries["MESSAGE"]) if no_colors else entries["MESSAGE"]
|
return entries["MESSAGE"]
|
||||||
|
|
||||||
|
|
||||||
@formatter(
|
@formatter(
|
||||||
@@ -53,7 +45,7 @@ def journal_plain_formatter(entries: dict[str, str], no_colors: bool = False) ->
|
|||||||
"MESSAGE",
|
"MESSAGE",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def journal_verbose_formatter(entries: dict[str, str], no_colors: bool = False) -> str:
|
def journal_verbose_formatter(entries: dict[str, str]) -> str:
|
||||||
"""Format parsed journal entries to a journalctl-like format."""
|
"""Format parsed journal entries to a journalctl-like format."""
|
||||||
ts = datetime.fromtimestamp(
|
ts = datetime.fromtimestamp(
|
||||||
int(entries["__REALTIME_TIMESTAMP"]) / 1e6, UTC
|
int(entries["__REALTIME_TIMESTAMP"]) / 1e6, UTC
|
||||||
@@ -66,24 +58,14 @@ def journal_verbose_formatter(entries: dict[str, str], no_colors: bool = False)
|
|||||||
else entries.get("SYSLOG_IDENTIFIER", "_UNKNOWN_")
|
else entries.get("SYSLOG_IDENTIFIER", "_UNKNOWN_")
|
||||||
)
|
)
|
||||||
|
|
||||||
message = (
|
return f"{ts} {entries.get('_HOSTNAME', '')} {identifier}: {entries.get('MESSAGE', '')}"
|
||||||
_strip_ansi_colors(entries.get("MESSAGE", ""))
|
|
||||||
if no_colors
|
|
||||||
else entries.get("MESSAGE", "")
|
|
||||||
)
|
|
||||||
|
|
||||||
return f"{ts} {entries.get('_HOSTNAME', '')} {identifier}: {message}"
|
|
||||||
|
|
||||||
|
|
||||||
async def journal_logs_reader(
|
async def journal_logs_reader(
|
||||||
journal_logs: ClientResponse,
|
journal_logs: ClientResponse, log_formatter: LogFormatter = LogFormatter.PLAIN
|
||||||
log_formatter: LogFormatter = LogFormatter.PLAIN,
|
|
||||||
no_colors: bool = False,
|
|
||||||
) -> AsyncGenerator[tuple[str | None, str]]:
|
) -> AsyncGenerator[tuple[str | None, str]]:
|
||||||
"""Read logs from systemd journal line by line, formatted using the given formatter.
|
"""Read logs from systemd journal line by line, formatted using the given formatter.
|
||||||
|
|
||||||
Optionally strip ANSI color codes from the entries' messages.
|
|
||||||
|
|
||||||
Returns a generator of (cursor, formatted_entry) tuples.
|
Returns a generator of (cursor, formatted_entry) tuples.
|
||||||
"""
|
"""
|
||||||
match log_formatter:
|
match log_formatter:
|
||||||
@@ -102,10 +84,7 @@ async def journal_logs_reader(
|
|||||||
# at EOF (likely race between at_eof and EOF check in readuntil)
|
# at EOF (likely race between at_eof and EOF check in readuntil)
|
||||||
if line == b"\n" or not line:
|
if line == b"\n" or not line:
|
||||||
if entries:
|
if entries:
|
||||||
yield (
|
yield entries.get("__CURSOR"), formatter_(entries)
|
||||||
entries.get("__CURSOR"),
|
|
||||||
formatter_(entries, no_colors=no_colors),
|
|
||||||
)
|
|
||||||
entries = {}
|
entries = {}
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
@@ -3,25 +3,22 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import errno
|
import errno
|
||||||
from http import HTTPStatus
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock, PropertyMock, call, patch
|
from unittest.mock import MagicMock, PropertyMock, patch
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
from docker.errors import APIError, DockerException, NotFound
|
from docker.errors import DockerException, ImageNotFound, NotFound
|
||||||
import pytest
|
import pytest
|
||||||
from securetar import SecureTarFile
|
from securetar import SecureTarFile
|
||||||
|
|
||||||
from supervisor.addons.addon import Addon
|
from supervisor.addons.addon import Addon
|
||||||
from supervisor.addons.const import AddonBackupMode
|
from supervisor.addons.const import AddonBackupMode
|
||||||
from supervisor.addons.model import AddonModel
|
from supervisor.addons.model import AddonModel
|
||||||
from supervisor.config import CoreConfig
|
|
||||||
from supervisor.const import AddonBoot, AddonState, BusEvent
|
from supervisor.const import AddonBoot, AddonState, BusEvent
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.docker.addon import DockerAddon
|
from supervisor.docker.addon import DockerAddon
|
||||||
from supervisor.docker.const import ContainerState
|
from supervisor.docker.const import ContainerState
|
||||||
from supervisor.docker.manager import CommandReturn, DockerAPI
|
from supervisor.docker.manager import CommandReturn
|
||||||
from supervisor.docker.monitor import DockerContainerStateEvent
|
from supervisor.docker.monitor import DockerContainerStateEvent
|
||||||
from supervisor.exceptions import AddonsError, AddonsJobError, AudioUpdateError
|
from supervisor.exceptions import AddonsError, AddonsJobError, AudioUpdateError
|
||||||
from supervisor.hardware.helper import HwHelper
|
from supervisor.hardware.helper import HwHelper
|
||||||
@@ -864,14 +861,16 @@ async def test_addon_loads_wrong_image(
|
|||||||
|
|
||||||
container.remove.assert_called_with(force=True, v=True)
|
container.remove.assert_called_with(force=True, v=True)
|
||||||
# one for removing the addon, one for removing the addon builder
|
# one for removing the addon, one for removing the addon builder
|
||||||
assert coresys.docker.images.delete.call_count == 2
|
assert coresys.docker.images.remove.call_count == 2
|
||||||
|
|
||||||
assert coresys.docker.images.delete.call_args_list[0] == call(
|
assert coresys.docker.images.remove.call_args_list[0].kwargs == {
|
||||||
"local/aarch64-addon-ssh:latest", force=True
|
"image": "local/aarch64-addon-ssh:latest",
|
||||||
)
|
"force": True,
|
||||||
assert coresys.docker.images.delete.call_args_list[1] == call(
|
}
|
||||||
"local/aarch64-addon-ssh:9.2.1", force=True
|
assert coresys.docker.images.remove.call_args_list[1].kwargs == {
|
||||||
)
|
"image": "local/aarch64-addon-ssh:9.2.1",
|
||||||
|
"force": True,
|
||||||
|
}
|
||||||
mock_run_command.assert_called_once()
|
mock_run_command.assert_called_once()
|
||||||
assert mock_run_command.call_args.args[0] == "docker.io/library/docker"
|
assert mock_run_command.call_args.args[0] == "docker.io/library/docker"
|
||||||
assert mock_run_command.call_args.kwargs["version"] == "1.0.0-cli"
|
assert mock_run_command.call_args.kwargs["version"] == "1.0.0-cli"
|
||||||
@@ -895,9 +894,7 @@ async def test_addon_loads_missing_image(
|
|||||||
mock_amd64_arch_supported,
|
mock_amd64_arch_supported,
|
||||||
):
|
):
|
||||||
"""Test addon corrects a missing image on load."""
|
"""Test addon corrects a missing image on load."""
|
||||||
coresys.docker.images.inspect.side_effect = aiodocker.DockerError(
|
coresys.docker.images.get.side_effect = ImageNotFound("missing")
|
||||||
HTTPStatus.NOT_FOUND, {"message": "missing"}
|
|
||||||
)
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch("pathlib.Path.is_file", return_value=True),
|
patch("pathlib.Path.is_file", return_value=True),
|
||||||
@@ -929,51 +926,41 @@ async def test_addon_loads_missing_image(
|
|||||||
assert install_addon_ssh.image == "local/amd64-addon-ssh"
|
assert install_addon_ssh.image == "local/amd64-addon-ssh"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"pull_image_exc",
|
|
||||||
[APIError("error"), aiodocker.DockerError(400, {"message": "error"})],
|
|
||||||
)
|
|
||||||
@pytest.mark.usefixtures("container", "mock_amd64_arch_supported")
|
|
||||||
async def test_addon_load_succeeds_with_docker_errors(
|
async def test_addon_load_succeeds_with_docker_errors(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys,
|
||||||
install_addon_ssh: Addon,
|
install_addon_ssh: Addon,
|
||||||
|
container: MagicMock,
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
pull_image_exc: Exception,
|
mock_amd64_arch_supported,
|
||||||
):
|
):
|
||||||
"""Docker errors while building/pulling an image during load should not raise and fail setup."""
|
"""Docker errors while building/pulling an image during load should not raise and fail setup."""
|
||||||
# Build env invalid failure
|
# Build env invalid failure
|
||||||
coresys.docker.images.inspect.side_effect = aiodocker.DockerError(
|
coresys.docker.images.get.side_effect = ImageNotFound("missing")
|
||||||
HTTPStatus.NOT_FOUND, {"message": "missing"}
|
|
||||||
)
|
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
await install_addon_ssh.load()
|
await install_addon_ssh.load()
|
||||||
assert "Invalid build environment" in caplog.text
|
assert "Invalid build environment" in caplog.text
|
||||||
|
|
||||||
# Image build failure
|
# Image build failure
|
||||||
|
coresys.docker.images.build.side_effect = DockerException()
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
with (
|
with (
|
||||||
patch("pathlib.Path.is_file", return_value=True),
|
patch("pathlib.Path.is_file", return_value=True),
|
||||||
patch.object(
|
patch.object(
|
||||||
CoreConfig, "local_to_extern_path", return_value="/addon/path/on/host"
|
type(coresys.config),
|
||||||
),
|
"local_to_extern_path",
|
||||||
patch.object(
|
return_value="/addon/path/on/host",
|
||||||
DockerAPI,
|
|
||||||
"run_command",
|
|
||||||
return_value=MagicMock(exit_code=1, output=b"error"),
|
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
await install_addon_ssh.load()
|
await install_addon_ssh.load()
|
||||||
assert (
|
assert "Can't build local/amd64-addon-ssh:9.2.1" in caplog.text
|
||||||
"Can't build local/amd64-addon-ssh:9.2.1: Docker build failed for local/amd64-addon-ssh:9.2.1 (exit code 1). Build output:\nerror"
|
|
||||||
in caplog.text
|
|
||||||
)
|
|
||||||
|
|
||||||
# Image pull failure
|
# Image pull failure
|
||||||
install_addon_ssh.data["image"] = "test/amd64-addon-ssh"
|
install_addon_ssh.data["image"] = "test/amd64-addon-ssh"
|
||||||
|
coresys.docker.images.build.reset_mock(side_effect=True)
|
||||||
|
coresys.docker.pull_image.side_effect = DockerException()
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
with patch.object(DockerAPI, "pull_image", side_effect=pull_image_exc):
|
await install_addon_ssh.load()
|
||||||
await install_addon_ssh.load()
|
assert "Unknown error with test/amd64-addon-ssh:9.2.1" in caplog.text
|
||||||
assert "Can't install test/amd64-addon-ssh:9.2.1:" in caplog.text
|
|
||||||
|
|
||||||
|
|
||||||
async def test_addon_manual_only_boot(coresys: CoreSys, install_addon_example: Addon):
|
async def test_addon_manual_only_boot(coresys: CoreSys, install_addon_example: Addon):
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
"""Test addon build."""
|
"""Test addon build."""
|
||||||
|
|
||||||
import base64
|
|
||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import PropertyMock, patch
|
from unittest.mock import PropertyMock, patch
|
||||||
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
@@ -10,7 +7,6 @@ from awesomeversion import AwesomeVersion
|
|||||||
from supervisor.addons.addon import Addon
|
from supervisor.addons.addon import Addon
|
||||||
from supervisor.addons.build import AddonBuild
|
from supervisor.addons.build import AddonBuild
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.docker.const import DOCKER_HUB
|
|
||||||
|
|
||||||
from tests.common import is_in_list
|
from tests.common import is_in_list
|
||||||
|
|
||||||
@@ -33,7 +29,7 @@ async def test_platform_set(coresys: CoreSys, install_addon_ssh: Addon):
|
|||||||
),
|
),
|
||||||
):
|
):
|
||||||
args = await coresys.run_in_executor(
|
args = await coresys.run_in_executor(
|
||||||
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest", None
|
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest"
|
||||||
)
|
)
|
||||||
|
|
||||||
assert is_in_list(["--platform", "linux/amd64"], args["command"])
|
assert is_in_list(["--platform", "linux/amd64"], args["command"])
|
||||||
@@ -57,7 +53,7 @@ async def test_dockerfile_evaluation(coresys: CoreSys, install_addon_ssh: Addon)
|
|||||||
),
|
),
|
||||||
):
|
):
|
||||||
args = await coresys.run_in_executor(
|
args = await coresys.run_in_executor(
|
||||||
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest", None
|
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest"
|
||||||
)
|
)
|
||||||
|
|
||||||
assert is_in_list(["--file", "Dockerfile"], args["command"])
|
assert is_in_list(["--file", "Dockerfile"], args["command"])
|
||||||
@@ -85,7 +81,7 @@ async def test_dockerfile_evaluation_arch(coresys: CoreSys, install_addon_ssh: A
|
|||||||
),
|
),
|
||||||
):
|
):
|
||||||
args = await coresys.run_in_executor(
|
args = await coresys.run_in_executor(
|
||||||
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest", None
|
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest"
|
||||||
)
|
)
|
||||||
|
|
||||||
assert is_in_list(["--file", "Dockerfile.aarch64"], args["command"])
|
assert is_in_list(["--file", "Dockerfile.aarch64"], args["command"])
|
||||||
@@ -121,158 +117,3 @@ async def test_build_invalid(coresys: CoreSys, install_addon_ssh: Addon):
|
|||||||
),
|
),
|
||||||
):
|
):
|
||||||
assert not await build.is_valid()
|
assert not await build.is_valid()
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_config_no_registries(coresys: CoreSys, install_addon_ssh: Addon):
|
|
||||||
"""Test docker config generation when no registries configured."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
# No registries configured by default
|
|
||||||
assert build.get_docker_config_json() is None
|
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_config_no_matching_registry(
|
|
||||||
coresys: CoreSys, install_addon_ssh: Addon
|
|
||||||
):
|
|
||||||
"""Test docker config generation when registry doesn't match base image."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
# Configure a registry that doesn't match the base image
|
|
||||||
# pylint: disable-next=protected-access
|
|
||||||
coresys.docker.config._data["registries"] = {
|
|
||||||
"some.other.registry": {"username": "user", "password": "pass"}
|
|
||||||
}
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
|
||||||
),
|
|
||||||
):
|
|
||||||
# Base image is ghcr.io/home-assistant/... which doesn't match
|
|
||||||
assert build.get_docker_config_json() is None
|
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_config_matching_registry(
|
|
||||||
coresys: CoreSys, install_addon_ssh: Addon
|
|
||||||
):
|
|
||||||
"""Test docker config generation when registry matches base image."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
# Configure ghcr.io registry which matches the default base image
|
|
||||||
# pylint: disable-next=protected-access
|
|
||||||
coresys.docker.config._data["registries"] = {
|
|
||||||
"ghcr.io": {"username": "testuser", "password": "testpass"}
|
|
||||||
}
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
|
||||||
),
|
|
||||||
):
|
|
||||||
config_json = build.get_docker_config_json()
|
|
||||||
assert config_json is not None
|
|
||||||
|
|
||||||
config = json.loads(config_json)
|
|
||||||
assert "auths" in config
|
|
||||||
assert "ghcr.io" in config["auths"]
|
|
||||||
|
|
||||||
# Verify base64-encoded credentials
|
|
||||||
expected_auth = base64.b64encode(b"testuser:testpass").decode()
|
|
||||||
assert config["auths"]["ghcr.io"]["auth"] == expected_auth
|
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_config_docker_hub(coresys: CoreSys, install_addon_ssh: Addon):
|
|
||||||
"""Test docker config generation for Docker Hub registry."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
# Configure Docker Hub registry
|
|
||||||
# pylint: disable-next=protected-access
|
|
||||||
coresys.docker.config._data["registries"] = {
|
|
||||||
DOCKER_HUB: {"username": "hubuser", "password": "hubpass"}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Mock base_image to return a Docker Hub image (no registry prefix)
|
|
||||||
with patch.object(
|
|
||||||
type(build),
|
|
||||||
"base_image",
|
|
||||||
new=PropertyMock(return_value="library/alpine:latest"),
|
|
||||||
):
|
|
||||||
config_json = build.get_docker_config_json()
|
|
||||||
assert config_json is not None
|
|
||||||
|
|
||||||
config = json.loads(config_json)
|
|
||||||
# Docker Hub uses special URL as key
|
|
||||||
assert "https://index.docker.io/v1/" in config["auths"]
|
|
||||||
|
|
||||||
expected_auth = base64.b64encode(b"hubuser:hubpass").decode()
|
|
||||||
assert config["auths"]["https://index.docker.io/v1/"]["auth"] == expected_auth
|
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_args_with_config_path(coresys: CoreSys, install_addon_ssh: Addon):
|
|
||||||
"""Test docker args include config volume when path provided."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.config),
|
|
||||||
"local_to_extern_path",
|
|
||||||
side_effect=lambda p: f"/extern{p}",
|
|
||||||
),
|
|
||||||
):
|
|
||||||
config_path = Path("/data/supervisor/tmp/config.json")
|
|
||||||
args = await coresys.run_in_executor(
|
|
||||||
build.get_docker_args,
|
|
||||||
AwesomeVersion("latest"),
|
|
||||||
"test-image:latest",
|
|
||||||
config_path,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Check that config is mounted
|
|
||||||
assert "/extern/data/supervisor/tmp/config.json" in args["volumes"]
|
|
||||||
assert (
|
|
||||||
args["volumes"]["/extern/data/supervisor/tmp/config.json"]["bind"]
|
|
||||||
== "/root/.docker/config.json"
|
|
||||||
)
|
|
||||||
assert args["volumes"]["/extern/data/supervisor/tmp/config.json"]["mode"] == "ro"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_args_without_config_path(
|
|
||||||
coresys: CoreSys, install_addon_ssh: Addon
|
|
||||||
):
|
|
||||||
"""Test docker args don't include config volume when no path provided."""
|
|
||||||
build = await AddonBuild(coresys, install_addon_ssh).load_config()
|
|
||||||
|
|
||||||
with (
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "supported", new=PropertyMock(return_value=["amd64"])
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.arch), "default", new=PropertyMock(return_value="amd64")
|
|
||||||
),
|
|
||||||
patch.object(
|
|
||||||
type(coresys.config),
|
|
||||||
"local_to_extern_path",
|
|
||||||
return_value="/addon/path/on/host",
|
|
||||||
),
|
|
||||||
):
|
|
||||||
args = await coresys.run_in_executor(
|
|
||||||
build.get_docker_args, AwesomeVersion("latest"), "test-image:latest", None
|
|
||||||
)
|
|
||||||
|
|
||||||
# Only docker socket and addon path should be mounted
|
|
||||||
assert len(args["volumes"]) == 2
|
|
||||||
# Verify no docker config mount
|
|
||||||
for bind in args["volumes"].values():
|
|
||||||
assert bind["bind"] != "/root/.docker/config.json"
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import asyncio
|
|||||||
from collections.abc import AsyncGenerator, Generator
|
from collections.abc import AsyncGenerator, Generator
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, call, patch
|
from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, patch
|
||||||
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
import pytest
|
import pytest
|
||||||
@@ -514,13 +514,19 @@ async def test_shared_image_kept_on_uninstall(
|
|||||||
latest = f"{install_addon_example.image}:latest"
|
latest = f"{install_addon_example.image}:latest"
|
||||||
|
|
||||||
await coresys.addons.uninstall("local_example2")
|
await coresys.addons.uninstall("local_example2")
|
||||||
coresys.docker.images.delete.assert_not_called()
|
coresys.docker.images.remove.assert_not_called()
|
||||||
assert not coresys.addons.get("local_example2", local_only=True)
|
assert not coresys.addons.get("local_example2", local_only=True)
|
||||||
|
|
||||||
await coresys.addons.uninstall("local_example")
|
await coresys.addons.uninstall("local_example")
|
||||||
assert coresys.docker.images.delete.call_count == 2
|
assert coresys.docker.images.remove.call_count == 2
|
||||||
assert coresys.docker.images.delete.call_args_list[0] == call(latest, force=True)
|
assert coresys.docker.images.remove.call_args_list[0].kwargs == {
|
||||||
assert coresys.docker.images.delete.call_args_list[1] == call(image, force=True)
|
"image": latest,
|
||||||
|
"force": True,
|
||||||
|
}
|
||||||
|
assert coresys.docker.images.remove.call_args_list[1].kwargs == {
|
||||||
|
"image": image,
|
||||||
|
"force": True,
|
||||||
|
}
|
||||||
assert not coresys.addons.get("local_example", local_only=True)
|
assert not coresys.addons.get("local_example", local_only=True)
|
||||||
|
|
||||||
|
|
||||||
@@ -548,17 +554,19 @@ async def test_shared_image_kept_on_update(
|
|||||||
assert example_2.version == "1.2.0"
|
assert example_2.version == "1.2.0"
|
||||||
assert install_addon_example_image.version == "1.2.0"
|
assert install_addon_example_image.version == "1.2.0"
|
||||||
|
|
||||||
image_new = {"Id": "image_new", "RepoTags": ["image_new:latest"]}
|
image_new = MagicMock()
|
||||||
image_old = {"Id": "image_old", "RepoTags": ["image_old:latest"]}
|
image_new.id = "image_new"
|
||||||
docker.images.inspect.side_effect = [image_new, image_old]
|
image_old = MagicMock()
|
||||||
|
image_old.id = "image_old"
|
||||||
|
docker.images.get.side_effect = [image_new, image_old]
|
||||||
docker.images.list.return_value = [image_new, image_old]
|
docker.images.list.return_value = [image_new, image_old]
|
||||||
|
|
||||||
with patch.object(DockerAPI, "pull_image", return_value=image_new):
|
with patch.object(DockerAPI, "pull_image", return_value=image_new):
|
||||||
await coresys.addons.update("local_example2")
|
await coresys.addons.update("local_example2")
|
||||||
docker.images.delete.assert_not_called()
|
docker.images.remove.assert_not_called()
|
||||||
assert example_2.version == "1.3.0"
|
assert example_2.version == "1.3.0"
|
||||||
|
|
||||||
docker.images.inspect.side_effect = [image_new]
|
docker.images.get.side_effect = [image_new]
|
||||||
await coresys.addons.update("local_example_image")
|
await coresys.addons.update("local_example_image")
|
||||||
docker.images.delete.assert_called_once_with("image_old", force=True)
|
docker.images.remove.assert_called_once_with("image_old", force=True)
|
||||||
assert install_addon_example_image.version == "1.3.0"
|
assert install_addon_example_image.version == "1.3.0"
|
||||||
|
|||||||
@@ -1 +1,95 @@
|
|||||||
"""Test for API calls."""
|
"""Test for API calls."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, MagicMock
|
||||||
|
|
||||||
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
|
from supervisor.coresys import CoreSys
|
||||||
|
from supervisor.host.const import LogFormat
|
||||||
|
|
||||||
|
DEFAULT_LOG_RANGE = "entries=:-99:100"
|
||||||
|
DEFAULT_LOG_RANGE_FOLLOW = "entries=:-99:18446744073709551615"
|
||||||
|
|
||||||
|
|
||||||
|
async def common_test_api_advanced_logs(
|
||||||
|
path_prefix: str,
|
||||||
|
syslog_identifier: str,
|
||||||
|
api_client: TestClient,
|
||||||
|
journald_logs: MagicMock,
|
||||||
|
coresys: CoreSys,
|
||||||
|
os_available: None,
|
||||||
|
):
|
||||||
|
"""Template for tests of endpoints using advanced logs."""
|
||||||
|
resp = await api_client.get(f"{path_prefix}/logs")
|
||||||
|
assert resp.status == 200
|
||||||
|
assert resp.content_type == "text/plain"
|
||||||
|
|
||||||
|
journald_logs.assert_called_once_with(
|
||||||
|
params={"SYSLOG_IDENTIFIER": syslog_identifier},
|
||||||
|
range_header=DEFAULT_LOG_RANGE,
|
||||||
|
accept=LogFormat.JOURNAL,
|
||||||
|
)
|
||||||
|
|
||||||
|
journald_logs.reset_mock()
|
||||||
|
|
||||||
|
resp = await api_client.get(f"{path_prefix}/logs/follow")
|
||||||
|
assert resp.status == 200
|
||||||
|
assert resp.content_type == "text/plain"
|
||||||
|
|
||||||
|
journald_logs.assert_called_once_with(
|
||||||
|
params={"SYSLOG_IDENTIFIER": syslog_identifier, "follow": ""},
|
||||||
|
range_header=DEFAULT_LOG_RANGE_FOLLOW,
|
||||||
|
accept=LogFormat.JOURNAL,
|
||||||
|
)
|
||||||
|
|
||||||
|
journald_logs.reset_mock()
|
||||||
|
|
||||||
|
mock_response = MagicMock()
|
||||||
|
mock_response.text = AsyncMock(
|
||||||
|
return_value='{"CONTAINER_LOG_EPOCH": "12345"}\n{"CONTAINER_LOG_EPOCH": "12345"}\n'
|
||||||
|
)
|
||||||
|
journald_logs.return_value.__aenter__.return_value = mock_response
|
||||||
|
|
||||||
|
resp = await api_client.get(f"{path_prefix}/logs/latest")
|
||||||
|
assert resp.status == 200
|
||||||
|
|
||||||
|
assert journald_logs.call_count == 2
|
||||||
|
|
||||||
|
# Check the first call for getting epoch
|
||||||
|
epoch_call = journald_logs.call_args_list[0]
|
||||||
|
assert epoch_call[1]["params"] == {"CONTAINER_NAME": syslog_identifier}
|
||||||
|
assert epoch_call[1]["range_header"] == "entries=:-1:2"
|
||||||
|
|
||||||
|
# Check the second call for getting logs with the epoch
|
||||||
|
logs_call = journald_logs.call_args_list[1]
|
||||||
|
assert logs_call[1]["params"]["SYSLOG_IDENTIFIER"] == syslog_identifier
|
||||||
|
assert logs_call[1]["params"]["CONTAINER_LOG_EPOCH"] == "12345"
|
||||||
|
assert logs_call[1]["range_header"] == "entries=:0:18446744073709551615"
|
||||||
|
|
||||||
|
journald_logs.reset_mock()
|
||||||
|
|
||||||
|
resp = await api_client.get(f"{path_prefix}/logs/boots/0")
|
||||||
|
assert resp.status == 200
|
||||||
|
assert resp.content_type == "text/plain"
|
||||||
|
|
||||||
|
journald_logs.assert_called_once_with(
|
||||||
|
params={"SYSLOG_IDENTIFIER": syslog_identifier, "_BOOT_ID": "ccc"},
|
||||||
|
range_header=DEFAULT_LOG_RANGE,
|
||||||
|
accept=LogFormat.JOURNAL,
|
||||||
|
)
|
||||||
|
|
||||||
|
journald_logs.reset_mock()
|
||||||
|
|
||||||
|
resp = await api_client.get(f"{path_prefix}/logs/boots/0/follow")
|
||||||
|
assert resp.status == 200
|
||||||
|
assert resp.content_type == "text/plain"
|
||||||
|
|
||||||
|
journald_logs.assert_called_once_with(
|
||||||
|
params={
|
||||||
|
"SYSLOG_IDENTIFIER": syslog_identifier,
|
||||||
|
"_BOOT_ID": "ccc",
|
||||||
|
"follow": "",
|
||||||
|
},
|
||||||
|
range_header=DEFAULT_LOG_RANGE_FOLLOW,
|
||||||
|
accept=LogFormat.JOURNAL,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,149 +0,0 @@
|
|||||||
"""Fixtures for API tests."""
|
|
||||||
|
|
||||||
from collections.abc import Awaitable, Callable
|
|
||||||
from unittest.mock import ANY, AsyncMock, MagicMock
|
|
||||||
|
|
||||||
from aiohttp.test_utils import TestClient
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from supervisor.coresys import CoreSys
|
|
||||||
from supervisor.host.const import LogFormat, LogFormatter
|
|
||||||
|
|
||||||
DEFAULT_LOG_RANGE = "entries=:-99:100"
|
|
||||||
DEFAULT_LOG_RANGE_FOLLOW = "entries=:-99:18446744073709551615"
|
|
||||||
|
|
||||||
|
|
||||||
async def _common_test_api_advanced_logs(
|
|
||||||
path_prefix: str,
|
|
||||||
syslog_identifier: str,
|
|
||||||
api_client: TestClient,
|
|
||||||
journald_logs: MagicMock,
|
|
||||||
coresys: CoreSys,
|
|
||||||
os_available: None,
|
|
||||||
journal_logs_reader: MagicMock,
|
|
||||||
):
|
|
||||||
"""Template for tests of endpoints using advanced logs."""
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs")
|
|
||||||
assert resp.status == 200
|
|
||||||
assert resp.content_type == "text/plain"
|
|
||||||
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={"SYSLOG_IDENTIFIER": syslog_identifier},
|
|
||||||
range_header=DEFAULT_LOG_RANGE,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.PLAIN, False)
|
|
||||||
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
journal_logs_reader.reset_mock()
|
|
||||||
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs?no_colors")
|
|
||||||
assert resp.status == 200
|
|
||||||
assert resp.content_type == "text/plain"
|
|
||||||
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={"SYSLOG_IDENTIFIER": syslog_identifier},
|
|
||||||
range_header=DEFAULT_LOG_RANGE,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.PLAIN, True)
|
|
||||||
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
journal_logs_reader.reset_mock()
|
|
||||||
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs/follow")
|
|
||||||
assert resp.status == 200
|
|
||||||
assert resp.content_type == "text/plain"
|
|
||||||
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={"SYSLOG_IDENTIFIER": syslog_identifier, "follow": ""},
|
|
||||||
range_header=DEFAULT_LOG_RANGE_FOLLOW,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.PLAIN, False)
|
|
||||||
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
journal_logs_reader.reset_mock()
|
|
||||||
|
|
||||||
mock_response = MagicMock()
|
|
||||||
mock_response.text = AsyncMock(
|
|
||||||
return_value='{"CONTAINER_LOG_EPOCH": "12345"}\n{"CONTAINER_LOG_EPOCH": "12345"}\n'
|
|
||||||
)
|
|
||||||
journald_logs.return_value.__aenter__.return_value = mock_response
|
|
||||||
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs/latest")
|
|
||||||
assert resp.status == 200
|
|
||||||
|
|
||||||
assert journald_logs.call_count == 2
|
|
||||||
|
|
||||||
# Check the first call for getting epoch
|
|
||||||
epoch_call = journald_logs.call_args_list[0]
|
|
||||||
assert epoch_call[1]["params"] == {"CONTAINER_NAME": syslog_identifier}
|
|
||||||
assert epoch_call[1]["range_header"] == "entries=:-1:2"
|
|
||||||
|
|
||||||
# Check the second call for getting logs with the epoch
|
|
||||||
logs_call = journald_logs.call_args_list[1]
|
|
||||||
assert logs_call[1]["params"]["SYSLOG_IDENTIFIER"] == syslog_identifier
|
|
||||||
assert logs_call[1]["params"]["CONTAINER_LOG_EPOCH"] == "12345"
|
|
||||||
assert logs_call[1]["range_header"] == "entries=:0:18446744073709551615"
|
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.PLAIN, True)
|
|
||||||
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
journal_logs_reader.reset_mock()
|
|
||||||
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs/boots/0")
|
|
||||||
assert resp.status == 200
|
|
||||||
assert resp.content_type == "text/plain"
|
|
||||||
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={"SYSLOG_IDENTIFIER": syslog_identifier, "_BOOT_ID": "ccc"},
|
|
||||||
range_header=DEFAULT_LOG_RANGE,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
|
|
||||||
resp = await api_client.get(f"{path_prefix}/logs/boots/0/follow")
|
|
||||||
assert resp.status == 200
|
|
||||||
assert resp.content_type == "text/plain"
|
|
||||||
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={
|
|
||||||
"SYSLOG_IDENTIFIER": syslog_identifier,
|
|
||||||
"_BOOT_ID": "ccc",
|
|
||||||
"follow": "",
|
|
||||||
},
|
|
||||||
range_header=DEFAULT_LOG_RANGE_FOLLOW,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
async def advanced_logs_tester(
|
|
||||||
api_client: TestClient,
|
|
||||||
journald_logs: MagicMock,
|
|
||||||
coresys: CoreSys,
|
|
||||||
os_available,
|
|
||||||
journal_logs_reader: MagicMock,
|
|
||||||
) -> Callable[[str, str], Awaitable[None]]:
|
|
||||||
"""Fixture that returns a function to test advanced logs endpoints.
|
|
||||||
|
|
||||||
This allows tests to avoid explicitly passing all the required fixtures.
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
async def test_my_logs(advanced_logs_tester):
|
|
||||||
await advanced_logs_tester("/path/prefix", "syslog_identifier")
|
|
||||||
"""
|
|
||||||
|
|
||||||
async def test_logs(path_prefix: str, syslog_identifier: str):
|
|
||||||
await _common_test_api_advanced_logs(
|
|
||||||
path_prefix,
|
|
||||||
syslog_identifier,
|
|
||||||
api_client,
|
|
||||||
journald_logs,
|
|
||||||
coresys,
|
|
||||||
os_available,
|
|
||||||
journal_logs_reader,
|
|
||||||
)
|
|
||||||
|
|
||||||
return test_logs
|
|
||||||
@@ -20,6 +20,7 @@ from supervisor.exceptions import HassioError
|
|||||||
from supervisor.store.repository import Repository
|
from supervisor.store.repository import Repository
|
||||||
|
|
||||||
from ..const import TEST_ADDON_SLUG
|
from ..const import TEST_ADDON_SLUG
|
||||||
|
from . import common_test_api_advanced_logs
|
||||||
|
|
||||||
|
|
||||||
def _create_test_event(name: str, state: ContainerState) -> DockerContainerStateEvent:
|
def _create_test_event(name: str, state: ContainerState) -> DockerContainerStateEvent:
|
||||||
@@ -71,11 +72,21 @@ async def test_addons_info_not_installed(
|
|||||||
|
|
||||||
|
|
||||||
async def test_api_addon_logs(
|
async def test_api_addon_logs(
|
||||||
advanced_logs_tester,
|
api_client: TestClient,
|
||||||
|
journald_logs: MagicMock,
|
||||||
|
coresys: CoreSys,
|
||||||
|
os_available,
|
||||||
install_addon_ssh: Addon,
|
install_addon_ssh: Addon,
|
||||||
):
|
):
|
||||||
"""Test addon logs."""
|
"""Test addon logs."""
|
||||||
await advanced_logs_tester("/addons/local_ssh", "addon_local_ssh")
|
await common_test_api_advanced_logs(
|
||||||
|
"/addons/local_ssh",
|
||||||
|
"addon_local_ssh",
|
||||||
|
api_client,
|
||||||
|
journald_logs,
|
||||||
|
coresys,
|
||||||
|
os_available,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_api_addon_logs_not_installed(api_client: TestClient):
|
async def test_api_addon_logs_not_installed(api_client: TestClient):
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
"""Test audio api."""
|
"""Test audio api."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
async def test_api_audio_logs(advanced_logs_tester) -> None:
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
|
from supervisor.coresys import CoreSys
|
||||||
|
|
||||||
|
from tests.api import common_test_api_advanced_logs
|
||||||
|
|
||||||
|
|
||||||
|
async def test_api_audio_logs(
|
||||||
|
api_client: TestClient, journald_logs: MagicMock, coresys: CoreSys, os_available
|
||||||
|
):
|
||||||
"""Test audio logs."""
|
"""Test audio logs."""
|
||||||
await advanced_logs_tester("/audio", "hassio_audio")
|
await common_test_api_advanced_logs(
|
||||||
|
"/audio", "hassio_audio", api_client, journald_logs, coresys, os_available
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
"""Test DNS API."""
|
"""Test DNS API."""
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from aiohttp.test_utils import TestClient
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.dbus.resolved import Resolved
|
from supervisor.dbus.resolved import Resolved
|
||||||
|
|
||||||
|
from tests.api import common_test_api_advanced_logs
|
||||||
from tests.dbus_service_mocks.base import DBusServiceMock
|
from tests.dbus_service_mocks.base import DBusServiceMock
|
||||||
from tests.dbus_service_mocks.resolved import Resolved as ResolvedService
|
from tests.dbus_service_mocks.resolved import Resolved as ResolvedService
|
||||||
|
|
||||||
@@ -65,6 +66,15 @@ async def test_options(api_client: TestClient, coresys: CoreSys):
|
|||||||
restart.assert_called_once()
|
restart.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
async def test_api_dns_logs(advanced_logs_tester):
|
async def test_api_dns_logs(
|
||||||
|
api_client: TestClient, journald_logs: MagicMock, coresys: CoreSys, os_available
|
||||||
|
):
|
||||||
"""Test dns logs."""
|
"""Test dns logs."""
|
||||||
await advanced_logs_tester("/dns", "hassio_dns")
|
await common_test_api_advanced_logs(
|
||||||
|
"/dns",
|
||||||
|
"hassio_dns",
|
||||||
|
api_client,
|
||||||
|
journald_logs,
|
||||||
|
coresys,
|
||||||
|
os_available,
|
||||||
|
)
|
||||||
|
|||||||
@@ -18,18 +18,26 @@ from supervisor.homeassistant.const import WSEvent
|
|||||||
from supervisor.homeassistant.core import HomeAssistantCore
|
from supervisor.homeassistant.core import HomeAssistantCore
|
||||||
from supervisor.homeassistant.module import HomeAssistant
|
from supervisor.homeassistant.module import HomeAssistant
|
||||||
|
|
||||||
from tests.common import AsyncIterator, load_json_fixture
|
from tests.api import common_test_api_advanced_logs
|
||||||
|
from tests.common import load_json_fixture
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("legacy_route", [True, False])
|
@pytest.mark.parametrize("legacy_route", [True, False])
|
||||||
async def test_api_core_logs(
|
async def test_api_core_logs(
|
||||||
advanced_logs_tester: AsyncMock,
|
api_client: TestClient,
|
||||||
|
journald_logs: MagicMock,
|
||||||
|
coresys: CoreSys,
|
||||||
|
os_available,
|
||||||
legacy_route: bool,
|
legacy_route: bool,
|
||||||
):
|
):
|
||||||
"""Test core logs."""
|
"""Test core logs."""
|
||||||
await advanced_logs_tester(
|
await common_test_api_advanced_logs(
|
||||||
f"/{'homeassistant' if legacy_route else 'core'}",
|
f"/{'homeassistant' if legacy_route else 'core'}",
|
||||||
"homeassistant",
|
"homeassistant",
|
||||||
|
api_client,
|
||||||
|
journald_logs,
|
||||||
|
coresys,
|
||||||
|
os_available,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -275,9 +283,9 @@ async def test_api_progress_updates_home_assistant_update(
|
|||||||
"""Test progress updates sent to Home Assistant for updates."""
|
"""Test progress updates sent to Home Assistant for updates."""
|
||||||
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
coresys.docker.docker.api.pull.return_value = load_json_fixture(
|
||||||
logs = load_json_fixture("docker_pull_image_log.json")
|
"docker_pull_image_log.json"
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
)
|
||||||
coresys.homeassistant.version = AwesomeVersion("2025.8.0")
|
coresys.homeassistant.version = AwesomeVersion("2025.8.0")
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
|||||||
@@ -272,7 +272,7 @@ async def test_advaced_logs_query_parameters(
|
|||||||
range_header=DEFAULT_RANGE,
|
range_header=DEFAULT_RANGE,
|
||||||
accept=LogFormat.JOURNAL,
|
accept=LogFormat.JOURNAL,
|
||||||
)
|
)
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE, False)
|
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE)
|
||||||
|
|
||||||
journal_logs_reader.reset_mock()
|
journal_logs_reader.reset_mock()
|
||||||
journald_logs.reset_mock()
|
journald_logs.reset_mock()
|
||||||
@@ -290,19 +290,7 @@ async def test_advaced_logs_query_parameters(
|
|||||||
range_header="entries=:-52:53",
|
range_header="entries=:-52:53",
|
||||||
accept=LogFormat.JOURNAL,
|
accept=LogFormat.JOURNAL,
|
||||||
)
|
)
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE, False)
|
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE)
|
||||||
|
|
||||||
journal_logs_reader.reset_mock()
|
|
||||||
journald_logs.reset_mock()
|
|
||||||
|
|
||||||
# Check no_colors query parameter
|
|
||||||
await api_client.get("/host/logs?no_colors")
|
|
||||||
journald_logs.assert_called_once_with(
|
|
||||||
params={"SYSLOG_IDENTIFIER": coresys.host.logs.default_identifiers},
|
|
||||||
range_header=DEFAULT_RANGE,
|
|
||||||
accept=LogFormat.JOURNAL,
|
|
||||||
)
|
|
||||||
journal_logs_reader.assert_called_with(ANY, LogFormatter.VERBOSE, True)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_advanced_logs_boot_id_offset(
|
async def test_advanced_logs_boot_id_offset(
|
||||||
@@ -355,24 +343,24 @@ async def test_advanced_logs_formatters(
|
|||||||
"""Test advanced logs formatters varying on Accept header."""
|
"""Test advanced logs formatters varying on Accept header."""
|
||||||
|
|
||||||
await api_client.get("/host/logs")
|
await api_client.get("/host/logs")
|
||||||
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE, False)
|
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE)
|
||||||
|
|
||||||
journal_logs_reader.reset_mock()
|
journal_logs_reader.reset_mock()
|
||||||
|
|
||||||
headers = {"Accept": "text/x-log"}
|
headers = {"Accept": "text/x-log"}
|
||||||
await api_client.get("/host/logs", headers=headers)
|
await api_client.get("/host/logs", headers=headers)
|
||||||
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE, False)
|
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE)
|
||||||
|
|
||||||
journal_logs_reader.reset_mock()
|
journal_logs_reader.reset_mock()
|
||||||
|
|
||||||
await api_client.get("/host/logs/identifiers/test")
|
await api_client.get("/host/logs/identifiers/test")
|
||||||
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.PLAIN, False)
|
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.PLAIN)
|
||||||
|
|
||||||
journal_logs_reader.reset_mock()
|
journal_logs_reader.reset_mock()
|
||||||
|
|
||||||
headers = {"Accept": "text/x-log"}
|
headers = {"Accept": "text/x-log"}
|
||||||
await api_client.get("/host/logs/identifiers/test", headers=headers)
|
await api_client.get("/host/logs/identifiers/test", headers=headers)
|
||||||
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE, False)
|
journal_logs_reader.assert_called_once_with(ANY, LogFormatter.VERBOSE)
|
||||||
|
|
||||||
|
|
||||||
async def test_advanced_logs_errors(coresys: CoreSys, api_client: TestClient):
|
async def test_advanced_logs_errors(coresys: CoreSys, api_client: TestClient):
|
||||||
|
|||||||
@@ -1,28 +1,12 @@
|
|||||||
"""Test ingress API."""
|
"""Test ingress API."""
|
||||||
|
|
||||||
from collections.abc import AsyncGenerator
|
from unittest.mock import AsyncMock, patch
|
||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
|
||||||
|
|
||||||
import aiohttp
|
from aiohttp.test_utils import TestClient
|
||||||
from aiohttp import hdrs, web
|
|
||||||
from aiohttp.test_utils import TestClient, TestServer
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from supervisor.addons.addon import Addon
|
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="real_websession")
|
|
||||||
async def fixture_real_websession(
|
|
||||||
coresys: CoreSys,
|
|
||||||
) -> AsyncGenerator[aiohttp.ClientSession]:
|
|
||||||
"""Fixture for real aiohttp ClientSession for ingress proxy tests."""
|
|
||||||
session = aiohttp.ClientSession()
|
|
||||||
coresys._websession = session # pylint: disable=W0212
|
|
||||||
yield session
|
|
||||||
await session.close()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_validate_session(api_client: TestClient, coresys: CoreSys):
|
async def test_validate_session(api_client: TestClient, coresys: CoreSys):
|
||||||
"""Test validating ingress session."""
|
"""Test validating ingress session."""
|
||||||
with patch("aiohttp.web_request.BaseRequest.__getitem__", return_value=None):
|
with patch("aiohttp.web_request.BaseRequest.__getitem__", return_value=None):
|
||||||
@@ -102,126 +86,3 @@ async def test_validate_session_with_user_id(
|
|||||||
assert (
|
assert (
|
||||||
coresys.ingress.get_session_data(session).user.display_name == "Some Name"
|
coresys.ingress.get_session_data(session).user.display_name == "Some Name"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_ingress_proxy_no_content_type_for_empty_body_responses(
|
|
||||||
api_client: TestClient, coresys: CoreSys, real_websession: aiohttp.ClientSession
|
|
||||||
):
|
|
||||||
"""Test that empty body responses don't get Content-Type header."""
|
|
||||||
|
|
||||||
# Create a mock add-on backend server that returns various status codes
|
|
||||||
async def mock_addon_handler(request: web.Request) -> web.Response:
|
|
||||||
"""Mock add-on handler that returns different status codes based on path."""
|
|
||||||
path = request.path
|
|
||||||
|
|
||||||
if path == "/204":
|
|
||||||
# 204 No Content - should not have Content-Type
|
|
||||||
return web.Response(status=204)
|
|
||||||
elif path == "/304":
|
|
||||||
# 304 Not Modified - should not have Content-Type
|
|
||||||
return web.Response(status=304)
|
|
||||||
elif path == "/100":
|
|
||||||
# 100 Continue - should not have Content-Type
|
|
||||||
return web.Response(status=100)
|
|
||||||
elif path == "/head":
|
|
||||||
# HEAD request - should have Content-Type (same as GET would)
|
|
||||||
return web.Response(body=b"test", content_type="text/html")
|
|
||||||
elif path == "/200":
|
|
||||||
# 200 OK with body - should have Content-Type
|
|
||||||
return web.Response(body=b"test content", content_type="text/plain")
|
|
||||||
elif path == "/200-no-content-type":
|
|
||||||
# 200 OK without explicit Content-Type - should get default
|
|
||||||
return web.Response(body=b"test content")
|
|
||||||
elif path == "/200-json":
|
|
||||||
# 200 OK with JSON - should preserve Content-Type
|
|
||||||
return web.Response(
|
|
||||||
body=b'{"key": "value"}', content_type="application/json"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
return web.Response(body=b"default", content_type="text/html")
|
|
||||||
|
|
||||||
# Create test server for mock add-on
|
|
||||||
app = web.Application()
|
|
||||||
app.router.add_route("*", "/{tail:.*}", mock_addon_handler)
|
|
||||||
addon_server = TestServer(app)
|
|
||||||
await addon_server.start_server()
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Create ingress session
|
|
||||||
resp = await api_client.post("/ingress/session")
|
|
||||||
result = await resp.json()
|
|
||||||
session = result["data"]["session"]
|
|
||||||
|
|
||||||
# Create a mock add-on
|
|
||||||
mock_addon = MagicMock(spec=Addon)
|
|
||||||
mock_addon.slug = "test_addon"
|
|
||||||
mock_addon.ip_address = addon_server.host
|
|
||||||
mock_addon.ingress_port = addon_server.port
|
|
||||||
mock_addon.ingress_stream = False
|
|
||||||
|
|
||||||
# Generate an ingress token and register the add-on
|
|
||||||
ingress_token = coresys.ingress.create_session()
|
|
||||||
with patch.object(coresys.ingress, "get", return_value=mock_addon):
|
|
||||||
# Test 204 No Content - should NOT have Content-Type
|
|
||||||
resp = await api_client.get(
|
|
||||||
f"/ingress/{ingress_token}/204",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 204
|
|
||||||
assert hdrs.CONTENT_TYPE not in resp.headers
|
|
||||||
|
|
||||||
# Test 304 Not Modified - should NOT have Content-Type
|
|
||||||
resp = await api_client.get(
|
|
||||||
f"/ingress/{ingress_token}/304",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 304
|
|
||||||
assert hdrs.CONTENT_TYPE not in resp.headers
|
|
||||||
|
|
||||||
# Test HEAD request - SHOULD have Content-Type (same as GET)
|
|
||||||
# per RFC 9110: HEAD should return same headers as GET
|
|
||||||
resp = await api_client.head(
|
|
||||||
f"/ingress/{ingress_token}/head",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 200
|
|
||||||
assert hdrs.CONTENT_TYPE in resp.headers
|
|
||||||
assert "text/html" in resp.headers[hdrs.CONTENT_TYPE]
|
|
||||||
# Body should be empty for HEAD
|
|
||||||
body = await resp.read()
|
|
||||||
assert body == b""
|
|
||||||
|
|
||||||
# Test 200 OK with body - SHOULD have Content-Type
|
|
||||||
resp = await api_client.get(
|
|
||||||
f"/ingress/{ingress_token}/200",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 200
|
|
||||||
assert hdrs.CONTENT_TYPE in resp.headers
|
|
||||||
assert resp.headers[hdrs.CONTENT_TYPE] == "text/plain"
|
|
||||||
body = await resp.read()
|
|
||||||
assert body == b"test content"
|
|
||||||
|
|
||||||
# Test 200 OK without explicit Content-Type - SHOULD get default
|
|
||||||
resp = await api_client.get(
|
|
||||||
f"/ingress/{ingress_token}/200-no-content-type",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 200
|
|
||||||
assert hdrs.CONTENT_TYPE in resp.headers
|
|
||||||
# Should get application/octet-stream as default from aiohttp ClientResponse
|
|
||||||
assert "application/octet-stream" in resp.headers[hdrs.CONTENT_TYPE]
|
|
||||||
|
|
||||||
# Test 200 OK with JSON - SHOULD preserve Content-Type
|
|
||||||
resp = await api_client.get(
|
|
||||||
f"/ingress/{ingress_token}/200-json",
|
|
||||||
cookies={"ingress_session": session},
|
|
||||||
)
|
|
||||||
assert resp.status == 200
|
|
||||||
assert hdrs.CONTENT_TYPE in resp.headers
|
|
||||||
assert "application/json" in resp.headers[hdrs.CONTENT_TYPE]
|
|
||||||
body = await resp.read()
|
|
||||||
assert body == b'{"key": "value"}'
|
|
||||||
|
|
||||||
finally:
|
|
||||||
await addon_server.close()
|
|
||||||
|
|||||||
@@ -1,6 +1,23 @@
|
|||||||
"""Test multicast api."""
|
"""Test multicast api."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
async def test_api_multicast_logs(advanced_logs_tester):
|
from aiohttp.test_utils import TestClient
|
||||||
|
|
||||||
|
from supervisor.coresys import CoreSys
|
||||||
|
|
||||||
|
from tests.api import common_test_api_advanced_logs
|
||||||
|
|
||||||
|
|
||||||
|
async def test_api_multicast_logs(
|
||||||
|
api_client: TestClient, journald_logs: MagicMock, coresys: CoreSys, os_available
|
||||||
|
):
|
||||||
"""Test multicast logs."""
|
"""Test multicast logs."""
|
||||||
await advanced_logs_tester("/multicast", "hassio_multicast")
|
await common_test_api_advanced_logs(
|
||||||
|
"/multicast",
|
||||||
|
"hassio_multicast",
|
||||||
|
api_client,
|
||||||
|
journald_logs,
|
||||||
|
coresys,
|
||||||
|
os_available,
|
||||||
|
)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ from supervisor.homeassistant.module import HomeAssistant
|
|||||||
from supervisor.store.addon import AddonStore
|
from supervisor.store.addon import AddonStore
|
||||||
from supervisor.store.repository import Repository
|
from supervisor.store.repository import Repository
|
||||||
|
|
||||||
from tests.common import AsyncIterator, load_json_fixture
|
from tests.common import load_json_fixture
|
||||||
from tests.const import TEST_ADDON_SLUG
|
from tests.const import TEST_ADDON_SLUG
|
||||||
|
|
||||||
REPO_URL = "https://github.com/awesome-developer/awesome-repo"
|
REPO_URL = "https://github.com/awesome-developer/awesome-repo"
|
||||||
@@ -732,10 +732,9 @@ async def test_api_progress_updates_addon_install_update(
|
|||||||
"""Test progress updates sent to Home Assistant for installs/updates."""
|
"""Test progress updates sent to Home Assistant for installs/updates."""
|
||||||
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
coresys.docker.docker.api.pull.return_value = load_json_fixture(
|
||||||
logs = load_json_fixture("docker_pull_image_log.json")
|
"docker_pull_image_log.json"
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
)
|
||||||
|
|
||||||
coresys.arch._supported_arch = ["amd64"] # pylint: disable=protected-access
|
coresys.arch._supported_arch = ["amd64"] # pylint: disable=protected-access
|
||||||
install_addon_example.data_store["version"] = AwesomeVersion("2.0.0")
|
install_addon_example.data_store["version"] = AwesomeVersion("2.0.0")
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ from supervisor.store.repository import Repository
|
|||||||
from supervisor.supervisor import Supervisor
|
from supervisor.supervisor import Supervisor
|
||||||
from supervisor.updater import Updater
|
from supervisor.updater import Updater
|
||||||
|
|
||||||
from tests.common import AsyncIterator, load_json_fixture
|
from tests.api import common_test_api_advanced_logs
|
||||||
|
from tests.common import load_json_fixture
|
||||||
from tests.dbus_service_mocks.base import DBusServiceMock
|
from tests.dbus_service_mocks.base import DBusServiceMock
|
||||||
from tests.dbus_service_mocks.os_agent import OSAgent as OSAgentService
|
from tests.dbus_service_mocks.os_agent import OSAgent as OSAgentService
|
||||||
|
|
||||||
@@ -154,9 +155,18 @@ async def test_api_supervisor_options_diagnostics(
|
|||||||
assert coresys.dbus.agent.diagnostics is False
|
assert coresys.dbus.agent.diagnostics is False
|
||||||
|
|
||||||
|
|
||||||
async def test_api_supervisor_logs(advanced_logs_tester):
|
async def test_api_supervisor_logs(
|
||||||
|
api_client: TestClient, journald_logs: MagicMock, coresys: CoreSys, os_available
|
||||||
|
):
|
||||||
"""Test supervisor logs."""
|
"""Test supervisor logs."""
|
||||||
await advanced_logs_tester("/supervisor", "hassio_supervisor")
|
await common_test_api_advanced_logs(
|
||||||
|
"/supervisor",
|
||||||
|
"hassio_supervisor",
|
||||||
|
api_client,
|
||||||
|
journald_logs,
|
||||||
|
coresys,
|
||||||
|
os_available,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_api_supervisor_fallback(
|
async def test_api_supervisor_fallback(
|
||||||
@@ -322,9 +332,9 @@ async def test_api_progress_updates_supervisor_update(
|
|||||||
"""Test progress updates sent to Home Assistant for updates."""
|
"""Test progress updates sent to Home Assistant for updates."""
|
||||||
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
coresys.docker.docker.api.pull.return_value = load_json_fixture(
|
||||||
logs = load_json_fixture("docker_pull_image_log.json")
|
"docker_pull_image_log.json"
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
)
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(
|
patch.object(
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
"""Common test functions."""
|
"""Common test functions."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Sequence
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from inspect import getclosurevars
|
from inspect import getclosurevars
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Self
|
from typing import Any
|
||||||
|
|
||||||
from dbus_fast.aio.message_bus import MessageBus
|
from dbus_fast.aio.message_bus import MessageBus
|
||||||
|
|
||||||
@@ -146,22 +145,3 @@ class MockResponse:
|
|||||||
|
|
||||||
async def __aexit__(self, exc_type, exc, tb):
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
"""Exit the context manager."""
|
"""Exit the context manager."""
|
||||||
|
|
||||||
|
|
||||||
class AsyncIterator:
|
|
||||||
"""Make list/fixture into async iterator for test mocks."""
|
|
||||||
|
|
||||||
def __init__(self, seq: Sequence[Any]) -> None:
|
|
||||||
"""Initialize with sequence."""
|
|
||||||
self.iter = iter(seq)
|
|
||||||
|
|
||||||
def __aiter__(self) -> Self:
|
|
||||||
"""Implement aiter."""
|
|
||||||
return self
|
|
||||||
|
|
||||||
async def __anext__(self) -> Any:
|
|
||||||
"""Return next in sequence."""
|
|
||||||
try:
|
|
||||||
return next(self.iter)
|
|
||||||
except StopIteration:
|
|
||||||
raise StopAsyncIteration() from None
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import subprocess
|
|||||||
from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, patch
|
from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, patch
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from aiodocker.docker import DockerImages
|
|
||||||
from aiohttp import ClientSession, web
|
from aiohttp import ClientSession, web
|
||||||
from aiohttp.test_utils import TestClient
|
from aiohttp.test_utils import TestClient
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
@@ -56,7 +55,6 @@ from supervisor.store.repository import Repository
|
|||||||
from supervisor.utils.dt import utcnow
|
from supervisor.utils.dt import utcnow
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
AsyncIterator,
|
|
||||||
MockResponse,
|
MockResponse,
|
||||||
load_binary_fixture,
|
load_binary_fixture,
|
||||||
load_fixture,
|
load_fixture,
|
||||||
@@ -114,46 +112,40 @@ async def supervisor_name() -> None:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
async def docker() -> DockerAPI:
|
async def docker() -> DockerAPI:
|
||||||
"""Mock DockerAPI."""
|
"""Mock DockerAPI."""
|
||||||
image_inspect = {
|
images = [MagicMock(tags=["ghcr.io/home-assistant/amd64-hassio-supervisor:latest"])]
|
||||||
"Os": "linux",
|
image = MagicMock()
|
||||||
"Architecture": "amd64",
|
image.attrs = {"Os": "linux", "Architecture": "amd64"}
|
||||||
"Id": "test123",
|
|
||||||
"RepoTags": ["ghcr.io/home-assistant/amd64-hassio-supervisor:latest"],
|
|
||||||
}
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch("supervisor.docker.manager.DockerClient", return_value=MagicMock()),
|
patch("supervisor.docker.manager.DockerClient", return_value=MagicMock()),
|
||||||
|
patch("supervisor.docker.manager.DockerAPI.images", return_value=MagicMock()),
|
||||||
patch(
|
patch(
|
||||||
"supervisor.docker.manager.DockerAPI.containers", return_value=MagicMock()
|
"supervisor.docker.manager.DockerAPI.containers", return_value=MagicMock()
|
||||||
),
|
),
|
||||||
patch("supervisor.docker.manager.DockerAPI.api", return_value=MagicMock()),
|
|
||||||
patch("supervisor.docker.manager.DockerAPI.info", return_value=MagicMock()),
|
|
||||||
patch("supervisor.docker.manager.DockerAPI.unload"),
|
|
||||||
patch("supervisor.docker.manager.aiodocker.Docker", return_value=MagicMock()),
|
|
||||||
patch(
|
patch(
|
||||||
"supervisor.docker.manager.DockerAPI.images",
|
"supervisor.docker.manager.DockerAPI.api",
|
||||||
new=PropertyMock(
|
return_value=(api_mock := MagicMock()),
|
||||||
return_value=(docker_images := MagicMock(spec=DockerImages))
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
patch("supervisor.docker.manager.DockerAPI.images.get", return_value=image),
|
||||||
|
patch("supervisor.docker.manager.DockerAPI.images.list", return_value=images),
|
||||||
|
patch(
|
||||||
|
"supervisor.docker.manager.DockerAPI.info",
|
||||||
|
return_value=MagicMock(),
|
||||||
|
),
|
||||||
|
patch("supervisor.docker.manager.DockerAPI.unload"),
|
||||||
):
|
):
|
||||||
docker_obj = await DockerAPI(MagicMock()).post_init()
|
docker_obj = await DockerAPI(MagicMock()).post_init()
|
||||||
docker_obj.config._data = {"registries": {}}
|
docker_obj.config._data = {"registries": {}}
|
||||||
with patch("supervisor.docker.monitor.DockerMonitor.load"):
|
with patch("supervisor.docker.monitor.DockerMonitor.load"):
|
||||||
await docker_obj.load()
|
await docker_obj.load()
|
||||||
|
|
||||||
docker_images.inspect.return_value = image_inspect
|
|
||||||
docker_images.list.return_value = [image_inspect]
|
|
||||||
docker_images.import_image.return_value = [
|
|
||||||
{"stream": "Loaded image: test:latest\n"}
|
|
||||||
]
|
|
||||||
|
|
||||||
docker_images.pull.return_value = AsyncIterator([{}])
|
|
||||||
|
|
||||||
docker_obj.info.logging = "journald"
|
docker_obj.info.logging = "journald"
|
||||||
docker_obj.info.storage = "overlay2"
|
docker_obj.info.storage = "overlay2"
|
||||||
docker_obj.info.version = AwesomeVersion("1.0.0")
|
docker_obj.info.version = AwesomeVersion("1.0.0")
|
||||||
|
|
||||||
|
# Need an iterable for logs
|
||||||
|
api_mock.pull.return_value = []
|
||||||
|
|
||||||
yield docker_obj
|
yield docker_obj
|
||||||
|
|
||||||
|
|
||||||
@@ -846,9 +838,11 @@ async def container(docker: DockerAPI) -> MagicMock:
|
|||||||
"""Mock attrs and status for container on attach."""
|
"""Mock attrs and status for container on attach."""
|
||||||
docker.containers.get.return_value = addon = MagicMock()
|
docker.containers.get.return_value = addon = MagicMock()
|
||||||
docker.containers.create.return_value = addon
|
docker.containers.create.return_value = addon
|
||||||
|
docker.images.build.return_value = (addon, "")
|
||||||
addon.status = "stopped"
|
addon.status = "stopped"
|
||||||
addon.attrs = {"State": {"ExitCode": 0}}
|
addon.attrs = {"State": {"ExitCode": 0}}
|
||||||
yield addon
|
with patch.object(DockerAPI, "pull_image", return_value=addon):
|
||||||
|
yield addon
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -184,20 +184,3 @@ async def test_interface_becomes_unmanaged(
|
|||||||
assert wireless.is_connected is False
|
assert wireless.is_connected is False
|
||||||
assert eth0.connection is None
|
assert eth0.connection is None
|
||||||
assert connection.is_connected is False
|
assert connection.is_connected is False
|
||||||
|
|
||||||
|
|
||||||
async def test_unknown_device_type(
|
|
||||||
device_eth0_service: DeviceService, dbus_session_bus: MessageBus
|
|
||||||
):
|
|
||||||
"""Test unknown device types are handled gracefully."""
|
|
||||||
interface = NetworkInterface("/org/freedesktop/NetworkManager/Devices/1")
|
|
||||||
await interface.connect(dbus_session_bus)
|
|
||||||
|
|
||||||
# Emit an unknown device type (e.g., 1000 which doesn't exist in the enum)
|
|
||||||
device_eth0_service.emit_properties_changed({"DeviceType": 1000})
|
|
||||||
await device_eth0_service.ping()
|
|
||||||
|
|
||||||
# Should return UNKNOWN instead of crashing
|
|
||||||
assert interface.type == DeviceType.UNKNOWN
|
|
||||||
# Wireless should be None since it's not a wireless device
|
|
||||||
assert interface.wireless is None
|
|
||||||
|
|||||||
@@ -41,51 +41,51 @@ async def test_dbus_resolved_info(
|
|||||||
assert resolved.dns_over_tls == DNSOverTLSEnabled.NO
|
assert resolved.dns_over_tls == DNSOverTLSEnabled.NO
|
||||||
|
|
||||||
assert len(resolved.dns) == 2
|
assert len(resolved.dns) == 2
|
||||||
assert resolved.dns[0] == (0, 2, inet_aton("127.0.0.1"))
|
assert resolved.dns[0] == [0, 2, inet_aton("127.0.0.1")]
|
||||||
assert resolved.dns[1] == (0, 10, inet_pton(AF_INET6, "::1"))
|
assert resolved.dns[1] == [0, 10, inet_pton(AF_INET6, "::1")]
|
||||||
assert len(resolved.dns_ex) == 2
|
assert len(resolved.dns_ex) == 2
|
||||||
assert resolved.dns_ex[0] == (0, 2, inet_aton("127.0.0.1"), 0, "")
|
assert resolved.dns_ex[0] == [0, 2, inet_aton("127.0.0.1"), 0, ""]
|
||||||
assert resolved.dns_ex[1] == (0, 10, inet_pton(AF_INET6, "::1"), 0, "")
|
assert resolved.dns_ex[1] == [0, 10, inet_pton(AF_INET6, "::1"), 0, ""]
|
||||||
|
|
||||||
assert len(resolved.fallback_dns) == 2
|
assert len(resolved.fallback_dns) == 2
|
||||||
assert resolved.fallback_dns[0] == (0, 2, inet_aton("1.1.1.1"))
|
assert resolved.fallback_dns[0] == [0, 2, inet_aton("1.1.1.1")]
|
||||||
assert resolved.fallback_dns[1] == (
|
assert resolved.fallback_dns[1] == [
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
inet_pton(AF_INET6, "2606:4700:4700::1111"),
|
inet_pton(AF_INET6, "2606:4700:4700::1111"),
|
||||||
)
|
]
|
||||||
assert len(resolved.fallback_dns_ex) == 2
|
assert len(resolved.fallback_dns_ex) == 2
|
||||||
assert resolved.fallback_dns_ex[0] == (
|
assert resolved.fallback_dns_ex[0] == [
|
||||||
0,
|
0,
|
||||||
2,
|
2,
|
||||||
inet_aton("1.1.1.1"),
|
inet_aton("1.1.1.1"),
|
||||||
0,
|
0,
|
||||||
"cloudflare-dns.com",
|
"cloudflare-dns.com",
|
||||||
)
|
]
|
||||||
assert resolved.fallback_dns_ex[1] == (
|
assert resolved.fallback_dns_ex[1] == [
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
inet_pton(AF_INET6, "2606:4700:4700::1111"),
|
inet_pton(AF_INET6, "2606:4700:4700::1111"),
|
||||||
0,
|
0,
|
||||||
"cloudflare-dns.com",
|
"cloudflare-dns.com",
|
||||||
)
|
]
|
||||||
|
|
||||||
assert resolved.current_dns_server == (0, 2, inet_aton("127.0.0.1"))
|
assert resolved.current_dns_server == [0, 2, inet_aton("127.0.0.1")]
|
||||||
assert resolved.current_dns_server_ex == (
|
assert resolved.current_dns_server_ex == [
|
||||||
0,
|
0,
|
||||||
2,
|
2,
|
||||||
inet_aton("127.0.0.1"),
|
inet_aton("127.0.0.1"),
|
||||||
0,
|
0,
|
||||||
"",
|
"",
|
||||||
)
|
]
|
||||||
|
|
||||||
assert len(resolved.domains) == 1
|
assert len(resolved.domains) == 1
|
||||||
assert resolved.domains[0] == (0, "local.hass.io", False)
|
assert resolved.domains[0] == [0, "local.hass.io", False]
|
||||||
|
|
||||||
assert resolved.transaction_statistics == (0, 100000)
|
assert resolved.transaction_statistics == [0, 100000]
|
||||||
assert resolved.cache_statistics == (10, 50000, 10000)
|
assert resolved.cache_statistics == [10, 50000, 10000]
|
||||||
assert resolved.dnssec == DNSSECValidation.NO
|
assert resolved.dnssec == DNSSECValidation.NO
|
||||||
assert resolved.dnssec_statistics == (0, 0, 0, 0)
|
assert resolved.dnssec_statistics == [0, 0, 0, 0]
|
||||||
assert resolved.dnssec_supported is False
|
assert resolved.dnssec_supported is False
|
||||||
assert resolved.dnssec_negative_trust_anchors == [
|
assert resolved.dnssec_negative_trust_anchors == [
|
||||||
"168.192.in-addr.arpa",
|
"168.192.in-addr.arpa",
|
||||||
|
|||||||
@@ -185,10 +185,10 @@ async def test_start_transient_unit(
|
|||||||
"tmp-test.mount",
|
"tmp-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Description", Variant("s", "Test")),
|
["Description", Variant("s", "Test")],
|
||||||
("What", Variant("s", "//homeassistant/config")),
|
["What", Variant("s", "//homeassistant/config")],
|
||||||
("Type", Variant("s", "cifs")),
|
["Type", Variant("s", "cifs")],
|
||||||
("Options", Variant("s", "username=homeassistant,password=password")),
|
["Options", Variant("s", "username=homeassistant,password=password")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ class Resolved(DBusServiceMock):
|
|||||||
def DNS(self) -> "a(iiay)":
|
def DNS(self) -> "a(iiay)":
|
||||||
"""Get DNS."""
|
"""Get DNS."""
|
||||||
return [
|
return [
|
||||||
(0, 2, bytes([127, 0, 0, 1])),
|
[0, 2, bytes([127, 0, 0, 1])],
|
||||||
(
|
[
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
bytes(
|
bytes(
|
||||||
@@ -69,15 +69,15 @@ class Resolved(DBusServiceMock):
|
|||||||
0x1,
|
0x1,
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def DNSEx(self) -> "a(iiayqs)":
|
def DNSEx(self) -> "a(iiayqs)":
|
||||||
"""Get DNSEx."""
|
"""Get DNSEx."""
|
||||||
return [
|
return [
|
||||||
(0, 2, bytes([127, 0, 0, 1]), 0, ""),
|
[0, 2, bytes([127, 0, 0, 1]), 0, ""],
|
||||||
(
|
[
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
bytes(
|
bytes(
|
||||||
@@ -102,15 +102,15 @@ class Resolved(DBusServiceMock):
|
|||||||
),
|
),
|
||||||
0,
|
0,
|
||||||
"",
|
"",
|
||||||
),
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def FallbackDNS(self) -> "a(iiay)":
|
def FallbackDNS(self) -> "a(iiay)":
|
||||||
"""Get FallbackDNS."""
|
"""Get FallbackDNS."""
|
||||||
return [
|
return [
|
||||||
(0, 2, bytes([1, 1, 1, 1])),
|
[0, 2, bytes([1, 1, 1, 1])],
|
||||||
(
|
[
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
bytes(
|
bytes(
|
||||||
@@ -133,15 +133,15 @@ class Resolved(DBusServiceMock):
|
|||||||
0x11,
|
0x11,
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def FallbackDNSEx(self) -> "a(iiayqs)":
|
def FallbackDNSEx(self) -> "a(iiayqs)":
|
||||||
"""Get FallbackDNSEx."""
|
"""Get FallbackDNSEx."""
|
||||||
return [
|
return [
|
||||||
(0, 2, bytes([1, 1, 1, 1]), 0, "cloudflare-dns.com"),
|
[0, 2, bytes([1, 1, 1, 1]), 0, "cloudflare-dns.com"],
|
||||||
(
|
[
|
||||||
0,
|
0,
|
||||||
10,
|
10,
|
||||||
bytes(
|
bytes(
|
||||||
@@ -166,33 +166,33 @@ class Resolved(DBusServiceMock):
|
|||||||
),
|
),
|
||||||
0,
|
0,
|
||||||
"cloudflare-dns.com",
|
"cloudflare-dns.com",
|
||||||
),
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def CurrentDNSServer(self) -> "(iiay)":
|
def CurrentDNSServer(self) -> "(iiay)":
|
||||||
"""Get CurrentDNSServer."""
|
"""Get CurrentDNSServer."""
|
||||||
return (0, 2, bytes([127, 0, 0, 1]))
|
return [0, 2, bytes([127, 0, 0, 1])]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def CurrentDNSServerEx(self) -> "(iiayqs)":
|
def CurrentDNSServerEx(self) -> "(iiayqs)":
|
||||||
"""Get CurrentDNSServerEx."""
|
"""Get CurrentDNSServerEx."""
|
||||||
return (0, 2, bytes([127, 0, 0, 1]), 0, "")
|
return [0, 2, bytes([127, 0, 0, 1]), 0, ""]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def Domains(self) -> "a(isb)":
|
def Domains(self) -> "a(isb)":
|
||||||
"""Get Domains."""
|
"""Get Domains."""
|
||||||
return [(0, "local.hass.io", False)]
|
return [[0, "local.hass.io", False]]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def TransactionStatistics(self) -> "(tt)":
|
def TransactionStatistics(self) -> "(tt)":
|
||||||
"""Get TransactionStatistics."""
|
"""Get TransactionStatistics."""
|
||||||
return (0, 100000)
|
return [0, 100000]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def CacheStatistics(self) -> "(ttt)":
|
def CacheStatistics(self) -> "(ttt)":
|
||||||
"""Get CacheStatistics."""
|
"""Get CacheStatistics."""
|
||||||
return (10, 50000, 10000)
|
return [10, 50000, 10000]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def DNSSEC(self) -> "s":
|
def DNSSEC(self) -> "s":
|
||||||
@@ -202,7 +202,7 @@ class Resolved(DBusServiceMock):
|
|||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def DNSSECStatistics(self) -> "(tttt)":
|
def DNSSECStatistics(self) -> "(tttt)":
|
||||||
"""Get DNSSECStatistics."""
|
"""Get DNSSECStatistics."""
|
||||||
return (0, 0, 0, 0)
|
return [0, 0, 0, 0]
|
||||||
|
|
||||||
@dbus_property(access=PropertyAccess.READ)
|
@dbus_property(access=PropertyAccess.READ)
|
||||||
def DNSSECSupported(self) -> "b":
|
def DNSSECSupported(self) -> "b":
|
||||||
|
|||||||
@@ -1,48 +1,8 @@
|
|||||||
"""Test docker login."""
|
"""Test docker login."""
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.docker.const import DOCKER_HUB, DOCKER_HUB_LEGACY, get_domain
|
from supervisor.docker.interface import DOCKER_HUB, DockerInterface
|
||||||
from supervisor.docker.interface import DockerInterface
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
("image_ref", "expected_domain"),
|
|
||||||
[
|
|
||||||
# No domain - Docker Hub images
|
|
||||||
("nginx", None),
|
|
||||||
("nginx:latest", None),
|
|
||||||
("library/nginx", None),
|
|
||||||
("library/nginx:latest", None),
|
|
||||||
("homeassistant/amd64-supervisor", None),
|
|
||||||
("homeassistant/amd64-supervisor:1.2.3", None),
|
|
||||||
# Domain with dot
|
|
||||||
("ghcr.io/homeassistant/amd64-supervisor", "ghcr.io"),
|
|
||||||
("ghcr.io/homeassistant/amd64-supervisor:latest", "ghcr.io"),
|
|
||||||
("myregistry.com/nginx", "myregistry.com"),
|
|
||||||
("registry.example.com/org/image:v1", "registry.example.com"),
|
|
||||||
("127.0.0.1/myimage", "127.0.0.1"),
|
|
||||||
# Domain with port
|
|
||||||
("myregistry:5000/myimage", "myregistry:5000"),
|
|
||||||
("localhost:5000/myimage", "localhost:5000"),
|
|
||||||
("registry.io:5000/org/app:v1", "registry.io:5000"),
|
|
||||||
# localhost special case
|
|
||||||
("localhost/myimage", "localhost"),
|
|
||||||
("localhost/myimage:tag", "localhost"),
|
|
||||||
# IPv6
|
|
||||||
("[::1]:5000/myimage", "[::1]:5000"),
|
|
||||||
("[2001:db8::1]:5000/myimage:tag", "[2001:db8::1]:5000"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_get_domain(image_ref: str, expected_domain: str | None):
|
|
||||||
"""Test get_domain extracts registry domain from image reference.
|
|
||||||
|
|
||||||
Based on Docker's reference implementation:
|
|
||||||
vendor/github.com/distribution/reference/normalize.go
|
|
||||||
"""
|
|
||||||
assert get_domain(image_ref) == expected_domain
|
|
||||||
|
|
||||||
|
|
||||||
def test_no_credentials(coresys: CoreSys, test_docker_interface: DockerInterface):
|
def test_no_credentials(coresys: CoreSys, test_docker_interface: DockerInterface):
|
||||||
@@ -86,36 +46,3 @@ def test_matching_credentials(coresys: CoreSys, test_docker_interface: DockerInt
|
|||||||
)
|
)
|
||||||
assert credentials["username"] == "Spongebob Squarepants"
|
assert credentials["username"] == "Spongebob Squarepants"
|
||||||
assert "registry" not in credentials
|
assert "registry" not in credentials
|
||||||
|
|
||||||
|
|
||||||
def test_legacy_docker_hub_credentials(
|
|
||||||
coresys: CoreSys, test_docker_interface: DockerInterface
|
|
||||||
):
|
|
||||||
"""Test legacy hub.docker.com credentials are used for Docker Hub images."""
|
|
||||||
coresys.docker.config._data["registries"] = {
|
|
||||||
DOCKER_HUB_LEGACY: {"username": "LegacyUser", "password": "Password1!"},
|
|
||||||
}
|
|
||||||
|
|
||||||
credentials = test_docker_interface._get_credentials(
|
|
||||||
"homeassistant/amd64-supervisor"
|
|
||||||
)
|
|
||||||
assert credentials["username"] == "LegacyUser"
|
|
||||||
# No registry should be included for Docker Hub
|
|
||||||
assert "registry" not in credentials
|
|
||||||
|
|
||||||
|
|
||||||
def test_docker_hub_preferred_over_legacy(
|
|
||||||
coresys: CoreSys, test_docker_interface: DockerInterface
|
|
||||||
):
|
|
||||||
"""Test docker.io is preferred over legacy hub.docker.com when both exist."""
|
|
||||||
coresys.docker.config._data["registries"] = {
|
|
||||||
DOCKER_HUB: {"username": "NewUser", "password": "Password1!"},
|
|
||||||
DOCKER_HUB_LEGACY: {"username": "LegacyUser", "password": "Password2!"},
|
|
||||||
}
|
|
||||||
|
|
||||||
credentials = test_docker_interface._get_credentials(
|
|
||||||
"homeassistant/amd64-supervisor"
|
|
||||||
)
|
|
||||||
# docker.io should be preferred
|
|
||||||
assert credentials["username"] == "NewUser"
|
|
||||||
assert "registry" not in credentials
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ from pathlib import Path
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, PropertyMock, call, patch
|
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, PropertyMock, call, patch
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
from docker.errors import DockerException, NotFound
|
from docker.errors import DockerException, NotFound
|
||||||
from docker.models.containers import Container
|
from docker.models.containers import Container
|
||||||
|
from docker.models.images import Image
|
||||||
import pytest
|
import pytest
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ from supervisor.addons.manager import Addon
|
|||||||
from supervisor.const import BusEvent, CoreState, CpuArch
|
from supervisor.const import BusEvent, CoreState, CpuArch
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.docker.const import ContainerState
|
from supervisor.docker.const import ContainerState
|
||||||
from supervisor.docker.interface import DOCKER_HUB, DockerInterface
|
from supervisor.docker.interface import DockerInterface
|
||||||
from supervisor.docker.manager import PullLogEntry, PullProgressDetail
|
from supervisor.docker.manager import PullLogEntry, PullProgressDetail
|
||||||
from supervisor.docker.monitor import DockerContainerStateEvent
|
from supervisor.docker.monitor import DockerContainerStateEvent
|
||||||
from supervisor.exceptions import (
|
from supervisor.exceptions import (
|
||||||
@@ -28,7 +28,7 @@ from supervisor.exceptions import (
|
|||||||
)
|
)
|
||||||
from supervisor.jobs import JobSchedulerOptions, SupervisorJob
|
from supervisor.jobs import JobSchedulerOptions, SupervisorJob
|
||||||
|
|
||||||
from tests.common import AsyncIterator, load_json_fixture
|
from tests.common import load_json_fixture
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@@ -48,68 +48,35 @@ async def test_docker_image_platform(
|
|||||||
platform: str,
|
platform: str,
|
||||||
):
|
):
|
||||||
"""Test platform set correctly from arch."""
|
"""Test platform set correctly from arch."""
|
||||||
coresys.docker.images.inspect.return_value = {"Id": "test:1.2.3"}
|
with patch.object(
|
||||||
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test", arch=cpu_arch)
|
coresys.docker.images, "get", return_value=Mock(id="test:1.2.3")
|
||||||
coresys.docker.images.pull.assert_called_once_with(
|
) as get:
|
||||||
"test", tag="1.2.3", platform=platform, auth=None, stream=True
|
await test_docker_interface.install(
|
||||||
)
|
AwesomeVersion("1.2.3"), "test", arch=cpu_arch
|
||||||
coresys.docker.images.inspect.assert_called_once_with("test:1.2.3")
|
)
|
||||||
|
coresys.docker.docker.api.pull.assert_called_once_with(
|
||||||
|
"test", tag="1.2.3", platform=platform, stream=True, decode=True
|
||||||
|
)
|
||||||
|
get.assert_called_once_with("test:1.2.3")
|
||||||
|
|
||||||
|
|
||||||
async def test_docker_image_default_platform(
|
async def test_docker_image_default_platform(
|
||||||
coresys: CoreSys, test_docker_interface: DockerInterface
|
coresys: CoreSys, test_docker_interface: DockerInterface
|
||||||
):
|
):
|
||||||
"""Test platform set using supervisor arch when omitted."""
|
"""Test platform set using supervisor arch when omitted."""
|
||||||
coresys.docker.images.inspect.return_value = {"Id": "test:1.2.3"}
|
|
||||||
with (
|
with (
|
||||||
patch.object(
|
patch.object(
|
||||||
type(coresys.supervisor), "arch", PropertyMock(return_value="i386")
|
type(coresys.supervisor), "arch", PropertyMock(return_value="i386")
|
||||||
),
|
),
|
||||||
|
patch.object(
|
||||||
|
coresys.docker.images, "get", return_value=Mock(id="test:1.2.3")
|
||||||
|
) as get,
|
||||||
):
|
):
|
||||||
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
||||||
coresys.docker.images.pull.assert_called_once_with(
|
coresys.docker.docker.api.pull.assert_called_once_with(
|
||||||
"test", tag="1.2.3", platform="linux/386", auth=None, stream=True
|
"test", tag="1.2.3", platform="linux/386", stream=True, decode=True
|
||||||
)
|
)
|
||||||
|
get.assert_called_once_with("test:1.2.3")
|
||||||
coresys.docker.images.inspect.assert_called_once_with("test:1.2.3")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"image,registry_key",
|
|
||||||
[
|
|
||||||
("homeassistant/amd64-supervisor", DOCKER_HUB),
|
|
||||||
("ghcr.io/home-assistant/amd64-supervisor", "ghcr.io"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_private_registry_credentials_passed_to_pull(
|
|
||||||
coresys: CoreSys,
|
|
||||||
test_docker_interface: DockerInterface,
|
|
||||||
image: str,
|
|
||||||
registry_key: str,
|
|
||||||
):
|
|
||||||
"""Test credentials for private registries are passed to aiodocker pull."""
|
|
||||||
coresys.docker.images.inspect.return_value = {"Id": f"{image}:1.2.3"}
|
|
||||||
|
|
||||||
# Configure registry credentials
|
|
||||||
coresys.docker.config._data["registries"] = { # pylint: disable=protected-access
|
|
||||||
registry_key: {"username": "testuser", "password": "testpass"}
|
|
||||||
}
|
|
||||||
|
|
||||||
with patch.object(
|
|
||||||
type(coresys.supervisor), "arch", PropertyMock(return_value="amd64")
|
|
||||||
):
|
|
||||||
await test_docker_interface.install(
|
|
||||||
AwesomeVersion("1.2.3"), image, arch=CpuArch.AMD64
|
|
||||||
)
|
|
||||||
|
|
||||||
# Verify credentials were passed to aiodocker
|
|
||||||
expected_auth = {"username": "testuser", "password": "testpass"}
|
|
||||||
if registry_key != DOCKER_HUB:
|
|
||||||
expected_auth["registry"] = registry_key
|
|
||||||
|
|
||||||
coresys.docker.images.pull.assert_called_once_with(
|
|
||||||
image, tag="1.2.3", platform="linux/amd64", auth=expected_auth, stream=True
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@@ -240,40 +207,57 @@ async def test_attach_existing_container(
|
|||||||
|
|
||||||
async def test_attach_container_failure(coresys: CoreSys):
|
async def test_attach_container_failure(coresys: CoreSys):
|
||||||
"""Test attach fails to find container but finds image."""
|
"""Test attach fails to find container but finds image."""
|
||||||
coresys.docker.containers.get.side_effect = DockerException()
|
container_collection = MagicMock()
|
||||||
coresys.docker.images.inspect.return_value.setdefault("Config", {})["Image"] = (
|
container_collection.get.side_effect = DockerException()
|
||||||
"sha256:abc123"
|
image_collection = MagicMock()
|
||||||
)
|
image_config = {"Image": "sha256:abc123"}
|
||||||
with patch.object(type(coresys.bus), "fire_event") as fire_event:
|
image_collection.get.return_value = Image({"Config": image_config})
|
||||||
|
with (
|
||||||
|
patch(
|
||||||
|
"supervisor.docker.manager.DockerAPI.containers",
|
||||||
|
new=PropertyMock(return_value=container_collection),
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"supervisor.docker.manager.DockerAPI.images",
|
||||||
|
new=PropertyMock(return_value=image_collection),
|
||||||
|
),
|
||||||
|
patch.object(type(coresys.bus), "fire_event") as fire_event,
|
||||||
|
):
|
||||||
await coresys.homeassistant.core.instance.attach(AwesomeVersion("2022.7.3"))
|
await coresys.homeassistant.core.instance.attach(AwesomeVersion("2022.7.3"))
|
||||||
assert not [
|
assert not [
|
||||||
event
|
event
|
||||||
for event in fire_event.call_args_list
|
for event in fire_event.call_args_list
|
||||||
if event.args[0] == BusEvent.DOCKER_CONTAINER_STATE_CHANGE
|
if event.args[0] == BusEvent.DOCKER_CONTAINER_STATE_CHANGE
|
||||||
]
|
]
|
||||||
assert (
|
assert coresys.homeassistant.core.instance.meta_config == image_config
|
||||||
coresys.homeassistant.core.instance.meta_config["Image"] == "sha256:abc123"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_attach_total_failure(coresys: CoreSys):
|
async def test_attach_total_failure(coresys: CoreSys):
|
||||||
"""Test attach fails to find container or image."""
|
"""Test attach fails to find container or image."""
|
||||||
coresys.docker.containers.get.side_effect = DockerException
|
container_collection = MagicMock()
|
||||||
coresys.docker.images.inspect.side_effect = aiodocker.DockerError(
|
container_collection.get.side_effect = DockerException()
|
||||||
400, {"message": ""}
|
image_collection = MagicMock()
|
||||||
)
|
image_collection.get.side_effect = DockerException()
|
||||||
with pytest.raises(DockerError):
|
with (
|
||||||
|
patch(
|
||||||
|
"supervisor.docker.manager.DockerAPI.containers",
|
||||||
|
new=PropertyMock(return_value=container_collection),
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"supervisor.docker.manager.DockerAPI.images",
|
||||||
|
new=PropertyMock(return_value=image_collection),
|
||||||
|
),
|
||||||
|
pytest.raises(DockerError),
|
||||||
|
):
|
||||||
await coresys.homeassistant.core.instance.attach(AwesomeVersion("2022.7.3"))
|
await coresys.homeassistant.core.instance.attach(AwesomeVersion("2022.7.3"))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize("err", [DockerException(), RequestException()])
|
||||||
"err", [aiodocker.DockerError(400, {"message": ""}), RequestException()]
|
|
||||||
)
|
|
||||||
async def test_image_pull_fail(
|
async def test_image_pull_fail(
|
||||||
coresys: CoreSys, capture_exception: Mock, err: Exception
|
coresys: CoreSys, capture_exception: Mock, err: Exception
|
||||||
):
|
):
|
||||||
"""Test failure to pull image."""
|
"""Test failure to pull image."""
|
||||||
coresys.docker.images.inspect.side_effect = err
|
coresys.docker.images.get.side_effect = err
|
||||||
with pytest.raises(DockerError):
|
with pytest.raises(DockerError):
|
||||||
await coresys.homeassistant.core.instance.install(
|
await coresys.homeassistant.core.instance.install(
|
||||||
AwesomeVersion("2022.7.3"), arch=CpuArch.AMD64
|
AwesomeVersion("2022.7.3"), arch=CpuArch.AMD64
|
||||||
@@ -305,9 +289,8 @@ async def test_install_fires_progress_events(
|
|||||||
coresys: CoreSys, test_docker_interface: DockerInterface
|
coresys: CoreSys, test_docker_interface: DockerInterface
|
||||||
):
|
):
|
||||||
"""Test progress events are fired during an install for listeners."""
|
"""Test progress events are fired during an install for listeners."""
|
||||||
|
|
||||||
# This is from a sample pull. Filtered log to just one per unique status for test
|
# This is from a sample pull. Filtered log to just one per unique status for test
|
||||||
logs = [
|
coresys.docker.docker.api.pull.return_value = [
|
||||||
{
|
{
|
||||||
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
||||||
"id": "2025.7.2",
|
"id": "2025.7.2",
|
||||||
@@ -329,11 +312,7 @@ async def test_install_fires_progress_events(
|
|||||||
"id": "1578b14a573c",
|
"id": "1578b14a573c",
|
||||||
},
|
},
|
||||||
{"status": "Pull complete", "progressDetail": {}, "id": "1578b14a573c"},
|
{"status": "Pull complete", "progressDetail": {}, "id": "1578b14a573c"},
|
||||||
{
|
{"status": "Verifying Checksum", "progressDetail": {}, "id": "6a1e931d8f88"},
|
||||||
"status": "Verifying Checksum",
|
|
||||||
"progressDetail": {},
|
|
||||||
"id": "6a1e931d8f88",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"status": "Digest: sha256:490080d7da0f385928022927990e04f604615f7b8c622ef3e58253d0f089881d"
|
"status": "Digest: sha256:490080d7da0f385928022927990e04f604615f7b8c622ef3e58253d0f089881d"
|
||||||
},
|
},
|
||||||
@@ -341,7 +320,6 @@ async def test_install_fires_progress_events(
|
|||||||
"status": "Status: Downloaded newer image for ghcr.io/home-assistant/odroid-n2-homeassistant:2025.7.2"
|
"status": "Status: Downloaded newer image for ghcr.io/home-assistant/odroid-n2-homeassistant:2025.7.2"
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
|
||||||
|
|
||||||
events: list[PullLogEntry] = []
|
events: list[PullLogEntry] = []
|
||||||
|
|
||||||
@@ -356,10 +334,10 @@ async def test_install_fires_progress_events(
|
|||||||
),
|
),
|
||||||
):
|
):
|
||||||
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
||||||
coresys.docker.images.pull.assert_called_once_with(
|
coresys.docker.docker.api.pull.assert_called_once_with(
|
||||||
"test", tag="1.2.3", platform="linux/386", auth=None, stream=True
|
"test", tag="1.2.3", platform="linux/386", stream=True, decode=True
|
||||||
)
|
)
|
||||||
coresys.docker.images.inspect.assert_called_once_with("test:1.2.3")
|
coresys.docker.images.get.assert_called_once_with("test:1.2.3")
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
assert events == [
|
assert events == [
|
||||||
@@ -437,11 +415,10 @@ async def test_install_progress_rounding_does_not_cause_misses(
|
|||||||
):
|
):
|
||||||
"""Test extremely close progress events do not create rounding issues."""
|
"""Test extremely close progress events do not create rounding issues."""
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
|
||||||
# Current numbers chosen to create a rounding issue with original code
|
# Current numbers chosen to create a rounding issue with original code
|
||||||
# Where a progress update came in with a value between the actual previous
|
# Where a progress update came in with a value between the actual previous
|
||||||
# value and what it was rounded to. It should not raise an out of order exception
|
# value and what it was rounded to. It should not raise an out of order exception
|
||||||
logs = [
|
coresys.docker.docker.api.pull.return_value = [
|
||||||
{
|
{
|
||||||
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
||||||
"id": "2025.7.1",
|
"id": "2025.7.1",
|
||||||
@@ -481,25 +458,29 @@ async def test_install_progress_rounding_does_not_cause_misses(
|
|||||||
"status": "Status: Downloaded newer image for ghcr.io/home-assistant/odroid-n2-homeassistant:2025.7.1"
|
"status": "Status: Downloaded newer image for ghcr.io/home-assistant/odroid-n2-homeassistant:2025.7.1"
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
|
||||||
|
|
||||||
# Schedule job so we can listen for the end. Then we can assert against the WS mock
|
with (
|
||||||
event = asyncio.Event()
|
patch.object(
|
||||||
job, install_task = coresys.jobs.schedule_job(
|
type(coresys.supervisor), "arch", PropertyMock(return_value="i386")
|
||||||
test_docker_interface.install,
|
),
|
||||||
JobSchedulerOptions(),
|
):
|
||||||
AwesomeVersion("1.2.3"),
|
# Schedule job so we can listen for the end. Then we can assert against the WS mock
|
||||||
"test",
|
event = asyncio.Event()
|
||||||
)
|
job, install_task = coresys.jobs.schedule_job(
|
||||||
|
test_docker_interface.install,
|
||||||
|
JobSchedulerOptions(),
|
||||||
|
AwesomeVersion("1.2.3"),
|
||||||
|
"test",
|
||||||
|
)
|
||||||
|
|
||||||
async def listen_for_job_end(reference: SupervisorJob):
|
async def listen_for_job_end(reference: SupervisorJob):
|
||||||
if reference.uuid != job.uuid:
|
if reference.uuid != job.uuid:
|
||||||
return
|
return
|
||||||
event.set()
|
event.set()
|
||||||
|
|
||||||
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, listen_for_job_end)
|
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, listen_for_job_end)
|
||||||
await install_task
|
await install_task
|
||||||
await event.wait()
|
await event.wait()
|
||||||
|
|
||||||
capture_exception.assert_not_called()
|
capture_exception.assert_not_called()
|
||||||
|
|
||||||
@@ -532,8 +513,7 @@ async def test_install_raises_on_pull_error(
|
|||||||
exc_msg: str,
|
exc_msg: str,
|
||||||
):
|
):
|
||||||
"""Test exceptions raised from errors in pull log."""
|
"""Test exceptions raised from errors in pull log."""
|
||||||
|
coresys.docker.docker.api.pull.return_value = [
|
||||||
logs = [
|
|
||||||
{
|
{
|
||||||
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
||||||
"id": "2025.7.2",
|
"id": "2025.7.2",
|
||||||
@@ -546,7 +526,6 @@ async def test_install_raises_on_pull_error(
|
|||||||
},
|
},
|
||||||
error_log,
|
error_log,
|
||||||
]
|
]
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
|
||||||
|
|
||||||
with pytest.raises(exc_type, match=exc_msg):
|
with pytest.raises(exc_type, match=exc_msg):
|
||||||
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
await test_docker_interface.install(AwesomeVersion("1.2.3"), "test")
|
||||||
@@ -560,11 +539,11 @@ async def test_install_progress_handles_download_restart(
|
|||||||
):
|
):
|
||||||
"""Test install handles docker progress events that include a download restart."""
|
"""Test install handles docker progress events that include a download restart."""
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
|
||||||
# Fixture emulates a download restart as it docker logs it
|
# Fixture emulates a download restart as it docker logs it
|
||||||
# A log out of order exception should not be raised
|
# A log out of order exception should not be raised
|
||||||
logs = load_json_fixture("docker_pull_image_log_restart.json")
|
coresys.docker.docker.api.pull.return_value = load_json_fixture(
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
"docker_pull_image_log_restart.json"
|
||||||
|
)
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(
|
patch.object(
|
||||||
@@ -597,79 +576,54 @@ async def test_install_progress_handles_layers_skipping_download(
|
|||||||
test_docker_interface: DockerInterface,
|
test_docker_interface: DockerInterface,
|
||||||
capture_exception: Mock,
|
capture_exception: Mock,
|
||||||
):
|
):
|
||||||
"""Test install handles small layers that skip downloading phase and go directly to download complete.
|
"""Test install handles small layers that skip downloading phase and go directly to download complete."""
|
||||||
|
|
||||||
Reproduces the real-world scenario from Supervisor issue #6286:
|
|
||||||
- Small layer (02a6e69d8d00) completes Download complete at 10:14:08 without ever Downloading
|
|
||||||
- Normal layer (3f4a84073184) starts Downloading at 10:14:09 with progress updates
|
|
||||||
"""
|
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
# Simulate multiple layers where one small layer (96 bytes) skips the downloading phase
|
||||||
# Reproduce EXACT sequence from SupervisorNoUpdateProgressLogs.txt:
|
# This layer should not block progress reporting for the parent job
|
||||||
# Small layer (02a6e69d8d00) completes BEFORE normal layer (3f4a84073184) starts downloading
|
coresys.docker.docker.api.pull.return_value = [
|
||||||
logs = [
|
|
||||||
{"status": "Pulling from test/image", "id": "latest"},
|
{"status": "Pulling from test/image", "id": "latest"},
|
||||||
# Small layer that skips downloading (02a6e69d8d00 in logs, 96 bytes)
|
# Layer 1: Normal layer with downloading phase
|
||||||
{"status": "Pulling fs layer", "progressDetail": {}, "id": "02a6e69d8d00"},
|
{"status": "Pulling fs layer", "progressDetail": {}, "id": "layer1"},
|
||||||
{"status": "Pulling fs layer", "progressDetail": {}, "id": "3f4a84073184"},
|
|
||||||
{"status": "Waiting", "progressDetail": {}, "id": "02a6e69d8d00"},
|
|
||||||
{"status": "Waiting", "progressDetail": {}, "id": "3f4a84073184"},
|
|
||||||
# Goes straight to Download complete (10:14:08 in logs) - THIS IS THE KEY MOMENT
|
|
||||||
{"status": "Download complete", "progressDetail": {}, "id": "02a6e69d8d00"},
|
|
||||||
# Normal layer that downloads (3f4a84073184 in logs, 25MB)
|
|
||||||
# Downloading starts (10:14:09 in logs) - progress updates should happen NOW!
|
|
||||||
{
|
{
|
||||||
"status": "Downloading",
|
"status": "Downloading",
|
||||||
"progressDetail": {"current": 260937, "total": 25371463},
|
"progressDetail": {"current": 100, "total": 1000},
|
||||||
"progress": "[> ] 260.9kB/25.37MB",
|
"progress": "[=====> ] 100B/1000B",
|
||||||
"id": "3f4a84073184",
|
"id": "layer1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"status": "Downloading",
|
"status": "Downloading",
|
||||||
"progressDetail": {"current": 5505024, "total": 25371463},
|
"progressDetail": {"current": 1000, "total": 1000},
|
||||||
"progress": "[==========> ] 5.505MB/25.37MB",
|
"progress": "[==================================================>] 1000B/1000B",
|
||||||
"id": "3f4a84073184",
|
"id": "layer1",
|
||||||
},
|
},
|
||||||
{
|
{"status": "Download complete", "progressDetail": {}, "id": "layer1"},
|
||||||
"status": "Downloading",
|
|
||||||
"progressDetail": {"current": 11272192, "total": 25371463},
|
|
||||||
"progress": "[======================> ] 11.27MB/25.37MB",
|
|
||||||
"id": "3f4a84073184",
|
|
||||||
},
|
|
||||||
{"status": "Download complete", "progressDetail": {}, "id": "3f4a84073184"},
|
|
||||||
{
|
{
|
||||||
"status": "Extracting",
|
"status": "Extracting",
|
||||||
"progressDetail": {"current": 25371463, "total": 25371463},
|
"progressDetail": {"current": 1000, "total": 1000},
|
||||||
"progress": "[==================================================>] 25.37MB/25.37MB",
|
"progress": "[==================================================>] 1000B/1000B",
|
||||||
"id": "3f4a84073184",
|
"id": "layer1",
|
||||||
},
|
},
|
||||||
{"status": "Pull complete", "progressDetail": {}, "id": "3f4a84073184"},
|
{"status": "Pull complete", "progressDetail": {}, "id": "layer1"},
|
||||||
# Small layer finally extracts (10:14:58 in logs)
|
# Layer 2: Small layer that skips downloading (like 02a6e69d8d00 from the logs)
|
||||||
|
{"status": "Pulling fs layer", "progressDetail": {}, "id": "layer2"},
|
||||||
|
{"status": "Waiting", "progressDetail": {}, "id": "layer2"},
|
||||||
|
# Goes straight to Download complete without Downloading phase
|
||||||
|
{"status": "Download complete", "progressDetail": {}, "id": "layer2"},
|
||||||
{
|
{
|
||||||
"status": "Extracting",
|
"status": "Extracting",
|
||||||
"progressDetail": {"current": 96, "total": 96},
|
"progressDetail": {"current": 96, "total": 96},
|
||||||
"progress": "[==================================================>] 96B/96B",
|
"progress": "[==================================================>] 96B/96B",
|
||||||
"id": "02a6e69d8d00",
|
"id": "layer2",
|
||||||
},
|
},
|
||||||
{"status": "Pull complete", "progressDetail": {}, "id": "02a6e69d8d00"},
|
{"status": "Pull complete", "progressDetail": {}, "id": "layer2"},
|
||||||
{"status": "Digest: sha256:test"},
|
{"status": "Digest: sha256:test"},
|
||||||
{"status": "Status: Downloaded newer image for test/image:latest"},
|
{"status": "Status: Downloaded newer image for test/image:latest"},
|
||||||
]
|
]
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
|
||||||
|
|
||||||
# Capture immutable snapshots of install job progress using job.as_dict()
|
with patch.object(
|
||||||
# This solves the mutable object problem - we snapshot state at call time
|
type(coresys.supervisor), "arch", PropertyMock(return_value="amd64")
|
||||||
install_job_snapshots = []
|
):
|
||||||
original_on_job_change = coresys.jobs._on_job_change # pylint: disable=W0212
|
# Schedule job so we can observe that it completes successfully
|
||||||
|
|
||||||
def capture_and_forward(job_obj, attribute, value):
|
|
||||||
# Capture immutable snapshot if this is the install job with progress
|
|
||||||
if job_obj.name == "docker_interface_install" and job_obj.progress > 0:
|
|
||||||
install_job_snapshots.append(job_obj.as_dict())
|
|
||||||
# Forward to original to maintain functionality
|
|
||||||
return original_on_job_change(job_obj, attribute, value)
|
|
||||||
|
|
||||||
with patch.object(coresys.jobs, "_on_job_change", side_effect=capture_and_forward):
|
|
||||||
event = asyncio.Event()
|
event = asyncio.Event()
|
||||||
job, install_task = coresys.jobs.schedule_job(
|
job, install_task = coresys.jobs.schedule_job(
|
||||||
test_docker_interface.install,
|
test_docker_interface.install,
|
||||||
@@ -687,74 +641,100 @@ async def test_install_progress_handles_layers_skipping_download(
|
|||||||
await install_task
|
await install_task
|
||||||
await event.wait()
|
await event.wait()
|
||||||
|
|
||||||
# First update from layer download should have rather low progress ((260937/25445459) / 2 ~ 0.5%)
|
# The key assertion: Job should complete successfully without errors
|
||||||
assert install_job_snapshots[0]["progress"] < 1
|
# Without the fix, layer2 would block all progress reporting until it reached Extracting,
|
||||||
|
# preventing the aggregate progress calculation from running
|
||||||
# Total 8 events should lead to a progress update on the install job
|
assert job.done is True
|
||||||
assert len(install_job_snapshots) == 8
|
assert job.progress == 100
|
||||||
|
capture_exception.assert_not_called()
|
||||||
# Job should complete successfully
|
|
||||||
assert job.done is True
|
|
||||||
assert job.progress == 100
|
|
||||||
capture_exception.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_missing_total_handled_gracefully(
|
async def test_install_progress_handles_containerd_snapshotter(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys,
|
||||||
test_docker_interface: DockerInterface,
|
test_docker_interface: DockerInterface,
|
||||||
ha_ws_client: AsyncMock,
|
|
||||||
capture_exception: Mock,
|
capture_exception: Mock,
|
||||||
):
|
):
|
||||||
"""Test missing 'total' fields in progress details handled gracefully."""
|
"""Test install handles containerd snapshotter time-based progress (total=None)."""
|
||||||
coresys.core.set_state(CoreState.RUNNING)
|
coresys.core.set_state(CoreState.RUNNING)
|
||||||
|
# Containerd snapshotter reports extraction progress as time elapsed (e.g., "7 s")
|
||||||
# Progress details with missing 'total' fields observed in real-world pulls
|
# with current=7, total=None instead of byte-based progress
|
||||||
logs = [
|
coresys.docker.docker.api.pull.return_value = [
|
||||||
{
|
{"status": "Pulling from test/image", "id": "latest"},
|
||||||
"status": "Pulling from home-assistant/odroid-n2-homeassistant",
|
{"status": "Pulling fs layer", "progressDetail": {}, "id": "layer1"},
|
||||||
"id": "2025.7.1",
|
|
||||||
},
|
|
||||||
{"status": "Pulling fs layer", "progressDetail": {}, "id": "1e214cd6d7d0"},
|
|
||||||
{
|
{
|
||||||
"status": "Downloading",
|
"status": "Downloading",
|
||||||
"progressDetail": {"current": 436480882},
|
"progressDetail": {"current": 100, "total": 1000},
|
||||||
"progress": "[===================================================] 436.5MB/436.5MB",
|
"progress": "[=====> ] 100B/1000B",
|
||||||
"id": "1e214cd6d7d0",
|
"id": "layer1",
|
||||||
},
|
},
|
||||||
{"status": "Verifying Checksum", "progressDetail": {}, "id": "1e214cd6d7d0"},
|
{
|
||||||
{"status": "Download complete", "progressDetail": {}, "id": "1e214cd6d7d0"},
|
"status": "Downloading",
|
||||||
|
"progressDetail": {"current": 1000, "total": 1000},
|
||||||
|
"progress": "[==================================================>] 1000B/1000B",
|
||||||
|
"id": "layer1",
|
||||||
|
},
|
||||||
|
{"status": "Download complete", "progressDetail": {}, "id": "layer1"},
|
||||||
{
|
{
|
||||||
"status": "Extracting",
|
"status": "Extracting",
|
||||||
"progressDetail": {"current": 436480882},
|
"progressDetail": {"current": 1000, "total": 1000},
|
||||||
"progress": "[===================================================] 436.5MB/436.5MB",
|
"progress": "[==================================================>] 1000B/1000B",
|
||||||
"id": "1e214cd6d7d0",
|
"id": "layer1",
|
||||||
},
|
},
|
||||||
{"status": "Pull complete", "progressDetail": {}, "id": "1e214cd6d7d0"},
|
{"status": "Pull complete", "progressDetail": {}, "id": "layer1"},
|
||||||
|
# Layer 2: Containerd snapshotter with time-based extraction
|
||||||
|
{"status": "Pulling fs layer", "progressDetail": {}, "id": "layer2"},
|
||||||
{
|
{
|
||||||
"status": "Digest: sha256:7d97da645f232f82a768d0a537e452536719d56d484d419836e53dbe3e4ec736"
|
"status": "Downloading",
|
||||||
|
"progressDetail": {"current": 50, "total": 500},
|
||||||
|
"progress": "[=====> ] 50B/500B",
|
||||||
|
"id": "layer2",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"status": "Status: Downloaded newer image for ghcr.io/home-assistant/odroid-n2-homeassistant:2025.7.1"
|
"status": "Downloading",
|
||||||
|
"progressDetail": {"current": 500, "total": 500},
|
||||||
|
"progress": "[==================================================>] 500B/500B",
|
||||||
|
"id": "layer2",
|
||||||
},
|
},
|
||||||
|
{"status": "Download complete", "progressDetail": {}, "id": "layer2"},
|
||||||
|
# Time-based extraction progress (containerd snapshotter)
|
||||||
|
{
|
||||||
|
"status": "Extracting",
|
||||||
|
"progressDetail": {"current": 3, "total": None},
|
||||||
|
"progress": "3 s",
|
||||||
|
"id": "layer2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"status": "Extracting",
|
||||||
|
"progressDetail": {"current": 7, "total": None},
|
||||||
|
"progress": "7 s",
|
||||||
|
"id": "layer2",
|
||||||
|
},
|
||||||
|
{"status": "Pull complete", "progressDetail": {}, "id": "layer2"},
|
||||||
|
{"status": "Digest: sha256:test"},
|
||||||
|
{"status": "Status: Downloaded newer image for test/image:latest"},
|
||||||
]
|
]
|
||||||
coresys.docker.images.pull.return_value = AsyncIterator(logs)
|
|
||||||
|
|
||||||
# Schedule job so we can listen for the end. Then we can assert against the WS mock
|
with patch.object(
|
||||||
event = asyncio.Event()
|
type(coresys.supervisor), "arch", PropertyMock(return_value="amd64")
|
||||||
job, install_task = coresys.jobs.schedule_job(
|
):
|
||||||
test_docker_interface.install,
|
event = asyncio.Event()
|
||||||
JobSchedulerOptions(),
|
job, install_task = coresys.jobs.schedule_job(
|
||||||
AwesomeVersion("1.2.3"),
|
test_docker_interface.install,
|
||||||
"test",
|
JobSchedulerOptions(),
|
||||||
)
|
AwesomeVersion("1.2.3"),
|
||||||
|
"test",
|
||||||
|
)
|
||||||
|
|
||||||
async def listen_for_job_end(reference: SupervisorJob):
|
async def listen_for_job_end(reference: SupervisorJob):
|
||||||
if reference.uuid != job.uuid:
|
if reference.uuid != job.uuid:
|
||||||
return
|
return
|
||||||
event.set()
|
event.set()
|
||||||
|
|
||||||
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, listen_for_job_end)
|
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, listen_for_job_end)
|
||||||
await install_task
|
await install_task
|
||||||
await event.wait()
|
await event.wait()
|
||||||
|
|
||||||
|
# The key assertion: Job should complete without crashing on None total
|
||||||
|
assert job.done is True
|
||||||
|
assert job.progress == 100
|
||||||
capture_exception.assert_not_called()
|
capture_exception.assert_not_called()
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
"""Test Docker manager."""
|
"""Test Docker manager."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from docker.errors import APIError, DockerException, NotFound
|
from docker.errors import DockerException
|
||||||
import pytest
|
import pytest
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
|
|
||||||
@@ -21,7 +20,7 @@ async def test_run_command_success(docker: DockerAPI):
|
|||||||
mock_container.logs.return_value = b"command output"
|
mock_container.logs.return_value = b"command output"
|
||||||
|
|
||||||
# Mock docker containers.run to return our mock container
|
# Mock docker containers.run to return our mock container
|
||||||
docker.dockerpy.containers.run.return_value = mock_container
|
docker.docker.containers.run.return_value = mock_container
|
||||||
|
|
||||||
# Execute the command
|
# Execute the command
|
||||||
result = docker.run_command(
|
result = docker.run_command(
|
||||||
@@ -34,7 +33,7 @@ async def test_run_command_success(docker: DockerAPI):
|
|||||||
assert result.output == b"command output"
|
assert result.output == b"command output"
|
||||||
|
|
||||||
# Verify docker.containers.run was called correctly
|
# Verify docker.containers.run was called correctly
|
||||||
docker.dockerpy.containers.run.assert_called_once_with(
|
docker.docker.containers.run.assert_called_once_with(
|
||||||
"alpine:3.18",
|
"alpine:3.18",
|
||||||
command="echo hello",
|
command="echo hello",
|
||||||
detach=True,
|
detach=True,
|
||||||
@@ -56,7 +55,7 @@ async def test_run_command_with_defaults(docker: DockerAPI):
|
|||||||
mock_container.logs.return_value = b"error output"
|
mock_container.logs.return_value = b"error output"
|
||||||
|
|
||||||
# Mock docker containers.run to return our mock container
|
# Mock docker containers.run to return our mock container
|
||||||
docker.dockerpy.containers.run.return_value = mock_container
|
docker.docker.containers.run.return_value = mock_container
|
||||||
|
|
||||||
# Execute the command with minimal parameters
|
# Execute the command with minimal parameters
|
||||||
result = docker.run_command(image="ubuntu")
|
result = docker.run_command(image="ubuntu")
|
||||||
@@ -67,7 +66,7 @@ async def test_run_command_with_defaults(docker: DockerAPI):
|
|||||||
assert result.output == b"error output"
|
assert result.output == b"error output"
|
||||||
|
|
||||||
# Verify docker.containers.run was called with defaults
|
# Verify docker.containers.run was called with defaults
|
||||||
docker.dockerpy.containers.run.assert_called_once_with(
|
docker.docker.containers.run.assert_called_once_with(
|
||||||
"ubuntu:latest", # default tag
|
"ubuntu:latest", # default tag
|
||||||
command=None, # default command
|
command=None, # default command
|
||||||
detach=True,
|
detach=True,
|
||||||
@@ -82,7 +81,7 @@ async def test_run_command_with_defaults(docker: DockerAPI):
|
|||||||
async def test_run_command_docker_exception(docker: DockerAPI):
|
async def test_run_command_docker_exception(docker: DockerAPI):
|
||||||
"""Test command execution when Docker raises an exception."""
|
"""Test command execution when Docker raises an exception."""
|
||||||
# Mock docker containers.run to raise DockerException
|
# Mock docker containers.run to raise DockerException
|
||||||
docker.dockerpy.containers.run.side_effect = DockerException("Docker error")
|
docker.docker.containers.run.side_effect = DockerException("Docker error")
|
||||||
|
|
||||||
# Execute the command and expect DockerError
|
# Execute the command and expect DockerError
|
||||||
with pytest.raises(DockerError, match="Can't execute command: Docker error"):
|
with pytest.raises(DockerError, match="Can't execute command: Docker error"):
|
||||||
@@ -92,7 +91,7 @@ async def test_run_command_docker_exception(docker: DockerAPI):
|
|||||||
async def test_run_command_request_exception(docker: DockerAPI):
|
async def test_run_command_request_exception(docker: DockerAPI):
|
||||||
"""Test command execution when requests raises an exception."""
|
"""Test command execution when requests raises an exception."""
|
||||||
# Mock docker containers.run to raise RequestException
|
# Mock docker containers.run to raise RequestException
|
||||||
docker.dockerpy.containers.run.side_effect = RequestException("Connection error")
|
docker.docker.containers.run.side_effect = RequestException("Connection error")
|
||||||
|
|
||||||
# Execute the command and expect DockerError
|
# Execute the command and expect DockerError
|
||||||
with pytest.raises(DockerError, match="Can't execute command: Connection error"):
|
with pytest.raises(DockerError, match="Can't execute command: Connection error"):
|
||||||
@@ -105,7 +104,7 @@ async def test_run_command_cleanup_on_exception(docker: DockerAPI):
|
|||||||
mock_container = MagicMock()
|
mock_container = MagicMock()
|
||||||
|
|
||||||
# Mock docker.containers.run to return container, but container.wait to raise exception
|
# Mock docker.containers.run to return container, but container.wait to raise exception
|
||||||
docker.dockerpy.containers.run.return_value = mock_container
|
docker.docker.containers.run.return_value = mock_container
|
||||||
mock_container.wait.side_effect = DockerException("Wait failed")
|
mock_container.wait.side_effect = DockerException("Wait failed")
|
||||||
|
|
||||||
# Execute the command and expect DockerError
|
# Execute the command and expect DockerError
|
||||||
@@ -124,7 +123,7 @@ async def test_run_command_custom_stdout_stderr(docker: DockerAPI):
|
|||||||
mock_container.logs.return_value = b"output"
|
mock_container.logs.return_value = b"output"
|
||||||
|
|
||||||
# Mock docker containers.run to return our mock container
|
# Mock docker containers.run to return our mock container
|
||||||
docker.dockerpy.containers.run.return_value = mock_container
|
docker.docker.containers.run.return_value = mock_container
|
||||||
|
|
||||||
# Execute the command with custom stdout/stderr
|
# Execute the command with custom stdout/stderr
|
||||||
result = docker.run_command(
|
result = docker.run_command(
|
||||||
@@ -151,7 +150,7 @@ async def test_run_container_with_cidfile(
|
|||||||
cidfile_path = coresys.config.path_cid_files / f"{container_name}.cid"
|
cidfile_path = coresys.config.path_cid_files / f"{container_name}.cid"
|
||||||
extern_cidfile_path = coresys.config.path_extern_cid_files / f"{container_name}.cid"
|
extern_cidfile_path = coresys.config.path_extern_cid_files / f"{container_name}.cid"
|
||||||
|
|
||||||
docker.dockerpy.containers.run.return_value = mock_container
|
docker.docker.containers.run.return_value = mock_container
|
||||||
|
|
||||||
# Mock container creation
|
# Mock container creation
|
||||||
with patch.object(
|
with patch.object(
|
||||||
@@ -352,101 +351,3 @@ async def test_run_container_with_leftover_cidfile_directory(
|
|||||||
assert cidfile_path.read_text() == mock_container.id
|
assert cidfile_path.read_text() == mock_container.id
|
||||||
|
|
||||||
assert result == mock_container
|
assert result == mock_container
|
||||||
|
|
||||||
|
|
||||||
async def test_repair(coresys: CoreSys, caplog: pytest.LogCaptureFixture):
|
|
||||||
"""Test repair API."""
|
|
||||||
coresys.docker.dockerpy.networks.get.side_effect = [
|
|
||||||
hassio := MagicMock(
|
|
||||||
attrs={
|
|
||||||
"Containers": {
|
|
||||||
"good": {"Name": "good"},
|
|
||||||
"corrupt": {"Name": "corrupt"},
|
|
||||||
"fail": {"Name": "fail"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
),
|
|
||||||
host := MagicMock(attrs={"Containers": {}}),
|
|
||||||
]
|
|
||||||
coresys.docker.dockerpy.containers.get.side_effect = [
|
|
||||||
MagicMock(),
|
|
||||||
NotFound("corrupt"),
|
|
||||||
DockerException("fail"),
|
|
||||||
]
|
|
||||||
|
|
||||||
await coresys.run_in_executor(coresys.docker.repair)
|
|
||||||
|
|
||||||
coresys.docker.dockerpy.api.prune_containers.assert_called_once()
|
|
||||||
coresys.docker.dockerpy.api.prune_images.assert_called_once_with(
|
|
||||||
filters={"dangling": False}
|
|
||||||
)
|
|
||||||
coresys.docker.dockerpy.api.prune_builds.assert_called_once()
|
|
||||||
coresys.docker.dockerpy.api.prune_volumes.assert_called_once()
|
|
||||||
coresys.docker.dockerpy.api.prune_networks.assert_called_once()
|
|
||||||
hassio.disconnect.assert_called_once_with("corrupt", force=True)
|
|
||||||
host.disconnect.assert_not_called()
|
|
||||||
assert "Docker fatal error on container fail on hassio" in caplog.text
|
|
||||||
|
|
||||||
|
|
||||||
async def test_repair_failures(coresys: CoreSys, caplog: pytest.LogCaptureFixture):
|
|
||||||
"""Test repair proceeds best it can through failures."""
|
|
||||||
coresys.docker.dockerpy.api.prune_containers.side_effect = APIError("fail")
|
|
||||||
coresys.docker.dockerpy.api.prune_images.side_effect = APIError("fail")
|
|
||||||
coresys.docker.dockerpy.api.prune_builds.side_effect = APIError("fail")
|
|
||||||
coresys.docker.dockerpy.api.prune_volumes.side_effect = APIError("fail")
|
|
||||||
coresys.docker.dockerpy.api.prune_networks.side_effect = APIError("fail")
|
|
||||||
coresys.docker.dockerpy.networks.get.side_effect = NotFound("missing")
|
|
||||||
|
|
||||||
await coresys.run_in_executor(coresys.docker.repair)
|
|
||||||
|
|
||||||
assert "Error for containers prune: fail" in caplog.text
|
|
||||||
assert "Error for images prune: fail" in caplog.text
|
|
||||||
assert "Error for builds prune: fail" in caplog.text
|
|
||||||
assert "Error for volumes prune: fail" in caplog.text
|
|
||||||
assert "Error for networks prune: fail" in caplog.text
|
|
||||||
assert "Error for networks hassio prune: missing" in caplog.text
|
|
||||||
assert "Error for networks host prune: missing" in caplog.text
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("log_starter", [("Loaded image ID"), ("Loaded image")])
|
|
||||||
async def test_import_image(coresys: CoreSys, tmp_path: Path, log_starter: str):
|
|
||||||
"""Test importing an image into docker."""
|
|
||||||
(test_tar := tmp_path / "test.tar").touch()
|
|
||||||
coresys.docker.images.import_image.return_value = [
|
|
||||||
{"stream": f"{log_starter}: imported"}
|
|
||||||
]
|
|
||||||
coresys.docker.images.inspect.return_value = {"Id": "imported"}
|
|
||||||
|
|
||||||
image = await coresys.docker.import_image(test_tar)
|
|
||||||
|
|
||||||
assert image["Id"] == "imported"
|
|
||||||
coresys.docker.images.inspect.assert_called_once_with("imported")
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_image_error(coresys: CoreSys, tmp_path: Path):
|
|
||||||
"""Test failure importing an image into docker."""
|
|
||||||
(test_tar := tmp_path / "test.tar").touch()
|
|
||||||
coresys.docker.images.import_image.return_value = [
|
|
||||||
{"errorDetail": {"message": "fail"}}
|
|
||||||
]
|
|
||||||
|
|
||||||
with pytest.raises(DockerError, match="Can't import image from tar: fail"):
|
|
||||||
await coresys.docker.import_image(test_tar)
|
|
||||||
|
|
||||||
coresys.docker.images.inspect.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
async def test_import_multiple_images_in_tar(
|
|
||||||
coresys: CoreSys, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
|
||||||
):
|
|
||||||
"""Test importing an image into docker."""
|
|
||||||
(test_tar := tmp_path / "test.tar").touch()
|
|
||||||
coresys.docker.images.import_image.return_value = [
|
|
||||||
{"stream": "Loaded image: imported-1"},
|
|
||||||
{"stream": "Loaded image: imported-2"},
|
|
||||||
]
|
|
||||||
|
|
||||||
assert await coresys.docker.import_image(test_tar) is None
|
|
||||||
|
|
||||||
assert "Unexpected image count 2 while importing image from tar" in caplog.text
|
|
||||||
coresys.docker.images.inspect.assert_not_called()
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ async def test_events(
|
|||||||
):
|
):
|
||||||
"""Test events created from docker events."""
|
"""Test events created from docker events."""
|
||||||
event["Actor"]["Attributes"]["name"] = "some_container"
|
event["Actor"]["Attributes"]["name"] = "some_container"
|
||||||
event["Actor"]["ID"] = "abc123"
|
event["id"] = "abc123"
|
||||||
event["time"] = 123
|
event["time"] = 123
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
@@ -131,12 +131,12 @@ async def test_unlabeled_container(coresys: CoreSys):
|
|||||||
new=PropertyMock(
|
new=PropertyMock(
|
||||||
return_value=[
|
return_value=[
|
||||||
{
|
{
|
||||||
|
"id": "abc123",
|
||||||
"time": 123,
|
"time": 123,
|
||||||
"Type": "container",
|
"Type": "container",
|
||||||
"Action": "die",
|
"Action": "die",
|
||||||
"Actor": {
|
"Actor": {
|
||||||
"ID": "abc123",
|
"Attributes": {"name": "homeassistant", "exitCode": "137"}
|
||||||
"Attributes": {"name": "homeassistant", "exitCode": "137"},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
"""Test Home Assistant core."""
|
"""Test Home Assistant core."""
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from http import HTTPStatus
|
from unittest.mock import ANY, MagicMock, Mock, PropertyMock, patch
|
||||||
from unittest.mock import ANY, MagicMock, Mock, PropertyMock, call, patch
|
|
||||||
|
|
||||||
import aiodocker
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
from docker.errors import APIError, DockerException, NotFound
|
from docker.errors import APIError, DockerException, ImageNotFound, NotFound
|
||||||
import pytest
|
import pytest
|
||||||
from requests import RequestException
|
|
||||||
from time_machine import travel
|
from time_machine import travel
|
||||||
|
|
||||||
from supervisor.const import CpuArch
|
from supervisor.const import CpuArch
|
||||||
@@ -26,12 +23,8 @@ from supervisor.exceptions import (
|
|||||||
from supervisor.homeassistant.api import APIState
|
from supervisor.homeassistant.api import APIState
|
||||||
from supervisor.homeassistant.core import HomeAssistantCore
|
from supervisor.homeassistant.core import HomeAssistantCore
|
||||||
from supervisor.homeassistant.module import HomeAssistant
|
from supervisor.homeassistant.module import HomeAssistant
|
||||||
from supervisor.resolution.const import ContextType, IssueType
|
|
||||||
from supervisor.resolution.data import Issue
|
|
||||||
from supervisor.updater import Updater
|
from supervisor.updater import Updater
|
||||||
|
|
||||||
from tests.common import AsyncIterator
|
|
||||||
|
|
||||||
|
|
||||||
async def test_update_fails_if_out_of_date(coresys: CoreSys):
|
async def test_update_fails_if_out_of_date(coresys: CoreSys):
|
||||||
"""Test update of Home Assistant fails when supervisor or plugin is out of date."""
|
"""Test update of Home Assistant fails when supervisor or plugin is out of date."""
|
||||||
@@ -59,23 +52,11 @@ async def test_update_fails_if_out_of_date(coresys: CoreSys):
|
|||||||
await coresys.homeassistant.core.update()
|
await coresys.homeassistant.core.update()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_install_landingpage_docker_error(
|
||||||
"err",
|
coresys: CoreSys, capture_exception: Mock, caplog: pytest.LogCaptureFixture
|
||||||
[
|
|
||||||
aiodocker.DockerError(HTTPStatus.TOO_MANY_REQUESTS, {"message": "ratelimit"}),
|
|
||||||
APIError("ratelimit", MagicMock(status_code=HTTPStatus.TOO_MANY_REQUESTS)),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_install_landingpage_docker_ratelimit_error(
|
|
||||||
coresys: CoreSys,
|
|
||||||
capture_exception: Mock,
|
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
err: Exception,
|
|
||||||
):
|
):
|
||||||
"""Test install landing page fails due to docker ratelimit error."""
|
"""Test install landing page fails due to docker error."""
|
||||||
coresys.security.force = True
|
coresys.security.force = True
|
||||||
coresys.docker.images.pull.side_effect = [err, AsyncIterator([{}])]
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(DockerHomeAssistant, "attach", side_effect=DockerError),
|
patch.object(DockerHomeAssistant, "attach", side_effect=DockerError),
|
||||||
patch.object(
|
patch.object(
|
||||||
@@ -88,35 +69,19 @@ async def test_install_landingpage_docker_ratelimit_error(
|
|||||||
),
|
),
|
||||||
patch("supervisor.homeassistant.core.asyncio.sleep") as sleep,
|
patch("supervisor.homeassistant.core.asyncio.sleep") as sleep,
|
||||||
):
|
):
|
||||||
|
coresys.docker.images.get.side_effect = [APIError("fail"), MagicMock()]
|
||||||
await coresys.homeassistant.core.install_landingpage()
|
await coresys.homeassistant.core.install_landingpage()
|
||||||
sleep.assert_awaited_once_with(30)
|
sleep.assert_awaited_once_with(30)
|
||||||
|
|
||||||
assert "Failed to install landingpage, retrying after 30sec" in caplog.text
|
assert "Failed to install landingpage, retrying after 30sec" in caplog.text
|
||||||
capture_exception.assert_not_called()
|
capture_exception.assert_not_called()
|
||||||
assert (
|
|
||||||
Issue(IssueType.DOCKER_RATELIMIT, ContextType.SYSTEM)
|
|
||||||
in coresys.resolution.issues
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"err",
|
|
||||||
[
|
|
||||||
aiodocker.DockerError(HTTPStatus.INTERNAL_SERVER_ERROR, {"message": "fail"}),
|
|
||||||
APIError("fail"),
|
|
||||||
DockerException(),
|
|
||||||
RequestException(),
|
|
||||||
OSError(),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_install_landingpage_other_error(
|
async def test_install_landingpage_other_error(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys, capture_exception: Mock, caplog: pytest.LogCaptureFixture
|
||||||
capture_exception: Mock,
|
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
err: Exception,
|
|
||||||
):
|
):
|
||||||
"""Test install landing page fails due to other error."""
|
"""Test install landing page fails due to other error."""
|
||||||
coresys.docker.images.inspect.side_effect = [err, MagicMock()]
|
coresys.docker.images.get.side_effect = [(err := OSError()), MagicMock()]
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(DockerHomeAssistant, "attach", side_effect=DockerError),
|
patch.object(DockerHomeAssistant, "attach", side_effect=DockerError),
|
||||||
@@ -137,23 +102,11 @@ async def test_install_landingpage_other_error(
|
|||||||
capture_exception.assert_called_once_with(err)
|
capture_exception.assert_called_once_with(err)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_install_docker_error(
|
||||||
"err",
|
coresys: CoreSys, capture_exception: Mock, caplog: pytest.LogCaptureFixture
|
||||||
[
|
|
||||||
aiodocker.DockerError(HTTPStatus.TOO_MANY_REQUESTS, {"message": "ratelimit"}),
|
|
||||||
APIError("ratelimit", MagicMock(status_code=HTTPStatus.TOO_MANY_REQUESTS)),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_install_docker_ratelimit_error(
|
|
||||||
coresys: CoreSys,
|
|
||||||
capture_exception: Mock,
|
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
err: Exception,
|
|
||||||
):
|
):
|
||||||
"""Test install fails due to docker ratelimit error."""
|
"""Test install fails due to docker error."""
|
||||||
coresys.security.force = True
|
coresys.security.force = True
|
||||||
coresys.docker.images.pull.side_effect = [err, AsyncIterator([{}])]
|
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(HomeAssistantCore, "start"),
|
patch.object(HomeAssistantCore, "start"),
|
||||||
patch.object(DockerHomeAssistant, "cleanup"),
|
patch.object(DockerHomeAssistant, "cleanup"),
|
||||||
@@ -170,35 +123,19 @@ async def test_install_docker_ratelimit_error(
|
|||||||
),
|
),
|
||||||
patch("supervisor.homeassistant.core.asyncio.sleep") as sleep,
|
patch("supervisor.homeassistant.core.asyncio.sleep") as sleep,
|
||||||
):
|
):
|
||||||
|
coresys.docker.images.get.side_effect = [APIError("fail"), MagicMock()]
|
||||||
await coresys.homeassistant.core.install()
|
await coresys.homeassistant.core.install()
|
||||||
sleep.assert_awaited_once_with(30)
|
sleep.assert_awaited_once_with(30)
|
||||||
|
|
||||||
assert "Error on Home Assistant installation. Retrying in 30sec" in caplog.text
|
assert "Error on Home Assistant installation. Retrying in 30sec" in caplog.text
|
||||||
capture_exception.assert_not_called()
|
capture_exception.assert_not_called()
|
||||||
assert (
|
|
||||||
Issue(IssueType.DOCKER_RATELIMIT, ContextType.SYSTEM)
|
|
||||||
in coresys.resolution.issues
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"err",
|
|
||||||
[
|
|
||||||
aiodocker.DockerError(HTTPStatus.INTERNAL_SERVER_ERROR, {"message": "fail"}),
|
|
||||||
APIError("fail"),
|
|
||||||
DockerException(),
|
|
||||||
RequestException(),
|
|
||||||
OSError(),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_install_other_error(
|
async def test_install_other_error(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys, capture_exception: Mock, caplog: pytest.LogCaptureFixture
|
||||||
capture_exception: Mock,
|
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
err: Exception,
|
|
||||||
):
|
):
|
||||||
"""Test install fails due to other error."""
|
"""Test install fails due to other error."""
|
||||||
coresys.docker.images.inspect.side_effect = [err, MagicMock()]
|
coresys.docker.images.get.side_effect = [(err := OSError()), MagicMock()]
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(HomeAssistantCore, "start"),
|
patch.object(HomeAssistantCore, "start"),
|
||||||
@@ -224,29 +161,21 @@ async def test_install_other_error(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("container_exc", "image_exc", "remove_calls"),
|
"container_exists,image_exists", [(False, True), (True, False), (True, True)]
|
||||||
[
|
|
||||||
(NotFound("missing"), None, []),
|
|
||||||
(
|
|
||||||
None,
|
|
||||||
aiodocker.DockerError(404, {"message": "missing"}),
|
|
||||||
[call(force=True, v=True)],
|
|
||||||
),
|
|
||||||
(None, None, [call(force=True, v=True)]),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
@pytest.mark.usefixtures("path_extern")
|
|
||||||
async def test_start(
|
async def test_start(
|
||||||
coresys: CoreSys,
|
coresys: CoreSys, container_exists: bool, image_exists: bool, path_extern
|
||||||
container_exc: DockerException | None,
|
|
||||||
image_exc: aiodocker.DockerError | None,
|
|
||||||
remove_calls: list[call],
|
|
||||||
):
|
):
|
||||||
"""Test starting Home Assistant."""
|
"""Test starting Home Assistant."""
|
||||||
coresys.docker.images.inspect.return_value = {"Id": "123"}
|
if image_exists:
|
||||||
coresys.docker.images.inspect.side_effect = image_exc
|
coresys.docker.images.get.return_value.id = "123"
|
||||||
coresys.docker.containers.get.return_value.id = "123"
|
else:
|
||||||
coresys.docker.containers.get.side_effect = container_exc
|
coresys.docker.images.get.side_effect = ImageNotFound("missing")
|
||||||
|
|
||||||
|
if container_exists:
|
||||||
|
coresys.docker.containers.get.return_value.image.id = "123"
|
||||||
|
else:
|
||||||
|
coresys.docker.containers.get.side_effect = NotFound("missing")
|
||||||
|
|
||||||
with (
|
with (
|
||||||
patch.object(
|
patch.object(
|
||||||
@@ -269,14 +198,18 @@ async def test_start(
|
|||||||
assert run.call_args.kwargs["hostname"] == "homeassistant"
|
assert run.call_args.kwargs["hostname"] == "homeassistant"
|
||||||
|
|
||||||
coresys.docker.containers.get.return_value.stop.assert_not_called()
|
coresys.docker.containers.get.return_value.stop.assert_not_called()
|
||||||
assert (
|
if container_exists:
|
||||||
coresys.docker.containers.get.return_value.remove.call_args_list == remove_calls
|
coresys.docker.containers.get.return_value.remove.assert_called_once_with(
|
||||||
)
|
force=True,
|
||||||
|
v=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
coresys.docker.containers.get.return_value.remove.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
async def test_start_existing_container(coresys: CoreSys, path_extern):
|
async def test_start_existing_container(coresys: CoreSys, path_extern):
|
||||||
"""Test starting Home Assistant when container exists and is viable."""
|
"""Test starting Home Assistant when container exists and is viable."""
|
||||||
coresys.docker.images.inspect.return_value = {"Id": "123"}
|
coresys.docker.images.get.return_value.id = "123"
|
||||||
coresys.docker.containers.get.return_value.image.id = "123"
|
coresys.docker.containers.get.return_value.image.id = "123"
|
||||||
coresys.docker.containers.get.return_value.status = "exited"
|
coresys.docker.containers.get.return_value.status = "exited"
|
||||||
|
|
||||||
@@ -461,33 +394,24 @@ async def test_core_loads_wrong_image_for_machine(
|
|||||||
"""Test core is loaded with wrong image for machine."""
|
"""Test core is loaded with wrong image for machine."""
|
||||||
coresys.homeassistant.set_image("ghcr.io/home-assistant/odroid-n2-homeassistant")
|
coresys.homeassistant.set_image("ghcr.io/home-assistant/odroid-n2-homeassistant")
|
||||||
coresys.homeassistant.version = AwesomeVersion("2024.4.0")
|
coresys.homeassistant.version = AwesomeVersion("2024.4.0")
|
||||||
|
container.attrs["Config"] = {"Labels": {"io.hass.version": "2024.4.0"}}
|
||||||
|
|
||||||
with patch.object(
|
await coresys.homeassistant.core.load()
|
||||||
DockerAPI,
|
|
||||||
"pull_image",
|
|
||||||
return_value={
|
|
||||||
"Id": "abc123",
|
|
||||||
"Config": {"Labels": {"io.hass.version": "2024.4.0"}},
|
|
||||||
},
|
|
||||||
) as pull_image:
|
|
||||||
container.attrs |= pull_image.return_value
|
|
||||||
await coresys.homeassistant.core.load()
|
|
||||||
pull_image.assert_called_once_with(
|
|
||||||
ANY,
|
|
||||||
"ghcr.io/home-assistant/qemux86-64-homeassistant",
|
|
||||||
"2024.4.0",
|
|
||||||
platform="linux/amd64",
|
|
||||||
auth=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
container.remove.assert_called_once_with(force=True, v=True)
|
container.remove.assert_called_once_with(force=True, v=True)
|
||||||
assert coresys.docker.images.delete.call_args_list[0] == call(
|
assert coresys.docker.images.remove.call_args_list[0].kwargs == {
|
||||||
"ghcr.io/home-assistant/odroid-n2-homeassistant:latest",
|
"image": "ghcr.io/home-assistant/odroid-n2-homeassistant:latest",
|
||||||
force=True,
|
"force": True,
|
||||||
)
|
}
|
||||||
assert coresys.docker.images.delete.call_args_list[1] == call(
|
assert coresys.docker.images.remove.call_args_list[1].kwargs == {
|
||||||
"ghcr.io/home-assistant/odroid-n2-homeassistant:2024.4.0",
|
"image": "ghcr.io/home-assistant/odroid-n2-homeassistant:2024.4.0",
|
||||||
force=True,
|
"force": True,
|
||||||
|
}
|
||||||
|
coresys.docker.pull_image.assert_called_once_with(
|
||||||
|
ANY,
|
||||||
|
"ghcr.io/home-assistant/qemux86-64-homeassistant",
|
||||||
|
"2024.4.0",
|
||||||
|
platform="linux/amd64",
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
coresys.homeassistant.image == "ghcr.io/home-assistant/qemux86-64-homeassistant"
|
coresys.homeassistant.image == "ghcr.io/home-assistant/qemux86-64-homeassistant"
|
||||||
@@ -504,8 +428,8 @@ async def test_core_load_allows_image_override(coresys: CoreSys, container: Magi
|
|||||||
await coresys.homeassistant.core.load()
|
await coresys.homeassistant.core.load()
|
||||||
|
|
||||||
container.remove.assert_not_called()
|
container.remove.assert_not_called()
|
||||||
coresys.docker.images.delete.assert_not_called()
|
coresys.docker.images.remove.assert_not_called()
|
||||||
coresys.docker.images.inspect.assert_not_called()
|
coresys.docker.images.get.assert_not_called()
|
||||||
assert (
|
assert (
|
||||||
coresys.homeassistant.image == "ghcr.io/home-assistant/odroid-n2-homeassistant"
|
coresys.homeassistant.image == "ghcr.io/home-assistant/odroid-n2-homeassistant"
|
||||||
)
|
)
|
||||||
@@ -516,37 +440,27 @@ async def test_core_loads_wrong_image_for_architecture(
|
|||||||
):
|
):
|
||||||
"""Test core is loaded with wrong image for architecture."""
|
"""Test core is loaded with wrong image for architecture."""
|
||||||
coresys.homeassistant.version = AwesomeVersion("2024.4.0")
|
coresys.homeassistant.version = AwesomeVersion("2024.4.0")
|
||||||
coresys.docker.images.inspect.return_value = img_data = (
|
container.attrs["Config"] = {"Labels": {"io.hass.version": "2024.4.0"}}
|
||||||
coresys.docker.images.inspect.return_value
|
coresys.docker.images.get("ghcr.io/home-assistant/qemux86-64-homeassistant").attrs[
|
||||||
| {
|
"Architecture"
|
||||||
"Architecture": "arm64",
|
] = "arm64"
|
||||||
"Config": {"Labels": {"io.hass.version": "2024.4.0"}},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
container.attrs |= img_data
|
|
||||||
|
|
||||||
with patch.object(
|
await coresys.homeassistant.core.load()
|
||||||
DockerAPI,
|
|
||||||
"pull_image",
|
|
||||||
return_value=img_data | {"Architecture": "amd64"},
|
|
||||||
) as pull_image:
|
|
||||||
await coresys.homeassistant.core.load()
|
|
||||||
pull_image.assert_called_once_with(
|
|
||||||
ANY,
|
|
||||||
"ghcr.io/home-assistant/qemux86-64-homeassistant",
|
|
||||||
"2024.4.0",
|
|
||||||
platform="linux/amd64",
|
|
||||||
auth=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
container.remove.assert_called_once_with(force=True, v=True)
|
container.remove.assert_called_once_with(force=True, v=True)
|
||||||
assert coresys.docker.images.delete.call_args_list[0] == call(
|
assert coresys.docker.images.remove.call_args_list[0].kwargs == {
|
||||||
"ghcr.io/home-assistant/qemux86-64-homeassistant:latest",
|
"image": "ghcr.io/home-assistant/qemux86-64-homeassistant:latest",
|
||||||
force=True,
|
"force": True,
|
||||||
)
|
}
|
||||||
assert coresys.docker.images.delete.call_args_list[1] == call(
|
assert coresys.docker.images.remove.call_args_list[1].kwargs == {
|
||||||
"ghcr.io/home-assistant/qemux86-64-homeassistant:2024.4.0",
|
"image": "ghcr.io/home-assistant/qemux86-64-homeassistant:2024.4.0",
|
||||||
force=True,
|
"force": True,
|
||||||
|
}
|
||||||
|
coresys.docker.pull_image.assert_called_once_with(
|
||||||
|
ANY,
|
||||||
|
"ghcr.io/home-assistant/qemux86-64-homeassistant",
|
||||||
|
"2024.4.0",
|
||||||
|
platform="linux/amd64",
|
||||||
)
|
)
|
||||||
assert (
|
assert (
|
||||||
coresys.homeassistant.image == "ghcr.io/home-assistant/qemux86-64-homeassistant"
|
coresys.homeassistant.image == "ghcr.io/home-assistant/qemux86-64-homeassistant"
|
||||||
|
|||||||
@@ -90,49 +90,6 @@ async def test_logs_coloured(journald_gateway: MagicMock, coresys: CoreSys):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_logs_no_colors(journald_gateway: MagicMock, coresys: CoreSys):
|
|
||||||
"""Test ANSI color codes being stripped when no_colors=True."""
|
|
||||||
journald_gateway.content.feed_data(
|
|
||||||
load_fixture("logs_export_supervisor.txt").encode("utf-8")
|
|
||||||
)
|
|
||||||
journald_gateway.content.feed_eof()
|
|
||||||
|
|
||||||
async with coresys.host.logs.journald_logs() as resp:
|
|
||||||
cursor, line = await anext(journal_logs_reader(resp, no_colors=True))
|
|
||||||
assert (
|
|
||||||
cursor
|
|
||||||
== "s=83fee99ca0c3466db5fc120d52ca7dd8;i=2049389;b=f5a5c442fa6548cf97474d2d57c920b3;m=4263828e8c;t=612dda478b01b;x=9ae12394c9326930"
|
|
||||||
)
|
|
||||||
# Colors should be stripped
|
|
||||||
assert (
|
|
||||||
line == "24-03-04 23:56:56 INFO (MainThread) [__main__] Closing Supervisor"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_logs_verbose_no_colors(journald_gateway: MagicMock, coresys: CoreSys):
|
|
||||||
"""Test ANSI color codes being stripped from verbose formatted logs when no_colors=True."""
|
|
||||||
journald_gateway.content.feed_data(
|
|
||||||
load_fixture("logs_export_supervisor.txt").encode("utf-8")
|
|
||||||
)
|
|
||||||
journald_gateway.content.feed_eof()
|
|
||||||
|
|
||||||
async with coresys.host.logs.journald_logs() as resp:
|
|
||||||
cursor, line = await anext(
|
|
||||||
journal_logs_reader(
|
|
||||||
resp, log_formatter=LogFormatter.VERBOSE, no_colors=True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
assert (
|
|
||||||
cursor
|
|
||||||
== "s=83fee99ca0c3466db5fc120d52ca7dd8;i=2049389;b=f5a5c442fa6548cf97474d2d57c920b3;m=4263828e8c;t=612dda478b01b;x=9ae12394c9326930"
|
|
||||||
)
|
|
||||||
# Colors should be stripped in verbose format too
|
|
||||||
assert (
|
|
||||||
line
|
|
||||||
== "2024-03-04 22:56:56.709 ha-hloub hassio_supervisor[466]: 24-03-04 23:56:56 INFO (MainThread) [__main__] Closing Supervisor"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_boot_ids(
|
async def test_boot_ids(
|
||||||
journald_gateway: MagicMock,
|
journald_gateway: MagicMock,
|
||||||
coresys: CoreSys,
|
coresys: CoreSys,
|
||||||
|
|||||||
@@ -1179,6 +1179,7 @@ async def test_job_scheduled_delay(coresys: CoreSys):
|
|||||||
|
|
||||||
async def test_job_scheduled_at(coresys: CoreSys):
|
async def test_job_scheduled_at(coresys: CoreSys):
|
||||||
"""Test job that schedules a job to start at a specified time."""
|
"""Test job that schedules a job to start at a specified time."""
|
||||||
|
dt = datetime.now()
|
||||||
|
|
||||||
class TestClass:
|
class TestClass:
|
||||||
"""Test class."""
|
"""Test class."""
|
||||||
@@ -1188,12 +1189,10 @@ async def test_job_scheduled_at(coresys: CoreSys):
|
|||||||
self.coresys = coresys
|
self.coresys = coresys
|
||||||
|
|
||||||
@Job(name="test_job_scheduled_at_job_scheduler")
|
@Job(name="test_job_scheduled_at_job_scheduler")
|
||||||
async def job_scheduler(
|
async def job_scheduler(self) -> tuple[SupervisorJob, asyncio.TimerHandle]:
|
||||||
self, scheduled_time: datetime
|
|
||||||
) -> tuple[SupervisorJob, asyncio.TimerHandle]:
|
|
||||||
"""Schedule a job to run at specified time."""
|
"""Schedule a job to run at specified time."""
|
||||||
return self.coresys.jobs.schedule_job(
|
return self.coresys.jobs.schedule_job(
|
||||||
self.job_task, JobSchedulerOptions(start_at=scheduled_time)
|
self.job_task, JobSchedulerOptions(start_at=dt + timedelta(seconds=0.1))
|
||||||
)
|
)
|
||||||
|
|
||||||
@Job(name="test_job_scheduled_at_job_task")
|
@Job(name="test_job_scheduled_at_job_task")
|
||||||
@@ -1202,28 +1201,29 @@ async def test_job_scheduled_at(coresys: CoreSys):
|
|||||||
self.coresys.jobs.current.stage = "work"
|
self.coresys.jobs.current.stage = "work"
|
||||||
|
|
||||||
test = TestClass(coresys)
|
test = TestClass(coresys)
|
||||||
|
job_started = asyncio.Event()
|
||||||
# Schedule job to run 0.1 seconds from now
|
job_ended = asyncio.Event()
|
||||||
scheduled_time = datetime.now() + timedelta(seconds=0.1)
|
|
||||||
job, _ = await test.job_scheduler(scheduled_time)
|
|
||||||
started = False
|
|
||||||
ended = False
|
|
||||||
|
|
||||||
async def start_listener(evt_job: SupervisorJob):
|
async def start_listener(evt_job: SupervisorJob):
|
||||||
nonlocal started
|
if evt_job.uuid == job.uuid:
|
||||||
started = started or evt_job.uuid == job.uuid
|
job_started.set()
|
||||||
|
|
||||||
async def end_listener(evt_job: SupervisorJob):
|
async def end_listener(evt_job: SupervisorJob):
|
||||||
nonlocal ended
|
if evt_job.uuid == job.uuid:
|
||||||
ended = ended or evt_job.uuid == job.uuid
|
job_ended.set()
|
||||||
|
|
||||||
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_START, start_listener)
|
async with time_machine.travel(dt):
|
||||||
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, end_listener)
|
job, _ = await test.job_scheduler()
|
||||||
|
|
||||||
await asyncio.sleep(0.2)
|
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_START, start_listener)
|
||||||
|
coresys.bus.register_event(BusEvent.SUPERVISOR_JOB_END, end_listener)
|
||||||
|
|
||||||
|
# Advance time to exactly when job should start and wait for completion
|
||||||
|
async with time_machine.travel(dt + timedelta(seconds=0.1)):
|
||||||
|
await asyncio.wait_for(
|
||||||
|
asyncio.gather(job_started.wait(), job_ended.wait()), timeout=1.0
|
||||||
|
)
|
||||||
|
|
||||||
assert started
|
|
||||||
assert ended
|
|
||||||
assert job.done
|
assert job.done
|
||||||
assert job.name == "test_job_scheduled_at_job_task"
|
assert job.name == "test_job_scheduled_at_job_task"
|
||||||
assert job.stage == "work"
|
assert job.stage == "work"
|
||||||
|
|||||||
@@ -115,17 +115,7 @@ async def test_not_started(coresys):
|
|||||||
assert filter_data(coresys, SAMPLE_EVENT, {}) == SAMPLE_EVENT
|
assert filter_data(coresys, SAMPLE_EVENT, {}) == SAMPLE_EVENT
|
||||||
|
|
||||||
await coresys.core.set_state(CoreState.SETUP)
|
await coresys.core.set_state(CoreState.SETUP)
|
||||||
filtered = filter_data(coresys, SAMPLE_EVENT, {})
|
assert filter_data(coresys, SAMPLE_EVENT, {}) == SAMPLE_EVENT
|
||||||
# During SETUP, we should have basic system info available
|
|
||||||
assert "contexts" in filtered
|
|
||||||
assert "versions" in filtered["contexts"]
|
|
||||||
assert "docker" in filtered["contexts"]["versions"]
|
|
||||||
assert "supervisor" in filtered["contexts"]["versions"]
|
|
||||||
assert "host" in filtered["contexts"]
|
|
||||||
assert "machine" in filtered["contexts"]["host"]
|
|
||||||
assert filtered["contexts"]["versions"]["docker"] == coresys.docker.info.version
|
|
||||||
assert filtered["contexts"]["versions"]["supervisor"] == coresys.supervisor.version
|
|
||||||
assert filtered["contexts"]["host"]["machine"] == coresys.machine
|
|
||||||
|
|
||||||
|
|
||||||
async def test_defaults(coresys):
|
async def test_defaults(coresys):
|
||||||
|
|||||||
@@ -119,10 +119,10 @@ async def test_load(
|
|||||||
"mnt-data-supervisor-mounts-backup_test.mount",
|
"mnt-data-supervisor-mounts-backup_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "noserverino,guest")),
|
["Options", Variant("s", "noserverino,guest")],
|
||||||
("Type", Variant("s", "cifs")),
|
["Type", Variant("s", "cifs")],
|
||||||
("Description", Variant("s", "Supervisor cifs mount: backup_test")),
|
["Description", Variant("s", "Supervisor cifs mount: backup_test")],
|
||||||
("What", Variant("s", "//backup.local/backups")),
|
["What", Variant("s", "//backup.local/backups")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -130,10 +130,10 @@ async def test_load(
|
|||||||
"mnt-data-supervisor-mounts-media_test.mount",
|
"mnt-data-supervisor-mounts-media_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "soft,timeo=200")),
|
["Options", Variant("s", "soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: media_test")),
|
["Description", Variant("s", "Supervisor nfs mount: media_test")],
|
||||||
("What", Variant("s", "media.local:/media")),
|
["What", Variant("s", "media.local:/media")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -141,12 +141,12 @@ async def test_load(
|
|||||||
"mnt-data-supervisor-media-media_test.mount",
|
"mnt-data-supervisor-media-media_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "bind")),
|
["Options", Variant("s", "bind")],
|
||||||
(
|
[
|
||||||
"Description",
|
"Description",
|
||||||
Variant("s", "Supervisor bind mount: bind_media_test"),
|
Variant("s", "Supervisor bind mount: bind_media_test"),
|
||||||
),
|
],
|
||||||
("What", Variant("s", "/mnt/data/supervisor/mounts/media_test")),
|
["What", Variant("s", "/mnt/data/supervisor/mounts/media_test")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -198,10 +198,10 @@ async def test_load_share_mount(
|
|||||||
"mnt-data-supervisor-mounts-share_test.mount",
|
"mnt-data-supervisor-mounts-share_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "soft,timeo=200")),
|
["Options", Variant("s", "soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: share_test")),
|
["Description", Variant("s", "Supervisor nfs mount: share_test")],
|
||||||
("What", Variant("s", "share.local:/share")),
|
["What", Variant("s", "share.local:/share")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -209,9 +209,9 @@ async def test_load_share_mount(
|
|||||||
"mnt-data-supervisor-share-share_test.mount",
|
"mnt-data-supervisor-share-share_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "bind")),
|
["Options", Variant("s", "bind")],
|
||||||
("Description", Variant("s", "Supervisor bind mount: bind_share_test")),
|
["Description", Variant("s", "Supervisor bind mount: bind_share_test")],
|
||||||
("What", Variant("s", "/mnt/data/supervisor/mounts/share_test")),
|
["What", Variant("s", "/mnt/data/supervisor/mounts/share_test")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -318,12 +318,12 @@ async def test_mount_failed_during_load(
|
|||||||
"mnt-data-supervisor-media-media_test.mount",
|
"mnt-data-supervisor-media-media_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "ro,bind")),
|
["Options", Variant("s", "ro,bind")],
|
||||||
(
|
[
|
||||||
"Description",
|
"Description",
|
||||||
Variant("s", "Supervisor bind mount: emergency_media_test"),
|
Variant("s", "Supervisor bind mount: emergency_media_test"),
|
||||||
),
|
],
|
||||||
("What", Variant("s", "/mnt/data/supervisor/emergency/media_test")),
|
["What", Variant("s", "/mnt/data/supervisor/emergency/media_test")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -634,10 +634,10 @@ async def test_reload_mounts_attempts_initial_mount(
|
|||||||
"mnt-data-supervisor-mounts-media_test.mount",
|
"mnt-data-supervisor-mounts-media_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "soft,timeo=200")),
|
["Options", Variant("s", "soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: media_test")),
|
["Description", Variant("s", "Supervisor nfs mount: media_test")],
|
||||||
("What", Variant("s", "media.local:/media")),
|
["What", Variant("s", "media.local:/media")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
@@ -645,9 +645,9 @@ async def test_reload_mounts_attempts_initial_mount(
|
|||||||
"mnt-data-supervisor-media-media_test.mount",
|
"mnt-data-supervisor-media-media_test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "bind")),
|
["Options", Variant("s", "bind")],
|
||||||
("Description", Variant("s", "Supervisor bind mount: bind_media_test")),
|
["Description", Variant("s", "Supervisor bind mount: bind_media_test")],
|
||||||
("What", Variant("s", "/mnt/data/supervisor/mounts/media_test")),
|
["What", Variant("s", "/mnt/data/supervisor/mounts/media_test")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ async def test_cifs_mount(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
(
|
[
|
||||||
"Options",
|
"Options",
|
||||||
Variant(
|
Variant(
|
||||||
"s",
|
"s",
|
||||||
@@ -117,10 +117,10 @@ async def test_cifs_mount(
|
|||||||
]
|
]
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
("Type", Variant("s", "cifs")),
|
["Type", Variant("s", "cifs")],
|
||||||
("Description", Variant("s", "Supervisor cifs mount: test")),
|
["Description", Variant("s", "Supervisor cifs mount: test")],
|
||||||
("What", Variant("s", "//test.local/camera")),
|
["What", Variant("s", "//test.local/camera")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -177,10 +177,10 @@ async def test_cifs_mount_read_only(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "ro,noserverino,guest")),
|
["Options", Variant("s", "ro,noserverino,guest")],
|
||||||
("Type", Variant("s", "cifs")),
|
["Type", Variant("s", "cifs")],
|
||||||
("Description", Variant("s", "Supervisor cifs mount: test")),
|
["Description", Variant("s", "Supervisor cifs mount: test")],
|
||||||
("What", Variant("s", "//test.local/camera")),
|
["What", Variant("s", "//test.local/camera")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -237,10 +237,10 @@ async def test_nfs_mount(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "port=1234,soft,timeo=200")),
|
["Options", Variant("s", "port=1234,soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: test")),
|
["Description", Variant("s", "Supervisor nfs mount: test")],
|
||||||
("What", Variant("s", "test.local:/media/camera")),
|
["What", Variant("s", "test.local:/media/camera")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -283,10 +283,10 @@ async def test_nfs_mount_read_only(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "ro,port=1234,soft,timeo=200")),
|
["Options", Variant("s", "ro,port=1234,soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: test")),
|
["Description", Variant("s", "Supervisor nfs mount: test")],
|
||||||
("What", Variant("s", "test.local:/media/camera")),
|
["What", Variant("s", "test.local:/media/camera")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -331,10 +331,10 @@ async def test_load(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "noserverino,guest")),
|
["Options", Variant("s", "noserverino,guest")],
|
||||||
("Type", Variant("s", "cifs")),
|
["Type", Variant("s", "cifs")],
|
||||||
("Description", Variant("s", "Supervisor cifs mount: test")),
|
["Description", Variant("s", "Supervisor cifs mount: test")],
|
||||||
("What", Variant("s", "//test.local/share")),
|
["What", Variant("s", "//test.local/share")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
@@ -736,10 +736,10 @@ async def test_mount_fails_if_down(
|
|||||||
"mnt-data-supervisor-mounts-test.mount",
|
"mnt-data-supervisor-mounts-test.mount",
|
||||||
"fail",
|
"fail",
|
||||||
[
|
[
|
||||||
("Options", Variant("s", "port=1234,soft,timeo=200")),
|
["Options", Variant("s", "port=1234,soft,timeo=200")],
|
||||||
("Type", Variant("s", "nfs")),
|
["Type", Variant("s", "nfs")],
|
||||||
("Description", Variant("s", "Supervisor nfs mount: test")),
|
["Description", Variant("s", "Supervisor nfs mount: test")],
|
||||||
("What", Variant("s", "test.local:/media/camera")),
|
["What", Variant("s", "test.local:/media/camera")],
|
||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest.mock import ANY, MagicMock, Mock, PropertyMock, call, patch
|
from unittest.mock import ANY, MagicMock, Mock, PropertyMock, patch
|
||||||
|
|
||||||
from awesomeversion import AwesomeVersion
|
from awesomeversion import AwesomeVersion
|
||||||
import pytest
|
import pytest
|
||||||
@@ -11,7 +11,6 @@ from supervisor.const import BusEvent, CpuArch
|
|||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.docker.const import ContainerState
|
from supervisor.docker.const import ContainerState
|
||||||
from supervisor.docker.interface import DockerInterface
|
from supervisor.docker.interface import DockerInterface
|
||||||
from supervisor.docker.manager import DockerAPI
|
|
||||||
from supervisor.docker.monitor import DockerContainerStateEvent
|
from supervisor.docker.monitor import DockerContainerStateEvent
|
||||||
from supervisor.exceptions import (
|
from supervisor.exceptions import (
|
||||||
AudioError,
|
AudioError,
|
||||||
@@ -360,26 +359,21 @@ async def test_load_with_incorrect_image(
|
|||||||
plugin.version = AwesomeVersion("2024.4.0")
|
plugin.version = AwesomeVersion("2024.4.0")
|
||||||
|
|
||||||
container.status = "running"
|
container.status = "running"
|
||||||
coresys.docker.images.inspect.return_value = img_data = (
|
container.attrs["Config"] = {"Labels": {"io.hass.version": "2024.4.0"}}
|
||||||
coresys.docker.images.inspect.return_value
|
|
||||||
| {"Config": {"Labels": {"io.hass.version": "2024.4.0"}}}
|
|
||||||
)
|
|
||||||
container.attrs |= img_data
|
|
||||||
|
|
||||||
with patch.object(DockerAPI, "pull_image", return_value=img_data) as pull_image:
|
await plugin.load()
|
||||||
await plugin.load()
|
|
||||||
pull_image.assert_called_once_with(
|
|
||||||
ANY, correct_image, "2024.4.0", platform="linux/amd64", auth=None
|
|
||||||
)
|
|
||||||
|
|
||||||
container.remove.assert_called_once_with(force=True, v=True)
|
container.remove.assert_called_once_with(force=True, v=True)
|
||||||
assert coresys.docker.images.delete.call_args_list[0] == call(
|
assert coresys.docker.images.remove.call_args_list[0].kwargs == {
|
||||||
f"{old_image}:latest",
|
"image": f"{old_image}:latest",
|
||||||
force=True,
|
"force": True,
|
||||||
)
|
}
|
||||||
assert coresys.docker.images.delete.call_args_list[1] == call(
|
assert coresys.docker.images.remove.call_args_list[1].kwargs == {
|
||||||
f"{old_image}:2024.4.0",
|
"image": f"{old_image}:2024.4.0",
|
||||||
force=True,
|
"force": True,
|
||||||
|
}
|
||||||
|
coresys.docker.pull_image.assert_called_once_with(
|
||||||
|
ANY, correct_image, "2024.4.0", platform="linux/amd64"
|
||||||
)
|
)
|
||||||
assert plugin.image == correct_image
|
assert plugin.image == correct_image
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ from unittest.mock import MagicMock, patch
|
|||||||
|
|
||||||
from supervisor.const import CoreState
|
from supervisor.const import CoreState
|
||||||
from supervisor.coresys import CoreSys
|
from supervisor.coresys import CoreSys
|
||||||
from supervisor.resolution.evaluations.operating_system import EvaluateOperatingSystem
|
from supervisor.resolution.evaluations.operating_system import (
|
||||||
|
SUPPORTED_OS,
|
||||||
|
EvaluateOperatingSystem,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_evaluation(coresys: CoreSys):
|
async def test_evaluation(coresys: CoreSys):
|
||||||
@@ -22,7 +25,13 @@ async def test_evaluation(coresys: CoreSys):
|
|||||||
assert operating_system.reason in coresys.resolution.unsupported
|
assert operating_system.reason in coresys.resolution.unsupported
|
||||||
|
|
||||||
coresys.os._available = True
|
coresys.os._available = True
|
||||||
assert coresys.os.available
|
await operating_system()
|
||||||
|
assert operating_system.reason not in coresys.resolution.unsupported
|
||||||
|
coresys.os._available = False
|
||||||
|
|
||||||
|
coresys.host._info = MagicMock(
|
||||||
|
operating_system=SUPPORTED_OS[0], timezone=None, timezone_tzinfo=None
|
||||||
|
)
|
||||||
await operating_system()
|
await operating_system()
|
||||||
assert operating_system.reason not in coresys.resolution.unsupported
|
assert operating_system.reason not in coresys.resolution.unsupported
|
||||||
|
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
"""Test evaluation supported system architectures."""
|
|
||||||
|
|
||||||
from unittest.mock import PropertyMock, patch
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from supervisor.const import CoreState
|
|
||||||
from supervisor.coresys import CoreSys
|
|
||||||
from supervisor.resolution.evaluations.system_architecture import (
|
|
||||||
EvaluateSystemArchitecture,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("arch", ["i386", "armhf", "armv7"])
|
|
||||||
async def test_evaluation_unsupported_architectures(
|
|
||||||
coresys: CoreSys,
|
|
||||||
arch: str,
|
|
||||||
):
|
|
||||||
"""Test evaluation of unsupported system architectures."""
|
|
||||||
system_architecture = EvaluateSystemArchitecture(coresys)
|
|
||||||
await coresys.core.set_state(CoreState.INITIALIZE)
|
|
||||||
|
|
||||||
with patch.object(
|
|
||||||
type(coresys.supervisor), "arch", PropertyMock(return_value=arch)
|
|
||||||
):
|
|
||||||
await system_architecture()
|
|
||||||
assert system_architecture.reason in coresys.resolution.unsupported
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("arch", ["amd64", "aarch64"])
|
|
||||||
async def test_evaluation_supported_architectures(
|
|
||||||
coresys: CoreSys,
|
|
||||||
arch: str,
|
|
||||||
):
|
|
||||||
"""Test evaluation of supported system architectures."""
|
|
||||||
system_architecture = EvaluateSystemArchitecture(coresys)
|
|
||||||
await coresys.core.set_state(CoreState.INITIALIZE)
|
|
||||||
|
|
||||||
with patch.object(
|
|
||||||
type(coresys.supervisor), "arch", PropertyMock(return_value=arch)
|
|
||||||
):
|
|
||||||
await system_architecture()
|
|
||||||
assert system_architecture.reason not in coresys.resolution.unsupported
|
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
"""Test fixup addon execute repair."""
|
"""Test fixup addon execute repair."""
|
||||||
|
|
||||||
from http import HTTPStatus
|
from unittest.mock import MagicMock, patch
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import aiodocker
|
from docker.errors import NotFound
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from supervisor.addons.addon import Addon
|
from supervisor.addons.addon import Addon
|
||||||
@@ -18,9 +17,7 @@ from supervisor.resolution.fixups.addon_execute_repair import FixupAddonExecuteR
|
|||||||
|
|
||||||
async def test_fixup(docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon):
|
async def test_fixup(docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon):
|
||||||
"""Test fixup rebuilds addon's container."""
|
"""Test fixup rebuilds addon's container."""
|
||||||
docker.images.inspect.side_effect = aiodocker.DockerError(
|
docker.images.get.side_effect = NotFound("missing")
|
||||||
HTTPStatus.NOT_FOUND, {"message": "missing"}
|
|
||||||
)
|
|
||||||
install_addon_ssh.data["image"] = "test_image"
|
install_addon_ssh.data["image"] = "test_image"
|
||||||
|
|
||||||
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
||||||
@@ -44,9 +41,7 @@ async def test_fixup_max_auto_attempts(
|
|||||||
docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon
|
docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon
|
||||||
):
|
):
|
||||||
"""Test fixup stops being auto-applied after 5 failures."""
|
"""Test fixup stops being auto-applied after 5 failures."""
|
||||||
docker.images.inspect.side_effect = aiodocker.DockerError(
|
docker.images.get.side_effect = NotFound("missing")
|
||||||
HTTPStatus.NOT_FOUND, {"message": "missing"}
|
|
||||||
)
|
|
||||||
install_addon_ssh.data["image"] = "test_image"
|
install_addon_ssh.data["image"] = "test_image"
|
||||||
|
|
||||||
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
||||||
@@ -87,6 +82,8 @@ async def test_fixup_image_exists(
|
|||||||
docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon
|
docker: DockerAPI, coresys: CoreSys, install_addon_ssh: Addon
|
||||||
):
|
):
|
||||||
"""Test fixup dismisses if image exists."""
|
"""Test fixup dismisses if image exists."""
|
||||||
|
docker.images.get.return_value = MagicMock()
|
||||||
|
|
||||||
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
addon_execute_repair = FixupAddonExecuteRepair(coresys)
|
||||||
assert addon_execute_repair.auto is True
|
assert addon_execute_repair.auto is True
|
||||||
|
|
||||||
|
|||||||
@@ -86,22 +86,6 @@ def test_format_verbose_newlines():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_format_verbose_colors():
|
|
||||||
"""Test verbose formatter with ANSI colors in message."""
|
|
||||||
fields = {
|
|
||||||
"__REALTIME_TIMESTAMP": "1379403171000000",
|
|
||||||
"_HOSTNAME": "homeassistant",
|
|
||||||
"SYSLOG_IDENTIFIER": "python",
|
|
||||||
"_PID": "666",
|
|
||||||
"MESSAGE": "\x1b[32mHello, world!\x1b[0m",
|
|
||||||
}
|
|
||||||
|
|
||||||
assert (
|
|
||||||
journal_verbose_formatter(fields)
|
|
||||||
== "2013-09-17 07:32:51.000 homeassistant python[666]: \x1b[32mHello, world!\x1b[0m"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_parsing_simple():
|
async def test_parsing_simple():
|
||||||
"""Test plain formatter."""
|
"""Test plain formatter."""
|
||||||
journal_logs, stream = _journal_logs_mock()
|
journal_logs, stream = _journal_logs_mock()
|
||||||
@@ -313,54 +297,3 @@ async def test_parsing_non_utf8_in_binary_message():
|
|||||||
)
|
)
|
||||||
_, line = await anext(journal_logs_reader(journal_logs))
|
_, line = await anext(journal_logs_reader(journal_logs))
|
||||||
assert line == "Hello, \ufffd world!"
|
assert line == "Hello, \ufffd world!"
|
||||||
|
|
||||||
|
|
||||||
def test_format_plain_no_colors():
|
|
||||||
"""Test plain formatter strips ANSI color codes when no_colors=True."""
|
|
||||||
fields = {"MESSAGE": "\x1b[32mHello, world!\x1b[0m"}
|
|
||||||
assert journal_plain_formatter(fields, no_colors=True) == "Hello, world!"
|
|
||||||
|
|
||||||
|
|
||||||
def test_format_verbose_no_colors():
|
|
||||||
"""Test verbose formatter strips ANSI color codes when no_colors=True."""
|
|
||||||
fields = {
|
|
||||||
"__REALTIME_TIMESTAMP": "1379403171000000",
|
|
||||||
"_HOSTNAME": "homeassistant",
|
|
||||||
"SYSLOG_IDENTIFIER": "python",
|
|
||||||
"_PID": "666",
|
|
||||||
"MESSAGE": "\x1b[32mHello, world!\x1b[0m",
|
|
||||||
}
|
|
||||||
assert (
|
|
||||||
journal_verbose_formatter(fields, no_colors=True)
|
|
||||||
== "2013-09-17 07:32:51.000 homeassistant python[666]: Hello, world!"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_parsing_colored_logs_verbose_no_colors():
|
|
||||||
"""Test verbose formatter strips colors from colored logs."""
|
|
||||||
journal_logs, stream = _journal_logs_mock()
|
|
||||||
stream.feed_data(
|
|
||||||
b"__REALTIME_TIMESTAMP=1379403171000000\n"
|
|
||||||
b"_HOSTNAME=homeassistant\n"
|
|
||||||
b"SYSLOG_IDENTIFIER=python\n"
|
|
||||||
b"_PID=666\n"
|
|
||||||
b"MESSAGE\n\x0e\x00\x00\x00\x00\x00\x00\x00\x1b[31mERROR\x1b[0m\n"
|
|
||||||
b"AFTER=after\n\n"
|
|
||||||
)
|
|
||||||
_, line = await anext(
|
|
||||||
journal_logs_reader(
|
|
||||||
journal_logs, log_formatter=LogFormatter.VERBOSE, no_colors=True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
assert line == "2013-09-17 07:32:51.000 homeassistant python[666]: ERROR"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_parsing_multiple_color_codes():
|
|
||||||
"""Test stripping multiple ANSI color codes in single message."""
|
|
||||||
journal_logs, stream = _journal_logs_mock()
|
|
||||||
stream.feed_data(
|
|
||||||
b"MESSAGE\n\x29\x00\x00\x00\x00\x00\x00\x00\x1b[31mRed\x1b[0m \x1b[32mGreen\x1b[0m \x1b[34mBlue\x1b[0m\n"
|
|
||||||
b"AFTER=after\n\n"
|
|
||||||
)
|
|
||||||
_, line = await anext(journal_logs_reader(journal_logs, no_colors=True))
|
|
||||||
assert line == "Red Green Blue"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user