/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@jelmer.uk>
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
#
17
18
"""Tests with "big" files.
19
20
These are meant to ensure that Breezy never keeps full copies of files in
21
memory.
22
"""
23
7331.4.4 by Jelmer Vernooij
Skip big file test if there is not enough disk space.
24
import errno
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
25
import os
26
import resource
7490.28.3 by Jelmer Vernooij
Use xrange on Python 2, to avoid running out of memory.
27
import sys
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
28
29
from breezy import (
30
    osutils,
31
    tests,
32
    )
33
from breezy.tests import (
34
    features,
35
    script,
36
    )
37
38
BIG_FILE_SIZE = 1024 * 1024 * 500
7455.1.2 by Jelmer Vernooij
Reduce chunk size.
39
BIG_FILE_CHUNK_SIZE = 1024
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
40
7331.4.3 by Jelmer Vernooij
use RLIMIT_AS rather than RLIMIT_DATA.
41
RESOURCE = resource.RLIMIT_AS
42
LIMIT = 1024 * 1024 * 100
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
43
7490.28.3 by Jelmer Vernooij
Use xrange on Python 2, to avoid running out of memory.
44
if sys.version_info[0] == 2:
45
    range = xrange
46
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
47
48
def make_big_file(path):
49
    blob_1mb = BIG_FILE_CHUNK_SIZE * b'\x0c'
7455.1.2 by Jelmer Vernooij
Reduce chunk size.
50
    fd = os.open(path, os.O_CREAT | os.O_WRONLY)
7455.1.1 by Jelmer Vernooij
Use lower level file IO, so we don't get any caching.
51
    try:
7331.3.2 by Jelmer Vernooij
Fix tests on Python3.
52
        for i in range(BIG_FILE_SIZE // BIG_FILE_CHUNK_SIZE):
7455.1.1 by Jelmer Vernooij
Use lower level file IO, so we don't get any caching.
53
            os.write(fd, blob_1mb)
54
    finally:
55
        os.close(fd)
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
56
57
58
class TestAdd(tests.TestCaseWithTransport):
59
60
    def writeBigFile(self, path):
61
        self.addCleanup(os.unlink, path)
7331.4.4 by Jelmer Vernooij
Skip big file test if there is not enough disk space.
62
        try:
63
            make_big_file(path)
64
        except EnvironmentError as e:
65
            if e.errno == errno.ENOSPC:
66
                self.skipTest('not enough disk space for big file')
7331.3.1 by Jelmer Vernooij
Add some tests with big files.
67
68
    def setUp(self):
69
        super(TestAdd, self).setUp()
70
        previous = resource.getrlimit(RESOURCE)
71
        self.addCleanup(resource.setrlimit, RESOURCE, previous)
72
        resource.setrlimit(RESOURCE, (LIMIT, -1))
73
74
    def test_allocate(self):
75
        def allocate():
76
            "." * BIG_FILE_SIZE
77
        self.assertRaises(MemoryError, allocate)
78
79
    def test_add(self):
80
        tree = self.make_branch_and_tree('tree')
81
        self.writeBigFile(os.path.join(tree.basedir, 'testfile'))
82
        tree.add('testfile')
83
84
    def test_make_file_big(self):
85
        self.knownFailure('commit keeps entire files in memory')
86
        tree = self.make_branch_and_tree('tree')
87
        self.build_tree(['tree/testfile'])
88
        tree.add('testfile')
89
        tree.commit('add small file')
90
        self.writeBigFile(os.path.join(tree.basedir, 'testfile'))
91
        tree.commit('small files get big')
92
        self.knownFailure('commit keeps entire files in memory')
93
94
    def test_commit(self):
95
        self.knownFailure('commit keeps entire files in memory')
96
        tree = self.make_branch_and_tree('tree')
97
        self.writeBigFile(os.path.join(tree.basedir, 'testfile'))
98
        tree.add('testfile')
99
        tree.commit('foo')
100
101
    def test_clone(self):
102
        self.knownFailure('commit keeps entire files in memory')
103
        tree = self.make_branch_and_tree('tree')
104
        self.writeBigFile(os.path.join(tree.basedir, 'testfile'))
105
        tree.add('testfile')
106
        tree.commit('foo')
107
        tree.clone.sprout('newtree')