Debian Codegraph @

glanceclient/tests/unit/v2/test_cache.py

raw symbols SCIP
1 # Copyright 2021 Red Hat 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 import testtools
17 from unittest import mock
18
19 from glanceclient.common import utils as common_utils
20 from glanceclient import exc
21 from glanceclient.tests import utils
22 from glanceclient.v2 import cache
23
24
25 data_fixtures = {
26 '/v2/cache': {
27 'GET': (
28 {},
29 {
30 'cached_images': [
31 {
32 'id': 'b0aa672a-bc26-4fcb-8be1-f53ca361943d',
33 'Last Accessed (UTC)': '2021-08-09T07:08:20.214543',
34 'Last Modified (UTC)': '2021-08-09T07:08:20.214543',
35 'Size': 13267968,
36 'Hits': 0
37 },
38 {
39 'id': 'df601a47-7251-4d20-84ae-07de335af424',
40 'Last Accessed (UTC)': '2021-08-09T07:08:20.214543',
41 'Last Modified (UTC)': '2021-08-09T07:08:20.214543',
42 'Size': 13267968,
43 'Hits': 0
44 },
45 ],
46 'queued_images': [
47 '3a4560a1-e585-443e-9b39-553b46ec92d1',
48 '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'
49 ],
50 },
51 ),
52 'DELETE': (
53 {},
54 '',
55 ),
56 },
57 '/v2/cache/3a4560a1-e585-443e-9b39-553b46ec92d1': {
58 'PUT': (
59 {},
60 '',
61 ),
62 'DELETE': (
63 {},
64 '',
65 ),
66 },
67 }
68
69
70 class TestCacheController(testtools.TestCase):
71 def setUp(self):
72 super(TestCacheController, self).setUp()
73 self.api = utils.FakeAPI(data_fixtures)
74 self.controller = cache.Controller(self.api)
75
76 @mock.patch.object(common_utils, 'has_version')
77 def test_list_cached(self, mock_has_version):
78 mock_has_version.return_value = True
79 images = self.controller.list()
80 # Verify that we have 2 cached and 2 queued images
81 self.assertEqual(2, len(images['cached_images']))
82 self.assertEqual(2, len(images['queued_images']))
83
84 @mock.patch.object(common_utils, 'has_version')
85 def test_list_cached_empty_response(self, mock_has_version):
86 dummy_fixtures = {
87 '/v2/cache': {
88 'GET': (
89 {},
90 {
91 'cached_images': [],
92 'queued_images': [],
93 },
94 ),
95 }
96 }
97 dummy_api = utils.FakeAPI(dummy_fixtures)
98 dummy_controller = cache.Controller(dummy_api)
99 mock_has_version.return_value = True
100 images = dummy_controller.list()
101 # Verify that we have 0 cached and 0 queued images
102 self.assertEqual(0, len(images['cached_images']))
103 self.assertEqual(0, len(images['queued_images']))
104
105 @mock.patch.object(common_utils, 'has_version')
106 def test_queue_image(self, mock_has_version):
107 mock_has_version.return_value = True
108 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
109 self.controller.queue(image_id)
110 expect = [('PUT', '/v2/cache/%s' % image_id,
111 {}, None)]
112 self.assertEqual(expect, self.api.calls)
113
114 @mock.patch.object(common_utils, 'has_version')
115 def test_cache_clear_with_header(self, mock_has_version):
116 mock_has_version.return_value = True
117 self.controller.clear("cache")
118 expect = [('DELETE', '/v2/cache',
119 {'x-image-cache-clear-target': 'cache'}, None)]
120 self.assertEqual(expect, self.api.calls)
121
122 @mock.patch.object(common_utils, 'has_version')
123 def test_cache_delete(self, mock_has_version):
124 mock_has_version.return_value = True
125 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
126 self.controller.delete(image_id)
127 expect = [('DELETE', '/v2/cache/%s' % image_id,
128 {}, None)]
129 self.assertEqual(expect, self.api.calls)
130
131 @mock.patch.object(common_utils, 'has_version')
132 def test_cache_not_supported(self, mock_has_version):
133 mock_has_version.return_value = False
134 self.assertRaises(exc.HTTPNotImplemented,
135 self.controller.list)