1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import unittest
15+ import json
1616
17- import os
17+ import unittest
1818
1919import time
2020
2121from unittest .mock import Mock , call
2222
23- from kubernetes import client , config
23+ from kubernetes import client
2424
2525from .watch import Watch
2626
@@ -497,15 +497,15 @@ def test_watch_with_error_event_and_timeout_param(self):
497497 amt = None , decode_content = False )
498498 fake_resp .close .assert_called_once ()
499499 fake_resp .release_conn .assert_called_once ()
500-
500+
501501 @classmethod
502502 def setUpClass (cls ):
503503 cls .api = Mock ()
504504 cls .namespace = "default"
505505
506506 def test_pod_log_empty_lines (self ):
507507 pod_name = "demo-bug"
508-
508+
509509 try :
510510 self .api .create_namespaced_pod = Mock ()
511511 self .api .read_namespaced_pod = Mock ()
@@ -514,12 +514,12 @@ def test_pod_log_empty_lines(self):
514514
515515 #pod creating step
516516 self .api .create_namespaced_pod .return_value = None
517-
517+
518518 #Checking pod status
519519 mock_pod = Mock ()
520520 mock_pod .status .phase = "Running"
521521 self .api .read_namespaced_pod .return_value = mock_pod
522-
522+
523523 # Printing at pod output
524524 self .api .read_namespaced_pod_log .return_value = iter (["Hello from Docker\n " ])
525525
@@ -553,7 +553,7 @@ def test_pod_log_empty_lines(self):
553553 print (event )
554554
555555 # Print outputs
556- print (f"Captured logs: { log_output } " )
556+ print (f"Captured logs: { log_output } " )
557557 # self.assertTrue(any("Hello from Docker" in line for line in log_output))
558558 # self.assertTrue(any(line.strip() == "" for line in log_output), "No empty lines found in logs")
559559 expected_log = [
@@ -566,7 +566,7 @@ def test_pod_log_empty_lines(self):
566566 "\n " ,
567567 "Final log"
568568 ]
569-
569+
570570 self .assertEqual (log_output , expected_log , "Captured logs do not match expected logs" )
571571
572572 except ApiException as e :
@@ -576,44 +576,44 @@ def test_pod_log_empty_lines(self):
576576 self .api .delete_namespaced_pod (name = pod_name , namespace = self .namespace )
577577 self .api .delete_namespaced_pod .assert_called_once_with (name = pod_name , namespace = self .namespace )
578578
579- if __name__ == '__main__' :
580- def test_watch_with_deserialize_param ( self ):
581- """test watch.stream() deserialize param"""
582- # prepare test data
583- test_json = '{"type": "ADDED", "object": {"metadata": {"name": "test1", "resourceVersion": "1"}, "spec": {}, "status": {}}}'
584- fake_resp = Mock ()
585- fake_resp .close = Mock ()
586- fake_resp .release_conn = Mock ()
587- fake_resp . stream = Mock ( return_value = [ test_json + ' \n ' ])
588-
589- fake_api = Mock ()
590- fake_api .get_namespaces = Mock ( return_value = fake_resp )
591- fake_api . get_namespaces . __doc__ = ':return: V1NamespaceList'
592-
593- # test case with deserialize=True
594- w = Watch ()
595- for e in w . stream ( fake_api . get_namespaces , deserialize = True ):
596- self . assertEqual ( "ADDED" , e [ 'type' ])
597- # Verify that the object is deserialized correctly
598- self .assertTrue ( hasattr ( e ['object' ], ' metadata' ) )
599- self .assertEqual ("test1 " , e ['object' ].metadata .name )
600- self . assertEqual ( "1" , e [ ' object' ]. metadata . resource_version )
601- # Verify that the original object is saved
602- self . assertEqual ( json . loads ( test_json )[ 'object' ], e [ 'raw_object' ])
603-
604- # test case with deserialize=False
605- w = Watch ()
606- for e in w . stream ( fake_api . get_namespaces , deserialize = False ):
607- self . assertEqual ( "ADDED" , e [ 'type' ])
608- # The validation object remains in the original dictionary format
609- self .assertIsInstance ( e ['object' ], dict )
610- self .assertEqual ("test1 " , e ['object' ]['metadata' ]['name ' ])
611- self . assertEqual ( "1" , e [ 'object' ][ 'metadata' ][ 'resourceVersion' ])
612-
613- # verify the api is called twice
614- fake_api . get_namespaces . assert_has_calls ([
615- call (_preload_content = False , watch = True ),
616- call ( _preload_content = False , watch = True )
617- ])
579+ def test_watch_with_deserialize_param ( self ) :
580+ """test watch.stream() deserialize param"""
581+ # prepare test data
582+ test_json = '{"type": "ADDED", "object": {"metadata": {"name": "test1", "resourceVersion": "1"}, "spec": {}, "status": {}}}'
583+ fake_resp = Mock ()
584+ fake_resp . close = Mock ()
585+ fake_resp .release_conn = Mock ()
586+ fake_resp .stream = Mock (return_value = [ test_json + ' \n ' ] )
587+
588+ fake_api = Mock ()
589+ fake_api . get_namespaces = Mock (return_value = fake_resp )
590+ fake_api .get_namespaces . __doc__ = ':return: V1NamespaceList'
591+
592+ # test case with deserialize=True
593+ w = Watch ()
594+ for e in w . stream ( fake_api . get_namespaces , deserialize = True ):
595+ self . assertEqual ( "ADDED" , e [ 'type' ])
596+ # Verify that the object is deserialized correctly
597+ self . assertTrue ( hasattr ( e [ ' object' ], 'metadata' ))
598+ self .assertEqual ( "test1" , e ['object' ]. metadata . name )
599+ self .assertEqual ("1 " , e ['object' ].metadata .resource_version )
600+ # Verify that the original object is saved
601+ self . assertEqual ( json . loads ( test_json )[ ' object' ], e [ 'raw_object' ])
602+
603+ # test case with deserialize=False
604+ w = Watch ()
605+ for e in w . stream ( fake_api . get_namespaces , deserialize = False ):
606+ self . assertEqual ( "ADDED" , e [ 'type' ])
607+ # The validation object remains in the original dictionary format
608+ self . assertIsInstance ( e [ ' object' ], dict )
609+ self .assertEqual ( "test1" , e ['object' ][ 'metadata' ][ 'name' ] )
610+ self .assertEqual ("1 " , e ['object' ]['metadata' ]['resourceVersion ' ])
611+
612+ # verify the api is called twice
613+ fake_api . get_namespaces . assert_has_calls ([
614+ call ( _preload_content = False , watch = True ),
615+ call (_preload_content = False , watch = True )
616+ ] )
617+
618618if __name__ == '__main__' :
619619 unittest .main ()
0 commit comments