Skip to content

Commit

Permalink
Merge pull request #177 from FundingCircle/ISSUE_176_reset_applicatio…
Browse files Browse the repository at this point in the history
…n_fixture_hardcodes_bootstrap_servers_value

[ISSUE-176] allow reset-application-fixture to get bootsrap.servers from app-config
  • Loading branch information
Andy Chambers authored Aug 27, 2019
2 parents 87f0e5d + a4147bb commit 1474750
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/jackdaw/test/fixtures.clj
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,39 @@

;;; reset-application-fixture ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn reset-application-fixture [app-config]
(fn [t]
(let [rt (StreamsResetter.)
app-id (get app-config "application.id")
args (->> ["--application-id" (get app-config "application.id")
"--bootstrap-servers" (get app-config "bootstrap.servers")]
(into-array String))
result (with-open [out-str (java.io.StringWriter.)
err-str (java.io.StringWriter.)]
(binding [*out* out-str
*err* err-str]
(let [status (.run rt args)]
(flush)
{:status status
:out (str out-str)
:err (str err-str)})))]

(if (zero? (:status result))
(t)
(throw (ex-info "failed to reset application. check logs for details"
result))))))
(defn default-reset-fn
[rt args]
(.run rt (into-array String args)))

(defn reset-application-fixture
"Returns a fixture that runs the kafka.tools.StreamsResetter with the supplied
`reset-args` as parameters"
([app-config]
(reset-application-fixture app-config [] default-reset-fn))

([app-config reset-args]
(reset-application-fixture app-config reset-args default-reset-fn))

([app-config reset-args reset-fn]
(fn [t]
(let [rt (StreamsResetter.)
app-id (get app-config "application.id")
args (concat ["--application-id" (get app-config "application.id")
"--bootstrap-servers" (get app-config "bootstrap.servers")]
reset-args)
result (with-open [out-str (java.io.StringWriter.)
err-str (java.io.StringWriter.)]
(binding [*out* out-str
*err* err-str]
(let [status (reset-fn rt args)]
(flush)
{:status status
:out (str out-str)
:err (str err-str)})))]
(if (zero? (:status result))
(t)
(throw (ex-info "failed to reset application. check logs for details"
result)))))))

(defn integration-fixture
[build-fn {:keys [broker-config
Expand Down
52 changes: 52 additions & 0 deletions test/jackdaw/test/fixtures_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,56 @@
(with-open [client (AdminClient/create kafka-config)]
(is (topic-exists? client topic-foo)))))

(defn test-resetter
""
{:style/indent 1}
[{:keys [app-config reset-params reset-fn]} assertion-fn]
(let [reset-args (atom [])
error-data (atom {})
test-fn (fn []
(is true "fake test function"))
fix-fn (reset-application-fixture app-config reset-params
(partial reset-fn reset-args))]

;; invoke the reset-application fixture with the sample test-fn
(try
(fix-fn test-fn)
(catch Exception e
(reset! error-data (ex-data e))))

(assertion-fn {:resetter (first @reset-args)
:reset-args (second @reset-args)
:error-data @error-data})))

(deftest test-reset-application-fixture
(test-resetter {:app-config {"application.id" "yolo"
"bootstrap.servers" "kafka.test:9092"}
:reset-params ["--foo" "foo"
"--bar" "bar"]
:reset-fn (fn [reset-args rt args]
(reset! reset-args [rt args])
0)}
(fn [{:keys [resetter reset-args error-data]}]
(is (instance? kafka.tools.StreamsResetter resetter))
(is (= ["--application-id" "yolo"
"--bootstrap-servers" "kafka.test:9092"
"--foo" "foo"
"--bar" "bar"]
reset-args))
(is (empty? error-data)))))

(deftest test-reset-application-fixture-failure
(test-resetter {:app-config {"application.id" "yolo"
"bootstrap.servers" "kafka.test:9092"}
:reset-params ["--foo" "foo"
"--bar" "bar"]
:reset-fn (fn [reset-args rt args]
(reset! reset-args [rt args])
(.write *err* "helpful error message\n")
(.write *out* "essential application info\n")
1)}
(fn [{:keys [resetter reset-args error-data]}]
(is (instance? kafka.tools.StreamsResetter resetter))
(is (= 1 (:status error-data)))
(is (= "helpful error message\n" (:err error-data)))
(is (= "essential application info\n" (:out error-data))))))

0 comments on commit 1474750

Please sign in to comment.