예제 #1
0
    def test_get_latest_commit_git_error(self, sh):
        # Current directory not a git repo
        err = b"fatal: Not a git repository (or any of the parent directories): .git"
        sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"",
                                             err)

        with self.assertRaisesRegex(GitContextError,
                                    u"fåke/path is not a git repository."):
            GitContext.from_local_repository(u"fåke/path")

        # assert that commit message was read using git command
        sh.git.assert_called_once_with("log", "-1", "--pretty=%H",
                                       **self.expected_sh_special_args)
        sh.git.reset_mock()
        err = b"fatal: Random git error"
        sh.git.side_effect = ErrorReturnCode("git log -1 --pretty=%H", b"",
                                             err)

        expected_msg = u"An error occurred while executing 'git log -1 --pretty=%H': {0}".format(
            err)
        with self.assertRaisesRegex(GitContextError, expected_msg):
            GitContext.from_local_repository(u"fåke/path")

        # assert that commit message was read using git command
        sh.git.assert_called_once_with("log", "-1", "--pretty=%H",
                                       **self.expected_sh_special_args)
예제 #2
0
파일: test_git.py 프로젝트: tommyip/gitlint
    def test_get_latest_commit_git_error(self, sh):
        # Current directory not a git repo
        err = b"fatal: Not a git repository (or any of the parent directories): .git"
        sh.git.side_effect = ErrorReturnCode("git rev-list --max-count=1 HEAD",
                                             b"", err)

        with self.assertRaisesRegex(GitContextError,
                                    u"fåke/path is not a git repository."):
            GitContext.from_local_repository(u"fåke/path")

        # assert that commit message was read using git command
        sh.git.assert_called_once_with("rev-list",
                                       "--max-count=1",
                                       "HEAD",
                                       _tty_out=False,
                                       _cwd=u"fåke/path")
        sh.git.reset_mock()
        err = b"fatal: Random git error"
        sh.git.side_effect = ErrorReturnCode("git rev-list --max-count=1 HEAD",
                                             b"", err)

        expected_msg = u"An error occurred while executing 'git rev-list --max-count=1 HEAD': {0}".format(
            err)
        with self.assertRaisesRegex(GitContextError, expected_msg):
            GitContext.from_local_repository(u"fåke/path")

        # assert that commit message was read using git command
        sh.git.assert_called_once_with("rev-list",
                                       "--max-count=1",
                                       "HEAD",
                                       _tty_out=False,
                                       _cwd=u"fåke/path")
예제 #3
0
    def test_get_latest_commit_git_error(self, sh):
        # Current directory not a git repo
        err = b"fatal: Not a git repository (or any of the parent directories): .git"
        sh.git.log.side_effect = ErrorReturnCode("git log -1 --pretty=%B", b"",
                                                 err)

        with self.assertRaisesRegexp(GitContextError,
                                     "fake/path is not a git repository."):
            GitContext.from_local_repository("fake/path")

        # assert that commit message was read using git command
        sh.git.log.assert_called_once_with('-1',
                                           '--pretty=%B',
                                           _tty_out=False,
                                           _cwd="fake/path")

        sh.git.log.reset_mock()
        err = b"fatal: Random git error"
        sh.git.log.side_effect = ErrorReturnCode("git log -1 --pretty=%B", b"",
                                                 err)

        expected_msg = "An error occurred while executing 'git log -1 --pretty=%B': {0}".format(
            err)
        with self.assertRaisesRegexp(GitContextError, expected_msg):
            GitContext.from_local_repository("fake/path")

        # assert that commit message was read using git command
        sh.git.log.assert_called_once_with('-1',
                                           '--pretty=%B',
                                           _tty_out=False,
                                           _cwd="fake/path")
