diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 0000000..a9286ff --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,139 @@ +name: Tests + +on: + workflow_call: + workflow_dispatch: + pull_request: + types: [opened, synchronize, reopened] + branches: [main] + paths-ignore: + - "**.md" + - "**.rst" + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} + cancel-in-progress: true + +jobs: + lint: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Complete git history is required to generate the version from git tags. + + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install dependencies + run: | + sudo apt update + sudo apt install -y yamllint + python -m pip install --upgrade pip + # pin tox to the current major version to avoid + # workflows breaking all at once when a new major version is released. + python -m pip install 'tox<5' + + - name: Run linters + run: tox -e lint + + - name: Lint yaml files + run: | + yamllint .yamllint snap/snapcraft.yaml + + unit: + name: Unit + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.10"] + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install 'tox<5' + + - name: Run unit tests + run: tox -e unit + + build: + needs: + - lint + runs-on: ${{ matrix.runs-on }} + strategy: + fail-fast: false + matrix: + runs-on: [[ubuntu-22.04]] + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Complete git history is required to generate the version from git tags. + + - name: Verify snap builds successfully + id: build + uses: canonical/action-build@v1 + + - name: Determine system architecture + run: echo "SYSTEM_ARCH=$(uname -m)" >> $GITHUB_ENV + + - name: Upload the built snap + uses: actions/upload-artifact@v4 + with: + name: snap_${{ env.SYSTEM_ARCH }} + path: ${{ steps.build.outputs.snap }} + + func: + needs: + - build + runs-on: ${{ matrix.runs-on }} + strategy: + fail-fast: false + matrix: + runs-on: [[ubuntu-22.04]] + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Complete git history is required to generate the version from git tags. + + - name: Determine system architecture + run: echo "SYSTEM_ARCH=$(uname -m)" >> $GITHUB_ENV + + - name: Download snap file artifact + uses: actions/download-artifact@v4 + with: + name: snap_${{ env.SYSTEM_ARCH }} + + - name: Set up Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install 'tox<5' + + - name: Setup Juju environment + uses: charmed-kubernetes/actions-operator@main + with: + provider: "lxd" + juju-channel: "3.5/stable" + charmcraft-channel: "3.x/stable" + + - name: Run func tests + run: | + export TEST_SNAP="$(pwd)/$(ls | grep '.*_.*\.snap$')" + echo "$TEST_SNAP" + tox -e func diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml deleted file mode 100644 index 31677a6..0000000 --- a/.github/workflows/pr.yaml +++ /dev/null @@ -1,30 +0,0 @@ -# This is a template `pr.yaml` file for snaps -# This file is managed by bootstack-charms-spec and should not be modified -# within individual snap repos. https://launchpad.net/bootstack-charms-spec - -name: PR workflow running lint checkers, unit and functional tests - -on: - workflow_dispatch: - workflow_call: - pull_request: - types: [ opened, synchronize, reopened ] - branches: [ master, main , '[0-9].[0-9]+', '[0-9]+'] - paths-ignore: - - '**.md' - - '**.rst' - -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref || github.ref }} - cancel-in-progress: true - -jobs: - pr: - uses: canonical/bootstack-actions/.github/workflows/pull-request.yaml@main - secrets: inherit - with: - python-version-unit: "['3.8', '3.10']" - python-version-func: "3.10" - juju-channel: "3.4/stable" - snapcraft: true - commands: "['make functional']" diff --git a/.github/workflows/promote.yaml b/.github/workflows/promote.yaml index 3bd5bef..b4c1304 100644 --- a/.github/workflows/promote.yaml +++ b/.github/workflows/promote.yaml @@ -1,19 +1,25 @@ -name: Promote snap to other tracks and channels +# This file is centrally managed as a template file in https://github.com/canonical/solutions-engineering-automation +# To update the file: +# - Edit it in the canonical/solutions-engineering-automation repository. +# - Open a PR with the changes. +# - When the PR merges, the soleng-terraform bot will open a PR to the target repositories with the changes. +name: Promote snap to default track, standard risk levels. on: workflow_dispatch: inputs: - destination-channel: - description: 'Destination Channel, e.g. latest/candidate' - required: true - origin-channel: - description: 'Origin Channel, e.g. latest/edge' + channel-promotion: + description: 'Channel Promotion, e.g. latest/edge -> latest/candidate' required: true + type: choice + options: + - 'latest/edge -> latest/candidate' + - 'latest/candidate -> latest/stable' jobs: promote-snap: name: Promote snap - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - name: Snapcraft install @@ -21,6 +27,14 @@ jobs: - name: Get snap name id: snap run: echo "name=$(awk '/^name:/ {print $2}' snap/snapcraft.yaml)" >> "$GITHUB_OUTPUT" + - name: Set channels + id: set-channels + run: | + channel_promotion="${{ github.event.inputs.channel-promotion }}" + origin=$(echo "$channel_promotion" | sed 's/\s*->.*//') + destination=$(echo "$channel_promotion" | sed 's/.*->\s*//') + echo "destination-channel=$destination" >> $GITHUB_OUTPUT + echo "origin-channel=$origin" >> $GITHUB_OUTPUT - name: Snapcraft promote snap env: SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.STORE_LOGIN }} @@ -30,5 +44,5 @@ jobs: # refuse to non-interactively promote a snap from the edge # channel if it is done without any branch qualifiers yes | snapcraft promote ${{ steps.snap.outputs.name }} \ - --from-channel ${{ github.event.inputs.origin-channel }} \ - --to-channel ${{ github.event.inputs.destination-channel }} + --from-channel ${{ steps.set-channels.outputs.origin-channel }} \ + --to-channel ${{ steps.set-channels.outputs.destination-channel }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 1209a8b..dff8e42 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,3 +1,8 @@ +# This file is centrally managed as a template file in https://github.com/canonical/solutions-engineering-automation +# To update the file: +# - Edit it in the canonical/solutions-engineering-automation repository. +# - Open a PR with the changes. +# - When the PR merges, the soleng-terraform bot will open a PR to the target repositories with the changes. name: Publish snap on: @@ -8,27 +13,35 @@ on: jobs: check: - uses: ./.github/workflows/pr.yaml + uses: ./.github/workflows/check.yaml secrets: inherit release: - runs-on: ubuntu-latest + runs-on: ${{ matrix.runs-on }} needs: check - outputs: - snap: ${{ steps.build.outputs.snap }} + strategy: + fail-fast: false + matrix: + runs-on: [[ubuntu-22.04]] steps: - uses: actions/checkout@v4 with: - fetch-depth: 0 # needed for version determination - - uses: snapcore/action-build@v1 - id: build - - uses: actions/upload-artifact@v4 + fetch-depth: 0 # Complete git history is required to generate the version from git tags. + + - name: Determine system architecture + run: echo "SYSTEM_ARCH=$(uname -m)" >> $GITHUB_ENV + + - name: Download the built snap from check workflow + uses: actions/download-artifact@v4 with: - name: snap - path: ${{ steps.build.outputs.snap }} + name: snap_${{ env.SYSTEM_ARCH }} + + - name: Find the downloaded snap file + run: echo "SNAP_FILE=$(find . -name "*.snap")" >> $GITHUB_ENV + - uses: snapcore/action-publish@v1 env: SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.STORE_LOGIN }} with: - snap: ${{ steps.build.outputs.snap }} + snap: ${{ env.SNAP_FILE }} release: latest/edge,3/edge diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml deleted file mode 100644 index 769738b..0000000 --- a/.pre-commit-config.yaml +++ /dev/null @@ -1,22 +0,0 @@ -# This is a template `.pre-commit-config.yaml` file for snaps -# This file is managed by bootstack-charms-spec and should not be modified -# within individual snap repos. https://launchpad.net/bootstack-charms-spec - -repos: - - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 - hooks: - - id: check-executables-have-shebangs - - id: check-merge-conflict - - id: end-of-file-fixer - - id: trailing-whitespace - - id: check-added-large-files - - id: check-json - - id: check-yaml - - repo: local - hooks: - - id: lint - name: lint - entry: make lint - language: system - types: [ python ] diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..669c864 --- /dev/null +++ b/.yamllint @@ -0,0 +1,5 @@ +extends: default + +rules: + line-length: disable + document-start: disable diff --git a/Makefile b/Makefile deleted file mode 100644 index 9cb0df0..0000000 --- a/Makefile +++ /dev/null @@ -1,64 +0,0 @@ -# This is a template `Makefile` file for snaps -# This file is managed by bootstack-charms-spec and should not be modified -# within individual snap repos. https://launchpad.net/bootstack-charms-spec - -PYTHON := /usr/bin/python3 - -PROJECTPATH=$(dir $(realpath ${MAKEFILE_LIST})) -SNAP_NAME=$(shell cat ${PROJECTPATH}/snap/snapcraft.yaml | grep -E '^name:' | awk '{print $$2}') -SNAP_FILE=${PROJECTPATH}/${SNAP_NAME}.snap - -help: - @echo "This project supports the following targets" - @echo "" - @echo " make help - show this text" - @echo " make clean - remove unneeded files" - @echo " make dev-environment - setup the development environment" - @echo " make build - build the snap" - @echo " make lint - run lint checkers" - @echo " make reformat - run lint tools to auto format code" - @echo " make unittests - run the tests defined in the unittest subdirectory" - @echo " make functional - run the tests defined in the functional subdirectory" - @echo " make test - run lint, proof, unittests and functional targets" - @echo " make pre-commit - run pre-commit checks on all the files" - @echo "" - -lint: - @echo "Running lint checks" - @tox -e lint - -unittests: - @echo "Running unit tests" - @tox -e unit -- ${UNIT_ARGS} - -test: lint unittests functional - @echo "Tests completed for the snap." - -reformat: - @echo "Reformat files with black and isort" - @tox -e reformat - -build: - @echo "Building the snap" - @snapcraft --use-lxd - @bash -c ./rename.sh - -clean: - @echo "Cleaning snap" - @snapcraft clean --use-lxd - @echo "Cleaning existing snap builds" - @rm -rf ${SNAP_FILE} - -dev-environment: - @echo "Creating virtualenv and installing pre-commit" - @tox -r -e dev-environment - -functional: build - @echo "Executing functional tests using built snap" - @TEST_SNAP=${SNAP_FILE} tox -e func -- ${FUNC_ARGS} - -pre-commit: - @tox -e pre-commit - -# The targets below don't depend on a file -.PHONY: help clean dev-environment build lint reformat unittests functional test pre-commit diff --git a/rename.sh b/rename.sh deleted file mode 100755 index 6d645e1..0000000 --- a/rename.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# This is a template `rename.sh` file for snaps -# This file is managed by bootstack-charms-spec and should not be modified -# within individual snap repos. https://launchpad.net/bootstack-charms-spec - -snap=$(grep -E "^name:" snap/snapcraft.yaml | awk '{print $2}') -echo "renaming ${snap}_*.snap to ${snap}.snap" -echo -n "pwd: " -pwd -ls -al -echo "Removing previous snap if it exists" -if [[ -e "${snap}.snap" ]]; -then - rm "${snap}.snap" -fi -echo "Renaming snap here." -mv ${snap}_*.snap ${snap}.snap diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 9bcefab..467233a 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -90,7 +90,7 @@ apps: command: bin/prometheus-juju-exporter plugs: [network, network-bind] environment: - PROMETHEUSJUJUEXPORTERDIR: $SNAP_DATA/ + PROMETHEUSJUJUEXPORTERDIR: $SNAP_DATA/ parts: prometheus-juju-exporter: diff --git a/tox.ini b/tox.ini index 25c4441..24ed76e 100644 --- a/tox.ini +++ b/tox.ini @@ -63,6 +63,7 @@ deps = commands = pytest {toxinidir}/tests/unit \ {posargs:-v --cov --cov-report=term-missing --cov-report=html --cov-report=xml} +# NOTE: requires a juju model [testenv:func] deps = -r {toxinidir}/requirements.txt