File: test_local.py

package info (click to toggle)
proxmoxer 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 432 kB
  • sloc: python: 3,214; sh: 12; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,630 bytes parent folder | download
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
__author__ = "John Hollowell"
__copyright__ = "(c) John Hollowell 2022"
__license__ = "MIT"

import tempfile

from proxmoxer.backends import local

# pylint: disable=no-self-use


class TestLocalBackend:
    def test_init(self):
        back = local.Backend()

        assert isinstance(back.session, local.LocalSession)
        assert back.target == "localhost"


class TestLocalSession:
    _session = local.LocalSession()

    def test_upload_file_obj(self):
        size = 100

        with tempfile.NamedTemporaryFile("w+b") as f_obj, tempfile.NamedTemporaryFile(
            "rb"
        ) as dest_obj:
            f_obj.write(b"a" * size)
            f_obj.seek(0)
            self._session.upload_file_obj(f_obj, dest_obj.name)

            # reset file cursor to start of file after copy
            f_obj.seek(0)

            assert f_obj.read() == dest_obj.read()

    def test_upload_file_obj_end(self):
        size = 100

        with tempfile.NamedTemporaryFile("w+b") as f_obj, tempfile.NamedTemporaryFile(
            "rb"
        ) as dest_obj:
            f_obj.write(b"a" * size)
            # do not seek to start of file before copy
            self._session.upload_file_obj(f_obj, dest_obj.name)

            assert b"" == dest_obj.read()

    def test_exec(self):
        cmd = [
            "python3",
            "-c",
            'import sys; sys.stdout.write("stdout content"); sys.stderr.write("stderr content")',
        ]

        stdout, stderr, exit_code = self._session._exec(cmd)

        assert stdout == "stdout content"
        assert stderr == "stderr content"
        assert exit_code == 0