/usr/share/pyshared/txaws/client/discover/tests/test_entry_point.py is in python-txaws 0.2.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2010 Jamu Kakar <jkakar@kakar.ca>
# Licenced under the txaws licence available at /LICENSE in the txaws source.
"""Unit tests for L{get_command}, L{parse_options} and L{main} functions."""
from cStringIO import StringIO
import os
import sys
from txaws.client.discover.entry_point import (
OptionError, UsageError, get_command, main, parse_options, USAGE_MESSAGE)
from txaws.testing.base import TXAWSTestCase
class ParseOptionsTestCase(TXAWSTestCase):
def test_parse_options(self):
"""
L{parse_options} returns a C{dict} contains options parsed from the
command-line.
"""
options = parse_options([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action",
"--something.else", "something.else"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action",
"something.else": "something.else"},
options)
def test_parse_options_without_options(self):
"""An L{OptionError} is raised if no options are provided."""
self.assertRaises(OptionError, parse_options, ["txaws-discover"])
def test_parse_options_with_missing_value(self):
"""
An L{OptionError} is raised if an option is specified without a value.
"""
self.assertRaises(OptionError, parse_options,
["txaws-discover", "--key"])
def test_parse_options_with_missing_option(self):
"""
An L{OptionError} is raised if a value is specified without an option
name.
"""
self.assertRaises(
OptionError, parse_options,
["txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action",
"random-value"])
def test_parse_options_without_required_arguments(self):
"""
An access key, access secret, endpoint and action can be specified as
command-line arguments. An L{OptionError} is raised if any one of
these is missing.
"""
self.assertRaises(OptionError, parse_options,
["txaws-discover", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertRaises(OptionError, parse_options,
["txaws-discover", "--key", "key",
"--endpoint", "endpoint", "--action", "action"])
self.assertRaises(OptionError, parse_options,
["txaws-discover", "--key", "key",
"--secret", "secret", "--action", "action"])
self.assertRaises(OptionError, parse_options,
["txaws-discover", "--key", "key",
"--secret", "secret", "--endpoint", "endpoint"])
def test_parse_options_gets_key_from_environment(self):
"""
If the C{AWS_ACCESS_KEY_ID} environment variable is present, it will
be used if the C{--key} command-line argument isn't specified.
"""
os.environ["AWS_ACCESS_KEY_ID"] = "key"
options = parse_options([
"txaws-discover", "--secret", "secret", "--endpoint", "endpoint",
"--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_prefers_explicit_key(self):
"""
If an explicit C{--key} command-line argument is specified it will be
preferred over the value specified in the C{AWS_ACCESS_KEY_ID}
environment variable.
"""
os.environ["AWS_ACCESS_KEY_ID"] = "fail"
options = parse_options([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_gets_secret_from_environment(self):
"""
If the C{AWS_SECRET_ACCESS_KEY} environment variable is present, it
will be used if the C{--secret} command-line argument isn't specified.
"""
os.environ["AWS_SECRET_ACCESS_KEY"] = "secret"
options = parse_options([
"txaws-discover", "--key", "key", "--endpoint", "endpoint",
"--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_prefers_explicit_secret(self):
"""
If an explicit C{--secret} command-line argument is specified it will
be preferred over the value specified in the C{AWS_SECRET_ACCESS_KEY}
environment variable.
"""
os.environ["AWS_SECRET_ACCESS_KEY"] = "fail"
options = parse_options([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_gets_endpoint_from_environment(self):
"""
If the C{AWS_ENDPOINT} environment variable is present, it will be
used if the C{--endpoint} command-line argument isn't specified.
"""
os.environ["AWS_ENDPOINT"] = "endpoint"
options = parse_options([
"txaws-discover", "--key", "key", "--secret", "secret",
"--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_prefers_explicit_endpoint(self):
"""
If an explicit C{--endpoint} command-line argument is specified it
will be preferred over the value specified in the C{AWS_ENDPOINT}
environment variable.
"""
os.environ["AWS_ENDPOINT"] = "fail"
options = parse_options([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertEqual({"key": "key", "secret": "secret",
"endpoint": "endpoint", "action": "action"},
options)
def test_parse_options_raises_usage_error_when_help_specified(self):
"""
L{UsageError} is raised if C{-h} or C{--help} appears in command-line
arguments.
"""
self.assertRaises(UsageError, parse_options,
["txaws-discover", "-h"])
self.assertRaises(UsageError, parse_options,
["txaws-discover", "--help"])
self.assertRaises(UsageError, parse_options,
["txaws-discover", "--key", "key",
"--secret", "secret", "--endpoint", "endpoint",
"--action", "action", "--help"])
class GetCommandTestCase(TXAWSTestCase):
def test_get_command_without_arguments(self):
"""An L{OptionError} is raised if no arguments are provided."""
self.assertRaises(OptionError, get_command, ["txaws-discover"])
def test_get_command(self):
"""
An access key, access secret, endpoint and action can be specified as
command-line arguments.
"""
command = get_command([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertEqual("key", command.key)
self.assertEqual("secret", command.secret)
self.assertEqual("endpoint", command.endpoint)
self.assertEqual("action", command.action)
self.assertIdentical(sys.stdout, command.output)
def test_get_command_with_custom_output_stream(self):
output = StringIO()
command = get_command([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"], output)
self.assertIdentical(output, command.output)
def test_get_command_without_required_arguments(self):
"""
An access key, access secret, endpoint and action can be specified as
command-line arguments. An L{OptionError} is raised if any one of
these is missing.
"""
self.assertRaises(OptionError, get_command,
["txaws-discover", "--secret", "secret",
"--endpoint", "endpoint", "--action", "action"])
self.assertRaises(OptionError, get_command,
["txaws-discover", "--key", "key",
"--endpoint", "endpoint", "--action", "action"])
self.assertRaises(OptionError, get_command,
["txaws-discover", "--key", "key",
"--secret", "secret", "--action", "action"])
self.assertRaises(OptionError, get_command,
["txaws-discover", "--key", "key",
"--secret", "secret", "--endpoint", "endpoint"])
def test_get_command_passes_additional_parameters_to_command(self):
"""
Command-line parameters beyond C{--key}, C{--secret}, C{--endpoint}
and C{--action} are passed to the L{Command} in a parameter C{dict}.
"""
command = get_command([
"txaws-discover", "--key", "key", "--secret", "secret",
"--endpoint", "endpoint", "--action", "DescribeRegions",
"--Region.Name.0", "us-west-1"])
self.assertEqual({"Region.Name.0": "us-west-1"}, command.parameters)
class MainTestCase(TXAWSTestCase):
def test_usage_message(self):
"""
If a L{UsageError} is raised, the help screen is written to the output
stream.
"""
output = StringIO()
main(["txaws-discover", "--help"], output, True)
self.assertEqual(USAGE_MESSAGE, output.getvalue())
def test_error_message(self):
"""
If an exception is raised, its message is written to the output
stream.
"""
output = StringIO()
main(["txaws-discover"], output, True)
self.assertEqual(
"ERROR: The '--key' command-line argument is required.\n",
output.getvalue())
|