Skip to content

Commit 13de6fc

Browse files
committed
test: docker build tool tests
Signed-off-by: Tim Yarkov <[email protected]>
1 parent 3aa14b4 commit 13de6fc

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from macaron.config.defaults import create_defaults, defaults, load_defaults
1111
from macaron.database.table_definitions import Analysis, Component, Repository
1212
from macaron.slsa_analyzer.analyze_context import AnalyzeContext
13+
from macaron.slsa_analyzer.build_tool.docker import Docker
1314
from macaron.slsa_analyzer.build_tool.gradle import Gradle
1415
from macaron.slsa_analyzer.build_tool.maven import Maven
1516
from macaron.slsa_analyzer.build_tool.pip import Pip
@@ -148,6 +149,25 @@ def pip_tool(setup_test) -> Pip: # type: ignore # pylint: disable=unused-argume
148149
return pip
149150

150151

152+
@pytest.fixture(autouse=True)
153+
def docker_tool(setup_test) -> Docker: # type: ignore # pylint: disable=unused-argument
154+
"""Create a Docker tool instance.
155+
156+
Parameters
157+
----------
158+
setup_test
159+
Depends on setup_test fixture.
160+
161+
Returns
162+
-------
163+
Docker
164+
The Docker instance.
165+
"""
166+
docker = Docker()
167+
docker.load_defaults()
168+
return docker
169+
170+
151171
class MockGitHubActions(GitHubActions):
152172
"""Mock the GitHubActions class."""
153173

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# serializer version: 1
2+
# name: test_get_build_dirs[mock_repo0]
3+
list([
4+
PosixPath('.'),
5+
])
6+
# ---
7+
# name: test_get_build_dirs[mock_repo1]
8+
list([
9+
PosixPath('project'),
10+
])
11+
# ---
12+
# name: test_get_build_dirs[mock_repo2]
13+
list([
14+
PosixPath('.'),
15+
])
16+
# ---
17+
# name: test_get_build_dirs[mock_repo3]
18+
list([
19+
PosixPath('.'),
20+
])
21+
# ---
22+
# name: test_get_build_dirs[mock_repo4]
23+
list([
24+
])
25+
# ---
26+
27+
28+
29+
30+
31+
32+
# name: test_get_build_dirs[mock_repo0]
33+
list([
34+
PosixPath('.'),
35+
])
36+
# ---
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
# syntax=docker/dockerfile:1
5+
6+
FROM node:18-alpine
7+
CMD ["echo", "Hello!"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
# syntax=docker/dockerfile:1
5+
6+
FROM node:18-alpine
7+
CMD ["echo", "Hello!"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
# syntax=docker/dockerfile:1
5+
6+
FROM node:18-alpine
7+
CMD ["echo", "Hello!"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
# syntax=docker/dockerfile:1
5+
6+
FROM node:18-alpine
7+
CMD ["echo", "Hello!"]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
3+
4+
"""This module tests the Docker build functions."""
5+
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from macaron.slsa_analyzer.build_tool.docker import Docker
11+
from tests.slsa_analyzer.mock_git_utils import prepare_repo_for_testing
12+
13+
14+
@pytest.mark.parametrize(
15+
"mock_repo",
16+
[
17+
Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_dockerfile"),
18+
Path(__file__).parent.joinpath("mock_repos", "docker_repos", "nested_dockerfile"),
19+
Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_wildcard_dockerfile"),
20+
Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_dockerfile_wildcard"),
21+
Path(__file__).parent.joinpath("mock_repos", "docker_repos", "no_docker"),
22+
],
23+
)
24+
def test_get_build_dirs(snapshot: list, docker_tool: Docker, mock_repo: Path) -> None:
25+
"""Test discovering build directories."""
26+
assert list(docker_tool.get_build_dirs(str(mock_repo))) == snapshot
27+
28+
29+
@pytest.mark.parametrize(
30+
("mock_repo", "expected_value"),
31+
[
32+
(Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_dockerfile"), True),
33+
(Path(__file__).parent.joinpath("mock_repos", "docker_repos", "nested_dockerfile"), True),
34+
(Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_wildcard_dockerfile"), True),
35+
(Path(__file__).parent.joinpath("mock_repos", "docker_repos", "root_dockerfile_wildcard"), True),
36+
(Path(__file__).parent.joinpath("mock_repos", "docker_repos", "no_docker"), False),
37+
],
38+
)
39+
def test_docker_build_tool(docker_tool: Docker, macaron_path: str, mock_repo: str, expected_value: bool) -> None:
40+
"""Test the Gradle build tool."""
41+
base_dir = Path(__file__).parent
42+
ctx = prepare_repo_for_testing(mock_repo, macaron_path, base_dir)
43+
assert docker_tool.is_detected(ctx.component.repository.fs_path) == expected_value

0 commit comments

Comments
 (0)