~notmyname/swift/deslogging

« back to all changes in this revision

Viewing changes to test/unit/stats/test_db_stats_collector.py

  • Committer: Tarmac
  • Author(s): David Goetz, Jay Payne
  • Date: 2011-05-18 15:48:17 UTC
  • mfrom: (286.3.22 containerstat)
  • Revision ID: tarmac-20110518154817-n03d5aig142496q2
Adding container stats collector, unit tests, and refactoring some of the stats code.  There will have to be changes to both the swift and rackswift conf files before this can be released.  Please DO NOT approve this branch for merge until glange's stats stuff is all ready to go.  gracias.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2010-2011 OpenStack, LLC.
 
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
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
 
 
17
import unittest
 
18
import os
 
19
import time
 
20
import uuid
 
21
from shutil import rmtree
 
22
from swift.stats import db_stats_collector
 
23
from tempfile import mkdtemp
 
24
from test.unit import FakeLogger
 
25
from swift.common.db import AccountBroker, ContainerBroker
 
26
from swift.common.utils import mkdirs
 
27
 
 
28
 
 
29
class TestDbStats(unittest.TestCase):
 
30
 
 
31
    def setUp(self):
 
32
        self._was_logger = db_stats_collector.get_logger
 
33
        db_stats_collector.get_logger = FakeLogger
 
34
        self.testdir = os.path.join(mkdtemp(), 'tmp_test_db_stats')
 
35
        self.devices = os.path.join(self.testdir, 'node')
 
36
        rmtree(self.testdir, ignore_errors=1)
 
37
        mkdirs(os.path.join(self.devices, 'sda'))
 
38
        self.accounts = os.path.join(self.devices, 'sda', 'accounts')
 
39
        self.containers = os.path.join(self.devices, 'sda', 'containers')
 
40
        self.log_dir = '%s/log' % self.testdir
 
41
 
 
42
        self.conf = dict(devices=self.devices,
 
43
                         log_dir=self.log_dir,
 
44
                         mount_check='false')
 
45
 
 
46
    def tearDown(self):
 
47
        db_stats_collector.get_logger = self._was_logger
 
48
        rmtree(self.testdir)
 
49
 
 
50
    def test_account_stat_get_data(self):
 
51
        stat = db_stats_collector.AccountStatsCollector(self.conf)
 
52
        account_db = AccountBroker("%s/acc.db" % self.accounts,
 
53
                                        account='test_acc')
 
54
        account_db.initialize()
 
55
        account_db.put_container('test_container', time.time(),
 
56
                                      None, 10, 1000)
 
57
        info = stat.get_data("%s/acc.db" % self.accounts)
 
58
        self.assertEquals('''"test_acc",1,10,1000\n''', info)
 
59
 
 
60
    def test_container_stat_get_data(self):
 
61
        stat = db_stats_collector.ContainerStatsCollector(self.conf)
 
62
        container_db = ContainerBroker("%s/con.db" % self.containers,
 
63
                                     account='test_acc', container='test_con')
 
64
        container_db.initialize()
 
65
        container_db.put_object('test_obj', time.time(), 10, 'text', 'faketag')
 
66
        info = stat.get_data("%s/con.db" % self.containers)
 
67
        self.assertEquals('''"test_acc","test_con",1,10\n''', info)
 
68
 
 
69
    def _gen_account_stat(self):
 
70
        stat = db_stats_collector.AccountStatsCollector(self.conf)
 
71
        output_data = set()
 
72
        for i in range(10):
 
73
            account_db = AccountBroker("%s/stats-201001010%s-%s.db" %
 
74
                                       (self.accounts, i, uuid.uuid4().hex),
 
75
                                        account='test_acc_%s' % i)
 
76
            account_db.initialize()
 
77
            account_db.put_container('test_container', time.time(),
 
78
                                      None, 10, 1000)
 
79
            # this will "commit" the data
 
80
            account_db.get_info()
 
81
            output_data.add('''"test_acc_%s",1,10,1000''' % i),
 
82
 
 
83
        self.assertEqual(len(output_data), 10)
 
84
        return stat, output_data
 
85
 
 
86
    def _gen_container_stat(self):
 
87
        stat = db_stats_collector.ContainerStatsCollector(self.conf)
 
88
        output_data = set()
 
89
        for i in range(10):
 
90
            account_db = ContainerBroker(
 
91
                "%s/container-stats-201001010%s-%s.db" % (self.containers, i,
 
92
                                                          uuid.uuid4().hex),
 
93
                 account='test_acc_%s' % i, container='test_con')
 
94
            account_db.initialize()
 
95
            account_db.put_object('test_obj', time.time(), 10, 'text',
 
96
                                  'faketag')
 
97
            # this will "commit" the data
 
98
            account_db.get_info()
 
99
            output_data.add('''"test_acc_%s","test_con",1,10''' % i),
 
100
 
 
101
        self.assertEqual(len(output_data), 10)
 
102
        return stat, output_data
 
103
 
 
104
    def test_account_stat_run_once_account(self):
 
105
        stat, output_data = self._gen_account_stat()
 
106
        stat.run_once()
 
107
        stat_file = os.listdir(self.log_dir)[0]
 
108
        with open(os.path.join(self.log_dir, stat_file)) as stat_handle:
 
109
            for i in range(10):
 
110
                data = stat_handle.readline()
 
111
                output_data.discard(data.strip())
 
112
 
 
113
        self.assertEqual(len(output_data), 0)
 
114
 
 
115
    def test_account_stat_run_once_both(self):
 
116
        acc_stat, acc_output_data = self._gen_account_stat()
 
117
        con_stat, con_output_data = self._gen_container_stat()
 
118
 
 
119
        acc_stat.run_once()
 
120
        stat_file = os.listdir(self.log_dir)[0]
 
121
        with open(os.path.join(self.log_dir, stat_file)) as stat_handle:
 
122
            for i in range(10):
 
123
                data = stat_handle.readline()
 
124
                acc_output_data.discard(data.strip())
 
125
 
 
126
        self.assertEqual(len(acc_output_data), 0)
 
127
 
 
128
        con_stat.run_once()
 
129
        stat_file = [f for f in os.listdir(self.log_dir) if f != stat_file][0]
 
130
        with open(os.path.join(self.log_dir, stat_file)) as stat_handle:
 
131
            for i in range(10):
 
132
                data = stat_handle.readline()
 
133
                con_output_data.discard(data.strip())
 
134
 
 
135
        self.assertEqual(len(con_output_data), 0)
 
136
 
 
137
    def test_account_stat_run_once_fail(self):
 
138
        stat, output_data = self._gen_account_stat()
 
139
        rmtree(self.accounts)
 
140
        stat.run_once()
 
141
        self.assertEquals(len(stat.logger.log_dict['debug']), 1)
 
142
 
 
143
    def test_not_implemented(self):
 
144
        db_stat = db_stats_collector.DatabaseStatsCollector(self.conf,
 
145
                                     'account', 'test_dir', 'stats-%Y%m%d%H_')
 
146
        self.assertRaises(Exception, db_stat.get_data)
 
147
 
 
148
    def test_not_not_mounted(self):
 
149
        self.conf['mount_check'] = 'true'
 
150
        stat, output_data = self._gen_account_stat()
 
151
        stat.run_once()
 
152
        self.assertEquals(len(stat.logger.log_dict['error']), 1)
 
153
 
 
154
if __name__ == '__main__':
 
155
    unittest.main()