Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from_env: Use default base_url if no environment is given #372

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions podman/api/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,8 @@ class AsyncContextManager(

__all__.append('AsyncContextManager')
elif sys.version_info[:2] >= (3, 5):
exec("""
exec(
"""
class AsyncContextManager(typing.Generic[T_co]):
__slots__ = ()

Expand All @@ -970,7 +971,8 @@ def __subclasshook__(cls, C):
return NotImplemented

__all__.append('AsyncContextManager')
""")
"""
)

if hasattr(typing, 'DefaultDict'):
DefaultDict = typing.DefaultDict
Expand Down
23 changes: 12 additions & 11 deletions podman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,19 @@ def from_env(
if version == "auto":
version = None

kwargs = {
'version': version,
'timeout': timeout,
'tls': False,
'credstore_env': credstore_env,
'max_pool_size': max_pool_size,
}

host = environment.get("CONTAINER_HOST") or environment.get("DOCKER_HOST") or None
if host is None:
raise ValueError("CONTAINER_HOST or DOCKER_HOST must be set to URL of podman service.")

return PodmanClient(
base_url=host,
version=version,
timeout=timeout,
tls=False,
credstore_env=credstore_env,
max_pool_size=max_pool_size,
)
if host is not None:
kwargs['base_url'] = host

return PodmanClient(**kwargs)

@cached_property
def containers(self) -> ContainersManager:
Expand Down
6 changes: 2 additions & 4 deletions podman/tests/integration/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ def test_login(self):
)

def test_from_env(self):
"""integration: from_env() error message"""
with self.assertRaises(ValueError) as e:
next(self.client.from_env())
self.assertIn("CONTAINER_HOST or DOCKER_HOST", repr(e.exception))
"""integration: from_env() no error"""
PodmanClient.from_env()
3 changes: 1 addition & 2 deletions podman/tests/unit/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def test_build(self, mock_prepare_containerfile, mock_create_tar):

with requests_mock.Mocker() as mock:
mock.post(
tests.LIBPOD_URL
+ "/build"
tests.LIBPOD_URL + "/build"
"?t=latest"
"&buildargs=%7B%22BUILD_DATE%22%3A+%22January+1%2C+1970%22%7D"
"&cpuperiod=10"
Expand Down
6 changes: 4 additions & 2 deletions podman/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@


class PodmanConfigTestCase(unittest.TestCase):
opener = mock.mock_open(read_data="""
opener = mock.mock_open(
read_data="""
[containers]
log_size_max = -1
pids_limit = 2048
Expand All @@ -27,7 +28,8 @@ class PodmanConfigTestCase(unittest.TestCase):
identity = "/home/qe/.ssh/id_rsa"

[network]
""")
"""
)

def setUp(self) -> None:
super().setUp()
Expand Down
36 changes: 12 additions & 24 deletions podman/tests/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def tearDown(self) -> None:
@requests_mock.Mocker()
def test_remove(self, mock):
adapter = mock.delete(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd?v=True&force=True",
status_code=204,
)
Expand Down Expand Up @@ -71,8 +70,7 @@ def test_rename_type_error(self, mock):
@requests_mock.Mocker()
def test_restart(self, mock):
adapter = mock.post(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/restart?timeout=10",
status_code=204,
)
Expand All @@ -83,8 +81,7 @@ def test_restart(self, mock):
@requests_mock.Mocker()
def test_start_dkeys(self, mock):
adapter = mock.post(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/start"
"?detachKeys=%5Ef%5Eu",
status_code=204,
Expand Down Expand Up @@ -120,8 +117,7 @@ def test_stats(self, mock):
buffer.write("\n")

adapter = mock.get(
tests.LIBPOD_URL
+ "/containers/stats"
tests.LIBPOD_URL + "/containers/stats"
"?containers=87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd"
"&stream=True",
text=buffer.getvalue(),
Expand All @@ -143,8 +139,7 @@ def test_stats(self, mock):
@requests_mock.Mocker()
def test_stop(self, mock):
adapter = mock.post(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/stop"
"?all=True&timeout=10.0",
status_code=204,
Expand Down Expand Up @@ -173,8 +168,7 @@ def test_stop_304(self, mock):
@requests_mock.Mocker()
def test_unpause(self, mock):
adapter = mock.post(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/unpause",
status_code=204,
)
Expand Down Expand Up @@ -225,8 +219,7 @@ def test_diff(self, mock):
{"Path": "deleted", "Kind": 2},
]
adapter = mock.get(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/changes",
json=payload,
)
Expand All @@ -238,8 +231,7 @@ def test_diff(self, mock):
@requests_mock.Mocker()
def test_diff_404(self, mock):
adapter = mock.get(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/changes",
json={
"cause": "Container not found.",
Expand Down Expand Up @@ -284,8 +276,7 @@ def test_get_archive(self, mock):
encoded_value = base64.urlsafe_b64encode(json.dumps(header_value).encode("utf8"))

adapter = mock.get(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/archive"
"?path=/etc/motd",
body=body,
Expand All @@ -306,8 +297,7 @@ def test_get_archive(self, mock):
@requests_mock.Mocker()
def test_commit(self, mock):
post_adapter = mock.post(
tests.LIBPOD_URL
+ "/commit"
tests.LIBPOD_URL + "/commit"
"?author=redhat&changes=ADD+%2fetc%2fmod&comment=This+is+a+unittest"
"&container=87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd&format=docker"
"&pause=True&repo=quay.local&tag=unittest",
Expand Down Expand Up @@ -340,8 +330,7 @@ def test_commit(self, mock):
@requests_mock.Mocker()
def test_put_archive(self, mock):
adapter = mock.put(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/archive"
"?path=%2fetc%2fmotd",
status_code=200,
Expand All @@ -357,8 +346,7 @@ def test_put_archive(self, mock):
@requests_mock.Mocker()
def test_put_archive_404(self, mock):
adapter = mock.put(
tests.LIBPOD_URL
+ "/containers/"
tests.LIBPOD_URL + "/containers/"
"87e1325c82424e49a00abdd4de08009eb76c7de8d228426a9b8af9318ced5ecd/archive"
"?path=deadbeef",
status_code=404,
Expand Down
3 changes: 1 addition & 2 deletions podman/tests/unit/test_containersmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def test_list(self, mock):
@requests_mock.Mocker()
def test_list_filtered(self, mock):
mock.get(
tests.LIBPOD_URL
+ "/containers/json?"
tests.LIBPOD_URL + "/containers/json?"
"all=True"
"&filters=%7B"
"%22before%22%3A"
Expand Down
12 changes: 4 additions & 8 deletions podman/tests/unit/test_imagesmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ def test_pull(self, mock):
},
)
mock.get(
tests.LIBPOD_URL
+ "/images"
tests.LIBPOD_URL + "/images"
"/sha256%3A326dd9d7add24646a325e8eaa82125294027db2332e49c5828d96312c5d773ab/json",
json=FIRST_IMAGE,
)
Expand All @@ -479,8 +478,7 @@ def test_pull_enhanced(self, mock):
},
)
mock.get(
tests.LIBPOD_URL
+ "/images"
tests.LIBPOD_URL + "/images"
"/sha256%3A326dd9d7add24646a325e8eaa82125294027db2332e49c5828d96312c5d773ab/json",
json=FIRST_IMAGE,
)
Expand All @@ -501,8 +499,7 @@ def test_pull_platform(self, mock):
},
)
mock.get(
tests.LIBPOD_URL
+ "/images"
tests.LIBPOD_URL + "/images"
"/sha256%3A326dd9d7add24646a325e8eaa82125294027db2332e49c5828d96312c5d773ab/json",
json=FIRST_IMAGE,
)
Expand All @@ -527,8 +524,7 @@ def test_pull_2x(self, mock):
},
)
mock.get(
tests.LIBPOD_URL
+ "/images"
tests.LIBPOD_URL + "/images"
"/sha256%3A326dd9d7add24646a325e8eaa82125294027db2332e49c5828d96312c5d773ab/json",
json=FIRST_IMAGE,
)
Expand Down
3 changes: 1 addition & 2 deletions podman/tests/unit/test_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ def test_top(self, mock):
"Titles": ["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME CMD"],
}
adapter = mock.get(
tests.LIBPOD_URL
+ "/pods"
tests.LIBPOD_URL + "/pods"
"/c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8/top"
"?ps_args=aux&stream=False",
json=body,
Expand Down
6 changes: 4 additions & 2 deletions podman/tests/unit/test_podmanclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
class PodmanClientTestCase(unittest.TestCase):
"""Test the PodmanClient() object."""

opener = mock.mock_open(read_data="""
opener = mock.mock_open(
read_data="""
[containers]
log_size_max = -1
pids_limit = 2048
Expand All @@ -32,7 +33,8 @@ class PodmanClientTestCase(unittest.TestCase):
identity = "/home/qe/.ssh/id_rsa"

[network]
""")
"""
)

def setUp(self) -> None:
super().setUp()
Expand Down
6 changes: 2 additions & 4 deletions podman/tests/unit/test_podsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ def test_stats(self, mock):
"Titles": ["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME CMD"],
}
mock.get(
tests.LIBPOD_URL
+ "/pods/stats"
tests.LIBPOD_URL + "/pods/stats"
"?namesOrIDs=c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
json=body,
)
Expand Down Expand Up @@ -180,8 +179,7 @@ def test_stats_without_decode(self, mock):
"Titles": ["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME CMD"],
}
mock.get(
tests.LIBPOD_URL
+ "/pods/stats"
tests.LIBPOD_URL + "/pods/stats"
"?namesOrIDs=c8b9f5b17dc1406194010c752fc6dcb330192032e27648db9b14060447ecf3b8",
json=body,
)
Expand Down
Loading