From a949a274e3f9c07218ccc092ee55eac5a5562ab7 Mon Sep 17 00:00:00 2001
From: Darragh Bailey <dbailey@hpe.com>
Date: Tue, 3 May 2016 19:22:21 +0100
Subject: [PATCH] Test stream no encoding bug

Tests that if no encoding is defined on the output that JJB does not
accidentally trigger a recursion issue when iterating over a large
number of jobs and attempting to wrap the output stream.

Inspired by commit message from: https://review.openstack.org/283441

Change-Id: Ie69c3af0beb9fd7c589a436e8c6d2862210f8a4c
Co-Authored-By: Hao Liu <hliu@redhat.com>
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
---
 tests/cmd/fixtures/large-number-of-jobs-001.yaml | 10 ++++++++++
 tests/cmd/subcommands/test_test.py               | 16 ++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 tests/cmd/fixtures/large-number-of-jobs-001.yaml

diff --git a/tests/cmd/fixtures/large-number-of-jobs-001.yaml b/tests/cmd/fixtures/large-number-of-jobs-001.yaml
new file mode 100644
index 000000000..26aa77c27
--- /dev/null
+++ b/tests/cmd/fixtures/large-number-of-jobs-001.yaml
@@ -0,0 +1,10 @@
+- job-template:
+    name: '{name}-{a}-{b}-{c}'
+
+- project:
+    name: project-name
+    a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+    b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+    c: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+    jobs:
+      - '{name}-{a}-{b}-{c}'
diff --git a/tests/cmd/subcommands/test_test.py b/tests/cmd/subcommands/test_test.py
index 4122a90dd..16f0e596b 100644
--- a/tests/cmd/subcommands/test_test.py
+++ b/tests/cmd/subcommands/test_test.py
@@ -73,6 +73,22 @@ class TestTests(CmdTestsBase):
                               'r', encoding='utf-8').read()
         self.assertEqual(console_out.getvalue().decode('utf-8'), xml_content)
 
+    def test_stream_input_output_no_encoding_exceed_recursion(self):
+        """
+        Test that we don't have issues processing large number of jobs and
+        outputting the result if the encoding is not set.
+        """
+        console_out = io.BytesIO()
+
+        input_file = os.path.join(self.fixtures_path,
+                                  'large-number-of-jobs-001.yaml')
+        with io.open(input_file, 'r') as f:
+            with mock.patch('sys.stdout', console_out):
+                console_out.encoding = None
+                with mock.patch('sys.stdin', f):
+                    args = ['test']
+                    self.execute_jenkins_jobs_with_args(args)
+
     def test_stream_input_output_utf8_encoding(self):
         """
         Run test mode simulating using pipes for input and output using