예제 #4
0
파일: shell.py 프로젝트: zombig/gitlint
    def _exec(*args, **kwargs):
        if sys.version_info[0] == 2:
            no_command_error = OSError  # noqa pylint: disable=undefined-variable,invalid-name
        else:
            no_command_error = FileNotFoundError  # noqa pylint: disable=undefined-variable

        pipe = subprocess.PIPE
        popen_kwargs = {'stdout': pipe, 'stderr': pipe, 'shell': kwargs.get('_tty_out', False)}
        if '_cwd' in kwargs:
            popen_kwargs['cwd'] = kwargs['_cwd']

        try:
            p = subprocess.Popen(args, **popen_kwargs)
            result = p.communicate()
        except no_command_error:
            raise CommandNotFound

        exit_code = p.returncode
        stdout = ustr(result[0])
        stderr = result[1]  # 'sh' does not decode the stderr bytes to unicode
        full_cmd = '' if args is None else ' '.join(args)

        # If not _ok_code is specified, then only a 0 exit code is allowed
        ok_exit_codes = kwargs.get('_ok_code', [0])

        if exit_code in ok_exit_codes:
            return ShResult(full_cmd, stdout, stderr, exit_code)

        # Unexpected error code => raise ErrorReturnCode
        raise ErrorReturnCode(full_cmd, stdout, stderr, p.returncode)
예제 #5
0
    def _exec(*args, **kwargs):
        pipe = subprocess.PIPE
        popen_kwargs = {
            'stdout': pipe,
            'stderr': pipe,
            'shell': kwargs.get('_tty_out', False)
        }
        if '_cwd' in kwargs:
            popen_kwargs['cwd'] = kwargs['_cwd']
        if '_env' in kwargs:
            popen_kwargs['env'] = kwargs['_env']

        try:
            p = subprocess.Popen(args, **popen_kwargs)
            result = p.communicate()
        except FileNotFoundError as exc:
            raise CommandNotFound from exc

        exit_code = p.returncode
        stdout = result[0].decode(DEFAULT_ENCODING)
        stderr = result[1]  # 'sh' does not decode the stderr bytes to unicode
        full_cmd = '' if args is None else ' '.join(args)

        # If not _ok_code is specified, then only a 0 exit code is allowed
        ok_exit_codes = kwargs.get('_ok_code', [0])

        if exit_code in ok_exit_codes:
            return ShResult(full_cmd, stdout, stderr, exit_code)

        # Unexpected error code => raise ErrorReturnCode
        raise ErrorReturnCode(full_cmd, stdout, stderr, p.returncode)
예제 #6
0
    def test_run_script_nonzero_exit_code(self, mock_open, redirect_callback_mock, tox_shell_command_mock):

        # Given the shell command exits with an error code
        tox_shell_command_mock.side_effect = ErrorReturnCode('tox', b'mock stdout', b'mock error')

        # When I use run_tox() to run 'mycommand'
        with raises(StepRunnerException): # Then it should raise an exception
            run_tox('/my/output/file', 'mycommand')
예제 #7
0
    def test_get_latest_commit_git_error(self, sh):
        err = b"fatal: Not a git repository (or any of the parent directories): .git"
        sh.git.log.side_effect = ErrorReturnCode("git log -1 --pretty=%B", b"",
                                                 err)

        with self.assertRaisesRegexp(GitContextError,
                                     "fake/path is not a git repository."):
            GitContext.from_local_repository("fake/path")

        # assert that commit message was read using git command
        sh.git.log.assert_called_once_with('-1',
                                           '--pretty=%B',
                                           _tty_out=False,
                                           _cwd="fake/path")
