Skip to content

Commit b7a4cc1

Browse files
committed
Test cases for PythonEnvironment.is_obsolete
1 parent d38b5c3 commit b7a4cc1

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

readthedocs/doc_builder/python_environments.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import os
1010
import shutil
11-
from builtins import object
11+
from builtins import object, open
1212

1313
from django.conf import settings
1414

@@ -130,7 +130,7 @@ def is_obsolete(self):
130130
environment_conf = json.load(fpath)
131131
env_python_version = environment_conf['python']['version']
132132
env_build_image = environment_conf['build']['image']
133-
except (TypeError, KeyError, ValueError):
133+
except (IOError, TypeError, KeyError, ValueError):
134134
log.error('Unable to read/parse environment.json file')
135135
return False
136136

readthedocs/rtd_tests/tests/test_doc_building.py

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414
from builtins import str
1515

1616
import mock
17+
import pytest
1718
from django.test import TestCase
1819
from docker.errors import APIError as DockerAPIError
1920
from docker.errors import DockerException
20-
from mock import Mock, PropertyMock, patch
21+
from mock import Mock, PropertyMock, mock_open, patch
2122

2223
from readthedocs.builds.constants import BUILD_STATE_CLONING
2324
from readthedocs.builds.models import Version
25+
from readthedocs.doc_builder.config import ConfigWrapper
2426
from readthedocs.doc_builder.environments import (
2527
BuildCommand, DockerBuildCommand, DockerEnvironment, LocalEnvironment)
2628
from readthedocs.doc_builder.exceptions import BuildEnvironmentError
29+
from readthedocs.doc_builder.python_environments import Virtualenv
2730
from readthedocs.projects.models import Project
2831
from readthedocs.rtd_tests.mocks.environment import EnvironmentMockGroup
32+
from readthedocs.rtd_tests.tests.test_config_wrapper import create_load
2933

3034
DUMMY_BUILD_ID = 123
3135
SAMPLE_UNICODE = u'HérÉ îß sömê ünïçó∂é'
@@ -831,3 +835,108 @@ def test_command_oom_kill(self):
831835
self.assertEqual(
832836
str(cmd.output),
833837
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_json_same_data_as_version(self):
923+
config_data = {
924+
'build': {
925+
'image': '2.0',
926+
},
927+
'python': {
928+
'version': 3.5,
929+
},
930+
}
931+
yaml_config = create_load(config_data)()[0]
932+
config = ConfigWrapper(version=self.version, yaml_config=yaml_config)
933+
934+
python_env = Virtualenv(
935+
version=self.version,
936+
build_env=None,
937+
config=config,
938+
)
939+
env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}'
940+
with patch('os.path.exists') as exists, patch('readthedocs.doc_builder.python_environments.open', mock_open(read_data=env_json_data)) as _open: # noqa
941+
exists.return_value = True
942+
self.assertFalse(python_env.is_obsolete)

0 commit comments

Comments
 (0)