# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import string

from nova import test
from nova.volume import iscsi


class TargetAdminTestCase(object):

    def setUp(self):
        self.cmds = []

        self.tid = 1
        self.target_name = 'iqn.2011-09.org.foo.bar:blaa'
        self.lun = 10
        self.path = '/foo/bar/blaa'

        self.script_template = None

    def get_script_params(self):
        return {'tid': self.tid,
                'target_name': self.target_name,
                'lun': self.lun,
                'path': self.path}

    def get_script(self):
        return self.script_template % self.get_script_params()

    def fake_execute(self, *cmd, **kwargs):
        self.cmds.append(string.join(cmd))
        return "", None

    def clear_cmds(self):
        cmds = []

    def verify_cmds(self, cmds):
        self.assertEqual(len(cmds), len(self.cmds))
        for a, b in zip(cmds, self.cmds):
            self.assertEqual(a, b)

    def verify(self):
        script = self.get_script()
        cmds = []
        for line in script.split('\n'):
            if not line.strip():
                continue
            cmds.append(line)
        self.verify_cmds(cmds)

    def run_commands(self):
        tgtadm = iscsi.get_target_admin()
        tgtadm.set_execute(self.fake_execute)
        tgtadm.new_target(self.target_name, self.tid)
        tgtadm.show_target(self.tid)
        tgtadm.new_logicalunit(self.tid, self.lun, self.path)
        tgtadm.delete_logicalunit(self.tid, self.lun)
        tgtadm.delete_target(self.tid)

    def test_target_admin(self):
        self.clear_cmds()
        self.run_commands()
        self.verify()


class TgtAdmTestCase(test.TestCase, TargetAdminTestCase):

    def setUp(self):
        super(TgtAdmTestCase, self).setUp()
        TargetAdminTestCase.setUp(self)
        self.flags(iscsi_helper='tgtadm')
        self.script_template = "\n".join([
        "tgtadm --op new --lld=iscsi --mode=target --tid=%(tid)s "
                "--targetname=%(target_name)s",
        "tgtadm --op bind --lld=iscsi --mode=target --initiator-address=ALL "
                "--tid=%(tid)s",
        "tgtadm --op show --lld=iscsi --mode=target --tid=%(tid)s",
        "tgtadm --op new --lld=iscsi --mode=logicalunit --tid=%(tid)s "
                "--lun=%(lun)d --backing-store=%(path)s",
        "tgtadm --op delete --lld=iscsi --mode=logicalunit --tid=%(tid)s "
                "--lun=%(lun)d",
        "tgtadm --op delete --lld=iscsi --mode=target --tid=%(tid)s"])

    def get_script_params(self):
        params = super(TgtAdmTestCase, self).get_script_params()
        params['lun'] += 1
        return params


class IetAdmTestCase(test.TestCase, TargetAdminTestCase):

    def setUp(self):
        super(IetAdmTestCase, self).setUp()
        TargetAdminTestCase.setUp(self)
        self.flags(iscsi_helper='ietadm')
        self.script_template = "\n".join([
        "ietadm --op new --tid=%(tid)s --params Name=%(target_name)s",
        "ietadm --op show --tid=%(tid)s",
        "ietadm --op new --tid=%(tid)s --lun=%(lun)d "
                "--params Path=%(path)s,Type=fileio",
        "ietadm --op delete --tid=%(tid)s --lun=%(lun)d",
        "ietadm --op delete --tid=%(tid)s"])

@@ -924,13 +924,33 @@
 /foo
-/bar/
+'%0A        self.vol_id = '
 blaa
@@ -1949,19 +1949,28 @@
  tgtadm.
-new
+create_iscsi
 _target(
@@ -1987,33 +1987,33 @@
 t_name, self.tid
-)
+,
 %0A        tgtadm.
@@ -2009,188 +2009,138 @@
     
-tgtadm.show_target(self.tid)%0A        tgtadm.new_logicalunit(self.tid, self.lun, self.path)%0A        tgtadm.delete_logicalunit(self.tid, self.lun)%0A        tgtadm.delete_target(self.t
+        self.lun, self.path)%0A        tgtadm.show_target(self.tid)%0A        tgtadm.remove_iscsi_target(self.tid, self.lun, self.vol_
 id)%0A
@@ -2453,24 +2453,61 @@
 r='tgtadm')%0A
+        self.flags(volumes_dir=%22./%22)%0A
         self
@@ -2550,222 +2550,55 @@
    %22
-tgtadm --op new --lld=iscsi --mode=target --tid=%25(tid)s %22%0A                %22--targetname=%25(target_name)s%22,%0A        %22tgtadm --op bind --lld=iscsi --mode=target --initiator-address=ALL %22%0A                %22--tid=%25(tid)s
+/usr/sbin/tgt-admin --conf ./blaa --update blaa
 %22,%0A 
@@ -2650,31 +2650,25 @@
 arget --tid=
-%25(tid)s
+1
 %22,%0A        %22
@@ -2671,455 +2671,57 @@
    %22
-tgtadm --op new --lld=iscsi --mode=logicalunit --tid=%25(tid)s %22%0A                %22--lun=%25(lun)d --backing-store=%25(path)s%22,%0A        %22
+/usr/bin/
 tgt
+-
 adm
- --op delete --lld=iscsi --mode=logicalunit --tid=%25(tid)s %22%0A                %22--lun=%25(lun)d%22,%0A        %22tgtadm --op delete --lld=iscsi --mode=target --tid=%25(tid)s%22%5D)%0A%0A    def get_script_params(self):%0A        params = super(TgtAdmTestCase, self).get_script_params()%0A        params%5B'lun'%5D += 1%0A        return params
+in --conf ./blaa --delete blaa%22%5D)
 %0A%0A%0Ac
@@ -3043,50 +3043,8 @@
 s%22,%0A
-        %22ietadm --op show --tid=%25(tid)s%22,%0A
     
@@ -3090,17 +3090,17 @@
 n=%25(lun)
-d
+s
  %22%0A     
@@ -3161,38 +3161,36 @@
    %22ietadm --op 
-delete
+show
  --tid=%25(tid)s -
@@ -3191,22 +3191,52 @@
 id)s
- --lun=%25(lun)d
+%22,%0A        %22ietadm --op delete --tid=%25(tid)s
 %22,%0A 
@@ -3275,12 +3275,26 @@
 =%25(tid)s
+ --lun=%25(lun)s
 %22%5D)%0A

Add persistent volumes for tgtd.