def test_runner_lpass_json(self, mock_stdout, mock_stderr, mock_db): runner = Runner() mock_db.return_value = { 'password': '******', 'host': 'host', 'user': '******', 'type': 'type', 'protocol': 'protocol', 'port': 123, 'database': 'dbname', 'lastpass_share_name_suffix': 'lastpass_share_name_suffix', 'connection_type': 'connection_type', } self.assertEqual(0, runner.run(['/bin/db-facts', 'json', 'foo'])) mock_db.assert_called_with(['foo']) self.assertEqual(mock_stderr.getvalue(), '') parsed_json_value = { 'database': 'dbname', 'user': '******', 'password': '******', 'port': 123, 'protocol': 'protocol', 'type': 'type', 'host': 'host', 'lastpass_share_name_suffix': 'lastpass_share_name_suffix', 'connection_type': 'connection_type' } self.assertEqual(json.loads(mock_stdout.getvalue()), parsed_json_value)
def test_runner_exception(self, mock_stdout, mock_stderr, mock_db): runner = Runner() mock_db.side_effect = UserErrorException('error message here') self.assertEqual(1, runner.run(['/bin/db-facts', 'sh', 'foo'])) mock_db.assert_called_with(['foo']) self.assertEqual(mock_stderr.getvalue(), 'error message here\n') self.assertEqual(mock_stdout.getvalue(), '')
def test_runner_no_arg(self, mock_stdout, mock_stderr, mock_db): runner = Runner() out = runner.run(['/bin/db-facts']) self.assertEqual(out, 1) self.assertIn( without_whitespace('Pull information about databases from'), without_whitespace(mock_stderr.getvalue())) self.assertEqual(mock_stdout.getvalue(), '')
def test_runner_lpass(self, mock_stdout, mock_stderr, mock_db): runner = Runner() mock_db.return_value = { 'password': '******', 'host': 'host', 'user': '******', 'type': 'type', 'protocol': 'protocol', 'port': 123, 'database': 'dbname', 'lastpass_share_name_suffix': 'lastpass_share_name_suffix', 'connection_type': 'connection_type', } self.assertEqual(0, runner.run(['/bin/db-facts', 'sh', 'foo'])) mock_db.assert_called_with(['foo']) self.assertEqual(mock_stderr.getvalue(), '') self.assertEqual(mock_stdout.getvalue(), 'export CONNECTION_TYPE\n' 'CONNECTION_TYPE=connection_type\n' 'export DB_DATABASE\n' 'DB_DATABASE=dbname\n' 'export DB_HOST\n' 'DB_HOST=host\n' 'export LASTPASS_SHARE_NAME_SUFFIX\n' 'LASTPASS_SHARE_NAME_SUFFIX=' 'lastpass_share_name_suffix\n' 'export DB_PASSWORD\n' 'DB_PASSWORD=password\n' 'export DB_PORT\n' 'DB_PORT=123\n' 'export DB_PROTOCOL\n' 'DB_PROTOCOL=protocol\n' 'export DB_TYPE\n' 'DB_TYPE=type\n' 'export DB_USERNAME\n' 'DB_USERNAME=user\n')
def test_runner_list(self, mock_load_config, mock_stdout, mock_stderr): runner = Runner() mock_load_config.return_value = { 'dbs': { 'mydb1': { 'description': 'My favorite database', 'password': '******', 'host': 'host', 'user': '******', 'type': 'type', 'protocol': 'protocol', 'port': 123, 'database': 'dbname', 'lastpass_share_name_suffix': 'lastpass_share_name_suffix', 'connection_type': 'connection_type', }, 'mydb2': { 'password': '******', 'host': 'host', 'user': '******', 'type': 'type', 'protocol': 'protocol', 'port': 123, 'database': 'dbname', 'lastpass_share_name_suffix': 'lastpass_share_name_suffix', 'connection_type': 'connection_type', } } } self.assertEqual(0, runner.run(['/bin/db-facts', 'list'])) mock_load_config.assert_called_with() self.assertEqual(mock_stderr.getvalue(), '') self.assertEqual(mock_stdout.getvalue(), ("Available db_names:\n" "* mydb1 (My favorite database)\n" "* mydb2\n"))
def test_runner_help(self, mock_stdout, mock_stderr, mock_db): runner = Runner() with self.assertRaises(SystemExit): runner.run(['/bin/db-facts', '--help']) self.assertEqual(mock_stderr.getvalue(), '') helpstr = """ usage: db-facts [-h] {list,json,config,sh} ... Pull information about databases from user-friendly names positional arguments: {list,json,config,sh} list List available dbnames json Report output in JSON format config Report output in db-facts config format sh Report output in Bourne shell envionment variable format optional arguments: -h, --help show this help message and exit""" self.assertEqual(without_whitespace(mock_stdout.getvalue()), without_whitespace(helpstr))