Skip to content

Commit

Permalink
Changing to connect everytime
Browse files Browse the repository at this point in the history
Since we had problems using this lib in bob, we decided to change the
behaviour to connect everytime you call invoke. The connect function is
now deprecated and only returns a map to avoid breaking the api. You
still are able to use client like before but it is discouraged now
to use connect and only using invoke is also sufficient.

Refers #20
  • Loading branch information
TimoKramer committed Mar 22, 2020
1 parent db8c81d commit 9793378
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
; You should have received a copy of the GNU Lesser General Public License
; along with clj-docker-client. If not, see <http://www.gnu.org/licenses/>.

(defproject lispyclouds/clj-docker-client "0.5.1"
(defproject lispyclouds/clj-docker-client "0.5.2"
:author "Rahul De <rahul@mailbox.org>"
:url "https://github.com/lispyclouds/clj-docker-client"
:description "An idiomatic data-driven clojure client for Docker."
Expand Down
36 changes: 28 additions & 8 deletions src/clj_docker_client/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
[^String message]
(throw (IllegalArgumentException. message)))

(defn connect
(defn- connect*
"Connects to the provided :uri in the connection options.
Optionally takes connect, read, write and call timeout in ms.
Expand All @@ -37,7 +37,7 @@
The url must be fully qualified with the protocol.
eg. unix:///var/run/docker.sock or https://my.docker.host:6375"
[{:keys [uri connect-timeout read-timeout write-timeout call-timeout]}]
[{:keys [uri timeouts]}]
(when (nil? uri)
(panic! ":uri is required"))
(let [uri (URI. uri)
Expand All @@ -49,13 +49,22 @@
(panic! (format "Protocol '%s' not supported yet." scheme)))
timeout-from #(Duration/ofMillis (or % 0))
builder+opts (-> builder
(.connectTimeout (timeout-from connect-timeout))
(.readTimeout (timeout-from read-timeout))
(.writeTimeout (timeout-from write-timeout))
(.callTimeout (timeout-from call-timeout)))]
(.connectTimeout (timeout-from (:connect-timeout timeouts)))
(.readTimeout (timeout-from (:read-timeout timeouts)))
(.writeTimeout (timeout-from (:write-timeout timeouts)))
(.callTimeout (timeout-from (:call-timeout timeouts))))]
{:client (.build builder+opts)
:socket socket}))

(defn ^:deprecated connect
"Deprecated but still there for compatibility reasons."
[{:keys [uri connect-timeout read-timeout write-timeout call-timeout]}]
(let [timeouts {:connect-timeout connect-timeout
:read-timeout read-timeout
:write-timeout write-timeout
:call-timeout call-timeout}]
{:uri uri :timeouts timeouts}))

(defn categories
"Returns the available categories.
Expand Down Expand Up @@ -130,7 +139,7 @@
(reduce (partial spec/gather-request-params
params)
{}))
response (req/fetch {:conn conn
response (req/fetch {:conn (connect* {:uri (:uri conn) :timeouts (:timeouts conn)})
:url (:path request-info)
:method (:method request-info)
:query query
Expand All @@ -155,6 +164,8 @@

(connect {:uri "unix:///var/run/docker.sock"})

(connect* {:uri "unix:///var/run/docker.sock"})

(connect {:uri "https://my.docker.host:6375"})

(req/fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
Expand Down Expand Up @@ -183,6 +194,11 @@
:write-timeout 0
:call-timeout 0}))

(def ping (client {:category :_ping
:conn conn}))

(invoke ping {:op :SystemPing})

(categories)

(categories "v1.40")
Expand All @@ -192,7 +208,11 @@
:api-version "v1.40"}))

(def images (client {:category :images
:conn conn}))
:conn {:uri "unix:///var/run/docker.sock"}}))

(invoke {:category :_ping
:conn {:uri "unix:///var/run/docker.sock"}}
{:op :SystemPing})

(ops images)

Expand Down
27 changes: 19 additions & 8 deletions test/clj_docker_client/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@
(ns clj-docker-client.core-test
(:require [clojure.test :refer :all]
[clojure.java.io :as io]
[clj-docker-client.core :refer :all]))
[clj-docker-client.core :refer :all])
(:import (clj_docker_client.socket TunnelingUnixSocket)
(okhttp3 OkHttpClient)))

(def latest-version "v1.40")

(deftest docker-connection
(testing "successful connection to the socket"
(testing "deprecated connect returns map"
(is (map? (connect {:uri "unix:///var/run/docker.sock"}))))

(testing "successful connection to the socket"
(let [priv-connect #'clj-docker-client.core/-connect]
(is (instance? clj_docker_client.socket.TunnelingUnixSocket
(:socket (priv-connect {:uri "unix:///var/run/docker.sock"}))))
(is (instance? okhttp3.OkHttpClient
(:client (priv-connect {:uri "unix:///var/run/docker.sock"}))))))

(testing "connection with usupported protocol"
(is (thrown? IllegalArgumentException
(connect {:uri "http://this-does-not-work"}))))
(let [connect #'clj-docker-client.core/-connect]
(is (thrown? IllegalArgumentException
(connect {:uri "http://this-does-not-work"})))))

(testing "connection with set timeouts"
(let [{:keys [client]} (connect {:uri "unix:///var/run/docker.sock"
:connect-timeout 10
:read-timeout 2000
:write-timeout 3000})]
(let [connect #'clj-docker-client.core/-connect
{:keys [client]} (connect {:uri "unix:///var/run/docker.sock"
:timeouts {:connect-timeout 10
:read-timeout 2000
:write-timeout 3000}})]
(is (and (= 10 (.connectTimeoutMillis client))
(= 2000 (.readTimeoutMillis client))
(= 3000 (.writeTimeoutMillis client))
Expand Down
25 changes: 14 additions & 11 deletions test/clj_docker_client/requests_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,21 @@

(deftest fetching-stuff
(testing "normal response"
(is (= "OK" (fetch {:conn (core/connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"}))))
(let [connect #'clj-docker-client.core/-connect]
(is (= "OK" (fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"})))))

(testing "streaming response"
(is (instance? InputStream
(fetch {:conn (core/connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"
:as :stream}))))
(let [connect #'clj-docker-client.core/-connect]
(is (instance? InputStream
(fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"
:as :stream})))))

(testing "exposing socket"
(let [socket (fetch {:conn (core/connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"
:as :socket})]
(is (instance? java.net.Socket socket))
(.close socket))))
(let [connect #'clj-docker-client.core/-connect]
(let [socket (fetch {:conn (connect {:uri "unix:///var/run/docker.sock"})
:url "/_ping"
:as :socket})]
(is (instance? java.net.Socket socket))
(.close socket)))))

0 comments on commit 9793378

Please sign in to comment.