Skip to content

Commit 861ffab

Browse files
unit tests
1 parent 859a7f1 commit 861ffab

File tree

6 files changed

+174
-49
lines changed

6 files changed

+174
-49
lines changed

SoftLayer/CLI/hardware/bandwidth.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from SoftLayer.CLI import environment
88
from SoftLayer.CLI import formatting
99
from SoftLayer.CLI import helpers
10+
from SoftLayer.CLI.virt.bandwidth import create_bandwidth_table
1011
from SoftLayer import utils
1112

1213

@@ -35,49 +36,8 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
3536
hardware_id = helpers.resolve_id(hardware.resolve_ids, identifier, 'hardware')
3637
data = hardware.get_bandwidth_data(hardware_id, start_date, end_date, None, summary_period)
3738

38-
formatted_data = {}
39-
for point in data:
40-
key = utils.clean_time(point['dateTime'])
41-
data_type = point['type']
42-
value = round(point['counter'] / 2 ** 30, 4)
43-
if formatted_data.get(key) is None:
44-
formatted_data[key] = {}
45-
formatted_data[key][data_type] = value
46-
47-
table = formatting.Table(['Date', 'Pub In', 'Pub Out', 'Pri In', 'Pri Out'],
48-
title="Bandwidth Report: %s - %s" % (start_date, end_date))
49-
50-
sum_table = formatting.Table(['Type', 'Sum GB', 'Average MBps', 'Max GB', 'Max Date'], title="Summary")
51-
52-
bw_totals = [
53-
{'keyName': 'publicIn_net_octet', 'sum': 0, 'max': 0, 'name': 'Pub In'},
54-
{'keyName': 'publicOut_net_octet', 'sum': 0, 'max': 0, 'name': 'Pub Out'},
55-
{'keyName': 'privateIn_net_octet', 'sum': 0, 'max': 0, 'name': 'Pri In'},
56-
{'keyName': 'privateOut_net_octet', 'sum': 0, 'max': 0, 'name': 'Pri Out'},
57-
]
58-
for point in formatted_data:
59-
new_row = [point]
60-
for bw_type in bw_totals:
61-
counter = formatted_data[point].get(bw_type['keyName'], 0)
62-
new_row.append(mb_to_gb(counter))
63-
bw_type['sum'] = bw_type['sum'] + counter
64-
if counter > bw_type['max']:
65-
bw_type['max'] = counter
66-
bw_type['maxDate'] = point
67-
table.add_row(new_row)
68-
69-
for bw_type in bw_totals:
70-
total = bw_type.get('sum', 0)
71-
average = 0
72-
if total > 0:
73-
average = round(total / len(formatted_data) / summary_period, 4)
74-
sum_table.add_row([
75-
bw_type.get('name'),
76-
mb_to_gb(total),
77-
average,
78-
mb_to_gb(bw_type.get('max')),
79-
bw_type.get('maxDate')
80-
])
39+
title = "Bandwidth Report: %s - %s" % (start_date, end_date)
40+
table, sum_table = create_bandwidth_table(data, summary_period, title)
8141

8242
env.fout(sum_table)
8343
if not quite_summary:

SoftLayer/CLI/virt/bandwidth.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
3535
vsi_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
3636
data = vsi.get_bandwidth_data(vsi_id, start_date, end_date, None, summary_period)
3737

38+
title = "Bandwidth Report: %s - %s" % (start_date, end_date)
39+
table, sum_table = create_bandwidth_table(data, summary_period, title)
40+
41+
42+
env.fout(sum_table)
43+
if not quite_summary:
44+
env.fout(table)
45+
46+
def create_bandwidth_table(data, summary_period, title="Bandwidth Report"):
47+
"""Create 2 tables, bandwidth and sumamry. Used here and in hw bandwidth command"""
48+
3849
formatted_data = {}
3950
for point in data:
4051
key = utils.clean_time(point['dateTime'])
@@ -45,11 +56,11 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
4556
formatted_data[key] = {}
4657
formatted_data[key][data_type] = value
4758

48-
table = formatting.Table(['Date', 'Pub In', 'Pub Out', 'Pri In', 'Pri Out'],
49-
title="Bandwidth Report: %s - %s" % (start_date, end_date))
59+
table = formatting.Table(['Date', 'Pub In', 'Pub Out', 'Pri In', 'Pri Out'], title=title)
5060

5161
sum_table = formatting.Table(['Type', 'Sum GB', 'Average MBps', 'Max GB', 'Max Date'], title="Summary")
5262

