File: test_file.py

package info (click to toggle)
python-stestr 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: python: 6,348; makefile: 37; sh: 12
file content (212 lines) | stat: -rw-r--r-- 8,287 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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.

"""Tests for the file repository implementation."""

import os.path
import shutil
import tempfile

import fixtures
import testtools
from testtools import matchers

from stestr.repository import file
from stestr.tests import base


class FileRepositoryFixture(fixtures.Fixture):
    def __init__(self, path=None, initialise=True):
        super().__init__()
        self.path = path
        self.initialise = initialise

    def setUp(self):
        super().setUp()
        if self.path and os.path.isdir(self.path):
            self.tempdir = self.path
        else:
            self.tempdir = tempfile.mkdtemp()
            self.addCleanup(shutil.rmtree, self.tempdir)
        if self.initialise:
            self.repo = file.RepositoryFactory().initialise(self.tempdir)


class HomeDirTempDir(fixtures.Fixture):
    """Creates a temporary directory in ~."""

    def setUp(self):
        super().setUp()
        home_dir = os.path.expanduser("~")
        self.temp_dir = tempfile.mkdtemp(dir=home_dir)
        self.addCleanup(shutil.rmtree, self.temp_dir)
        self.short_path = os.path.join("~", os.path.basename(self.temp_dir))


class TestFileRepository(base.TestCase):
    def setUp(self):
        super().setUp()
        self.tempdir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, self.tempdir)

    def test_initialise(self):
        self.useFixture(FileRepositoryFixture(path=self.tempdir))
        base = os.path.join(self.tempdir, ".stestr")
        self.assertTrue(os.path.isdir(base))
        self.assertTrue(os.path.isfile(os.path.join(base, "format")))
        with open(os.path.join(base, "format")) as stream:
            contents = stream.read()
        self.assertEqual("1\n", contents)
        with open(os.path.join(base, "next-stream")) as stream:
            contents = stream.read()
        self.assertEqual("0\n", contents)

    def test_initialise_empty_dir(self):
        self.useFixture(FileRepositoryFixture(path=self.tempdir, initialise=False))
        base = os.path.join(self.tempdir, ".stestr")
        os.mkdir(base)
        self.assertFalse(os.path.isfile(os.path.join(base, "format")))
        self.repo = file.RepositoryFactory().initialise(self.tempdir)
        self.assertTrue(os.path.isdir(base))
        self.assertTrue(os.path.isfile(os.path.join(base, "format")))
        with open(os.path.join(base, "format")) as stream:
            contents = stream.read()
        self.assertEqual("1\n", contents)
        with open(os.path.join(base, "next-stream")) as stream:
            contents = stream.read()
        self.assertEqual("0\n", contents)

    def test_initialise_non_empty_dir(self):
        self.useFixture(FileRepositoryFixture(path=self.tempdir, initialise=False))
        base = os.path.join(self.tempdir, ".stestr")
        os.mkdir(base)
        with open(os.path.join(base, "foo"), "wt") as stream:
            stream.write("1\n")
        factory = file.RepositoryFactory()
        self.assertRaises(OSError, factory.initialise, self.tempdir)

    # Skip if windows since ~ in a path doesn't work there
    @testtools.skipIf(os.name == "nt", "Windows doesn't support '~' expand")
    def test_initialise_expands_user_directory(self):
        short_path = self.useFixture(HomeDirTempDir()).short_path
        repo = file.RepositoryFactory().initialise(short_path)
        self.assertTrue(os.path.exists(repo.base))

    def test_inserter_output_path(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        inserter = repo.get_inserter()
        inserter.startTestRun()
        inserter.stopTestRun()
        self.assertTrue(os.path.exists(os.path.join(repo.base, "0")))

    def test_inserting_creates_id(self):
        # When inserting a stream, an id is returned from stopTestRun.
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        self.assertEqual(0, result.get_id())

    # Skip if windows since ~ in a path doesn't work there
    @testtools.skipIf(os.name == "nt", "Windows doesn't support '~' expand")
    def test_open_expands_user_directory(self):
        short_path = self.useFixture(HomeDirTempDir()).short_path
        repo1 = file.RepositoryFactory().initialise(short_path)
        repo2 = file.RepositoryFactory().open(short_path)
        self.assertEqual(repo1.base, repo2.base)

    def test_next_stream_corruption_error(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        open(os.path.join(repo.base, "next-stream"), "wb").close()
        self.assertThat(
            repo.count,
            matchers.Raises(
                matchers.MatchesException(ValueError("Corrupt next-stream file: ''"))
            ),
        )

    # Skip if windows since chmod doesn't work there
    @testtools.skipIf(os.name == "nt", "Windows doesn't support chmod")
    def test_get_test_run_unexpected_ioerror_errno(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        inserter = repo.get_inserter()
        inserter.startTestRun()
        inserter.stopTestRun()
        self.assertTrue(os.path.isfile(os.path.join(repo.base, "0")))
        os.chmod(os.path.join(repo.base, "0"), 0000)
        self.assertRaises(IOError, repo.get_test_run, "0")

    def test_get_metadata(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter(metadata="fun")
        result.startTestRun()
        result.stopTestRun()
        run = repo.get_test_run(result.get_id())
        self.assertEqual(b"fun", run.get_metadata())

    def test_find_metadata(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter(metadata="fun")
        result.startTestRun()
        result.stopTestRun()
        result_bad = repo.get_inserter(metadata="not_fun")
        result_bad.startTestRun()
        result_bad.stopTestRun()
        run_ids = repo.find_metadata(b"fun")
        run_ids_int = [int(x) for x in run_ids]
        self.assertIn(result.get_id(), run_ids_int)
        self.assertNotIn(result_bad.get_id(), run_ids_int)

    def test_get_run_ids(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter(metadata="fun")
        result.startTestRun()
        result.stopTestRun()
        result_bad = repo.get_inserter(metadata="not_fun")
        result_bad.startTestRun()
        result_bad.stopTestRun()
        run_ids = repo.get_run_ids()
        self.assertEqual(["0", "1"], run_ids)

    def test_get_run_ids_empty(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        run_ids = repo.get_run_ids()
        self.assertEqual([], run_ids)

    def test_get_run_ids_with_hole(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        repo.remove_run_id("1")
        self.assertEqual(["0", "2"], repo.get_run_ids())

    def test_remove_ids(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        repo.remove_run_id("0")
        self.assertEqual([], repo.get_run_ids())

    def test_remove_ids_id_not_in_repo(self):
        repo = self.useFixture(FileRepositoryFixture()).repo
        result = repo.get_inserter()
        result.startTestRun()
        result.stopTestRun()
        self.assertRaises(KeyError, repo.remove_run_id, "3")