1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Red Hat, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
23
from nova.volume import iscsi
26
class TargetAdminTestCase(object):
32
self.target_name = 'iqn.2011-09.org.foo.bar:blaa'
37
self.script_template = None
38
self.stubs.Set(os.path, 'isfile', lambda _: True)
39
self.stubs.Set(os, 'unlink', lambda _: '')
40
self.stubs.Set(iscsi.TgtAdm, '_get_target', self.fake_get_target)
42
def fake_get_target(obj, iqn):
45
def get_script_params(self):
46
return {'tid': self.tid,
47
'target_name': self.target_name,
52
return self.script_template % self.get_script_params()
54
def fake_execute(self, *cmd, **kwargs):
55
self.cmds.append(string.join(cmd))
61
def verify_cmds(self, cmds):
62
self.assertEqual(len(cmds), len(self.cmds))
63
for a, b in zip(cmds, self.cmds):
64
self.assertEqual(a, b)
67
script = self.get_script()
69
for line in script.split('\n'):
73
self.verify_cmds(cmds)
75
def run_commands(self):
76
tgtadm = iscsi.get_target_admin()
77
tgtadm.set_execute(self.fake_execute)
78
tgtadm.create_iscsi_target(self.target_name, self.tid,
80
tgtadm.show_target(self.tid, iqn=self.target_name)
81
tgtadm.remove_iscsi_target(self.tid, self.lun, self.vol_id)
83
def test_target_admin(self):
89
class TgtAdmTestCase(test.TestCase, TargetAdminTestCase):
92
super(TgtAdmTestCase, self).setUp()
93
TargetAdminTestCase.setUp(self)
94
self.persist_tempdir = tempfile.mkdtemp()
95
self.flags(iscsi_helper='tgtadm')
96
self.flags(volumes_dir=self.persist_tempdir)
97
self.script_template = "\n".join([
98
'tgt-admin --update iqn.2011-09.org.foo.bar:blaa',
99
'tgt-admin --delete iqn.2010-10.org.openstack:volume-blaa'])
103
shutil.rmtree(self.persist_tempdir)
106
super(TgtAdmTestCase, self).tearDown()
109
class IetAdmTestCase(test.TestCase, TargetAdminTestCase):
112
super(IetAdmTestCase, self).setUp()
113
TargetAdminTestCase.setUp(self)
114
self.flags(iscsi_helper='ietadm')
115
self.script_template = "\n".join([
116
'ietadm --op new --tid=%(tid)s --params Name=%(target_name)s',
117
'ietadm --op new --tid=%(tid)s --lun=%(lun)s '
118
'--params Path=%(path)s,Type=fileio',
119
'ietadm --op show --tid=%(tid)s',
120
'ietadm --op delete --tid=%(tid)s --lun=%(lun)s',
121
'ietadm --op delete --tid=%(tid)s'])