diff --git a/src/ductile/conn.clj b/src/ductile/conn.clj index 7855388..45c4a7e 100644 --- a/src/ductile/conn.clj +++ b/src/ductile/conn.clj @@ -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) diff --git a/src/ductile/index.clj b/src/ductile/index.clj index 000f666..ed3c972 100644 --- a/src/ductile/index.clj +++ b/src/ductile/index.clj @@ -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])) @@ -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" diff --git a/test/ductile/document_test.clj b/test/ductile/document_test.clj index 7a90d9c..4401c98 100644 --- a/test/ductile/document_test.clj +++ b/test/ductile/document_test.clj @@ -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) {}) @@ -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))) @@ -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)))) diff --git a/test/ductile/index_test.clj b/test/ductile/index_test.clj index 5217120..5ac7dae 100644 --- a/test/ductile/index_test.clj +++ b/test/ductile/index_test.clj @@ -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]] @@ -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)] diff --git a/test/ductile/test_helpers.clj b/test/ductile/test_helpers.clj index a9744ea..dd88ec6 100644 --- a/test/ductile/test_helpers.clj +++ b/test/ductile/test_helpers.clj @@ -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)