File: test_storage.py

package info (click to toggle)
python-securesystemslib 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,316 kB
  • sloc: python: 5,319; sh: 38; makefile: 5
file content (114 lines) | stat: -rw-r--r-- 3,829 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
"""
<Program Name>
  test_storage.py

<Author>
  Joshua Lock <jlock@vmware.com>

<Started>
  April 17, 2020

<Copyright>
  See LICENSE for licensing information.

<Purpose>
  Unit test for 'storage.py'
"""

import os
import shutil
import stat
import tempfile
import unittest
from pathlib import Path

import securesystemslib.storage
from securesystemslib.exceptions import StorageError


class TestStorage(unittest.TestCase):
    def setUp(self):
        self.storage_backend = securesystemslib.storage.FilesystemBackend()
        self.temp_dir = tempfile.mkdtemp(dir=os.getcwd())
        self.filepath = os.path.join(self.temp_dir, "testfile")
        with open(self.filepath, "wb") as test:
            test.write(b"testing")
        self.fileobj = open(self.filepath, "rb")

    def tearDown(self):
        self.fileobj.close()
        shutil.rmtree(self.temp_dir)

    def test_exceptions(self):
        invalid_path = ""
        non_existent_path = Path(self.temp_dir) / "not_existent"
        self.assertFalse(non_existent_path.exists())

        with self.assertRaises(StorageError):
            with self.storage_backend.get(non_existent_path) as _:
                pass

        with self.assertRaises(StorageError):
            self.storage_backend.getsize(non_existent_path)

        with self.assertRaises(StorageError):
            self.storage_backend.list_folder(non_existent_path)

        with self.assertRaises(StorageError):
            self.storage_backend.create_folder(invalid_path)

    @unittest.skipIf(os.name == "nt", "n/a on Windows")
    def test_permission_exceptions(self):
        non_writable_path = Path(self.temp_dir) / "not_writable"
        os.mkdir(non_writable_path, mode=stat.S_IRUSR)

        with self.assertRaises(StorageError):
            self.storage_backend.put(self.fileobj, non_writable_path / "new")

        with self.assertRaises(StorageError):
            self.storage_backend.create_folder(non_writable_path / "new")

    def test_files(self):
        with self.storage_backend.get(self.filepath) as get_fileobj:
            self.assertEqual(get_fileobj.read(), self.fileobj.read())

        self.assertEqual(
            self.storage_backend.getsize(self.filepath),
            os.path.getsize(self.filepath),
        )

        put_path = os.path.join(self.temp_dir, "put")
        with self.storage_backend.get(self.filepath) as get_fileobj:
            self.storage_backend.put(get_fileobj, put_path)
            self.fileobj.seek(0)
            with open(put_path, "rb") as put_file:
                self.assertEqual(put_file.read(), self.fileobj.read())

        self.assertTrue(os.path.exists(put_path))
        self.storage_backend.remove(put_path)
        self.assertFalse(os.path.exists(put_path))

    def test_folders(self):
        leaves = ["test1", "test2", "test3"]
        folder = os.path.join(self.temp_dir, "test_dir")
        self.storage_backend.create_folder(folder)
        for leaf in leaves:
            with open(os.path.join(folder, leaf), "wb") as fi:
                fi.write(leaf.encode("utf-8"))
        found_leaves = self.storage_backend.list_folder(folder)
        self.assertListEqual(leaves, sorted(found_leaves))

    def test_singleton(self):
        # There should only ever be a single instance of FilesystemBackend.
        # An object's id is unique and constant for the object during its
        # lifetime. Therefore create more than one instance of FilesystemBackend
        # and compare their id's
        fb1 = securesystemslib.storage.FilesystemBackend()
        fb2 = securesystemslib.storage.FilesystemBackend()
        self.assertEqual(id(fb1), id(fb2))
        self.assertEqual(id(self.storage_backend), id(fb1))
        self.assertEqual(id(fb2), id(self.storage_backend))


if __name__ == "__main__":
    unittest.main()