def test_success_healthcheck(self, mocks_dict): test_root_path = '/' test_healthcheck_path = '/healthchk_dir' healthcheck_warning = \ "WARNING --healthcheck-path is deprecated and will be dropped in future releases." \ " The connector should have the same value for root path and health check path." self._setup_external_api_mocks(mocks_dict) with patch(_BUILTINS_OPEN, self._read_valid_json()) as mock_file: with patch(_BUILTINS_PRINT) as mock_print: util = APSConnectUtil() util.install_backend(name=self._TEST_NAME, image='', config_file=FakeData.CONFIG_PATH, hostname='localhost', root_path=test_root_path, healthcheck_path=test_healthcheck_path) loading_file_msg = 'Loading config file: {}'.format(FakeData.CONFIG_PATH) connector_backend_msg = 'Connector backend - https://{}/'.format('localhost') mock_print.assert_has_calls([call(healthcheck_warning), call(loading_file_msg), call('Create config [ok]'), call('Create deployment [ok]'), call('Create service [ok]'), call('Create ingress [ok]'), call('Checking service availability'), call('Expose service [ok]'), call('Checking connector backend availability'), call('Check connector backend host [ok]'), call(connector_backend_msg), call('[Success]')]) mock_file.assert_has_calls([call(FakeData.CONFIG_PATH)]) self._assert_create_deployment_call(mocks_dict, test_healthcheck_path)
def test_success_defaults(self, mocks_dict): test_root_path = '/' self._setup_external_api_mocks(mocks_dict) with patch(_BUILTINS_OPEN, self._read_valid_json()) as mock_file: with patch(_BUILTINS_PRINT) as mock_print: util = APSConnectUtil() util.install_backend(name=self._TEST_NAME, image='', config_file=FakeData.CONFIG_PATH, hostname='localhost', root_path=test_root_path) loading_file_msg = 'Loading config file: {}'.format(FakeData.CONFIG_PATH) connector_backend_msg = 'Connector backend - https://{}/'.format('localhost') mock_print.assert_has_calls([call(loading_file_msg), call('Create config [ok]'), call('Create deployment [ok]'), call('Create service [ok]'), call('Create ingress [ok]'), call('Checking service availability'), call('Expose service [ok]'), call('Checking connector backend availability'), call('Check connector backend host [ok]'), call(connector_backend_msg), call('[Success]')]) mock_file.assert_has_calls([call(FakeData.CONFIG_PATH)]) self._assert_create_deployment_call(mocks_dict, test_root_path)
def test_hub_token(self, hub_cls): with patch(_BUILTINS_PRINT) as mock_print: expected_hub_token = '359b67d3-fdd7-4e90-a891-b909734fb64a' hub_mock = MagicMock() hub_mock.hub_id = expected_hub_token hub_cls.return_value = hub_mock util = APSConnectUtil() util.hub_token() mock_print.assert_called_with(expected_hub_token)
def _check_bad_config(self, mock_reader): util = APSConnectUtil() with patch(_BUILTINS_OPEN, mock_reader) as mock_file: self.assertRaises(SystemExit, util.install_backend, name=self._TEST_NAME, image='', config_file=FakeData.CONFIG_PATH, hostname='localhost') mock_file.assert_has_calls([call(FakeData.CONFIG_PATH)])
def test_unknown_version(self): with patch('apsconnectcli.apsconnect.get_version') as version_mock, \ patch('apsconnectcli.apsconnect.get_latest_version'), \ patch(_BUILTINS_PRINT) as print_mock: version_mock.return_value = None APSConnectUtil().version() self.assertEqual(print_mock.call_count, 1) self.assertTrue(GITHUB_RELEASES_PAGE in print_mock.call_args[0][0])
def test_outdated_version(self): with patch('apsconnectcli.apsconnect.get_version') as version_mock, \ patch('apsconnectcli.apsconnect.get_latest_version') as latest_version_mock, \ patch(_BUILTINS_PRINT) as print_mock: version_mock.return_value = '1.2.3' latest_version_mock.return_value = '1.2.4' APSConnectUtil().version() self.assertEqual(print_mock.call_count, 2) self.assertTrue('1.2.4' in print_mock.call_args[0][0])
def test_config_file_read_failure(self): file_read_err_msg = 'File read error.' with patch(_BUILTINS_PRINT) as mock_print: with patch(_BUILTINS_OPEN, mock_open(read_data=None)) as mock_file: mock_file.side_effect = Exception(file_read_err_msg) util = APSConnectUtil() self.assertRaises(SystemExit, util.install_backend, name=self._TEST_NAME, image='', config_file=FakeData.CONFIG_PATH, hostname='localhost') err_msg = "Unable to read config file, error: {}".format( file_read_err_msg) mock_print.assert_called_with(err_msg)
def _check_internal_fn_causes_systemexit(self, mock_dict, expected_err_msg): mock_dict['get_k8s_client'].return_value = FakeK8sApi(False) mock_dict['version_api'].return_value = MagicMock() mock_dict['core_api'].return_value = MagicMock() mock_dict['extensions_api'].return_value = MagicMock() util = APSConnectUtil() with patch(_BUILTINS_PRINT) as mock_print: with patch(_BUILTINS_OPEN, self._read_valid_json()) as mock_file: self.assertRaises(SystemExit, util.install_backend, name=self._TEST_NAME, image='', config_file=FakeData.CONFIG_PATH, hostname='localhost') mock_file.assert_has_calls([call(FakeData.CONFIG_PATH)]) mock_print.assert_called_with(expected_err_msg)