Permalink
Browse files

Fix XML settings bug, add tests for config resolution

  • Loading branch information...
jpb committed Nov 3, 2015
1 parent e5ba1c3 commit 794e72ddcd9fcf7b44b07d115480b36c96d5e7fd
Showing with 94 additions and 25 deletions.
  1. +27 −23 src/com/unbounce/yopa/config.clj
  2. +65 −0 test/com/unbounce/yopa/config_test.clj
  3. +2 −2 yopa-config-example.yml
@@ -107,18 +107,15 @@
(.setParameter "region" region)
(.setParameter "sqs-port" (get-in config [:sqs :port]))
(.setParameter "sqs-host" (get-in config [:sqs :host]
(str host ":"
(get-in config [:sqs :port]))))
(str host ":" (get-in config [:sqs :port]))))
(.setParameter "sqs-https" (get-in config [:sqs :https]))
(.setParameter "sns-port" (get-in config [:sns :port]))
(.setParameter "sns-host" (get-in config [:sns :host]
(str host ":"
(get-in config [:sqs :port]))))
(str host ":" (get-in config [:sns :port]))))
(.setParameter "sns-https" (get-in config [:sns :https]))
(.setParameter "s3-port" (get-in config [:s3 :port]))
(.setParameter "s3-host" (get-in config [:s3 :host]
(str host ":"
(get-in config [:sqs :port]))))
(str host ":" (get-in config [:s3 :port]))))
(.setParameter "s3-https" (get-in config [:s3 :https]))
(.transform source target))
@@ -142,27 +139,34 @@
(defn- rewrite-config-as-nested
[config]
(let [rewritten-config
(deep-merge
{:bind-address (:bindAddress config)
:sqs
{:port (:sqsPort config)}
:sns
{:port (:snsPort config)}
:s3
{:port (:s3Port config)
:data-dir (:s3DataDir config)}}
config)]
(dissoc rewritten-config
:bindAddress
:sqsPort
:snsPort
:s3Port
:s3DataDir)))
(defn resolve-config
[config-from-file]
(deep-merge
{:bind-address (:bindAddress config)
:sqs
{:port (:sqsPort config)}
:sns
{:port (:snsPort config)}
:s3
{:port (:s3Port config)
:data-dir (:s3DataDir config)}}
config))
(defn- resolve-config
[config-file]
(let [config-from-file (yaml/parse-string (slurp config-file))]
(deep-merge
default-config
(rewrite-config-as-nested config-from-file))))
default-config
(rewrite-config-as-nested config-from-file)))
(defn init [config-file output-file]
(log/info "Loading config file: " (.getAbsolutePath config-file))
(let [config (resolve-config config-file)]
(let [config-from-file (yaml/parse-string (slurp config-file))
config (resolve-config config-from-file)]
(reset! region (:region config))
(generate-regions-override output-file config)
@@ -0,0 +1,65 @@
(ns com.unbounce.yopa.config-test
(:require [clojure.java.io :as io]
[com.unbounce.yopa.core :as yopa]
[com.unbounce.yopa.config :as config]
[clojure.test :refer :all]))
(defonce config-file
(io/file "yopa-config-example.yml"))
(deftest init-test
(is (= (config/init config-file
(yopa/default-override-file))
{:region "yopa-local"
:host "localhost"
:bind-address "0.0.0.0"
:sqs-port 47195
:sns-port 47196
:s3-port 47197
:s3-data-dir "/tmp/yopa-fake-s3/"})))
(deftest resolve-config-test
(testing "defaults"
(is (=
(config/resolve-config {})
{:region "yopa-local"
:host "localhost"
:bind-address "0.0.0.0"
:sqs {:port 47195 :https false}
:sns {:port 47196 :https false}
:s3 {:port 47197 :https false :data-dir "/tmp/yopa-fake-s3/"}
:messaging []
:storage {}})))
(testing "overrides"
(is (=
(config/resolve-config {:sqs {:port 1}
:sns {:port 2}
:s3 {:port 3 :data-dir "/foo"}
:bind-address "1.2.3.4"
:host "foo"
:region "foo-local"})
{:region "foo-local"
:host "foo"
:bind-address "1.2.3.4"
:sqs {:port 1 :https false}
:sns {:port 2 :https false}
:s3 {:port 3 :https false :data-dir "/foo"}
:messaging []
:storage {}})))
(testing "rewriting 'legacy' config"
(is (=
(config/resolve-config {:sqsPort 1
:snsPort 2
:s3Port 3
:s3DataDir "/foo"
:bindAddress "1.2.3.4"})
{:region "yopa-local"
:host "localhost"
:bind-address "1.2.3.4"
:sqs {:port 1 :https false}
:sns {:port 2 :https false}
:s3 {:port 3 :https false :data-dir "/foo"}
:messaging []
:storage {}}))))
@@ -8,9 +8,9 @@ sqs:
port: 47195
# `https` and `host` are used for the regions override XML.
# This is useful if yopa is fronted by a proxy that offloads HTTPS.
https: true
# https: false
# host: (defaults to top level `host`:`port`)
host: sqs.unbounce.xyz
# host: localhost:47195
sns:
port: 47196
https: false

0 comments on commit 794e72d

Please sign in to comment.