1#    (c) Copyright 2016 Brocade Communications Systems Inc.
2#    All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15#
16
17
18"""Unit tests for brcd fc san lookup service."""
19
20import mock
21from oslo_config import cfg
22from oslo_utils import importutils
23
24from cinder import test
25from cinder.volume import configuration as conf
26import cinder.zonemanager.drivers.brocade.brcd_fc_san_lookup_service \
27    as brcd_lookup
28
29
30parsed_switch_port_wwns = ['20:1a:00:05:1e:e8:e3:29',
31                           '10:00:00:90:fa:34:40:f6']
32switch_data = ("""
33 Type Pid    COS     PortName                NodeName                 TTL(sec)
34 N    011a00;    2,3;    %(port_1)s;    20:1a:00:05:1e:e8:e3:29;    na
35    FC4s: FCP
36    PortSymb: [26] "222222 - 1:1:1 - LPe12442"
37    NodeSymb: [32] "SomeSym 7211"
38    Fabric Port Name: 20:1a:00:05:1e:e8:e3:29
39    Permanent Port Name: 22:22:00:22:ac:00:bc:b0
40    Port Index: 0
41    Share Area: No
42    Device Shared in Other AD: No
43    Redirect: No
44    Partial: No
45    LSAN: No
46 N    010100;    2,3;    %(port_2)s;    20:00:00:00:af:00:00:af;     na
47    FC4s: FCP
48    PortSymb: [26] "333333 - 1:1:1 - LPe12442"
49    NodeSymb: [32] "SomeSym 2222"
50    Fabric Port Name: 10:00:00:90:fa:34:40:f6
51    Permanent Port Name: 22:22:00:22:ac:00:bc:b0
52    Port Index: 0
53    Share Area: No
54    Device Shared in Other AD: No
55    Redirect: No
56    Partial: No
57    LSAN: No""" % {'port_1': parsed_switch_port_wwns[0],
58                   'port_2': parsed_switch_port_wwns[1]})
59
60_device_map_to_verify = {
61    'BRCD_FAB_2': {
62        'initiator_port_wwn_list': [parsed_switch_port_wwns[1].replace(':',
63                                                                       '')],
64        'target_port_wwn_list': [parsed_switch_port_wwns[0].replace(':', '')]}}
65
66
67class TestBrcdFCSanLookupService(brcd_lookup.BrcdFCSanLookupService,
68                                 test.TestCase):
69
70    def setUp(self):
71        super(TestBrcdFCSanLookupService, self).setUp()
72        self.configuration = conf.Configuration(None)
73        self.configuration.set_default('fc_fabric_names', 'BRCD_FAB_2',
74                                       'fc-zone-manager')
75        self.configuration.fc_fabric_names = 'BRCD_FAB_2'
76        self.configuration.brcd_sb_connector = ('cinder.tests.unit.zonemanager'
77                                                '.test_brcd_fc_san_lookup_'
78                                                'service'
79                                                '.FakeBrcdFCZoneClientCLI')
80        self.create_configuration()
81
82    # override some of the functions
83    def __init__(self, *args, **kwargs):
84        test.TestCase.__init__(self, *args, **kwargs)
85
86    def create_configuration(self):
87        fc_fabric_opts = []
88        fc_fabric_opts.append(cfg.StrOpt('fc_fabric_address',
89                                         default='10.24.49.100', help=''))
90        fc_fabric_opts.append(cfg.StrOpt('fc_fabric_user',
91                                         default='admin', help=''))
92        fc_fabric_opts.append(cfg.StrOpt('fc_fabric_password',
93                                         default='password', help='',
94                                         secret=True))
95        fc_fabric_opts.append(cfg.PortOpt('fc_fabric_port',
96                                          default=22, help=''))
97        config = conf.Configuration(fc_fabric_opts, 'BRCD_FAB_2')
98        self.fabric_configs = {'BRCD_FAB_2': config}
99
100    def get_client(self, protocol='HTTPS'):
101        conn = ('cinder.tests.unit.zonemanager.'
102                'test_brcd_fc_san_lookup_service.' +
103                ('FakeBrcdFCZoneClientCLI' if protocol == "CLI"
104                 else 'FakeBrcdHttpFCZoneClient'))
105        client = importutils.import_object(
106            conn,
107            ipaddress="10.24.48.213",
108            username="admin",
109            password="password",
110            key="/home/stack/.ssh/id_rsa",
111            port=22,
112            vfid="2",
113            protocol=protocol
114        )
115        return client
116
117    @mock.patch.object(brcd_lookup.BrcdFCSanLookupService,
118                       '_get_southbound_client')
119    def test_get_device_mapping_from_network(self, get_southbound_client_mock):
120        initiator_list = [parsed_switch_port_wwns[1]]
121        target_list = [parsed_switch_port_wwns[0], '20240002ac000a40']
122        get_southbound_client_mock.return_value = self.get_client("HTTPS")
123        device_map = self.get_device_mapping_from_network(
124            initiator_list, target_list)
125        self.assertDictEqual(_device_map_to_verify, device_map)
126
127
128class FakeClient(object):
129    def is_supported_firmware(self):
130        return True
131
132    def get_nameserver_info(self):
133        ns_info_list_expected = (parsed_switch_port_wwns)
134        return ns_info_list_expected
135
136    def close_connection(self):
137        pass
138
139    def cleanup(self):
140        pass
141
142
143class FakeBrcdFCZoneClientCLI(FakeClient):
144    def __init__(self, ipaddress, username,
145                 password, port, key, vfid, protocol):
146        self.firmware_supported = True
147
148
149class FakeBrcdHttpFCZoneClient(FakeClient):
150
151    def __init__(self, ipaddress, username,
152                 password, port, key, vfid, protocol):
153        self.firmware_supported = True
154