Skip to content

Commit d184d5d

Browse files
committed
Adds a few tests for vs module
update_with_template_args() now takes a list_args argument to specify which arguments should be treated as a comma-separated list. Bumped required coverage percentage
1 parent 93a2754 commit d184d5d

File tree

9 files changed

+172
-61
lines changed

9 files changed

+172
-61
lines changed

SoftLayer/CLI/modules/server.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -779,17 +779,8 @@ class CreateServer(CLIRunnable):
779779
'--memory', '--os']
780780

781781
def execute(self, args):
782-
update_with_template_args(args)
782+
update_with_template_args(args, list_args=['--disk', '--key'])
783783
mgr = HardwareManager(self.client)
784-
785-
# Disks will be a comma-separated list. Let's make it a real list.
786-
if isinstance(args.get('--disk'), str):
787-
args['--disk'] = args.get('--disk').split(',')
788-
789-
# Do the same thing for SSH keys
790-
if isinstance(args.get('--key'), str):
791-
args['--key'] = args.get('--key').split(',')
792-
793784
self._validate_args(args)
794785

795786
ds_options = mgr.get_dedicated_server_create_options(args['--chassis'])

SoftLayer/CLI/modules/vs.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,9 @@ class CreateVS(CLIRunnable):
383383
required_params = ['--hostname', '--domain', '--cpu', '--memory']
384384

385385
def execute(self, args):
386-
update_with_template_args(args)
386+
update_with_template_args(args, list_args=['--disk', '--key'])
387387
vsi = VSManager(self.client)
388388
self._update_with_like_args(args)
389-
390-
# Disks will be a comma-separated list. Let's make it a real list.
391-
if isinstance(args.get('--disk'), str):
392-
args['--disk'] = args.get('--disk').split(',')
393-
394-
# SSH keys may be a comma-separated list. Let's make it a real list.
395-
if isinstance(args.get('--key'), str):
396-
args['--key'] = args.get('--key').split(',')
397-
398389
self._validate_args(args)
399390

400391
# Do not create a virtual server with --test or --export

SoftLayer/CLI/template.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,35 @@
1313
from SoftLayer.utils import configparser, StringIO
1414

1515

16-
def update_with_template_args(args):
16+
def update_with_template_args(args, list_args=None):
1717
""" Populates arguments with arguments from the template file, if provided.
1818
1919
:param dict args: command-line arguments
2020
"""
21-
if args.get('--template'):
22-
template_path = args.pop('--template')
23-
if not os.path.exists(template_path):
24-
raise ArgumentError(
25-
'File does not exist [-t | --template] = %s'
26-
% template_path)
27-
28-
config = configparser.ConfigParser()
29-
ini_str = '[settings]\n' + open(
30-
os.path.expanduser(template_path), 'r').read()
31-
ini_fp = StringIO(ini_str)
32-
config.readfp(ini_fp)
33-
34-
# Merge template options with the options passed in
35-
for key, value in config.items('settings'):
36-
option_key = '--%s' % key
37-
if not args.get(option_key):
38-
args[option_key] = value
21+
if not args.get('--template'):
22+
return
23+
24+
list_args = list_args or []
25+
26+
template_path = args.pop('--template')
27+
if not os.path.exists(template_path):
28+
raise ArgumentError(
29+
'File does not exist [-t | --template] = %s'
30+
% template_path)
31+
32+
config = configparser.ConfigParser()
33+
ini_str = '[settings]\n' + open(
34+
os.path.expanduser(template_path), 'r').read()
35+
ini_fp = StringIO(ini_str)
36+
config.readfp(ini_fp)
37+
38+
# Merge template options with the options passed in
39+
for key, value in config.items('settings'):
40+
option_key = '--%s' % key
41+
if option_key in list_args:
42+
value = value.split(',')
43+
if not args.get(option_key):
44+
args[option_key] = value
3945

4046

4147
def export_to_template(filename, args, exclude=None):

