File: test_output.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (142 lines) | stat: -rw-r--r-- 5,730 bytes parent folder | download
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
# Copyright 2020 Amazon.com, Inc. or its affiliates. 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://aws.amazon.com/apache2.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.
import json

from awscli.autocomplete import parser
from awscli.autoprompt.output import OutputGetter
from awscli.clidriver import create_clidriver
from awscli.testutils import unittest
from tests.unit.autocomplete import InMemoryIndex


class TestOutputGetter(unittest.TestCase):
    def setUp(self):
        index = InMemoryIndex(
            {
                'command_names': {
                    '': [('aws', None)],
                    'aws': [
                        ('s3api', 'Amazon Simple Storage Service'),
                        ('ec2', None),
                    ],
                    'aws.s3api': [('get-object', None)],
                    'aws.ec2': [('bundle-instance', None)],
                },
                'arg_names': {
                    '': {
                        'aws': ['query', 'output'],
                    },
                    'aws.s3api': {
                        'get-object': [],
                    },
                    'aws.ec2': {
                        'bundle-instance': [],
                    },
                },
                'arg_data': {
                    '': {
                        'aws': {
                            'query': (
                                'query',
                                'string',
                                'aws',
                                '',
                                None,
                                False,
                                False,
                            ),
                            'output': (
                                'output',
                                'string',
                                'aws',
                                '',
                                None,
                                False,
                                False,
                            ),
                        }
                    },
                    'aws.s3api': {'get-object': {}},
                    'aws.ec2': {
                        'bundle-instance': {},
                    },
                },
            }
        )
        self.parser = parser.CLIParser(index)
        self.driver = create_clidriver()
        self.driver.session.set_config_variable('output', 'json')
        self.base_output_getter = OutputGetter(self.driver)

    def test_get_output_content(self):
        parsed = self.parser.parse('aws s3api get-object')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('"Body":', content)

    def test_can_return_no_output(self):
        parsed = self.parser.parse('aws s3')
        content = self.base_output_getter.get_output(parsed)
        self.assertEqual('No output', content)

    def test_can_return_json(self):
        parsed = self.parser.parse('aws s3api get-object --output json ')
        content = json.loads(self.base_output_getter.get_output(parsed))
        self.assertIn('Body', content)

    def test_can_take_output_format_wo_trailing_space(self):
        parsed = self.parser.parse('aws s3api get-object --output table')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('------+------', content)

    def test_use_session_output_value_on_partial_input(self):
        parsed = self.parser.parse('aws s3api get-object --output yam')
        content = json.loads(self.base_output_getter.get_output(parsed))
        self.assertIn('Body', content)

    def test_can_use_session_output_format(self):
        parsed = self.parser.parse('aws s3api get-object')
        content = json.loads(self.base_output_getter.get_output(parsed))
        self.assertIn('Body', content)

    def test_can_return_error_message_on_invalid_output_format(self):
        parsed = self.parser.parse('aws s3api get-object --output poem ')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('Bad value for --output: poem', content)

    def test_can_return_error_message_on_invalid_query(self):
        parsed = self.parser.parse('aws s3api get-object --query A[}')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('Bad value for --query: A[}', content)

    def test_can_handle_open_brackets(self):
        parsed = self.parser.parse('aws s3api get-object --query Metadata.[')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('KeyName', content)

    def test_can_handle_trailing_dot(self):
        parsed = self.parser.parse('aws s3api get-object --query Metadata.')
        content = self.base_output_getter.get_output(parsed)
        self.assertIn('KeyName', content)

    def test_not_show_omap_in_yaml_output(self):
        parsed = self.parser.parse('aws s3api get-object --output yaml')
        content = self.base_output_getter.get_output(parsed)
        self.assertNotIn('omap', content)

    def test_yaml_output_can_parse_datetime(self):
        parsed = self.parser.parse(
            'aws ec2 bundle-instance --output yaml '
            '--query BundleTask.StartTime'
        )
        content = self.base_output_getter.get_output(parsed)
        self.assertEqual('"1970-01-01T00:00:00"\n', content)