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

chore: add jobRunAsUser integration test #54

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
157 changes: 149 additions & 8 deletions test/integ/test_job_submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,110 @@
Deadline Cloud service and checking that the result/output of the jobs is as we expect it.
"""

import boto3
import botocore.client
import botocore.config
import botocore.exceptions
import dataclasses

import pytest # noqa: F401

import logging

from typing import Generator

from deadline_test_fixtures import (
DeadlineClient,
DeadlineResources,
DeadlineWorkerConfiguration,
Farm,
Fleet,
Job,
PosixUser,
Queue,
QueueFleetAssociation,
TaskStatus,
)

LOG = logging.getLogger(__name__)


@pytest.mark.usefixtures("worker")
class TestJobSubmissions:
@pytest.fixture
def farm(self, deadline_resources: DeadlineResources) -> Farm:
return deadline_resources.farm
@pytest.fixture(scope="session")
def farm(deadline_resources: DeadlineResources) -> Farm:
return deadline_resources.farm


@pytest.fixture(scope="session")
def queue(deadline_resources: DeadlineResources) -> Queue:
return deadline_resources.queue


@pytest.fixture(scope="session")
def fleet(deadline_resources: DeadlineResources) -> Fleet:
return deadline_resources.fleet


@pytest.fixture(scope="session")
def job_run_as_user() -> PosixUser:
return PosixUser(
user="job-run-as-user",
group="job-run-as-user-group",
)


@pytest.fixture(scope="session")
def worker_config(
worker_config: DeadlineWorkerConfiguration,
job_run_as_user: PosixUser,
) -> DeadlineWorkerConfiguration:
return dataclasses.replace(
worker_config,
job_users=[
*worker_config.job_users,
job_run_as_user,
],
)

@pytest.fixture
def queue(self, deadline_resources: DeadlineResources) -> Queue:
return deadline_resources.queue

@pytest.fixture(scope="session")
def queue_with_job_run_as_user(
farm: Farm,
fleet: Fleet,
deadline_client: DeadlineClient,
job_run_as_user: PosixUser,
) -> Generator[Queue, None, None]:
queue = Queue.create(
client=deadline_client,
display_name=f"Queue with jobsRunAsUser {job_run_as_user.user}",
farm=farm,
raw_kwargs={
"jobRunAsUser": {
"posix": {
"user": job_run_as_user.user,
"group": job_run_as_user.group,
},
},
},
)

qfa = QueueFleetAssociation.create(
client=deadline_client,
farm=farm,
queue=queue,
fleet=fleet,
)

yield queue

qfa.delete(
client=deadline_client,
stop_mode="STOP_SCHEDULING_AND_CANCEL_TASKS",
)
queue.delete(client=deadline_client)


@pytest.mark.usefixtures("worker")
class TestJobSubmission:
def test_success(
self,
deadline_client: DeadlineClient,
Expand Down Expand Up @@ -60,3 +138,66 @@ def test_success(
LOG.info(f"Job result: {job}")

assert job.task_run_status == TaskStatus.SUCCEEDED

def test_jobs_run_as_user(
self,
deadline_client: DeadlineClient,
farm: Farm,
queue_with_job_run_as_user: Queue,
job_run_as_user: PosixUser,
) -> None:
# WHEN
job = Job.submit(
client=deadline_client,
farm=farm,
queue=queue_with_job_run_as_user,
priority=98,
template={
"specificationVersion": "jobtemplate-2023-09",
"name": "whoami",
"steps": [
{
"name": "Step0",
"script": {
"embeddedFiles": [
{
"name": "whoami",
"type": "TEXT",
"runnable": True,
"data": "\n".join(
[
"#!/bin/bash",
'echo "I am: $(whoami)"',
]
),
},
],
"actions": {
"onRun": {
"command": "{{ Task.File.whoami }}",
},
},
},
},
],
},
)

# THEN
job.wait_until_complete(client=deadline_client)

# Retrieve job output and verify whoami printed the queue's jobsRunAsUser
job_logs = job.get_logs(
deadline_client=deadline_client,
logs_client=boto3.client(
"logs",
config=botocore.config.Config(retries={"max_attempts": 10, "mode": "adaptive"}),
),
)
full_log = "\n".join(
[le.message for _, log_events in job_logs.logs.items() for le in log_events]
)
assert (
f"I am: {job_run_as_user.user}" in full_log
), f"Expected message not found in Job logs. Logs are in CloudWatch log group: {job_logs.log_group_name}"
assert job.task_run_status == TaskStatus.SUCCEEDED