Path: blob/develop/tests/unit/customizations/history/test_history.py
1569 views
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import argparse13import os1415from botocore.session import Session16from botocore.history import HistoryRecorder17from botocore.exceptions import ProfileNotFound1819from awscli.testutils import unittest, mock, FileCreator20from awscli.compat import StringIO21from awscli.customizations.history import attach_history_handler22from awscli.customizations.history import add_history_commands23from awscli.customizations.history import HistoryCommand24from awscli.customizations.history.db import DatabaseHistoryHandler252627class TestAttachHistoryHandler(unittest.TestCase):28def setUp(self):29self.files = FileCreator()3031def tearDown(self):32self.files.remove_all()3334@mock.patch('awscli.customizations.history.sqlite3')35@mock.patch('awscli.customizations.history.db.sqlite3')36@mock.patch('awscli.customizations.history.HISTORY_RECORDER',37spec=HistoryRecorder)38def test_attach_history_handler(self, mock_recorder, mock_db_sqlite3, mock_sqlite3):39mock_session = mock.Mock(Session)40mock_session.get_scoped_config.return_value = {41'cli_history': 'enabled'42}4344parsed_args = argparse.Namespace()45parsed_args.command = 's3'4647attach_history_handler(session=mock_session, parsed_args=parsed_args)48self.assertEqual(mock_recorder.add_handler.call_count, 1)49self.assertIsInstance(50mock_recorder.add_handler.call_args[0][0], DatabaseHistoryHandler)51self.assertTrue(mock_db_sqlite3.connect.called)5253@mock.patch('awscli.customizations.history.sqlite3')54@mock.patch('awscli.customizations.history.db.sqlite3')55@mock.patch('awscli.customizations.history.HISTORY_RECORDER',56spec=HistoryRecorder)57def test_no_attach_history_handler_when_history_not_configured(58self, mock_recorder, mock_db_sqlite3, mock_sqlite3):59mock_session = mock.Mock(Session)60mock_session.get_scoped_config.return_value = {}6162parsed_args = argparse.Namespace()63parsed_args.command = 's3'6465attach_history_handler(session=mock_session, parsed_args=parsed_args)66self.assertFalse(mock_recorder.add_handler.called)67self.assertFalse(mock_db_sqlite3.connect.called)6869@mock.patch('awscli.customizations.history.sqlite3')70@mock.patch('awscli.customizations.history.db.sqlite3')71@mock.patch('awscli.customizations.history.HISTORY_RECORDER',72spec=HistoryRecorder)73def test_no_attach_history_handler_when_command_is_history(74self, mock_recorder, mock_db_sqlite3, mock_sqlite3):75mock_session = mock.Mock(Session)76mock_session.get_scoped_config.return_value = {77'cli_history': 'enabled'78}7980parsed_args = argparse.Namespace()81parsed_args.command = 'history'8283attach_history_handler(session=mock_session, parsed_args=parsed_args)84self.assertFalse(mock_recorder.add_handler.called)85self.assertFalse(mock_db_sqlite3.connect.called)8687@mock.patch('awscli.customizations.history.sqlite3', None)88@mock.patch('awscli.customizations.history.db.sqlite3')89@mock.patch('awscli.customizations.history.HISTORY_RECORDER',90spec=HistoryRecorder)91def test_no_attach_history_handler_when_no_sqlite3(92self, mock_recorder, mock_sqlite3):93mock_session = mock.Mock(Session)94mock_session.get_scoped_config.return_value = {95'cli_history': 'enabled'96}9798parsed_args = argparse.Namespace()99parsed_args.command = 's3'100101with mock.patch('sys.stderr', StringIO()) as mock_stderr:102attach_history_handler(103session=mock_session, parsed_args=parsed_args)104self.assertIn(105'enabled but sqlite3 is unavailable', mock_stderr.getvalue())106self.assertFalse(mock_recorder.add_handler.called)107self.assertFalse(mock_sqlite3.connect.called)108109@mock.patch('awscli.customizations.history.sqlite3')110@mock.patch('awscli.customizations.history.db.sqlite3')111@mock.patch('awscli.customizations.history.HISTORY_RECORDER',112spec=HistoryRecorder)113def test_create_directory_no_exists(self, mock_recorder,114mock_db_sqlite3, mock_sqlite3):115mock_session = mock.Mock(Session)116mock_session.get_scoped_config.return_value = {117'cli_history': 'enabled'118}119120parsed_args = argparse.Namespace()121parsed_args.command = 's3'122123directory_to_create = os.path.join(self.files.rootdir, 'create-dir')124db_filename = os.path.join(directory_to_create, 'name.db')125with mock.patch('os.environ', {'AWS_CLI_HISTORY_FILE': db_filename}):126attach_history_handler(127session=mock_session, parsed_args=parsed_args)128self.assertEqual(mock_recorder.add_handler.call_count, 1)129# Is should create any missing parent directories of the130# file as well.131self.assertTrue(os.path.exists(directory_to_create))132self.assertTrue(mock_db_sqlite3.connect.called)133134@mock.patch('awscli.customizations.history.sqlite3')135@mock.patch('awscli.customizations.history.db.sqlite3')136@mock.patch('awscli.customizations.history.HISTORY_RECORDER',137spec=HistoryRecorder)138def test_profile_does_not_exist(self, mock_recorder, mock_db_sqlite3,139mock_sqlite3):140mock_session = mock.Mock(Session)141mock_session.get_scoped_config.side_effect = ProfileNotFound(142profile='no-exist')143144parsed_args = argparse.Namespace()145parsed_args.command = 'configure'146147attach_history_handler(session=mock_session, parsed_args=parsed_args)148self.assertFalse(mock_recorder.add_handler.called)149self.assertFalse(mock_db_sqlite3.connect.called)150151152class TestAddHistoryCommand(unittest.TestCase):153def test_add_history_command(self):154command_table = {}155mock_session = mock.Mock(Session)156add_history_commands(157command_table=command_table, session=mock_session)158self.assertIn('history', command_table)159self.assertIsInstance(command_table['history'], HistoryCommand)160161162class TestHistoryCommand(unittest.TestCase):163def test_requires_subcommand(self):164mock_session = mock.Mock(Session)165history_command = HistoryCommand(mock_session)166parsed_args = argparse.Namespace()167parsed_args.subcommand = None168parsed_globals = argparse.Namespace()169with self.assertRaises(ValueError):170history_command._run_main(parsed_args, parsed_globals)171172173