Skip to content

Commit 8b360ce

Browse files
authored
Merge pull request #2892 from alexandrevicenzi/reproducible-containers
Support SOURCE_DATE_EPOCH for OCI containers
2 parents 12af5bd + f0f116e commit 8b360ce

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

kiwi/oci_tools/base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with kiwi. If not, see <http://www.gnu.org/licenses/>
1717
#
1818
import os
19-
from datetime import datetime
19+
from datetime import datetime, timezone
2020

2121
# project
2222
from kiwi.path import Path
@@ -38,9 +38,17 @@ class OCIBase:
3838
"""
3939
def __init__(self):
4040
self.oci_root_dir = None
41-
self.creation_date = datetime.utcnow().strftime(
42-
'%Y-%m-%dT%H:%M:%S+00:00'
43-
)
41+
sde = os.environ.get('SOURCE_DATE_EPOCH')
42+
if sde:
43+
self.creation_date = datetime.fromtimestamp(
44+
int(sde), tz=timezone.utc
45+
).strftime(
46+
'%Y-%m-%dT%H:%M:%S+00:00'
47+
)
48+
else:
49+
self.creation_date = datetime.utcnow().strftime(
50+
'%Y-%m-%dT%H:%M:%S+00:00'
51+
)
4452
self.post_init()
4553

4654
def __enter__(self):

test/unit/oci_tools/base_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pytest import raises
2-
from unittest.mock import patch
2+
from unittest.mock import Mock, patch
33

44
from kiwi.oci_tools.base import OCIBase
55

@@ -59,3 +59,20 @@ def test_skopeo_provides_tmpdir_option(
5959
assert self.oci._skopeo_provides_tmpdir_option() is True
6060
mock_Path_which.return_value = None
6161
assert self.oci._skopeo_provides_tmpdir_option() is False
62+
63+
@patch('kiwi.oci_tools.base.datetime')
64+
def test_creation_date(self, mock_datetime):
65+
strftime = Mock()
66+
strftime.strftime = Mock(return_value='current_date')
67+
mock_datetime.utcnow = Mock(
68+
return_value=strftime
69+
)
70+
71+
with patch.dict('os.environ', {'SOURCE_DATE_EPOCH': ''}):
72+
oci = OCIBase()
73+
assert oci.creation_date == 'current_date'
74+
75+
def test_creation_date_from_source_date_epoch(self):
76+
with patch.dict('os.environ', {'SOURCE_DATE_EPOCH': '946729830'}):
77+
oci = OCIBase()
78+
assert oci.creation_date == '2000-01-01T12:30:30+00:00'

0 commit comments

Comments
 (0)