Skip to content

Commit

Permalink
Fix JMP_LEASE handling in pytest
Browse files Browse the repository at this point in the history
1) Look for JMP_LEASE inside the client.lease methods
2) Only release runtime created leases, leases received via JMP_LEASE
   are kept, because those must be manually released.
  • Loading branch information
mangelajo committed Jan 8, 2025
1 parent 9b31895 commit 6b2d2a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
12 changes: 2 additions & 10 deletions jumpstarter/cli/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import logging
import os
from typing import Optional

import click

from jumpstarter.common import MetadataFilter
from jumpstarter.common.utils import launch_shell
from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers, UserConfigV1Alpha1, env
from jumpstarter.config import ClientConfigV1Alpha1, ClientConfigV1Alpha1Drivers, UserConfigV1Alpha1

from .util import AliasedGroup, make_table
from .version import version
Expand Down Expand Up @@ -223,14 +222,7 @@ def client_shell(name: str, labels, lease_name):
if not config:
raise ValueError("no client specified")

# lease_name can be provided as an argument or via environment variable
lease_name = lease_name or os.environ.get(env.JMP_LEASE, "")

# when no lease name is provided, release the lease on exit
release_lease = lease_name == ""

with config.lease(metadata_filter=MetadataFilter(labels=dict(labels)), lease_name=lease_name,
release=release_lease) as lease:
with config.lease(metadata_filter=MetadataFilter(labels=dict(labels)), lease_name=lease_name) as lease:
with lease.serve_unix() as path:
launch_shell(path, config.drivers.allow, config.drivers.unsafe)

Expand Down
18 changes: 11 additions & 7 deletions jumpstarter/config/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from jumpstarter.v1 import jumpstarter_pb2, jumpstarter_pb2_grpc

from .common import CONFIG_PATH
from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_TOKEN
from .env import JMP_DRIVERS_ALLOW, JMP_ENDPOINT, JMP_LEASE, JMP_TOKEN
from .tls import TLSConfigV1Alpha1


Expand Down Expand Up @@ -56,10 +56,10 @@ async def channel(self):
return aio_secure_channel(self.endpoint, credentials)

@contextmanager
def lease(self, metadata_filter: MetadataFilter, lease_name: str | None, release: bool = False):
def lease(self, metadata_filter: MetadataFilter, lease_name: str | None):
with start_blocking_portal() as portal:
with portal.wrap_async_context_manager(
self.lease_async(metadata_filter, lease_name, portal, release)) as lease:
self.lease_async(metadata_filter, lease_name, portal)) as lease:
yield lease

def request_lease(self, metadata_filter: MetadataFilter):
Expand Down Expand Up @@ -97,18 +97,22 @@ async def release_lease_async(self, name):
await controller.ReleaseLease(jumpstarter_pb2.ReleaseLeaseRequest(name=name))

@asynccontextmanager
async def lease_async(self, metadata_filter: MetadataFilter, lease_name: str | None, portal: BlockingPortal,
release=True):
# dynamically import to avoid circular imports
async def lease_async(self, metadata_filter: MetadataFilter, lease_name: str | None, portal: BlockingPortal):
from jumpstarter.client import Lease

# if no lease_name provided, check if it is set in the environment
lease_name = lease_name or os.environ.get(JMP_LEASE, "")
# when no lease name is provided, release the lease on exit
release_lease = lease_name == ""

async with Lease(
channel=await self.channel(),
name=lease_name,
metadata_filter=metadata_filter,
portal=portal,
allow=self.drivers.allow,
unsafe=self.drivers.unsafe,
release=release,
release=release_lease,
tls_config=self.tls,
) as lease:
yield lease
Expand Down
6 changes: 1 addition & 5 deletions jumpstarter/testing/pytest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
import time
from typing import ClassVar

Expand Down Expand Up @@ -59,10 +58,7 @@ def client(self):
except RuntimeError:
labels = getattr(self, "filter_labels", {})
config = ClientConfigV1Alpha1.load("default")
lease_name = os.getenv(env.JMP_LEASE, None)
if lease_name is not None:
log.info(f"Using lease from env var JMP_LEASE: {lease_name}")
with config.lease(metadata_filter=MetadataFilter(labels=labels), lease_name=lease_name) as lease:
with config.lease(metadata_filter=MetadataFilter(labels=labels)) as lease:
with lease.connect() as client:
yield client
# BUG workaround: make sure that grpc servers get the client/lease release properly
Expand Down

0 comments on commit 6b2d2a2

Please sign in to comment.