1
# Copyright 2017 Canonical Ltd
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
18
from mock import patch
19
from testtools import TestCase
20
from tempfile import mkdtemp
21
from charmhelpers.contrib.storage.linux import bcache
25
'cache_bypass_hits': '1132623\n',
26
'cache_bypass_misses': '0\n',
27
'cache_hit_ratio': '64\n',
28
'cache_hits': '12177090\n',
29
'cache_miss_collisions': '7091\n',
30
'cache_misses': '6717011\n',
31
'cache_readaheads': '0\n',
34
tmpdir = 'bcache-stats-test.'
39
class BcacheTestCase(TestCase):
41
super(BcacheTestCase, self).setUp()
42
self.sysfs = sysfs = mkdtemp(prefix=tmpdir)
44
self.cacheset = '{}/fs/bcache/{}'.format(sysfs, cacheset)
45
os.makedirs(self.cacheset)
46
self.devcache = '{}/block/{}/bcache'.format(sysfs, cachedev)
47
for n in ['register', 'register_quiet']:
48
with open('{}/fs/bcache/{}'.format(sysfs, n), 'w') as f:
50
for kind in self.cacheset, self.devcache:
51
for sub in bcache.stats_intervals:
52
intvaldir = '{}/{}'.format(kind, sub)
53
os.makedirs(intvaldir)
54
for fn, val in test_stats.items():
55
with open(os.path.join(intvaldir, fn), 'w') as f:
58
def test_get_bcache_fs(self):
59
bcachedirs = bcache.get_bcache_fs()
60
assert len(bcachedirs) == 1
61
assert next(iter(bcachedirs)).cachepath.endswith('/fs/bcache/abcde')
63
@patch('charmhelpers.contrib.storage.linux.bcache.os')
64
def test_get_bcache_fs_nobcache(self, mock_os):
65
mock_os.listdir.side_effect = OSError(
66
'[Errno 2] No such file or directory:...')
67
bcachedirs = bcache.get_bcache_fs()
68
assert bcachedirs == []
70
def test_get_stats_global(self):
71
out = bcache.get_stats_action(
74
assert len(out.keys()) == 1
75
k = next(iter(out.keys()))
76
assert k.endswith(cacheset)
77
assert out[k]['bypassed'] == '128G'
79
def test_get_stats_dev(self):
80
out = bcache.get_stats_action(
83
assert len(out.keys()) == 1
84
k = next(iter(out.keys()))
85
assert k.endswith('sdfoo/bcache')
86
assert out[k]['cache_hit_ratio'] == '64'
89
super(BcacheTestCase, self).tearDown()
90
shutil.rmtree(self.sysfs)