SoftLayer/tests/CLI/helper_tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,9 @@ def test_template_options(self):
377377
'--memory': '32',
378378
'--template': path,
379379
'--hourly': False,
380+
'--disk': [],
380381
}
381-
cli.helpers.update_with_template_args(args)
382+
cli.helpers.update_with_template_args(args, list_args=['--disk'])
382383
self.assertEqual(args, {
383384
'--cpu': '4',
384385
'--datacenter': 'dal05',
@@ -390,6 +391,7 @@ def test_template_options(self):
390391
'--network': '100',
391392
'--os': 'DEBIAN_7_64',
392393
'key': 'value',
394+
'--disk': ['50', '100'],
393395
})
394396

395397

SoftLayer/tests/CLI/modules/server_tests.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -519,15 +519,6 @@ def test_CreateServer(self):
519519

520520
self.assertEqual(expected, format_output(output, 'python'))
521521

522-
# And make sure we can pass in disk and SSH keys as comma separated
523-
# strings, which is what templates do
524-
args['--disk'] = '1000_DRIVE,1000_DRIVE'
525-
args['--key'] = '123,456'
526-
527-
output = runnable.execute(args)
528-
529-
self.assertEqual(expected, format_output(output, 'python'))
530-
531522
# Test explicitly setting a RAID configuration
532523
args['--controller'] = 'RAID0'
533524

@@ -691,15 +682,6 @@ def test_CreateServer_for_bmc(self, bmpi, packages):
691682

692683
self.assertEqual(expected, format_output(output, 'python'))
693684

