Skip to content
Permalink
Browse files

Add max-ports to task constraints (#1239)

  • Loading branch information...
pschorf authored and dposada committed Oct 9, 2019
1 parent be3a6e1 commit 93b98d05ec51df49ae4a5210d61f251a1e7a5c6e
@@ -1322,9 +1322,9 @@ def test_ports(self):
job_uuid, resp = util.submit_job(self.cook_url, ports=1)
instance = util.wait_for_instance(self.cook_url, job_uuid)
self.assertEqual(1, len(instance['ports']))
job_uuid, resp = util.submit_job(self.cook_url, ports=10)
job_uuid, resp = util.submit_job(self.cook_url, ports=5)
instance = util.wait_for_instance(self.cook_url, job_uuid)
self.assertEqual(10, len(instance['ports']))
self.assertEqual(5, len(instance['ports']))

def test_allow_partial_for_groups(self):
def absent_uuids(response):
@@ -2671,3 +2671,15 @@ def test_priority(self):
_, resp = util.submit_job(self.cook_url, priority=16000001)
self.assertEqual(400, resp.status_code, msg=resp.content)
self.assertTrue(f'priority":"(not (between-0-and-16000000' in str(resp.content))

def test_max_ports(self):
settings = util.settings(self.cook_url)
max_ports = util.get_in(settings, 'task-constraints', 'max-ports')
job_uuid, resp = util.submit_job(self.cook_url, ports=(max_ports + 1))
try:
self.assertEqual(resp.status_code, 400, resp.content)
self.assertTrue(f'Requested {max_ports+1} ports, but only allowed to use {max_ports}'
in resp.text,
resp.text)
finally:
util.kill_jobs(self.cook_url, [job_uuid], assert_response=False)
@@ -290,7 +290,8 @@
:timeout-interval-minutes 1
:retry-limit 20
:memory-gb 12
:cpus 4}
:cpus 4
:max-ports 5}
(:task-constraints scheduler)))
:offer-incubate-time-ms (fnk [[:config {scheduler nil}]]
(when scheduler
@@ -858,7 +858,7 @@
& {:keys [commit-latch-id override-group-immutability?]
:or {commit-latch-id nil
override-group-immutability? false}}]
(let [{:keys [docker-parameters-allowed]} (config/task-constraints)
(let [{:keys [docker-parameters-allowed max-ports]} (config/task-constraints)
group-uuid (when group group)
munged (merge
{:user user
@@ -905,6 +905,10 @@
(* 1024 (:memory-gb task-constraints)))
{:constraints task-constraints
:job job})))
(when (> (:ports munged) max-ports)
(throw (ex-info (str "Requested " ports " ports, but only allowed to use " max-ports)
{:constraints task-constraints
:job job})))
(when (and (:retry-limit task-constraints)
(> max-retries (:retry-limit task-constraints)))
(throw (ex-info (str "Requested " max-retries " exceeds the maximum retry limit")
@@ -244,6 +244,7 @@
(is (= ((first fail-body) "state") "failed")))))

(deftest job-validator
(testutil/setup)
(let [conn (restore-fresh-database! "datomic:mem://mesos-api-test")
job (fn [cpus mem max-retries] {"uuid" (str (UUID/randomUUID))
"command" "hello world"
@@ -319,7 +320,7 @@
499)))

(testing "allow all docker params by default"
(with-redefs [config/task-constraints (constantly {})]
(with-redefs [config/task-constraints (constantly {:max-ports 5})]
(let [minimal-job (basic-job)
container-job (assoc minimal-job "container" {"type" "docker"
"docker" {"image" "dummy:latest"
@@ -334,7 +335,8 @@
:body-params {"jobs" [container-job]}}))))))

(testing "disallow docker parameters"
(with-redefs [config/task-constraints (constantly {:docker-parameters-allowed #{"user"}})]
(with-redefs [config/task-constraints (constantly {:docker-parameters-allowed #{"user"}
:max-ports 5})]
(let [minimal-job (basic-job)
container-job (assoc minimal-job "container" {"type" "docker"
"docker" {"image" "dummy:latest"
@@ -349,7 +351,8 @@
:body-params {"jobs" [container-job]}})))))))

(testing "allowed docker parameters"
(with-redefs [config/task-constraints (constantly {:docker-parameters-allowed #{"foo"}})]
(with-redefs [config/task-constraints (constantly {:docker-parameters-allowed #{"foo"}
:max-ports 5})]
(let [minimal-job (basic-job)
container-job (assoc minimal-job "container" {"type" "docker"
"docker" {"image" "dummy:latest"
@@ -361,7 +364,18 @@
:uri "/rawscheduler"
:headers {"Content-Type" "application/json"}
:authorization/user "test"
:body-params {"jobs" [container-job]}}))))))))))
:body-params {"jobs" [container-job]}})))))))

(testing "max ports"
(with-redefs [config/task-constraints (constantly {:max-ports 5})]
(let [job (assoc (basic-job) :ports 6)]
(is (= 400
(:status (h {:request-method :post
:scheme :http
:uri "/rawscheduler"
:headers {"Content-Type" "application/json"}
:authorization/user "test"
:body-params {"jobs" [job]}}))))))))))

(deftest gpus-api
(let [conn (restore-fresh-database! "datomic:mem://mesos-api-test")

0 comments on commit 93b98d0

Please sign in to comment.
You can’t perform that action at this time.