mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Add context manager to ipalib.API
`ipalib.API` instances like `ipalib.api` now provide a context manager
that connects and disconnects the API object. Users no longer have to
deal with different types of backends or finalize the API correctly.
```python
import ipalib
with ipalib.api as api:
api.Commands.ping()
```
See: https://pagure.io/freeipa/issue/9443
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
This commit is contained in:
committed by
Florence Blanc-Renaud
parent
8b70ee1ea8
commit
6aebfe74fb
19
ipatests/test_integration/example_cli.py
Normal file
19
ipatests/test_integration/example_cli.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import os
|
||||
|
||||
import ipalib
|
||||
from ipaplatform.paths import paths
|
||||
|
||||
# authenticate with host keytab and custom ccache
|
||||
os.environ.update(
|
||||
KRB5_CLIENT_KTNAME=paths.KRB5_KEYTAB,
|
||||
)
|
||||
|
||||
# custom options
|
||||
overrides = {"context": "example_cli"}
|
||||
ipalib.api.bootstrap(**overrides)
|
||||
|
||||
with ipalib.api as api:
|
||||
user = api.Command.user_show("admin")
|
||||
print(user)
|
||||
|
||||
assert not api.Backend.rpcclient.isconnected()
|
||||
@@ -13,6 +13,7 @@ import random
|
||||
import shlex
|
||||
import ssl
|
||||
from itertools import chain, repeat
|
||||
import sys
|
||||
import textwrap
|
||||
import time
|
||||
import pytest
|
||||
@@ -1557,6 +1558,34 @@ class TestIPACommand(IntegrationTest):
|
||||
|
||||
assert 'Discovered server %s' % self.master.hostname in result
|
||||
|
||||
def test_ipa_context_manager(self):
|
||||
"""Exercise ipalib.api context manager and KRB5_CLIENT_KTNAME auth
|
||||
|
||||
The example_cli.py script uses the context manager to connect and
|
||||
disconnect the global ipalib.api object. The test also checks whether
|
||||
KRB5_CLIENT_KTNAME env var automatically acquires a TGT.
|
||||
"""
|
||||
host = self.clients[0]
|
||||
tasks.kdestroy_all(host)
|
||||
|
||||
here = os.path.abspath(os.path.dirname(__file__))
|
||||
with open(os.path.join(here, "example_cli.py")) as f:
|
||||
contents = f.read()
|
||||
|
||||
# upload script and run with Python executable
|
||||
script = "/tmp/example_cli.py"
|
||||
host.put_file_contents(script, contents)
|
||||
result = host.run_command([sys.executable, script])
|
||||
|
||||
# script prints admin account
|
||||
admin_princ = f"admin@{host.domain.realm}"
|
||||
assert admin_princ in result.stdout_text
|
||||
|
||||
# verify that auto-login did use correct principal
|
||||
host_princ = f"host/{host.hostname}@{host.domain.realm}"
|
||||
result = host.run_command([paths.KLIST])
|
||||
assert host_princ in result.stdout_text
|
||||
|
||||
|
||||
class TestIPACommandWithoutReplica(IntegrationTest):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user