Skip to content

Commit def654a

Browse files
committed
Give better assertion errors when CLI commands fail in tests (softlayer#725)
1 parent 9d55c6c commit def654a

17 files changed

+100
-93
lines changed

SoftLayer/testing/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ def assert_called_with(self, service, method, **props):
146146
raise AssertionError('%s::%s was not called with given properties: %s'
147147
% (service, method, props))
148148

149+
def assert_no_fail(self, result):
150+
"""Fail when a failing click result has an error"""
151+
if result.exception:
152+
raise result.exception
153+
154+
self.assertEqual(result.exit_code, 0)
155+
149156
def set_mock(self, service, method):
150157
"""Set and return mock on the current client."""
151158
return self.mocks.set_mock(service, method)

tests/CLI/core_tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ def test_verbose_max(self):
3131
with mock.patch('logging.getLogger') as log_mock:
3232
result = self.run_command(['-vvv', 'vs', 'list'])
3333

34-
self.assertEqual(result.exit_code, 0)
34+
self.assert_no_fail(result)
3535
log_mock().addHandler.assert_called_with(mock.ANY)
3636
log_mock().setLevel.assert_called_with(logging.DEBUG)
3737

3838
def test_build_client(self):
3939
env = environment.Environment()
4040
result = self.run_command(['vs', 'list'], env=env)
4141

42-
self.assertEqual(result.exit_code, 0)
42+
self.assert_no_fail(result)
4343
self.assertIsNotNone(env.client)
4444

4545
def test_diagnostics(self):
4646
result = self.run_command(['-v', 'vs', 'list'])
4747

48-
self.assertEqual(result.exit_code, 0)
48+
self.assert_no_fail(result)
4949
self.assertIn('SoftLayer_Account::getVirtualGuests', result.output)
5050
self.assertIn('"execution_time"', result.output)
5151
self.assertIn('"api_calls"', result.output)

tests/CLI/modules/block_tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BlockTests(testing.TestCase):
1414
def test_access_list(self):
1515
result = self.run_command(['block', 'access-list', '1234'])
1616

17-
self.assertEqual(result.exit_code, 0)
17+
self.assert_no_fail(result)
1818
self.assertEqual([
1919
{
2020
'username': 'joe',
@@ -58,15 +58,15 @@ def test_volume_cancel(self):
5858
result = self.run_command([
5959
'--really', 'block', 'volume-cancel', '1234'])
6060

61-
self.assertEqual(result.exit_code, 0)
61+
self.assert_no_fail(result)
6262
self.assertEqual("", result.output)
6363
self.assert_called_with('SoftLayer_Billing_Item', 'cancelItem',
6464
args=(False, True, None))
6565

6666
def test_volume_detail(self):
6767
result = self.run_command(['block', 'volume-detail', '1234'])
6868

69-
self.assertEqual(result.exit_code, 0)
69+
self.assert_no_fail(result)
7070
self.assertEqual({
7171
'Username': 'username',
7272
'LUN Id': '2',
@@ -84,7 +84,7 @@ def test_volume_detail(self):
8484
def test_volume_list(self):
8585
result = self.run_command(['block', 'volume-list'])
8686

87-
self.assertEqual(result.exit_code, 0)
87+
self.assert_no_fail(result)
8888
self.assertEqual([
8989
{
9090
'bytes_used': None,

tests/CLI/modules/call_api_tests.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_python_output(self):
4242
'-f nested.property=5432',
4343
'--output-python'])
4444

45-
self.assertEqual(result.exit_code, 0)
45+
self.assert_no_fail(result)
4646
# NOTE(kmcdonald): Python 3 no longer inserts 'u' before unicode
4747
# string literals but python 2 does. These are stripped out to make
4848
# this test pass on both python versions.
@@ -72,7 +72,7 @@ def test_options(self):
7272
'-f property=1234',
7373
'-f nested.property=5432'])
7474

75-
self.assertEqual(result.exit_code, 0)
75+
self.assert_no_fail(result)
7676
self.assertEqual(json.loads(result.output), 'test')
7777
self.assert_called_with('SoftLayer_Service', 'method',
7878
mask='mask[some.mask]',
@@ -94,7 +94,7 @@ def test_object(self):
9494

9595
result = self.run_command(['call-api', 'Service', 'method'])
9696

97-
self.assertEqual(result.exit_code, 0)
97+
self.assert_no_fail(result)
9898
self.assertEqual(json.loads(result.output),
9999
{'string': 'string',
100100
'int': 10,
@@ -113,7 +113,7 @@ def test_object_table(self):
113113
result = self.run_command(['call-api', 'Service', 'method'],
114114
fmt='table')
115115

116-
self.assertEqual(result.exit_code, 0)
116+
self.assert_no_fail(result)
117117
# NOTE(kmcdonald): Order is not guaranteed
118118
self.assertIn(":........:........:", result.output)
119119
self.assertIn(": name : value :", result.output)
@@ -130,7 +130,7 @@ def test_object_nested(self):
130130

131131
result = self.run_command(['call-api', 'Service', 'method'])
132132

133-
self.assertEqual(result.exit_code, 0)
133+
self.assert_no_fail(result)
134134
self.assertEqual(json.loads(result.output),
135135
{'this': {'is': [{'pretty': 'nested'}]}})
136136

@@ -144,7 +144,7 @@ def test_list(self):
144144

145145
result = self.run_command(['call-api', 'Service', 'method'])
146146

147-
self.assertEqual(result.exit_code, 0)
147+
self.assert_no_fail(result)
148148
self.assertEqual(json.loads(result.output),
149149
[{'string': 'string',
150150
'int': 10,
@@ -163,7 +163,7 @@ def test_list_table(self):
163163
result = self.run_command(['call-api', 'Service', 'method'],
164164
fmt='table')
165165

166-
self.assertEqual(result.exit_code, 0)
166+
self.assert_no_fail(result)
167167
self.assertEqual(result.output,
168168
""":......:......:.......:.....:........:
169169
: Bool : None : float : int : string :
@@ -179,6 +179,6 @@ def test_parameters(self):
179179
result = self.run_command(['call-api', 'Service', 'method',
180180
'arg1', '1234'])
181181

182-
self.assertEqual(result.exit_code, 0)
182+
self.assert_no_fail(result)
183183
self.assert_called_with('SoftLayer_Service', 'method',
184184
args=('arg1', '1234'))

tests/CLI/modules/cdn_tests.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CdnTests(testing.TestCase):
1414
def test_list_accounts(self):
1515
result = self.run_command(['cdn', 'list'])
1616

17-
self.assertEqual(result.exit_code, 0)
17+
self.assert_no_fail(result)
1818
self.assertEqual(json.loads(result.output),
1919
[{'notes': None,
2020
'created': '2012-06-25T14:05:28-07:00',
@@ -30,7 +30,7 @@ def test_list_accounts(self):
3030
def test_detail_account(self):
3131
result = self.run_command(['cdn', 'detail', '1245'])
3232

33-
self.assertEqual(result.exit_code, 0)
33+
self.assert_no_fail(result)
3434
self.assertEqual(json.loads(result.output),
3535
{'notes': None,
3636
'created': '2012-06-25T14:05:28-07:00',
@@ -43,20 +43,20 @@ def test_load_content(self):
4343
result = self.run_command(['cdn', 'load', '1234',
4444
'http://example.com'])
4545

46-
self.assertEqual(result.exit_code, 0)
46+
self.assert_no_fail(result)
4747
self.assertEqual(result.output, "")
4848

4949
def test_purge_content(self):
5050
result = self.run_command(['cdn', 'purge', '1234',
5151
'http://example.com'])
5252

53-
self.assertEqual(result.exit_code, 0)
53+
self.assert_no_fail(result)
5454
self.assertEqual(result.output, "")
5555

5656
def test_list_origins(self):
5757
result = self.run_command(['cdn', 'origin-list', '1234'])
5858

59-
self.assertEqual(result.exit_code, 0)
59+
self.assert_no_fail(result)
6060
self.assertEqual(json.loads(result.output), [
6161
{'media_type': 'FLASH',
6262
'origin_url': 'http://ams01.objectstorage.softlayer.net:80',
@@ -71,12 +71,12 @@ def test_add_origin(self):
7171
result = self.run_command(['cdn', 'origin-add', '1234',
7272
'http://example.com'])
7373

74-
self.assertEqual(result.exit_code, 0)
74+
self.assert_no_fail(result)
7575
self.assertEqual(result.output, "")
7676

7777
def test_remove_origin(self):
7878
result = self.run_command(['cdn', 'origin-remove', '1234',
7979
'http://example.com'])
8080

81-
self.assertEqual(result.exit_code, 0)
81+
self.assert_no_fail(result)
8282
self.assertEqual(result.output, "")

tests/CLI/modules/config_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def set_up(self):
3131
def test_show(self):
3232
result = self.run_command(['config', 'show'])
3333

34-
self.assertEqual(result.exit_code, 0)
34+
self.assert_no_fail(result)
3535
self.assertEqual(json.loads(result.output),
3636
{'Username': 'username',
3737
'API Key': 'api-key',
@@ -62,7 +62,7 @@ def test_setup(self, input, getpass, confirm_mock):
6262
result = self.run_command(['--config=%s' % config_file.name,
6363
'config', 'setup'])
6464

65-
self.assertEqual(result.exit_code, 0)
65+
self.assert_no_fail(result)
6666
self.assertTrue('Configuration Updated Successfully'
6767
in result.output)
6868
contents = config_file.read().decode("utf-8")

tests/CLI/modules/dns_tests.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ class DnsTests(testing.TestCase):
1919
def test_zone_print(self):
2020
result = self.run_command(['dns', 'zone-print', '1234'])
2121

22-
self.assertEqual(result.exit_code, 0)
22+
self.assert_no_fail(result)
2323
self.assertEqual(json.loads(result.output), "lots of text")
2424

2525
def test_create_zone(self):
2626
result = self.run_command(['dns', 'zone-create', 'example.com'])
2727

28-
self.assertEqual(result.exit_code, 0)
28+
self.assert_no_fail(result)
2929
self.assertEqual(result.output, "")
3030

3131
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
3232
def test_delete_zone(self, no_going_back_mock):
3333
no_going_back_mock.return_value = True
3434
result = self.run_command(['dns', 'zone-delete', '1234'])
3535

36-
self.assertEqual(result.exit_code, 0)
36+
self.assert_no_fail(result)
3737
self.assertEqual(result.output, "")
3838

3939
no_going_back_mock.return_value = False
4040
result = self.run_command(['--really', 'dns', 'zone-delete', '1234'])
4141

42-
self.assertEqual(result.exit_code, 0)
42+
self.assert_no_fail(result)
4343
self.assertEqual(result.output, "")
4444

4545
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
@@ -53,7 +53,7 @@ def test_delete_zone_abort(self, no_going_back_mock):
5353
def test_list_zones(self):
5454
result = self.run_command(['dns', 'zone-list'])
5555

56-
self.assertEqual(result.exit_code, 0)
56+
self.assert_no_fail(result)
5757
self.assertEqual(json.loads(result.output),
5858
[{'serial': 2014030728,
5959
'updated': '2014-03-07T13:52:31-06:00',
@@ -63,7 +63,7 @@ def test_list_zones(self):
6363
def test_list_records(self):
6464
result = self.run_command(['dns', 'record-list', '1234'])
6565

66-
self.assertEqual(result.exit_code, 0)
66+
self.assert_no_fail(result)
6767
self.assertEqual(json.loads(result.output)[0],
6868
{'record': 'a',
6969
'type': 'CNAME',
@@ -75,15 +75,15 @@ def test_add_record(self):
7575
result = self.run_command(['dns', 'record-add', '1234', 'hostname',
7676
'A', 'd', '--ttl=100'])
7777

78-
self.assertEqual(result.exit_code, 0)
78+
self.assert_no_fail(result)
7979
self.assertEqual(result.output, "")
8080

8181
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
8282
def test_delete_record(self, no_going_back_mock):
8383
no_going_back_mock.return_value = True
8484
result = self.run_command(['dns', 'record-remove', '1234'])
8585

86-
self.assertEqual(result.exit_code, 0)
86+
self.assert_no_fail(result)
8787
self.assertEqual(result.output, "")
8888

8989
@mock.patch('SoftLayer.CLI.formatting.no_going_back')

tests/CLI/modules/firewall_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class FirewallTests(testing.TestCase):
1414
def test_list_firewalls(self):
1515
result = self.run_command(['firewall', 'list'])
1616

17-
self.assertEqual(result.exit_code, 0)
17+
self.assert_no_fail(result)
1818
self.assertEqual(json.loads(result.output),
1919
[{'type': 'VLAN - dedicated',
2020
'server/vlan id': 1,

tests/CLI/modules/globalip_tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ class DnsTests(testing.TestCase):
1717
def test_ip_assign(self):
1818
result = self.run_command(['globalip', 'assign', '1', '127.0.0.1'])
1919

20-
self.assertEqual(result.exit_code, 0)
20+
self.assert_no_fail(result)
2121
self.assertEqual(result.output, "")
2222

2323
@mock.patch('SoftLayer.CLI.formatting.no_going_back')
2424
def test_ip_cancel(self, no_going_back_mock):
2525
# Test using --really flag
2626
result = self.run_command(['--really', 'globalip', 'cancel', '1'])
2727

28-
self.assertEqual(result.exit_code, 0)
28+
self.assert_no_fail(result)
2929
self.assertEqual(result.output, "")
3030

3131
# Test with confirmation
3232
no_going_back_mock.return_value = True
3333
result = self.run_command(['globalip', 'cancel', '1'])
3434

35-
self.assertEqual(result.exit_code, 0)
35+
self.assert_no_fail(result)
3636
self.assertEqual(result.output, "")
3737

3838
# Test with confirmation and responding negatively
@@ -45,7 +45,7 @@ def test_ip_cancel(self, no_going_back_mock):
4545
def test_ip_list(self):
4646
result = self.run_command(['globalip', 'list', '--ip-version=v4'])
4747

48-
self.assertEqual(result.exit_code, 0)
48+
self.assert_no_fail(result)
4949
self.assertEqual(json.loads(result.output),
5050
[{'assigned': 'Yes',
5151
'id': '200',

tests/CLI/modules/nas_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RWhoisTests(testing.TestCase):
1313
def test_list_nas(self):
1414
result = self.run_command(['nas', 'list'])
1515

16-
self.assertEqual(result.exit_code, 0)
16+
self.assert_no_fail(result)
1717
self.assertEqual(json.loads(result.output),
1818
[{'datacenter': 'Dallas',
1919
'server': '127.0.0.1',

0 commit comments

Comments
 (0)