├── .travis.yml ├── .gitignore ├── src └── de │ └── otto │ └── machroput │ ├── deploy_api.clj │ ├── chronos │ ├── connection.clj │ └── simple_deployment.clj │ ├── utils │ └── http_utils.clj │ └── marathon │ ├── checks.clj │ ├── connection.clj │ └── deployment.clj ├── project.clj ├── test └── de │ └── otto │ └── machroput │ └── marathon │ ├── mocks.clj │ ├── deployment_test.clj │ └── checks_test.clj ├── README.md └── LICENSE /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | *.iml 4 | .lein-failures 5 | pom.xml* 6 | -------------------------------------------------------------------------------- /src/de/otto/machroput/deploy_api.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.deploy-api) 2 | 3 | (defprotocol DeploymentAPI 4 | (start-deployment [self json version])) 5 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject de.otto/machroput "1.1.2-SNAPSHOT" 2 | :description "A simple marathon and chronos api written in clojure" 3 | :license {:name "Apache License 2.0" :url "http://www.apache.org/license/LICENSE-2.0.html"} 4 | 5 | :url "https://github.com/otto-de/machroput.git" 6 | :dependencies [[http-kit "2.2.0"] 7 | [org.clojure/data.json "0.2.6"] 8 | [org.clojure/clojure "1.8.0"]] 9 | :lein-release {:deploy-via :clojars} 10 | :plugins [[lein-ancient "0.6.10"]] 11 | :target-path "target/%s" 12 | :profiles {:uberjar {:aot :all} 13 | :dev {:plugins [[lein-release/lein-release "1.0.9"]]}}) 14 | -------------------------------------------------------------------------------- /src/de/otto/machroput/chronos/connection.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.chronos.connection 2 | (:require 3 | [de.otto.machroput.utils.http-utils :refer :all] 4 | [clojure.data.json :as json])) 5 | 6 | (defn has-parent-dependency? [json] 7 | (not (nil? (:parents json)))) 8 | 9 | (defprotocol ChronosApi 10 | (create-new-app [self json])) 11 | 12 | (defrecord ChronosConnection [url user password ] 13 | ChronosApi 14 | (create-new-app [self json] 15 | (let [json-str (json/write-str json)] 16 | (if (has-parent-dependency? json) 17 | (a-json-request self POST "/scheduler/dependency" json-str) 18 | (a-json-request self POST "/scheduler/iso8601" json-str))))) 19 | 20 | (defn new-chronos-connection [{:keys [url user password]} ] 21 | (map->ChronosConnection 22 | {:url url 23 | :user user 24 | :password password})) 25 | -------------------------------------------------------------------------------- /src/de/otto/machroput/chronos/simple_deployment.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.chronos.simple-deployment 2 | (:require 3 | [de.otto.machroput.deploy-api :refer :all] 4 | [de.otto.machroput.chronos.connection :as c] 5 | [clojure.data.json :as json])) 6 | 7 | (defrecord SimpleChronosDeployment [conn print-fn] 8 | DeploymentAPI 9 | (start-deployment [self json version] 10 | (print-fn "Starting simple chronos deployment") 11 | (print-fn "Thats your Deploy-JSON for version " version ": ") 12 | (print-fn (json/write-str json)) 13 | (c/create-new-app conn json) 14 | (print-fn "Deployment successful?!?!"))) 15 | 16 | (defn new-simple-chronos-deployment 17 | ([cconf] 18 | (new-simple-chronos-deployment cconf println)) 19 | ([cconf print-fn] 20 | (map->SimpleChronosDeployment 21 | {:conn (c/new-chronos-connection cconf) 22 | :print-fn print-fn}))) 23 | -------------------------------------------------------------------------------- /src/de/otto/machroput/utils/http_utils.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.utils.http-utils 2 | (:require 3 | [org.httpkit.client :as http] 4 | [clojure.data.json :as json])) 5 | 6 | (def POST http/post) 7 | (def PUT http/put) 8 | (def GET http/get) 9 | 10 | (defn request-options [user password body] 11 | (let [opts {:body body 12 | :headers {"Accept" "application/json" 13 | "Content-Type" "application/json"}}] 14 | (if (and user password) 15 | (assoc opts :basic-auth [user password]) 16 | opts))) 17 | 18 | (defn a-json-request [{:keys [url user password]} mthd path body] 19 | (let [target (str url path)] 20 | (let [{:keys [status body error]} @(mthd target (request-options user password body))] 21 | (when error 22 | (throw (RuntimeException. error))) 23 | (when (>= status 400) 24 | (throw (RuntimeException. (str "An error occured when requesting " target " status was:" status " body:" body)))) 25 | (if-not (empty? body) 26 | (json/read-str body :key-fn keyword))))) 27 | -------------------------------------------------------------------------------- /src/de/otto/machroput/marathon/checks.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.checks 2 | (:require [de.otto.machroput.marathon.connection :as mc])) 3 | 4 | (defprotocol MarathonDeploymentCheckApi 5 | (with-app-version-check [self fn]) 6 | (with-marathon-task-health-check [self]) 7 | (with-marathon-app-version-check [self]) 8 | (with-deployment-stopped-check [self])) 9 | 10 | (defn- fcheck [print-fn cond msg] 11 | (when (not cond) 12 | (print-fn msg)) 13 | cond) 14 | 15 | (defn- get-current-version! [print-fn app-version-fn] 16 | (try 17 | (app-version-fn) 18 | (catch Exception e 19 | (print-fn (str "An error occured when trying to execute the current-version-fn " (.getMessage e))) 20 | nil))) 21 | 22 | (defn app-version-check [_ {:keys [print-fn app-version-fn]} {expected-version :version}] 23 | (let [current-version (get-current-version! print-fn app-version-fn)] 24 | (fcheck print-fn 25 | (= current-version expected-version) 26 | (format (str "Version Check was NOT ok!\n" 27 | "Actual: '%s' on status page\n" 28 | "Expected: '%s' after deployment") current-version expected-version )))) 29 | 30 | (defn marathon-task-health-check [mconn {:keys [print-fn]} {:keys [id instances]}] 31 | (let [{{:keys [tasksUnhealthy tasksHealthy tasksRunning]} :app} (mc/get-app mconn id)] 32 | (fcheck print-fn 33 | (and (= tasksRunning instances) 34 | (= tasksHealthy tasksRunning) 35 | (= 0 tasksUnhealthy)) 36 | (format (str "Task Check was NOT ok!\n" 37 | "Running: %s Healthy: %s Unhealthy: %s") tasksRunning tasksHealthy tasksUnhealthy)))) 38 | 39 | (defn marathon-app-version-check [mconn {:keys [print-fn]} {:keys [id marathon-deploy-version]}] 40 | (let [{{current-app-version :version} :app} (mc/get-app mconn id)] 41 | (fcheck print-fn 42 | (= current-app-version marathon-deploy-version) 43 | (format (str "Marathon-Deploy-Version Check was NOT ok!\n" 44 | "App is not running latest deployment-version\n" 45 | "Currently running: '%s'\n" 46 | "Deployed with Marathon: '%s'") current-app-version marathon-deploy-version )))) 47 | 48 | (defn deployment-stopped-check [mconn {:keys [print-fn]} {:keys [marathon-deploy-version]}] 49 | (fcheck print-fn 50 | (= false (mc/deployment-still-running? mconn marathon-deploy-version)) 51 | "Marathon-Deployment Check was NOT ok! The started Marathon-deployment is still running")) 52 | 53 | -------------------------------------------------------------------------------- /src/de/otto/machroput/marathon/connection.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.connection 2 | (:require 3 | [de.otto.machroput.utils.http-utils :refer :all] 4 | [clojure.data.json :as json])) 5 | 6 | (defn is-deployment-for [deployment appid] 7 | (let [affectedApps (:affectedApps deployment)] 8 | (not (empty? (filter #(= % appid) affectedApps))))) 9 | 10 | (defn current-app-deployments [deployments appid] 11 | (some->> deployments 12 | (filter #(is-deployment-for % appid)))) 13 | 14 | (defn is-app-currently-deploying? [current-deployments app-id] 15 | (not (empty? (current-app-deployments current-deployments app-id)))) 16 | 17 | (defn current-deployment-version-for [deployments appid] 18 | (some-> (current-app-deployments deployments appid) 19 | (first) 20 | (:version))) 21 | 22 | (defprotocol MarathonAPI 23 | (start [self]) 24 | (create-new-app [self json]) 25 | (get-apps [self]) 26 | (get-app [self app-id]) 27 | (get-app-versions [self app-id]) 28 | (get-app-config [self app-id version]) 29 | (get-deployments [self])) 30 | 31 | 32 | (defprotocol MaratohnAPIHelper 33 | (determine-deployment-version [self app-id]) 34 | (deployment-still-running? [self depl-version]) 35 | (deployment-exists-for? [self app-id])) 36 | 37 | (defrecord MarathonConnection [url user password print-fn] 38 | MarathonAPI 39 | (create-new-app [self json] 40 | (let [app-id (:id json) 41 | json-str (json/write-str json)] 42 | (a-json-request self PUT (str "/v2/apps/" app-id) json-str))) 43 | 44 | (get-apps [self] 45 | (a-json-request self GET "/v2/apps" nil)) 46 | 47 | (get-app [self app-id] 48 | (a-json-request self GET (str "/v2/apps/" app-id) nil)) 49 | 50 | (get-app-versions [self app-id] 51 | (a-json-request self GET (str "/v2/apps/" app-id "/versions") nil)) 52 | 53 | (get-app-config [self app-id version] 54 | (a-json-request self GET (str "/v2/apps/" app-id "/versions/" version) nil)) 55 | 56 | (get-deployments [self] 57 | (a-json-request self GET "/v2/deployments" nil)) 58 | 59 | MaratohnAPIHelper 60 | (determine-deployment-version [self appid] 61 | (let [max-retries 20] 62 | (loop [tries 0] 63 | (let [current-deployments (get-deployments self) 64 | result (current-deployment-version-for current-deployments appid)] 65 | (if-not (nil? result) 66 | result 67 | (if (< tries max-retries) 68 | (do 69 | (Thread/sleep 500) 70 | (recur (+ tries 1))) 71 | nil)))))) 72 | 73 | (deployment-still-running? [self depl-version] 74 | (let [current-depl (get-deployments self) 75 | current-depl-versions (set (map :version current-depl))] 76 | (contains? current-depl-versions depl-version))) 77 | 78 | (deployment-exists-for? [self app-id] 79 | (let [current-deployments (get-deployments self)] 80 | (is-app-currently-deploying? current-deployments app-id)))) 81 | 82 | (defn new-marathon-connection [{:keys [url user password]}] 83 | (map->MarathonConnection 84 | {:url url 85 | :user user 86 | :password password})) 87 | -------------------------------------------------------------------------------- /test/de/otto/machroput/marathon/mocks.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.mocks 2 | (:require [de.otto.machroput.marathon.connection :as mc])) 3 | 4 | 5 | 6 | (defrecord MarathonConectionMock [version deployment-still-running? deployment-exists? creations] 7 | mc/MarathonAPI 8 | (start [self]) 9 | (create-new-app [self json] 10 | (swap! creations conj json)) 11 | (get-apps [self]) 12 | (get-app [self app-id]) 13 | (get-app-versions [self app-id]) 14 | (get-app-config [self app-id version]) 15 | (get-deployments [self]) 16 | 17 | mc/MaratohnAPIHelper 18 | (determine-deployment-version [_ _] 19 | version) 20 | (deployment-still-running? [_ _] 21 | deployment-still-running?) 22 | (deployment-exists-for? [_ _] 23 | deployment-exists?)) 24 | 25 | 26 | (defn update-interactive-deployment-state! [deploy-time state] 27 | (when-let [started-time (:deployment-started-at @state)] 28 | (let [time-taken (- (System/currentTimeMillis) started-time)] 29 | (when (> time-taken deploy-time) 30 | (swap! state dissoc :deployment-started-at)))) 31 | nil) 32 | 33 | (defrecord InteractiveMarathonConectionMock [deploy-id deploy-time state app-transition] 34 | mc/MarathonAPI 35 | (start [self]) 36 | (create-new-app [self json] 37 | (update-interactive-deployment-state! deploy-time state) 38 | (swap! state assoc :deployment-started-at (System/currentTimeMillis))) 39 | (get-apps [self] 40 | (update-interactive-deployment-state! deploy-time state)) 41 | (get-app [self app-id] 42 | (update-interactive-deployment-state! deploy-time state) 43 | (if (not (nil? (:deployment-started-at @state))) 44 | (first app-transition) 45 | (second app-transition))) 46 | (get-app-versions [self app-id] 47 | (update-interactive-deployment-state! deploy-time state)) 48 | (get-app-config [self app-id version] 49 | (update-interactive-deployment-state! deploy-time state)) 50 | (get-deployments [self] 51 | (update-interactive-deployment-state! deploy-time state)) 52 | 53 | mc/MaratohnAPIHelper 54 | (determine-deployment-version [_ appid] 55 | (update-interactive-deployment-state! deploy-time state) 56 | (get-in (second app-transition) [:app :version])) 57 | 58 | (deployment-still-running? [_ depl-version] 59 | (update-interactive-deployment-state! deploy-time state) 60 | (not (nil? (:deployment-started-at @state)))) 61 | 62 | (deployment-exists-for? [_ app-id] 63 | (update-interactive-deployment-state! deploy-time state) 64 | (not (nil? (:deployment-started-at @state))))) 65 | 66 | (def no-deployment-mock (->MarathonConectionMock nil false false (atom []))) 67 | (def deployment-running-mock (->MarathonConectionMock "somid" true true (atom []))) 68 | 69 | (def default-app-transition 70 | [{:app {:version "old-version"}} {:app {:version "new-version"}}]) 71 | 72 | (defn interactive-deployment-mock [deploy-time & {:keys [app-transition] 73 | :or {app-transition default-app-transition}}] 74 | (->InteractiveMarathonConectionMock "deploy-id" deploy-time (atom {}) app-transition)) 75 | 76 | (defn catch-app-creations-mock [creations] 77 | (->MarathonConectionMock nil false false creations)) 78 | 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #machroput 2 | 3 | This is a simple clojure-library to deploy apps to Marathon or Chronos. It can be used standalone, but was developed with a [lambdacd](https://github.com/flosell/lambdacd)-integration in mind. 4 | It is still in an early stage but e.g. already supports some easy post-deployment checks for marathon (like e.g. a task-health-check). 5 | 6 | [![Clojars Project](http://clojars.org/de.otto/machroput/latest-version.svg)](http://clojars.org/de.otto/machroput) 7 | 8 | [![Build Status](https://travis-ci.org/otto-de/machroput.svg?branch=master)](https://travis-ci.org/otto-de/machroput) 9 | [![Dependencies Status](http://jarkeeper.com/otto-de/machroput/status.svg)](http://jarkeeper.com/otto-de/machroput) 10 | 11 | ## Features included 12 | * Deploy Chronos (v2.4.0) 13 | * Deploy Marathon (v0.10.0) 14 | * Post-Deployment-Checks for Marathon 15 | 16 | ## Examples 17 | 18 | ```clojure 19 | (ns my.name.space 20 | (:require 21 | [de.otto.machroput.deploy-api :as dapi] 22 | [de.otto.machroput.marathon.checks :as checks] 23 | [de.otto.machroput.marathon.deployment :as msd])) 24 | 25 | (def sample-marathon-config 26 | { :url "http://your.marathon.instance" 27 | :user "a-user" ;marathon basic-auth-user, if required 28 | :password "a-password" ;marathon basic-auth-password, if required 29 | }) 30 | 31 | (def sample-json ;a-marathon-json-as-map 32 | { :id "your/marathon/id" 33 | :instances 3 34 | :mem 2024 35 | :cpus 1 36 | ... 37 | }) 38 | 39 | (def new-version "0.1.1") ; your version to be deployed 40 | 41 | 42 | (defn deploy-marathon [mconf json version] 43 | (-> (msd/new-marathon-deployment 44 | mconf 45 | :print-fn println 46 | :deployment-timeout-in-min 2 47 | :polling-interval-in-millis 1000 48 | :post-deployment-checks [] 49 | :app-version-fn (fn [])) 50 | (checks/with-marathon-task-health-check) 51 | (checks/with-marathon-app-version-check) 52 | (checks/with-deployment-stopped-check) 53 | (checks/with-app-version-check (a-fn-returning-your-current-app-version)) 54 | (dapi/start-deployment json version))) 55 | 56 | ; do the actual deployment 57 | (deploy-marathon sample-marathon-config sample-json new-version) 58 | 59 | ``` 60 | 61 | To integrate the deployment with lambdacd do this: 62 | 63 | ```clojure 64 | 65 | (ns my.name.space 66 | (:require 67 | [lambdacd.steps.support :as supp] 68 | [de.otto.machroput.marathon.checks :as checks] 69 | [de.otto.machroput.deploy-api :as dapi] 70 | [de.otto.machroput.marathon.deployment :as msd])) 71 | 72 | (defn deploy-marathon [mconf json version print-fn] 73 | (-> (msd/new-marathon-deployment mconf :print-fn print-fn) 74 | (checks/with-marathon-task-health-check) 75 | (checks/with-marathon-app-version-check) 76 | (checks/with-deployment-stopped-check) 77 | (checks/with-app-version-check (a-fn-returning-your-current-app-version)) 78 | (dapi/start-deployment json version))) 79 | 80 | ;; define lambdacd deployment-step 81 | (defn start-deployment [] 82 | (fn [args ctx] 83 | (let [printer (supp/new-printer) 84 | print-fn (fn [& msgs] (supp/print-to-output ctx printer (clojure.string/join msgs)))] 85 | (deploy-marathon sample-marathon-config sample-json new-version print-fn) 86 | {:status :success 87 | :out (supp/printed-output printer)}))) 88 | 89 | ``` 90 | 91 | 92 | -------------------------------------------------------------------------------- /src/de/otto/machroput/marathon/deployment.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.deployment 2 | (:require 3 | [de.otto.machroput.marathon.connection :as mc] 4 | [de.otto.machroput.deploy-api :refer :all] 5 | [de.otto.machroput.marathon.checks :as checks] 6 | [clojure.data.json :as json])) 7 | 8 | (defn get-current-version! [{:keys [print-fn app-version-fn]}] 9 | (try 10 | (app-version-fn) 11 | (catch Exception e 12 | (print-fn (str "An error occured when trying to execute the current-version-fn " (.getMessage e))) 13 | nil))) 14 | 15 | (defn build-deployment-info [json version marathon-deploy-version] 16 | {:version version 17 | :id (:id json) 18 | :marathon-deploy-version marathon-deploy-version 19 | :instances (:instances json)}) 20 | 21 | (defn all-deployment-checks-successful? [{:keys [mconn deploy-conf]} deploy-info] 22 | (->> (:post-deployment-checks deploy-conf) 23 | (map (fn [check] (check mconn deploy-conf deploy-info))) 24 | (not-any? false?))) 25 | 26 | (defn max-wait-time-reached [starttime deployment-timeout-in-min] 27 | (let [timeout-as-millis (* deployment-timeout-in-min 60 1000) 28 | time-taken (- (System/currentTimeMillis) starttime)] 29 | (> time-taken timeout-as-millis))) 30 | 31 | (defn wait-for-deployment [{{:keys [print-fn polling-interval-in-millis deployment-timeout-in-min]} :deploy-conf :as self} deploy-infos] 32 | (print-fn "Waiting for deployment to be finished... ") 33 | (let [starttime (System/currentTimeMillis)] 34 | (loop [success? false] 35 | (if success? 36 | (print-fn "Deployment was successful!") 37 | (do 38 | (when (max-wait-time-reached starttime deployment-timeout-in-min) 39 | (throw (RuntimeException. "The deployment timed out"))) 40 | (Thread/sleep polling-interval-in-millis) 41 | (recur 42 | (all-deployment-checks-successful? self deploy-infos))))))) 43 | 44 | (defn print-pre-deployment-infos [{:keys [print-fn] :as deploy-conf} json version] 45 | (print-fn "Starting simple marathon deployment") 46 | (print-fn (format "Thats your Deploy-JSON for version %s: " version)) 47 | (print-fn (json/write-str json)) 48 | (let [current-version (get-current-version! deploy-conf)] 49 | (print-fn (str "Current version deployed is " (or current-version "not-known") " Your are deploying: " version)))) 50 | 51 | (defn handle-running-deployment [{:keys [mconn deploy-conf] :as self} json version] 52 | (if-let [marathon-deploy-version (mc/determine-deployment-version mconn (:id json))] 53 | (wait-for-deployment self (build-deployment-info json version marathon-deploy-version)) 54 | (if (= version (get-current-version! deploy-conf)) 55 | ((:print-fn deploy-conf) "No deployment started, version to deploy is the same as the one deployed") 56 | (throw (RuntimeException. (str "Error: No deployment was started for version " version)))))) 57 | 58 | (defn start-marathon-deployment [{:keys [mconn deploy-conf] :as self} {app-id :id :as json} version] 59 | (let [deployment-ongoing? (mc/deployment-exists-for? mconn app-id)] 60 | (when deployment-ongoing? (throw (IllegalStateException. "There should not be a deployment already running"))) 61 | (print-pre-deployment-infos deploy-conf json version) 62 | (mc/create-new-app mconn json) 63 | (handle-running-deployment self json version))) 64 | 65 | (def default-app-version-fn (fn [])) 66 | 67 | (defn deploy-conf-str 68 | [{:keys [print-fn deployment-timeout-in-min polling-interval-in-millis post-deployment-checks app-version-fn]}] 69 | (str "deployment-timeout-in-min: " deployment-timeout-in-min " " 70 | "polling-interval-in-millis: " polling-interval-in-millis " " 71 | "nr-of-post-deployment-checks: " (count post-deployment-checks) " " 72 | "app-version-fn?: " (not (= app-version-fn default-app-version-fn)) " " 73 | "custom-print-fn?: " (not (= println print-fn)))) 74 | 75 | (defrecord MarathonDeployment [mconn deploy-conf] 76 | checks/MarathonDeploymentCheckApi 77 | (with-app-version-check [self app-version-check-fn] 78 | (-> (update-in self [:deploy-conf :post-deployment-checks] conj checks/app-version-check) 79 | (update :deploy-conf assoc :app-version-fn app-version-check-fn))) 80 | 81 | (with-marathon-task-health-check [self] 82 | (update-in self [:deploy-conf :post-deployment-checks] conj checks/marathon-task-health-check)) 83 | 84 | (with-marathon-app-version-check [self] 85 | (update-in self [:deploy-conf :post-deployment-checks] conj checks/marathon-app-version-check)) 86 | 87 | (with-deployment-stopped-check [self] 88 | (update-in self [:deploy-conf :post-deployment-checks] conj checks/deployment-stopped-check)) 89 | 90 | DeploymentAPI 91 | (start-deployment [self json version] 92 | ((:print-fn deploy-conf) (str "Using Deploy-Conf: " (deploy-conf-str deploy-conf))) 93 | (if (= (get-current-version! deploy-conf) version) 94 | ((:print-fn deploy-conf) (str "Version " version " is already deployed. Nothing to do.")) 95 | (start-marathon-deployment self json version)))) 96 | 97 | (defn new-marathon-deployment 98 | [mconf & {:keys [print-fn deployment-timeout-in-min polling-interval-in-millis 99 | post-deployment-checks app-version-fn] 100 | :or {print-fn println 101 | deployment-timeout-in-min 5 102 | polling-interval-in-millis 2000 103 | post-deployment-checks [] 104 | app-version-fn default-app-version-fn}}] 105 | (map->MarathonDeployment 106 | {:deploy-conf {:print-fn print-fn 107 | :deployment-timeout-in-min deployment-timeout-in-min 108 | :polling-interval-in-millis polling-interval-in-millis 109 | :post-deployment-checks post-deployment-checks 110 | :app-version-fn app-version-fn} 111 | :mconn (mc/new-marathon-connection mconf)})) 112 | 113 | 114 | (defn new-marathon-deployment-with-deploy-conf [mconf deploy-conf] 115 | (apply (partial new-marathon-deployment mconf) 116 | (apply concat (seq deploy-conf)))) 117 | -------------------------------------------------------------------------------- /test/de/otto/machroput/marathon/deployment_test.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.deployment-test 2 | (:require 3 | [de.otto.machroput.marathon.deployment :as mdep] 4 | [de.otto.machroput.marathon.mocks :as mocks] 5 | [de.otto.machroput.deploy-api :as dapi] 6 | [clojure.test :refer :all] 7 | [de.otto.machroput.marathon.checks :as checks])) 8 | 9 | (deftest not-starting-deployments 10 | (testing "should NOT start a deployment if deployment is still running" 11 | (let [mdepl (-> (mdep/new-marathon-deployment {}) 12 | (checks/with-app-version-check (constantly "0.0.0")) 13 | (assoc :mconn mocks/deployment-running-mock))] 14 | (is (thrown? Throwable 15 | (dapi/start-deployment mdepl {:id "someid"} "0.0.1")))))) 16 | 17 | (deftest starting-deployments 18 | (testing "should start a new marathon deployment" 19 | (with-redefs [mdep/handle-running-deployment (constantly nil)] 20 | (let [created-jsons (atom []) 21 | mdepl (-> (mdep/new-marathon-deployment {}) 22 | (checks/with-app-version-check (constantly "0.0.0")) 23 | (assoc :mconn (mocks/catch-app-creations-mock created-jsons))) 24 | deployment-json {:id "someid"}] 25 | (dapi/start-deployment mdepl deployment-json "0.0.1") 26 | (is (= [deployment-json] @created-jsons)))))) 27 | 28 | (deftest wait-for-deployment-test 29 | (testing "should abort deployment after configured timeout" 30 | (with-redefs [mdep/all-deployment-checks-successful? (constantly nil)] 31 | (let [start-time (System/currentTimeMillis) 32 | ten-milli-in-min (/ 1 60 100)] 33 | (is (thrown? RuntimeException 34 | (mdep/wait-for-deployment {:deploying (atom true) 35 | :deploy-conf {:print-fn println 36 | :polling-interval-in-millis 0 37 | :deployment-timeout-in-min ten-milli-in-min}} 38 | {}))) 39 | (let [time-taken (- (System/currentTimeMillis) start-time)] 40 | (is (<= time-taken 20))))))) 41 | 42 | (deftest simple-marathon-deployment 43 | (testing "should build a simple deployment" 44 | (let [app-check-fn (fn [] :bar) 45 | print-fn (fn [] :foo) 46 | standard-deployment (mdep/new-marathon-deployment 47 | {} 48 | :app-version-fn app-check-fn 49 | :print-fn print-fn)] 50 | (is (= {:deployment-timeout-in-min 5 51 | :polling-interval-in-millis 2000 52 | :post-deployment-checks [] 53 | :app-version-fn app-check-fn 54 | :print-fn print-fn} 55 | (:deploy-conf standard-deployment))))) 56 | 57 | (testing "should use default print-fn" 58 | (let [standard-deployment (mdep/new-marathon-deployment {})] 59 | (is (= println 60 | (get-in standard-deployment [:deploy-conf :print-fn]))))) 61 | 62 | (testing "should use default app-version-fn" 63 | (let [standard-deployment (mdep/new-marathon-deployment {}) 64 | default-app-version-fn (get-in standard-deployment [:deploy-conf :app-version-fn])] 65 | (is (= nil (default-app-version-fn))))) 66 | 67 | (testing "should build a simple deployment with with-app-version-check" 68 | (let [standard-deployment (-> (mdep/new-marathon-deployment {}) 69 | (checks/with-app-version-check :foo-check))] 70 | (is (= [checks/app-version-check] 71 | (get-in standard-deployment [:deploy-conf :post-deployment-checks]))) 72 | (is (= :foo-check 73 | (get-in standard-deployment [:deploy-conf :app-version-fn]))))) 74 | 75 | (testing "should build a simple deployment with with-marathon-task-health-check" 76 | (let [standard-deployment (-> (mdep/new-marathon-deployment {}) 77 | (checks/with-marathon-task-health-check))] 78 | (is (= [checks/marathon-task-health-check] 79 | (get-in standard-deployment [:deploy-conf :post-deployment-checks]))))) 80 | 81 | (testing "should build a simple deployment with with-marathon-app-version-check" 82 | (let [standard-deployment (-> (mdep/new-marathon-deployment {}) 83 | (checks/with-marathon-app-version-check))] 84 | (is (= [checks/marathon-app-version-check] 85 | (get-in standard-deployment [:deploy-conf :post-deployment-checks]))))) 86 | 87 | (testing "should build a simple deployment with with-deployment-stopped-check" 88 | (let [standard-deployment (-> (mdep/new-marathon-deployment {}) 89 | (checks/with-deployment-stopped-check))] 90 | (is (= [checks/deployment-stopped-check] 91 | (get-in standard-deployment [:deploy-conf :post-deployment-checks])))))) 92 | 93 | 94 | (deftest initializing-deployments-with-helpers 95 | (testing "should initialize a simple deployment with helper methods" 96 | (let [print-fn (fn [] :foo) 97 | app-version-fn (fn [] :buf)] 98 | (is (= (mdep/new-marathon-deployment-with-deploy-conf 99 | {:foo :bar} 100 | {:print-fn print-fn 101 | :deployment-timeout-in-min 2 102 | :polling-interval-in-millis 1000 103 | :post-deployment-checks [:baz] 104 | :app-version-fn app-version-fn}) 105 | (mdep/new-marathon-deployment 106 | {:foo :bar} 107 | :print-fn print-fn 108 | :deployment-timeout-in-min 2 109 | :polling-interval-in-millis 1000 110 | :post-deployment-checks [:baz] 111 | :app-version-fn app-version-fn))) 112 | (is (= (mdep/new-marathon-deployment-with-deploy-conf 113 | {:foo :bar} 114 | {:print-fn print-fn 115 | :app-version-fn app-version-fn}) 116 | (mdep/new-marathon-deployment 117 | {:foo :bar} 118 | :print-fn print-fn 119 | :app-version-fn app-version-fn))) 120 | (is (= (mdep/new-marathon-deployment-with-deploy-conf {:foo :bar} {}) 121 | (mdep/new-marathon-deployment {:foo :bar})))))) 122 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /test/de/otto/machroput/marathon/checks_test.clj: -------------------------------------------------------------------------------- 1 | (ns de.otto.machroput.marathon.checks-test 2 | (:require 3 | [de.otto.machroput.marathon.deployment :as mdep] 4 | [de.otto.machroput.marathon.mocks :as mocks] 5 | [clojure.test :refer :all] 6 | [de.otto.machroput.marathon.checks :as checks] 7 | [de.otto.machroput.marathon.connection :as mc]) 8 | (:import (de.otto.machroput.marathon.connection MaratohnAPIHelper))) 9 | 10 | (def min-as-millis (/ 1 60 1000)) 11 | 12 | (deftest running-deployments-without-any-check 13 | (testing "should throw no exception if no post-deployment-check is configured" 14 | (let [hundred-millis-in-min (* min-as-millis 100) 15 | mdeployment (-> (mdep/new-marathon-deployment {} 16 | :deployment-timeout-in-min hundred-millis-in-min 17 | :polling-interval-in-millis 10) 18 | (assoc :mconn (mocks/interactive-deployment-mock 10)))] 19 | (is (= nil (mdep/start-marathon-deployment mdeployment {} "0.0.1")))))) 20 | 21 | (defrecord TestMarathonAPIHelper [] 22 | MaratohnAPIHelper 23 | (determine-deployment-version [_ _] nil) 24 | (deployment-still-running? [_ _] nil) 25 | (deployment-exists-for? [_ _] true)) 26 | 27 | (deftest running-deployments-without-any-check 28 | (testing "should throw an exception if a deployment is already ongoing" 29 | (let [self {:mconn (map->TestMarathonAPIHelper {})}] 30 | (is (thrown? IllegalStateException (mdep/start-marathon-deployment self nil nil)))))) 31 | 32 | 33 | (deftest running-any-post-deployment-checks 34 | (testing "should start post-deployment-checks on deployment" 35 | (let [check-started (atom nil) 36 | store-check-call-fn (fn [_ _ _] (reset! check-started :post-deployment-check-started!)) 37 | mdeployment (-> (mdep/new-marathon-deployment {} 38 | :deployment-timeout-in-min 1 39 | :polling-interval-in-millis 10 40 | :post-deployment-checks [store-check-call-fn]) 41 | (assoc :mconn (mocks/interactive-deployment-mock 100)))] 42 | (mdep/start-marathon-deployment mdeployment {} "0.0.1") 43 | (is (= :post-deployment-check-started! @check-started))))) 44 | 45 | 46 | (deftest running-app-version-check 47 | (let [deployment-with-app-version-check (fn [& {:keys [deployment-timeout-in-min deploy-time app-version]}] 48 | (-> (mdep/new-marathon-deployment {} 49 | :deployment-timeout-in-min deployment-timeout-in-min 50 | :polling-interval-in-millis 10) 51 | (checks/with-app-version-check (fn [] @app-version)) 52 | (assoc :mconn (mocks/interactive-deployment-mock deploy-time))))] 53 | 54 | (testing "should throw RTException if app-version does not change" 55 | (let [app-version (atom "0.0.1")] 56 | (is (thrown? RuntimeException 57 | (-> (deployment-with-app-version-check 58 | :deployment-timeout-in-min (* 30 min-as-millis) 59 | :deploy-time 60 60 | :app-version app-version) 61 | (mdep/start-marathon-deployment {} "0.0.2")))))) 62 | 63 | (testing "should finish successful if app-version changes as expected" 64 | (let [app-version (atom "0.0.1")] 65 | (future 66 | (is (= nil (-> (deployment-with-app-version-check 67 | :deployment-timeout-in-min (* 30 min-as-millis) 68 | :deploy-time 10 69 | :app-version app-version) 70 | (mdep/start-marathon-deployment {} "0.0.2"))))) 71 | (Thread/sleep 10) 72 | (reset! app-version "0.0.2") 73 | (Thread/sleep 30))))) 74 | 75 | (deftest running-deployment-stopped-check 76 | (let [deployment-with-deployment-stopped-check (fn [& {:keys [deployment-timeout-in-min deploy-time]}] 77 | (-> (mdep/new-marathon-deployment {} 78 | :deployment-timeout-in-min deployment-timeout-in-min 79 | :polling-interval-in-millis 10) 80 | (checks/with-deployment-stopped-check) 81 | (assoc :mconn (mocks/interactive-deployment-mock deploy-time))))] 82 | (testing "should throw an exception if deployment does not stop in time" 83 | (is (thrown? RuntimeException 84 | (-> (deployment-with-deployment-stopped-check 85 | :deployment-timeout-in-min (* 30 min-as-millis) 86 | :deploy-time 60) 87 | (mdep/start-marathon-deployment {} "0.0.1"))))) 88 | 89 | (testing "should throw no exception if deployment stops in time" 90 | (is (= nil (-> (deployment-with-deployment-stopped-check 91 | :deployment-timeout-in-min (* 30 min-as-millis) 92 | :deploy-time 15) 93 | (mdep/start-marathon-deployment {} "0.0.1"))))))) 94 | 95 | 96 | (deftest running-marathon-app-version-check 97 | (let [deployment-withapp-version-check (fn [& {:keys [deployment-timeout-in-min deploy-time]}] 98 | (-> (mdep/new-marathon-deployment {} 99 | :deployment-timeout-in-min deployment-timeout-in-min 100 | :polling-interval-in-millis 10) 101 | (checks/with-marathon-app-version-check) 102 | (assoc :mconn (mocks/interactive-deployment-mock 103 | deploy-time 104 | :app-transition [{:app {:version "marathon-0.0.1"}} {:app {:version "marathon-0.0.2"}}]))))] 105 | (testing "should throw an exception if marathon-app-version-check does not return valid response in time" 106 | (is (thrown? RuntimeException 107 | (-> (deployment-withapp-version-check 108 | :deployment-timeout-in-min (* 30 min-as-millis) 109 | :deploy-time 60) 110 | (mdep/start-marathon-deployment {} "0.0.1"))))) 111 | 112 | (testing "should throw no exception if marathon-app-version-check returns valid response in time" 113 | (is (= nil (-> (deployment-withapp-version-check 114 | :deployment-timeout-in-min (* 30 min-as-millis) 115 | :deploy-time 15) 116 | (mdep/start-marathon-deployment {} "some-app-version"))))))) 117 | 118 | 119 | (deftest running-marathon-task-health-check 120 | (let [deployment-with-marathon-task-health-check (fn [& {:keys [deployment-timeout-in-min deploy-time]}] 121 | (-> (mdep/new-marathon-deployment {} 122 | :deployment-timeout-in-min deployment-timeout-in-min 123 | :polling-interval-in-millis 10) 124 | (checks/with-marathon-task-health-check) 125 | (assoc :mconn (mocks/interactive-deployment-mock 126 | deploy-time 127 | :app-transition [{:app {:version "marathon-0.0.1" 128 | :tasksUnhealthy 1 129 | :tasksHealthy 1 130 | :tasksRunning 2}} 131 | {:app {:version "marathon-0.0.2" 132 | :tasksUnhealthy 0 133 | :tasksHealthy 1 134 | :tasksRunning 1}}]))))] 135 | (testing "should throw an exception if marathon-task-health-check does not return valid response in time" 136 | (is (thrown? RuntimeException 137 | (-> (deployment-with-marathon-task-health-check 138 | :deployment-timeout-in-min (* 30 min-as-millis) 139 | :deploy-time 60) 140 | (mdep/start-marathon-deployment {:instances 1} "0.0.1"))))) 141 | 142 | (testing "should throw no exception if marathon-task-health-check returns valid response in time" 143 | (is (= nil 144 | (-> (deployment-with-marathon-task-health-check 145 | :deployment-timeout-in-min (* 30 min-as-millis) 146 | :deploy-time 15) 147 | (mdep/start-marathon-deployment {:instances 1} "0.0.1"))))))) 148 | 149 | (deftest marathon-app-version-check-unit 150 | (testing "should call api with id and print error and return false" 151 | (let [call (atom :no-call) 152 | error-print-call (atom :no-call)] 153 | (with-redefs [mc/get-app (fn [_ id] (reset! call id))] 154 | (is (= false (checks/marathon-app-version-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:id "123" :marathon-deploy-version "000"}))) 155 | (is (= "123" @call)) 156 | (is (= (str "Marathon-Deploy-Version Check was NOT ok!\n" 157 | "App is not running latest deployment-version\n" 158 | "Currently running: 'null'\n" 159 | "Deployed with Marathon: '000'") @error-print-call))))) 160 | 161 | (testing "should call api with id and print no error and return true" 162 | (let [call (atom :no-call) 163 | error-print-call (atom :no-call)] 164 | (with-redefs [mc/get-app (fn [_ id] (reset! call id) 165 | {:app {:version "000"}})] 166 | (is (= true (checks/marathon-app-version-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:id "123" :marathon-deploy-version "000"}))) 167 | (is (= "123" @call)) 168 | (is (= :no-call @error-print-call)))))) 169 | 170 | (deftest deployment-stopped-check-unit 171 | (testing "should call api with id and print error and return false" 172 | (let [call (atom :no-call) 173 | error-print-call (atom :no-call)] 174 | (with-redefs [mc/deployment-still-running? (fn [_ depl-version] (reset! call depl-version))] 175 | (is (= false (checks/deployment-stopped-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:marathon-deploy-version "000"}))) 176 | (is (= "000" @call)) 177 | (is (= "Marathon-Deployment Check was NOT ok! The started Marathon-deployment is still running" @error-print-call))))) 178 | 179 | (testing "should call api with id and print no error and return true" 180 | (let [call (atom :no-call) 181 | error-print-call (atom :no-call)] 182 | (with-redefs [mc/deployment-still-running? (fn [_ depl-version] (reset! call depl-version) 183 | false)] 184 | (is (= true (checks/deployment-stopped-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:marathon-deploy-version "000"}))) 185 | (is (= "000" @call)) 186 | (is (= :no-call @error-print-call)))))) 187 | 188 | (deftest marathon-task-health-check-unit 189 | (testing "should call api with id and print error and return false" 190 | (let [call (atom :no-call) 191 | error-print-call (atom :no-call)] 192 | (with-redefs [mc/get-app (fn [_ id] (reset! call id))] 193 | (is (= false (checks/marathon-task-health-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:id "123" :instances 1}))) 194 | (is (= "123" @call)) 195 | (is (= (str "Task Check was NOT ok!\n" 196 | "Running: null Healthy: null Unhealthy: null") @error-print-call))))) 197 | 198 | (testing "should call api with id and print no error and return true" 199 | (let [call (atom :no-call) 200 | error-print-call (atom :no-call)] 201 | (with-redefs [mc/get-app (fn [_ id] (reset! call id) 202 | {:app {:tasksUnhealthy 0 203 | :tasksHealthy 1 204 | :tasksRunning 1}})] 205 | (is (= true (checks/marathon-task-health-check {} {:print-fn (fn [e] (reset! error-print-call e))} {:id "123" :instances 1}))) 206 | (is (= "123" @call)) 207 | (is (= :no-call @error-print-call)))))) 208 | 209 | (deftest app-version-check-unit 210 | (testing "should call app-version-fn and print error and return false" 211 | (let [call (atom :no-call) 212 | error-print-call (atom :no-call)] 213 | (is (= false (checks/app-version-check {} {:app-version-fn (fn [] (reset! call :call)) :print-fn (fn [e] (reset! error-print-call e))} {:version "0.0.1"}))) 214 | (is (= :call @call)) 215 | (is (= (str "Version Check was NOT ok!\n" 216 | "Actual: ':call' on status page\n" 217 | "Expected: '0.0.1' after deployment") @error-print-call)))) 218 | 219 | (testing "should call app-version-fn and print no error and return true" 220 | (let [call (atom :no-call) 221 | error-print-call (atom :no-call)] 222 | (is (= true (checks/app-version-check {} {:app-version-fn (fn [] (reset! call :call) 223 | "0.0.1") :print-fn (fn [e] (reset! error-print-call e))} {:version "0.0.1"}))) 224 | (is (= :call @call)) 225 | (is (= :no-call @error-print-call))))) 226 | --------------------------------------------------------------------------------