This file is indexed.

/usr/lib/python2.7/dist-packages/pylxd/deprecated/tests/test_image.py is in python-pylxd 2.2.6-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
# Copyright (c) 2015 Canonical 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 datetime
from ddt import ddt
import mock
from six.moves import builtins
from six.moves import cStringIO
import unittest

from pylxd.deprecated import connection
from pylxd.deprecated import exceptions
from pylxd.deprecated import image

from pylxd.deprecated.tests import annotated_data
from pylxd.deprecated.tests import fake_api
from pylxd.deprecated.tests import LXDAPITestBase


@ddt
@mock.patch.object(connection.LXDConnection, 'get_object',
                   return_value=('200', fake_api.fake_image_info()))
class LXDAPIImageTestObject(LXDAPITestBase):

    list_data = (
        ('list', (), ()),
        ('search', ({'foo': 'bar'},), ('foo=bar',)),
    )

    @annotated_data(*list_data)
    def test_list_images(self, method, args, call_args, ms):
        ms.return_value = ('200', fake_api.fake_image_list())
        self.assertEqual(
            ['trusty'], getattr(self.lxd, 'image_' + method)(*args))
        ms.assert_called_once_with('GET', '/1.0/images', *call_args)

    @annotated_data(*list_data)
    def test_list_images_fail(self, method, args, call_args, ms):
        ms.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException,
                          getattr(self.lxd, 'image_' + method),
                          *args)
        ms.assert_called_once_with('GET', '/1.0/images', *call_args)

    @annotated_data(
        (True, (('200', fake_api.fake_image_info()),)),
        (False, exceptions.APIError("404", 404)),
    )
    def test_image_defined(self, expected, side_effect, ms):
        ms.side_effect = side_effect
        self.assertEqual(expected, self.lxd.image_defined('test-image'))
        ms.assert_called_once_with('GET', '/1.0/images/test-image')

    @annotated_data(
        ('APIError', exceptions.APIError("500", 500), exceptions.APIError),
        ('PyLXDException', exceptions.PyLXDException,
         exceptions.PyLXDException)
    )
    def test_image_defined_fail(self, tag, side_effect, expected, ms):
        ms.side_effect = side_effect
        self.assertRaises(expected,
                          self.lxd.image_defined, ('test-image',))
        ms.assert_called_once_with('GET', '/1.0/images/test-image')

    def test_image_info(self, ms):
        self.assertEqual({
            'image_upload_date': (datetime.datetime
                                  .fromtimestamp(1435669853)
                                  .strftime('%Y-%m-%d %H:%M:%S')),
            'image_created_date': 'Unknown',
            'image_expires_date': 'Unknown',
            'image_public': False,
            'image_size': '63MB',
            'image_fingerprint': '04aac4257341478b49c25d22cea8a6ce'
                                 '0489dc6c42d835367945e7596368a37f',
            'image_architecture': 'x86_64',
        }, self.lxd.image_info('test-image'))
        ms.assert_called_once_with('GET', '/1.0/images/test-image')

    def test_image_info_fail(self, ms):
        ms.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException,
                          self.lxd.image_info, ('test-image',))
        ms.assert_called_once_with('GET', '/1.0/images/test-image')

    dates_data = (
        ('upload', (datetime.datetime.fromtimestamp(1435669853)
                    .strftime('%Y-%m-%d %H:%M:%S'))),
        ('create', 'Unknown'),
        ('expire', 'Unknown'),
    )

    @annotated_data(*dates_data)
    def test_image_date(self, method, expected, ms):
        self.assertEqual(expected, getattr(
            self.lxd, 'image_{}_date'.format(method))('test-image',
                                                      data=None))
        ms.assert_called_once_with('GET', '/1.0/images/test-image')

    @annotated_data(*dates_data)
    def test_image_date_fail(self, method, expected, ms):
        ms.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException, getattr(
            self.lxd,
            'image_{}_date'.format(method)),
            'test-image',
            data=None)
        ms.assert_called_once_with('GET', '/1.0/images/test-image')


