Skip to content

Commit d8f603c

Browse files
committed
Revert "Save docker image hash to consider when auto wiping the environment (#3793)"
This reverts commit 7ac7dfc.
1 parent 5a39385 commit d8f603c

File tree

3 files changed

+24
-119
lines changed

3 files changed

+24
-119
lines changed

readthedocs/doc_builder/environments.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,6 @@ def __init__(self, *args, **kwargs):
625625
)
626626
if self.config and self.config.build_image:
627627
self.container_image = self.config.build_image
628-
if self.project.container_image:
629-
self.container_image = self.project.container_image
630628
if self.project.container_mem_limit:
631629
self.container_mem_limit = self.project.container_mem_limit
632630
if self.project.container_time_limit:
@@ -788,13 +786,6 @@ def get_container_host_config(self):
788786
mem_limit=self.container_mem_limit,
789787
)
790788

791-
@property
792-
def image_hash(self):
793-
"""Return the hash of the Docker image."""
794-
client = self.get_client()
795-
image_metadata = client.inspect_image(self.container_image)
796-
return image_metadata.get('Id')
797-
798789
@property
799790
def container_id(self):
800791
"""Return id of container if it is valid."""
@@ -837,13 +828,13 @@ def update_build_from_container_state(self):
837828
def create_container(self):
838829
"""Create docker container."""
839830
client = self.get_client()
831+
image = self.container_image
832+
if self.project.container_image:
833+
image = self.project.container_image
840834
try:
841-
log.info(
842-
'Creating Docker container: image=%s',
843-
self.container_image,
844-
)
835+
log.info('Creating Docker container: image=%s', image)
845836
self.container = client.create_container(
846-
image=self.container_image,
837+
image=image,
847838
command=('/bin/sh -c "sleep {time}; exit {exit}"'
848839
.format(time=self.container_time_limit,
849840
exit=DOCKER_TIMEOUT_EXIT_CODE)),

readthedocs/doc_builder/python_environments.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,33 @@ def is_obsolete(self):
131131
environment_conf = json.load(fpath)
132132
env_python_version = environment_conf['python']['version']
133133
env_build_image = environment_conf['build']['image']
134-
env_build_hash = environment_conf['build']['hash']
135134
except (IOError, TypeError, KeyError, ValueError):
136135
log.error('Unable to read/parse readthedocs-environment.json file')
137136
return False
138137

139-
build_image = self.config.build_image or DOCKER_IMAGE
138+
# TODO: remove getattr when https://github.com/rtfd/readthedocs.org/pull/3339 got merged
139+
build_image = getattr(self.config, 'build_image', self.version.project.container_image) or DOCKER_IMAGE # noqa
140+
140141
# If the user define the Python version just as a major version
141142
# (e.g. ``2`` or ``3``) we won't know exactly which exact version was
142143
# used to create the venv but we can still compare it against the new
143144
# one coming from the project version config.
144145
return any([
145146
env_python_version != self.config.python_full_version,
146147
env_build_image != build_image,
147-
env_build_hash != self.build_env.image_hash,
148148
])
149149

150150
def save_environment_json(self):
151151
"""Save on disk Python and build image versions used to create the venv."""
152-
build_image = self.config.build_image or DOCKER_IMAGE
152+
# TODO: remove getattr when https://github.com/rtfd/readthedocs.org/pull/3339 got merged
153+
build_image = getattr(self.config, 'build_image', self.version.project.container_image) or DOCKER_IMAGE # noqa
154+
153155
data = {
154156
'python': {
155157
'version': self.config.python_full_version,
156158
},
157159
'build': {
158160
'image': build_image,
159-
'hash': self.build_env.image_hash,
160161
},
161162
}
162163

readthedocs/rtd_tests/tests/test_doc_building.py

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
absolute_import, division, print_function, unicode_literals)
1010

1111
import os.path
12-
import json
1312
import re
14-
import tempfile
1513
import uuid
1614
from builtins import str
1715

@@ -839,54 +837,14 @@ def test_command_oom_kill(self):
839837
u'Command killed due to excessive memory consumption\n')
840838

841839

842-
class AutoWipeEnvironmentBase(object):
840+
841+
842+
class TestAutoWipeEnvironment(TestCase):
843843
fixtures = ['test_data']
844-
build_env_class = None
845844

