summaryrefslogtreecommitdiff
path: root/obnamlib/repo_factory_tests.py
blob: c59009423004a0e3624297b5c8e32f995ae5bbcd (plain)
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
# Copyright 2013,2015  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import os
import shutil
import tempfile
import unittest

import obnamlib


class RepositoryFormatTests(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.repodir = os.path.join(self.tempdir, 'repo')
        os.mkdir(self.repodir)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def test_returns_list_of_implementation_classes(self):
        factory = obnamlib.RepositoryFactory()
        self.assertEqual(type(factory.get_implementation_classes()), list)

    def test_raises_exception_for_unknown_format(self):
        fs = obnamlib.LocalFS(self.repodir)
        fs.write_file('metadata/format', 'unknown')
        factory = obnamlib.RepositoryFactory()
        self.assertRaises(
            obnamlib.UnknownRepositoryFormat,
            factory.open_existing_repo,
            fs)

    def test_accepts_good_format(self):
        good = obnamlib.RepositoryFormat6
        fs = obnamlib.LocalFS(self.repodir)
        fs.write_file('metadata/format', good.format)
        factory = obnamlib.RepositoryFactory()
        repo = factory.open_existing_repo(fs)
        self.assertTrue(isinstance(repo, good))

    def test_creates_a_new_repository(self):
        good = obnamlib.RepositoryFormat6
        fs = obnamlib.LocalFS(self.repodir)
        factory = obnamlib.RepositoryFactory()
        repo = factory.create_repo(fs, good)
        self.assertTrue(isinstance(repo, good))

    def test_raises_error_when_unknown_format_requested(self):
        fs = obnamlib.LocalFS(self.repodir)
        factory = obnamlib.RepositoryFactory()
        self.assertRaises(
            obnamlib.UnknownRepositoryFormatWanted,
            factory.create_repo, fs, int)

    def test_create_repo_is_ok_with_existing_repo(self):
        good = obnamlib.RepositoryFormat6
        fs = obnamlib.LocalFS(self.repodir)
        fs.write_file('metadata/format', good.format)
        factory = obnamlib.RepositoryFactory()
        repo = factory.create_repo(fs, good)
        self.assertTrue(isinstance(repo, good))

    def test_create_repo_twice_is_ok(self):
        good = obnamlib.RepositoryFormat6
        fs = obnamlib.LocalFS(self.repodir)
        factory = obnamlib.RepositoryFactory()
        repo = factory.create_repo(fs, good)
        self.assertTrue(isinstance(repo, good))
        repo2 = factory.create_repo(fs, good)
        self.assertTrue(isinstance(repo2, good))