Skip to content
Open
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
45 changes: 23 additions & 22 deletions src/container_inspector/dockerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_dockerfile(location):
otherwise return None.
"""
fn = path.basename(location)
if not "Dockerfile" in fn:
if "dockerfile" not in fn.lower():
return {}

if TRACE:
Expand All @@ -43,27 +43,28 @@ def get_dockerfile(location):
# assign the comments before an instruction line to a line "comment" attribute
# assign end of line comment to the line
# assign top of file and end of file comments to file level comment attribute
df = dockerfile_parse.DockerfileParser(location)

df_data = dict()
df_data["location"] = location
df_data["base_image"] = df.baseimage
df_data["instructions"] = []

for entry in df.structure:
entry = dict(
[
(k, v)
for k, v in sorted(entry.items())
if k
in (
"instruction",
"startline",
"value",
)
]
)
df_data["instructions"].append(entry)
with open(location, "rb") as df_file:
df = dockerfile_parse.DockerfileParser(fileobj=df_file)

df_data = dict()
df_data["location"] = location
df_data["base_image"] = df.baseimage
df_data["instructions"] = []

for entry in df.structure:
entry = dict(
[
(k, v)
for k, v in sorted(entry.items())
if k
in (
"instruction",
"startline",
"value",
)
]
)
df_data["instructions"].append(entry)
return {location: df_data}
except:
if TRACE:
Expand Down
2 changes: 2 additions & 0 deletions tests/data/dockerfiles/container-inspector.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM alpine:3.20
RUN echo hello
11 changes: 11 additions & 0 deletions tests/test_dockerfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@

from commoncode.testcase import FileBasedTesting

from container_inspector.dockerfile import get_dockerfile
from container_inspector.dockerfile import normalized_layer_command


class TestDockerfile(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), "data")

def test_get_dockerfile_accepts_lowercase_prefixed_dockerfile_name(self):
test_file = os.path.join(
self.test_data_dir,
"dockerfiles",
"container-inspector.dockerfile",
)
dockerfiles = get_dockerfile(test_file)
assert test_file in dockerfiles
assert "alpine:3.20" == dockerfiles[test_file]["base_image"]

def test_normalized_layer_command(self):
# tuple of command and expected result tuples
test_data = [
Expand Down