|
14 | 14 | from builtins import str
|
15 | 15 |
|
16 | 16 | import mock
|
| 17 | +import pytest |
17 | 18 | from django.test import TestCase
|
18 | 19 | from docker.errors import APIError as DockerAPIError
|
19 | 20 | from docker.errors import DockerException
|
20 |
| -from mock import Mock, PropertyMock, patch |
| 21 | +from mock import Mock, PropertyMock, mock_open, patch |
21 | 22 |
|
22 | 23 | from readthedocs.builds.constants import BUILD_STATE_CLONING
|
23 | 24 | from readthedocs.builds.models import Version
|
| 25 | +from readthedocs.doc_builder.config import ConfigWrapper |
24 | 26 | from readthedocs.doc_builder.environments import (
|
25 | 27 | BuildCommand, DockerBuildCommand, DockerEnvironment, LocalEnvironment)
|
26 | 28 | from readthedocs.doc_builder.exceptions import BuildEnvironmentError
|
| 29 | +from readthedocs.doc_builder.python_environments import Virtualenv |
27 | 30 | from readthedocs.projects.models import Project
|
28 | 31 | from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup
|
| 32 | +from readthedocs.rtd_tests.tests.test_config_wrapper import create_load |
29 | 33 |
|
30 | 34 | DUMMY_BUILD_ID = 123
|
31 | 35 | SAMPLE_UNICODE = u'HérÉ îß sömê ünïçó∂é'
|
@@ -831,3 +835,134 @@ def test_command_oom_kill(self):
|
831 | 835 | self.assertEqual(
|
832 | 836 | str(cmd.output),
|
833 | 837 | u'Command killed due to excessive memory consumption\n')
|
| 838 | + |
| 839 | + |
| 840 | + |
| 841 | + |
| 842 | +class TestAutoWipeEnvironment(TestCase): |
| 843 | + fixtures = ['test_data'] |
| 844 | + |
| 845 | + def setUp(self): |
| 846 | + self.pip = Project.objects.get(slug='pip') |
| 847 | + self.version = self.pip.versions.get(slug='0.8') |
| 848 | + |
| 849 | + def test_is_obsolete_without_env_json_file(self): |
| 850 | + yaml_config = create_load()()[0] |
| 851 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 852 | + |
| 853 | + with patch('os.path.exists') as exists: |
| 854 | + exists.return_value = False |
| 855 | + python_env = Virtualenv( |
| 856 | + version=self.version, |
| 857 | + build_env=None, |
| 858 | + config=config, |
| 859 | + ) |
| 860 | + |
| 861 | + self.assertFalse(python_env.is_obsolete) |
| 862 | + |
| 863 | + def test_is_obsolete_with_invalid_env_json_file(self): |
| 864 | + yaml_config = create_load()()[0] |
| 865 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 866 | + |
| 867 | + with patch('os.path.exists') as exists: |
| 868 | + exists.return_value = True |
| 869 | + python_env = Virtualenv( |
| 870 | + version=self.version, |
| 871 | + build_env=None, |
| 872 | + config=config, |
| 873 | + ) |
| 874 | + |
| 875 | + self.assertFalse(python_env.is_obsolete) |
| 876 | + |
| 877 | + def test_is_obsolete_with_json_different_python_version(self): |
| 878 | + config_data = { |
| 879 | + 'build': { |
| 880 | + 'image': '2.0', |
| 881 | + }, |
| 882 | + 'python': { |
| 883 | + 'version': 2.7, |
| 884 | + }, |
| 885 | + } |
| 886 | + yaml_config = create_load(config_data)()[0] |
| 887 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 888 | + |
| 889 | + python_env = Virtualenv( |
| 890 | + version=self.version, |
| 891 | + build_env=None, |
| 892 | + config=config, |
| 893 | + ) |
| 894 | + env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}' |
| 895 | + with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa |
| 896 | + exists.return_value = True |
| 897 | + self.assertTrue(python_env.is_obsolete) |
| 898 | + |
| 899 | + @pytest.mark.xfail(reason='build.image is not being considered yet') |
| 900 | + def test_is_obsolete_with_json_different_build_image(self): |
| 901 | + config_data = { |
| 902 | + 'build': { |
| 903 | + 'image': 'latest', |
| 904 | + }, |
| 905 | + 'python': { |
| 906 | + 'version': 2.7, |
| 907 | + }, |
| 908 | + } |
| 909 | + yaml_config = create_load(config_data)()[0] |
| 910 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 911 | + |
| 912 | + python_env = Virtualenv( |
| 913 | + version=self.version, |
| 914 | + build_env=None, |
| 915 | + config=config, |
| 916 | + ) |
| 917 | + env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' |
| 918 | + with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa |
| 919 | + exists.return_value = True |
| 920 | + self.assertTrue(python_env.is_obsolete) |
| 921 | + |
| 922 | + def test_is_obsolete_with_project_different_build_image(self): |
| 923 | + config_data = { |
| 924 | + 'build': { |
| 925 | + 'image': '2.0', |
| 926 | + }, |
| 927 | + 'python': { |
| 928 | + 'version': 2.7, |
| 929 | + }, |
| 930 | + } |
| 931 | + yaml_config = create_load(config_data)()[0] |
| 932 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 933 | + |
| 934 | + # Set container_image manually |
| 935 | + self.pip.container_image = 'readthedocs/build:latest' |
| 936 | + self.pip.save() |
| 937 | + |
| 938 | + python_env = Virtualenv( |
| 939 | + version=self.version, |
| 940 | + build_env=None, |
| 941 | + config=config, |
| 942 | + ) |
| 943 | + env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}' |
| 944 | + with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa |
| 945 | + exists.return_value = True |
| 946 | + self.assertTrue(python_env.is_obsolete) |
| 947 | + |
| 948 | + def test_is_obsolete_with_json_same_data_as_version(self): |
| 949 | + config_data = { |
| 950 | + 'build': { |
| 951 | + 'image': '2.0', |
| 952 | + }, |
| 953 | + 'python': { |
| 954 | + 'version': 3.5, |
| 955 | + }, |
| 956 | + } |
| 957 | + yaml_config = create_load(config_data)()[0] |
| 958 | + config = ConfigWrapper(version=self.version, yaml_config=yaml_config) |
| 959 | + |
| 960 | + python_env = Virtualenv( |
| 961 | + version=self.version, |
| 962 | + build_env=None, |
| 963 | + config=config, |
| 964 | + ) |
| 965 | + env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}' |
| 966 | + with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa |
| 967 | + exists.return_value = True |
| 968 | + self.assertFalse(python_env.is_obsolete) |
0 commit comments