694-
# And make sure we can pass in disk and SSH keys as comma separated
695-
# strings, which is what templates do
696-
args['--disk'] = '1000_DRIVE,1000_DRIVE'
697-
args['--key'] = '123,456'
698-
699-
output = runnable.execute(args)
700-
701-
self.assertEqual(expected, format_output(output, 'python'))
702-
703685
# Test explicitly setting a RAID configuration
704686
args['--controller'] = 'RAID0'
705687

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""
2+
SoftLayer.tests.CLI.modules.vs_tests
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4+
5+
:license: MIT, see LICENSE for more details.
6+
"""
7+
from mock import patch
8+
9+
from SoftLayer.tests import unittest, FixtureClient
10+
from SoftLayer.CLI.helpers import format_output
11+
from SoftLayer.CLI.modules import vs
12+
13+
14+
class DnsTests(unittest.TestCase):
15+
def setUp(self):
16+
self.client = FixtureClient()
17+
18+
def test_list_vs(self):
19+
command = vs.ListVSIs(client=self.client)
20+
21+
output = command.execute({'--tags': 'tag'})
22+
self.assertEqual([{'datacenter': 'TEST00',
23+
'primary_ip': '172.16.240.2',
24+
'host': 'vs-test1.test.sftlyr.ws',
25+
'memory': 1024,
26+
'cores': 2,
27+
'active_transaction': None,
28+
'id': 100,
29+
'backend_ip': '10.45.19.37'},
30+
{'datacenter': 'TEST00',
31+
'primary_ip': '172.16.240.7',
32+
'host': 'vs-test2.test.sftlyr.ws',
33+
'memory': 4096,
34+
'cores': 4,
35+
'active_transaction': None,
36+
'id': 104,
37+
'backend_ip': '10.45.19.35'}],
38+
format_output(output, 'python'))
39+
40+
def test_detail_vs(self):
41+
command = vs.VSDetails(client=self.client)
42+
43+
output = command.execute({'<identifier>': '100',
44+
'--passwords': True,
45+
'--price': True})
46+
47+
self.assertEqual({'active_transaction': None,
48+
'cores': 2,
49+
'created': '2013-08-01 15:23:45',
50+
'datacenter': 'TEST00',
51+
'hostname': 'vs-test1.test.sftlyr.ws',
52+
'id': 100,
53+
'memory': 1024,
54+
'modified': {},
55+
'os': '12.04-64 Minimal for CCI',
56+
'os_version': '12.04-64 Minimal for CCI',
57+
'price rate': {},
58+
'notes': 'notes',
59+
'tags': ['production'],
60+
'private_cpu': {},
61+
'private_ip': '10.45.19.37',
62+
'private_only': {},
63+
'ptr': 'test.softlayer.com.',
64+
'public_ip': '172.16.240.2',
65+
'state': 'RUNNING',
66+
'status': 'ACTIVE',
67+
'users': [{'password': 'pass', 'username': 'user'}],
68+
'vlans': [{'type': 'PUBLIC',
69+
'number': 23,
70+
'id': 1}]},
71+
format_output(output, 'python'))
72+
73+
def test_create_options(self):
74+
command = vs.CreateOptionsVS(client=self.client)
75+
76+
output = command.execute({'--all': True,
77+
'--cpu': True,
78+
'--datacenter': True,
79+
'--disk': True,
80+
'--memory': True,
81+
'--nic': True,
82+
'--os': True})
83+
84+
self.assertEqual({'cpus (private)': [],
85+
'cpus (standard)': ['1', '2', '3', '4'],
86+
'datacenter': ['ams01', 'dal05'],
87+
'local disk(0)': ['25', '100'],
88+
'memory': ['1024', '2048', '3072', '4096'],
89+
'nic': ['10', '100', '1000'],
90+
'os (CENTOS)': 'CENTOS_6_64',
91+
'os (DEBIAN)': 'DEBIAN_7_64',
92+
'os (UBUNTU)': 'UBUNTU_12_64'},
93+
format_output(output, 'python'))
94+
95+
@patch('SoftLayer.CLI.modules.vs.confirm')
96+
def test_create(self, confirm_mock):
97+
confirm_mock.return_value = True
98+
command = vs.CreateVS(client=self.client)
99+
100+
output = command.execute({'--cpu': '2',
101+
'--domain': 'example.com',
102+
'--hostname': 'host',
103+
'--image': None,
104+
'--os': 'UBUNTU_LATEST',
105+
'--memory': '1024',
106+
'--nic': '100',
107+
'--hourly': True,
108+
'--monthly': False,
109+
'--like': None,
110+
'--datacenter': None,
111+
'--dedicated': False,
112+
'--san': False,
113+
'--test': False,
114+
'--export': None,
115+
'--userfile': None,
116+
'--postinstall': None,
117+
'--key': [],
118+
'--like': [],
119+
'--network': [],
120+
'--disk': [],
121+
'--private': False,
122+
'--template': None,
123+
'--userdata': None,
124+
'--vlan_public': None,
125+
'--vlan_private': None,
126+
'--wait': None,
127+
'--really': False})
128+
129+
self.assertEqual([{'guid': '1a2b3c-1701',
130+
'id': 100,
131+
'created': '2013-08-01 15:23:45'}],
132+
format_output(output, 'python'))

SoftLayer/tests/fixtures/Virtual_Guest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
'blockDevices': [{"device": 0, "uuid": 1},
1919
{"device": 1},
2020
{"device": 2, "uuid": 2}],
21+
'notes': 'notes',
22+
'networkVlans': [{'networkSpace': 'PUBLIC',
23+
'vlanNumber': 23,
24+
'id': 1}],
2125
'operatingSystem': {
26+
'passwords': [{'username': 'user', 'password': 'pass'}],
2227
'softwareLicense': {
2328
'softwareDescription': {'version': '12.04-64 Minimal for CCI',
2429
'name': 'Ubuntu'}}
25-
}
30+
},
31+
'tagReferences': [{'tag': {'name': 'production'}}],
2632
}
2733

2834
getCreateObjectOptions = {

SoftLayer/tests/fixtures/sample_vs_template.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ memory = 1024
66
os = DEBIAN_7_64
77
network = 100
88
hourly = true
9-
monthly= false
9+
monthly= false
10+
disk=50,100

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
verbosity=2
33
detailed-errors=1
44
with-coverage=1
5-
cover-min-percentage=66
5+
cover-min-percentage=74
66
cover-erase=true
77
cover-package=SoftLayer
88
cover-html=1

0 commit comments

Comments
 (0)