846845
def setUp(self):
847846
self.pip = Project.objects.get(slug='pip')
848847
self.version = self.pip.versions.get(slug='0.8')
849-
self.build_env = self.build_env_class(
850-
project=self.pip,
851-
version=self.version,
852-
build={'id': DUMMY_BUILD_ID},
853-
)
854-
855-
def test_save_environment_json(self):
856-
config_data = {
857-
'build': {
858-
'image': '2.0',
859-
},
860-
'python': {
861-
'version': 2.7,
862-
},
863-
}
864-
yaml_config = create_load(config_data)()[0]
865-
config = ConfigWrapper(version=self.version, yaml_config=yaml_config)
866-
867-
python_env = Virtualenv(
868-
version=self.version,
869-
build_env=self.build_env,
870-
config=config,
871-
)
872-
873-
with patch(
874-
'readthedocs.doc_builder.python_environments.PythonEnvironment.environment_json_path',
875-
return_value=tempfile.mktemp(suffix='envjson'),
876-
):
877-
python_env.save_environment_json()
878-
json_data = json.load(open(python_env.environment_json_path()))
879-
880-
expected_data = {
881-
'build': {
882-
'image': 'readthedocs/build:2.0',
883-
'hash': 'a1b2c3',
884-
},
885-
'python': {
886-
'version': 2.7,
887-
},
888-
}
889-
self.assertDictEqual(json_data, expected_data)
890848

891849
def test_is_obsolete_without_env_json_file(self):
892850
yaml_config = create_load()()[0]
@@ -896,7 +854,7 @@ def test_is_obsolete_without_env_json_file(self):
896854
exists.return_value = False
897855
python_env = Virtualenv(
898856
version=self.version,
899-
build_env=self.build_env,
857+
build_env=None,
900858
config=config,
901859
)
902860

@@ -910,7 +868,7 @@ def test_is_obsolete_with_invalid_env_json_file(self):
910868
exists.return_value = True
911869
python_env = Virtualenv(
912870
version=self.version,
913-
build_env=self.build_env,
871+
build_env=None,
914872
config=config,
915873
)
916874

@@ -930,10 +888,10 @@ def test_is_obsolete_with_json_different_python_version(self):
930888

931889
python_env = Virtualenv(
932890
version=self.version,
933-
build_env=self.build_env,
891+
build_env=None,
934892
config=config,
935893
)
936-
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 3.5}}' # noqa
894+
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}'
937895
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
938896
exists.return_value = True
939897
self.assertTrue(python_env.is_obsolete)
@@ -952,10 +910,10 @@ def test_is_obsolete_with_json_different_build_image(self):
952910

953911
python_env = Virtualenv(
954912
version=self.version,
955-
build_env=self.build_env,
913+
build_env=None,
956914
config=config,
957915
)
958-
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 2.7}}' # noqa
916+
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}'
959917
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
960918
exists.return_value = True
961919
self.assertTrue(python_env.is_obsolete)
@@ -978,10 +936,10 @@ def test_is_obsolete_with_project_different_build_image(self):
978936

979937
python_env = Virtualenv(
980938
version=self.version,
981-
build_env=self.build_env,
939+
build_env=None,
982940
config=config,
983941
)
984-
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 2.7}}' # noqa
942+
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}'
985943
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
986944
exists.return_value = True
987945
self.assertTrue(python_env.is_obsolete)
@@ -1000,55 +958,10 @@ def test_is_obsolete_with_json_same_data_as_version(self):
1000958

1001959
python_env = Virtualenv(
1002960
version=self.version,
1003-
build_env=self.build_env,
961+
build_env=None,
1004962
config=config,
1005963
)
1006-
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 3.5}}' # noqa
964+
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}'
1007965
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
1008966
exists.return_value = True
1009967
self.assertFalse(python_env.is_obsolete)
1010-
1011-
def test_is_obsolete_with_json_different_build_hash(self):
1012-
config_data = {
1013-
'build': {
1014-
'image': '2.0',
1015-
},
1016-
'python': {
1017-
'version': 2.7,
1018-
},
1019-
}
1020-
yaml_config = create_load(config_data)()[0]
1021-
config = ConfigWrapper(version=self.version, yaml_config=yaml_config)
1022-
1023-
# Set container_image manually
1024-
self.pip.container_image = 'readthedocs/build:2.0'
1025-
self.pip.save()
1026-
1027-
python_env = Virtualenv(
1028-
version=self.version,
1029-
build_env=self.build_env,
1030-
config=config,
1031-
)
1032-
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "foo"}, "python": {"version": 2.7}}' # noqa
1033-
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
1034-
exists.return_value = True
1035-
self.assertTrue(python_env.is_obsolete)
1036-
1037-
1038-
@patch(
1039-
'readthedocs.doc_builder.environments.DockerBuildEnvironment.image_hash',
1040-
PropertyMock(return_value='a1b2c3'),
1041-
)
1042-
class AutoWipeDockerBuildEnvironmentTest(AutoWipeEnvironmentBase, TestCase):
1043-
build_env_class = DockerBuildEnvironment
1044-
1045-
1046-
@pytest.mark.xfail(
1047-
reason='PythonEnvironment needs to be refactored to do not rely on DockerBuildEnvironment',
1048-
)
1049-
@patch(
1050-
'readthedocs.doc_builder.environments.DockerBuildEnvironment.image_hash',
1051-
PropertyMock(return_value='a1b2c3'),
1052-
)
1053-
class AutoWipeLocalBuildEnvironmentTest(AutoWipeEnvironmentBase, TestCase):
1054-
build_env_class = LocalBuildEnvironment

0 commit comments

Comments
 (0)