Skip to content

Commit

Permalink
test: add linux job user override tests
Browse files Browse the repository at this point in the history
Signed-off-by: Li <[email protected]>
  • Loading branch information
YutongLi291 committed Nov 14, 2024
1 parent 31ba080 commit 7314f99
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 94 deletions.
21 changes: 21 additions & 0 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def worker_config(
service_model,
region,
operating_system,
posix_job_users,
windows_job_users,
) -> Generator[DeadlineWorkerConfiguration, None, None]:
"""
Expand Down Expand Up @@ -207,6 +208,7 @@ def worker_config(
),
service_model_path=dst_path,
file_mappings=file_mappings or None,
job_users=posix_job_users,
windows_job_users=windows_job_users,
start_service=True,
)
Expand Down Expand Up @@ -385,6 +387,25 @@ def job_run_as_user() -> PosixSessionUser:
)


@pytest.fixture(scope="session")
def posix_job_users() -> list:
return [
PosixSessionUser("job-user", "job-user"),
PosixSessionUser(
user="cli-override",
group="job-override-group",
),
PosixSessionUser(
user="config-override",
group="job-override-group",
),
PosixSessionUser(
user="env-override",
group="job-override-group",
),
]


@pytest.fixture(scope="session")
def windows_job_users() -> list:
return [
Expand Down
93 changes: 0 additions & 93 deletions test/e2e/test_job_user.py

This file was deleted.

211 changes: 210 additions & 1 deletion test/e2e/test_override_job_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Deadline Cloud service and checking that the result/output of the jobs is as we expect it.
"""

