Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start a new workflow for PR CI. #227

Merged
merged 9 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ jobs:
make lint

- name: Run unit tests
if: matrix.python-version != '3.8'
run: |
make test/unit

- name: Run functional tests
run: |
make test/functional

- name: run the integration tests
run: make test/integration


publish:
name: Build and publish to PyPI registry
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ test/unit/annotate:
test/unit/annotate/clean:
find galaxy_importer -type f -name '*,cover' -delete

.PHONY: test/functional
test/functional:
@sh tests/functional/*
.PHONY: test/integration
test/integration:
@pytest tests/integration -v
61 changes: 0 additions & 61 deletions tests/functional/run_importer.sh

This file was deleted.

85 changes: 85 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import atexit
import os
import shutil
import subprocess
import tempfile

import pytest


def clean_files(path):
shutil.rmtree(path)


@pytest.fixture
def workdir():
tdir = tempfile.mkdtemp()
atexit.register(clean_files, tdir)
return tdir


@pytest.fixture
def local_image_config():
config = [
"[galaxy-importer]",
"RUN_ANSIBLE_TEST=True",
"ANSIBLE_TEST_LOCAL_IMAGE=True",
"LOCAL_IMAGE_DOCKER=True",
]
config = "\n".join(config)

tdir = tempfile.mkdtemp()
atexit.register(clean_files, tdir)

config_path = os.path.join(tdir, "galaxy-importer.cfg")
with open(config_path, "w") as f:
f.write(config)

return {"GALAXY_IMPORTER_CONFIG": config_path}


@pytest.fixture
def simple_artifact():
tdir = tempfile.mkdtemp()
atexit.register(clean_files, tdir)

cmd = "ansible-galaxy collection init foo.bar"
pid = subprocess.run(
cmd, shell=True, cwd=tdir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
assert pid.returncode == 0, pid.stdout

coldir = os.path.join(tdir, "foo", "bar")
metadir = os.path.join(coldir, "meta")

if not os.path.exists(metadir):
os.makedirs(metadir)
with open(os.path.join(metadir, "runtime.yml"), "w") as f:
f.write("requires_ansible: '>=2.9.10,<2.11.5'\n")

cmd = "ansible-galaxy collection build ."
pid = subprocess.run(
cmd, shell=True, cwd=coldir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
lines = pid.stdout.decode("utf-8").split("\n")
tarball = None
for line in lines:
line = line.strip()
if not line or not line.endswith(".tar.gz"):
continue
tarball = line.split()[-1]
assert tarball, lines
return tarball


@pytest.fixture
def simple_legacy_role():
tdir = tempfile.mkdtemp()
atexit.register(clean_files, tdir)

cmd = "ansible-galaxy role init bar_role"
pid = subprocess.run(
cmd, shell=True, cwd=tdir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
assert pid.returncode == 0, pid.stdout
return os.path.join(tdir, "bar_role")
90 changes: 90 additions & 0 deletions tests/integration/test_containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import copy
import json
import os
import subprocess


def test_local_build_container_with_collection(workdir, local_image_config, simple_artifact):
# use the parent env to preserve venv vars
env = copy.deepcopy(dict(os.environ))
env.update(local_image_config)

# this relies on the GALAXY_IMPORTER_CONFIG to know where to find the config
# it should also spawn an image build and use that image for the ansible-test phase
cmd = f"python3 -m galaxy_importer.main {simple_artifact}"
pid = subprocess.run(
cmd, shell=True, cwd=workdir, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
assert pid.returncode == 0, pid.stdout

# the log should contain all the relevant messages
log = pid.stdout.decode("utf-8")
assert "Running ansible-test sanity on" in log
assert "Running sanity test" in log
assert "ansible-test sanity complete." in log
assert "No EDA content found. Skipping linters." in log
assert "Removing temporary files, image and container" in log
assert "Importer processing completed successfully" in log

# it should have stored structured data in the pwd
results_file = os.path.join(workdir, "importer_result.json")
assert os.path.exists(results_file)
with open(results_file, "r") as f:
results = json.loads(f.read())

# the data should have all the relevant bits
assert results["contents"] == []
assert results["docs_blob"]["contents"] == []
assert results["docs_blob"]["collection_readme"]["name"] == "README.md"
assert results["docs_blob"]["collection_readme"]["html"]
assert results["docs_blob"]["documentation_files"] == []
assert results["metadata"]["namespace"] == "foo"
assert results["metadata"]["name"] == "bar"
assert results["metadata"]["version"] == "1.0.0"
assert results["requires_ansible"] == ">=2.9.10,<2.11.5"


def test_local_build_container_with_legacy_role(local_image_config, simple_legacy_role):
# use the parent env to preserve venv vars
env = copy.deepcopy(dict(os.environ))
env.update(local_image_config)

# this relies on the GALAXY_IMPORTER_CONFIG to know where to find the config
# it should also spawn an image build and use that image for the ansible-test phase
cmd = (
f"python3 -m galaxy_importer.main {simple_legacy_role}"
+ " --legacy-role --namespace foo-namespace"
)
workdir = os.path.dirname(simple_legacy_role)
pid = subprocess.run(
cmd, shell=True, cwd=workdir, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
assert pid.returncode == 0, pid.stdout

# the log should contain all the relevant messages
log = pid.stdout.decode("utf-8")

assert "Determined role name to be bar_role" in log
assert "Linting role bar_role via ansible-lint..." in log
assert "Should change default metadata: author" in log
assert "Should change default metadata: company" in log
assert "Should change default metadata: license" in log
assert "Role info should contain platforms" in log
assert "All plays should be named." in log
assert "...ansible-lint run complete" in log
assert "Legacy role loading complete" in log

# it should have stored structured data in the pwd
results_file = os.path.join(workdir, "importer_result.json")
assert os.path.exists(results_file)
with open(results_file, "r") as f:
results = json.loads(f.read())

# the data should have all the relevant bits
assert results["metadata"]["dependencies"] == []
assert results["metadata"]["galaxy_info"]["author"] == "your name"
assert results["metadata"]["galaxy_info"]["description"] == "your role description"
assert results["metadata"]["galaxy_info"]["role_name"] is None
assert results["name"] == "bar_role"
assert results["namespace"] == "foo-namespace"
assert results["readme_html"]