This file is indexed.

/usr/lib/python2.7/dist-packages/ironicclient/tests/unit/v1/test_volume_target_shell.py is in python-ironicclient 2.2.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2017 Hitachi, Ltd.
#
#    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 mock

from oslo_utils import uuidutils

from ironicclient.common.apiclient import exceptions
from ironicclient.common import cliutils
from ironicclient.common import utils as commonutils
from ironicclient.tests.unit import utils
import ironicclient.v1.volume_target_shell as vt_shell


class Volume_TargetShellTest(utils.BaseTestCase):

    def test_volume_target_show(self):
        actual = {}
        fake_print_dict = lambda data, *args, **kwargs: actual.update(data)
        with mock.patch.object(cliutils, 'print_dict', fake_print_dict):
            volume_target = object()
            vt_shell._print_volume_target_show(volume_target)
        exp = ['created_at', 'extra', 'node_uuid', 'volume_type',
               'updated_at', 'uuid', 'properties', 'boot_index',
               'volume_id']
        act = actual.keys()
        self.assertEqual(sorted(exp), sorted(act))

    def test_do_volume_target_show(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = 'volume_target_uuid'
        args.fields = None
        args.json = False

        vt_shell.do_volume_target_show(client_mock, args)
        client_mock.volume_target.get.assert_called_once_with(
            'volume_target_uuid', fields=None)

    def test_do_volume_target_show_space_uuid(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = '   '
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_show,
                          client_mock, args)

    def test_do_volume_target_show_empty_uuid(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = ''
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_show,
                          client_mock, args)

    def test_do_volume_target_show_fields(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = 'volume_target_uuid'
        args.fields = [['uuid', 'boot_index']]
        args.json = False
        vt_shell.do_volume_target_show(client_mock, args)
        client_mock.volume_target.get.assert_called_once_with(
            'volume_target_uuid', fields=['uuid', 'boot_index'])

    def test_do_volume_target_show_invalid_fields(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = 'volume_target_uuid'
        args.fields = [['foo', 'bar']]
        args.json = False
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_show,
                          client_mock, args)

    def test_do_volume_target_update(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = 'volume_target_uuid'
        args.op = 'add'
        args.attributes = [['arg1=val1', 'arg2=val2']]
        args.json = False

        vt_shell.do_volume_target_update(client_mock, args)
        patch = commonutils.args_array_to_patch(args.op, args.attributes[0])
        client_mock.volume_target.update.assert_called_once_with(
            'volume_target_uuid', patch)

    def test_do_volume_target_update_wrong_op(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = 'volume_target_uuid'
        args.op = 'foo'
        args.attributes = [['arg1=val1', 'arg2=val2']]
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_update,
                          client_mock, args)
        self.assertFalse(client_mock.volume_target.update.called)

    def _get_client_mock_args(self, node=None, marker=None, limit=None,
                              sort_dir=None, sort_key=None, detail=False,
                              fields=None, json=False):
        args = mock.MagicMock(spec=True)
        args.node = node
        args.marker = marker
        args.limit = limit
        args.sort_dir = sort_dir
        args.sort_key = sort_key
        args.detail = detail
        args.fields = fields
        args.json = json

        return args

    def test_do_volume_target_list(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args()

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(detail=False)

    def test_do_volume_target_list_detail(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(detail=True)

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(detail=True)

    def test_do_volume_target_list_sort_key(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_key='uuid', detail=False)

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(
            sort_key='uuid', detail=False)

    def test_do_volume_target_list_wrong_sort_key(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_key='node_uuid', detail=False)

        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_list,
                          client_mock, args)
        self.assertFalse(client_mock.volume_target.list.called)

    def test_do_volume_target_list_detail_sort_key(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_key='uuid', detail=True)

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(
            sort_key='uuid', detail=True)

    def test_do_volume_target_list_detail_wrong_sort_key(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_key='node_uuid', detail=True)

        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_list,
                          client_mock, args)
        self.assertFalse(client_mock.volume_target.list.called)

    def test_do_volume_target_list_fields(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(fields=[['uuid', 'boot_index']])

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(
            fields=['uuid', 'boot_index'], detail=False)

    def test_do_volume_target_list_invalid_fields(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(fields=[['foo', 'bar']])
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_list,
                          client_mock, args)

    def test_do_volume_target_list_sort_dir(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_dir='desc', detail=False)

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(
            sort_dir='desc', detail=False)

    def test_do_volume_target_list_detail_sort_dir(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_dir='asc', detail=True)

        vt_shell.do_volume_target_list(client_mock, args)
        client_mock.volume_target.list.assert_called_once_with(
            sort_dir='asc', detail=True)

    def test_do_volume_target_wrong_sort_dir(self):
        client_mock = mock.MagicMock()
        args = self._get_client_mock_args(sort_dir='abc', detail=False)

        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_list,
                          client_mock, args)
        self.assertFalse(client_mock.volume_target.list.called)

    def test_do_volume_target_create(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.json = False
        vt_shell.do_volume_target_create(client_mock, args)
        client_mock.volume_target.create.assert_called_once_with()

    def test_do_volume_target_create_with_uuid(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.uuid = uuidutils.generate_uuid()
        args.json = False

        vt_shell.do_volume_target_create(client_mock, args)
        client_mock.volume_target.create.assert_called_once_with(
            uuid=args.uuid)

    def test_do_volume_target_create_valid_fields_values(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_type = 'volume_type'
        args.properties = ["key1=val1", "key2=val2"]
        args.boot_index = 100
        args.node_uuid = 'uuid'
        args.volume_id = 'volume_id'
        args.extra = ["key1=val1", "key2=val2"]
        args.json = False
        vt_shell.do_volume_target_create(client_mock, args)
        client_mock.volume_target.create.assert_called_once_with(
            volume_type='volume_type',
            properties={'key1': 'val1', 'key2': 'val2'},
            boot_index=100, node_uuid='uuid', volume_id='volume_id',
            extra={'key1': 'val1', 'key2': 'val2'})

    def test_do_volume_target_create_invalid_extra_fields(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_type = 'volume_type'
        args.properties = ["key1=val1", "key2=val2"]
        args.boot_index = 100
        args.node_uuid = 'uuid'
        args.volume_id = 'volume_id'
        args.extra = ["foo"]
        args.json = False
        self.assertRaises(exceptions.CommandError,
                          vt_shell.do_volume_target_create,
                          client_mock, args)

    def test_do_volume_target_delete(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = ['volume_target_uuid']
        vt_shell.do_volume_target_delete(client_mock, args)
        client_mock.volume_target.delete.assert_called_once_with(
            'volume_target_uuid')

    def test_do_volume_target_delete_multi(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = ['uuid1', 'uuid2']
        vt_shell.do_volume_target_delete(client_mock, args)
        self.assertEqual([mock.call('uuid1'), mock.call('uuid2')],
                         client_mock.volume_target.delete.call_args_list)

    def test_do_volume_target_delete_multi_error(self):
        client_mock = mock.MagicMock()
        args = mock.MagicMock()
        args.volume_target = ['uuid1', 'uuid2']
        client_mock.volume_target.delete.side_effect = [
            exceptions.ClientException('error'), None]
        self.assertRaises(exceptions.ClientException,
                          vt_shell.do_volume_target_delete,
                          client_mock, args)
        self.assertEqual([mock.call('uuid1'), mock.call('uuid2')],
                         client_mock.volume_target.delete.call_args_list)