12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import unittest
15
+ import json
16
16
17
- import os
17
+ import unittest
18
18
19
19
import time
20
20
21
21
from unittest .mock import Mock , call
22
22
23
- from kubernetes import client , config
23
+ from kubernetes import client
24
24
25
25
from .watch import Watch
26
26
@@ -497,15 +497,15 @@ def test_watch_with_error_event_and_timeout_param(self):
497
497
amt = None , decode_content = False )
498
498
fake_resp .close .assert_called_once ()
499
499
fake_resp .release_conn .assert_called_once ()
500
-
500
+
501
501
@classmethod
502
502
def setUpClass (cls ):
503
503
cls .api = Mock ()
504
504
cls .namespace = "default"
505
505
506
506
def test_pod_log_empty_lines (self ):
507
507
pod_name = "demo-bug"
508
-
508
+
509
509
try :
510
510
self .api .create_namespaced_pod = Mock ()
511
511
self .api .read_namespaced_pod = Mock ()
@@ -514,12 +514,12 @@ def test_pod_log_empty_lines(self):
514
514
515
515
#pod creating step
516
516
self .api .create_namespaced_pod .return_value = None
517
-
517
+
518
518
#Checking pod status
519
519
mock_pod = Mock ()
520
520
mock_pod .status .phase = "Running"
521
521
self .api .read_namespaced_pod .return_value = mock_pod
522
-
522
+
523
523
# Printing at pod output
524
524
self .api .read_namespaced_pod_log .return_value = iter (["Hello from Docker\n " ])
525
525
@@ -553,7 +553,7 @@ def test_pod_log_empty_lines(self):
553
553
print (event )
554
554
555
555
# Print outputs
556
- print (f"Captured logs: { log_output } " )
556
+ print (f"Captured logs: { log_output } " )
557
557
# self.assertTrue(any("Hello from Docker" in line for line in log_output))
558
558
# self.assertTrue(any(line.strip() == "" for line in log_output), "No empty lines found in logs")
559
559
expected_log = [
@@ -566,7 +566,7 @@ def test_pod_log_empty_lines(self):
566
566
"\n " ,
567
567
"Final log"
568
568
]
569
-
569
+
570
570
self .assertEqual (log_output , expected_log , "Captured logs do not match expected logs" )
571
571
572
572
except ApiException as e :
@@ -576,44 +576,44 @@ def test_pod_log_empty_lines(self):
576
576
self .api .delete_namespaced_pod (name = pod_name , namespace = self .namespace )
577
577
self .api .delete_namespaced_pod .assert_called_once_with (name = pod_name , namespace = self .namespace )
578
578
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
+
618
618
if __name__ == '__main__' :
619
619
unittest .main ()
0 commit comments