<commit_before>;;
;; Copyright (c) Two Sigma Open Source, LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;  http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;
(ns cook.test.mesos.unscheduled
  (:use clojure.test)
  (:require [cook.mesos.unscheduled :as u]
            [datomic.api :as d]
            [cook.mesos.scheduler :as scheduler]
            [cook.mesos.util :as util]
            [cook.test.testutil :refer (create-dummy-instance
                                        create-dummy-job
                                        restore-fresh-database!)]
            [cook.mesos.quota :as quota]))

(defn resource-map->list
  [m]
  (map (fn [[k v]]
         {:resource/type (keyword "resource.type" (name k))
          :resource/amount v}) m))

(deftest test-how-job-would-exceed-resource-limits
  (let [quota {:mem 6000 :cpus 8 :count 5}
        running-jobs [{:job/resource (resource-map->list {:mem 3000 :cpus 4})}
                      {:job/resource (resource-map->list {:mem 2000 :cpus 2})}]]

    (testing "exceeding both mem and cpu"
      (is (= (u/how-job-would-exceed-resource-limits
              quota running-jobs
              {:job/resource (resource-map->list {:mem 4000 :cpus 4})})
             {:mem {:limit 6000, :usage 9000}, :cpus {:limit 8, :usage 10}})))

    (testing "exceeding just 1 resource"
      (is (= (u/how-job-would-exceed-resource-limits
              quota running-jobs
              {:job/resource (resource-map->list {:mem 2000 :cpus 1})})
             {:mem {:limit 6000, :usage 7000}})))

    (testing "exceeding cpus and count"
      (is (= (u/how-job-would-exceed-resource-limits
              (merge quota {:count 2}) running-jobs
              {:job/resource (resource-map->list {:mem 500 :cpus 4})})
             {:cpus {:limit 8, :usage 10} :count {:limit 2 :usage 3}})))))

(def datomic-uri "datomic:mem://test-unscheduled")

(deftest test-fenzo-placement
  (let [conn (restore-fresh-database! datomic-uri)
        placement-failures (str {:constraints {"novel_host_constraint" 3}
                                 :resources {"mem" 14
                                             "cpus" 8}})]
    (is (= (u/check-fenzo-placement conn
                                    {:db/id (d/tempid :db.part/user)
                                     :job/under-investigation true
                                     :job/last-fenzo-placement-failure placement-failures})
           ["The job couldn't be placed on any available hosts."
            {:reasons
             [{:reason "Not enough mem available." :host_count 14}
              {:reason "Not enough cpus available." :host_count 8}
              {:reason "Job already ran on this host." :host_count 3}]}]))))

(deftest test-reasons
  (let [conn (restore-fresh-database! datomic-uri)
        _ (quota/set-quota! conn "mforsyth" :count 2)
        running-job-id1 (-> (create-dummy-job conn :user "mforsyth"
                                              :ncpus 1.0 :memory 3.0
                                              :job-state :job.state/running))
        running-job-id2 (-> (create-dummy-job conn :user "mforsyth"
                                              :ncpus 1.0 :memory 3.1<commit_msg>add missing reason to failing test (#310)<commit_after>;;
;; Copyright (c) Two Sigma Open Source, LLC
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;  http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;
(ns cook.test.mesos.unscheduled
  (:use clojure.test)
  (:require [cook.mesos.unscheduled :as u]
            [datomic.api :as d]
            [cook.mesos.scheduler :as scheduler]
            [cook.mesos.util :as util]
            [cook.test.testutil :refer (create-dummy-instance
                                        create-dummy-job
                                        restore-fresh-database!)]
            [cook.mesos.quota :as quota]))

(defn resource-map->list
  [m]
  (map (fn [[k v]]
         {:resource/type (keyword "resource.type" (name k))
          :resource/amount v}) m))

(deftest test-how-job-would-exceed-resource-limits
  (let [quota {:mem 6000 :cpus 8 :count 5}
        running-jobs [{:job/resource (resource-map->list {:mem 3000 :cpus 4})}
                      {:job/resource (resource-map->list {:mem 2000 :cpus 2})}]]

    (testing "exceeding both mem and cpu"
      (is (= (u/how-job-would-exceed-resource-limits
              quota running-jobs
              {:job/resource (resource-map->list {:mem 4000 :cpus 4})})
             {:mem {:limit 6000, :usage 9000}, :cpus {:limit 8, :usage 10}})))

    (testing "exceeding just 1 resource"
      (is (= (u/how-job-would-exceed-resource-limits
              quota running-jobs
              {:job/resource (resource-map->list {:mem 2000 :cpus 1})})
             {:mem {:limit 6000, :usage 7000}})))

    (testing "exceeding cpus and count"
      (is (= (u/how-job-would-exceed-resource-limits
              (merge quota {:count 2}) running-jobs
              {:job/resource (resource-map->list {:mem 500 :cpus 4})})
             {:cpus {:limit 8, :usage 10} :count {:limit 2 :usage 3}})))))

(def datomic-uri "datomic:mem://test-unscheduled")

(deftest test-fenzo-placement
  (let [conn (restore-fresh-database! datomic-uri)
        placement-failures (str {:constraints {"novel_host_constraint" 3}
                                 :resources {"mem" 14
                                             "cpus" 8}})]
    (is (= (u/check-fenzo-placement conn
                                    {:db/id (d/tempid :db.part/user)
                                     :job/under-investigation true
                                     :job/last-fenzo-placement-failure placement-failures})
           ["The job couldn't be placed on any available hosts."
            {:reasons
             [{:reason "Not enough mem available." :host_count 14}
              {:reason "Not enough cpus available." :host_count 8}
              {:reason "Job already ran on this host." :host_count 3}]}]))))

(deftest test-reasons
  (let [conn (restore-fresh-database! datomic-uri)
        _ (quota/set-quota! conn "mforsyth" "test-reasons" :count 2)
        running-job-id1 (-> (create-dummy-job conn :user "mforsyth"
                                              :ncpus 1.0 :memory 3.0
                                              :job-state :job.state/running))
        running-job-id2 (-> (create-dummy-job conn :user "mforsyth"
                                              :ncpus 1.0 :memory 3.1
{'fraction_non_alphanumeric': 0.12962711864406778, 'fraction_numerical': 0.021830508474576273, 'mean_word_length': 1.9188761377127028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2642629', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2093, 'n_words': 638}