예제 #8
0
    def test_check_mod_ipip(self, m_command):

        # Mock out sh.Command._create
        m_modprobe = Mock(Command)
        m_ip6tables = Mock(Command)

        def _create(cmd):
            if cmd == "modprobe":
                return m_modprobe
            elif cmd == "ip6tables":
                return m_ip6tables
            else:
                raise ValueError()
        m_command._create = _create

        def m_module_loaded(module):
            return module == "xt_set"

        with patch("calico_ctl.checksystem.module_loaded", m_module_loaded):
            result = _check_kernel_modules(False)

            # Fix = false means system is not OK.
            self.assertFalse(result)
            self.assertFalse(m_modprobe.called)
            m_ip6tables.assert_called_once_with("-L")

            # Reset mocks
            m_modprobe.reset_mock()
            m_ip6tables.reset_mock()

            result = _check_kernel_modules(True)

            # Fix = true should attempt to fix with modprobe.
            self.assertTrue(result)
            m_modprobe.assert_called_once_with("ipip")
            m_ip6tables.assert_called_once_with("-L")

            # Reset mocks
            m_modprobe.reset_mock()
            m_ip6tables.reset_mock()

            # Fix = true, but modprobe fails.
            m_modprobe.side_effect = ErrorReturnCode("modprobe ipip", "", "")
            result = _check_kernel_modules(True)

            self.assertFalse(result)
            m_modprobe.assert_called_once_with("ipip")
            m_ip6tables.assert_called_once_with("-L")
    def test_run_error(self, mock_packer):
        action = self.get_action_instance(self.blank_config)
        test_dict = {'packerfile': 'test/file/packer'}

        # Made a bytes string to allow encodding and decodding to pass python 3 test
        expected_result = b'Test Build'
        mock_error = ErrorReturnCode(stdout=expected_result,
                                     full_cmd=b'test_cmd',
                                     stderr=b'test_stdout')

        mock_packer.return_value.build.side_effect = mock_error

        result = action.run(**test_dict)
        self.assertEqual(result, (False, str(expected_result)))
        mock_packer.assert_called_once_with(test_dict['packerfile'],
                                            exc=None,
                                            only=None,
                                            variables=None,
                                            vars_file=None)
