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

test: add linux job user override tests #477

Merged
merged 1 commit into from
Nov 15, 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
22 changes: 21 additions & 1 deletion test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def worker_config(
service_model,
region,
operating_system,
posix_job_user,
posix_env_override_job_user,
posix_config_override_job_user,
windows_job_users,
) -> Generator[DeadlineWorkerConfiguration, None, None]:
"""
Expand Down Expand Up @@ -207,6 +210,7 @@ def worker_config(
),
service_model_path=dst_path,
file_mappings=file_mappings or None,
job_users=[posix_job_user, posix_config_override_job_user, posix_env_override_job_user],
windows_job_users=windows_job_users,
start_service=True,
)
Expand Down Expand Up @@ -378,13 +382,29 @@ def region() -> str:


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


@pytest.fixture(scope="session")
def posix_config_override_job_user() -> PosixSessionUser:
return PosixSessionUser(
user="config-override",
group="job-override-group",
)


@pytest.fixture(scope="session")
def posix_env_override_job_user() -> PosixSessionUser:
return 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.

216 changes: 215 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,214 @@ 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,
posix_job_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(posix_job_user.user)}",
)

assert job.task_run_status == TaskStatus.SUCCEEDED

def test_config_file_user_override(
self,
deadline_resources,
class_worker: EC2InstanceWorker,
posix_config_override_job_user: PosixSessionUser,
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(
f'sed -i \'s/# posix_job_user = "user:group"/posix_job_user = "{posix_config_override_job_user.user}:{posix_config_override_job_user.group}"/g\' /etc/amazon/deadline/worker.toml'
)
assert (
cmd_result.exit_code == 0
), f"Setting the job user override via CLI failed: {cmd_result}"

try:
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=f"I am: {posix_config_override_job_user.user}",
)

assert job.task_run_status == TaskStatus.SUCCEEDED
finally:
cmd_result = class_worker.send_command(
f"sed -i '/posix_job_user = \"{posix_config_override_job_user.user}:{posix_config_override_job_user.group}\"/d' /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,
posix_env_override_job_user: PosixSessionUser,
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(
f'echo "Environment=DEADLINE_WORKER_POSIX_JOB_USER={posix_env_override_job_user.user}:{posix_env_override_job_user.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")

try:
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=f"I am: {posix_env_override_job_user.user}",
)

assert job.task_run_status == TaskStatus.SUCCEEDED
finally:
cmd_result = class_worker.send_command(
f"sed -i '/Environment=DEADLINE_WORKER_POSIX_JOB_USER={posix_env_override_job_user.user}/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")