9
9
absolute_import , division , print_function , unicode_literals )
10
10
11
11
import os .path
12
+ import json
12
13
import re
14
+ import tempfile
13
15
import uuid
14
16
from builtins import str
15
17
@@ -837,14 +839,54 @@ def test_command_oom_kill(self):
837
839
u'Command killed due to excessive memory consumption\n ' )
838
840
839
841
840
-
841
-
842
- class TestAutoWipeEnvironment (TestCase ):
842
+ class AutoWipeEnvironmentBase (object ):
843
843
fixtures = ['test_data' ]
844
+ build_env_class = None
844
845
845
846
def setUp (self ):
846
847
self .pip = Project .objects .get (slug = 'pip' )
847
848
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 )
848
890
849
891
def test_is_obsolete_without_env_json_file (self ):
850
892
yaml_config = create_load ()()[0 ]
@@ -854,7 +896,7 @@ def test_is_obsolete_without_env_json_file(self):
854
896
exists .return_value = False
855
897
python_env = Virtualenv (
856
898
version = self .version ,
857
- build_env = None ,
899
+ build_env = self . build_env ,
858
900
config = config ,
859
901
)
860
902
@@ -868,7 +910,7 @@ def test_is_obsolete_with_invalid_env_json_file(self):
868
910
exists .return_value = True
869
911
python_env = Virtualenv (
870
912
version = self .version ,
871
- build_env = None ,
913
+ build_env = self . build_env ,
872
914
config = config ,
873
915
)
874
916
@@ -888,10 +930,10 @@ def test_is_obsolete_with_json_different_python_version(self):
888
930
889
931
python_env = Virtualenv (
890
932
version = self .version ,
891
- build_env = None ,
933
+ build_env = self . build_env ,
892
934
config = config ,
893
935
)
894
- env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}'
936
+ env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3" }, "python": {"version": 3.5}}' # noqa
895
937
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
938
exists .return_value = True
897
939
self .assertTrue (python_env .is_obsolete )
@@ -910,10 +952,10 @@ def test_is_obsolete_with_json_different_build_image(self):
910
952
911
953
python_env = Virtualenv (
912
954
version = self .version ,
913
- build_env = None ,
955
+ build_env = self . build_env ,
914
956
config = config ,
915
957
)
916
- env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}'
958
+ env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3" }, "python": {"version": 2.7}}' # noqa
917
959
with patch ('os.path.exists' ) as exists , patch ('readthedocs.doc_builder.python_environments.open' , mock_open (read_data = env_json_data )) as _open : # noqa
918
960
exists .return_value = True
919
961
self .assertTrue (python_env .is_obsolete )
@@ -936,10 +978,10 @@ def test_is_obsolete_with_project_different_build_image(self):
936
978
937
979
python_env = Virtualenv (
938
980
version = self .version ,
939
- build_env = None ,
981
+ build_env = self . build_env ,
940
982
config = config ,
941
983
)
942
- env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 2.7}}'
984
+ env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3" }, "python": {"version": 2.7}}' # noqa
943
985
with patch ('os.path.exists' ) as exists , patch ('readthedocs.doc_builder.python_environments.open' , mock_open (read_data = env_json_data )) as _open : # noqa
944
986
exists .return_value = True
945
987
self .assertTrue (python_env .is_obsolete )
@@ -958,10 +1000,55 @@ def test_is_obsolete_with_json_same_data_as_version(self):
958
1000
959
1001
python_env = Virtualenv (
960
1002
version = self .version ,
961
- build_env = None ,
1003
+ build_env = self . build_env ,
962
1004
config = config ,
963
1005
)
964
- env_json_data = '{"build": {"image": "readthedocs/build:2.0"}, "python": {"version": 3.5}}'
1006
+ env_json_data = '{"build": {"image": "readthedocs/build:2.0", "hash": "a1b2c3" }, "python": {"version": 3.5}}' # noqa
965
1007
with patch ('os.path.exists' ) as exists , patch ('readthedocs.doc_builder.python_environments.open' , mock_open (read_data = env_json_data )) as _open : # noqa
966
1008
exists .return_value = True
967
1009
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