@ddt
@mock.patch.object(connection.LXDConnection, 'get_status', return_value=True)
class LXDAPIImageTestStatus(LXDAPITestBase):
    operations_data = (
        ('delete', 'DELETE', '/test-image', ('test-image',), ()),
        ('update', 'PUT', '/test-image', ('test-image', 'fake',), ('"fake"',)),
        ('rename', 'POST', '/test-image',
         ('test-image', 'fake',), ('"fake"',)),
    )

    @annotated_data(*operations_data)
    def test_image_operations(self, method, http, path, args, call_args, ms):
        self.assertTrue(
            getattr(self.lxd, 'image_' + method)(*args))
        ms.assert_called_once_with(
            http,
            '/1.0/images' + path,
            *call_args
        )

    @annotated_data(*operations_data)
    def test_image_operations_fail(self, method, http, path,
                                   args, call_args, ms):
        ms.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException,
                          getattr(self.lxd, 'image_' + method),
                          *args)
        ms.assert_called_once_with(
            http,
            '/1.0/images' + path,
            *call_args
        )


@mock.patch.object(connection.LXDConnection, 'get_object',
                   return_value=('200', fake_api.fake_image_info()))
class LXDAPAPIImageTestUpload(LXDAPITestBase):
    @mock.patch.object(builtins, 'open', return_value=cStringIO('fake'))
    def test_image_upload_file(self, mo, ms):
        self.assertTrue(self.lxd.image_upload(path='/fake/path'))
        mo.assert_called_once_with('/fake/path', 'rb')
        ms.assert_called_once_with('POST', '/1.0/images', 'fake', {})


@mock.patch.object(connection.LXDConnection, 'get_raw')
class LXDAPIImageTestRaw(LXDAPITestBase):

    def test_image_export(self, ms):
        ms.return_value = 'fake contents'
        self.assertEqual('fake contents', self.lxd.image_export('fake'))
        ms.assert_called_once_with('GET', '/1.0/images/fake/export')

    def test_image_export_fail(self, ms):
        ms.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException,
                          self.lxd.image_export, 'fake')
        ms.assert_called_once_with('GET', '/1.0/images/fake/export')


@ddt
@mock.patch.object(connection.LXDConnection, 'get_object',
                   return_value=(200, fake_api.fake_image_info()))
class LXDAPIImageInfoTest(unittest.TestCase):

    def setUp(self):
        super(LXDAPIImageInfoTest, self).setUp()
        self.image = image.LXDImage()

    info_list = (
        ('permission', False),
        ('size', 63),
        ('fingerprint', '04aac4257341478b49c25d22cea8a6ce'
                        '0489dc6c42d835367945e7596368a37f'),
        ('architecture', 'x86_64'),
    )

    @annotated_data(*info_list)
    def test_info_no_data(self, method, expected, mc):
        self.assertEqual(expected,
                         (getattr(self.image, 'get_image_' + method)
                          ('test-image', data=None)))
        mc.assert_called_once_with('GET', '/1.0/images/test-image')

    @annotated_data(*info_list)
    def test_info_no_data_fail(self, method, expected, mc):
        mc.side_effect = exceptions.PyLXDException
        self.assertRaises(exceptions.PyLXDException,
                          getattr(self.image, 'get_image_' + method),
                          'test-image',
                          data=None)

    @annotated_data(
        ('permission_true', 'permission', {'public': 0}, False),
        ('permission_false', 'permission', {'public': 1}, True),
        ('size', 'size', {'size': 52428800}, 50),
        ('fingerprint', 'fingerprint', {'fingerprint': 'AAAA'}, 'AAAA'),
        *[('architecture_' + v, 'architecture', {'architecture': k}, v)
          for k, v in image.image_architecture.items()]
    )
    def test_info_data(self, tag, method, metadata, expected, mc):
        self.assertEqual(
            expected, getattr(self.image, 'get_image_' + method)
            ('test-image', data=metadata))
        self.assertFalse(mc.called)

    @annotated_data(
        ('permission', 'permission', {}, KeyError),
        ('size', 'size', {'size': 0}, exceptions.ImageInvalidSize),
        ('size', 'size', {'size': -1}, exceptions.ImageInvalidSize),
        ('fingerprint', 'fingerprint', {}, KeyError),
        ('architecture', 'architecture', {}, KeyError),
        ('architecture_invalid', 'architecture',
         {'architecture': -1}, KeyError)
    )
    def test_info_data_fail(self, tag, method, metadata, expected, mc):
        self.assertRaises(expected,
                          getattr(self.image, 'get_image_' + method),
                          'test-image',
                          data=metadata)
        self.assertFalse(mc.called)