Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/ductile/conn.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,49 @@
(s/defn close [conn :- ESConn]
(-> conn :cm shutdown-manager))

(defn ^:private unauthorized-request
[res]
(log/warn "Unauthorized ES Request:" res)
(throw (ex-info "Unauthorized ES Request"
{:type ::unauthorized
:es-http-res res})))

(defn ^:private invalid-request
[res]
(log/warn "ES Invalid Request:" res)
(throw (ex-info "ES query parsing error"
{:type ::invalid-request
:es-http-res res})))

(defn ^:private unknown-error
[res]
(log/warn "ES Unknown Error:" res)
(throw (ex-info "ES Unknown Error"
{:type ::es-unknown-error
:es-http-res res})))


(defn safe-es-read [{:keys [status body]
:as res}]
(case (int status)
200 body
201 body
404 nil
401 (do (log/warn "Unauthorized ES Request:" res)
(throw (ex-info "Unauthorized ES Request"
{:type ::unauthorized
:es-http-res res})))
400 (do (log/warn "ES Invalid Request:" res)
(throw (ex-info "ES query parsing error"
{:type ::invalid-request
:es-http-res res})))
(do (log/warn "ES Unknown Error:" res)
(throw (ex-info "ES Unknown Error"
{:type ::es-unknown-error
:es-http-res res})))))
401 (unauthorized-request res)
400 (invalid-request res)
(unknown-error res)))

(defn safe-es-read-head
"For use with HEAD requests. As opposed to safe-es-read, returns the status
instead of body or nil."
[{:keys [status]
:as res}]
(case (int status)
200 status
404 status
401 (unauthorized-request res)
400 (invalid-request res)
(unknown-error res)))

(defn safe-es-bulk-read [body]
(if (:errors body)
Expand Down
14 changes: 7 additions & 7 deletions src/ductile/index.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns ductile.index
(:refer-clojure :exclude [get])
(:require [cemerick.uri :as uri]
[ductile.conn :refer [make-http-opts safe-es-read]]
[ductile.conn :refer [make-http-opts safe-es-read safe-es-read-head]]
[ductile.schemas :refer [ESConn RolloverConditions CatIndices]]
[schema.core :as s]
[schema-tools.core :as st]))
Expand Down Expand Up @@ -47,12 +47,12 @@
"check if the supplied ES index exists"
[{:keys [uri request-fn] :as conn} :- ESConn
index-name :- s/Str]
(not= 404
(-> (make-http-opts conn {})
(assoc :method :head
:url (index-uri uri index-name))
request-fn
:status)))
(= 200
(-> (make-http-opts conn {})
(assoc :method :head
:url (index-uri uri index-name))
request-fn
safe-es-read-head)))

(s/defn create!
"create an index"
Expand Down
11 changes: 3 additions & 8 deletions test/ductile/document_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@
:foo "bar is a lie"
:test_value 42}
doc-type (if (= version 5) "test-type" "_doc")
sample-docs
(repeatedly 10
#(hash-map :id (str (UUID/randomUUID))
:_index indexname
:bar "foo"
:_type doc-type))

get-doc (fn [doc-id opts]
(sut/get-doc conn indexname doc-type doc-id opts))
get-sample-doc #(get-doc (:id sample-doc) {})
Expand All @@ -180,7 +175,7 @@
delete-doc (fn [doc-id opts]
(sut/delete-doc conn indexname doc-type doc-id opts))

update-doc (fn [doc-id doc opts]
update-doc (fn [doc-id doc _opts]
(sut/update-doc conn indexname doc-type doc-id doc {}))]
(testing "create-doc and get-doc"
(is (nil? (get-sample-doc)))
Expand Down Expand Up @@ -317,7 +312,7 @@
(deftest format-bulk-res-test
(let [bulk-res-errors (rand-bulk-response 2 true)
bulk-res-ok (rand-bulk-response 4 false)
check-fn (fn [{:keys [msg bulk-res-list nb-items errors?]}]
check-fn (fn [{:keys [_msg bulk-res-list nb-items errors?]}]
(let [{:keys [took errors items]}
(sut/format-bulk-res (shuffle bulk-res-list))]
(is (= took (* 3 (count bulk-res-list))))
Expand Down
23 changes: 22 additions & 1 deletion test/ductile/index_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns ductile.index-test
(:require [clojure.test :refer [deftest is testing use-fixtures]]
[ductile.conn :as es-conn]
[ductile.document :as es-doc]
[ductile.index :as sut]
[ductile.test-helpers :refer [for-each-es-version]]
Expand Down Expand Up @@ -34,12 +35,32 @@
"http://127.0.0.1/test/_rollover/test2"))))

(deftest refresh-uri-test
(testing "should generat a proper refresh URI"
(testing "should generate a proper refresh URI"
(is (= (sut/refresh-uri "http://127.0.0.1" "test-index")
"http://127.0.0.1/test-index/_refresh"))
(is (= (sut/refresh-uri "http://127.0.0.1" nil)
"http://127.0.0.1/_refresh"))))

(deftest index-exists?-test
(letfn [(make-conn [status] {:host "localhost"
:port 9200
:request-fn (fn [_req]
{:status status
:headers {:content-type "application/clojure"}})})]
(testing "should return false unless the status is 200"
(is (= false
(sut/index-exists? (es-conn/connect (make-conn 404))
"test_index")))

(is (= true
(sut/index-exists? (es-conn/connect (make-conn 200))
"test_index")))

(is (thrown?
clojure.lang.ExceptionInfo
(sut/index-exists? (es-conn/connect (make-conn 401))
"test_index"))))))

(deftest ^:integration index-crud-ops
(let [indexname "test_index"
indexkw (keyword indexname)]
Expand Down
11 changes: 6 additions & 5 deletions test/ductile/test_helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
(seq auth-opts) (assoc :auth auth-opts)
:finally es-conn/connect))

(defmacro for-each-es-version [msg clean & body]
(defmacro for-each-es-version
"for each ES version:
- init an ES connection
- expose anaphoric `version` and `conn` to use in body
- wrap body with a `testing` block with with `msg` formatted with `version`
- call `clean` fn if not `nil` before and after body."
- init an ES connection
- expose anaphoric `version` and `conn` to use in body
- wrap body with a `testing` block with with `msg` formatted with `version`
- call `clean` fn if not `nil` before and after body."
[msg clean & body]
{:style/indent 2}
`(doseq [~'version [5 7]]
(let [~'conn (connect ~'version basic-auth-opts)
Expand Down