    :web-router-service {:puppetlabs.puppetdb.cli.services/puppetdb-service ""
                         :puppetlabs.puppetdb.metrics/metrics-service "/metrics"}})
 
+(defn assoc-logging-config
+  "Adds a dynamically created logback.xml with a test log. The
+  generated log file name is returned for printing to the console."
+  [config]
+  (let [logback-file (fs/absolute-path (temp-file "logback" ".xml"))
+        log-file (fs/absolute-path (temp-file "jett-test" ".log"))]
+    (spit logback-file (log-config log-file "ERROR"))
+    [log-file (assoc-in config [:global :logging-config] logback-file)]))
+
 (defn current-port
   "Given a trapperkeeper server, return the port of the running jetty instance.
-   Note there can be more than one port (i.e. SSL + non-SSL connector). This only
-   returns the first one."
+  Note there can be more than one port (i.e. SSL + non-SSL connector). This only
+  returns the first one."
   [server]
   (-> @(tka/app-context server)
       (get-in [:WebserverService :jetty9-servers :default :server])

            [puppetlabs.puppetdb.utils :as utils]
            [clj-http.client :as client]
            [puppetlabs.puppetdb.config :as conf]
            [clj-http.util :refer [url-encode]]))

;; See utils.clj for more information about base-urls.
(def ^:dynamic *base-url* nil) ; Will not have a :version.

(defn create-config
  "Creates a default config, populated with a temporary vardir and
   a fresh hypersql instance"
  []
  {:nrepl {}
   :global {:vardir (temp-dir)}


(defn puppetdb-instance
  "Stands up a puppetdb instance with `config`, tears down once `f` returns.
  `services` is a seq of additional services that should be started in addition
  to the core PuppetDB services. Binds *server* and *base-url* to refer to
  the instance."
  ([f] (puppetdb-instance (create-config) f))
  ([config f] (puppetdb-instance config [] f))
  ([config services f]
   (let [[log-file config] (-> config conf/adjust-tk-config assoc-logging-config)
         prefix (get-in config
                        [:web-router-service
                         :puppetlabs.puppetdb.cli.services/puppetdb-service])]
     (try
       (tkbs/with-app-with-config server
         (concat [jetty9-service puppetdb-service message-listener-service command-service webrouting-service metrics-service]
                 services)
         config
         (binding [*server* server
                   *base-url* (merge {:protocol "http"
                                      :host "localhost"
                                      :port (current-port server)}
                                     (when prefix {:prefix prefix}))]
           (f)))
       (finally
         (let [log-contents (slurp log-file)]
           (when-not (str/blank? log-contents)
             (utils/println-err "-------Begin PuppetDB Instance Log--------------------\n"
                                log-contents
                                "\n-------End PuppetDB Instance Log----------------------"))))))))

(defmacro with-puppetdb-instance
  "Convenience macro to launch a puppetdb instance"
