3232 anotherAttrStr: value1
3333"""
3434
35+ # Shared storage for mock data
36+ _stored_body = []
3537
36- def mocked_custom_api ():
37- stored_body = []
3838
39+ class MockResourceResult :
40+ """Mock for DynamicClient resource result."""
41+
42+ def __init__ (self , data ):
43+ self ._data = data
44+
45+ def to_dict (self ):
46+ return self ._data
47+
48+
49+ class MockResource :
50+ """Mock for DynamicClient resource API."""
51+
52+ def server_side_apply (self , namespace , body , field_manager ):
53+ _stored_body .append (body )
54+ return MockResourceResult (body )
55+
56+
57+ class MockDynamicClient :
58+ """Mock for DynamicClient."""
59+
60+ class Resources :
61+ def get (self , api_version , kind ):
62+ return MockResource ()
63+
64+ resources = Resources ()
65+
66+
67+ def mocked_dynamic_client ():
68+ return MockDynamicClient ()
69+
70+
71+ def mocked_custom_api ():
3972 def get_namespaced_custom_object (group , version , namespace , plural , name ):
40- if len (stored_body ) > 0 :
41- return stored_body [- 1 ]
73+ if len (_stored_body ) > 0 :
74+ return _stored_body [- 1 ]
4275 return {"name" : name }
4376
4477 def create_namespaced_custom_object (group , version , namespace , plural , body : dict ):
4578 body .update ({"name" : body ["metadata" ]["name" ]})
46- stored_body .append (body )
47- return body
48-
49- def patch_namespaced_custom_object (group , version , namespace , plural , name , body : dict ):
50- stored_body .append (body )
79+ _stored_body .append (body )
5180 return body
5281
5382 base = MagicMock ()
5483 base .get_namespaced_custom_object = MagicMock (side_effect = get_namespaced_custom_object )
55- base .patch_namespaced_custom_object = MagicMock (side_effect = patch_namespaced_custom_object )
5684 base .create_namespaced_custom_object = MagicMock (side_effect = create_namespaced_custom_object )
5785
5886 return base
@@ -68,10 +96,13 @@ def mocked_crd_return_value():
6896 )
6997
7098
99+ @mock .patch ("kubeobject.customobject.DynamicClient" )
71100@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
72101@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
73- def test_custom_object_creation (mocked_get_crd_names , mocked_client ):
102+ def test_custom_object_creation (mocked_get_crd_names , mocked_client , mocked_dynamic ):
103+ _stored_body .clear ()
74104 mocked_client .return_value = mocked_custom_api ()
105+ mocked_dynamic .return_value = mocked_dynamic_client ()
75106 custom = CustomObject (
76107 "my-dummy-object" ,
77108 "my-dummy-namespace" ,
@@ -84,10 +115,13 @@ def test_custom_object_creation(mocked_get_crd_names, mocked_client):
84115 assert custom ["name" ] == "my-dummy-object"
85116
86117
118+ @mock .patch ("kubeobject.customobject.DynamicClient" )
87119@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
88120@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
89- def test_custom_object_read_from_disk (mocked_get_crd_names , mocked_client ):
121+ def test_custom_object_read_from_disk (mocked_get_crd_names , mocked_client , mocked_dynamic ):
122+ _stored_body .clear ()
90123 mocked_client .return_value = mocked_custom_api ()
124+ mocked_dynamic .return_value = mocked_dynamic_client ()
91125 with mock .patch (
92126 "kubeobject.customobject.open" ,
93127 mock .mock_open (read_data = yaml_data0 ),
@@ -106,10 +140,13 @@ def test_custom_object_read_from_disk(mocked_get_crd_names, mocked_client):
106140 assert custom ["spec" ]["subDoc" ]["anotherAttrStr" ] == "value1"
107141
108142
143+ @mock .patch ("kubeobject.customobject.DynamicClient" )
109144@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
110145@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
111- def test_custom_object_read_from_disk_with_dat_from_yaml (mocked_get_crd_names , mocked_client ):
146+ def test_custom_object_read_from_disk_with_dat_from_yaml (mocked_get_crd_names , mocked_client , mocked_dynamic ):
147+ _stored_body .clear ()
112148 mocked_client .return_value = mocked_custom_api ()
149+ mocked_dynamic .return_value = mocked_dynamic_client ()
113150 with mock .patch (
114151 "kubeobject.customobject.open" ,
115152 mock .mock_open (read_data = yaml_data0 ),
@@ -137,10 +174,13 @@ def test_custom_object_read_from_disk_with_dat_from_yaml(mocked_get_crd_names, m
137174 assert custom .plural == "dummies"
138175
139176
177+ @mock .patch ("kubeobject.customobject.DynamicClient" )
140178@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
141179@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
142- def test_custom_object_can_be_subclassed_create (mocked_crd_return_value , mocked_client ):
180+ def test_custom_object_can_be_subclassed_create (mocked_crd_return_value , mocked_client , mocked_dynamic ):
181+ _stored_body .clear ()
143182 mocked_client .return_value = mocked_custom_api ()
183+ mocked_dynamic .return_value = mocked_dynamic_client ()
144184
145185 class Subklass (CustomObject ):
146186 pass
@@ -156,10 +196,13 @@ class Subklass(CustomObject):
156196 assert a .__class__ .__name__ == "Subklass"
157197
158198
199+ @mock .patch ("kubeobject.customobject.DynamicClient" )
159200@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
160201@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
161- def test_custom_object_can_be_subclassed_from_yaml (mocked_crd_return_value , mocked_client ):
202+ def test_custom_object_can_be_subclassed_from_yaml (mocked_crd_return_value , mocked_client , mocked_dynamic ):
203+ _stored_body .clear ()
162204 mocked_client .return_value = mocked_custom_api ()
205+ mocked_dynamic .return_value = mocked_dynamic_client ()
163206
164207 class Subklass (CustomObject ):
165208 pass
@@ -175,10 +218,13 @@ class Subklass(CustomObject):
175218 assert a .__class__ .__name__ == "Subklass"
176219
177220
221+ @mock .patch ("kubeobject.customobject.DynamicClient" )
178222@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
179223@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
180- def test_custom_object_defined (mocked_crd_return_value , mocked_client ):
224+ def test_custom_object_defined (mocked_crd_return_value , mocked_client , mocked_dynamic ):
225+ _stored_body .clear ()
181226 mocked_client .return_value = mocked_custom_api ()
227+ mocked_dynamic .return_value = mocked_dynamic_client ()
182228 klass = CustomObject .define ("Dummy" , plural = "dummies" , group = "dummy.com" , version = "v1" )
183229
184230 k = klass ("my-dummy" , "default" ).create ()
@@ -189,9 +235,12 @@ def test_custom_object_defined(mocked_crd_return_value, mocked_client):
189235 assert repr (k ) == "Dummy('my-dummy', 'default')"
190236
191237
238+ @mock .patch ("kubeobject.customobject.DynamicClient" )
192239@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
193- def test_defined_wont_require_api_if_all_parameteres_are_provided (mocked_client ):
240+ def test_defined_wont_require_api_if_all_parameteres_are_provided (mocked_client , mocked_dynamic ):
241+ _stored_body .clear ()
194242 mocked_client .return_value = mocked_custom_api ()
243+ mocked_dynamic .return_value = mocked_dynamic_client ()
195244 BaseKlass = CustomObject .define ("Dummy" , kind = "Dummy" , plural = "dummies" , group = "dummy.com" , version = "v1" )
196245
197246 class SubKlass (BaseKlass ):
@@ -207,11 +256,14 @@ def get_spec(self):
207256 assert k .get_spec () == {"testAttr" : "value" }
208257
209258
259+ @mock .patch ("kubeobject.customobject.DynamicClient" )
210260@mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
211261@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
212- def test_custom_object_auto_reload (mocked_get_crd_names , mocked_client ):
262+ def test_custom_object_auto_reload (mocked_get_crd_names , mocked_client , mocked_dynamic ):
263+ _stored_body .clear ()
213264 instance = mocked_custom_api ()
214265 mocked_client .return_value = instance
266+ mocked_dynamic .return_value = mocked_dynamic_client ()
215267
216268 klass = CustomObject .define ("Dummy" , plural = "dummies" , group = "dummy.com" , version = "v1" )
217269 k = klass ("my-dummy" , "default" )
@@ -265,9 +317,13 @@ def test_raises_if_no_namespace():
265317 CustomObject .from_yaml ("some-other-file.yaml" , name = "some-name" )
266318
267319
320+ @mock .patch ("kubeobject.customobject.DynamicClient" )
321+ @mock .patch ("kubeobject.customobject.client.CustomObjectsApi" )
268322@mock .patch ("kubeobject.customobject.get_crd_names" , return_value = mocked_crd_return_value ())
269- def test_name_is_set_as_argument (_ ):
323+ def test_name_is_set_as_argument (_ , mocked_client , mocked_dynamic ):
270324 # TODO: what is this test supposed to do?
325+ mocked_client .return_value = mocked_custom_api ()
326+ mocked_dynamic .return_value = mocked_dynamic_client ()
271327 with mock .patch (
272328 "kubeobject.customobject.open" ,
273329 mock .mock_open (read_data = yaml_data1 ),
0 commit comments