63+
# Required to specify keyName because getBandwidthTotals returns other counter types for some reason.
5364
bw_totals = [
5465
{'keyName': 'publicIn_net_octet', 'sum': 0, 'max': 0, 'name': 'Pub In'},
5566
{'keyName': 'publicOut_net_octet', 'sum': 0, 'max': 0, 'name': 'Pub Out'},
@@ -81,10 +92,7 @@ def cli(env, identifier, start_date, end_date, summary_period, quite_summary):
8192
bw_type.get('maxDate')
8293
])
8394

84-
env.fout(sum_table)
85-
if not quite_summary:
86-
env.fout(table)
87-
95+
return table, sum_table
8896

8997
def mb_to_gb(mbytes):
9098
"""Converts a MegaByte int to GigaByte. mbytes/2^10"""

SoftLayer/fixtures/SoftLayer_Hardware_Server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@
147147
}
148148
}
149149
]
150+
151+
getMetricTrackingObjectId = 1000

SoftLayer/fixtures/SoftLayer_Metric_Tracking_Object.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,48 @@
1010
"type": "cpu0"
1111
},
1212
]
13+
14+
15+
# Using counter > 32bit int causes unit tests to fail.
16+
getBandwidthData =[
17+
{
18+
'counter': 37.21,
19+
'dateTime': '2019-05-20T23:00:00-06:00',
20+
'type': 'cpu0'
21+
},
22+
{
23+
'counter': 76.12,
24+
'dateTime': '2019-05-20T23:00:00-06:00',
25+
'type': 'cpu1'
26+
},
27+
{
28+
'counter': 257623973,
29+
'dateTime': '2019-05-20T23:00:00-06:00',
30+
'type': 'memory'
31+
},
32+
{
33+
'counter': 137118503,
34+
'dateTime': '2019-05-20T23:00:00-06:00',
35+
'type': 'memory_usage'
36+
},
37+
{
38+
'counter': 125888818,
39+
'dateTime': '2019-05-20T23:00:00-06:00',
40+
'type': 'privateIn_net_octet'
41+
},
42+
{
43+
'counter': 961037,
44+
'dateTime': '2019-05-20T23:00:00-06:00',
45+
'type': 'privateOut_net_octet'
46+
},
47+
{
48+
'counter': 1449885176,
49+
'dateTime': '2019-05-20T23:00:00-06:00',
50+
'type': 'publicIn_net_octet'
51+
},
52+
{
53+
'counter': 91803794,
54+
'dateTime': '2019-05-20T23:00:00-06:00',
55+
'type': 'publicOut_net_octet'
56+
}
57+
]

tests/CLI/modules/server_tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,35 @@ def test_toggle_ipmi_off(self):
584584
result = self.run_command(['server', 'toggle-ipmi', '--disable', '12345'])
585585
self.assert_no_fail(result)
586586
self.assertEqual(result.output, 'True\n')
587+
588+
def test_bandwidth_hw(self):
589+
result = self.run_command(['server', 'bandwidth', '100', '--start_date=2019-01-01', '--end_date=2019-02-01'])
590+
self.assert_no_fail(result)
591+
592+
# Since this is 2 tables, it gets returned as invalid json like "[{}][{}]"" instead of "[[{}],[{}]]"
593+
# so we just do some hacky string substitution to pull out the respective arrays that can be jsonifyied
594+
from pprint import pprint as pp
595+
pp(result.output)
596+
print("FUCK")
597+
pp(result.output[0:-157])
598+
output_summary = json.loads(result.output[0:-157])
599+
output_list = json.loads(result.output[-158:])
600+
601+
self.assertEqual(output_summary[0]['Average MBps'], 0.3841)
602+
self.assertEqual(output_summary[1]['Max Date'], '2019-05-20 23:00')
603+
self.assertEqual(output_summary[2]['Max GB'], 0.1172)
604+
self.assertEqual(output_summary[3]['Sum GB'], 0.0009)
605+
606+
self.assertEqual(output_list[0]['Date'], '2019-05-20 23:00')
607+
self.assertEqual(output_list[0]['Pub In'], 1.3503)
608+
609+
def test_bandwidth_hw_quite(self):
610+
result = self.run_command(['server', 'bandwidth', '100', '--start_date=2019-01-01', '--end_date=2019-02-01', '-q'])
611+
self.assert_no_fail(result)
612+
output_summary = json.loads(result.output)
613+
614+
self.assertEqual(output_summary[0]['Average MBps'], 0.3841)
615+
self.assertEqual(output_summary[1]['Max Date'], '2019-05-20 23:00')
616+
self.assertEqual(output_summary[2]['Max GB'], 0.1172)
617+
self.assertEqual(output_summary[3]['Sum GB'], 0.0009)
618+

