~peter-sabaini/charm-helpers/bcache-helpers

« back to all changes in this revision

Viewing changes to tests/contrib/storage/test_bcache.py

  • Committer: Peter Sabaini
  • Date: 2017-05-04 15:34:09 UTC
  • Revision ID: peter.sabaini@canonical.com-20170504153409-v3tayraiwa4bny82
Add bcache support module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2017 Canonical Ltd
 
2
#
 
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
 
6
#
 
7
#  http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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.
 
14
 
 
15
import os
 
16
import shutil
 
17
import json
 
18
from mock import patch
 
19
from testtools import TestCase
 
20
from tempfile import mkdtemp
 
21
from charmhelpers.contrib.storage.linux import bcache
 
22
 
 
23
test_stats = {
 
24
    'bypassed': '128G\n',
 
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',
 
32
}
 
33
 
 
34
tmpdir = 'bcache-stats-test.'
 
35
cacheset = 'abcde'
 
36
cachedev = 'sdfoo'
 
37
 
 
38
 
 
39
class BcacheTestCase(TestCase):
 
40
    def setUp(self):
 
41
        super(BcacheTestCase, self).setUp()
 
42
        self.sysfs = sysfs = mkdtemp(prefix=tmpdir)
 
43
        bcache.SYSFS = sysfs
 
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:
 
49
                f.write('foo')
 
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:
 
56
                        f.write(val)
 
57
 
 
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')
 
62
 
 
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 == []
 
69
 
 
70
    def test_get_stats_global(self):
 
71
        out = bcache.get_stats_action(
 
72
            'global', 'hour')
 
73
        out = json.loads(out)
 
74
        assert len(out.keys()) == 1
 
75
        k = next(iter(out.keys()))
 
76
        assert k.endswith(cacheset)
 
77
        assert out[k]['bypassed'] == '128G'
 
78
 
 
79
    def test_get_stats_dev(self):
 
80
        out = bcache.get_stats_action(
 
81
            cachedev, 'hour')
 
82
        out = json.loads(out)
 
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'
 
87
 
 
88
    def tearDown(self):
 
89
        super(BcacheTestCase, self).tearDown()
 
90
        shutil.rmtree(self.sysfs)