File: test_drive_full_checker.py

package info (click to toggle)
swift 2.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,760 kB
  • sloc: python: 281,901; javascript: 1,059; sh: 619; pascal: 295; makefile: 81; xml: 32
file content (174 lines) | stat: -rw-r--r-- 5,900 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2024, Thomas Goirand <zigo@debian.org>
#
# 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 collections
import filecmp
import mock
import os
import tempfile
import unittest
import shutil
import sys

from test.debug_logger import debug_logger
from swift.cli import drive_full_checker

GiB = 1024 * 1024 * 1024

if sys.version_info[0] == 2:
    # os.statvfs is removed from Python >= 3
    freespace_func = 'os.statvfs'
else:
    # shutil.disk_usage doesn't exist in Python <= 2
    freespace_func = 'shutil.disk_usage'


class TestContainerDeleter(unittest.TestCase):
    def setUp(self):
        self.logger = debug_logger()

    def _write_rsyncd_conf(self, path, max_conn):
        rsyncdconf = """pid file = /var/run/rsyncd.pid
uid = nobody
gid = nobody
use chroot = no
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
address = 192.168.100.2

[ account_sdb ]
path = /srv/node
read only = false
write only = no
list = yes
uid = swift
gid = swift
incoming chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
outgoing chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
max connections = {max_conn}
timeout = 0
lock file = /var/lock/account_sdb.lock

[ container_sdb ]
path = /srv/node
read only = false
write only = no
list = yes
uid = swift
gid = swift
incoming chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
outgoing chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
max connections = {max_conn}
timeout = 0
lock file = /var/lock/container_sdb.lock

[ object_sdb ]
path = /srv/node
read only = false
write only = no
list = yes
uid = swift
gid = swift
incoming chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
outgoing chmod = Du=rwx,g=rx,o=rx,Fu=rw,g=r,o=r
max connections = {max_conn}
timeout = 0
lock file = /var/lock/object_sdb.lock

"""
        f = os.open(path, os.O_RDWR | os.O_CREAT)
        if sys.version_info[0] == 2:
            os.write(f, rsyncdconf.format(max_conn=max_conn))
        else:
            os.write(f, bytes(rsyncdconf.format(max_conn=max_conn), 'utf-8'))
        os.close(f)

    @mock.patch.object(drive_full_checker, 'ismount', return_value=True)
    @mock.patch(freespace_func)
    def test_drive_full(self, mock_freespace_func, os_path_ismount):
        # Create a temp folder to run our tests
        tmpdirname = tempfile.mkdtemp()

        storagepath = tmpdirname + '/srvnode'
        os.mkdir(storagepath)
        os.mkdir(storagepath + '/sdb')

        rsyncdpath = tmpdirname + "/rsyncd.conf"

        # Write a first rsyncd.conf with 8 connections for a,c,o
        self._write_rsyncd_conf(rsyncdpath, 8)

        if sys.version_info[0] == 2:
            retval = collections.namedtuple('statvfs_result',
                                            'f_bsize f_bavail')
            mock_freespace_func.return_value = retval(10, 10)
        else:
            retval = collections.namedtuple('usage', 'total used free')
            # This says: 10 bytes remaining
            mock_freespace_func.return_value = retval(10, 10, 10)

        drive_full_checker.configure_rsyncd_conf(10 * GiB, 8, ' account_{} ',
                                                 10 * GiB, 8, ' container_{} ',
                                                 10 * GiB, 8, ' object_{} ',
                                                 storagepath, rsyncdpath,
                                                 self.logger)

        should_be_rsyncdconf = tmpdirname + "/rsyncd_should_be.conf"
        self._write_rsyncd_conf(should_be_rsyncdconf, -1)

        # Assert that rsyncd.conf and rsyncd_should_be.conf are the same
        self.assertTrue(filecmp.cmp(rsyncdpath, should_be_rsyncdconf))

        shutil.rmtree(tmpdirname)

    @mock.patch.object(drive_full_checker, 'ismount', return_value=True)
    @mock.patch(freespace_func)
    def test_drive_with_space(self, mock_freespace_func, os_path_ismount):
        # Create a temp folder to run our tests
        tmpdirname = tempfile.mkdtemp()

        storagepath = tmpdirname + '/srvnode'
        os.mkdir(storagepath)
        os.mkdir(storagepath + '/sdb')

        rsyncdpath = tmpdirname + "/rsyncd.conf"

        # Write a first rsyncd.conf with 8 connections for a,c,o
        self._write_rsyncd_conf(rsyncdpath, -1)

        if sys.version_info[0] == 2:
            retval = collections.namedtuple('statvfs_result',
                                            'f_bsize f_bavail')
            mock_freespace_func.return_value = retval(10 * GiB, 10 * GiB)
        else:
            retval = collections.namedtuple('usage', 'total used free')
            # This says: 10 bytes remaining
            mock_freespace_func.return_value = retval(10 * GiB,
                                                      10 * GiB,
                                                      10 * GiB)

        drive_full_checker.configure_rsyncd_conf(10, 8, ' account_{} ',
                                                 10, 8, ' container_{} ',
                                                 10, 8, ' object_{} ',
                                                 storagepath, rsyncdpath,
                                                 self.logger)

        should_be_rsyncdconf = tmpdirname + "/rsyncd_should_be.conf"
        self._write_rsyncd_conf(should_be_rsyncdconf, 8)

        # Assert that rsyncd.conf and rsyncd_should_be.conf are the same
        self.assertTrue(filecmp.cmp(rsyncdpath, should_be_rsyncdconf))

        shutil.rmtree(tmpdirname)