예제 #10
0
class TestStepImplementerUatTest(BaseTSSCTestCase):
    """Test Step Implementer UAT

    Runner for the UAT Step Implementer.
    """
    @patch('sh.mvn', create=True)
    def test_uat_mandatory_selenium_hub_url_missing(self, _mvn_mock):
        """Test when mandatory selenium hub url is missing."""
        config = {'tssc-config': {'uat': {'implementer': 'Maven'}}}
        factory = TSSCFactory(config)
        error_message = '.* is missing the required configuration keys ' \
            '\\(\\[\'selenium-hub-url\'\\]\\).*'
        with self.assertRaisesRegex(AssertionError, error_message):
            factory.run_step('uat')

    @patch('sh.mvn', create=True)
    def test_uat_mandatory_target_base_url_missing(self, _mvn_mock):
        """Test when mandatory target base url is missing."""
        config = {'tssc-config': {'uat': {'implementer': 'Maven'}}}
        factory = TSSCFactory(config)
        with self.assertRaisesRegex(ValueError,
                                    'No target base url was specified'):
            factory.config.set_step_config_overrides(
                'uat', {
                    'selenium-hub-url': SELENIUM_HUB_URL,
                })
            factory.run_step('uat')

    @patch('sh.mvn', create=True)
    def test_uat_default_pom_file_missing(self, _mvn_mock):
        """Test if the default pom file is missing."""
        config = {
            'tssc-config': {
                'uat': {
                    'implementer': 'Maven',
                }
            }
        }
        factory = TSSCFactory(config)
        with self.assertRaisesRegex(ValueError,
                                    'Given pom file does not exist: pom.xml'):
            factory.config.set_step_config_overrides(
                'uat', {
                    'selenium-hub-url': SELENIUM_HUB_URL,
                    'target-base-url': TARGET_BASE_URL
                })
            factory.run_step('uat')

    @patch('sh.mvn', create=True)
    def test_uat_runtime_pom_file_missing(self, _mvn_mock):
        """Test when pom file is invalid."""
        config = {'tssc-config': {'uat': {'implementer': 'Maven'}}}
        factory = TSSCFactory(config)
        with self.assertRaisesRegex(
                ValueError,
                'Given pom file does not exist: does-not-exist-pom.xml'):
            factory.config.set_step_config_overrides(
                'uat', {
                    'pom-file': 'does-not-exist-pom.xml',
                    'selenium-hub-url': SELENIUM_HUB_URL,
                    'target-base-url': TARGET_BASE_URL
                })
            factory.run_step('uat')

    @patch('sh.mvn', create=True)
    def test_uat_config_file_pom_file_missing(self, _mvn_mock):
        """Test when config has invalid pom file."""
        config = {
            'tssc-config': {
                'uat': {
                    'implementer': 'Maven',
                    'config': {
                        'pom-file': 'does-not-exist.pom',
                        'selenium-hub-url': SELENIUM_HUB_URL,
                        'target-base-url': TARGET_BASE_URL
                    }
                }
            }
        }
        factory = TSSCFactory(config)
        with self.assertRaisesRegex(
                ValueError,
                'Given pom file does not exist: does-not-exist.pom'):
            factory.run_step('uat')

    @patch('sh.mvn',
           create=True,
           side_effect=ErrorReturnCode('mvn clean -Pintegration-test test',
                                       b'mock out', b'mock error'))
    def test_uat_mvn_error_return_code(self, _mvn_mock):
        """Test when maven returns an error code."""
        with TempDirectory() as temp_dir:
            temp_dir.write(
                'src/main/java/com/mycompany/app/App.java',
                b'''package com.mycompany.app;
                    public class App {
                        public static void main( String[] args ) {
                            System.out.println( "Hello World!" );
                        }
                    }
                ''')
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': 'Failure message'
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error invoking mvn:.*'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)

    @patch('sh.mvn', create=True)
    def test_uat_no_reports_directory_reference_in_pom(self, mvn_mock):
        """Test when no reports directory in defined in the pom."""
        reports_dir = 'target/surefire-reports'
        group_id = 'com.mycompany.app'
        artifact_id = 'my-app'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir,
                                       build_config,
                                       group_id=group_id,
                                       artifact_id=artifact_id)
            test_results_dir = path.join(temp_dir.path, 'target/cucumber')
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path,
                reports_dir,
                [
                    '{group_id}.{artifact_id}.ClassNameTest.txt' \
                        .format(group_id=group_id, artifact_id=artifact_id),
                    'TEST-{group_id}.{artifact_id}.ClassNameTest.xml' \
                        .format(group_id=group_id, artifact_id=artifact_id)
                ]
            )
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'Uat step run successfully and reports were generated'
                        },
                        'options': {
                            'pom-path': str(pom_file_path)
                        },
                        'report-artifacts': [{
                            'name':
                            'Uat results generated',
                            'path':
                            f'file://{str(test_results_dir)}'
                        }]
                    }
                }
            }

            settings_file_path = f'{temp_dir.path}/tssc-working/uat/settings.xml'
            run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                 expected_step_results)
            mvn_mock.assert_called_once_with(
                'clean',
                '-Pintegration-test',
                f'-Dselenium.hub.url={SELENIUM_HUB_URL}',
                f'-Dtarget.base.url={TARGET_BASE_URL}',
                '-Dcucumber.plugin=html:target/cucumber/cucumber.html,' \
                    'json:target/cucumber/cucumber.json',
                'test',
                '-f', pom_file_path,
                '-s', settings_file_path,
                _out=Any(IOBase),
                _err=Any(IOBase)
            )

    @patch('sh.mvn', create=True)
    def test_uat_reports_directory_reference_exists_in_pom(self, mvn_mock):
        """Test when the reports directory exists in the pom."""
        group_id = 'com.mycompany.app'
        artifact_id = 'my-app'
        with TempDirectory() as temp_dir:
            reports_dir = path.join(temp_dir.path, 'target/custom-reports-dir')
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration>
                                              <reportsDirectory>{reports_dir}</reportsDirectory>
                                          </configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''.format(reports_dir=reports_dir)
            pom_file_path = create_pom(temp_dir,
                                       build_config,
                                       group_id=group_id,
                                       artifact_id=artifact_id)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path,
                reports_dir,
                [
                    '{group_id}.{artifact_id}.ClassNameTest.txt' \
                        .format(group_id=group_id, artifact_id=artifact_id),
                    'TEST-{group_id}.{artifact_id}.ClassNameTest.xml' \
                        .format(group_id=group_id, artifact_id=artifact_id)
                ]
            )

            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'Uat step run successfully and reports were generated'
                        },
                        'options': {
                            'pom-path': str(pom_file_path)
                        },
                        'report-artifacts': [{
                            'name':
                            'Uat results generated',
                            'path':
                            f'file://{path.join(temp_dir.path, "target/cucumber")}'
                        }]
                    }
                }
            }

            settings_file_path = f'{temp_dir.path}/tssc-working/uat/settings.xml'
            run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                 expected_step_results)
            mvn_mock.assert_called_once_with(
                'clean',
                '-Pintegration-test',
                f'-Dselenium.hub.url={SELENIUM_HUB_URL}',
                f'-Dtarget.base.url={TARGET_BASE_URL}',
                '-Dcucumber.plugin=html:target/cucumber/cucumber.html,' \
                    'json:target/cucumber/cucumber.json',
                'test',
                '-f', pom_file_path,
                '-s', settings_file_path,
                _out=Any(IOBase),
                _err=Any(IOBase)
            )

    def test_uat_test_missing_surefire_plugin_in_pom(self):
        """Test when missing surefire plugin in pom."""
        with TempDirectory() as temp_dir:
            build_config = ''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            factory = TSSCFactory(config, temp_dir)
            with self.assertRaisesRegex(
                    ValueError,
                    'Uat dependency "maven-surefire-plugin" missing from POM.'
            ):
                factory.run_step('uat')

    @patch('sh.mvn', create=True)
    def test_uat_empty_reports_dir(self, mvn_mock):
        """Test when report dir is empty."""
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, reports_dir, [])
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': "Failure message"
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error: No uat defined'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)

    @patch('sh.mvn', create=True)
    def test_uat_run_attempt_fails_default_fail_on_no_tests_flag(
            self, mvn_mock):
        """Test when failure on no tests."""
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, reports_dir, [], True)
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': "Failure message"
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error: No uat defined'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)

    @patch('sh.mvn', create=True)
    def test_uat_run_attempt_fails_fail_on_no_tests_flag_false(self, mvn_mock):
        """Test when failure on no tests with flag to ignore it set to false."""
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'fail-on-no-tests': False,
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, reports_dir, [])
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'Uat step run successfully, but no tests were found'
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path),
                            'fail-on-no-tests': False
                        }
                    }
                }
            }

            settings_file_path = f'{temp_dir.path}/tssc-working/uat/settings.xml'
            run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                 expected_step_results)
            mvn_mock.assert_called_once_with(
                'clean',
                '-Pintegration-test',
                f'-Dselenium.hub.url={SELENIUM_HUB_URL}',
                f'-Dtarget.base.url={TARGET_BASE_URL}',
                '-Dcucumber.plugin=html:target/cucumber/cucumber.html,' \
                    'json:target/cucumber/cucumber.json',
                'test',
                '-f', pom_file_path,
                '-s', settings_file_path,
                _out=Any(IOBase),
                _err=Any(IOBase)
            )

    @patch('sh.mvn', create=True)
    def test_uat_run_attempt_fails_fail_on_no_tests_flag_true(self, mvn_mock):
        """Test when failure on no tests with flag to ignore it set to true."""
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                          <configuration></configuration>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir, build_config)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'fail-on-no-tests': True,
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path, reports_dir, [])
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success': False,
                            'message': "Failure message"
                        },
                        'report-artifacts': [],
                        'options': {
                            'pom-path': str(pom_file_path)
                        }
                    }
                }
            }

            with self.assertRaisesRegex(RuntimeError, 'Error: No uat defined'):
                run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                     expected_step_results)

    @patch('sh.mvn', create=True)
    def test_uat_with_custom_report_dir(self, mvn_mock):
        """Test with custom report directory."""
        group_id = 'com.mycompany.app'
        artifact_id = 'my-app'
        reports_dir = 'target/surefire-reports'
        with TempDirectory() as temp_dir:
            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir,
                                       build_config,
                                       group_id=group_id,
                                       artifact_id=artifact_id)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL,
                            'target-base-url': TARGET_BASE_URL,
                            'report-dir': 'custom-cucumber'
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path,
                reports_dir,
                [
                    '{group_id}.{artifact_id}.ClassNameTest.txt' \
                        .format(group_id=group_id, artifact_id=artifact_id),
                    'TEST-{group_id}.{artifact_id}.ClassNameTest.xml' \
                        .format(group_id=group_id, artifact_id=artifact_id)
                ]
            )

            test_results_dir = path.join(temp_dir.path,
                                         'target/custom-cucumber')
            expected_step_results = {
                'tssc-results': {
                    'uat': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'Uat step run successfully and reports were generated'
                        },
                        'options': {
                            'pom-path': str(pom_file_path)
                        },
                        'report-artifacts': [{
                            'name':
                            'Uat results generated',
                            'path':
                            f'file://{str(test_results_dir)}'
                        }]
                    }
                }
            }

            settings_file_path = f'{temp_dir.path}/tssc-working/uat/settings.xml'
            run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                 expected_step_results)
            mvn_mock.assert_called_once_with(
                'clean',
                '-Pintegration-test',
                f'-Dselenium.hub.url={SELENIUM_HUB_URL}',
                f'-Dtarget.base.url={TARGET_BASE_URL}',
                '-Dcucumber.plugin=html:target/custom-cucumber/cucumber.html,' \
                    'json:target/custom-cucumber/cucumber.json',
                'test',
                '-f', pom_file_path,
                '-s', settings_file_path,
                _out=Any(IOBase),
                _err=Any(IOBase)
            )

    @patch('sh.mvn', create=True)
    def test_uat_with_deploy_dir(self, mvn_mock):
        """Test with report directory from the deploy."""
        group_id = 'com.mycompany.app'
        artifact_id = 'my-app'
        reports_dir = 'target/surefire-reports'
        endpoint_url = 'http://new-unique-app:8080'
        with TempDirectory() as temp_dir:
            temp_dir.makedir('tssc-results')
            temp_dir.write(
                'tssc-results/tssc-results.yml',
                bytes(
                    f'''tssc-results:
                            deploy:
                              result:
                                deploy-endpoint-url: {endpoint_url}''',
                    'utf-8'))

            build_config = '''<build>
                                  <plugins>
                                      <plugin>
                                          <artifactId>maven-surefire-plugin</artifactId>
                                          <version>${{surefire-plugin.version}}</version>
                                      </plugin>
                                  </plugins>
                              </build>'''
            pom_file_path = create_pom(temp_dir,
                                       build_config,
                                       group_id=group_id,
                                       artifact_id=artifact_id)
            config = {
                'tssc-config': {
                    'uat': {
                        'implementer': 'Maven',
                        'config': {
                            'pom-file': str(pom_file_path),
                            'selenium-hub-url': SELENIUM_HUB_URL
                        }
                    }
                }
            }
            mvn_mock.side_effect = create_mvn_side_effect(
                pom_file_path,
                reports_dir,
                [
                    '{group_id}.{artifact_id}.ClassNameTest.txt' \
                        .format(group_id=group_id, artifact_id=artifact_id),
                    'TEST-{group_id}.{artifact_id}.ClassNameTest.xml' \
                        .format(group_id=group_id, artifact_id=artifact_id)
                ]
            )

            test_results_dir = path.join(temp_dir.path, 'target/cucumber')
            expected_step_results = {
                'tssc-results': {
                    'deploy': {
                        'result': {
                            'deploy-endpoint-url': f'{endpoint_url}'
                        }
                    },
                    'uat': {
                        'result': {
                            'success':
                            True,
                            'message':
                            'Uat step run successfully and reports were generated'
                        },
                        'options': {
                            'pom-path': str(pom_file_path)
                        },
                        'report-artifacts': [{
                            'name':
                            'Uat results generated',
                            'path':
                            f'file://{str(test_results_dir)}'
                        }]
                    }
                }
            }

            settings_file_path = f'{temp_dir.path}/tssc-working/uat/settings.xml'
            run_step_test_with_result_validation(temp_dir, 'uat', config,
                                                 expected_step_results)
            mvn_mock.assert_called_once_with(
                'clean',
                '-Pintegration-test',
                f'-Dselenium.hub.url={SELENIUM_HUB_URL}',
                f'-Dtarget.base.url={endpoint_url}',
                '-Dcucumber.plugin=html:target/cucumber/cucumber.html,' \
                    'json:target/cucumber/cucumber.json',
                'test',
                '-f', pom_file_path,
                '-s', settings_file_path,
                _out=Any(IOBase),
                _err=Any(IOBase)
            )