tests/CLI/modules/vs/vs_tests.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mock
1010

1111
from SoftLayer.CLI import exceptions
12+
from SoftLayer.fixtures import SoftLayer_Virtual_Guest as SoftLayer_Virtual_Guest
1213
from SoftLayer import SoftLayerAPIError
1314
from SoftLayer import testing
1415

@@ -220,6 +221,49 @@ def test_detail_vs_no_dedicated_host_hostname(self):
220221
self.assertEqual(json.loads(result.output)['dedicated_host_id'], 37401)
221222
self.assertIsNone(json.loads(result.output)['dedicated_host'])
222223

224+
def test_detail_vs_security_group(self):
225+
vg_return = SoftLayer_Virtual_Guest.getObject
226+
sec_group = [
227+
{
228+
'id': 35386715,
229+
'name': 'eth',
230+
'port': 0,
231+
'speed': 100,
232+
'status': 'ACTIVE',
233+
'primaryIpAddress': '10.175.106.149',
234+
'securityGroupBindings': [
235+
{
236+
'id': 1620971,
237+
'networkComponentId': 35386715,
238+
'securityGroupId': 128321,
239+
'securityGroup': {
240+
'id': 128321,
241+
'name': 'allow_all'
242+
}
243+
}
244+
]
245+
}
246+
]
247+
248+
vg_return['networkComponents'] = sec_group
249+
mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject')
250+
mock.return_value = vg_return
251+
result = self.run_command(['vs', 'detail', '100'])
252+
self.assert_no_fail(result)
253+
output = json.loads(result.output)
254+
self.assertEqual(output['security_groups'][0]['id'], 128321)
255+
self.assertEqual(output['security_groups'][0]['name'], 'allow_all')
256+
self.assertEqual(output['security_groups'][0]['interface'], 'PRIVATE')
257+
258+
def test_detail_vs_ptr_error(self):
259+
mock = self.set_mock('SoftLayer_Virtual_Guest', 'getReverseDomainRecords')
260+
mock.side_effect = SoftLayerAPIError("SoftLayer_Exception", "Not Found")
261+
result = self.run_command(['vs', 'detail', '100'])
262+
self.assert_no_fail(result)
263+
output = json.loads(result.output)
264+
self.assertEqual(output.get('ptr', None), None)
265+
266+
223267
def test_create_options(self):
224268
result = self.run_command(['vs', 'create-options'])
225269

@@ -638,3 +682,37 @@ def test_usage_metric_data_empty(self):
638682
'--summary_period=300'])
639683
self.assertEqual(result.exit_code, 2)
640684
self.assertIsInstance(result.exception, exceptions.CLIAbort)
685+
686+
def test_bandwidth_vs(self):
687+
result = self.run_command(['vs', 'bandwidth', '100', '--start_date=2019-01-01', '--end_date=2019-02-01'])
688+
self.assert_no_fail(result)
689+
690+
# Since this is 2 tables, it gets returned as invalid json like "[{}][{}]"" instead of "[[{}],[{}]]"
691+
# so we just do some hacky string substitution to pull out the respective arrays that can be jsonifyied
692+
693+
from pprint import pprint as pp
694+
pp(result.output)
695+
print("FUCK")
696+
pp(result.output[0:-157])
697+
698+
output_summary = json.loads(result.output[0:-157])
699+
output_list = json.loads(result.output[-158:])
700+
701+
self.assertEqual(output_summary[0]['Average MBps'], 0.3841)
702+
self.assertEqual(output_summary[1]['Max Date'], '2019-05-20 23:00')
703+
self.assertEqual(output_summary[2]['Max GB'], 0.1172)
704+
self.assertEqual(output_summary[3]['Sum GB'], 0.0009)
705+
706+
self.assertEqual(output_list[0]['Date'], '2019-05-20 23:00')
707+
self.assertEqual(output_list[0]['Pub In'], 1.3503)
708+
709+
def test_bandwidth_vs_quite(self):
710+
result = self.run_command(['vs', 'bandwidth', '100', '--start_date=2019-01-01', '--end_date=2019-02-01', '-q'])
711+
self.assert_no_fail(result)
712+
output_summary = json.loads(result.output)
713+
714+
self.assertEqual(output_summary[0]['Average MBps'], 0.3841)
715+
self.assertEqual(output_summary[1]['Max Date'], '2019-05-20 23:00')
716+
self.assertEqual(output_summary[2]['Max GB'], 0.1172)
717+
self.assertEqual(output_summary[3]['Sum GB'], 0.0009)
718+

0 commit comments

Comments
 (0)