/
test_completer.py
311 lines (281 loc) · 12.8 KB
/
test_completer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
# Copyright 2015 Donne Martin. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import unicode_literals
from __future__ import print_function
import unittest
import mock
import re
from prompt_toolkit.document import Document
from awscli import completer as awscli_completer
from saws.completer import AwsCompleter
from saws.commands import AwsCommands
from saws.saws import Saws
class CompleterTest(unittest.TestCase):
@mock.patch('saws.resources.print')
def setUp(self, mock_print):
self.saws = Saws(refresh_resources=False)
self.completer = self.create_completer()
self.completer.resources._set_resources_path(
'data/RESOURCES_SAMPLE.txt')
self.completer.refresh_resources_and_options()
self.completer_event = self.create_completer_event()
mock_print.assert_called_with('Loaded resources from cache')
def create_completer(self):
# TODO: Fix duplicate creation of AwsCompleter, which is already
# created by Saws init
self.aws_commands = AwsCommands()
self.all_commands = self.aws_commands.all_commands
self.commands, self.sub_commands, self.global_options, \
self.resource_options = self.all_commands
return AwsCompleter(awscli_completer,
self.all_commands,
self.saws.config,
self.saws.config_obj,
self.saws.logger)
def create_completer_event(self):
return mock.Mock()
def _get_completions(self, command):
position = len(command)
result = set(self.completer.get_completions(
Document(text=command, cursor_position=position),
self.completer_event))
return result
def verify_completions(self, commands, expected):
result = set()
for command in commands:
# Call the AWS CLI autocompleter
result.update(self._get_completions(command))
result_texts = []
for item in result:
# Each result item is a Completion object,
# we are only interested in the text portion
result_texts.append(item.text)
assert result_texts
if len(expected) == 1:
assert expected[0] in result_texts
else:
for item in expected:
assert item in result_texts
def test_no_completions(self):
command = 'aws ec2'
expected = set([])
assert expected == self._get_completions(command)
command = 'aws elb'
assert expected == self._get_completions(command)
command = 'aws elasticache'
assert expected == self._get_completions(command)
def test_ec2_commands(self):
commands = ['aws e']
expected = ['ec2',
'ecs',
'efs',
'elasticache',
'elasticbeanstalk',
'elastictranscoder',
'elb',
'emr']
self.verify_completions(commands, expected)
def test_ec2_subcommands(self):
commands = ['aws ec2 c']
expected = ['cancel-bundle-task',
'cancel-conversion-task',
'cancel-export-task',
'cancel-import-task',
'cancel-reserved-instances-listing',
'cancel-spot-fleet-requests',
'cancel-spot-instance-requests']
self.verify_completions(commands, expected)
def test_aws_command(self):
commands = ['a', 'aw']
expected = [AwsCommands.AWS_COMMAND]
self.verify_completions(commands, expected)
def test_global_options(self):
commands = ['aws -', 'aws --']
expected = self.saws \
.all_commands[AwsCommands.CommandType.GLOBAL_OPTIONS.value]
self.verify_completions(commands, expected)
def test_resource_options(self):
commands = ['aws ec2 describe-instances --',
'aws s3api get-bucket-acl --',
'aws emr list-clusters --']
expected = self.saws \
.all_commands[AwsCommands.CommandType.RESOURCE_OPTIONS.value]
self.verify_completions(commands, expected)
def test_shortcuts(self):
commands = ['aws ec2 ls',
'aws emr ls',
'aws elb ls',
'aws dynamodb ls']
expected = ['aws ec2 describe-instances',
'aws emr list-clusters',
'aws elb describe-load-balancers',
'aws dynamodb list-tables']
shortcuts = dict(zip(commands, expected))
for command, expect in shortcuts.items():
result = self.completer.replace_shortcut(command)
assert result == expect
def test_shortcuts_fuzzy(self):
self.completer.fuzzy_match = True
self.completer.shortcut_match = True
commands = ['aws ec2ls']
expected = ['ec2 ls --instance-ids']
self.verify_completions(commands, expected)
commands = ['aws ec2start']
expected = ['ec2 start-instances --instance-ids']
self.verify_completions(commands, expected)
commands = ['aws ec2stop']
expected = ['ec2 stop-instances --instance-ids']
self.verify_completions(commands, expected)
commands = ['aws ec2tagk']
expected = ['ec2 ls --ec2-tag-key']
self.verify_completions(commands, expected)
commands = ['aws ec2tagv']
expected = ['ec2 ls --ec2-tag-value']
self.verify_completions(commands, expected)
def test_substitutions(self):
command = 'aws ec2 ls --filters "Name=tag-key,Values=%s prod"'
expected = 'aws ec2 ls --filters "Name=tag-key,Values=prod"'
result = self.completer.replace_substitution(command)
assert result == expected
command = 'aws ec2 ls --ec2-tag-key Stack'
expected = \
'aws ec2 describe-instances --filters "Name=tag-key,Values=Stack"'
result = self.completer.replace_shortcut(command)
assert result == expected
command = 'aws ec2 ls --ec2-tag-value prod'
expected = \
'aws ec2 describe-instances --filters "Name=tag-value,Values=prod"'
result = self.completer.replace_shortcut(command)
assert result == expected
def test_substitutions_with_more_tokens(self):
command = 'aws ec2 ls --filters "Name=tag-key,Values=%s prod" ' \
'| grep IpAddress'
expected = 'aws ec2 ls --filters "Name=tag-key,Values=prod" ' \
'| grep IpAddress'
result = self.completer.replace_substitution(command)
assert result == expected
command = 'aws ec2 ls --ec2-tag-key Stack | grep IpAddress'
expected = 'aws ec2 describe-instances --filters ' \
'"Name=tag-key,Values=Stack" | grep IpAddress'
result = self.completer.replace_shortcut(command)
assert result == expected
command = 'aws ec2 ls --ec2-tag-value prod | grep IpAddress'
expected = 'aws ec2 describe-instances --filters ' \
'"Name=tag-value,Values=prod" | grep IpAddress'
result = self.completer.replace_shortcut(command)
assert result == expected
@mock.patch('saws.resources.print')
def test_refresh_resources_and_options(self, mock_print):
self.completer.refresh_resources_and_options(force_refresh=False)
mock_print.assert_called_with('Loaded resources from cache')
def test_instance_ids(self):
commands = ['aws ec2 ls --instance-ids i-b']
expected = ['i-b875ecc3', 'i-b51d05f4', 'i-b3628153']
instance_ids = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.INSTANCE_IDS.value]
instance_ids.resources.extend(expected)
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --instance-ids i-a']
expected = ['i-a51d05f4', 'i-a71bd617', 'i-a1637b56']
instance_ids.resources.extend(expected)
self.verify_completions(commands, expected)
def test_instance_ids_fuzzy(self):
self.completer.fuzzy_match = True
commands = ['aws ec2 ls --instance-ids a5']
expected = ['i-a875ecc3', 'i-a41d55f4', 'i-a3628153']
instance_ids = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.INSTANCE_IDS.value]
instance_ids.resources.extend(expected)
self.verify_completions(commands, expected)
def test_instance_keys(self):
commands = ['aws ec2 ls --ec2-tag-key na']
expected = ['name', 'namE']
instance_tag_keys = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.INSTANCE_TAG_KEYS.value]
instance_tag_keys.resources.extend(expected)
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-tag-key Sta']
expected = ['Stack']
instance_tag_keys.resources.extend(expected)
self.verify_completions(commands, expected)
def test_instance_tag_values(self):
commands = ['aws ec2 ls --ec2-tag-value prod']
expected = ['production', 'production-blue', 'production-green']
instance_tag_values = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.INSTANCE_TAG_VALUES.value]
instance_tag_values.resources.extend(expected)
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-tag-value test']
expected = ['testing']
instance_tag_values.resources.extend(expected)
self.verify_completions(commands, expected)
def test_bucket_names(self):
commands = ['aws s3pi get-bucket-acl --bucket web-']
expected = ['web-server-logs', 'web-server-images']
bucket_names = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.BUCKET_NAMES.value]
for bucket_name in expected:
bucket_names.add_bucket_name(bucket_name)
self.verify_completions(commands, expected)
def test_s3_uri(self):
commands = ['aws s3 ls s3:']
expected = ['s3://web-server-logs', 's3://web-server-images']
bucket_uris = self.completer.resources.resource_lists[
self.completer.resources.ResourceType.BUCKET_URIS.value]
for s3_uri in expected:
bucket_name = re.sub('s3://', '', s3_uri)
bucket_uris.add_bucket_name(bucket_name)
self.verify_completions(commands, expected)
commands = ['aws s3 ls s3://web']
self.verify_completions(commands, expected)
def test_ec2_states(self):
commands = ['aws ec2 ls --ec2-state pend']
expected = ['pending']
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-state run']
expected = ['running']
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-state shut']
expected = ['shutting-down']
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-state term']
expected = ['terminated']
self.verify_completions(commands, expected)
commands = ['aws ec2 ls --ec2-state stop']
expected = ['stopping',
'stopped']
self.verify_completions(commands, expected)
def test_cluster_states(self):
self.verify_cluster_states()
def test_cluster_states_fuzzy(self):
self.completer.fuzzy_match = True
self.verify_cluster_states()
def verify_cluster_states(self):
commands = ['aws emr ls --cluster-states star']
expected = ['STARTING']
self.verify_completions(commands, expected)
commands = ['emr ls --cluster-states BOOT']
expected = ['BOOTSTRAPPING']
self.verify_completions(commands, expected)
commands = ['emr ls --cluster-states run']
expected = ['RUNNING']
self.verify_completions(commands, expected)
commands = ['emr ls --cluster-states WAIT']
expected = ['WAITING']
self.verify_completions(commands, expected)
commands = ['emr ls --cluster-states term']
expected = ['TERMINATING',
'TERMINATED',
'TERMINATED_WITH_ERRORS']
self.verify_completions(commands, expected)