File: test_config.py

package info (click to toggle)
python-podman 5.4.0.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 1,140 kB
  • sloc: python: 7,532; makefile: 82; sh: 75
file content (170 lines) | stat: -rw-r--r-- 5,040 bytes parent folder | download | duplicates (3)
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
import unittest
import urllib.parse
import json
import os
import tempfile
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock
from podman.domain.config import PodmanConfig


class PodmanConfigTestCaseDefault(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()

        # Data to be written to the JSON file
        self.data_json = """
{
  "Connection": {
    "Default": "testing_json",
    "Connections": {
      "testing_json": {
        "URI": "ssh://qe@localhost:2222/run/podman/podman.sock",
        "Identity": "/home/qe/.ssh/id_rsa"
      },
      "production": {
        "URI": "ssh://root@localhost:22/run/podman/podman.sock",
        "Identity": "/home/root/.ssh/id_rsajson"
      }
    }
  },
  "Farm": {}
}
"""

        # Data to be written to the TOML file
        self.data_toml = """
[containers]
  log_size_max = -1
  pids_limit = 2048
  userns_size = 65536

[engine]
  num_locks = 2048
  active_service = "testing"
  stop_timeout = 10
  [engine.service_destinations]
    [engine.service_destinations.production]
      uri = "ssh://root@localhost:22/run/podman/podman.sock"
      identity = "/home/root/.ssh/id_rsa"
    [engine.service_destinations.testing]
      uri = "ssh://qe@localhost:2222/run/podman/podman.sock"
      identity = "/home/qe/.ssh/id_rsa"

[network]
"""

        # Define the file path
        self.path_json = os.path.join(self.temp_dir, 'podman-connections.json')
        self.path_toml = os.path.join(self.temp_dir, 'containers.conf')

        # Write data to the JSON file
        j_data = json.loads(self.data_json)
        with open(self.path_json, 'w+') as file_json:
            json.dump(j_data, file_json)

        # Write data to the TOML file
        with open(self.path_toml, 'w+') as file_toml:
            # toml.dump(self.data_toml, file_toml)
            file_toml.write(self.data_toml)

    def test_connections(self):
        config = PodmanConfig("@@is_test@@" + self.temp_dir)

        self.assertEqual(config.active_service.id, "testing_json")

        expected = urllib.parse.urlparse("ssh://qe@localhost:2222/run/podman/podman.sock")
        self.assertEqual(config.active_service.url, expected)
        self.assertEqual(config.services["production"].identity, Path("/home/root/.ssh/id_rsajson"))


class PodmanConfigTestCaseTOML(unittest.TestCase):
    opener = mock.mock_open(
        read_data="""
[containers]
  log_size_max = -1
  pids_limit = 2048
  userns_size = 65536

[engine]
  num_locks = 2048
  active_service = "testing"
  stop_timeout = 10
  [engine.service_destinations]
    [engine.service_destinations.production]
      uri = "ssh://root@localhost:22/run/podman/podman.sock"
      identity = "/home/root/.ssh/id_rsa"
    [engine.service_destinations.testing]
      uri = "ssh://qe@localhost:2222/run/podman/podman.sock"
      identity = "/home/qe/.ssh/id_rsa"

[network]
"""
    )

    def setUp(self) -> None:
        super().setUp()

        def mocked_open(self, *args, **kwargs):
            return PodmanConfigTestCaseTOML.opener(self, *args, **kwargs)

        self.mocked_open = mocked_open

    def test_connections(self):
        with mock.patch.multiple(Path, open=self.mocked_open, exists=MagicMock(return_value=True)):
            config = PodmanConfig("/home/developer/containers.conf")

            self.assertEqual(config.active_service.id, "testing")

            expected = urllib.parse.urlparse("ssh://qe@localhost:2222/run/podman/podman.sock")
            self.assertEqual(config.active_service.url, expected)
            self.assertEqual(config.services["production"].identity, Path("/home/root/.ssh/id_rsa"))

            PodmanConfigTestCaseTOML.opener.assert_called_with(
                Path("/home/developer/containers.conf"), encoding='utf-8'
            )


class PodmanConfigTestCaseJSON(unittest.TestCase):
    def setUp(self) -> None:
        super().setUp()

        self.temp_dir = tempfile.mkdtemp()
        self.data = """
{
  "Connection": {
    "Default": "testing",
    "Connections": {
      "testing": {
        "URI": "ssh://qe@localhost:2222/run/podman/podman.sock",
        "Identity": "/home/qe/.ssh/id_rsa"
      },
      "production": {
        "URI": "ssh://root@localhost:22/run/podman/podman.sock",
        "Identity": "/home/root/.ssh/id_rsa"
      }
    }
  },
  "Farm": {}
}
"""

        self.path = os.path.join(self.temp_dir, 'podman-connections.json')
        # Write data to the JSON file
        data = json.loads(self.data)
        with open(self.path, 'w+') as file:
            json.dump(data, file)

    def test_connections(self):
        config = PodmanConfig(self.path)

        self.assertEqual(config.active_service.id, "testing")

        expected = urllib.parse.urlparse("ssh://qe@localhost:2222/run/podman/podman.sock")
        self.assertEqual(config.active_service.url, expected)
        self.assertEqual(config.services["production"].identity, Path("/home/root/.ssh/id_rsa"))


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