From 9793378382a5d75f76d32dfff854d9e486ec9e8e Mon Sep 17 00:00:00 2001 From: Timo Kramer Date: Mon, 16 Mar 2020 18:21:54 +0100 Subject: [PATCH] Changing to connect everytime 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 https://github.com/lispyclouds/clj-docker-client/issues/20 --- project.clj | 2 +- src/clj_docker_client/core.clj | 36 ++++++++++++++++++------ test/clj_docker_client/core_test.clj | 27 ++++++++++++------ test/clj_docker_client/requests_test.clj | 25 ++++++++-------- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/project.clj b/project.clj index 28dcdfd..39e7321 100644 --- a/project.clj +++ b/project.clj @@ -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 . -(defproject lispyclouds/clj-docker-client "0.5.1" +(defproject lispyclouds/clj-docker-client "0.5.2" :author "Rahul De " :url "https://github.com/lispyclouds/clj-docker-client" :description "An idiomatic data-driven clojure client for Docker." diff --git a/src/clj_docker_client/core.clj b/src/clj_docker_client/core.clj index 411c664..5aebeed 100644 --- a/src/clj_docker_client/core.clj +++ b/src/clj_docker_client/core.clj @@ -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. @@ -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) @@ -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. @@ -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 @@ -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"}) @@ -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") @@ -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) diff --git a/test/clj_docker_client/core_test.clj b/test/clj_docker_client/core_test.clj index d2e6fff..720072c 100644 --- a/test/clj_docker_client/core_test.clj +++ b/test/clj_docker_client/core_test.clj @@ -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)) diff --git a/test/clj_docker_client/requests_test.clj b/test/clj_docker_client/requests_test.clj index 1a5d8cc..c042e75 100644 --- a/test/clj_docker_client/requests_test.clj +++ b/test/clj_docker_client/requests_test.clj @@ -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)))))