|
14 | 14 |
|
15 | 15 | import unittest
|
16 | 16 |
|
| 17 | +import os |
| 18 | + |
| 19 | +import time |
| 20 | + |
17 | 21 | from unittest.mock import Mock, call
|
18 | 22 |
|
19 |
| -from kubernetes import client |
| 23 | +from kubernetes import client,config |
20 | 24 |
|
21 | 25 | from .watch import Watch
|
22 | 26 |
|
| 27 | +from kubernetes.client import ApiException |
| 28 | + |
23 | 29 |
|
24 | 30 | class WatchTests(unittest.TestCase):
|
25 | 31 | def setUp(self):
|
@@ -99,6 +105,9 @@ def test_watch_with_interspersed_newlines(self):
|
99 | 105 | # Note that "timeout_seconds" below is not a timeout; rather, it disables retries and is
|
100 | 106 | # the only way to do so. Without that, the stream will re-read the test data forever.
|
101 | 107 | for e in w.stream(fake_api.get_namespaces, timeout_seconds=1):
|
| 108 | + # Here added a statement for exception for empty lines. |
| 109 | + if e is None: |
| 110 | + continue |
102 | 111 | count += 1
|
103 | 112 | self.assertEqual("test%d" % count, e['object'].metadata.name)
|
104 | 113 | self.assertEqual(3, count)
|
@@ -488,7 +497,84 @@ def test_watch_with_error_event_and_timeout_param(self):
|
488 | 497 | amt=None, decode_content=False)
|
489 | 498 | fake_resp.close.assert_called_once()
|
490 | 499 | fake_resp.release_conn.assert_called_once()
|
491 |
| - |
| 500 | + |
| 501 | + @classmethod |
| 502 | + def setUpClass(cls): |
| 503 | + cls.api = Mock() |
| 504 | + cls.namespace = "default" |
| 505 | + |
| 506 | + def test_pod_log_empty_lines(self): |
| 507 | + pod_name = "demo-bug" |
| 508 | + |
| 509 | + try: |
| 510 | + self.api.create_namespaced_pod = Mock() |
| 511 | + self.api.read_namespaced_pod = Mock() |
| 512 | + self.api.delete_namespaced_pod = Mock() |
| 513 | + self.api.read_namespaced_pod_log = Mock() |
| 514 | + |
| 515 | + #pod creating step |
| 516 | + self.api.create_namespaced_pod.return_value = None |
| 517 | + |
| 518 | + #Checking pod status |
| 519 | + mock_pod = Mock() |
| 520 | + mock_pod.status.phase = "Running" |
| 521 | + self.api.read_namespaced_pod.return_value = mock_pod |
| 522 | + |
| 523 | + # Printing at pod output |
| 524 | + self.api.read_namespaced_pod_log.return_value = iter(["Hello from Docker\n"]) |
| 525 | + |
| 526 | + # Wait for the pod to reach 'Running' |
| 527 | + timeout = 60 |
| 528 | + start_time = time.time() |
| 529 | + while time.time() - start_time < timeout: |
| 530 | + pod = self.api.read_namespaced_pod(name=pod_name, namespace=self.namespace) |
| 531 | + if pod.status.phase == "Running": |
| 532 | + break |
| 533 | + time.sleep(2) |
| 534 | + else: |
| 535 | + self.fail("Pod did not reach 'Running' state within timeout") |
| 536 | + |
| 537 | + # Reading and streaming logs using Watch (mocked) |
| 538 | + w = Watch() |
| 539 | + log_output = [] |
| 540 | + #Mock logs used for this test |
| 541 | + w.stream = Mock(return_value=[ |
| 542 | + "Hello from Docker", |
| 543 | + "", |
| 544 | + "", |
| 545 | + "\n\n", |
| 546 | + "Another log line", |
| 547 | + "", |
| 548 | + "\n", |
| 549 | + "Final log" |
| 550 | + ]) |
| 551 | + for event in w.stream(self.api.read_namespaced_pod_log, name=pod_name, namespace=self.namespace, follow=True): |
| 552 | + log_output.append(event) |
| 553 | + print(event) |
| 554 | + |
| 555 | + # Print outputs |
| 556 | + print(f"Captured logs: {log_output}") |
| 557 | + # self.assertTrue(any("Hello from Docker" in line for line in log_output)) |
| 558 | + # self.assertTrue(any(line.strip() == "" for line in log_output), "No empty lines found in logs") |
| 559 | + expected_log = [ |
| 560 | + "Hello from Docker", |
| 561 | + "", |
| 562 | + "", |
| 563 | + "\n\n", |
| 564 | + "Another log line", |
| 565 | + "", |
| 566 | + "\n", |
| 567 | + "Final log" |
| 568 | + ] |
| 569 | + |
| 570 | + self.assertEqual(log_output, expected_log, "Captured logs do not match expected logs") |
| 571 | + |
| 572 | + except ApiException as e: |
| 573 | + self.fail(f"Kubernetes API exception: {e}") |
| 574 | + finally: |
| 575 | + #checking pod is calling for delete |
| 576 | + self.api.delete_namespaced_pod(name=pod_name, namespace=self.namespace) |
| 577 | + self.api.delete_namespaced_pod.assert_called_once_with(name=pod_name, namespace=self.namespace) |
492 | 578 |
|
493 | 579 | if __name__ == '__main__':
|
494 | 580 | unittest.main()
|
0 commit comments