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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
import io
import json
import unittest
from collections.abc import Iterable
import requests_mock
from podman import PodmanClient, tests
from podman.domain.pods import Pod
from podman.domain.pods_manager import PodsManager
from podman.errors import NotFound
FIRST_POD = {
"ID": "c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
"Name": "blog-nginx",
}
SECOND_POD = {
"ID": "c847d00ed0474835a2e246f00e90346fe98d388f98064f4494953c5fb921b8bc",
"Name": "podman",
}
class PodsManagerTestCase(unittest.TestCase):
def setUp(self) -> None:
super().setUp()
self.client = PodmanClient(base_url=tests.BASE_SOCK)
def tearDown(self) -> None:
super().tearDown()
self.client.close()
def test_podmanclient(self):
manager = self.client.pods
self.assertIsInstance(manager, PodsManager)
@requests_mock.Mocker()
def test_create(self, mock):
adapter = mock.post(
tests.LIBPOD_URL + "/pods/create",
json={"Id": "c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8"},
status_code=201,
)
mock.get(
tests.LIBPOD_URL
+ "/pods/c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8/json",
json=FIRST_POD,
)
actual = self.client.pods.create(name="database")
self.assertIsInstance(actual, Pod)
self.assertTrue(adapter.called_once)
self.assertDictEqual(adapter.last_request.json(), {"name": "database"})
@requests_mock.Mocker()
def test_get(self, mock):
mock.get(
tests.LIBPOD_URL
+ "/pods/c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8/json",
json=FIRST_POD,
)
actual = self.client.pods.get(
"c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8"
)
self.assertEqual(
actual.id, "c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8"
)
@requests_mock.Mocker()
def test_get404(self, mock):
mock.get(
tests.LIBPOD_URL
+ "/pods/c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8/json",
status_code=404,
json={
"cause": "no such pod",
"message": (
"no pod with name or ID "
"c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8"
" found: no such pod"
),
"response": 404,
},
)
with self.assertRaises(NotFound):
self.client.pods.get("c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8")
@requests_mock.Mocker()
def test_list(self, mock):
mock.get(tests.LIBPOD_URL + "/pods/json", json=[FIRST_POD, SECOND_POD])
actual = self.client.pods.list()
self.assertEqual(
actual[0].id, "c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8"
)
self.assertEqual(
actual[1].id, "c847d00ed0474835a2e246f00e90346fe98d388f98064f4494953c5fb921b8bc"
)
@requests_mock.Mocker()
def test_prune(self, mock):
adapter = mock.post(
tests.LIBPOD_URL + "/pods/prune",
json=[
{
"Err": None,
"Id": "c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
},
{
"Err": None,
"Id": "c847d00ed0474835a2e246f00e90346fe98d388f98064f4494953c5fb921b8bc",
},
],
)
actual = self.client.pods.prune()
self.assertTrue(adapter.called_once)
self.assertListEqual(
actual["PodsDeleted"],
[
"c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
"c847d00ed0474835a2e246f00e90346fe98d388f98064f4494953c5fb921b8bc",
],
)
self.assertEqual(actual["SpaceReclaimed"], 0)
@requests_mock.Mocker()
def test_stats(self, mock):
body = {
"Processes": [
[
'jhonce',
'2417',
'2274',
'0',
'Mar01',
'?',
'00:00:01',
'/usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/gnome-session"',
],
['jhonce', '5544', '3522', '0', 'Mar01', 'pts/1', '00:00:02', '-bash'],
['jhonce', '6140', '3522', '0', 'Mar01', 'pts/2', '00:00:00', '-bash'],
],
"Titles": ["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME CMD"],
}
mock.get(
tests.LIBPOD_URL + "/pods/stats"
"?namesOrIDs=c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
json=body,
)
actual = self.client.pods.stats(
name="c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
)
self.assertEqual(actual, json.dumps(body).encode())
@requests_mock.Mocker()
def test_stats_without_decode(self, mock):
body = {
"Processes": [
[
'jhonce',
'2417',
'2274',
'0',
'Mar01',
'?',
'00:00:01',
'/usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/gnome-session"',
],
['jhonce', '5544', '3522', '0', 'Mar01', 'pts/1', '00:00:02', '-bash'],
['jhonce', '6140', '3522', '0', 'Mar01', 'pts/2', '00:00:00', '-bash'],
],
"Titles": ["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME CMD"],
}
mock.get(
tests.LIBPOD_URL + "/pods/stats"
"?namesOrIDs=c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
json=body,
)
actual = self.client.pods.stats(
name="c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8", decode=True
)
self.assertDictEqual(actual, body)
@requests_mock.Mocker()
def test_top_with_streaming(self, mock):
stream = [
[
{
'CPU': '2.53%',
'MemUsage': '49.15kB / 16.71GB',
'MemUsageBytes': '48KiB / 15.57GiB',
'Mem': '0.00%',
'NetIO': '7.638kB / 430B',
'BlockIO': '-- / --',
'PIDS': '1',
'Pod': '1c948ab42339',
'CID': 'd999c49a7b6c',
'Name': '1c948ab42339-infra',
}
],
[
{
'CPU': '1.46%',
'MemUsage': '57.23B / 16.71GB',
'MemUsageBytes': '48KiB / 15.57GiB',
'Mem': '0.00%',
'NetIO': '7.638kB / 430B',
'BlockIO': '-- / --',
'PIDS': '1',
'Pod': '1c948ab42339',
'CID': 'd999c49a7b6c',
'Name': '1c948ab42339-infra',
}
],
]
buffer = io.StringIO()
for entry in stream:
buffer.write(json.JSONEncoder().encode(entry))
buffer.write("\n")
adapter = mock.get(
tests.LIBPOD_URL + "/pods/stats?stream=True",
text=buffer.getvalue(),
)
stream_results = self.client.pods.stats(stream=True, decode=True)
self.assertIsInstance(stream_results, Iterable)
for response, actual in zip(stream_results, stream):
self.assertIsInstance(response, list)
self.assertListEqual(response, actual)
self.assertTrue(adapter.called_once)
def test_stats_400(self):
with self.assertRaises(ValueError):
self.client.pods.stats(all=True, name="container")
if __name__ == '__main__':
unittest.main()
|