-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Save docker image hash to consider when auto wiping the environment #3793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af67c19
Remove obsolete code
humitos 83afdf1
Move container_image selection to the init
humitos aefada3
Save Docker Image hash in readthedocs-environment.json
humitos 026a411
Simplify the class naming
humitos 622ef10
Save the image hash in the json file
humitos ca388c0
Lint
humitos 2189b93
Remove invalid properties from YAML config in tests
humitos 05d74ae
Add test for save_environment_json
humitos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,9 @@ | |
absolute_import, division, print_function, unicode_literals) | ||
|
||
import os.path | ||
import json | ||
import re | ||
import tempfile | ||
import uuid | ||
from builtins import str | ||
|
||
|
@@ -837,14 +839,54 @@ def test_command_oom_kill(self): | |
u'Command killed due to excessive memory consumption\n') | ||
|
||
|
||
|
||
|
||
class TestAutoWipeEnvironment(TestCase): | ||
class AutoWipeEnvironmentBase(object): | ||
fixtures = ['test_data'] | ||
build_env_class = None | ||
|
||
def setUp(self): | ||
self.pip = Project.objects.get(slug='pip') | ||
self.version = self.pip.versions.get(slug='0.8') | ||
self.build_env = self.build_env_class( | ||
project=self.pip, | ||
version=self.version, | ||
build={'id': DUMMY_BUILD_ID}, | ||
) | ||
|
||
def test_save_environment_json(self): | ||
config_data = { | ||
'build': { | ||
'image': '2.0', | ||
}, | ||
'python': { | ||
'version': 2.7, | ||
}, | ||
} | ||
yaml_config = create_load(config_data)()[0] | ||
config = ConfigWrapper(version=self.version, yaml_config=yaml_config) | ||
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
|
||
with patch( | ||
'readthedocs.doc_builder.python_environments.PythonEnvironment.environment_json_path', | ||
return_value=tempfile.mktemp(suffix='envjson'), | ||
): | ||
python_env.save_environment_json() | ||
json_data = json.load(open(python_env.environment_json_path())) | ||
|
||
expected_data = { | ||
'build': { | ||
'image': 'readthedocs/build:2.0', | ||
'hash': 'a1b2c3', | ||
}, | ||
'python': { | ||
'version': 2.7, | ||
}, | ||
} | ||
self.assertDictEqual(json_data, expected_data) | ||
|
||
def test_is_obsolete_without_env_json_file(self): | ||
yaml_config = create_load()()[0] | ||
|
@@ -854,7 +896,7 @@ def test_is_obsolete_without_env_json_file(self): | |
exists.return_value = False | ||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
|
||
|
@@ -868,7 +910,7 @@ def test_is_obsolete_with_invalid_env_json_file(self): | |
exists.return_value = True | ||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
|
||
|
@@ -888,15 +930,14 @@ def test_is_obsolete_with_json_different_python_version(self): | |
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}' | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 3.5}}' # noqa | ||
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa | ||
exists.return_value = True | ||
self.assertTrue(python_env.is_obsolete) | ||
|
||
@pytest.mark.xfail(reason='build.image is not being considered yet') | ||
def test_is_obsolete_with_json_different_build_image(self): | ||
config_data = { | ||
'build': { | ||
|
@@ -911,10 +952,10 @@ def test_is_obsolete_with_json_different_build_image(self): | |
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 2.7}}' # noqa | ||
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa | ||
exists.return_value = True | ||
self.assertTrue(python_env.is_obsolete) | ||
|
@@ -937,10 +978,10 @@ def test_is_obsolete_with_project_different_build_image(self): | |
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 2.7}}' # noqa | ||
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa | ||
exists.return_value = True | ||
self.assertTrue(python_env.is_obsolete) | ||
|
@@ -959,10 +1000,55 @@ def test_is_obsolete_with_json_same_data_as_version(self): | |
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=None, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}' | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3"}, "python": {"version": 3.5}}' # noqa | ||
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa | ||
exists.return_value = True | ||
self.assertFalse(python_env.is_obsolete) | ||
|
||
def test_is_obsolete_with_json_different_build_hash(self): | ||
config_data = { | ||
'build': { | ||
'image': '2.0', | ||
}, | ||
'python': { | ||
'version': 2.7, | ||
}, | ||
} | ||
yaml_config = create_load(config_data)()[0] | ||
config = ConfigWrapper(version=self.version, yaml_config=yaml_config) | ||
|
||
# Set container_image manually | ||
self.pip.container_image = 'readthedocs/build:2.0' | ||
self.pip.save() | ||
|
||
python_env = Virtualenv( | ||
version=self.version, | ||
build_env=self.build_env, | ||
config=config, | ||
) | ||
env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "foo"}, "python": {"version": 2.7}}' # noqa | ||
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa | ||
exists.return_value = True | ||
self.assertTrue(python_env.is_obsolete) | ||
|
||
|
||
@patch( | ||
'readthedocs.doc_builder.environments.DockerBuildEnvironment.image_hash', | ||
PropertyMock(return_value='a1b2c3'), | ||
) | ||
class AutoWipeDockerBuildEnvironmentTest(AutoWipeEnvironmentBase, TestCase): | ||
build_env_class = DockerBuildEnvironment | ||
|
||
|
||
@pytest.mark.xfail( | ||
reason='PythonEnvironment needs to be refactored to do not rely on DockerBuildEnvironment', | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be good to lay out in a new ticket |
||
@patch( | ||
'readthedocs.doc_builder.environments.DockerBuildEnvironment.image_hash', | ||
PropertyMock(return_value='a1b2c3'), | ||
) | ||
class AutoWipeLocalBuildEnvironmentTest(AutoWipeEnvironmentBase, TestCase): | ||
build_env_class = LocalBuildEnvironment |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will force wipe all existing builds, but perhaps that is a good thing. We'll have explicit metadata after the next build of all of these projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm...
This won't force the wipe because it will fail in the L134 (https://github.com/rtfd/readthedocs.org/pull/3793/files/05d74aec013e1b211798ba02e91636d331794a1d#diff-31f8391d8643c47c6caedf53930c0b7eR134) when trying to access the
build.hash
that will produce a KeyError exception and we will returnFalse
.Then, the current build with the old environment will be used but we will be saving the current new attributes and... we have a bug here, right?