import re
import backoff
import boto3
import botocore
import pytest
Expand All @@ -14,6 +16,7 @@
from deadline_test_fixtures import (
Job,
Farm,
PosixSessionUser,
Queue,
TaskStatus,
DeadlineClient,
Expand All @@ -29,7 +32,7 @@
reason="Windows Specific Job User Override Tests.",
)
@pytest.mark.parametrize("operating_system", ["windows"], indirect=True)
class TestJobUserOverride:
class TestWindowsJobUserOverride:
@staticmethod
def submit_whoami_job(
test_name: str, deadline_client: DeadlineClient, farm: Farm, queue: Queue
Expand Down Expand Up @@ -234,3 +237,209 @@ def test_env_var_user_override(
assert (
cmd_result.exit_code == 0
), f"Failed to unset DEADLINE_WORKER_WINDOWS_JOB_USER: {cmd_result}"


@pytest.mark.usefixtures("operating_system")
@pytest.mark.skipif(
os.environ["OPERATING_SYSTEM"] == "windows",
reason="Linux specific Job User Override tests",
)
@pytest.mark.parametrize("operating_system", ["linux"], indirect=True)
class TestLinuxJobUserOverride:
@staticmethod
def submit_whoami_job(
test_name: str, deadline_client: DeadlineClient, farm: Farm, queue: Queue
) -> Job:
job = Job.submit(
client=deadline_client,
farm=farm,
queue=queue,
priority=98,
template={
"specificationVersion": "jobtemplate-2023-09",
"name": f"whoami {test_name}",
"steps": [
{
"name": "Step0",
"hostRequirements": {
"attributes": [{"name": "attr.worker.os.family", "allOf": ["linux"]}]
},
"script": {
"embeddedFiles": [
{
"name": "whoami",
"type": "TEXT",
"runnable": True,
"data": "\n".join(
[
"#!/bin/bash",
'echo "I am: $(whoami)"',
]
),
},
],
"actions": {
"onRun": {
"command": "{{ Task.File.whoami }}",
},
},
},
},
],
},
)
return job

def test_no_user_override(
self,
deadline_resources,
deadline_client: DeadlineClient,
class_worker: EC2InstanceWorker,
job_run_as_user: PosixSessionUser,
) -> None:
# WHEN
job = self.submit_whoami_job(
"No user override",
deadline_client,
deadline_resources.farm,
deadline_resources.queue_a,
)

# THEN
job.wait_until_complete(client=deadline_client, max_retries=20)

job.assert_single_task_log_contains(
deadline_client=deadline_client,
logs_client=boto3.client(
"logs",
config=botocore.config.Config(retries={"max_attempts": 10, "mode": "adaptive"}),
),
expected_pattern=rf"I am: {re.escape(job_run_as_user.user)}",
)

assert job.task_run_status == TaskStatus.SUCCEEDED

def test_config_file_user_override(
self,
deadline_resources,
class_worker: EC2InstanceWorker,
deadline_client: DeadlineClient,
) -> None:

class_worker.stop_worker_service()

@backoff.on_exception(
backoff.constant,
Exception,
max_time=45,
interval=5,
)
def check_worker_service_stopped() -> None:
worker_status_cmd_response = class_worker.send_command(
"systemctl is-active deadline-worker"
)

assert worker_status_cmd_response.exit_code != 0
assert worker_status_cmd_response.stdout != "active"

check_worker_service_stopped()

cmd_result = class_worker.send_command(
'sed -i \'s/# posix_job_user = "user:group"/posix_job_user = "config-override:job-override-group"/g\' /etc/amazon/deadline/worker.toml'
)
assert (
cmd_result.exit_code == 0
), f"Setting the job user override via CLI failed: {cmd_result}"

class_worker.start_worker_service()

job = self.submit_whoami_job(
"config user override",
deadline_client,
deadline_resources.farm,
deadline_resources.queue_a,
)

job.wait_until_complete(client=deadline_client, max_retries=20)

job.assert_single_task_log_contains(
deadline_client=deadline_client,
logs_client=boto3.client(
"logs",
config=botocore.config.Config(retries={"max_attempts": 10, "mode": "adaptive"}),
),
expected_pattern="I am: config-override",
)

assert job.task_run_status == TaskStatus.SUCCEEDED

cmd_result = class_worker.send_command(
'sed -i \'s/posix_job_user = "config-override:job-override-group"/# posix_job_user = "user:group"/g\' /etc/amazon/deadline/worker.toml'
)
assert (
cmd_result.exit_code == 0
), f"Resetting the job user override via CLI failed: {cmd_result}"

def test_env_var_user_override(
self,
deadline_resources,
class_worker: EC2InstanceWorker,
deadline_client: DeadlineClient,
) -> None:

class_worker.stop_worker_service()

@backoff.on_exception(
backoff.constant,
Exception,
max_time=45,
interval=5,
)
def check_worker_service_stopped() -> None:
worker_status_cmd_response = class_worker.send_command(
"systemctl is-active deadline-worker"
)

assert worker_status_cmd_response.exit_code != 0
assert worker_status_cmd_response.stdout != "active"

check_worker_service_stopped()

cmd_result = class_worker.send_command(
'echo "Environment=DEADLINE_WORKER_POSIX_JOB_USER=env-override:job-override-group" >> /etc/systemd/system/deadline-worker.service.d/config.conf',
)

assert (
cmd_result.exit_code == 0
), f"Failed to set DEADLINE_WORKER_POSIX_JOB_USER: {cmd_result}"

class_worker.send_command("systemctl daemon-reload")
class_worker.start_worker_service()

job = self.submit_whoami_job(
"environment override",
deadline_client,
deadline_resources.farm,
deadline_resources.queue_a,
)

job.wait_until_complete(client=deadline_client, max_retries=20)

job.assert_single_task_log_contains(
deadline_client=deadline_client,
logs_client=boto3.client(
"logs",
config=botocore.config.Config(retries={"max_attempts": 10, "mode": "adaptive"}),
),
expected_pattern="I am: env-override",
)

assert job.task_run_status == TaskStatus.SUCCEEDED

cmd_result = class_worker.send_command(
"sed -i '/Environment=DEADLINE_WORKER_POSIX_JOB_USER=env-override/d' /etc/systemd/system/deadline-worker.service.d/config.conf"
)
assert (
cmd_result.exit_code == 0
), f"Resetting the job user override via CLI failed: {cmd_result}"
class_worker.send_command("sudo systemctl daemon-reload")

0 comments on commit 7314f99

Please sign in to comment.