Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/history/test_history.py
1569 views
1
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import argparse
14
import os
15
16
from botocore.session import Session
17
from botocore.history import HistoryRecorder
18
from botocore.exceptions import ProfileNotFound
19
20
from awscli.testutils import unittest, mock, FileCreator
21
from awscli.compat import StringIO
22
from awscli.customizations.history import attach_history_handler
23
from awscli.customizations.history import add_history_commands
24
from awscli.customizations.history import HistoryCommand
25
from awscli.customizations.history.db import DatabaseHistoryHandler
26
27
28
class TestAttachHistoryHandler(unittest.TestCase):
29
def setUp(self):
30
self.files = FileCreator()
31
32
def tearDown(self):
33
self.files.remove_all()
34
35
@mock.patch('awscli.customizations.history.sqlite3')
36
@mock.patch('awscli.customizations.history.db.sqlite3')
37
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
38
spec=HistoryRecorder)
39
def test_attach_history_handler(self, mock_recorder, mock_db_sqlite3, mock_sqlite3):
40
mock_session = mock.Mock(Session)
41
mock_session.get_scoped_config.return_value = {
42
'cli_history': 'enabled'
43
}
44
45
parsed_args = argparse.Namespace()
46
parsed_args.command = 's3'
47
48
attach_history_handler(session=mock_session, parsed_args=parsed_args)
49
self.assertEqual(mock_recorder.add_handler.call_count, 1)
50
self.assertIsInstance(
51
mock_recorder.add_handler.call_args[0][0], DatabaseHistoryHandler)
52
self.assertTrue(mock_db_sqlite3.connect.called)
53
54
@mock.patch('awscli.customizations.history.sqlite3')
55
@mock.patch('awscli.customizations.history.db.sqlite3')
56
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
57
spec=HistoryRecorder)
58
def test_no_attach_history_handler_when_history_not_configured(
59
self, mock_recorder, mock_db_sqlite3, mock_sqlite3):
60
mock_session = mock.Mock(Session)
61
mock_session.get_scoped_config.return_value = {}
62
63
parsed_args = argparse.Namespace()
64
parsed_args.command = 's3'
65
66
attach_history_handler(session=mock_session, parsed_args=parsed_args)
67
self.assertFalse(mock_recorder.add_handler.called)
68
self.assertFalse(mock_db_sqlite3.connect.called)
69
70
@mock.patch('awscli.customizations.history.sqlite3')
71
@mock.patch('awscli.customizations.history.db.sqlite3')
72
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
73
spec=HistoryRecorder)
74
def test_no_attach_history_handler_when_command_is_history(
75
self, mock_recorder, mock_db_sqlite3, mock_sqlite3):
76
mock_session = mock.Mock(Session)
77
mock_session.get_scoped_config.return_value = {
78
'cli_history': 'enabled'
79
}
80
81
parsed_args = argparse.Namespace()
82
parsed_args.command = 'history'
83
84
attach_history_handler(session=mock_session, parsed_args=parsed_args)
85
self.assertFalse(mock_recorder.add_handler.called)
86
self.assertFalse(mock_db_sqlite3.connect.called)
87
88
@mock.patch('awscli.customizations.history.sqlite3', None)
89
@mock.patch('awscli.customizations.history.db.sqlite3')
90
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
91
spec=HistoryRecorder)
92
def test_no_attach_history_handler_when_no_sqlite3(
93
self, mock_recorder, mock_sqlite3):
94
mock_session = mock.Mock(Session)
95
mock_session.get_scoped_config.return_value = {
96
'cli_history': 'enabled'
97
}
98
99
parsed_args = argparse.Namespace()
100
parsed_args.command = 's3'
101
102
with mock.patch('sys.stderr', StringIO()) as mock_stderr:
103
attach_history_handler(
104
session=mock_session, parsed_args=parsed_args)
105
self.assertIn(
106
'enabled but sqlite3 is unavailable', mock_stderr.getvalue())
107
self.assertFalse(mock_recorder.add_handler.called)
108
self.assertFalse(mock_sqlite3.connect.called)
109
110
@mock.patch('awscli.customizations.history.sqlite3')
111
@mock.patch('awscli.customizations.history.db.sqlite3')
112
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
113
spec=HistoryRecorder)
114
def test_create_directory_no_exists(self, mock_recorder,
115
mock_db_sqlite3, mock_sqlite3):
116
mock_session = mock.Mock(Session)
117
mock_session.get_scoped_config.return_value = {
118
'cli_history': 'enabled'
119
}
120
121
parsed_args = argparse.Namespace()
122
parsed_args.command = 's3'
123
124
directory_to_create = os.path.join(self.files.rootdir, 'create-dir')
125
db_filename = os.path.join(directory_to_create, 'name.db')
126
with mock.patch('os.environ', {'AWS_CLI_HISTORY_FILE': db_filename}):
127
attach_history_handler(
128
session=mock_session, parsed_args=parsed_args)
129
self.assertEqual(mock_recorder.add_handler.call_count, 1)
130
# Is should create any missing parent directories of the
131
# file as well.
132
self.assertTrue(os.path.exists(directory_to_create))
133
self.assertTrue(mock_db_sqlite3.connect.called)
134
135
@mock.patch('awscli.customizations.history.sqlite3')
136
@mock.patch('awscli.customizations.history.db.sqlite3')
137
@mock.patch('awscli.customizations.history.HISTORY_RECORDER',
138
spec=HistoryRecorder)
139
def test_profile_does_not_exist(self, mock_recorder, mock_db_sqlite3,
140
mock_sqlite3):
141
mock_session = mock.Mock(Session)
142
mock_session.get_scoped_config.side_effect = ProfileNotFound(
143
profile='no-exist')
144
145
parsed_args = argparse.Namespace()
146
parsed_args.command = 'configure'
147
148
attach_history_handler(session=mock_session, parsed_args=parsed_args)
149
self.assertFalse(mock_recorder.add_handler.called)
150
self.assertFalse(mock_db_sqlite3.connect.called)
151
152
153
class TestAddHistoryCommand(unittest.TestCase):
154
def test_add_history_command(self):
155
command_table = {}
156
mock_session = mock.Mock(Session)
157
add_history_commands(
158
command_table=command_table, session=mock_session)
159
self.assertIn('history', command_table)
160
self.assertIsInstance(command_table['history'], HistoryCommand)
161
162
163
class TestHistoryCommand(unittest.TestCase):
164
def test_requires_subcommand(self):
165
mock_session = mock.Mock(Session)
166
history_command = HistoryCommand(mock_session)
167
parsed_args = argparse.Namespace()
168
parsed_args.subcommand = None
169
parsed_globals = argparse.Namespace()
170
with self.assertRaises(ValueError):
171
history_command._run_main(parsed_args, parsed_globals)
172
173