Skip to content

Commit

Permalink
ci(tests): use simplify intergration tests for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
syu-w committed Nov 22, 2023
1 parent db8981c commit f34b65b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ jobs:
done
echo "::endgroup::"
- name: Setup Tox environments
run: tox run --colored yes -e integration-py${{ matrix.python }} --notest
run: tox run --colored yes -e integration-smoketests-py${{ matrix.python }} --notest
- name: Enable ssh access
uses: mxschmitt/action-tmate@v3
if: ${{ inputs.enable_ssh_access }}
with:
limit-access-to-actor: true
- name: Run integration tests on MacOS
- name: Run integration smoketests on MacOS
env:
CRAFT_PROVIDERS_TESTS_ENABLE_MULTIPASS_INSTALL: 1
CRAFT_PROVIDERS_TESTS_ENABLE_MULTIPASS_UNINSTALL: 1
PYTEST_ADDOPTS: "--no-header -vv -rN"
run: |
.tox/.tox/bin/tox run --skip-pkg-install --no-list-dependencies --colored yes -e integration-py${{ matrix.python }}
.tox/.tox/bin/tox run --skip-pkg-install --no-list-dependencies --colored yes -e integration-smoketests-py${{ matrix.python }}
108 changes: 108 additions & 0 deletions tests/integration/multipass/test_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#
# Copyright 2021-2023 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

import pathlib
import subprocess

import pytest
from craft_providers import multipass
from craft_providers.bases import ubuntu


@pytest.fixture()
def simple_file(home_tmp_path):
"""Create a file in the home directory (accessible by Multipass)."""
file = home_tmp_path / "src.txt"
file.write_text("this is a test")
return file


@pytest.mark.smoketest()
def test_smoketest(instance_name, home_tmp_path):
"""Launch an instance and run some basic tasks."""

assert multipass.is_installed()

base_configuration = ubuntu.BuilddBase(alias=ubuntu.BuilddBaseAlias.JAMMY)

instance = multipass.launch(
name=instance_name,
base_configuration=base_configuration,
image_name="snapcraft:core22",
cpus=2,
disk_gb=8,
mem_gb=4,
)

try:
assert isinstance(instance, multipass.MultipassInstance)
assert instance.exists() is True
assert instance.is_running() is True

# test execute command
proc = instance.execute_run(["echo", "hi"], check=True, stdout=subprocess.PIPE)
assert proc.stdout == b"hi\n"

# test push file
test_file = pathlib.Path(home_tmp_path / "src.txt")
test_file.write_text("this is a test")
test_file.chmod(0o755)
destination = pathlib.Path("/tmp/test.txt")
test_file_pull = pathlib.Path(home_tmp_path / "src2.txt")

instance.push_file(source=test_file, destination=destination)

proc = instance.execute_run(
command=["cat", str(destination)], capture_output=True
)
assert proc.stdout.decode() == "this is a test"

proc = instance.execute_run(
command=["stat", "--format", "%a:%U:%G", str(destination)],
capture_output=True,
text=True,
)
assert proc.stdout.strip() == "755:ubuntu:ubuntu"

# test pull file
instance.pull_file(source=destination, destination=test_file_pull)
assert test_file.read_bytes() == test_file_pull.read_bytes()

# test mount and unmount
target = pathlib.Path("/tmp/mnt")
assert instance.is_mounted(host_source=home_tmp_path, target=target) is False

instance.mount(host_source=home_tmp_path, target=target)
assert instance.is_mounted(host_source=home_tmp_path, target=target) is True

proc = instance.execute_run(
command=["cat", "/tmp/mnt/src.txt"],
capture_output=True,
)
assert proc.stdout == test_file.read_bytes()

instance.unmount(target=target)
assert instance.is_mounted(host_source=home_tmp_path, target=target) is False

# test stop instance
instance.stop()
assert instance.is_running() is False
finally:
instance.delete()

# test delete instance
assert instance.exists() is False
14 changes: 13 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ labels =
change_dir =
unit: tests/unit
integration: tests/integration
commands = pytest {tty:--color=yes} --cov --cov-report=xml:{tox_root}/results/coverage-{env_name}.xml --junit-xml={tox_root}/results/test-results-{env_name}.xml {posargs}
commands =
unit: pytest {tty:--color=yes} --cov --cov-report=xml:{tox_root}/results/coverage-{env_name}.xml --junit-xml={tox_root}/results/test-results-{env_name}.xml {posargs}
integration: pytest {tty:--color=yes} --cov --cov-report=xml:{tox_root}/results/coverage-{env_name}.xml --junit-xml={tox_root}/results/test-results-{env_name}.xml -m "not smoketest" {posargs}

[testenv:integration-smoketests-py3.{8,9,10,11,12}] # Configuration for all tests using pytest
base = testenv, test
description =
integration: Run integration smoketests with pytest
labels =
integration-smoketest-py3.{8,10,11}: integration-smoketests
change_dir =
integration: tests/integration
commands = pytest {tty:--color=yes} --cov --cov-report=xml:{tox_root}/results/coverage-{env_name}.xml --junit-xml={tox_root}/results/test-results-{env_name}.xml -m smoketest {posargs}

[lint] # Standard linting configuration
package = editable
Expand Down

0 comments on commit f34b65b

Please sign in to comment.