Skip to content

Commit

Permalink
Started to make a script to automatically generate the results expect…
Browse files Browse the repository at this point in the history
…ed from CI
  • Loading branch information
ESultanik committed Aug 18, 2021
1 parent 7d035f6 commit 4d86c21
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 3,759 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/repos
7 changes: 7 additions & 0 deletions it_depends/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def run(
remove: bool = True,
interactive: bool = True,
mounts: Optional[Iterable[Tuple[Union[str, Path], Union[str, Path]]]] = None,
privileged: bool = False,
env: Optional[Dict[str, str]] = None,
stdin=None,
stdout=None,
Expand Down Expand Up @@ -180,6 +181,9 @@ def run(
if mounts is not None:
for source, target in mounts:
cmd_args.append("-v")
if not isinstance(source, Path):
source = Path(source)
source = source.absolute()
cmd_args.append(f"{source!s}:{target!s}:cached")

if env is not None:
Expand All @@ -188,6 +192,9 @@ def run(
escaped_value = v.replace('"', '\\"')
cmd_args.append(f"{k}={escaped_value}")

if privileged:
cmd_args.append("--privileged=true")

cmd_args.append(self.name)

cmd_args.extend(args)
Expand Down
80 changes: 80 additions & 0 deletions test/rebuild_expected_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Rebuilds repos/*.expected.json by running the tests in a Docker container to match how they would be run in CI
"""

from pathlib import Path
from typing import Optional

from it_depends.docker import DockerContainer, Dockerfile

from test_smoke import IT_DEPENDS_DIR, SmokeTest, SMOKE_TESTS


CI_TEST_PATH: Path = Path(__file__).parent.parent / ".github" / "workflows" / "tests.yml"
_CONTAINER: Optional[DockerContainer] = None


def container_type() -> str:
"""Returns the Docker container name used in GitHub CI"""
if not CI_TEST_PATH.exists():
raise ValueError(f"GitHub action file {CI_TEST_PATH!s} does not exist!")
with open(CI_TEST_PATH, "r") as f:
for line in f.readlines():
line = line.strip()
if line.startswith("runs-on:"):
github_name = line[len("runs-on:"):].lstrip()
hyphen_index = github_name.find("-")
if hyphen_index < 0:
raise ValueError(f"Unknown runs-on: container type {github_name!r} in {CI_TEST_PATH}")
return f"{github_name[:hyphen_index]}:{github_name[hyphen_index+1:]}"
raise ValueError(f"Did not find `runs-on: ...` line in {CI_TEST_PATH}")


def get_container() -> DockerContainer:
global _CONTAINER
if _CONTAINER is None:
dockerfile = Dockerfile(IT_DEPENDS_DIR / "Dockerfile")
dockerfile_existed = dockerfile.exists()
try:
if not dockerfile_existed:
with open(dockerfile.path, "w") as f:
f.write(f"""FROM {container_type()}
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \\
DEBIAN_FRONTEND=noninteractive apt-get install -y python3 python3-dev python3-pip docker \\
cmake autoconf golang cargo npm clang libz3-dev \\
&& mkdir -p /it-depends
# this is required for cargo:
ENV USER=root
COPY . /it-depends
WORKDIR /it-depends
RUN pip3 install .
""")
_CONTAINER = DockerContainer("trailofbits/it-depends", dockerfile=dockerfile, tag="latest")
_CONTAINER.rebuild()
finally:
if not dockerfile_existed and dockerfile.exists():
dockerfile.path.unlink()
return _CONTAINER


def rebuild(test: SmokeTest):
print(f"Rebuilding {test.expected_json!s}")
container = get_container()
if container.run(
"it-depends", str(test.snapshot_folder.relative_to(IT_DEPENDS_DIR)), "-f", "json",
"-o", str(test.expected_json.relative_to(IT_DEPENDS_DIR)), "--force",
cwd=IT_DEPENDS_DIR,
check_existence=False, rebuild=False, mounts=(
(test.expected_json.parent, "/it-depends/test/repos"),
("/var/run/docker.sock", "/var/run/docker.sock"),
),
privileged=True
) != 0:
raise ValueError(f"it-depends exited with non-zero status for {test.snapshot_folder}!")
print(f"Updated {test.expected_json!s}")


if __name__ == "__main__":
for t in sorted(SMOKE_TESTS, key=lambda st: st.repo_name):
rebuild(t)
Loading

0 comments on commit 4d86c21

Please sign in to comment.