Skip to content

Commit

Permalink
Rndmh3ro namespace get job with tests (#121)
Browse files Browse the repository at this point in the history
* add support for namespace argument in get_job
* fixes #118

* add tests for get_jobs with namespace override. Update responses dependency.
* update requests minor version
* check for namespace to get_job, if not pass empty dict
* add namespace tests for get_job
Co-authored-by: Sebastian Gumprich <[email protected]>
Co-authored-by: Jonathan Cross <>
  • Loading branch information
jrxFive authored Aug 31, 2021
1 parent 1cc1077 commit 100e68b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 19 deletions.
14 changes: 4 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ services:
- docker
language: python
python:
- '3.6'
- '3.7'
- '3.8'
env:
global:
- NOMAD_IP="127.0.0.1"
- NOMAD_PORT="4646"
- NOMAD_LATEST="1.1.4"
matrix:
- NOMAD_VERSION="0.3.2"
- NOMAD_VERSION="0.4.1"
- NOMAD_VERSION="0.5.6"
- NOMAD_VERSION="0.6.0"
- NOMAD_VERSION="0.7.1"
- NOMAD_VERSION="0.8.1"
- NOMAD_VERSION="0.8.3"
- NOMAD_VERSION="1.0.0"
- NOMAD_VERSION="1.1.4"
before_install:
- curl -L -o /tmp/nomad_${NOMAD_VERSION}_linux_amd64.zip https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_amd64.zip
- yes | unzip -d /tmp /tmp/nomad_${NOMAD_VERSION}_linux_amd64.zip
Expand All @@ -48,4 +42,4 @@ before_script:
script:
- py.test --cov=nomad --cov-report=term-missing --runxfail tests/
after_success:
- test $NOMAD_VERSION=="0.8.3" && codecov
- test $NOMAD_VERSION = $NOMAD_LATEST && codecov
11 changes: 9 additions & 2 deletions nomad/api/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,26 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_job(self, id):
def get_job(self, id, namespace=None):
""" Query a single job for its specification and status.
https://www.nomadproject.io/docs/http/job.html
arguments:
- id
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
returns: dict
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request(id, method="get").json()
params = {}

if namespace:
params["namespace"] = namespace

return self.request(id, method="get", params=params).json()

def get_versions(self, id):
""" This endpoint reads information about all versions of a job.
Expand Down
5 changes: 4 additions & 1 deletion nomad/api/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ def get_jobs(self, prefix=None, namespace=None):
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix, "namespace": namespace}
params = {"prefix": prefix}
if namespace:
params["namespace"] = namespace

return self.request(method="get", params=params).json()

def register_job(self, job):
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pytest==6.2.4
pytest-cov==2.12.1
mkdocs==0.15.3
mock==1.2.0
responses==0.9.0
flaky==3.7.0
flaky==3.7.0
responses==0.13.4
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.20.0
requests==2.26.0
41 changes: 41 additions & 0 deletions tests/test_job.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import os
import uuid

import pytest
import responses

import nomad
import tests.common as common

from flaky import flaky

Expand All @@ -22,6 +26,43 @@ def test_get_job(nomad_setup):
assert isinstance(nomad_setup.job.get_job("example"), dict) == True


@responses.activate
def test_get_jobs_with_namespace_override_no_namespace_declared_on_create_incorrect_declared_namespace(nomad_setup):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/job/18a0f501-41d5-ae43-ff61-1d8ec3ec8314?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

with pytest.raises(exceptions.BaseNomadException):
nomad_setup.job.get_job(id=str(uuid.uuid4()))


@responses.activate
def test_get_jobs_with_namespace_override_no_namespace_declared_on_create(nomad_setup):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/job/18a0f501-41d5-ae43-ff61-1d8ec3ec8314?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

nomad_setup.job.get_job(id="18a0f501-41d5-ae43-ff61-1d8ec3ec8314", namespace=common.NOMAD_NAMESPACE)


@responses.activate
def test_get_jobs_with_namespace_override_namespace_declared_on_create(nomad_setup_with_namespace):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/job/18a0f501-41d5-ae43-ff61-1d8ec3ec8314?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace="override-namespace"),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

nomad_setup_with_namespace.job.get_job(id="18a0f501-41d5-ae43-ff61-1d8ec3ec8314", namespace="override-namespace")


def test_get_allocations(nomad_setup):
j = nomad_setup.job["example"]
a = nomad_setup.job.get_allocations("example")
Expand Down
46 changes: 43 additions & 3 deletions tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import responses
import tests.common as common


from nomad.api.exceptions import BaseNomadException


# integration tests requires nomad Vagrant VM or Binary running
def test_register_job(nomad_setup):

Expand Down Expand Up @@ -72,10 +76,9 @@ def test_dunder_iter(nomad_setup):
def test_dunder_len(nomad_setup):
assert len(nomad_setup.jobs) >= 0

@responses.activate
#

# fix No data when you are using namespaces #82
#
@responses.activate
def test_get_jobs_with_namespace(nomad_setup_with_namespace):
responses.add(
responses.GET,
Expand All @@ -84,3 +87,40 @@ def test_get_jobs_with_namespace(nomad_setup_with_namespace):
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)
assert common.NOMAD_NAMESPACE in nomad_setup_with_namespace.jobs.get_jobs()[0]["Namespace"]


@responses.activate
def test_get_jobs_with_namespace_override_no_namespace_declared_on_create_incorrect_declared_namespace(nomad_setup):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/jobs?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

with pytest.raises(BaseNomadException):
nomad_setup.jobs.get_jobs(namespace="should-raise")


@responses.activate
def test_get_jobs_with_namespace_override_no_namespace_declared_on_create(nomad_setup):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/jobs?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

nomad_setup.jobs.get_jobs(namespace=common.NOMAD_NAMESPACE)


@responses.activate
def test_get_jobs_with_namespace_override_namespace_declared_on_create(nomad_setup_with_namespace):
responses.add(
responses.GET,
"http://{ip}:{port}/v1/jobs?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace="override-namespace"),
status=200,
json=[{"Region": "global","ID": "my-job", "ParentID": "", "Name": "my-job","Namespace": common.NOMAD_NAMESPACE, "Type": "batch", "Priority": 50}]
)

nomad_setup_with_namespace.jobs.get_jobs(namespace="override-namespace")
1 change: 1 addition & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_evaluate_node(nomad_setup):
assert "EvalIDs" in nomad_setup.node.evaluate_node(nodeID)


@pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) > (1, 1, 0), reason="Not supported in version")
def test_drain_node(nomad_setup):
nodeID = nomad_setup.nodes["pynomad1"]["ID"]
assert "EvalIDs" in nomad_setup.node.drain_node(nodeID)
Expand Down

0 comments on commit 100e68b

Please sign in to comment.